diff options
Diffstat (limited to 'libgo/go/syscall/passfd_test.go')
-rw-r--r-- | libgo/go/syscall/passfd_test.go | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/libgo/go/syscall/passfd_test.go b/libgo/go/syscall/passfd_test.go index 20ef39ecaab..5d9980f3d15 100644 --- a/libgo/go/syscall/passfd_test.go +++ b/libgo/go/syscall/passfd_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build linux darwin +// +build linux darwin freebsd netbsd openbsd package syscall_test @@ -49,7 +49,11 @@ func TestPassFD(t *testing.T) { defer readFile.Close() cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir) - cmd.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...) + cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} + path := os.Getenv("LD_LIBRARY_PATH") + if path != "" { + cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+path) + } cmd.ExtraFiles = []*os.File{writeFile} out, err := cmd.CombinedOutput() @@ -149,3 +153,49 @@ func passFDChild() { return } } + +// TestUnixRightsRoundtrip tests that UnixRights, ParseSocketControlMessage, +// and ParseUnixRights are able to successfully round-trip lists of file descriptors. +func TestUnixRightsRoundtrip(t *testing.T) { + testCases := [...][][]int{ + {{42}}, + {{1, 2}}, + {{3, 4, 5}}, + {{}}, + {{1, 2}, {3, 4, 5}, {}, {7}}, + } + for _, testCase := range testCases { + b := []byte{} + var n int + for _, fds := range testCase { + // Last assignment to n wins + n = len(b) + syscall.CmsgLen(4*len(fds)) + b = append(b, syscall.UnixRights(fds...)...) + } + // Truncate b + b = b[:n] + + scms, err := syscall.ParseSocketControlMessage(b) + if err != nil { + t.Fatalf("ParseSocketControlMessage: %v", err) + } + if len(scms) != len(testCase) { + t.Fatalf("expected %v SocketControlMessage; got scms = %#v", len(testCase), scms) + } + for i, scm := range scms { + gotFds, err := syscall.ParseUnixRights(&scm) + if err != nil { + t.Fatalf("ParseUnixRights: %v", err) + } + wantFds := testCase[i] + if len(gotFds) != len(wantFds) { + t.Fatalf("expected %v fds, got %#v", len(wantFds), gotFds) + } + for j, fd := range gotFds { + if fd != wantFds[j] { + t.Fatalf("expected fd %v, got %v", wantFds[j], fd) + } + } + } + } +} |