跳到主要内容

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

浏览文件

diagnostics_test.go

158 行 · 5.3 KB历史下载
1package workbench
2
3import (
4 "archive/zip"
5 "bytes"
6 "encoding/json"
7 "io"
8 "net/http"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 "sync"
14 "testing"
15
16 "github.com/sylvinus/agent-vm/internal/diagnostics"
17)
18
19func TestDiagnosticRoutesAuthenticateRedactAndExportWhenLocked(t *testing.T) {
20 s := testServer(t)
21 s.account.lock()
22 for _, path := range []string{"/api/diagnostics", "/api/diagnostics/export"} {
23 if w := request(s, "GET", path, "", "", false); w.Code != 401 {
24 t.Fatalf("anonymous %s: %d", path, w.Code)
25 }
26 }
27 body := `{"source":"frontend","level":"error","message":"token=sample-private-token https://user:[email protected]/secret Error: UI failed"}`
28 if w := request(s, "POST", "/api/diagnostics/events", "http://evil.example", body, true); w.Code != 403 {
29 t.Fatal("cross-origin event accepted")
30 }
31 if w := request(s, "POST", "/api/diagnostics/events", s.origin, body, true); w.Code != 200 {
32 t.Fatalf("event rejected: %d %s", w.Code, w.Body.String())
33 }
34 w := request(s, "GET", "/api/diagnostics/export", "", "", true)
35 if w.Code != 200 || w.Header().Get("Content-Type") != "application/zip" {
36 t.Fatalf("export failed: %d %s", w.Code, w.Body.String())
37 }
38 archive, err := zip.NewReader(bytes.NewReader(w.Body.Bytes()), int64(w.Body.Len()))
39 if err != nil {
40 t.Fatal(err)
41 }
42 var content string
43 for _, f := range archive.File {
44 r, _ := f.Open()
45 b, _ := io.ReadAll(r)
46 r.Close()
47 content += string(b)
48 }
49 if strings.Contains(content, "sample-private-token") || strings.Contains(content, "example.com") || !strings.Contains(content, "UI failed") {
50 t.Fatalf("invalid exported content: %s", content)
51 }
52 // Invalid event sources cannot impersonate native/controller diagnostics.
53 if w := request(s, "POST", "/api/diagnostics/events", s.origin, `{"source":"native","message":"spoof"}`, true); w.Code != 400 {
54 t.Fatal("source spoof accepted")
55 }
56 if w := request(s, "POST", "/api/diagnostics/events", s.origin, `{"source":"frontend","message":"`+strings.Repeat("x", 33<<10)+`"}`, true); w.Code != 400 {
57 t.Fatal("oversized event accepted")
58 }
59 for i := 0; i < 65; i++ {
60 w = request(s, "POST", "/api/diagnostics/events", s.origin, body, true)
61 }
62 if w.Code != http.StatusTooManyRequests {
63 t.Fatalf("no rate limit: %d", w.Code)
64 }
65}
66func TestOfflineExportPreservesDestinationOnErrorAndNoArchiveSymlink(t *testing.T) {
67 profile := t.TempDir()
68 path := filepath.Join(profile, "config.json")
69 output := filepath.Join(t.TempDir(), "bundle.zip")
70 if err := diagnostics.New(profile).Record("error", "native", "password=offline-secret startup failed"); err != nil {
71 t.Fatal(err)
72 }
73 if err := ExportDiagnostics(path, output); err != nil {
74 t.Fatal(err)
75 }
76 archive, err := zip.OpenReader(output)
77 if err != nil {
78 t.Fatal(err)
79 }
80 archive.Close()
81 original, _ := os.ReadFile(output)
82 if err := ExportDiagnostics(path, "relative.zip"); err == nil {
83 t.Fatal("relative destination accepted")
84 }
85 if err := os.Mkdir(filepath.Join(profile, "control.json"), 0700); err != nil {
86 t.Fatal(err)
87 }
88 if err := ExportDiagnostics(path, output); err != nil {
89 t.Fatal("corrupt controller record blocked offline recovery", err)
90 }
91 original, _ = os.ReadFile(output)
92 if err := os.Rename(filepath.Join(profile, "logs"), filepath.Join(profile, "logs.saved")); err != nil {
93 t.Fatal(err)
94 }
95 if err := os.WriteFile(filepath.Join(profile, "logs"), []byte("not a directory"), 0600); err != nil {
96 t.Fatal(err)
97 }
98 if err := ExportDiagnostics(path, output); err == nil {
99 t.Fatal("invalid log storage accepted")
100 }
101 got, _ := os.ReadFile(output)
102 if !bytes.Equal(original, got) {
103 t.Fatal("failed export overwrote destination")
104 }
105}
106func TestTaskPanicRecordsFailureBeforeProcessExit(t *testing.T) {
107 const flag = "CAGE_DIAGNOSTICS_PANIC_TEST"
108 if profile := os.Getenv(flag); profile != "" {
109 diagnostics.SetDefault(diagnostics.New(profile))
110 m := new(jobManager)
111 var operation sync.Mutex
112 _, err := m.submit(&operation, "test", 0, nil, nil, func() error { panic("token=panic-private-secret task broke") })
113 if err != nil {
114 os.Exit(3)
115 }
116 select {}
117 }
118 profile := t.TempDir()
119 exe, err := os.Executable()
120 if err != nil {
121 t.Fatal(err)
122 }
123 cmd := exec.Command(exe, "-test.run=^TestTaskPanicRecordsFailureBeforeProcessExit$")
124 cmd.Env = append(os.Environ(), flag+"="+profile)
125 if err := cmd.Run(); err == nil {
126 t.Fatal("task panic unexpectedly kept process alive")
127 }
128 var bundle bytes.Buffer
129 if err := diagnostics.New(profile).Export(&bundle, "test", nil, "offline"); err != nil {
130 t.Fatal(err)
131 }
132 archive, _ := zip.NewReader(bytes.NewReader(bundle.Bytes()), int64(bundle.Len()))
133 var messages string
134 for _, f := range archive.File {
135 r, _ := f.Open()
136 b, _ := io.ReadAll(r)
137 r.Close()
138 messages += string(b)
139 }
140 if strings.Contains(messages, "panic-private-secret") || !strings.Contains(messages, "task broke") {
141 t.Fatal("panic evidence missing or not redacted")
142 }
143}
144
145func TestDiagnosticExportBusyAndManifest(t *testing.T) {
146 s := testServer(t)
147 s.diagnosticMu.Lock()
148 w := request(s, "GET", "/api/diagnostics/export", "", "", true)
149 s.diagnosticMu.Unlock()
150 if w.Code != 409 {
151 t.Fatal("parallel bundle generation allowed")
152 }
153 info := request(s, "GET", "/api/diagnostics", "", "", true)
154 var v diagnostics.Info
155 if json.Unmarshal(info.Body.Bytes(), &v) != nil || v.MaxBytes+v.GuestMaxBytes != 50<<20 || v.RetentionDays != 14 {
156 t.Fatalf("policy unavailable: %s", info.Body.String())
157 }
158}