跳到主要内容

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

浏览文件

scan_native_test.go

337 行 · 11.4 KB历史下载
1package gitguard
2
3// Tests of the scan that do not compare with 0.2's: what it must find, and
4// what it must say when it could not look.
5
6import (
7 "context"
8 "os"
9 "runtime"
10 "slices"
11 "sort"
12 "strings"
13 "testing"
14 "time"
15
16 "github.com/sylvinus/agent-vm/internal/mounts"
17)
18
19// scanBox is Scan of the box's project, with git reading the box's config.
20func (b *box) scan(volumes ...string) *Result {
21 b.t.Helper()
22 b.useEnv()
23 return Scan(context.Background(), Shares(b.proj, volumes), b.home)
24}
25
26func (b *box) newScan(volumes ...string) *scan {
27 b.useEnv()
28 shares := Shares(b.proj, volumes)
29 return &scan{ctx: context.Background(), shares: shares, proj: shares[0], home: b.home, seen: map[string]bool{}}
30}
31
32func risksText(r *Result) string { return strings.Join(r.Risks(mounts.BaseNames), "\n") }
33
34// A path with a newline or a tab in it is checked all the same: it was
35// skipped, with nothing said.
36func TestScanOddPaths(t *testing.T) {
37 if runtime.GOOS == "windows" {
38 t.Skip("no newline in Windows paths")
39 }
40 for _, name := range []string{"pro\nj", "pro\tj", "pro\n"} {
41 b := newBox(t)
42 b.proj = b.home + "/" + name
43 os.MkdirAll(b.proj, 0o755)
44 b.git(b.proj, "init", "-q")
45 b.git(b.proj, "config", "core.hooksPath", ".githooks")
46 b.git(b.proj, "config", "core.fsmonitor", "tools/mon.sh")
47 r := b.scan()
48 if len(r.Hooks) != 1 || r.Hooks[0] != ".githooks\t.githooks" {
49 t.Errorf("%q: hooks %q", name, r.Hooks)
50 }
51 if got := risksText(r); got != "core.fsmonitor = tools/mon.sh" {
52 t.Errorf("%q: risks %q", name, got)
53 }
54 }
55 // A hooks folder whose own name has them: listed with no name to give.
56 b := newBox(t)
57 b.git(b.proj, "config", "core.hooksPath", "hk\tx/h")
58 b.git(b.proj, "config", "--add", "core.hooksPath", "hk\nx/h")
59 r := b.scan()
60 for _, h := range r.Hooks {
61 if strings.ContainsAny(h[:strings.LastIndexByte(h, '\t')], "\t\n") || !strings.HasSuffix(h, "\t") {
62 t.Errorf("hooks line %q", h)
63 }
64 }
65 if len(r.Hooks) == 0 {
66 t.Error("a hooks folder with a tab in its name was not listed")
67 }
68}
69
70// What git could not tell is a risk, not an empty answer.
71func TestScanGitFailures(t *testing.T) {
72 // A config file git cannot read.
73 b := newBox(t)
74 cfg := b.proj + "/.git/config"
75 old, _ := os.ReadFile(cfg)
76 os.WriteFile(cfg, append(old, "[core\n"...), 0o644)
77 got := risksText(b.scan())
78 if !strings.Contains(got, "could not check the repository of the project: ") || !strings.Contains(got, "bad config") {
79 t.Errorf("bad repository config:\n%s", got)
80 }
81
82 // git's own config.
83 b = newBox(t)
84 b.write(b.home+"/.gitconfig", "[core\n")
85 got = risksText(b.scan())
86 if !strings.Contains(got, "could not check git's own config: ") {
87 t.Errorf("bad global config:\n%s", got)
88 }
89}
90
91// A git that does not answer is a risk, and does not stall the scan.
92func TestScanGitTimeout(t *testing.T) {
93 if runtime.GOOS == "windows" {
94 t.Skip("shell script for git")
95 }
96 b := newBox(t)
97 fake := t.TempDir()
98 os.WriteFile(fake+"/git", []byte("#!/bin/sh\nexec sleep 60\n"), 0o755)
99 t.Setenv("PATH", fake+string(os.PathListSeparator)+os.Getenv("PATH"))
100 old := gitTimeout
101 gitTimeout = 300 * time.Millisecond
102 t.Cleanup(func() { gitTimeout = old })
103 start := time.Now()
104 got := risksText(b.scan())
105 if d := time.Since(start); d > 20*time.Second {
106 t.Errorf("took %v", d)
107 }
108 if !strings.Contains(got, "could not check the repository of the project: git did not answer in time") {
109 t.Errorf("risks:\n%s", got)
110 }
111 if st := BareState(context.Background()); st != BareUnavailable {
112 t.Errorf("BareState with a hung git: %s", st)
113 }
114}
115
116// An option's value, or an assignment, can be the command: ssh -o
117// ProxyCommand=./proxy.sh, --upload-pack=./x.
118func TestScanOptionValues(t *testing.T) {
119 b := newBox(t)
120 b.git(b.proj, "config", "core.sshCommand", "ssh -o ProxyCommand=tools/proxy.sh")
121 b.git(b.proj, "config", "remote.o.uploadpack", "git-upload-pack --exec=./up.sh")
122 b.git(b.proj, "config", "core.pager", `less -o "ProxyCommand=./pager.sh arg"`)
123 b.git(b.proj, "config", "core.askPass", "env X=tools/ask.sh askpass")
124 // No path in them, a path outside the shares, or a URL.
125 b.git(b.proj, "config", "credential.helper", "store --file=y --path=/usr/bin/true")
126 b.git(b.proj, "config", "alias.p", "!git push --repo=https://example.com/x")
127 b.git(b.proj, "config", "core.editor", "vi -o StrictHostKeyChecking=no")
128 got := risksText(b.scan())
129 want := []string{
130 "core.sshcommand = ssh -o ProxyCommand=tools/proxy.sh",
131 "remote.o.uploadpack = git-upload-pack --exec=./up.sh",
132 `core.pager = less -o "ProxyCommand=./pager.sh arg"`,
133 "core.askpass = env X=tools/ask.sh askpass",
134 }
135 lines := strings.Split(got, "\n")
136 slices.Sort(lines)
137 slices.Sort(want)
138 if strings.Join(lines, "\n") != strings.Join(want, "\n") {
139 t.Errorf("risks:\n%s\nwant:\n%s", got, strings.Join(want, "\n"))
140 }
141}
142
143// A hook that is another name for a file is that file: the VM, writing it
144// where it is linked from a share, changes what git runs.
145func TestScanHookHardLink(t *testing.T) {
146 if runtime.GOOS == "windows" {
147 t.Skip("not checked on Windows")
148 }
149 b := newBox(t)
150 b.write(b.proj+"/scripts/pre-commit", "#!/bin/sh\n")
151 b.write(b.proj+"/.git/hooks/post-commit", "#!/bin/sh\n")
152 b.write(b.proj+"/scripts/sample", "#!/bin/sh\n")
153 b.write(b.proj+"/scripts/hidden", "#!/bin/sh\n")
154 for hook, file := range map[string]string{"pre-commit": "pre-commit", "pre-push.sample": "sample", ".dot": "hidden"} {
155 if err := os.Link(b.proj+"/scripts/"+file, b.proj+"/.git/hooks/"+hook); err != nil {
156 t.Skip("no hard links here:", err)
157 }
158 }
159 got := risksText(b.scan())
160 if got != "hook pre-commit has other hard links: one may be in a share" {
161 t.Errorf("risks:\n%s", got)
162 }
163}
164
165// Where core.hooksPath can point, and what the scan makes of each.
166func TestScanHooksPathVariants(t *testing.T) {
167 nfd := "e\xcc\x81cole/hooks" // e + combining acute
168 for _, c := range []struct {
169 value string
170 want string // the hooks line, "" for none
171 }{
172 {".githooks", ".githooks\t.githooks"},
173 {"./.githooks/", ".githooks\t.githooks"},
174 {"tools/../.githooks", ".githooks\t.githooks"},
175 {"~/proj/.githooks", ".githooks\t.githooks"},
176 {"PROJ/.githooks", ".githooks\t.githooks"},
177 {".GITHOOKS", ".GITHOOKS\t.GITHOOKS"},
178 {".", ".\t."},
179 {"../outside", ""},
180 {"~/other/hooks", ""},
181 {nfd, nfd + "\t" + nfd},
182 {"ev\xe2\x80\xaeil/hooks", "ev?il/hooks\t"}, // U+202E
183 {"zero\xe2\x80\x8bwidth/hooks", "zero?width/hooks\t"}, // U+200B
184 {"my hooks/x", "my hooks/x\tmy hooks/x"},
185 {".git/hooks", ""},
186 {".GIT/hooks", ""},
187 } {
188 b := newBox(t)
189 v := strings.ReplaceAll(c.value, "PROJ", b.proj)
190 if v == nfd {
191 b.setRaw("core", "hooksPath", v)
192 } else {
193 b.git(b.proj, "config", "core.hooksPath", v)
194 }
195 r := b.scan()
196 got := strings.Join(r.Hooks, "\n")
197 if got != c.want {
198 t.Errorf("core.hooksPath = %q: hooks %q, want %q", c.value, got, c.want)
199 }
200 }
201 // Names: the folder's first component as spelled, so that the server,
202 // which compares names as the file system does, keeps every spelling.
203 b := newBox(t)
204 b.setRaw("core", "hooksPath", nfd)
205 if names := b.scan().Names(); !slices.Contains(names, "e\xcc\x81cole") {
206 t.Errorf("names: %q", names)
207 }
208 b = newBox(t)
209 b.git(b.proj, "config", "core.hooksPath", "ev\xe2\x80\xaeil/hooks")
210 for _, n := range b.scan().Names() {
211 if strings.ContainsRune(n, 0x202e) {
212 t.Errorf("a bidi override in the names: %q", n)
213 }
214 }
215}
216
217// A name accepted keeps a path read-only whatever its case; a spelling the
218// comparison does not take for the name stays a risk to accept.
219func TestRisksNameSpellings(t *testing.T) {
220 b := newBox(t)
221 b.git(b.proj, "config", "core.fsmonitor", "Tools/run.sh")
222 b.setRaw("core", "pager", "e\xcc\x81cole/run.sh")
223 r := b.scan()
224 got := strings.Join(r.Risks([]string{".git", ".hg", "tools", "\xc3\xa9cole"}), "\n")
225 if strings.Contains(got, "fsmonitor") {
226 t.Errorf("Tools/ is not under the name tools:\n%s", got)
227 }
228 if !strings.Contains(got, "core.pager") {
229 t.Errorf("the decomposed spelling is taken for the composed name:\n%s", got)
230 }
231 if got := strings.Join(r.Risks([]string{".git", ".hg", "tools", "e\xcc\x81cole"}), "\n"); strings.Contains(got, "core.pager") {
232 t.Errorf("the name as spelled does not cover it:\n%s", got)
233 }
234}
235
236// A bare repository in a share is a repository: its config's commands and its
237// hooks count.
238func TestScanBareRepo(t *testing.T) {
239 b := newBox(t)
240 for _, d := range []string{"mirror.git", "deep/er/bare.git"} {
241 p := b.proj + "/" + d
242 os.MkdirAll(p, 0o755)
243 b.git(p, "init", "-q", "--bare")
244 b.git(p, "config", "core.pager", "tools/p.sh")
245 b.git(p, "config", "core.hooksPath", "hk")
246 }
247 r := b.scan()
248 for _, want := range []string{"mirror.git/hk\tmirror.git/hk", "deep/er/bare.git/hk\tdeep/er/bare.git/hk"} {
249 if !slices.Contains(r.Hooks, want) {
250 t.Errorf("hooks %q lack %q", r.Hooks, want)
251 }
252 }
253 // A bare repository's config is a file of the share, with no name to keep
254 // it read-only; one risk per distinct text: both repositories say the
255 // same of core.pager.
256 got := risksText(r)
257 for _, want := range []string{"config file mirror.git/config", "config file deep/er/bare.git/config"} {
258 if !strings.Contains(got, want) {
259 t.Errorf("risks lack %q:\n%s", want, got)
260 }
261 }
262 if strings.Count(got, "core.pager = tools/p.sh") != 1 {
263 t.Errorf("risks:\n%s", got)
264 }
265}
266
267// Where repositories are looked for: down to maxRepoDepth folders, not in a
268// node_modules below the first level (find's -mindepth 2), nor in a .git; a
269// bound on the folders read.
270func TestReposWalk(t *testing.T) {
271 b := newBox(t)
272 for _, d := range []string{
273 "node_modules/pkg", "a/node_modules/x", "a/b", "a/b/c/d", "l1/l2/l3/l4/l5/l6", "m1/m2/m3/m4/m5/m6/m7",
274 } {
275 os.MkdirAll(b.proj+"/"+d, 0o755)
276 b.git(b.proj+"/"+d, "init", "-q")
277 }
278 s := b.newScan()
279 var got []string
280 for _, r := range s.repos(s.proj) {
281 got = append(got, strings.TrimPrefix(strings.TrimPrefix(r.at, s.proj), "/"))
282 }
283 sort.Strings(got)
284 want := []string{"", "a/b", "a/b/c/d", "l1/l2/l3/l4/l5/l6", "node_modules/pkg"}
285 if strings.Join(got, ",") != strings.Join(want, ",") {
286 t.Errorf("repositories %q, want %q", got, want)
287 }
288 if len(s.risks) != 0 {
289 t.Errorf("risks: %+v", s.risks)
290 }
291
292 old := maxFolders
293 maxFolders = 3
294 t.Cleanup(func() { maxFolders = old })
295 s = b.newScan()
296 s.repos(s.proj)
297 if got := risksText(&Result{risks: s.risks}); !strings.Contains(got, "repositories in the project: stopped looking after 3 folders") {
298 t.Errorf("risks:\n%s", got)
299 }
300}
301
302func TestVisible(t *testing.T) {
303 for in, want := range map[string]string{
304 "core.editor = vi": "core.editor = vi",
305 "été 日本 😀": "été 日本 😀",
306 "a\x1b[2Jb": "a?[2Jb",
307 "a‮b": "a?b",
308 "a​b⁦c": "a?b?c",
309 "nb sp": "nb?sp",
310 "a
b": "a?b",
311 "a\U000e0041b": "a?b",
312 "a\U0000feffb": "a?b",
313 "tab\there\nnl": "tab?here?nl",
314 "\u009b31m": "?31m",
315 "private\U000f0000": "private?",
316 } {
317 if got := visible(in); got != want {
318 t.Errorf("visible(%q) = %q, want %q", in, got, want)
319 }
320 }
321}
322
323func TestBareStateScopes(t *testing.T) {
324 b := newBox(t)
325 b.useEnv()
326 if st := BareState(context.Background()); st != BareUnset && st != BareOld {
327 t.Errorf("unset: %s", st)
328 }
329 b.write(b.home+"/.gitconfig", "[safe]\n\tbareRepository = explicit\n")
330 if st := BareState(context.Background()); st != BareOK && st != BareOld {
331 t.Errorf("explicit in the global config: %s", st)
332 }
333 b.write(b.home+"/.gitconfig", "[safe]\n\tbareRepository = all\n")
334 if st := BareState(context.Background()); st != BareUnset && st != BareOld {
335 t.Errorf("all: %s", st)
336 }
337}