github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/pox/pox_test.go (about) 1 // Copyright 2017-2018 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 main 6 7 import ( 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "syscall" 12 "testing" 13 14 "github.com/u-root/u-root/pkg/testutil" 15 ) 16 17 var ( 18 uskip = len("2018/08/10 21:20:42 ") 19 ) 20 21 func TestSimple(t *testing.T) { 22 if os.Getuid() != 0 { 23 t.Skip("Must be root for this test") 24 } 25 26 tmpDir, err := ioutil.TempDir("", "pox") 27 if err != nil { 28 t.Fatal(err) 29 } 30 defer os.RemoveAll(tmpDir) 31 f := filepath.Join(tmpDir, "x.tcz") 32 var tests = []struct { 33 args []string 34 name string 35 status int 36 out string 37 skip int 38 stdin *testutil.FakeStdin 39 }{ 40 // -c, --create create it (default true) 41 // -d, --debug enable debug prints 42 // -o, --output string Output file (default "/tmp/pox.tcz") 43 // -t, --test run a test with the first argument 44 { 45 args: []string{"-c", "-f", f, "bin/bash"}, 46 name: "Bad Executable", 47 status: 1, 48 out: "Running ldd on [bin/bash]: lstat bin/bash: no such file or directory \n", 49 skip: uskip, 50 }, 51 { 52 args: []string{"-c", "-f", f, "/bin/bash"}, 53 name: "Build", 54 status: 0, 55 out: "", 56 skip: uskip, 57 }, 58 { 59 args: []string{"-r", "-f", f, "/bin/bash"}, 60 name: "", 61 status: 0, 62 out: "", 63 skip: uskip, 64 }, 65 { 66 args: []string{"-r", "-f", f, "--", "/bin/bash", "-c", "echo hi"}, 67 name: "", 68 status: 0, 69 out: "", 70 skip: uskip, 71 }, 72 } 73 74 // Table-driven testing 75 for _, tt := range tests { 76 t.Run(tt.name, func(t *testing.T) { 77 c := testutil.Command(t, tt.args...) 78 // ignore the error, we deal with it via process status, 79 // and most of these commands are supposed to get an error. 80 out, _ := c.CombinedOutput() 81 status := c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() 82 if tt.status == status && len(tt.out) == 0 { 83 return 84 } 85 if tt.status != status { 86 t.Errorf("err got: %v want %v", status, tt.status) 87 } 88 if len(out) < tt.skip { 89 t.Errorf("err got: %v wanted at least %d bytes", string(out), tt.skip) 90 return 91 } 92 m := string(out[tt.skip:]) 93 if m != tt.out { 94 t.Errorf("got:'%q'(%d bytes) want:'%q'(%d bytes)", m, len(m), tt.out, len(tt.out)) 95 } 96 }) 97 } 98 } 99 100 func TestMain(m *testing.M) { 101 testutil.Run(m, main) 102 }