跳到主要内容

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

浏览文件

safety_system_timezone_test.go

93 行 · 3.1 KB历史下载
1package workbench
2
3import (
4 "encoding/base64"
5 "encoding/binary"
6 "encoding/json"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "runtime"
11 "strings"
12 "testing"
13
14 "github.com/sylvinus/agent-vm/internal/cage"
15)
16
17func TestLinuxTimezoneRejectsStaleSystemFile(t *testing.T) {
18 if runtime.GOOS == "windows" {
19 t.Skip("Linux probe uses POSIX TZ semantics; native Windows settings are tested separately")
20 }
21 python, err := exec.LookPath("python3")
22 if err != nil {
23 t.Skip("python3 not installed")
24 }
25 directory := t.TempDir()
26 localtime := filepath.Join(directory, "localtime")
27 timezone := filepath.Join(directory, "timezone")
28 if err := os.WriteFile(timezone, []byte("Asia/Shanghai\n"), 0600); err != nil {
29 t.Fatal(err)
30 }
31 // Redirect only file paths to isolated fixtures. Keep the shipped probe's
32 // time calculations, OS readlink behavior and Go decoder unchanged.
33 script := strings.ReplaceAll(guestSafetyProbe, "/etc/localtime", filepath.ToSlash(localtime))
34 script = strings.ReplaceAll(script, "/etc/timezone", filepath.ToSlash(timezone))
35 zoneDirectory := filepath.Join(directory, "zoneinfo", "Asia")
36 if err := os.MkdirAll(zoneDirectory, 0700); err != nil {
37 t.Fatal(err)
38 }
39 shanghai := fixedOffsetTZif(28800)
40 if err := os.WriteFile(filepath.Join(zoneDirectory, "Shanghai"), shanghai, 0600); err != nil {
41 t.Fatal(err)
42 }
43 zonePath, _ := json.Marshal([]string{filepath.Dir(zoneDirectory)})
44 script = "import zoneinfo; zoneinfo.reset_tzpath(" + string(zonePath) + ")\n" + script
45 input, _ := json.Marshal(map[string]any{"stage": "environment", "timezone": "Asia/Shanghai"})
46 config := cage.DefaultConfig()
47 config.Timezone = "Asia/Shanghai"
48 config.Locale = "C"
49 for _, tc := range []struct {
50 name string
51 data []byte
52 want bool
53 }{
54 {"stale UTC file", fixedOffsetTZif(0), false},
55 {"matching regular file", shanghai, true},
56 {"unreadable zone data", []byte("invalid tzif"), false},
57 {"missing file", nil, false},
58 } {
59 t.Run(tc.name, func(t *testing.T) {
60 if tc.data == nil {
61 if err := os.Remove(localtime); err != nil {
62 t.Fatal(err)
63 }
64 } else if err := os.WriteFile(localtime, tc.data, 0600); err != nil {
65 t.Fatal(err)
66 }
67 command := exec.Command(python, "-I", "-c", script, base64.StdEncoding.EncodeToString(input))
68 command.Env = append(os.Environ(), "TZ=Asia/Shanghai", "LANG=C", "LC_ALL=C")
69 output, err := command.CombinedOutput()
70 if err != nil {
71 t.Fatalf("probe failed: %v %s", err, output)
72 }
73 var observation guestObservation
74 if err := json.Unmarshal(output, &observation); err != nil {
75 t.Fatal(err)
76 }
77 if got := guestSettingsMatch(observation, config); got != tc.want {
78 t.Fatalf("settings match=%t want=%t: %s", got, tc.want, output)
79 }
80 })
81 }
82}
83
84// A single fixed-offset TZif v1 record keeps fixtures independent of host tzdata.
85func fixedOffsetTZif(offset int32) []byte {
86 data := make([]byte, 54)
87 copy(data, "TZif")
88 binary.BigEndian.PutUint32(data[36:40], 1) // one local-time type
89 binary.BigEndian.PutUint32(data[40:44], 4) // abbreviation including NUL
90 binary.BigEndian.PutUint32(data[44:48], uint32(offset))
91 copy(data[50:], "FIX\x00")
92 return data
93}