2017-06-05 07:57:01 -04:00
|
|
|
//+build linux
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package term // import "github.com/docker/docker/pkg/term"
|
2017-06-05 07:57:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2017-06-05 07:57:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// RequiresRoot skips tests that require root, unless the test.root flag has
|
|
|
|
// been set
|
|
|
|
func RequiresRoot(t *testing.T) {
|
2017-07-05 17:15:59 -04:00
|
|
|
if os.Getuid() != 0 {
|
2017-06-05 07:57:01 -04:00
|
|
|
t.Skip("skipping test that requires root")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTtyForTest(t *testing.T) (*os.File, error) {
|
|
|
|
RequiresRoot(t)
|
|
|
|
return os.OpenFile("/dev/tty", os.O_RDWR, os.ModeDevice)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTempFile() (*os.File, error) {
|
|
|
|
return ioutil.TempFile(os.TempDir(), "temp")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWinsize(t *testing.T) {
|
|
|
|
tty, err := newTtyForTest(t)
|
|
|
|
defer tty.Close()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
winSize, err := GetWinsize(tty.Fd())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, winSize != nil)
|
|
|
|
assert.Assert(t, winSize.Height != nil)
|
|
|
|
assert.Assert(t, winSize.Width != nil)
|
2017-06-05 07:57:01 -04:00
|
|
|
newSize := Winsize{Width: 200, Height: 200, x: winSize.x, y: winSize.y}
|
|
|
|
err = SetWinsize(tty.Fd(), &newSize)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
winSize, err = GetWinsize(tty.Fd())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, *winSize, newSize)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetWinsize(t *testing.T) {
|
|
|
|
tty, err := newTtyForTest(t)
|
|
|
|
defer tty.Close()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
winSize, err := GetWinsize(tty.Fd())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, winSize != nil)
|
2017-06-05 07:57:01 -04:00
|
|
|
newSize := Winsize{Width: 200, Height: 200, x: winSize.x, y: winSize.y}
|
|
|
|
err = SetWinsize(tty.Fd(), &newSize)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
winSize, err = GetWinsize(tty.Fd())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, *winSize, newSize)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFdInfo(t *testing.T) {
|
|
|
|
tty, err := newTtyForTest(t)
|
|
|
|
defer tty.Close()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
inFd, isTerminal := GetFdInfo(tty)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, inFd, tty.Fd())
|
|
|
|
assert.Equal(t, isTerminal, true)
|
2017-06-05 07:57:01 -04:00
|
|
|
tmpFile, err := newTempFile()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
defer tmpFile.Close()
|
|
|
|
inFd, isTerminal = GetFdInfo(tmpFile)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, inFd, tmpFile.Fd())
|
|
|
|
assert.Equal(t, isTerminal, false)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsTerminal(t *testing.T) {
|
|
|
|
tty, err := newTtyForTest(t)
|
|
|
|
defer tty.Close()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
isTerminal := IsTerminal(tty.Fd())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, isTerminal, true)
|
2017-06-05 07:57:01 -04:00
|
|
|
tmpFile, err := newTempFile()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
defer tmpFile.Close()
|
|
|
|
isTerminal = IsTerminal(tmpFile.Fd())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, isTerminal, false)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveState(t *testing.T) {
|
|
|
|
tty, err := newTtyForTest(t)
|
|
|
|
defer tty.Close()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
state, err := SaveState(tty.Fd())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, state != nil)
|
2017-06-05 07:57:01 -04:00
|
|
|
tty, err = newTtyForTest(t)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
defer tty.Close()
|
|
|
|
err = RestoreTerminal(tty.Fd(), state)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisableEcho(t *testing.T) {
|
|
|
|
tty, err := newTtyForTest(t)
|
|
|
|
defer tty.Close()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
state, err := SetRawTerminal(tty.Fd())
|
2017-07-05 17:15:59 -04:00
|
|
|
defer RestoreTerminal(tty.Fd(), state)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, state != nil)
|
2017-06-05 07:57:01 -04:00
|
|
|
err = DisableEcho(tty.Fd(), state)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|