1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin dragonfly freebsd linux netbsd openbsd
16 func checkUidGid(t
*testing
.T
, path
string, uid
, gid
int) {
17 dir
, err
:= Stat(path
)
19 t
.Fatalf("Stat %q (looking for uid/gid %d/%d): %s", path
, uid
, gid
, err
)
21 sys
:= dir
.Sys().(*syscall
.Stat_t
)
22 if int(sys
.Uid
) != uid
{
23 t
.Errorf("Stat %q: uid %d want %d", path
, sys
.Uid
, uid
)
25 if int(sys
.Gid
) != gid
{
26 t
.Errorf("Stat %q: gid %d want %d", path
, sys
.Gid
, gid
)
30 func TestChown(t
*testing
.T
) {
31 // Chown is not supported under windows os Plan 9.
32 // Plan9 provides a native ChownPlan9 version instead.
33 if runtime
.GOOS
== "windows" || runtime
.GOOS
== "plan9" {
36 // Use TempDir() to make sure we're on a local file system,
37 // so that the group ids returned by Getgroups will be allowed
38 // on the file. On NFS, the Getgroups groups are
40 f
:= newFile("TestChown", t
)
41 defer Remove(f
.Name())
45 t
.Fatalf("stat %s: %s", f
.Name(), err
)
48 // Can't change uid unless root, but can try
49 // changing the group id. First try our current group.
52 if err
= Chown(f
.Name(), -1, gid
); err
!= nil {
53 t
.Fatalf("chown %s -1 %d: %s", f
.Name(), gid
, err
)
55 sys
:= dir
.Sys().(*syscall
.Stat_t
)
56 checkUidGid(t
, f
.Name(), int(sys
.Uid
), gid
)
58 // Then try all the auxiliary groups.
59 groups
, err
:= Getgroups()
61 t
.Fatalf("getgroups: %s", err
)
63 t
.Log("groups: ", groups
)
64 for _
, g
:= range groups
{
65 if err
= Chown(f
.Name(), -1, g
); err
!= nil {
66 t
.Fatalf("chown %s -1 %d: %s", f
.Name(), g
, err
)
68 checkUidGid(t
, f
.Name(), int(sys
.Uid
), g
)
70 // change back to gid to test fd.Chown
71 if err
= f
.Chown(-1, gid
); err
!= nil {
72 t
.Fatalf("fchown %s -1 %d: %s", f
.Name(), gid
, err
)
74 checkUidGid(t
, f
.Name(), int(sys
.Uid
), gid
)
78 func TestReaddirWithBadLstat(t
*testing
.T
) {
79 handle
, err
:= Open(sfdir
)
80 failfile
:= sfdir
+ "/" + sfname
82 t
.Fatalf("Couldn't open %s: %s", sfdir
, err
)
85 *LstatP
= func(file
string) (FileInfo
, error
) {
92 defer func() { *LstatP
= Lstat
}()
94 dirs
, err
:= handle
.Readdir(-1)
96 t
.Fatalf("Expected Readdir to return no error, got %v", err
)
99 for _
, dir
:= range dirs
{
100 if dir
.Name() == sfname
{
102 if dir
.Sys() != nil {
103 t
.Errorf("Expected Readdir for %s should not contain Sys", failfile
)
106 if dir
.Sys() == nil {
107 t
.Errorf("Readdir for every file other than %s should contain Sys, but %s/%s didn't either", failfile
, sfdir
, dir
.Name())
112 t
.Fatalf("Expected %s from Readdir, but didn't find it", failfile
)