跳到主要内容

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

浏览文件

helper_windows_test.go

173 行 · 4.3 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package hcs
5
6import (
7 "encoding/json"
8 "net"
9 "os"
10 "path/filepath"
11 "regexp"
12 "sync"
13 "testing"
14
15 "gotest.tools/v3/assert"
16)
17
18const (
19 testDiskPath = `C:\Users\lima\.lima\hcs\disk.vhdx`
20 testISOPath = `C:\Users\lima\.lima\hcs\cidata.iso`
21 testEndpointID = "11111111-2222-3333-4444-555555555555"
22 testMACAddress = "52-55-55-AA-BB-CC"
23 testSerialPipe = `\\.\pipe\lima-hcs-com1`
24 testCPUs = 2
25 testMemoryMiB = 2048
26)
27
28func TestNetworkAndEndpointName(t *testing.T) {
29 assert.Equal(t, networkName("test"), "lima-test-net")
30 assert.Equal(t, endpointName("test"), "lima-test-ep")
31}
32
33func TestHNSMACAddress(t *testing.T) {
34 dir := t.TempDir()
35 got := hnsMACAddress(dir)
36
37 re := regexp.MustCompile(`^52-55-55(-[0-9A-F]{2}){3}$`)
38 assert.Assert(t, re.MatchString(got), "unexpected MAC address %#q", got)
39
40 assert.Equal(t, got, hnsMACAddress(dir), "MAC Address should be identical if input is the same.")
41
42 assert.Assert(t, got != hnsMACAddress(t.TempDir()))
43}
44
45func TestBuildVMConfig(t *testing.T) {
46 got, err := buildVMConfig(testDiskPath, testISOPath, testEndpointID, testMACAddress, testSerialPipe, testCPUs, testMemoryMiB)
47 assert.NilError(t, err)
48
49 const want = `{
50 "SchemaVersion": {"Major": 2, "Minor": 1},
51 "Owner": "Lima",
52 "ShouldTerminateOnLastHandleClosed": true,
53 "VirtualMachine": {
54 "Chipset": {
55 "Uefi": {
56 "Console": "ComPort1",
57 "BootThis": {"DevicePath": "Primary disk", "DiskNumber": 0, "DeviceType": "ScsiDrive"}
58 }
59 },
60 "ComputeTopology": {
61 "Memory": {"Backing": "Virtual", "SizeInMB": 2048},
62 "Processor": {"Count": 2}
63 },
64 "Devices": {
65 "Scsi": {
66 "Primary disk": {
67 "Attachments": {"0": {"Type": "VirtualDisk", "Path": "C:\\Users\\lima\\.lima\\hcs\\disk.vhdx"}}
68 },
69 "cidata": {
70 "Attachments": {"0": {"Type": "Iso", "Path": "C:\\Users\\lima\\.lima\\hcs\\cidata.iso"}}
71 }
72 },
73 "ComPorts": {"0": {"NamedPipe": "\\\\.\\pipe\\lima-hcs-com1"}},
74 "NetworkAdapters": {
75 "11111111-2222-3333-4444-555555555555": {
76 "EndpointId": "11111111-2222-3333-4444-555555555555",
77 "MacAddress": "52-55-55-AA-BB-CC"
78 }
79 }
80 }
81 }
82}`
83
84 var gotDoc, wantDoc any
85 assert.NilError(t, json.Unmarshal([]byte(got), &gotDoc))
86 assert.NilError(t, json.Unmarshal([]byte(want), &wantDoc))
87 assert.DeepEqual(t, gotDoc, wantDoc)
88}
89
90func TestBuildVMConfigSerialPortNumbering(t *testing.T) {
91 got, err := buildVMConfig(testDiskPath, testISOPath, testEndpointID, testMACAddress, testSerialPipe, testCPUs, testMemoryMiB)
92 assert.NilError(t, err)
93
94 var doc struct {
95 VirtualMachine struct {
96 Chipset struct {
97 Uefi struct {
98 Console string
99 }
100 }
101 Devices struct {
102 ComPorts map[string]struct {
103 NamedPipe string
104 }
105 }
106 }
107 }
108 assert.NilError(t, json.Unmarshal([]byte(got), &doc))
109
110 assert.Equal(t, doc.VirtualMachine.Chipset.Uefi.Console, "ComPort1")
111 assert.Equal(t, doc.VirtualMachine.Devices.ComPorts["0"].NamedPipe, testSerialPipe)
112}
113
114func TestServeSerial(t *testing.T) {
115 logPath := filepath.Join(t.TempDir(), "serial.log")
116 ln := newFakeListener()
117
118 done := make(chan struct{})
119 go func() {
120 serveSerial(ln, logPath)
121 close(done)
122 }()
123
124 lines := []string{"[ 0.000000] Linux version 6.11\n", "lima login: \n"}
125 for _, line := range lines {
126 guest, host := net.Pipe()
127 ln.conns <- host
128 _, err := guest.Write([]byte(line))
129 assert.NilError(t, err)
130 assert.NilError(t, guest.Close())
131 }
132
133 assert.NilError(t, ln.Close())
134 <-done
135
136 b, err := os.ReadFile(logPath)
137 assert.NilError(t, err)
138 assert.Equal(t, string(b), lines[0]+lines[1])
139}
140
141type fakeListener struct {
142 conns chan net.Conn
143 done chan struct{}
144 once sync.Once
145}
146
147func newFakeListener() *fakeListener {
148 return &fakeListener{
149 conns: make(chan net.Conn),
150 done: make(chan struct{}),
151 }
152}
153
154func (ln *fakeListener) Accept() (net.Conn, error) {
155 select {
156 case conn := <-ln.conns:
157 return conn, nil
158 case <-ln.done:
159 return nil, net.ErrClosed
160 }
161}
162
163func (ln *fakeListener) Close() error {
164 ln.once.Do(func() { close(ln.done) })
165 return nil
166}
167
168func (ln *fakeListener) Addr() net.Addr { return fakeAddr{} }
169
170type fakeAddr struct{}
171
172func (fakeAddr) Network() string { return "fake" }
173func (fakeAddr) String() string { return "fake" }