forked from kata-containers/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal_linux_test.go
63 lines (51 loc) · 2.06 KB
/
terminal_linux_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// +build linux
//
// Copyright (c) 2017 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
)
const (
RawModeErr = "should be properly set in raw mode"
FlagRawModeErr = "flag " + RawModeErr
ValueRawModeErr = "value " + RawModeErr
)
func newTestTerminal(t *testing.T) (*os.File, error) {
if os.Getuid() != 0 {
t.Skip("Skipping this test: Requires to be root")
return nil, nil
}
return os.OpenFile("/dev/tty", os.O_RDWR, os.ModeDevice)
}
func TestSetupTerminalOnNonTerminalFailure(t *testing.T) {
file, err := ioutil.TempFile("", "tmp")
assert.Nil(t, err, "Failed to create temporary file")
defer file.Close()
_, err = setupTerminal(int(file.Fd()))
assert.NotNil(t, err, "Should fail because the file is not a terminal")
}
func TestSetupTerminalSuccess(t *testing.T) {
file, err := newTestTerminal(t)
assert.Nil(t, err, "Failed to create terminal")
defer file.Close()
savedTermios, err := setupTerminal(int(file.Fd()))
assert.Nil(t, err, "Should not fail because the file is a terminal")
termios, err := unix.IoctlGetTermios(int(file.Fd()), unix.TCGETS)
assert.Nil(t, err, "Failed to get terminal information")
assert.True(t, (termios.Iflag&termiosIFlagRawTermInvMask) == 0, "Termios I %s", FlagRawModeErr)
assert.True(t, (termios.Oflag&termiosOFlagRawTermInvMask) == 0, "Termios O %s", FlagRawModeErr)
assert.True(t, (termios.Lflag&termiosLFlagRawTermInvMask) == 0, "Termios L %s", FlagRawModeErr)
assert.True(t, (termios.Cflag&termiosCFlagRawTermInvMask) == 0, "Termios C %s", FlagRawModeErr)
assert.True(t, (termios.Cflag&termiosCFlagRawTermMask) == termiosCFlagRawTermMask, "Termios C %s", FlagRawModeErr)
assert.True(t, termios.Cc[unix.VMIN] == termiosCcVMinRawTermVal, "Termios CC VMIN %s", ValueRawModeErr)
assert.True(t, termios.Cc[unix.VTIME] == termiosCcVTimeRawTermVal, "Termios CC VTIME %s", ValueRawModeErr)
err = restoreTerminal(int(file.Fd()), savedTermios)
assert.Nil(t, err, "Terminal should be properly restored")
}