| 1 | |
| 2 | |
| 3 | |
| 4 | package apfs |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "syscall" |
| 12 | "testing" |
| 13 | |
| 14 | "gotest.tools/v3/assert" |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | func TestChownIntegration(t *testing.T) { |
| 22 | tmpDir := t.TempDir() |
| 23 | imgPath := filepath.Join(tmpDir, "test.dmg") |
| 24 | |
| 25 | |
| 26 | ctx := t.Context() |
| 27 | cmd := exec.CommandContext(ctx, "hdiutil", "create", |
| 28 | "-size", "64m", |
| 29 | "-fs", "APFS", |
| 30 | "-volname", "TestVol", |
| 31 | imgPath, |
| 32 | ) |
| 33 | out, err := cmd.CombinedOutput() |
| 34 | assert.NilError(t, err, "hdiutil create failed: %s", out) |
| 35 | |
| 36 | |
| 37 | |
| 38 | mntDir := filepath.Join(tmpDir, "mnt") |
| 39 | cmd = exec.CommandContext(ctx, "hdiutil", "attach", imgPath, "-mountpoint", mntDir) |
| 40 | out, err = cmd.CombinedOutput() |
| 41 | assert.NilError(t, err, "hdiutil attach failed: %s", out) |
| 42 | |
| 43 | assert.NilError(t, os.WriteFile(filepath.Join(mntDir, "testfile.txt"), []byte("hello"), 0o644)) |
| 44 | |
| 45 | |
| 46 | |
| 47 | cmd = exec.CommandContext(ctx, "hdiutil", "detach", mntDir, "-force") |
| 48 | out, err = cmd.CombinedOutput() |
| 49 | assert.NilError(t, err, "hdiutil detach failed: %s", out) |
| 50 | |
| 51 | |
| 52 | |
| 53 | assert.NilError(t, Chown(imgPath, 0, 0, 0, "testfile.txt")) |
| 54 | |
| 55 | |
| 56 | cmd = exec.CommandContext(ctx, "hdiutil", "attach", imgPath, "-nomount") |
| 57 | out, err = cmd.CombinedOutput() |
| 58 | assert.NilError(t, err, "hdiutil attach -nomount failed: %s", out) |
| 59 | dev := strings.Fields(string(out))[0] |
| 60 | cmd = exec.CommandContext(ctx, "fsck_apfs", "-n", dev) |
| 61 | out, err = cmd.CombinedOutput() |
| 62 | assert.NilError(t, err, "fsck_apfs failed: %s", out) |
| 63 | |
| 64 | cmd = exec.CommandContext(ctx, "hdiutil", "detach", dev, "-force") |
| 65 | out, err = cmd.CombinedOutput() |
| 66 | assert.NilError(t, err, "hdiutil detach failed: %s", out) |
| 67 | |
| 68 | |
| 69 | cmd = exec.CommandContext(ctx, "hdiutil", "attach", imgPath, |
| 70 | "-mountpoint", mntDir, "-owners", "on") |
| 71 | out, err = cmd.CombinedOutput() |
| 72 | assert.NilError(t, err, "hdiutil re-attach failed: %s", out) |
| 73 | defer func() { |
| 74 | _ = exec.CommandContext(ctx, "hdiutil", "detach", mntDir, "-force").Run() |
| 75 | }() |
| 76 | |
| 77 | fi, err := os.Stat(filepath.Join(mntDir, "testfile.txt")) |
| 78 | assert.NilError(t, err) |
| 79 | stat := fi.Sys().(*syscall.Stat_t) |
| 80 | assert.Equal(t, stat.Uid, uint32(0), "expected uid=0, got uid=%d", stat.Uid) |
| 81 | assert.Equal(t, stat.Gid, uint32(0), "expected gid=0, got gid=%d", stat.Gid) |
| 82 | } |