unix: don't defer close raw Socketpair fds in tests

The raw fds are successively wrapped using os.NewFile and will be closed
by (*os.File).Close. Avoids a double close, in the worst case closing an
unrelated fd.

Same as CL 309353 does for package syscall.

Change-Id: I26dffc3fefa90d636cb67a904df0bfc3b131f702
Reviewed-on: https://go-review.googlesource.com/c/sys/+/309689
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/unix/creds_test.go b/unix/creds_test.go
index 752c82e..a192845 100644
--- a/unix/creds_test.go
+++ b/unix/creds_test.go
@@ -39,16 +39,19 @@
 		if err != nil {
 			t.Fatalf("Socketpair: %v", err)
 		}
-		defer unix.Close(fds[0])
-		defer unix.Close(fds[1])
 
 		err = unix.SetsockoptInt(fds[0], unix.SOL_SOCKET, unix.SO_PASSCRED, 1)
 		if err != nil {
+			unix.Close(fds[0])
+			unix.Close(fds[1])
 			t.Fatalf("SetsockoptInt: %v", err)
 		}
 
 		srvFile := os.NewFile(uintptr(fds[0]), "server")
+		cliFile := os.NewFile(uintptr(fds[1]), "client")
 		defer srvFile.Close()
+		defer cliFile.Close()
+
 		srv, err := net.FileConn(srvFile)
 		if err != nil {
 			t.Errorf("FileConn: %v", err)
@@ -56,8 +59,6 @@
 		}
 		defer srv.Close()
 
-		cliFile := os.NewFile(uintptr(fds[1]), "client")
-		defer cliFile.Close()
 		cli, err := net.FileConn(cliFile)
 		if err != nil {
 			t.Errorf("FileConn: %v", err)
diff --git a/unix/syscall_unix_test.go b/unix/syscall_unix_test.go
index 5d31524..6792cef 100644
--- a/unix/syscall_unix_test.go
+++ b/unix/syscall_unix_test.go
@@ -206,8 +206,6 @@
 	if err != nil {
 		t.Fatalf("Socketpair: %v", err)
 	}
-	defer unix.Close(fds[0])
-	defer unix.Close(fds[1])
 	writeFile := os.NewFile(uintptr(fds[0]), "child-writes")
 	readFile := os.NewFile(uintptr(fds[1]), "parent-reads")
 	defer writeFile.Close()