跳到主要内容

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

浏览文件

rooted_relay_test.go

158 行 · 4.4 KB历史下载
1//go:build linux || darwin || windows
2
3package reversesshfs
4
5import (
6 "fmt"
7 "os"
8 "path/filepath"
9 "testing"
10 "time"
11)
12
13// A removal the server made for the guest is not relayed: the host's event for it must not
14// leave a token that swallows the guest's next removal of a file it created again.
15func TestRootedOwnRemovalNotRelayed(t *testing.T) {
16 c, root, h := setupRootedHandlers(t, false)
17 p := func(s string) string { return filepath.Join(root, s) }
18 create := func(s string) {
19 t.Helper()
20 f, err := c.Create(p(s))
21 if err != nil {
22 t.Fatal(err)
23 }
24 f.Close()
25 }
26 create("f")
27 if err := c.Remove(p("f")); err != nil {
28 t.Fatal(err)
29 }
30 if h.expectRemove(p("f")) {
31 t.Fatal("the host's event for the guest's own removal was relayed")
32 }
33 create("f")
34 if err := c.Remove(p("f")); err != nil {
35 t.Fatal(err)
36 }
37 if _, err := os.Lstat(p("f")); err == nil {
38 t.Fatal("rm f; touch f; rm f left the file on the host")
39 }
40
41 // A rename away is a removal as well: log rotation.
42 create("log")
43 if err := c.PosixRename(p("log"), p("log.1")); err != nil {
44 t.Fatal(err)
45 }
46 create("log")
47 if h.expectRemove(p("log")) {
48 t.Fatal("the host's event for the guest's rename was relayed")
49 }
50 if err := c.Remove(p("log")); err != nil {
51 t.Fatal(err)
52 }
53 if _, err := os.Lstat(p("log")); err == nil {
54 t.Fatal("the new log was not removed")
55 }
56
57 // Once the event window is over, a host deletion is relayed again.
58 now := time.Now()
59 h.now = func() time.Time { return now.Add(ownRemovalTTL + time.Second) }
60 if !h.expectRemove(p("log")) {
61 t.Error("not relayed after the window")
62 }
63}
64
65// A removal that fails removed nothing: the host's deletion of the path that follows is still relayed.
66func TestRootedFailedRemovalNotOwn(t *testing.T) {
67 c, root, h := setupRootedHandlers(t, false)
68 p := func(s string) string { return filepath.Join(root, s) }
69 if err := os.MkdirAll(p("d/sub"), 0o755); err != nil {
70 t.Fatal(err)
71 }
72 if err := c.RemoveDirectory(p("d")); err == nil {
73 t.Fatal("a folder with something in it was removed")
74 }
75 if !h.expectRemove(p("d")) {
76 t.Error("a removal that failed hid the host's deletion")
77 }
78}
79
80// A file the guest creates after the host deleted it, within the token's life, is its own.
81func TestRootedTokenDroppedByCreate(t *testing.T) {
82 c, root, h := setupRootedHandlers(t, false)
83 p := func(s string) string { return filepath.Join(root, s) }
84 if !h.expectRemove(p("new")) {
85 t.Fatal("not expected")
86 }
87 f, err := c.Create(p("new"))
88 if err != nil {
89 t.Fatal(err)
90 }
91 f.Close()
92 if err := c.Remove(p("new")); err != nil {
93 t.Fatal(err)
94 }
95 if _, err := os.Lstat(p("new")); err == nil {
96 t.Fatal("the removal was swallowed though the guest created the file")
97 }
98
99 // Same for the other ways to create a name.
100 for name, create := range map[string]func(string) error{
101 "mkdir": func(s string) error { return c.Mkdir(s) },
102 "symlink": func(s string) error { return c.Symlink("x", s) },
103 "rename": func(s string) error { return c.PosixRename(p("src/main.go"), s) },
104 } {
105 target := p("t-" + name)
106 h.expectRemove(target)
107 if err := create(target); err != nil {
108 t.Fatal(name, err)
109 }
110 if err := c.Remove(target); err != nil {
111 if err := c.RemoveDirectory(target); err != nil {
112 t.Fatal(name, err)
113 }
114 }
115 if _, err := os.Lstat(target); err == nil {
116 t.Errorf("%s: the removal was swallowed", name)
117 }
118 }
119}
120
121// A host rm -rf of many files must not stall the server.
122func TestExpectRemoveMany(t *testing.T) {
123 _, root, h := setupRootedHandlers(t, false)
124 start := time.Now()
125 const n = 200000
126 for i := 0; i < n; i++ {
127 h.expectRemove(filepath.Join(root, "src", fmt.Sprint(i)))
128 }
129 if d := time.Since(start); d > 5*time.Second {
130 t.Fatalf("%d tokens took %v", n, d)
131 }
132 // They expire in order, and the tables empty out.
133 now := time.Now()
134 h.now = func() time.Time { return now.Add(noopRemovalTTL + time.Second) }
135 h.expectRemove(filepath.Join(root, "x"))
136 h.mu.Lock()
137 defer h.mu.Unlock()
138 if len(h.relay.tokens.m) != 1 {
139 t.Errorf("%d tokens left", len(h.relay.tokens.m))
140 }
141}
142
143func TestExpiringKeyAddedAgain(t *testing.T) {
144 e := newExpiring(10 * time.Second)
145 t0 := time.Now()
146 e.add("k", t0)
147 e.add("k", t0.Add(8*time.Second))
148 // The first entry expired, the key is still there with the later expiry.
149 if !e.has("k", t0.Add(12*time.Second)) {
150 t.Error("key added again was purged by its first entry")
151 }
152 if e.has("k", t0.Add(19*time.Second)) {
153 t.Error("key not expired")
154 }
155 if len(e.m) != 0 {
156 t.Error("left over")
157 }
158}