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