Skip to content

Commit 15517cc

Browse files
committed
update tests, remove test case map
Signed-off-by: leongross <[email protected]>
1 parent b4869b6 commit 15517cc

File tree

2 files changed

+63
-48
lines changed

2 files changed

+63
-48
lines changed

src/os/file_anyos.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ func Chmod(name string, mode FileMode) error {
151151
// If the file is a symbolic link, it changes the uid and gid of the link's target.
152152
// A uid or gid of -1 means to not change that value.
153153
// If there is an error, it will be of type *PathError.
154-
//
155-
// On Windows or Plan 9, Chown always returns the syscall.EWINDOWS or
156-
// EPLAN9 error, wrapped in *PathError.
157154
func Chown(name string, uid, gid int) error {
158155
e := ignoringEINTR(func() error {
159156
return syscall.Chown(name, uid, gid)

src/os/os_chmod_test.go

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,56 +34,74 @@ func TestChmod(t *testing.T) {
3434
}
3535

3636
func TestChown(t *testing.T) {
37+
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"
44+
)
45+
3746
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
3847
t.Skip()
3948
}
4049

41-
testCases := map[string]struct {
42-
uid int
43-
gid int
44-
wantErr bool
45-
}{
46-
"root": {
47-
uid: 0,
48-
gid: 0,
49-
wantErr: true,
50-
},
51-
"user-" + runtime.GOOS: {
52-
uid: 1001,
53-
gid: 127,
54-
wantErr: false,
55-
},
50+
f := newFile("TestChown", t)
51+
defer Remove(f.Name())
52+
defer f.Close()
53+
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+
}
61+
}
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())
5698
}
5799

58-
for name, tc := range testCases {
59-
t.Run(name, func(t *testing.T) {
60-
f := newFile("TestChown", t)
61-
defer Remove(f.Name())
62-
defer f.Close()
63-
64-
err := Chown(f.Name(), tc.uid, tc.gid)
65-
if (tc.wantErr && err == nil) || (!tc.wantErr && err != nil) {
66-
t.Fatalf("chown(%s, uid=%v, gid=%v): got %v, want error: %v", f.Name(), tc.uid, tc.gid, err, tc.wantErr)
67-
}
68-
69-
fi, err := Stat(f.Name())
70-
if err != nil {
71-
t.Fatalf("stat %s: got %v, want nil", f.Name(), err)
72-
}
73-
74-
if fi.Sys() == nil {
75-
t.Fatalf("stat %s: fi.Sys(): got nil", f.Name())
76-
}
77-
78-
s, ok := fi.Sys().(*syscall.Stat_t)
79-
if !ok {
80-
t.Fatalf("stat %s: fi.Sys(): is not *syscall.Stat_t", f.Name())
81-
}
82-
83-
uid, gid := s.Uid, s.Gid
84-
if uid != uint32(tc.uid) || gid != uint32(tc.gid) {
85-
t.Fatalf("chown(%s, %d, %d): want (%d,%d), got (%d, %d)", f.Name(), tc.uid, tc.gid, tc.uid, tc.gid, uid, gid)
86-
}
87-
})
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)
105+
}
88106
}
89107
}

0 commit comments

Comments
 (0)