Explorar o código

unix: enable TestFchmodat for all Unices

Now that Fchmodat is available on all Unices, move TestFchmodat to
syscall_unix_test.go and adjust it such that Fchmodat with
AT_SYMLINK_NOFOLLOW in flags is tested where supported.

Change-Id: I41ef5b874e36d84ed4866706dbba075c39aa00d4
Reviewed-on: https://go-review.googlesource.com/101615
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Tobias Klauser %!s(int64=7) %!d(string=hai) anos
pai
achega
641605214e
Modificáronse 2 ficheiros con 52 adicións e 29 borrados
  1. 0 29
      unix/syscall_linux_test.go
  2. 52 0
      unix/syscall_unix_test.go

+ 0 - 29
unix/syscall_linux_test.go

@@ -16,35 +16,6 @@ import (
 	"golang.org/x/sys/unix"
 )
 
-func TestFchmodat(t *testing.T) {
-	defer chtmpdir(t)()
-
-	touch(t, "file1")
-	err := os.Symlink("file1", "symlink1")
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, 0)
-	if err != nil {
-		t.Fatalf("Fchmodat: unexpected error: %v", err)
-	}
-
-	fi, err := os.Stat("file1")
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	if fi.Mode() != 0444 {
-		t.Errorf("Fchmodat: failed to change mode: expected %v, got %v", 0444, fi.Mode())
-	}
-
-	err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, unix.AT_SYMLINK_NOFOLLOW)
-	if err != unix.EOPNOTSUPP {
-		t.Fatalf("Fchmodat: unexpected error: %v, expected EOPNOTSUPP", err)
-	}
-}
-
 func TestIoctlGetInt(t *testing.T) {
 	f, err := os.Open("/dev/random")
 	if err != nil {

+ 52 - 0
unix/syscall_unix_test.go

@@ -467,6 +467,58 @@ func TestFstatat(t *testing.T) {
 	}
 }
 
+func TestFchmodat(t *testing.T) {
+	defer chtmpdir(t)()
+
+	touch(t, "file1")
+	err := os.Symlink("file1", "symlink1")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	mode := os.FileMode(0444)
+	err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", uint32(mode), 0)
+	if err != nil {
+		t.Fatalf("Fchmodat: unexpected error: %v", err)
+	}
+
+	fi, err := os.Stat("file1")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if fi.Mode() != mode {
+		t.Errorf("Fchmodat: failed to change file mode: expected %v, got %v", mode, fi.Mode())
+	}
+
+	mode = os.FileMode(0644)
+	err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", uint32(mode), unix.AT_SYMLINK_NOFOLLOW)
+	if err != nil {
+		if runtime.GOOS == "linux" && err == unix.EOPNOTSUPP {
+			// Linux doesn't support flags != 0
+		} else {
+			t.Fatalf("Fchmodat: unexpected error: %v", err)
+		}
+	}
+
+	if runtime.GOOS == "linux" {
+		// Didn't change mode of the symlink. On Linux, the permissions
+		// of a symbolic link are always 0777 according to symlink(7)
+		mode = os.FileMode(0777)
+	}
+
+	var st unix.Stat_t
+	err = unix.Lstat("symlink1", &st)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	got := os.FileMode(st.Mode & 0777)
+	if got != mode {
+		t.Errorf("Fchmodat: failed to change symlink mode: expected %v, got %v", mode, got)
+	}
+}
+
 // mktmpfifo creates a temporary FIFO and provides a cleanup function.
 func mktmpfifo(t *testing.T) (*os.File, func()) {
 	err := unix.Mkfifo("fifo", 0666)