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