github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/mknod/mknod_test.go (about) 1 // Copyright 2016-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 main 6 7 import ( 8 "bytes" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "testing" 14 15 "github.com/u-root/u-root/pkg/testutil" 16 ) 17 18 type test struct { 19 args []string 20 expects string 21 } 22 23 func run(c *exec.Cmd) (string, string, error) { 24 var o, e bytes.Buffer 25 c.Stdout, c.Stderr = &o, &e 26 err := c.Run() 27 return o.String(), e.String(), err 28 } 29 30 func TestMknodFifo(t *testing.T) { 31 tmpDir, err := ioutil.TempDir("", "ls") 32 if err != nil { 33 t.Fatal(err) 34 } 35 defer os.RemoveAll(tmpDir) 36 37 // Delete a preexisting pipe if it exists, thought it shouldn't 38 pipepath := filepath.Join(tmpDir, "testpipe") 39 _ = os.Remove(pipepath) 40 if _, err := os.Stat(pipepath); err != nil && !os.IsNotExist(err) { 41 // Couldn't delete the file for reasons other than it didn't exist. 42 t.Fatalf("couldn't delete preexisting pipe") 43 } 44 45 // Make a pipe and check that it exists. 46 c := testutil.Command(t, pipepath, "p") 47 c.Run() 48 if _, err := os.Stat(pipepath); os.IsNotExist(err) { 49 t.Errorf("Pipe was not created.") 50 } 51 } 52 53 func TestInvocationErrors(t *testing.T) { 54 tmpDir, err := ioutil.TempDir("", "ls") 55 if err != nil { 56 t.Fatal(err) 57 } 58 defer os.RemoveAll(tmpDir) 59 60 devpath := filepath.Join(tmpDir, "testdev") 61 var tests = []test{ 62 {args: []string{devpath}, expects: "mknod: usage: mknod path type [major minor]\n"}, 63 {args: []string{""}, expects: "mknod: usage: mknod path type [major minor]\n"}, 64 {args: []string{devpath, "p", "254", "3"}, expects: "mknod: device type p requires no other arguments\n"}, 65 {args: []string{devpath, "b", "254"}, expects: "mknod: usage: mknod path type [major minor]\n"}, 66 {args: []string{devpath, "b"}, expects: "mknod: device type b requires a major and minor number\n"}, 67 {args: []string{devpath, "k"}, expects: "mknod: device type not recognized: k\n"}, 68 } 69 70 for _, v := range tests { 71 c := testutil.Command(t, v.args...) 72 _, e, _ := run(c) 73 if e[20:] != v.expects { 74 t.Errorf("mknod for '%v' failed: got '%s', want '%s'", v.args, e[20:], v.expects) 75 } 76 } 77 } 78 79 func TestMain(m *testing.M) { 80 testutil.Run(m, main) 81 }