跳到主要内容

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

浏览文件

cidata_test.go

113 行 · 3.2 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package cidata
5
6import (
7 "fmt"
8 "io"
9 "net"
10 "net/url"
11 "strings"
12 "testing"
13
14 "gotest.tools/v3/assert"
15
16 "github.com/lima-vm/lima/v2/pkg/iso9660util"
17 "github.com/lima-vm/lima/v2/pkg/limatype"
18 "github.com/lima-vm/lima/v2/pkg/networks"
19)
20
21func fakeLookupIP(_ string) []net.IP {
22 return []net.IP{net.IPv4(127, 0, 0, 0)}
23}
24
25func TestSetupEnv(t *testing.T) {
26 netLookupIP = fakeLookupIP
27 urlMustParse := func(urlStr string) *url.URL {
28 u, err := url.Parse(urlStr)
29 assert.NilError(t, err)
30 return u
31 }
32
33 httpProxyCases := []*url.URL{
34 urlMustParse("http://127.0.0.1"),
35 urlMustParse("http://127.0.0.1:8080"),
36 urlMustParse("https://127.0.0.1:8080"),
37 urlMustParse("sock4://127.0.0.1:8080"),
38 urlMustParse("sock5://127.0.0.1:8080"),
39 urlMustParse("http://127.0.0.1:8080/"),
40 urlMustParse("http://127.0.0.1:8080/path"),
41 urlMustParse("http://localhost:8080"),
42 urlMustParse("http://localhost:8080/"),
43 urlMustParse("http://localhost:8080/path"),
44 urlMustParse("http://docker.for.mac.localhost:8080"),
45 urlMustParse("http://docker.for.mac.localhost:8080/"),
46 urlMustParse("http://docker.for.mac.localhost:8080/path"),
47 }
48
49 for _, httpProxy := range httpProxyCases {
50 t.Run(httpProxy.Host, func(t *testing.T) {
51 envKey := "http_proxy"
52 envValue := httpProxy.String()
53 envs, err := setupEnv(map[string]string{envKey: envValue}, false, networks.SlirpGateway)
54 assert.NilError(t, err)
55 assert.Equal(t, envs[envKey], strings.ReplaceAll(envValue, httpProxy.Hostname(), networks.SlirpGateway))
56 })
57 }
58}
59
60func TestSetupInvalidEnv(t *testing.T) {
61 envKey := "http_proxy"
62 envValue := "://localhost:8080"
63 envs, err := setupEnv(map[string]string{envKey: envValue}, false, networks.SlirpGateway)
64 assert.NilError(t, err)
65 assert.Equal(t, envs[envKey], envValue)
66}
67
68func TestAppendProvisionEntries(t *testing.T) {
69 scriptBoot := "Write-Host boot"
70 scriptSystem := "Write-Host system"
71 contentData := "hello"
72 exprYQ := ".foo=1"
73 provisions := []limatype.Provision{
74 {Mode: limatype.ProvisionModeBoot, Script: &scriptBoot},
75 {Mode: limatype.ProvisionModeSystem, Script: &scriptSystem},
76 {Mode: limatype.ProvisionModeData, ProvisionData: limatype.ProvisionData{Content: &contentData}},
77 {Mode: limatype.ProvisionModeYQ, Expression: &exprYQ},
78 }
79
80 layout, err := appendProvisionEntries(nil, provisions)
81 assert.NilError(t, err)
82 // Boot file is not added into the ISO file.
83 assert.Assert(t, !hasEntryPath(layout, "provision.boot/00000000"))
84 assert.Assert(t, hasEntryPath(layout, "provision.data/00000002"))
85 assert.Assert(t, hasEntryPath(layout, "provision.yq/00000003"))
86
87 gotData, err := readEntry(layout, "provision.data/00000002")
88 assert.NilError(t, err)
89 assert.Equal(t, gotData, contentData)
90}
91
92func hasEntryPath(layout []iso9660util.Entry, path string) bool {
93 for _, e := range layout {
94 if e.Path == path {
95 return true
96 }
97 }
98 return false
99}
100
101func readEntry(layout []iso9660util.Entry, path string) (string, error) {
102 for _, e := range layout {
103 if e.Path != path {
104 continue
105 }
106 b, err := io.ReadAll(e.Reader)
107 if err != nil {
108 return "", err
109 }
110 return string(b), nil
111 }
112 return "", fmt.Errorf("entry not found: %s", path)
113}