Skip to content

Commit 9f4304f

Browse files
committed
update tests
Signed-off-by: leongross <[email protected]>
1 parent 724b52c commit 9f4304f

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

src/os/os_chmod_test.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,55 @@ func TestChmod(t *testing.T) {
3434
}
3535

3636
func TestChown(t *testing.T) {
37-
f := newFile("TestChown", t)
38-
defer Remove(f.Name())
39-
defer f.Close()
40-
41-
if runtime.GOOS != "windows" {
42-
if err := Chown(f.Name(), 0, 0); err != nil {
43-
t.Fatalf("chown %s 0 0: %s", f.Name(), err)
37+
if runtime.GOOS == "linux" {
38+
testCases := map[string]struct {
39+
uid int
40+
gid int
41+
wantErr bool
42+
}{
43+
"root": {
44+
uid: 0,
45+
gid: 0,
46+
wantErr: true,
47+
},
48+
"user-" + runtime.GOOS: {
49+
uid: 1001,
50+
gid: 127,
51+
wantErr: false,
52+
},
4453
}
4554

46-
fi, err := Stat(f.Name())
47-
if err != nil {
48-
t.Fatalf("stat %s: %s", f.Name(), err)
49-
}
55+
for name, tc := range testCases {
56+
t.Run(name, func(t *testing.T) {
57+
f := t.TempFile("TestChown", t)
58+
defer Remove(f.Name())
59+
defer f.Close()
60+
61+
err := Chown(f.Name(), tc.uid, tc.gid)
62+
if (tc.wantErr && err == nil) || (!tc.wantErr && err != nil) {
63+
t.Fatalf("chown(%s, uid=%v, gid=%v): got %v, want error: %v", f.Name(), tc.uid, tc.gid, err, tc.wantErr)
64+
}
5065

51-
if fi.Sys().(*syscall.Stat_t).Uid != 0 || fi.Sys().(*syscall.Stat_t).Gid != 0 {
52-
t.Fatalf("chown %s 0 0: uid=%d gid=%d", f.Name(), fi.Sys().(*syscall.Stat_t).Uid, fi.Sys().(*syscall.Stat_t).Gid)
66+
fi, err := Stat(f.Name())
67+
if err != nil {
68+
t.Fatalf("stat %s: got %v, want nil", f.Name(), err)
69+
}
70+
71+
if fi.Sys() == nil {
72+
t.Fatalf("stat %s: fi.Sys() == nil", f.Name())
73+
}
74+
75+
s, ok := fi.Sys().(*syscall.Stat_t)
76+
if !ok {
77+
t.Fatalf("stat %s: fi.Sys() is not *syscall.Stat_t", f.Name())
78+
}
79+
80+
uid, gid := s.Uid, s.Gid
81+
if uid != uint32(tc.uid) || gid != uint32(tc.gid) {
82+
t.Fatalf("chown(%s, uid=%d, gid=%d): want (0,0), got (%d, %d)", f.Name(), tc.uid, tc.gid, uid, gid)
83+
}
84+
})
5385
}
86+
5487
}
5588
}

0 commit comments

Comments
 (0)