跳到主要内容

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

浏览文件

dns_test.go

327 行 · 9.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package dns
5
6import (
7 "fmt"
8 "io"
9 "log"
10 "net"
11 "regexp"
12 "runtime"
13 "testing"
14
15 "github.com/foxcpp/go-mockdns"
16 "github.com/miekg/dns"
17 "gotest.tools/v3/assert"
18 "gotest.tools/v3/assert/cmp"
19)
20
21var dnsResult *dns.Msg
22
23func TestNewHandler(t *testing.T) {
24 t.Run("with upstream servers", func(t *testing.T) {
25 upstreamServers := []string{"8.8.4.4", "1.1.1.1", "9.9.9.9"}
26 opts := HandlerOptions{
27 IPv6: true,
28 UpstreamServers: upstreamServers,
29 StaticHosts: map[string]string{
30 "test.local": "192.168.1.1",
31 "alias.local": "test.local",
32 },
33 }
34 h, err := NewHandler(opts)
35 assert.NilError(t, err)
36 assert.Assert(t, h != nil)
37
38 handler := h.(*Handler)
39 assert.Equal(t, handler.ipv6, true)
40 assert.Equal(t, len(handler.clients), 2)
41 assert.DeepEqual(t, handler.clientConfig.Servers, upstreamServers)
42 assert.Equal(t, handler.hostToIP["test.local."].String(), "192.168.1.1")
43 assert.Equal(t, handler.cnameToHost["alias.local."], "test.local.")
44 })
45
46 t.Run("without upstream servers on non-Windows", func(t *testing.T) {
47 if runtime.GOOS == "windows" {
48 t.Skip("Skipping on Windows")
49 }
50 opts := HandlerOptions{
51 IPv6: false,
52 StaticHosts: map[string]string{},
53 }
54 h, err := NewHandler(opts)
55 assert.NilError(t, err)
56 assert.Assert(t, h != nil)
57
58 handler := h.(*Handler)
59 assert.Equal(t, handler.ipv6, false)
60 assert.Assert(t, handler.clientConfig != nil)
61 })
62
63 t.Run("without upstream servers on Windows", func(t *testing.T) {
64 if runtime.GOOS != "windows" {
65 t.Skip("Skipping on non-Windows")
66 }
67 opts := HandlerOptions{
68 IPv6: false,
69 StaticHosts: map[string]string{},
70 }
71 h, err := NewHandler(opts)
72 assert.NilError(t, err)
73 assert.Assert(t, h != nil)
74
75 handler := h.(*Handler)
76 assert.Equal(t, handler.ipv6, false)
77 assert.Assert(t, handler.clientConfig != nil)
78 // Should use default fallback IPs on Windows
79 assert.Assert(t, len(handler.clientConfig.Servers) > 0)
80 })
81
82 t.Run("with invalid upstream servers fallback", func(t *testing.T) {
83 opts := HandlerOptions{
84 IPv6: true,
85 UpstreamServers: []string{}, // empty should trigger default behavior
86 StaticHosts: map[string]string{},
87 }
88 h, err := NewHandler(opts)
89 assert.NilError(t, err)
90 assert.Assert(t, h != nil)
91 })
92
93 t.Run("with static hosts IP and CNAME", func(t *testing.T) {
94 opts := HandlerOptions{
95 IPv6: true,
96 UpstreamServers: []string{"8.8.8.8"},
97 StaticHosts: map[string]string{
98 "host1.local": "10.0.0.1",
99 "host2.local": "10.0.0.2",
100 "cname1": "host1.local",
101 "cname2": "cname1",
102 },
103 }
104 h, err := NewHandler(opts)
105 assert.NilError(t, err)
106 assert.Assert(t, h != nil)
107
108 handler := h.(*Handler)
109 assert.Equal(t, handler.hostToIP["host1.local."].String(), "10.0.0.1")
110 assert.Equal(t, handler.hostToIP["host2.local."].String(), "10.0.0.2")
111 assert.Equal(t, handler.cnameToHost["cname1."], "host1.local.")
112 assert.Equal(t, handler.cnameToHost["cname2."], "cname1.")
113 })
114
115 t.Run("with truncate option", func(t *testing.T) {
116 opts := HandlerOptions{
117 IPv6: false,
118 UpstreamServers: []string{"1.1.1.1"},
119 TruncateReply: true,
120 StaticHosts: map[string]string{},
121 }
122 h, err := NewHandler(opts)
123 assert.NilError(t, err)
124 assert.Assert(t, h != nil)
125
126 handler := h.(*Handler)
127 assert.Equal(t, handler.truncate, true)
128 })
129}
130
131func TestDNSRecords(t *testing.T) {
132 if runtime.GOOS == "windows" {
133 // "On Windows, the resolver always uses C library functions, such as GetAddrInfo and DnsQuery."
134 t.Skip()
135 }
136
137 srv, err := mockdns.NewServerWithLogger(map[string]mockdns.Zone{
138 "onerecord.com.": {
139 TXT: []string{"My txt record"},
140 },
141 "multistringrecord.com.": {
142 TXT: []string{"123456789012345678901234567890123456789012345678901234567890" +
143 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
144 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
145 "12345678901234567890123456789012345678901234567890"},
146 },
147 "multiplerecords.com.": {
148 TXT: []string{"record 1", "record 2"},
149 },
150 }, log.New(io.Discard, "mockdns server: ", log.LstdFlags), false)
151 assert.NilError(t, err)
152 defer srv.Close()
153
154 srv.PatchNet(net.DefaultResolver)
155 defer mockdns.UnpatchNet(net.DefaultResolver)
156 w := new(TestResponseWriter)
157 options := HandlerOptions{
158 IPv6: true,
159 StaticHosts: map[string]string{
160 "MY.DOMAIN.COM": "192.168.0.23",
161 "host.lima.internal": "10.10.0.34",
162 "my.host": "host.lima.internal",
163 "default": "my.domain.com",
164 "cycle1.example.com": "cycle2.example.com",
165 "cycle2.example.com": "cycle1.example.com",
166 "self.example.com": "self.example.com",
167 },
168 }
169
170 h, err := NewHandler(options)
171 assert.NilError(t, err)
172
173 regexMatch := func(value string, pattern string) cmp.Comparison {
174 return func() cmp.Result {
175 re := regexp.MustCompile(pattern)
176 if re.MatchString(value) {
177 return cmp.ResultSuccess
178 }
179 return cmp.ResultFailure(
180 fmt.Sprintf("%#q did not match pattern %#q", value, pattern))
181 }
182 }
183 t.Run("test TXT records", func(t *testing.T) {
184 tests := []struct {
185 testDomain string
186 expectedTXTRecord string
187 }{
188 {testDomain: "onerecord.com", expectedTXTRecord: `onerecord.com.\s+5\s+IN\s+TXT\s+"My txt record"`},
189 {testDomain: "multistringrecord.com", expectedTXTRecord: `multistringrecord.com.\s+5\s+IN\s+TXT\s+"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" "67890123456789012345678901234567890"`},
190 {testDomain: "multiplerecords.com", expectedTXTRecord: `multiplerecords.com.\s+5\s+IN\s+TXT\s+"record 1"\nmultiplerecords.com.\s*5\s*IN\s*TXT\s*"record 2"`},
191 }
192
193 for _, tc := range tests {
194 req := new(dns.Msg)
195 req.SetQuestion(dns.Fqdn(tc.testDomain), dns.TypeTXT)
196 h.ServeDNS(w, req)
197 assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedTXTRecord))
198 }
199 })
200
201 t.Run("test A records", func(t *testing.T) {
202 tests := []struct {
203 testDomain string
204 expectedARecord string
205 }{
206 {testDomain: "my.domain.com", expectedARecord: `my.domain.com.\s+5\s+IN\s+A\s+192.168.0.23`},
207 {testDomain: "host.lima.internal", expectedARecord: `host.lima.internal.\s+5\s+IN\s+A\s+10.10.0.34`},
208 }
209
210 for _, tc := range tests {
211 req := new(dns.Msg)
212 req.SetQuestion(dns.Fqdn(tc.testDomain), dns.TypeA)
213 h.ServeDNS(w, req)
214 assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedARecord))
215 }
216 })
217
218 t.Run("test CNAME records", func(t *testing.T) {
219 tests := []struct {
220 testDomain string
221 expectedCNAME string
222 }{
223 {testDomain: "my.host", expectedCNAME: `my.host.\s+5\s+IN\s+CNAME\s+host.lima.internal.`},
224 {testDomain: "default", expectedCNAME: `default.\s+5\s+IN\s+CNAME\s+my.domain.com.`},
225 }
226
227 for _, tc := range tests {
228 req := new(dns.Msg)
229 req.SetQuestion(dns.Fqdn(tc.testDomain), dns.TypeCNAME)
230 h.ServeDNS(w, req)
231 assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedCNAME))
232 }
233 })
234
235 t.Run("test static hosts are matched case-insensitively", func(t *testing.T) {
236 tests := []struct {
237 testDomain string
238 expectedRecord string
239 qtype uint16
240 }{
241 {testDomain: "My.Domain.Com", expectedRecord: `My.Domain.Com.\s+5\s+IN\s+A\s+192.168.0.23`, qtype: dns.TypeA},
242 {testDomain: "HOST.LIMA.INTERNAL", expectedRecord: `HOST.LIMA.INTERNAL.\s+5\s+IN\s+A\s+10.10.0.34`, qtype: dns.TypeA},
243 {testDomain: "MY.HOST", expectedRecord: `MY.HOST.\s+5\s+IN\s+CNAME\s+host.lima.internal.`, qtype: dns.TypeCNAME},
244 }
245
246 for _, tc := range tests {
247 req := new(dns.Msg)
248 req.SetQuestion(dns.Fqdn(tc.testDomain), tc.qtype)
249 h.ServeDNS(w, req)
250 assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedRecord))
251 }
252 })
253
254 t.Run("test cyclic CNAME records", func(t *testing.T) {
255 tests := []struct {
256 testDomain string
257 expectedCNAME string
258 }{
259 {testDomain: "cycle1.example.com", expectedCNAME: `cycle1.example.com.`},
260 {testDomain: "self.example.com", expectedCNAME: `self.example.com.`},
261 }
262
263 for _, tc := range tests {
264 req := new(dns.Msg)
265 req.SetQuestion(dns.Fqdn(tc.testDomain), dns.TypeCNAME)
266 h.ServeDNS(w, req)
267 assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedCNAME))
268 }
269 })
270}
271
272type TestResponseWriter struct{}
273
274// LocalAddr returns the net.Addr of the server
275func (r TestResponseWriter) LocalAddr() net.Addr {
276 return new(TestAddr)
277}
278
279// RemoteAddr returns the net.Addr of the client that sent the current request.
280func (r TestResponseWriter) RemoteAddr() net.Addr {
281 return new(TestAddr)
282}
283
284// Network returns the value of the Net field of the Server (e.g., "tcp", "tcp-tls").
285func (r TestResponseWriter) Network() string {
286 return ""
287}
288
289// WriteMsg writes a reply back to the client.
290func (r TestResponseWriter) WriteMsg(newMsg *dns.Msg) error {
291 dnsResult = newMsg
292 return nil
293}
294
295// Write writes a raw buffer back to the client.
296func (r TestResponseWriter) Write([]byte) (int, error) {
297 return 0, nil
298}
299
300// Close closes the connection.
301func (r TestResponseWriter) Close() error {
302 return nil
303}
304
305// TsigStatus returns the status of the Tsig.
306func (r TestResponseWriter) TsigStatus() error {
307 return nil
308}
309
310// TsigTimersOnly sets the tsig timers only boolean.
311func (r TestResponseWriter) TsigTimersOnly(bool) {
312}
313
314// Hijack lets the caller take over the connection.
315// After a call to Hijack(), the DNS package will not do anything with the connection.
316func (r TestResponseWriter) Hijack() {
317}
318
319type TestAddr struct{}
320
321func (r TestAddr) Network() string {
322 return ""
323}
324
325func (r TestAddr) String() string {
326 return ""
327}