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"
|
|
|
|
|
2017-12-22 16:30:49 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-05 17:31:50 -04:00
|
|
|
defer tty.Close()
|
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-12-22 16:30:49 -05:00
|
|
|
|
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)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, *winSize, newSize, cmpWinsize)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|
|
|
|
|
2017-12-22 16:30:49 -05:00
|
|
|
var cmpWinsize = cmp.AllowUnexported(Winsize{})
|
|
|
|
|
2017-06-05 07:57:01 -04:00
|
|
|
func TestSetWinsize(t *testing.T) {
|
|
|
|
tty, err := newTtyForTest(t)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-05 17:31:50 -04:00
|
|
|
defer tty.Close()
|
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)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, *winSize, newSize, cmpWinsize)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFdInfo(t *testing.T) {
|
|
|
|
tty, err := newTtyForTest(t)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-05 17:31:50 -04:00
|
|
|
defer tty.Close()
|
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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-05 17:31:50 -04:00
|
|
|
defer tty.Close()
|
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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-05 17:31:50 -04:00
|
|
|
defer tty.Close()
|
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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-05 17:31:50 -04:00
|
|
|
defer tty.Close()
|
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
|
|
|
}
|