github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/syscall/syscall_unix_test.go (about) 1 // Copyright 2013 The Go 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 // +build darwin dragonfly freebsd linux netbsd openbsd solaris 6 7 package syscall_test 8 9 import ( 10 "flag" 11 "fmt" 12 "io/ioutil" 13 "net" 14 "os" 15 "os/exec" 16 "path/filepath" 17 "runtime" 18 "syscall" 19 "testing" 20 "time" 21 ) 22 23 // Tests that below functions, structures and constants are consistent 24 // on all Unix-like systems. 25 func _() { 26 // program scheduling priority functions and constants 27 var ( 28 _ func(int, int, int) error = syscall.Setpriority 29 _ func(int, int) (int, error) = syscall.Getpriority 30 ) 31 const ( 32 _ int = syscall.PRIO_USER 33 _ int = syscall.PRIO_PROCESS 34 _ int = syscall.PRIO_PGRP 35 ) 36 37 // termios constants 38 const ( 39 _ int = syscall.TCIFLUSH 40 _ int = syscall.TCIOFLUSH 41 _ int = syscall.TCOFLUSH 42 ) 43 44 // fcntl file locking structure and constants 45 var ( 46 _ = syscall.Flock_t{ 47 Type: int16(0), 48 Whence: int16(0), 49 Start: int64(0), 50 Len: int64(0), 51 Pid: int32(0), 52 } 53 ) 54 const ( 55 _ = syscall.F_GETLK 56 _ = syscall.F_SETLK 57 _ = syscall.F_SETLKW 58 ) 59 } 60 61 // TestFcntlFlock tests whether the file locking structure matches 62 // the calling convention of each kernel. 63 func TestFcntlFlock(t *testing.T) { 64 name := filepath.Join(os.TempDir(), "TestFcntlFlock") 65 fd, err := syscall.Open(name, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0) 66 if err != nil { 67 t.Fatalf("Open failed: %v", err) 68 } 69 defer syscall.Unlink(name) 70 defer syscall.Close(fd) 71 flock := syscall.Flock_t{ 72 Type: syscall.F_RDLCK, 73 Start: 0, Len: 0, Whence: 1, 74 } 75 if err := syscall.FcntlFlock(uintptr(fd), syscall.F_GETLK, &flock); err != nil { 76 t.Fatalf("FcntlFlock failed: %v", err) 77 } 78 } 79 80 // TestPassFD tests passing a file descriptor over a Unix socket. 81 // 82 // This test involved both a parent and child process. The parent 83 // process is invoked as a normal test, with "go test", which then 84 // runs the child process by running the current test binary with args 85 // "-test.run=^TestPassFD$" and an environment variable used to signal 86 // that the test should become the child process instead. 87 func TestPassFD(t *testing.T) { 88 switch runtime.GOOS { 89 case "dragonfly": 90 // TODO(jsing): Figure out why sendmsg is returning EINVAL. 91 t.Skip("skipping test on dragonfly") 92 case "solaris": 93 // TODO(aram): Figure out why ReadMsgUnix is returning empty message. 94 t.Skip("skipping test on solaris, see issue 7402") 95 } 96 if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" { 97 passFDChild() 98 return 99 } 100 101 tempDir, err := ioutil.TempDir("", "TestPassFD") 102 if err != nil { 103 t.Fatal(err) 104 } 105 defer os.RemoveAll(tempDir) 106 107 fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0) 108 if err != nil { 109 t.Fatalf("Socketpair: %v", err) 110 } 111 defer syscall.Close(fds[0]) 112 defer syscall.Close(fds[1]) 113 writeFile := os.NewFile(uintptr(fds[0]), "child-writes") 114 readFile := os.NewFile(uintptr(fds[1]), "parent-reads") 115 defer writeFile.Close() 116 defer readFile.Close() 117 118 cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir) 119 cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} 120 cmd.ExtraFiles = []*os.File{writeFile} 121 122 out, err := cmd.CombinedOutput() 123 if len(out) > 0 || err != nil { 124 t.Fatalf("child process: %q, %v", out, err) 125 } 126 127 c, err := net.FileConn(readFile) 128 if err != nil { 129 t.Fatalf("FileConn: %v", err) 130 } 131 defer c.Close() 132 133 uc, ok := c.(*net.UnixConn) 134 if !ok { 135 t.Fatalf("unexpected FileConn type; expected UnixConn, got %T", c) 136 } 137 138 buf := make([]byte, 32) // expect 1 byte 139 oob := make([]byte, 32) // expect 24 bytes 140 closeUnix := time.AfterFunc(5*time.Second, func() { 141 t.Logf("timeout reading from unix socket") 142 uc.Close() 143 }) 144 _, oobn, _, _, err := uc.ReadMsgUnix(buf, oob) 145 closeUnix.Stop() 146 147 scms, err := syscall.ParseSocketControlMessage(oob[:oobn]) 148 if err != nil { 149 t.Fatalf("ParseSocketControlMessage: %v", err) 150 } 151 if len(scms) != 1 { 152 t.Fatalf("expected 1 SocketControlMessage; got scms = %#v", scms) 153 } 154 scm := scms[0] 155 gotFds, err := syscall.ParseUnixRights(&scm) 156 if err != nil { 157 t.Fatalf("syscall.ParseUnixRights: %v", err) 158 } 159 if len(gotFds) != 1 { 160 t.Fatalf("wanted 1 fd; got %#v", gotFds) 161 } 162 163 f := os.NewFile(uintptr(gotFds[0]), "fd-from-child") 164 defer f.Close() 165 166 got, err := ioutil.ReadAll(f) 167 want := "Hello from child process!\n" 168 if string(got) != want { 169 t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want) 170 } 171 } 172 173 // passFDChild is the child process used by TestPassFD. 174 func passFDChild() { 175 defer os.Exit(0) 176 177 // Look for our fd. It should be fd 3, but we work around an fd leak 178 // bug here (http://golang.org/issue/2603) to let it be elsewhere. 179 var uc *net.UnixConn 180 for fd := uintptr(3); fd <= 10; fd++ { 181 f := os.NewFile(fd, "unix-conn") 182 var ok bool 183 netc, _ := net.FileConn(f) 184 uc, ok = netc.(*net.UnixConn) 185 if ok { 186 break 187 } 188 } 189 if uc == nil { 190 fmt.Println("failed to find unix fd") 191 return 192 } 193 194 // Make a file f to send to our parent process on uc. 195 // We make it in tempDir, which our parent will clean up. 196 flag.Parse() 197 tempDir := flag.Arg(0) 198 f, err := ioutil.TempFile(tempDir, "") 199 if err != nil { 200 fmt.Printf("TempFile: %v", err) 201 return 202 } 203 204 f.Write([]byte("Hello from child process!\n")) 205 f.Seek(0, 0) 206 207 rights := syscall.UnixRights(int(f.Fd())) 208 dummyByte := []byte("x") 209 n, oobn, err := uc.WriteMsgUnix(dummyByte, rights, nil) 210 if err != nil { 211 fmt.Printf("WriteMsgUnix: %v", err) 212 return 213 } 214 if n != 1 || oobn != len(rights) { 215 fmt.Printf("WriteMsgUnix = %d, %d; want 1, %d", n, oobn, len(rights)) 216 return 217 } 218 } 219 220 // TestUnixRightsRoundtrip tests that UnixRights, ParseSocketControlMessage, 221 // and ParseUnixRights are able to successfully round-trip lists of file descriptors. 222 func TestUnixRightsRoundtrip(t *testing.T) { 223 testCases := [...][][]int{ 224 {{42}}, 225 {{1, 2}}, 226 {{3, 4, 5}}, 227 {{}}, 228 {{1, 2}, {3, 4, 5}, {}, {7}}, 229 } 230 for _, testCase := range testCases { 231 b := []byte{} 232 var n int 233 for _, fds := range testCase { 234 // Last assignment to n wins 235 n = len(b) + syscall.CmsgLen(4*len(fds)) 236 b = append(b, syscall.UnixRights(fds...)...) 237 } 238 // Truncate b 239 b = b[:n] 240 241 scms, err := syscall.ParseSocketControlMessage(b) 242 if err != nil { 243 t.Fatalf("ParseSocketControlMessage: %v", err) 244 } 245 if len(scms) != len(testCase) { 246 t.Fatalf("expected %v SocketControlMessage; got scms = %#v", len(testCase), scms) 247 } 248 for i, scm := range scms { 249 gotFds, err := syscall.ParseUnixRights(&scm) 250 if err != nil { 251 t.Fatalf("ParseUnixRights: %v", err) 252 } 253 wantFds := testCase[i] 254 if len(gotFds) != len(wantFds) { 255 t.Fatalf("expected %v fds, got %#v", len(wantFds), gotFds) 256 } 257 for j, fd := range gotFds { 258 if fd != wantFds[j] { 259 t.Fatalf("expected fd %v, got %v", wantFds[j], fd) 260 } 261 } 262 } 263 } 264 } 265 266 func TestRlimit(t *testing.T) { 267 var rlimit, zero syscall.Rlimit 268 err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit) 269 if err != nil { 270 t.Fatalf("Getrlimit: save failed: %v", err) 271 } 272 if zero == rlimit { 273 t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit) 274 } 275 set := rlimit 276 set.Cur = set.Max - 1 277 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &set) 278 if err != nil { 279 t.Fatalf("Setrlimit: set failed: %#v %v", set, err) 280 } 281 var get syscall.Rlimit 282 err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &get) 283 if err != nil { 284 t.Fatalf("Getrlimit: get failed: %v", err) 285 } 286 set = rlimit 287 set.Cur = set.Max - 1 288 if set != get { 289 // Seems like Darwin requires some privilege to 290 // increase the soft limit of rlimit sandbox, though 291 // Setrlimit never reports an error. 292 switch runtime.GOOS { 293 case "darwin": 294 default: 295 t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get) 296 } 297 } 298 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit) 299 if err != nil { 300 t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err) 301 } 302 } 303 304 func TestSeekFailure(t *testing.T) { 305 _, err := syscall.Seek(-1, 0, 0) 306 if err == nil { 307 t.Fatalf("Seek(-1, 0, 0) did not fail") 308 } 309 str := err.Error() // used to crash on Linux 310 t.Logf("Seek: %v", str) 311 if str == "" { 312 t.Fatalf("Seek(-1, 0, 0) return error with empty message") 313 } 314 }