rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/os/os_unix_test.go (about) 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. 4 5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris 6 7 package os_test 8 9 import ( 10 . "os" 11 "runtime" 12 "syscall" 13 "testing" 14 ) 15 16 func init() { 17 isReadonlyError = func(err error) bool { return err == syscall.EROFS } 18 } 19 20 func checkUidGid(t *testing.T, path string, uid, gid int) { 21 dir, err := Stat(path) 22 if err != nil { 23 t.Fatalf("Stat %q (looking for uid/gid %d/%d): %s", path, uid, gid, err) 24 } 25 sys := dir.Sys().(*syscall.Stat_t) 26 if int(sys.Uid) != uid { 27 t.Errorf("Stat %q: uid %d want %d", path, sys.Uid, uid) 28 } 29 if int(sys.Gid) != gid { 30 t.Errorf("Stat %q: gid %d want %d", path, sys.Gid, gid) 31 } 32 } 33 34 func TestChown(t *testing.T) { 35 // Chown is not supported under windows os Plan 9. 36 // Plan9 provides a native ChownPlan9 version instead. 37 if runtime.GOOS == "windows" || runtime.GOOS == "plan9" { 38 return 39 } 40 // Use TempDir() to make sure we're on a local file system, 41 // so that the group ids returned by Getgroups will be allowed 42 // on the file. On NFS, the Getgroups groups are 43 // basically useless. 44 f := newFile("TestChown", t) 45 defer Remove(f.Name()) 46 defer f.Close() 47 dir, err := f.Stat() 48 if err != nil { 49 t.Fatalf("stat %s: %s", f.Name(), err) 50 } 51 52 // Can't change uid unless root, but can try 53 // changing the group id. First try our current group. 54 gid := Getgid() 55 t.Log("gid:", gid) 56 if err = Chown(f.Name(), -1, gid); err != nil { 57 t.Fatalf("chown %s -1 %d: %s", f.Name(), gid, err) 58 } 59 sys := dir.Sys().(*syscall.Stat_t) 60 checkUidGid(t, f.Name(), int(sys.Uid), gid) 61 62 // Then try all the auxiliary groups. 63 groups, err := Getgroups() 64 if err != nil { 65 t.Fatalf("getgroups: %s", err) 66 } 67 t.Log("groups: ", groups) 68 for _, g := range groups { 69 if err = Chown(f.Name(), -1, g); err != nil { 70 t.Fatalf("chown %s -1 %d: %s", f.Name(), g, err) 71 } 72 checkUidGid(t, f.Name(), int(sys.Uid), g) 73 74 // change back to gid to test fd.Chown 75 if err = f.Chown(-1, gid); err != nil { 76 t.Fatalf("fchown %s -1 %d: %s", f.Name(), gid, err) 77 } 78 checkUidGid(t, f.Name(), int(sys.Uid), gid) 79 } 80 }