@@ -33,75 +33,36 @@ func TestChmod(t *testing.T) {
33
33
34
34
}
35
35
36
- 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
-
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 ) {
46
42
if runtime .GOOS == "windows" || runtime .GOOS == "plan9" {
47
- t .Skip ()
43
+ t .Skip ("skipping on " + runtime . GOOS )
48
44
}
49
45
46
+ var (
47
+ TEST_UID_ROOT = 0
48
+ TEST_GID_ROOT = 0
49
+ )
50
+
50
51
f := newFile ("TestChown" , t )
51
52
defer Remove (f .Name ())
52
53
defer f .Close ()
53
54
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 )
55
+ // EACCES
56
+ if err := Chown (f .Name (), TEST_UID_ROOT , TEST_GID_ROOT ); err != nil {
57
+ if err .(syscall.Errno ) != syscall .EPERM {
58
+ t .Fatalf ("chown(%s, uid=%v, gid=%v): got '%v', want %v" , f .Name (), TEST_UID_ROOT , TEST_GID_ROOT , err , syscall .EPERM )
60
59
}
61
60
}
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
- }
99
61
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 )
62
+ // ENOENT
63
+ if err := Chown ("invalid" , Geteuid (), Getgid ()); err != nil {
64
+ if err .(syscall.Errno ) != syscall .ENOENT {
65
+ t .Fatalf ("chown(%s, uid=%v, gid=%v): got '%v', want %v" , f .Name (), Geteuid (), Getegid (), err , syscall .ENOENT )
105
66
}
106
67
}
107
68
}
0 commit comments