Skip to content

Commit c7e6851

Browse files
committed
restructure test to check for errors
Signed-off-by: leongross <[email protected]>
1 parent 15517cc commit c7e6851

File tree

1 file changed

+24
-58
lines changed

1 file changed

+24
-58
lines changed

src/os/os_chmod_test.go

+24-58
Original file line numberDiff line numberDiff line change
@@ -33,75 +33,41 @@ func TestChmod(t *testing.T) {
3333

3434
}
3535

36-
func TestChown(t *testing.T) {
36+
// Since testing syscalls requires a static, predictable environment that has to be controlled
37+
// by the CI, we don't test for success but for failures and verify that the error messages are as expected.
38+
// EACCES is returned when the user does not have the required permissions to change the ownership of the file
39+
// ENOENT is returned when the file does not exist
40+
// ENOTDIR is returned when the file is not a directory
41+
func TestChownErr(t *testing.T) {
42+
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
43+
t.Skip("skipping on " + runtime.GOOS)
44+
}
45+
3746
var (
38-
TEST_UID_1 = 1001
39-
TEST_GID_1 = 127
40-
TEST_UID_2 = 0
41-
TEST_GID_2 = 0
42-
ERR_PERM_DENIED = "permission denied"
43-
ERR_OP_NOT_PERMITTED = "operation not permitted"
47+
TEST_UID_ROOT = 0
48+
TEST_GID_ROOT = 0
4449
)
4550

46-
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
47-
t.Skip()
51+
f := newFile("TestChown", t)
52+
// technically, newFile should never return nil, but just in case
53+
if f == nil {
54+
t.Fatalf("newFile failed")
4855
}
4956

50-
f := newFile("TestChown", t)
5157
defer Remove(f.Name())
5258
defer f.Close()
5359

54-
// User with CI permissions run chown on owned file
55-
// This test does not change the initial permissions of the file
56-
// It only checks if the chown operation was successful and consistent
57-
if err := Chown(f.Name(), TEST_UID_1, TEST_GID_1); err != nil {
58-
if err.Error() != ERR_OP_NOT_PERMITTED {
59-
t.Fatalf("chown(%s, uid=%v, gid=%v): got %v, want != nil", f.Name(), TEST_UID_1, TEST_GID_1, err)
60+
// EACCES
61+
if err := Chown(f.Name(), TEST_UID_ROOT, TEST_GID_ROOT); err != nil {
62+
if err.(syscall.Errno) != syscall.EPERM {
63+
t.Fatalf("chown(%s, uid=%v, gid=%v): got '%v', want %v", f.Name(), TEST_UID_ROOT, TEST_GID_ROOT, err, syscall.EPERM)
6064
}
6165
}
62-
fi, err := Stat(f.Name())
63-
if err != nil {
64-
t.Fatalf("stat %s: got %v, want nil", f.Name(), err)
65-
}
66-
67-
if fi.Sys() == nil {
68-
t.Fatalf("stat %s: fi.Sys(): got nil", f.Name())
69-
}
70-
71-
s, ok := fi.Sys().(*syscall.Stat_t)
72-
if !ok {
73-
t.Fatalf("stat %s: fi.Sys(): is not *syscall.Stat_t", f.Name())
74-
}
75-
76-
uid, gid := s.Uid, s.Gid
77-
if uid != uint32(TEST_UID_1) || gid != uint32(TEST_GID_1) {
78-
t.Fatalf("chown(%s, %d, %d): want (%d,%d), got (%d, %d)", f.Name(), TEST_UID_1, TEST_GID_1, TEST_UID_1, TEST_GID_1, uid, gid)
79-
}
80-
81-
// Root permission denied
82-
if err = Chown(f.Name(), TEST_UID_2, TEST_GID_2); err.Error() != ERR_PERM_DENIED {
83-
t.Fatalf("chown(%s, uid=%v, gid=%v): got %v, want != nil", f.Name(), TEST_UID_2, TEST_GID_2, err)
84-
}
85-
86-
fi, err = Stat(f.Name())
87-
if err != nil {
88-
t.Fatalf("stat %s: got %v, want nil", f.Name(), err)
89-
}
90-
91-
if fi.Sys() == nil {
92-
t.Fatalf("stat %s: fi.Sys(): got nil", f.Name())
93-
}
94-
95-
s, ok = fi.Sys().(*syscall.Stat_t)
96-
if !ok {
97-
t.Fatalf("stat %s: fi.Sys(): is not *syscall.Stat_t", f.Name())
98-
}
9966

100-
uid, gid = s.Uid, s.Gid
101-
if uid != uint32(TEST_UID_2) || gid != uint32(TEST_GID_2) {
102-
// Chown will fail due to permissions denied, so the UID and GID should stay the same
103-
if uid != uint32(TEST_UID_1) && gid != uint32(TEST_GID_1) {
104-
t.Fatalf("chown(%s, %d, %d): want (%d,%d), got (%d, %d)", f.Name(), TEST_UID_2, TEST_GID_2, TEST_UID_2, TEST_GID_2, uid, gid)
67+
// ENOENT
68+
if err := Chown("invalid", Geteuid(), Getgid()); err != nil {
69+
if err.(syscall.Errno) != syscall.ENOENT {
70+
t.Fatalf("chown(%s, uid=%v, gid=%v): got '%v', want %v", f.Name(), Geteuid(), Getegid(), err, syscall.ENOENT)
10571
}
10672
}
10773
}

0 commit comments

Comments
 (0)