github.com/criyle/go-sandbox@v0.10.3/pkg/forkexec/fork_linux_test.go (about) 1 package forkexec 2 3 import ( 4 "os" 5 "syscall" 6 "testing" 7 8 "github.com/criyle/go-sandbox/pkg/mount" 9 ) 10 11 func TestFork_DropCaps(t *testing.T) { 12 t.Parallel() 13 r := Runner{ 14 Args: []string{"/bin/echo"}, 15 CloneFlags: syscall.CLONE_NEWUSER, 16 DropCaps: true, 17 } 18 _, err := r.Start() 19 if err != nil { 20 t.Fatal(err) 21 } 22 } 23 24 func TestFork_ETXTBSY(t *testing.T) { 25 f, err := os.CreateTemp("", "") 26 if err != nil { 27 t.Fatal(err) 28 } 29 t.Cleanup(func() { 30 os.Remove(f.Name()) 31 f.Close() 32 }) 33 34 if err := f.Chmod(0777); err != nil { 35 t.Fatal(err) 36 } 37 38 echo, err := os.Open("/bin/echo") 39 if err != nil { 40 t.Fatal(err) 41 } 42 defer echo.Close() 43 44 _, err = f.ReadFrom(echo) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 r := Runner{ 50 Args: []string{f.Name()}, 51 ExecFile: f.Fd(), 52 } 53 _, err = r.Start() 54 e, ok := err.(ChildError) 55 if !ok { 56 t.Fatalf("not a child error") 57 } 58 if e.Err != syscall.ETXTBSY && e.Location != LocExecve && e.Index != 0 { 59 t.Fatal(err) 60 } 61 } 62 63 func TestFork_OK(t *testing.T) { 64 t.Parallel() 65 f, err := os.CreateTemp("", "") 66 if err != nil { 67 t.Fatal(err) 68 } 69 defer os.Remove(f.Name()) 70 71 if err := f.Chmod(0777); err != nil { 72 t.Fatal(err) 73 } 74 75 echo, err := os.Open("/bin/echo") 76 if err != nil { 77 t.Fatal(err) 78 } 79 defer echo.Close() 80 81 _, err = f.ReadFrom(echo) 82 if err != nil { 83 t.Fatal(err) 84 } 85 f.Close() 86 87 r := Runner{ 88 Args: []string{f.Name()}, 89 } 90 _, err = r.Start() 91 if err != nil { 92 t.Fatal(err) 93 } 94 } 95 96 func TestFork_ENOENT(t *testing.T) { 97 t.Parallel() 98 m, err := mount.NewBuilder(). 99 WithMount( 100 mount.Mount{ 101 Source: "NOT_EXISTS", 102 }).Build() 103 if err != nil { 104 t.Fatal(err) 105 } 106 r := Runner{ 107 Args: []string{"/bin/echo"}, 108 CloneFlags: syscall.CLONE_NEWNS | syscall.CLONE_NEWUSER, 109 Mounts: m, 110 } 111 _, err = r.Start() 112 e, ok := err.(ChildError) 113 if !ok { 114 t.Fatalf("not a child error") 115 } 116 if e.Err != syscall.ENOENT && e.Location != LocExecve { 117 t.Fatal(err) 118 } 119 }