github.com/jlowellwofford/u-root@v1.0.0/pkg/pty/pty_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 pty 6 7 import ( 8 "encoding/json" 9 "os" 10 "reflect" 11 "testing" 12 ) 13 14 func TestNew(t *testing.T) { 15 if _, err := New(); os.IsNotExist(err) { 16 t.Skipf("No /dev/tty here.") 17 } else if err != nil { 18 t.Errorf("New pty: want nil, got %v", err) 19 } 20 } 21 22 func TestRunRestoreTTYMode(t *testing.T) { 23 p, err := New() 24 if os.IsNotExist(err) { 25 t.Skipf("No /dev/tty here.") 26 } else if err != nil { 27 t.Fatalf("TestStart New pty: want nil, got %v", err) 28 } 29 30 p.Command("echo", "hi") 31 if err := p.Start(); err != nil { 32 t.Fatalf("TestStart Start: want nil, got %v", err) 33 } 34 if err := p.Wait(); err != nil { 35 t.Error(err) 36 } 37 ti, err := p.TTY.Get() 38 if err != nil { 39 t.Fatalf("TestStart Get: want nil, got %v", err) 40 } 41 if !reflect.DeepEqual(ti, p.Restorer) { 42 tt, err := json.Marshal(ti) 43 if err != nil { 44 t.Fatalf("Can't marshall %v: %v", ti, err) 45 } 46 r, err := json.Marshal(p.Restorer) 47 if err != nil { 48 t.Fatalf("Can't marshall %v: %v", p.Restorer, err) 49 } 50 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) 51 } 52 b := make([]byte, 1024) 53 n, err := p.Ptm.Read(b) 54 t.Logf("ptm read is %d bytes, b is %q", n, b[:n]) 55 if err != nil { 56 t.Fatalf("Error reading from process: %v", err) 57 } 58 if n != 4 { 59 t.Errorf("Bogus returned amount: got %d, want 4", n) 60 } 61 if string(b[:n]) != "hi\r\n" { 62 t.Errorf("bogus returned data: got %q, want %q", string(b[:n]), "hi\r\n") 63 } 64 }