| 60 | } |
| 61 | |
| 62 | func TestChownFile(t *testing.T) { |
| 63 | if build.IsWindows { |
| 64 | t.Skip("Not supported on Windows") |
| 65 | return |
| 66 | } |
| 67 | if os.Getuid() != 0 { |
| 68 | // We are not root. No expectation of being able to chown. Our tests |
| 69 | // typically don't run with CAP_FOWNER. |
| 70 | t.Skip("Test not possible") |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | fs, dir := setup(t) |
| 75 | path := filepath.Join(dir, "file") |
| 76 | |
| 77 | defer os.Chmod(path, 0o666) |
| 78 | |
| 79 | fd, err := os.Create(path) |
| 80 | if err != nil { |
| 81 | t.Error("Unexpected error:", err) |
| 82 | } |
| 83 | fd.Close() |
| 84 | |
| 85 | _, err = fs.Lstat("file") |
| 86 | if err != nil { |
| 87 | t.Error("Unexpected error:", err) |
| 88 | } |
| 89 | |
| 90 | newUID := 1000 + rand.Intn(30000) |
| 91 | newGID := 1000 + rand.Intn(30000) |
| 92 | |
| 93 | if err := fs.Lchown("file", strconv.Itoa(newUID), strconv.Itoa(newGID)); err != nil { |
| 94 | t.Error("Unexpected error:", err) |
| 95 | } |
| 96 | |
| 97 | info, err := fs.Lstat("file") |
| 98 | if err != nil { |
| 99 | t.Error("Unexpected error:", err) |
| 100 | } |
| 101 | if info.Owner() != newUID { |
| 102 | t.Errorf("Incorrect owner, expected %d but got %d", newUID, info.Owner()) |
| 103 | } |
| 104 | if info.Group() != newGID { |
| 105 | t.Errorf("Incorrect group, expected %d but got %d", newGID, info.Group()) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestChmodDir(t *testing.T) { |
| 110 | fs, dir := setup(t) |