跳到主要内容

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

浏览文件

config_test.go

598 行 · 15.5 KB历史下载
1package main
2
3import (
4 "flag"
5 "net/netip"
6 "os"
7 "slices"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 yaml "gopkg.in/yaml.v3"
13)
14
15func TestIPAddressConversions(t *testing.T) {
16 t.Parallel()
17 cases := [][]string{
18 {"192.168.127.1/24", "192.168.127.1", "192.168.127.254"},
19 {"10.10.0.0/16", "10.10.0.1", "10.10.255.254"},
20 {"172.16.16.16/12", "172.16.0.1", "172.31.255.254"},
21 {"fc00::fff/64", "fc00::1", "fc00::ffff:ffff:ffff:fffe"},
22 }
23 for _, v := range cases {
24 naddr, _ := netip.ParsePrefix(v[0])
25
26 fuaddr, err := getFirstUsableIPFromSubnet(naddr)
27 require.NoError(t, err, "getFirstUsableIPFromSubnet returns error for \"%s\" -> \"%s\"", v[0], fuaddr)
28
29 luaddr, err := getLastUsableIPFromSubnet(naddr)
30 require.NoError(t, err, "getLastUsableIPFromSubnet returns error for \"%s\" -> \"%s\"", v[0], luaddr)
31
32 assert.Equal(t, v[1], fuaddr.String(), "getFirstUsableIPFromSubnet returns wrong result: expects \"%s\", got \"%s\"", v[1], fuaddr)
33 assert.Equal(t, v[2], luaddr.String(), "getLastUsableIPFromSubnet returns wrong result: expects \"%s\", got \"%s\"", v[2], luaddr)
34 }
35}
36
37func TestConfigInit(t *testing.T) {
38 t.Parallel()
39 for _, v := range getCaseDataConfig() {
40 var cnf GvproxyConfig
41 var args GvproxyArgs
42
43 flagSet := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
44
45 _, errArgParse := GvproxyArgParse(flagSet, &args, v.Args)
46 require.NoError(t, errArgParse, "%s: failed toFparse command line arguments", v.CaseName)
47
48 // Read config
49 errUnmarshal := yaml.Unmarshal([]byte(v.InputConfig), &cnf)
50 require.NoError(t, errUnmarshal, "%s: failed to parse config file", v.CaseName)
51
52 _, errConfig := GvproxyConfigure(&cnf, &args, "testing")
53 if v.ErrorPrefix != "" {
54 require.ErrorContains(t, errConfig, v.ErrorPrefix, "%s: wrong config error", v.CaseName)
55 } else {
56 require.NoError(t, errConfig, "%s: unexpected config error", v.CaseName)
57 }
58
59 // Ignore os-specific things while testing
60 if len(cnf.Listen) == 1 && slices.Contains(getCaseDataPossibleDefaultListen(), cnf.Listen[0]) {
61 cnf.Listen[0] = "default"
62 }
63 cnf.Stack.DNSSearchDomains = nil
64
65 result, errMarshal := yaml.Marshal(cnf)
66 require.NoErrorf(t, errMarshal, "%s: unmarshallable config", v.CaseName)
67
68 assert.YAMLEq(t, v.ResultConfig, string(result), "%s: mismatch between generated and expected config", v.CaseName)
69 }
70}
71
72func getCaseDataPossibleDefaultListen() []string {
73 return []string{
74 "unix:///var/run/gvproxy/default.sock",
75 "unix:\\\\\\.\\pipe\\gvproxy\\default_sock",
76 }
77}
78
79// Data for test cases
80type caseDataConfig struct {
81 CaseName string
82 Args []string
83 InputConfig string
84 ResultConfig string
85 ErrorPrefix string
86}
87
88func getCaseDataConfig() []caseDataConfig {
89 return []caseDataConfig{
90 {
91 CaseName: "Legacy with no args",
92 Args: []string{},
93 InputConfig: "",
94 ResultConfig: `log-level: info
95stack:
96 mtu: 1500
97 subnet: 192.168.127.0/24
98 gatewayIP: 192.168.127.1
99 gatewayMacAddress: 5a:94:ef:e4:0c:dd
100 dns:
101 - name: containers.internal.
102 records:
103 - name: gateway
104 ip: 192.168.127.1
105 - name: host
106 ip: 192.168.127.254
107 - name: docker.internal.
108 records:
109 - name: gateway
110 ip: 192.168.127.1
111 - name: host
112 ip: 192.168.127.254
113 forwards:
114 127.0.0.1:2222: 192.168.127.2:22
115 nat:
116 192.168.127.254: 127.0.0.1
117 gatewayVirtualIPs:
118 - 192.168.127.254
119 dhcpStaticLeases:
120 192.168.127.2: 5a:94:ef:e4:0c:ee
121 vpnKitUUIDMacAddresses:
122 c3d68012-0208-11ea-9fd7-f2189899ab08: 5a:94:ef:e4:0c:ee
123`,
124 },
125 {
126 CaseName: "Legacy from README: qemu tcp",
127 Args: []string{"-listen", "unix:///tmp/network.sock", "-listen-qemu", "tcp://0.0.0.0:1234"},
128 InputConfig: "",
129 ResultConfig: `listen:
130 - unix:///tmp/network.sock
131log-level: info
132stack:
133 mtu: 1500
134 subnet: 192.168.127.0/24
135 gatewayIP: 192.168.127.1
136 gatewayMacAddress: 5a:94:ef:e4:0c:dd
137 dns:
138 - name: containers.internal.
139 records:
140 - name: gateway
141 ip: 192.168.127.1
142 - name: host
143 ip: 192.168.127.254
144 - name: docker.internal.
145 records:
146 - name: gateway
147 ip: 192.168.127.1
148 - name: host
149 ip: 192.168.127.254
150 forwards:
151 127.0.0.1:2222: 192.168.127.2:22
152 nat:
153 192.168.127.254: 127.0.0.1
154 gatewayVirtualIPs:
155 - 192.168.127.254
156 dhcpStaticLeases:
157 192.168.127.2: 5a:94:ef:e4:0c:ee
158 vpnKitUUIDMacAddresses:
159 c3d68012-0208-11ea-9fd7-f2189899ab08: 5a:94:ef:e4:0c:ee
160interfaces:
161 qemu: tcp://0.0.0.0:1234
162`,
163 },
164 {
165 CaseName: "Legacy from README: qemu unix",
166 Args: []string{"-debug", "-listen", "unix:///tmp/network.sock", "-listen-qemu", "unix:///tmp/qemu.sock"},
167 InputConfig: "",
168 ResultConfig: `listen:
169 - unix:///tmp/network.sock
170log-level: debug
171stack:
172 debug: true
173 mtu: 1500
174 subnet: 192.168.127.0/24
175 gatewayIP: 192.168.127.1
176 gatewayMacAddress: 5a:94:ef:e4:0c:dd
177 dns:
178 - name: containers.internal.
179 records:
180 - name: gateway
181 ip: 192.168.127.1
182 - name: host
183 ip: 192.168.127.254
184 - name: docker.internal.
185 records:
186 - name: gateway
187 ip: 192.168.127.1
188 - name: host
189 ip: 192.168.127.254
190 forwards:
191 127.0.0.1:2222: 192.168.127.2:22
192 nat:
193 192.168.127.254: 127.0.0.1
194 gatewayVirtualIPs:
195 - 192.168.127.254
196 dhcpStaticLeases:
197 192.168.127.2: 5a:94:ef:e4:0c:ee
198 vpnKitUUIDMacAddresses:
199 c3d68012-0208-11ea-9fd7-f2189899ab08: 5a:94:ef:e4:0c:ee
200interfaces:
201 qemu: unix:///tmp/qemu.sock
202`,
203 },
204 {
205 CaseName: "Legacy from README: UML",
206 Args: []string{"-debug", "-listen", "unix:///tmp/network.sock", "-listen-bess", "unixpacket:///tmp/bess.sock"},
207 InputConfig: "",
208 ResultConfig: `listen:
209 - unix:///tmp/network.sock
210log-level: debug
211stack:
212 debug: true
213 mtu: 1500
214 subnet: 192.168.127.0/24
215 gatewayIP: 192.168.127.1
216 gatewayMacAddress: 5a:94:ef:e4:0c:dd
217 dns:
218 - name: containers.internal.
219 records:
220 - name: gateway
221 ip: 192.168.127.1
222 - name: host
223 ip: 192.168.127.254
224 - name: docker.internal.
225 records:
226 - name: gateway
227 ip: 192.168.127.1
228 - name: host
229 ip: 192.168.127.254
230 forwards:
231 127.0.0.1:2222: 192.168.127.2:22
232 nat:
233 192.168.127.254: 127.0.0.1
234 gatewayVirtualIPs:
235 - 192.168.127.254
236 dhcpStaticLeases:
237 192.168.127.2: 5a:94:ef:e4:0c:ee
238 vpnKitUUIDMacAddresses:
239 c3d68012-0208-11ea-9fd7-f2189899ab08: 5a:94:ef:e4:0c:ee
240interfaces:
241 bess: unixpacket:///tmp/bess.sock
242`,
243 },
244 {
245 CaseName: "Legacy from README: VFKit",
246 Args: []string{"-debug", "-listen", "unix:///tmp/network.sock", "--listen-vfkit", "unixgram:///tmp/vfkit.sock"},
247 InputConfig: "",
248 ResultConfig: `listen:
249 - unix:///tmp/network.sock
250log-level: debug
251stack:
252 debug: true
253 mtu: 1500
254 subnet: 192.168.127.0/24
255 gatewayIP: 192.168.127.1
256 gatewayMacAddress: 5a:94:ef:e4:0c:dd
257 dns:
258 - name: containers.internal.
259 records:
260 - name: gateway
261 ip: 192.168.127.1
262 - name: host
263 ip: 192.168.127.254
264 - name: docker.internal.
265 records:
266 - name: gateway
267 ip: 192.168.127.1
268 - name: host
269 ip: 192.168.127.254
270 forwards:
271 127.0.0.1:2222: 192.168.127.2:22
272 nat:
273 192.168.127.254: 127.0.0.1
274 gatewayVirtualIPs:
275 - 192.168.127.254
276 dhcpStaticLeases:
277 192.168.127.2: 5a:94:ef:e4:0c:ee
278 vpnKitUUIDMacAddresses:
279 c3d68012-0208-11ea-9fd7-f2189899ab08: 5a:94:ef:e4:0c:ee
280interfaces:
281 vfkit: unixgram:///tmp/vfkit.sock
282`,
283 },
284 {
285 CaseName: "Legacy from README: vsock",
286 Args: []string{"-debug", "-listen", "unix:///tmp/network.sock", "-listen-qemu", "unix:///tmp/qemu.sock"},
287 InputConfig: "",
288 ResultConfig: `listen:
289 - unix:///tmp/network.sock
290log-level: debug
291stack:
292 debug: true
293 mtu: 1500
294 subnet: 192.168.127.0/24
295 gatewayIP: 192.168.127.1
296 gatewayMacAddress: 5a:94:ef:e4:0c:dd
297 dns:
298 - name: containers.internal.
299 records:
300 - name: gateway
301 ip: 192.168.127.1
302 - name: host
303 ip: 192.168.127.254
304 - name: docker.internal.
305 records:
306 - name: gateway
307 ip: 192.168.127.1
308 - name: host
309 ip: 192.168.127.254
310 forwards:
311 127.0.0.1:2222: 192.168.127.2:22
312 nat:
313 192.168.127.254: 127.0.0.1
314 gatewayVirtualIPs:
315 - 192.168.127.254
316 dhcpStaticLeases:
317 192.168.127.2: 5a:94:ef:e4:0c:ee
318 vpnKitUUIDMacAddresses:
319 c3d68012-0208-11ea-9fd7-f2189899ab08: 5a:94:ef:e4:0c:ee
320interfaces:
321 qemu: unix:///tmp/qemu.sock
322`,
323 },
324 {
325 CaseName: "config: empty config",
326 Args: []string{"-config", "config.yaml"},
327 InputConfig: ``,
328 ResultConfig: `log-level: info
329stack:
330 mtu: 1500
331 subnet: 192.168.127.0/24
332 gatewayIP: 192.168.127.1
333 gatewayMacAddress: 5a:94:ef:e4:0c:dd
334 nat:
335 192.168.127.254: 127.0.0.1
336 gatewayVirtualIPs:
337 - 192.168.127.254
338`,
339 },
340 {
341 CaseName: "config: listen, loglevel, qemu",
342 Args: []string{"-config", "config.yaml"},
343 InputConfig: `listen:
344 - unix:///var/run/gvproxy/domain-2.sock
345log-level: warning
346interfaces:
347 qemu: unix:///tmp/qemu.sock`,
348 ResultConfig: `listen:
349 - unix:///var/run/gvproxy/domain-2.sock
350log-level: warning
351stack:
352 mtu: 1500
353 subnet: 192.168.127.0/24
354 gatewayIP: 192.168.127.1
355 gatewayMacAddress: 5a:94:ef:e4:0c:dd
356 nat:
357 192.168.127.254: 127.0.0.1
358 gatewayVirtualIPs:
359 - 192.168.127.254
360interfaces:
361 qemu: unix:///tmp/qemu.sock
362`,
363 },
364 {
365 CaseName: "config: stack changes: auto addresses from subnet",
366 Args: []string{"-config", "config.yaml"},
367 InputConfig: `listen:
368 - unix:///var/run/gvproxy/domain-2.sock
369log-level: warning
370stack:
371 mtu: 1480
372 subnet: 10.0.0.0/16
373 gatewayMacAddress: 10:11:11:11:11:00
374`,
375 ResultConfig: `listen:
376 - unix:///var/run/gvproxy/domain-2.sock
377log-level: warning
378stack:
379 mtu: 1480
380 subnet: 10.0.0.0/16
381 gatewayIP: 10.0.0.1
382 gatewayMacAddress: "10:11:11:11:11:00"
383 nat:
384 10.0.255.254: 127.0.0.1
385 gatewayVirtualIPs:
386 - 10.0.255.254
387`,
388 },
389 {
390 CaseName: "config: stack changes: dhcpStaticLeases",
391 Args: []string{"-config", "config.yaml"},
392 InputConfig: `stack:
393 subnet: 10.0.0.0/16
394 dhcpStaticLeases:
395 10.0.0.2: "10:11:11:11:11:02"
396 10.0.0.3: "10:11:11:11:11:03"
397 10.0.0.100: "10:11:11:11:11:dd"
398`,
399 ResultConfig: `log-level: info
400stack:
401 mtu: 1500
402 subnet: 10.0.0.0/16
403 gatewayIP: 10.0.0.1
404 gatewayMacAddress: 5a:94:ef:e4:0c:dd
405 nat:
406 10.0.255.254: 127.0.0.1
407 gatewayVirtualIPs:
408 - 10.0.255.254
409 dhcpStaticLeases:
410 10.0.0.2: "10:11:11:11:11:02"
411 10.0.0.3: "10:11:11:11:11:03"
412 10.0.0.100: 10:11:11:11:11:dd
413`,
414 },
415 {
416 CaseName: "config: stack changes: tcp forwards",
417 Args: []string{"-config", "config.yaml"},
418 InputConfig: `stack:
419 subnet: 10.0.0.0/16
420 forwards:
421 127.0.0.1:59022: 192.168.127.2:22
422 127.0.0.1:59080: 192.168.127.2:80
423 127.0.0.1:59443: 192.168.127.2:443
424`,
425 ResultConfig: `log-level: info
426stack:
427 mtu: 1500
428 subnet: 10.0.0.0/16
429 gatewayIP: 10.0.0.1
430 gatewayMacAddress: 5a:94:ef:e4:0c:dd
431 forwards:
432 127.0.0.1:59022: 192.168.127.2:22
433 127.0.0.1:59080: 192.168.127.2:80
434 127.0.0.1:59443: 192.168.127.2:443
435 nat:
436 10.0.255.254: 127.0.0.1
437 gatewayVirtualIPs:
438 - 10.0.255.254
439`,
440 },
441 {
442 CaseName: "config: ssh forwards fails on identity file missing",
443 Args: []string{"-config", "config.yaml"},
444 InputConfig: `stack:
445 subnet: 10.0.0.0/16
446forwards:
447 - socket: ???
448 dest: ???
449 user: ???
450 identity: ???
451`,
452 ResultConfig: `log-level: info
453stack:
454 mtu: 1500
455 subnet: 10.0.0.0/16
456 gatewayIP: 10.0.0.1
457 gatewayMacAddress: 5a:94:ef:e4:0c:dd
458 nat:
459 10.0.255.254: 127.0.0.1
460 gatewayVirtualIPs:
461 - 10.0.255.254
462forwards:
463 - socket: ???
464 dest: ???
465 user: ???
466 identity: ???
467`,
468 ErrorPrefix: "identity file",
469 },
470 {
471 CaseName: "debug check #1",
472 Args: []string{"-config", "config.yaml"},
473 InputConfig: `log-level: debug`,
474 ResultConfig: `log-level: debug
475stack:
476 debug: true
477 mtu: 1500
478 subnet: 192.168.127.0/24
479 gatewayIP: 192.168.127.1
480 gatewayMacAddress: 5a:94:ef:e4:0c:dd
481 nat:
482 192.168.127.254: 127.0.0.1
483 gatewayVirtualIPs:
484 - 192.168.127.254
485`,
486 },
487 {
488 CaseName: "debug check #2",
489 Args: []string{"-debug", "-config", "config.yaml"},
490 InputConfig: `log-level: error`,
491 ResultConfig: `log-level: debug
492stack:
493 debug: true
494 mtu: 1500
495 subnet: 192.168.127.0/24
496 gatewayIP: 192.168.127.1
497 gatewayMacAddress: 5a:94:ef:e4:0c:dd
498 nat:
499 192.168.127.254: 127.0.0.1
500 gatewayVirtualIPs:
501 - 192.168.127.254
502`,
503 },
504 {
505 CaseName: "debug check #3",
506 Args: []string{"-debug", "-pcap", "capture.pcap"},
507 InputConfig: "",
508 ResultConfig: `log-level: debug
509stack:
510 debug: true
511 capture-file: capture.pcap
512 mtu: 1500
513 subnet: 192.168.127.0/24
514 gatewayIP: 192.168.127.1
515 gatewayMacAddress: 5a:94:ef:e4:0c:dd
516 dns:
517 - name: containers.internal.
518 records:
519 - name: gateway
520 ip: 192.168.127.1
521 - name: host
522 ip: 192.168.127.254
523 - name: docker.internal.
524 records:
525 - name: gateway
526 ip: 192.168.127.1
527 - name: host
528 ip: 192.168.127.254
529 forwards:
530 127.0.0.1:2222: 192.168.127.2:22
531 nat:
532 192.168.127.254: 127.0.0.1
533 gatewayVirtualIPs:
534 - 192.168.127.254
535 dhcpStaticLeases:
536 192.168.127.2: 5a:94:ef:e4:0c:ee
537 vpnKitUUIDMacAddresses:
538 c3d68012-0208-11ea-9fd7-f2189899ab08: 5a:94:ef:e4:0c:ee
539`,
540 },
541 {
542 CaseName: "Legacy: stdio",
543 Args: []string{"-listen-stdio", "accept"},
544 InputConfig: "",
545 ResultConfig: `log-level: info
546stack:
547 mtu: 1500
548 subnet: 192.168.127.0/24
549 gatewayIP: 192.168.127.1
550 gatewayMacAddress: 5a:94:ef:e4:0c:dd
551 dns:
552 - name: containers.internal.
553 records:
554 - name: gateway
555 ip: 192.168.127.1
556 - name: host
557 ip: 192.168.127.254
558 - name: docker.internal.
559 records:
560 - name: gateway
561 ip: 192.168.127.1
562 - name: host
563 ip: 192.168.127.254
564 forwards:
565 127.0.0.1:2222: 192.168.127.2:22
566 nat:
567 192.168.127.254: 127.0.0.1
568 gatewayVirtualIPs:
569 - 192.168.127.254
570 dhcpStaticLeases:
571 192.168.127.2: 5a:94:ef:e4:0c:ee
572 vpnKitUUIDMacAddresses:
573 c3d68012-0208-11ea-9fd7-f2189899ab08: 5a:94:ef:e4:0c:ee
574interfaces:
575 stdio: accept
576`,
577 },
578 {
579 CaseName: "config: stdio",
580 Args: []string{"-config", "config.yaml"},
581 InputConfig: `interfaces:
582 stdio: accept`,
583 ResultConfig: `log-level: info
584stack:
585 mtu: 1500
586 subnet: 192.168.127.0/24
587 gatewayIP: 192.168.127.1
588 gatewayMacAddress: 5a:94:ef:e4:0c:dd
589 nat:
590 192.168.127.254: 127.0.0.1
591 gatewayVirtualIPs:
592 - 192.168.127.254
593interfaces:
594 stdio: accept
595`,
596 },
597 }
598}