跳到主要内容

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

浏览文件

fuzz_test.go

112 行 · 3.1 KB历史下载
1package gitguard
2
3import (
4 "os"
5 "path/filepath"
6 "slices"
7 "testing"
8 "unicode"
9)
10
11// hops must list every symlink the path goes through: a link the VM can
12// write chooses where git looks. The oracle: a link the path depends on is
13// one whose retargeting changes where the path leads.
14func FuzzHops(f *testing.F) {
15 for _, seed := range [][]byte{
16 {0, 1, 2, 3, 4, 5, 6, 7, 8},
17 {4, 4, 4, 4, 1, 2},
18 {2, 3, 9, 1, 0, 5, 11, 7, 4},
19 {7, 6, 5, 4, 8, 8, 0, 10},
20 } {
21 f.Add(seed)
22 }
23 f.Fuzz(func(t *testing.T, in []byte) {
24 if len(in) > 24 {
25 return
26 }
27 next := func() int {
28 if len(in) == 0 {
29 return 0
30 }
31 b := in[0]
32 in = in[1:]
33 return int(b)
34 }
35 root, err := filepath.EvalSymlinks(t.TempDir())
36 if err != nil {
37 t.Fatal(err)
38 }
39 root = filepath.ToSlash(root)
40 sentinel := root + "/sentinel"
41 for _, d := range []string{"d0/d0", "d1/d0", "d2", "d3", "sentinel"} {
42 if err := os.MkdirAll(root+"/"+d, 0o755); err != nil {
43 t.Fatal(err)
44 }
45 }
46 targets := []string{"d0", "../d1", "l1", root + "/d2", "/", ".", "..", "l0", root + "/l3/d0", "d0/d0", "missing", "../l2"}
47 var links []string
48 for _, l := range []string{"l0", "l1", "l2", "l3", "d0/l0", "d1/l1"} {
49 p := root + "/" + l
50 if err := os.Symlink(targets[next()%len(targets)], p); err != nil {
51 t.Fatal(err)
52 }
53 links = append(links, p)
54 }
55 comps := []string{"d0", "d1", "d2", "d3", "l0", "l1", "l2", "l3", "..", ".", "x"}
56 p := root
57 for n := next()%5 + 1; n > 0; n-- {
58 p += "/" + comps[next()%len(comps)]
59 }
60 want, err := filepath.EvalSymlinks(p)
61 if err != nil {
62 return // a loop, or not there: nothing to compare with
63 }
64 got := hops(p)
65 if last := got[len(got)-1]; filepath.Clean(last) != filepath.Clean(want) {
66 t.Fatalf("hops(%q) ends at %q, the path leads to %q (%q)", p, last, want, got)
67 }
68 for _, l := range links {
69 old, _ := os.Readlink(l)
70 os.Remove(l)
71 os.Symlink(sentinel, l)
72 moved, err := filepath.EvalSymlinks(p)
73 os.Remove(l)
74 os.Symlink(old, l)
75 if (err != nil || moved != want) && !slices.Contains(got, l) {
76 t.Fatalf("%q goes through %q, which hops leaves out: %q", p, l, got)
77 }
78 }
79 })
80}
81
82// A hooks name Lima would read otherwise is no name: the folder is a risk
83// to accept instead.
84func TestHooksName(t *testing.T) {
85 for rel, want := range map[string]string{
86 ".husky/_": ".husky",
87 "tools/h": "tools",
88 "R&D/hooks": "R&D",
89 ".": "",
90 `a"b/x`: "",
91 "a\xffb/x": "",
92 "a\u2028b/x": "",
93 } {
94 if got, _ := HooksName(rel); got != want {
95 t.Errorf("HooksName(%q) = %q, want %q", rel, got, want)
96 }
97 }
98}
99
100func FuzzVisible(f *testing.F) {
101 for _, s := range []string{"core.editor = vi", "a\x1b[2Jb", "\u009b31m", "x\ny", "\xff\x9b", "a\xe2\x80\xaeb", "x\xe2\x80\x8by", "\xe2\x81\xa6z", "\xf3\xa0\x81\x81"} {
102 f.Add(s)
103 }
104 f.Fuzz(func(t *testing.T, s string) {
105 for _, r := range visible(s) {
106 // Control, format (bidi overrides, zero-width), separators...
107 if unicode.IsControl(r) || unicode.Is(unicode.Cf, r) || unicode.Is(unicode.Z, r) && r != ' ' || unicode.Is(unicode.Co, r) {
108 t.Fatalf("%U left in %q", r, visible(s))
109 }
110 }
111 })
112}