diff options
author | Jakob Unterwurzacher | 2019-01-07 23:00:05 +0100 |
---|---|---|
committer | Jakob Unterwurzacher | 2019-01-07 23:07:53 +0100 |
commit | 8253c55386e59295ea48dfc1b9beea4d785fcbbd (patch) | |
tree | 6af5422a5b4dbff6d4c67d4678ad6fcd0122f5e2 /internal/syscallcompat | |
parent | 57a52d6aef9b320800a42f9a12fd7e1f47f63073 (diff) |
tests: add Fchmodat test
Test that we handle symlinks correctly.
Diffstat (limited to 'internal/syscallcompat')
-rw-r--r-- | internal/syscallcompat/sys_common_test.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/syscallcompat/sys_common_test.go b/internal/syscallcompat/sys_common_test.go index e568a36..455774f 100644 --- a/internal/syscallcompat/sys_common_test.go +++ b/internal/syscallcompat/sys_common_test.go @@ -2,6 +2,7 @@ package syscallcompat import ( "bytes" + "os" "syscall" "testing" ) @@ -32,3 +33,56 @@ func TestReadlinkat(t *testing.T) { } } } + +func TestFchmodat(t *testing.T) { + regular := "TestFchmodat_Regular" + f, err := os.OpenFile(tmpDir+"/"+regular, os.O_CREATE|os.O_WRONLY, 0000) + if err != nil { + t.Fatal(err) + } + f.Close() + symlink := "TestFchmodat_Symlink" + err = syscall.Symlink(regular, tmpDir+"/"+symlink) + if err != nil { + t.Fatal(err) + } + dirfd, err := syscall.Open(tmpDir, syscall.O_RDONLY, 0) + if err != nil { + t.Fatal(err) + } + defer syscall.Close(dirfd) + + // Check that chmod on a regular file works ok + err = Fchmodat(dirfd, regular, 0111, 0) + if err != nil { + t.Fatal(err) + } + var st syscall.Stat_t + syscall.Lstat(tmpDir+"/"+regular, &st) + st.Mode &= 0777 + if st.Mode != 0111 { + t.Errorf("wrong mode: %#0o", st.Mode) + } + err = Fchmodat(dirfd, regular, 0000, 0) + if err != nil { + t.Error(err) + } + syscall.Lstat(tmpDir+"/"+regular, &st) + st.Mode &= 0777 + if st.Mode != 0000 { + t.Errorf("wrong mode: %#0o", st.Mode) + } + + // Check what happens on a symlink + err = Fchmodat(dirfd, symlink, 0333, 0) + if err == nil { + syscall.Lstat(tmpDir+"/"+symlink, &st) + st.Mode &= 0777 + t.Errorf("chmod on symlink should have failed, but did not. New mode=%#0o", st.Mode) + } + syscall.Lstat(tmpDir+"/"+regular, &st) + st.Mode &= 0777 + if st.Mode != 0000 { + t.Errorf("chmod on symlink affected symlink target: New mode=%#0o", st.Mode) + } +} |