Skip to content

Commit fab1602

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

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

src/os/os_chmod_test.go

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,56 @@ 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()
37+
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
38+
t.Skip()
39+
}
40+
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+
},
56+
}
57+
58+
for name, tc := range testCases {
59+
t.Run(name, func(t *testing.T) {
60+
f := t.TempFile("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+
}
4073

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)
44-
}
74+
if fi.Sys() == nil {
75+
t.Fatalf("stat %s: fi.Sys(): got nil", f.Name())
76+
}
4577

46-
fi, err := Stat(f.Name())
47-
if err != nil {
48-
t.Fatalf("stat %s: %s", f.Name(), err)
49-
}
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+
}
5082

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)
53-
}
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+
})
5488
}
5589
}

0 commit comments

Comments
 (0)