Daily bump.
[official-gcc.git] / libgo / go / os / os_unix_test.go
blobb0fc0256de415a4cdf1e4b3865f5f2aec4baef4d
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
7 package os_test
9 import (
10 . "os"
11 "runtime"
12 "syscall"
13 "testing"
16 func checkUidGid(t *testing.T, path string, uid, gid int) {
17 dir, err := Stat(path)
18 if err != nil {
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" {
34 return
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
39 // basically useless.
40 f := newFile("TestChown", t)
41 defer Remove(f.Name())
42 defer f.Close()
43 dir, err := f.Stat()
44 if err != nil {
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.
50 gid := Getgid()
51 t.Log("gid:", gid)
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()
60 if err != nil {
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
81 if err != nil {
82 t.Fatalf("Couldn't open %s: %s", sfdir, err)
85 *LstatP = func(file string) (FileInfo, error) {
86 if file == failfile {
87 var fi FileInfo
88 return fi, ErrInvalid
90 return Lstat(file)
92 defer func() { *LstatP = Lstat }()
94 dirs, err := handle.Readdir(-1)
95 if err != nil {
96 t.Fatalf("Expected Readdir to return no error, got %v", err)
98 foundfail := false
99 for _, dir := range dirs {
100 if dir.Name() == sfname {
101 foundfail = true
102 if dir.Sys() != nil {
103 t.Errorf("Expected Readdir for %s should not contain Sys", failfile)
105 } else {
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())
111 if !foundfail {
112 t.Fatalf("Expected %s from Readdir, but didn't find it", failfile)