跳到主要内容

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

浏览文件

envutil_test.go

249 行 · 6.5 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package envutil
5
6import (
7 "os"
8 "slices"
9 "strings"
10 "testing"
11
12 "gotest.tools/v3/assert"
13)
14
15func isUsingDefaultBlockList() bool {
16 blockEnv := os.Getenv("LIMA_SHELLENV_BLOCK")
17 return blockEnv == "" || strings.HasPrefix(blockEnv, "+")
18}
19
20func TestMatchesPattern(t *testing.T) {
21 tests := []struct {
22 name string
23 pattern string
24 expected bool
25 }{
26 {"PATH", "PATH", true},
27 {"PATH", "HOME", false},
28 {"SSH_AUTH_SOCK", "SSH_*", true},
29 {"SSH_AGENT_PID", "SSH_*", true},
30 {"HOME", "SSH_*", false},
31 {"XDG_CONFIG_HOME", "XDG_*", true},
32 {"_LIMA_TEST", "_*", true},
33 {"LIMA_HOME", "_*", false},
34 }
35
36 for _, tt := range tests {
37 t.Run(tt.name+"_matches_"+tt.pattern, func(t *testing.T) {
38 result := matchesPattern(tt.name, tt.pattern)
39 assert.Equal(t, result, tt.expected)
40 })
41 }
42}
43
44func TestMatchesAnyPattern(t *testing.T) {
45 patterns := []string{"PATH", "SSH_*", "XDG_*"}
46
47 tests := []struct {
48 name string
49 expected bool
50 }{
51 {"PATH", true},
52 {"HOME", false},
53 {"SSH_AUTH_SOCK", true},
54 {"XDG_CONFIG_HOME", true},
55 {"USER", false},
56 }
57
58 for _, tt := range tests {
59 t.Run(tt.name, func(t *testing.T) {
60 result := matchesAnyPattern(tt.name, patterns)
61 assert.Equal(t, result, tt.expected)
62 })
63 }
64}
65
66func TestParseEnvList(t *testing.T) {
67 tests := []struct {
68 input string
69 expected []string
70 }{
71 {"", []string{}},
72 {"PATH", []string{"PATH"}},
73 {"PATH,HOME", []string{"PATH", "HOME"}},
74 {"PATH, HOME , USER", []string{"PATH", "HOME", "USER"}},
75 {" , , ", []string{}},
76 }
77
78 for _, tt := range tests {
79 t.Run(tt.input, func(t *testing.T) {
80 result := parseEnvList(tt.input)
81 assert.DeepEqual(t, result, tt.expected)
82 })
83 }
84}
85
86func TestGetBlockAndAllowLists(t *testing.T) {
87 t.Run("default config", func(t *testing.T) {
88 t.Setenv("LIMA_SHELLENV_BLOCK", "")
89 t.Setenv("LIMA_SHELLENV_ALLOW", "")
90
91 blockList := getBlockList()
92 allowList := getAllowList()
93
94 assert.Assert(t, isUsingDefaultBlockList())
95 assert.DeepEqual(t, blockList, defaultBlockList)
96 assert.Equal(t, len(allowList), 0)
97 })
98
99 t.Run("custom blocklist", func(t *testing.T) {
100 t.Setenv("LIMA_SHELLENV_BLOCK", "PATH,HOME")
101
102 blockList := getBlockList()
103 assert.Assert(t, !isUsingDefaultBlockList())
104 expected := []string{"PATH", "HOME"}
105 assert.DeepEqual(t, blockList, expected)
106 })
107
108 t.Run("additive blocklist", func(t *testing.T) {
109 t.Setenv("LIMA_SHELLENV_BLOCK", "+CUSTOM_VAR")
110
111 blockList := getBlockList()
112 assert.Assert(t, isUsingDefaultBlockList())
113 expected := slices.Concat(GetDefaultBlockList(), []string{"CUSTOM_VAR"})
114 assert.DeepEqual(t, blockList, expected)
115 })
116
117 t.Run("allowlist", func(t *testing.T) {
118 t.Setenv("LIMA_SHELLENV_ALLOW", "FOO,BAR")
119
120 allowList := getAllowList()
121 expected := []string{"FOO", "BAR"}
122 assert.DeepEqual(t, allowList, expected)
123 })
124}
125
126func TestFilterEnvironment(t *testing.T) {
127 testEnv := []string{
128 "PATH=/usr/bin",
129 "HOME=/home/user",
130 "USER=testuser",
131 "FOO=bar",
132 "SSH_AUTH_SOCK=/tmp/ssh",
133 "XDG_CONFIG_HOME=/config",
134 "BASH_VERSION=5.0",
135 "_INTERNAL=secret",
136 "CUSTOM_VAR=value",
137 }
138
139 t.Run("default blocklist", func(t *testing.T) {
140 result := filterEnvironmentWithLists(testEnv, nil, defaultBlockList)
141
142 expected := []string{"FOO=bar", "CUSTOM_VAR=value"}
143 assert.Assert(t, containsAll(result, expected))
144
145 blockedPrefixes := []string{
146 "PATH=",
147 "HOME=",
148 "SSH_AUTH_SOCK=",
149 "XDG_CONFIG_HOME=",
150 "BASH_VERSION=",
151 "_INTERNAL=",
152 }
153 for _, prefix := range blockedPrefixes {
154 for _, envVar := range result {
155 assert.Assert(t, !strings.HasPrefix(envVar, prefix), "Expected result to not contain variable with prefix %q, but found %q", prefix, envVar)
156 }
157 }
158 })
159
160 t.Run("custom blocklist", func(t *testing.T) {
161 result := filterEnvironmentWithLists(testEnv, nil, []string{"FOO"})
162
163 assert.Assert(t, !slices.Contains(result, "FOO=bar"))
164
165 expected := []string{"PATH=/usr/bin", "HOME=/home/user", "USER=testuser"}
166 assert.Assert(t, containsAll(result, expected))
167 })
168
169 t.Run("allowlist without blocklist passes everything", func(t *testing.T) {
170 result := filterEnvironmentWithLists(testEnv, []string{"FOO", "USER"}, nil)
171
172 // The allow list only exempts variables from the block list.
173 // With no block list, nothing is blocked, so all variables pass through.
174 assert.Equal(t, len(result), len(testEnv))
175 })
176
177 t.Run("allowlist takes precedence over blocklist", func(t *testing.T) {
178 result := filterEnvironmentWithLists(testEnv, []string{"FOO", "CUSTOM_VAR"}, []string{"FOO", "USER"})
179
180 expected := []string{"FOO=bar", "CUSTOM_VAR=value"}
181 assert.Assert(t, containsAll(result, expected))
182
183 assert.Assert(t, !slices.Contains(result, "USER=testuser"))
184 })
185}
186
187func containsAll(slice, items []string) bool {
188 for _, item := range items {
189 if !slices.Contains(slice, item) {
190 return false
191 }
192 }
193 return true
194}
195
196func TestGetDefaultBlockList(t *testing.T) {
197 blocklist := GetDefaultBlockList()
198
199 if &blocklist[0] == &defaultBlockList[0] {
200 t.Error("GetDefaultBlockList should return a copy, not the original slice")
201 }
202
203 assert.DeepEqual(t, blocklist, defaultBlockList)
204
205 expectedItems := []string{"PATH", "HOME", "SSH_*", "USER"}
206 for _, item := range expectedItems {
207 found := slices.Contains(blocklist, item)
208 assert.Assert(t, found, "Expected builtin blocklist to contain %#q", item)
209 }
210}
211
212func TestValidatePattern(t *testing.T) {
213 tests := []struct {
214 name string
215 pattern string
216 valid bool
217 }{
218 {"simple alphanumeric uppercase", "FOO", true},
219 {"simple alphanumeric lowercase", "foo", true},
220 {"mixed case", "FooBar", true},
221 {"with underscore", "FOO_BAR", true},
222 {"with numbers", "VAR123", true},
223 {"with trailing asterisk", "FOO*", true},
224 {"with multiple asterisks", "FOO*BAR*", true},
225 {"asterisk at beginning", "*FOO", true},
226 {"asterisk in middle", "FOO*BAR", true},
227 {"only asterisk", "*", true},
228 {"with dash", "FOO-BAR", false},
229 {"with space", "FOO BAR", false},
230 }
231
232 for _, tt := range tests {
233 t.Run(tt.name, func(t *testing.T) {
234 err := validatePattern(tt.pattern)
235 if tt.valid {
236 assert.NilError(t, err, "Expected pattern %#q to be valid", tt.pattern)
237 } else {
238 assert.Assert(t, err != nil, "Expected pattern %#q to be invalid", tt.pattern)
239 }
240 })
241 }
242}
243
244func TestValidatePatternErrorMessage(t *testing.T) {
245 err := validatePattern("FOO-BAR")
246 assert.Assert(t, err != nil, "Expected pattern with dash to be invalid")
247 expectedMsg := "pattern `FOO-BAR` contains invalid character `-` at position 3"
248 assert.Equal(t, err.Error(), expectedMsg)
249}