github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/termios/termios_test.go (about) 1 // Copyright 2015-2017 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build !plan9 6 7 // Package termios implements basic termios operations including getting 8 // a termio struct, a winsize struct, and setting raw mode. 9 // To set raw mode and then restore, one can do: 10 // t, err := termios.Raw() 11 // do things 12 // t.Set() 13 package termios 14 15 import ( 16 "encoding/json" 17 "os" 18 "reflect" 19 "strings" 20 "testing" 21 22 "github.com/u-root/u-root/pkg/testutil" 23 ) 24 25 func TestNew(t *testing.T) { 26 if _, err := New(); os.IsNotExist(err) { 27 t.Skipf("No /dev/tty here.") 28 } else if err != nil { 29 t.Errorf("TestNew: want nil, got %v", err) 30 } 31 32 } 33 34 func TestRaw(t *testing.T) { 35 // TestRaw no longer works in CircleCi, Restrict to only VM tests. 36 testutil.SkipIfNotRoot(t) 37 tty, err := New() 38 if os.IsNotExist(err) { 39 t.Skipf("No /dev/tty here.") 40 } else if err != nil { 41 t.Fatalf("TestRaw new: want nil, got %v", err) 42 } 43 term, err := tty.Get() 44 if err != nil { 45 t.Fatalf("TestRaw get: want nil, got %v", err) 46 } 47 48 n, err := tty.Raw() 49 if err != nil { 50 t.Fatalf("TestRaw raw: want nil, got %v", err) 51 } 52 if !reflect.DeepEqual(term, n) { 53 t.Fatalf("TestRaw: New(%v) and Raw(%v) should be equal, are not", t, n) 54 } 55 if err := tty.Set(n); err != nil { 56 t.Fatalf("TestRaw restore mode: want nil, got %v", err) 57 } 58 n, err = tty.Get() 59 if err != nil { 60 t.Fatalf("TestRaw second call to New(): want nil, got %v", err) 61 } 62 if !reflect.DeepEqual(term, n) { 63 t.Fatalf("TestRaw: After Raw restore: New(%v) and check(%v) should be equal, are not", term, n) 64 } 65 } 66 67 // Test proper unmarshaling and consistent, repeatable output from String() 68 func TestString(t *testing.T) { 69 // This JSON is from a real device. 70 j := `{ 71 "Ispeed": 0, 72 "Ospeed": 0, 73 "Row": 72, 74 "Col": 238, 75 "CC": { 76 "eof": 4, 77 "eol": 255, 78 "eol2": 255, 79 "erase": 127, 80 "intr": 3, 81 "kill": 21, 82 "lnext": 22, 83 "min": 0, 84 "quit": 28, 85 "start": 17, 86 "stop": 19, 87 "susp": 26, 88 "werase": 23, 89 "time": 3 90 }, 91 "Opts": { 92 "xcase": false, 93 "brkint": true, 94 "clocal": false, 95 "cread": true, 96 "cstopb": false, 97 "echo": true, 98 "echoctl": true, 99 "echoe": true, 100 "echok": true, 101 "echoke": true, 102 "echonl": false, 103 "echoprt": false, 104 "flusho": false, 105 "hupcl": false, 106 "icanon": true, 107 "icrnl": true, 108 "iexten": true, 109 "ignbrk": false, 110 "igncr": false, 111 "ignpar": true, 112 "imaxbel": true, 113 "inlcr": false, 114 "inpck": false, 115 "isig": true, 116 "istrip": false, 117 "iuclc": false, 118 "iutf8": true, 119 "ixany": false, 120 "ixoff": false, 121 "ixon": true, 122 "noflsh": false, 123 "ocrnl": false, 124 "ofdel": false, 125 "ofill": false, 126 "olcuc": false, 127 "onlcr": true, 128 "onlret": false, 129 "onocr": false, 130 "opost": true, 131 "parenb": false, 132 "parmrk": false, 133 "parodd": false, 134 "pendin": true, 135 "tostop": false 136 } 137 }` 138 s := `speed:0 rows:72 cols:238 brkint:1 clocal:0 cread:1 cstopb:0 echo:1 echoctl:1 echoe:1 echok:1 echoke:1 echonl:0 echoprt:0 eof:0x04 eol2:0xff eol:0xff erase:0x7f flusho:0 hupcl:0 icanon:1 icrnl:1 iexten:1 ignbrk:0 igncr:0 ignpar:1 imaxbel:1 inlcr:0 inpck:0 intr:0x03 isig:1 istrip:0 iuclc:0 iutf8:1 ixany:0 ixoff:0 ixon:1 kill:0x15 lnext:0x16 min:0x00 noflsh:0 ocrnl:0 ofdel:0 ofill:0 olcuc:0 onlcr:1 onlret:0 onocr:0 opost:1 parenb:0 parmrk:0 parodd:0 pendin:1 quit:0x1c start:0x11 stop:0x13 susp:0x1a time:0x03 tostop:0 werase:0x17 xcase:0` 139 g := &TTY{} 140 if err := json.Unmarshal([]byte(j), g); err != nil { 141 t.Fatalf("stty load: %v", err) 142 } 143 144 if g.String() != s { 145 t.Errorf("GTTY: want '%v', got '%v'", s, g.String()) 146 as := strings.Split(s, " ") 147 ag := strings.Split(g.String(), " ") 148 if len(as) != len(ag) { 149 t.Fatalf("Wrong # elements in gtty: want %d, got %d", len(as), len(ag)) 150 } 151 for i := range as { 152 t.Errorf("want %s got %s Same %v", as[i], ag[i], as[i] == ag[i]) 153 } 154 155 } 156 157 }