跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

chown_darwin_test.go

82 行 · 3.0 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package apfs
5
6import (
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "syscall"
12 "testing"
13
14 "gotest.tools/v3/assert"
15)
16
17// TestChownIntegration creates a real APFS disk image with hdiutil,
18// writes a test file, runs Chown on the raw image, and verifies
19// ownership via os.Stat after remounting with ownership enabled.
20// No root required.
21func TestChownIntegration(t *testing.T) {
22 tmpDir := t.TempDir()
23 imgPath := filepath.Join(tmpDir, "test.dmg")
24
25 // Create a 64MB APFS disk image (GPT + APFS container).
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 // Attach, write a test file, and detach. Default mount uses
37 // noowners, so the on-disk UID will be 99 (nobody).
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 // -force, because Spotlight starts indexing the volume as soon as the file
46 // above is written, and an unforced eject fails with "Resource busy".
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 // Change ownership to root:wheel via raw APFS modification.
52 // hdiutil creates a single volume with role=0 (APFS_VOL_ROLE_NONE).
53 assert.NilError(t, Chown(imgPath, 0, 0, 0, "testfile.txt"))
54
55 // Verify filesystem integrity after raw block modification.
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 // -force, because the device can still be busy right after fsck_apfs.
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 // Re-attach with ownership enabled so os.Stat reflects on-disk UIDs.
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}