gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/pty/pty_test.go (about) 1 // Copyright 2015-2020 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 // This test is flaky AF under the race detector. 6 7 // +build !race 8 9 package pty 10 11 import ( 12 "encoding/json" 13 "os" 14 "reflect" 15 "testing" 16 ) 17 18 func TestNew(t *testing.T) { 19 if _, err := New(); os.IsNotExist(err) { 20 t.Skipf("Failed allocate /dev/pts device") 21 } else if err != nil { 22 t.Errorf("New pty: want nil, got %v", err) 23 } 24 } 25 26 func TestRunRestoreTTYMode(t *testing.T) { 27 p, err := New() 28 if os.IsNotExist(err) { 29 t.Skipf("Failed to allocate /dev/pts device") 30 } else if err != nil { 31 t.Fatalf("TestStart New pty: want nil, got %v", err) 32 } 33 34 p.Command("echo", "hi") 35 if err := p.Start(); err != nil { 36 t.Fatalf("TestStart Start: want nil, got %v", err) 37 } 38 if err := p.Wait(); err != nil { 39 t.Error(err) 40 } 41 ti, err := p.TTY.Get() 42 if err != nil { 43 t.Fatalf("TestStart Get: want nil, got %v", err) 44 } 45 if !reflect.DeepEqual(ti, p.Restorer) { 46 tt, err := json.Marshal(ti) 47 if err != nil { 48 t.Fatalf("Can't marshall %v: %v", ti, err) 49 } 50 r, err := json.Marshal(p.Restorer) 51 if err != nil { 52 t.Fatalf("Can't marshall %v: %v", p.Restorer, err) 53 } 54 t.Errorf("TestStart: want termios from Get %s to be the same as termios from Start (%s) to be the same, they differ", tt, r) 55 } 56 b := make([]byte, 1024) 57 n, err := p.Ptm.Read(b) 58 t.Logf("ptm read is %d bytes, b is %q", n, b[:n]) 59 if err != nil { 60 t.Fatalf("Error reading from process: %v", err) 61 } 62 if n != 4 { 63 t.Errorf("Bogus returned amount: got %d, want 4", n) 64 } 65 if string(b[:n]) != "hi\r\n" { 66 t.Errorf("bogus returned data: got %q, want %q", string(b[:n]), "hi\r\n") 67 } 68 }