跳到主要内容

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

浏览文件

setup_recovery_test.go

106 行 · 3.2 KB历史下载
1package agentvm
2
3import (
4 "os"
5 "os/exec"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11func setupFunction(t *testing.T, name string, end string) string {
12 t.Helper()
13 start := strings.Index(SetupScript, name+"() ")
14 if start < 0 {
15 t.Fatal("missing function", name)
16 }
17 body := SetupScript[start:]
18 last := strings.Index(body, "\n"+end+"\n")
19 if last < 0 {
20 t.Fatal("missing function end", name)
21 }
22 return body[:last+len(end)+2]
23}
24
25func TestRepositoryKeyFailurePreservesTrustedFile(t *testing.T) {
26 for _, mode := range []string{"eof", "empty", "invalid", "success"} {
27 t.Run(mode, func(t *testing.T) {
28 dir := t.TempDir()
29 key := filepath.Join(dir, "nodesource.asc")
30 if err := os.WriteFile(key, []byte("trusted"), 0644); err != nil {
31 t.Fatal(err)
32 }
33 commands := map[string]string{
34 "curl": `#!/bin/sh
35while [ "$#" -gt 0 ]; do
36 if [ "$1" = -o ]; then shift; target="$1"; fi
37 shift
38done
39case "$KEY_MODE" in
40 eof) printf partial > "$target"; exit 56;;
41 empty) : > "$target";;
42 invalid) printf '<html>bad gateway</html>' > "$target";;
43 success) printf '%s\n' '-----BEGIN PGP PUBLIC KEY BLOCK-----' new '-----END PGP PUBLIC KEY BLOCK-----' > "$target";;
44esac
45`,
46 // Exercise actual local filesystem operations without root.
47 "sudo": `#!/bin/sh
48if [ "$1" = install ]; then
49 shift
50 while [ "$#" -gt 2 ]; do shift; done
51 cp "$1" "$2"
52else
53 exec "$@"
54fi
55`,
56 }
57 for name, body := range commands {
58 if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0755); err != nil {
59 t.Fatal(err)
60 }
61 }
62 cmd := exec.Command("bash", "-c", setupFunction(t, "install_repository_key", ")")+`install_repository_key https://example.invalid/key "$KEY_TARGET"`)
63 cmd.Env = append(os.Environ(), "HOME="+dir, "PATH="+dir+":"+os.Getenv("PATH"), "KEY_MODE="+mode, "KEY_TARGET="+key)
64 out, err := cmd.CombinedOutput()
65 data, readErr := os.ReadFile(key)
66 if readErr != nil {
67 t.Fatal(readErr)
68 }
69 if mode == "success" {
70 if err != nil || !strings.Contains(string(data), "\nnew\n") {
71 t.Fatalf("%v %s %q", err, out, data)
72 }
73 } else if err == nil || string(data) != "trusted" {
74 t.Fatalf("failed download replaced key: %v %q", err, data)
75 }
76 left, _ := filepath.Glob(key + ".*")
77 if len(left) != 0 {
78 t.Fatal("staging key leaked", left)
79 }
80 })
81 }
82}
83
84func TestAPTFailureRecordsOOMWithoutChangingExitCode(t *testing.T) {
85 dir := t.TempDir()
86 commands := map[string]string{
87 "sudo": `#!/bin/sh
88if [ "$1" = env ]; then echo apt-failed; exit 137; fi
89if [ "$1" = timeout ]; then echo 'Out of memory: Killed process 2926 (apt-get)'; exit 0; fi
90exit 99
91`,
92 "awk": "#!/bin/sh\necho 3\n",
93 "ps": "#!/bin/sh\necho 'PID RSS COMMAND'\n",
94 }
95 for name, body := range commands {
96 if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0755); err != nil {
97 t.Fatal(err)
98 }
99 }
100 cmd := exec.Command("bash", "-c", "set -euo pipefail\n"+setupFunction(t, "apt_get", "}")+"apt_get install package")
101 cmd.Env = append(os.Environ(), "HOME="+dir, "PATH="+dir+":"+os.Getenv("PATH"), "CLAUDE_CAGE=1")
102 out, err := cmd.CombinedOutput()
103 if err == nil || cmd.ProcessState.ExitCode() != 137 || !strings.Contains(string(out), "oom_kill before=3") || !strings.Contains(string(out), "Killed process") {
104 t.Fatalf("lost failure evidence: %v %s", err, out)
105 }
106}