跳到主要内容

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

浏览文件

storage_test.go

250 行 · 8.6 KB历史下载
1package cage
2
3import (
4 "bytes"
5 "context"
6 "os"
7 "path/filepath"
8 "runtime"
9 "strconv"
10 "strings"
11 "testing"
12)
13
14func newStorageFixture(t *testing.T) (string, string) {
15 t.Helper()
16 root, err := filepath.EvalSymlinks(t.TempDir())
17 if err != nil {
18 t.Fatal(err)
19 }
20 profile := filepath.Join(root, "profile")
21 if err := os.Mkdir(profile, 0700); err != nil {
22 t.Fatal(err)
23 }
24 return profile, root
25}
26
27func writeStorageFixture(t *testing.T, path string, data []byte) {
28 t.Helper()
29 if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
30 t.Fatal(err)
31 }
32 if err := os.WriteFile(path, data, 0600); err != nil {
33 t.Fatal(err)
34 }
35}
36
37func assertStorageFile(t *testing.T, path string, want []byte) {
38 t.Helper()
39 b, err := os.ReadFile(path)
40 if err != nil || !bytes.Equal(b, want) {
41 t.Fatalf("storage content mismatch in %s: %v", path, err)
42 }
43}
44
45func TestStorageMovePreservesLegacyDiskAndColdBackup(t *testing.T) {
46 profile, root := newStorageFixture(t)
47 file := filepath.Join("lima", "instance", "disk")
48 payload := make([]byte, (3<<20)+41)
49 copy(payload, []byte("guest header"))
50 copy(payload[3<<20:], []byte("guest tail"))
51 writeStorageFixture(t, filepath.Join(profile, "state", file), payload)
52 writeStorageFixture(t, filepath.Join(profile, "account.json"), []byte("account"))
53 target := filepath.Join(root, "中文 folder")
54 loc, err := MoveStorage(context.Background(), profile, target, storageExpectation(t, profile), nil)
55 if err != nil {
56 t.Fatal(err)
57 }
58 if !loc.Custom || !loc.HasEnvironment || loc.Backup == "" {
59 t.Fatal(loc)
60 }
61 if info, err := os.Stat(filepath.Join(target, "state", file)); err != nil || info.Size() != int64(len(payload)) {
62 t.Fatalf("stored disk size: %v %v", info, err)
63 }
64 assertStorageFile(t, filepath.Join(loc.Backup, file), payload)
65 assertStorageFile(t, filepath.Join(profile, "state", file), payload)
66 assertStorageFile(t, filepath.Join(profile, "account.json"), []byte("account"))
67 old := loc.Backup
68 target2 := filepath.Join(root, "second")
69 loc, err = MoveStorage(context.Background(), profile, target2, storageExpectation(t, profile), nil)
70 if err != nil {
71 t.Fatal(err)
72 }
73 if loc.Backup != target {
74 t.Fatal(loc)
75 }
76 assertStorageFile(t, filepath.Join(target, "state", file), payload)
77 assertStorageFile(t, filepath.Join(profile, "state", file), payload)
78 paths, err := StoragePrivatePaths(profile)
79 if err != nil || len(paths) != 3 || paths[0] != old {
80 t.Fatalf("private paths %v, %v", paths, err)
81 }
82 if err := os.Rename(target2, target2+"-offline"); err != nil {
83 t.Fatal(err)
84 }
85 if _, _, err := AcquireStorageUse(profile); err == nil {
86 t.Fatal("started with missing volume")
87 }
88}
89
90func TestStorageRefusesBusyAndRollsBackCancelledCopy(t *testing.T) {
91 profile, root := newStorageFixture(t)
92 lease, _, err := AcquireStorageUse(profile)
93 if err != nil {
94 t.Fatal(err)
95 }
96 target := filepath.Join(root, "new")
97 if _, err = MoveStorage(context.Background(), profile, target, storageExpectation(t, profile), nil); err == nil {
98 t.Fatal("concurrent move admitted")
99 }
100 lease.Close()
101 instance := filepath.Join(profile, "state", "lima", "vm")
102 pid := filepath.Join(instance, "qemu.pid")
103 writeStorageFixture(t, pid, []byte(strconv.Itoa(os.Getpid())))
104 if _, err = MoveStorage(context.Background(), profile, target, storageExpectation(t, profile), nil); err == nil {
105 t.Fatal("running VM admitted")
106 }
107 if err = os.Remove(pid); err != nil {
108 t.Fatal(err)
109 }
110 writeStorageFixture(t, filepath.Join(instance, "disk"), []byte("original"))
111 ctx, cancel := context.WithCancel(context.Background())
112 defer cancel()
113 if _, err = MoveStorage(ctx, profile, target, storageExpectation(t, profile), func(int64, int64) { cancel() }); err == nil {
114 t.Fatal("ignored cancel")
115 }
116 assertStorageFile(t, filepath.Join(instance, "disk"), []byte("original"))
117 if _, err := MoveStorage(context.Background(), profile, target, storageExpectation(t, profile), nil); err != nil {
118 t.Fatal("retry after cancellation:", err)
119 }
120}
121
122func TestStorageFirstUseAndCrashRollback(t *testing.T) {
123 profile, root := newStorageFixture(t)
124 first := filepath.Join(root, "fresh")
125 loc, err := MoveStorage(context.Background(), profile, first, storageExpectation(t, profile), nil)
126 if err != nil {
127 t.Fatal(err)
128 }
129 if loc.HasEnvironment || loc.Backup != "" || !loc.Custom {
130 t.Fatal(loc)
131 }
132 payload := []byte("preserved")
133 writeStorageFixture(t, filepath.Join(profile, "state", "user-data"), payload)
134 target := filepath.Join(root, "interrupted")
135 id := "1234567890abcdef1234567890abcdef"
136 if err = os.MkdirAll(filepath.Join(target, "state"), 0700); err != nil {
137 t.Fatal(err)
138 }
139 if err = claimStorageTarget(target, id); err != nil {
140 t.Fatal(err)
141 }
142 if err = claimStorageTarget(filepath.Join(target, "state"), id); err != nil {
143 t.Fatal(err)
144 }
145 p := storageMove{Next: storageRecord{Directory: target, ID: id}, PreviousCustom: true}
146 if err = writeStorageJSON(filepath.Join(profile, pendingFile), p); err != nil {
147 t.Fatal(err)
148 }
149 if err = os.Rename(storageAnchor(profile), filepath.Join(profile, "state-backup-"+id)); err != nil {
150 t.Fatal(err)
151 }
152 if err = linkStorage(storageAnchor(profile), filepath.Join(target, "state")); err != nil {
153 t.Fatal(err)
154 }
155 loc, err = GetStorageLocation(profile)
156 if err != nil || loc.Directory != first {
157 t.Fatalf("recovery: %+v %v", loc, err)
158 }
159 assertStorageFile(t, filepath.Join(profile, "state", "user-data"), payload)
160 if _, err := MoveStorage(context.Background(), profile, target, storageExpectation(t, profile), nil); err != nil {
161 t.Fatal("retry after crash:", err)
162 }
163}
164
165func TestStorageRollbackLeavesUnownedDestinationUntouched(t *testing.T) {
166 profile, root := newStorageFixture(t)
167 target := filepath.Join(root, "external")
168 id := "abcdef1234567890abcdef1234567890"
169 foreign := filepath.Join(target, "state", "user-files")
170 writeStorageFixture(t, foreign, []byte("existing"))
171 if err := claimStorageTarget(target, id); err != nil {
172 t.Fatal(err)
173 }
174 if err := writeStorageJSON(filepath.Join(profile, pendingFile), storageMove{Next: storageRecord{Directory: target, ID: id}}); err != nil {
175 t.Fatal(err)
176 }
177 if _, err := GetStorageLocation(profile); err != nil {
178 t.Fatal(err)
179 }
180 assertStorageFile(t, foreign, []byte("existing"))
181}
182
183func storageExpectation(t *testing.T, profile string) StorageLocation {
184 t.Helper()
185 loc, err := GetStorageLocation(profile)
186 if err != nil {
187 t.Fatal(err)
188 }
189 return loc
190}
191
192// An attached test volume lets native QA exercise a genuine cross-volume
193// copy. The parent is never modified beyond the unique temporary folder.
194func TestStorageAcrossMountedVolume(t *testing.T) {
195 base := os.Getenv("CAGE_TEST_STORAGE_VOLUME")
196 if base == "" {
197 t.Skip("set CAGE_TEST_STORAGE_VOLUME to a writable second volume")
198 }
199 profile, _ := newStorageFixture(t)
200 testDir, err := os.MkdirTemp(base, "cage-storage-test-*")
201 if err != nil {
202 t.Fatal(err)
203 }
204 t.Cleanup(func() { os.RemoveAll(testDir) })
205 if runtime.GOOS == "windows" && strings.EqualFold(filepath.VolumeName(profile), filepath.VolumeName(testDir)) {
206 t.Fatal("CAGE_TEST_STORAGE_VOLUME must be on a different drive from the temporary profile")
207 }
208 data := make([]byte, 2<<20)
209 copy(data[len(data)-32:], []byte("second volume"))
210 rel := filepath.Join("lima", "guest", "disk")
211 writeStorageFixture(t, filepath.Join(profile, "state", rel), data)
212 loc, err := MoveStorage(context.Background(), profile, testDir, storageExpectation(t, profile), nil)
213 if err != nil {
214 t.Fatal(err)
215 }
216 assertStorageFile(t, filepath.Join(testDir, "state", rel), data)
217 assertStorageFile(t, filepath.Join(loc.Backup, rel), data)
218 assertStorageFile(t, filepath.Join(profile, "state", rel), data)
219}
220
221func TestStorageRejectsStaleFirstUseDecision(t *testing.T) {
222 profile, root := newStorageFixture(t)
223 fresh := storageExpectation(t, profile)
224 data := []byte("already running user data")
225 disk := filepath.Join(profile, "state", "lima", "vm", "disk")
226 writeStorageFixture(t, disk, data)
227 if _, err := MoveStorage(context.Background(), profile, filepath.Join(root, "target"), fresh, nil); err == nil {
228 t.Fatal("silently migrated a disk created after first-use confirmation")
229 }
230 assertStorageFile(t, disk, data)
231}
232
233func TestStorageResolvesAliasesToPrivateDisk(t *testing.T) {
234 profile, root := newStorageFixture(t)
235 target := filepath.Join(root, "private disk")
236 loc, err := MoveStorage(context.Background(), profile, target, storageExpectation(t, profile), nil)
237 if err != nil {
238 t.Fatal(err)
239 }
240 alias := filepath.Join(root, "project-looking-alias")
241 if err := linkStorage(alias, loc.Directory); err != nil {
242 t.Fatal(err)
243 }
244 defer os.Remove(alias)
245 logical := filepath.Join(alias, "state", "future-file")
246 physical, err := StoragePhysicalPath(logical)
247 if err != nil || !StorageContains(target, physical) {
248 t.Fatalf("private path escaped through alias: %s: %v", physical, err)
249 }
250}