github.com/rck/u-root@v0.0.0-20180106144920-7eb602e381bb/cmds/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 "fmt" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "syscall" 14 "testing" 15 16 "github.com/u-root/u-root/pkg/testutil" 17 ) 18 19 var ( 20 testPath = "." 21 // if true removeAll the testPath on the end 22 remove = true 23 ) 24 25 type test struct { 26 args []string 27 expects string 28 } 29 30 func run(c *exec.Cmd) (string, string, error) { 31 var o, e bytes.Buffer 32 c.Stdout, c.Stderr = &o, &e 33 err := c.Run() 34 return o.String(), e.String(), err 35 } 36 37 func TestMknodFifo(t *testing.T) { 38 tempDir, mknodPath := testutil.CompileInTempDir(t) 39 40 if remove { 41 defer os.RemoveAll(tempDir) 42 } 43 44 // Delete a preexisting pipe if it exists, thought it shouldn't 45 pipepath := filepath.Join(tempDir, "testpipe") 46 _ = os.Remove(pipepath) 47 if _, err := os.Stat(pipepath); err != nil && !os.IsNotExist(err) { 48 // Couldn't delete the file for reasons other than it didn't exist. 49 t.Fatalf("couldn't delete preexisting pipe") 50 } 51 52 // Make a pipe and check that it exists. 53 fmt.Print(pipepath) 54 c := exec.Command(mknodPath, pipepath, "p") 55 c.Run() 56 if _, err := os.Stat(pipepath); os.IsNotExist(err) { 57 t.Errorf("Pipe was not created.") 58 } 59 } 60 61 func TestInvocationErrors(t *testing.T) { 62 tempDir, mknodPath := testutil.CompileInTempDir(t) 63 64 if remove { 65 defer os.RemoveAll(tempDir) 66 } 67 68 devpath := filepath.Join(tempDir, "testdev") 69 var tests = []test{ 70 {args: []string{devpath}, expects: "Usage: mknod path type [major minor]\n"}, 71 {args: []string{""}, expects: "Usage: mknod path type [major minor]\n"}, 72 {args: []string{devpath, "p", "254", "3"}, expects: "device type p requires no other arguments\n"}, 73 {args: []string{devpath, "b", "254"}, expects: "Usage: mknod path type [major minor]\n"}, 74 {args: []string{devpath, "b"}, expects: "device type b requires a major and minor number\n"}, 75 {args: []string{devpath, "k"}, expects: "device type not recognized: k\n"}, 76 } 77 78 for _, v := range tests { 79 c := exec.Command(mknodPath, v.args...) 80 _, e, _ := run(c) 81 if e[20:] != v.expects { 82 t.Errorf("mknod for '%v' failed: got '%s', want '%s'", v.args, e[20:], v.expects) 83 } 84 } 85 } 86 87 func TestMknodBlock(t *testing.T) { 88 uid := syscall.Getuid() 89 90 if uid != 0 { 91 t.Logf("not root, uid %d, skipping test\n", uid) 92 return 93 } 94 t.Log("root user, proceeding\n") 95 //TODO(ganshun): implement block test 96 t.Skip("Unimplemented test, need root") 97 }