| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package term |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"math/rand" |
| 13 |
"os" |
| 14 |
"strconv" |
| 15 |
"sync" |
| 16 |
"syscall" |
| 17 |
"testing" |
| 18 |
) |
| 19 |
|
| 20 |
var pty *PTY |
| 21 |
var tios Termios |
| 22 |
var wz *Winsize |
| 23 |
|
| 24 |
|
| 25 |
const NUMTEST = 100 |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
func donormfile(input string) (*os.File, error) { |
| 30 |
tdir := os.TempDir() |
| 31 |
pid := os.Getpid() |
| 32 |
nf, err := os.Create(tdir + "/" + "s2test-" + strconv.Itoa(pid)) |
| 33 |
if err != nil { |
| 34 |
return nil, err |
| 35 |
} |
| 36 |
os.Remove(tdir + "/" + "s2test-" + strconv.Itoa(pid)) |
| 37 |
if _, err = nf.Write([]byte(input)); err != nil { |
| 38 |
return nil, err |
| 39 |
} |
| 40 |
return nf, nil |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func testraw(tr Termios, f string) error { |
| 45 |
if (tr.Iflag & (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON)) != 0 { |
| 46 |
return fmt.Errorf("%q Raw failed setting c_iflag , got: %d want: 0", f, tr.Iflag) |
| 47 |
} |
| 48 |
if (tr.Oflag & OPOST) != 0 { |
| 49 |
return fmt.Errorf("%q Raw failed setting Oflag , got: %d want: 0", f, tr.Oflag) |
| 50 |
} |
| 51 |
if (tr.Lflag & (ECHO | ECHONL | ICANON | ISIG | IEXTEN)) != 0 { |
| 52 |
return fmt.Errorf("%q Raw failed setting Lflag, got: %d want: 0", f, tr.Lflag) |
| 53 |
} |
| 54 |
if (tr.Cflag & (PARENB)) != 0 { |
| 55 |
return fmt.Errorf("%q Raw failed setting Cflag , got: %d want: 0", f, tr.Cflag) |
| 56 |
} |
| 57 |
if (tr.Cflag & CSIZE) != CS8 { |
| 58 |
return fmt.Errorf("%q Raw failed setting Cflag CS8, got: %d ", f, tr.Cflag) |
| 59 |
} |
| 60 |
if !(tr.Cc[VMIN] == 1 && tr.Cc[VTIME] == 0) { |
| 61 |
return fmt.Errorf("%q Raw failed setting Cc, got: %d want: 0", f, tr.Cc) |
| 62 |
} |
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func testcook(tr Termios, f string) error { |
| 68 |
if tr.Iflag&(BRKINT|IGNPAR|ISTRIP|ICRNL|IXON) != BRKINT+IGNPAR+ISTRIP+ICRNL+IXON { |
| 69 |
return fmt.Errorf("%q Cook failed setting Iflag , got: %d want: %d", f, tr.Iflag, BRKINT+IGNPAR+ISTRIP+ICRNL+IXON) |
| 70 |
} |
| 71 |
if (tr.Oflag & OPOST) != OPOST { |
| 72 |
return fmt.Errorf("%q Cook failed setting Oflag , got: %d want: %d", f, tr.Oflag, OPOST) |
| 73 |
} |
| 74 |
if (tr.Lflag & (ISIG | ICANON)) != ISIG+ICANON { |
| 75 |
return fmt.Errorf("%q Cook failed setting Lflag , got: %d want: %d", f, tr.Lflag, ECHO+ECHONL+ICANON+ISIG+IEXTEN) |
| 76 |
} |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func TestOpenPTY(t *testing.T) { |
| 82 |
var err error |
| 83 |
if pty, err = OpenPTY(); err != nil { |
| 84 |
t.Fatal("Openpty failed: ", err) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func TestClose(t *testing.T) { |
| 90 |
tty, err := OpenPTY() |
| 91 |
if err != nil { |
| 92 |
t.Fatalf("OpenPTY failed: %v", err) |
| 93 |
} |
| 94 |
fd := tty.Slave.Fd() |
| 95 |
|
| 96 |
if err = syscall.Close(int(fd)); err != nil { |
| 97 |
t.Fatalf("Closing PTY slave failed: %v", err) |
| 98 |
} |
| 99 |
if err = tty.Close(); err == nil { |
| 100 |
t.Fatal("Close() already closed FD want: fail got: <nil>") |
| 101 |
} |
| 102 |
t.Logf("Closing of half closed PTY resulted in err: %v", err) |
| 103 |
|
| 104 |
if tty, err = OpenPTY(); err != nil { |
| 105 |
t.Fatalf("OpenPTY failed: %v", err) |
| 106 |
} |
| 107 |
if err = tty.Close(); err != nil { |
| 108 |
t.Fatalf("Closing PTY failed, want: <nil> got: %v", err) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func TestIsatty(t *testing.T) { |
| 114 |
f, err := donormfile("TestIsatty") |
| 115 |
if err != nil { |
| 116 |
t.Fatalf("donormfile(\"TestIsatty\") failed: %v", err) |
| 117 |
} |
| 118 |
if Isatty(f) { |
| 119 |
t.Errorf("Isatty for normal file %v got: true want: false", f) |
| 120 |
} |
| 121 |
if !Isatty(pty.Slave) { |
| 122 |
t.Errorf("Isatty for tty file %v got: false want: true", pty.Slave) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
func TestGetPass(t *testing.T) { |
| 128 |
f, err := donormfile("TestGetPass") |
| 129 |
if err != nil { |
| 130 |
t.Fatalf("donormfile(\"TestGetPass\") failed: %v", err) |
| 131 |
} |
| 132 |
buf := make([]byte, 512) |
| 133 |
if _, err := GetPass("TestGetPass:", f, buf); err == nil { |
| 134 |
t.Errorf("GetPass(\"TestGetPass\",buf) got: <nil> want: file %s not a tty.", f.Name()) |
| 135 |
} |
| 136 |
pty, err := OpenPTY() |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("OpenPTY() failed: %v", err) |
| 139 |
} |
| 140 |
|
| 141 |
var mu sync.Mutex |
| 142 |
var readbuffer bytes.Buffer |
| 143 |
go func() { |
| 144 |
b := make([]byte, 512) |
| 145 |
for { |
| 146 |
nr, err := pty.Master.Read(b) |
| 147 |
if err != nil { |
| 148 |
break |
| 149 |
} |
| 150 |
mu.Lock() |
| 151 |
readbuffer.Write(b[:nr]) |
| 152 |
mu.Unlock() |
| 153 |
} |
| 154 |
}() |
| 155 |
tststring := "SuperSecret\n" |
| 156 |
tstWriter := func(in string) { |
| 157 |
w := []byte(in) |
| 158 |
var err error |
| 159 |
for tot, nr := 0, 0; tot < len(w); tot += nr { |
| 160 |
if nr, err = pty.Master.Write(w[tot:]); err != nil { |
| 161 |
break |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
go tstWriter(tststring) |
| 166 |
|
| 167 |
pass, err := GetPass("TestGetPass:", pty.Slave, buf) |
| 168 |
if err != nil { |
| 169 |
t.Errorf("GetPass(\"TestGetPass:\",pty.Slave,buf) failed: %v", err) |
| 170 |
} |
| 171 |
if string(pass) != tststring[:len(tststring)-1] { |
| 172 |
t.Errorf("GetPass got: %q want: %q", pass, tststring) |
| 173 |
} |
| 174 |
mu.Lock() |
| 175 |
if readbuffer.String() != "TestGetPass:" { |
| 176 |
t.Errorf("GetPass got: %q want: %q", readbuffer.String(), "TestGetPass:") |
| 177 |
} |
| 178 |
readbuffer.Reset() |
| 179 |
mu.Unlock() |
| 180 |
sbuf := buf[:10] |
| 181 |
tststring = "SuperSuperSuperSecret\n" |
| 182 |
go tstWriter(tststring) |
| 183 |
if _, err := GetPass("Pass: ", pty.Slave, sbuf); err == nil { |
| 184 |
t.Errorf("GetPass should fail got: <nil> want: ran out of buffespace") |
| 185 |
} |
| 186 |
|
| 187 |
for i := 0; i < len(sbuf); i++ { |
| 188 |
if sbuf[i] != 0 { |
| 189 |
t.Errorf("GetPass should clear buffer on errors got: %q want: \"\"", sbuf) |
| 190 |
break |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
func TestGetChar(t *testing.T) { |
| 197 |
pty, err := OpenPTY() |
| 198 |
if err != nil { |
| 199 |
t.Fatalf("OpenPTY failed: %v", err) |
| 200 |
} |
| 201 |
tstWrite := func(in string) { |
| 202 |
w := []byte(in) |
| 203 |
var err error |
| 204 |
for tot, nr := 0, 0; tot < len(w); tot += nr { |
| 205 |
if nr, err = pty.Master.Write(w[tot:]); err != nil { |
| 206 |
break |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
tstring := "TestTheReader" |
| 211 |
go tstWrite(tstring) |
| 212 |
for idx, c := range []byte(tstring) { |
| 213 |
gc, err := pty.GetChar() |
| 214 |
if err != nil { |
| 215 |
t.Errorf("GetChar failed: %v", err) |
| 216 |
} |
| 217 |
if gc != c { |
| 218 |
t.Errorf("char at idx: %d does not match got: %b want: %b", idx, gc, c) |
| 219 |
} |
| 220 |
} |
| 221 |
tstIface := func(cr io.ByteReader, cmp string) error { |
| 222 |
for idx, c := range []byte(cmp) { |
| 223 |
gc, err := cr.ReadByte() |
| 224 |
if err != nil { |
| 225 |
t.Errorf("ReadByte failed: %v", err) |
| 226 |
return err |
| 227 |
} |
| 228 |
if gc != c { |
| 229 |
t.Errorf("char at idx: %d does not match got: %b want: %b", idx, gc, c) |
| 230 |
} |
| 231 |
} |
| 232 |
return nil |
| 233 |
} |
| 234 |
tstring = "TestTheByteReader" |
| 235 |
go tstWrite(tstring) |
| 236 |
if err := tstIface(pty, tstring); err != nil { |
| 237 |
t.Errorf("ByteReader failed: %v", err) |
| 238 |
} |
| 239 |
pty.Close() |
| 240 |
if _, err := pty.GetChar(); err == nil { |
| 241 |
t.Error("Reading from a closed PTY should fail want: io Error got: nil") |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
func TestPTSName(t *testing.T) { |
| 247 |
name, err := pty.PTSName() |
| 248 |
if err != nil { |
| 249 |
t.Error("PTS_name failed: ", err) |
| 250 |
} |
| 251 |
fi, err := os.Stat(name) |
| 252 |
if err != nil { |
| 253 |
t.Errorf("PTS_name failed to open slave named: %q err: %v", name, err) |
| 254 |
} |
| 255 |
mode := fi.Mode() |
| 256 |
if mode&os.ModeCharDevice == 0 { |
| 257 |
t.Errorf("PTS_name failed slave: %q is not a char device", name) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
func TestWinsz(t *testing.T) { |
| 264 |
nf, err := donormfile("TestGetwinsz") |
| 265 |
if err != nil { |
| 266 |
t.Fatal("TestWinsz Could not create testfile, err: ", err) |
| 267 |
} |
| 268 |
defer nf.Close() |
| 269 |
wz = &tios.Wz |
| 270 |
|
| 271 |
|
| 272 |
wz.WsRow, wz.WsCol = 255, 255 |
| 273 |
if err = tios.Winsz(pty.Slave); err != nil { |
| 274 |
t.Error("Winsz failed: ", err) |
| 275 |
} |
| 276 |
if wz.WsRow == 255 && wz.WsCol == 255 { |
| 277 |
wz.WsRow, wz.WsCol = 123, 124 |
| 278 |
if err = tios.Winsz(pty.Slave); err != nil { |
| 279 |
t.Error("Getwinsz failed: ", err) |
| 280 |
} |
| 281 |
if wz.WsRow == 123 && wz.WsCol == 124 { |
| 282 |
t.Error("Winsz is not reading in values") |
| 283 |
} |
| 284 |
} |
| 285 |
if err = tios.Winsz(nf); err == nil { |
| 286 |
t.Error("Should not be able to read Windowsize from non pty") |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
func TestSetwinsz(t *testing.T) { |
| 292 |
nf, err := donormfile("TestSetwinsz") |
| 293 |
if err != nil { |
| 294 |
t.Fatal("TestSetwinsz Could not create testfile, err: ", err) |
| 295 |
} |
| 296 |
defer nf.Close() |
| 297 |
wz = &tios.Wz |
| 298 |
var i, y uint16 |
| 299 |
for i = 0; i < NUMTEST; i++ { |
| 300 |
for y = 0; y < NUMTEST; y++ { |
| 301 |
wz.WsRow = i |
| 302 |
wz.WsCol = y |
| 303 |
wz.WsXpixel = uint16(rand.Int()) |
| 304 |
wz.WsYpixel = uint16(rand.Int()) |
| 305 |
xp, yp := wz.WsXpixel, wz.WsYpixel |
| 306 |
if err = tios.Setwinsz(pty.Slave); err != nil { |
| 307 |
t.Errorf("Setwinsz could not set row: %d col: %d err: %v", i, y, err) |
| 308 |
t.Errorf("Setwinsz could not set x: %d y: %d err: %v", xp, yp, err) |
| 309 |
} |
| 310 |
if err = tios.Winsz(pty.Slave); err != nil { |
| 311 |
t.Error("Winsz failed: ", err) |
| 312 |
} |
| 313 |
if !(wz.WsRow == i && wz.WsCol == y && wz.WsXpixel == xp && wz.WsYpixel == yp) { |
| 314 |
t.Errorf("Setwinsz got row: %d col: %d want row: %d col: %d", i, y, wz.WsRow, wz.WsCol) |
| 315 |
t.Errorf("Setwinsz got x: %d y: %d want x: %d y: %d", xp, yp, wz.WsXpixel, wz.WsYpixel) |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
if err = tios.Setwinsz(nf); err == nil { |
| 320 |
t.Error("Setwinsz should not work for std. file") |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
func TestTraw(t *testing.T) { |
| 326 |
tios.Raw() |
| 327 |
if err := testraw(tios, "TestRaw"); err != nil { |
| 328 |
t.Errorf("TestTraw failed: %v", err) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
func TestTset(t *testing.T) { |
| 334 |
tios.Raw() |
| 335 |
err := tios.Set(pty.Slave) |
| 336 |
if err != nil { |
| 337 |
t.Fatal("Tset failed: ", err) |
| 338 |
} |
| 339 |
if err := testraw(tios, "TestTset"); err != nil { |
| 340 |
t.Errorf("TestTset failed: %v", err) |
| 341 |
} |
| 342 |
nf, err := donormfile("TestTset") |
| 343 |
if err != nil { |
| 344 |
t.Fatal("Could not create testfile: ", err) |
| 345 |
} |
| 346 |
defer nf.Close() |
| 347 |
err = tios.Set(nf) |
| 348 |
if err == nil { |
| 349 |
t.Error("Should not be able to set attributes on regular file: ", nf.Name()) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
func TestTattr(t *testing.T) { |
| 355 |
tios.Raw() |
| 356 |
|
| 357 |
err := tios.Set(pty.Slave) |
| 358 |
if err != nil { |
| 359 |
t.Fatal("Tattr failed: ", err) |
| 360 |
} |
| 361 |
|
| 362 |
tios, err = Attr(pty.Slave) |
| 363 |
if err != nil { |
| 364 |
t.Fatal("Ttattr failed: ", err) |
| 365 |
} |
| 366 |
|
| 367 |
if err := testraw(tios, "TestTattr"); err != nil { |
| 368 |
t.Errorf("TestTattr failed: %v", err) |
| 369 |
} |
| 370 |
|
| 371 |
tios.Cook() |
| 372 |
if err = tios.Set(pty.Slave); err != nil { |
| 373 |
t.Fatal("Failed to set Cook attributes") |
| 374 |
} |
| 375 |
if err := testcook(tios, "TestTattr"); err != nil { |
| 376 |
t.Errorf("TestTattr failed: %v", err) |
| 377 |
} |
| 378 |
|
| 379 |
nf, err := donormfile("TestTattr") |
| 380 |
if err != nil { |
| 381 |
t.Fatal("TestTattr failed to create testfile, err: ", err) |
| 382 |
} |
| 383 |
defer nf.Close() |
| 384 |
err = tios.Set(nf) |
| 385 |
if err == nil { |
| 386 |
t.Error("Tattr , should not be able to set attributes on regular file: ", nf.Name()) |
| 387 |
} |
| 388 |
tios, err = Attr(nf) |
| 389 |
if err == nil { |
| 390 |
t.Error("Tattr, should not be able to get attributes from regular file: ", nf.Name()) |
| 391 |
} |
| 392 |
} |