跳到主要内容

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

浏览文件

dnshosts_test.go

191 行 · 5.0 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4// From https://raw.githubusercontent.com/abiosoft/colima/v0.5.5/daemon/process/gvproxy/dnshosts_test.go
5/*
6 MIT License
7
8 Copyright (c) 2021 Abiola Ibrahim
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27*/
28
29package dnshosts
30
31import (
32 "fmt"
33 "net"
34 "slices"
35 "strings"
36 "testing"
37
38 "github.com/containers/gvisor-tap-vsock/pkg/types"
39)
40
41func Test_hostsMapIP(t *testing.T) {
42 hosts := hostMap{}
43 hosts["sample"] = "1.1.1.1"
44 hosts["another.sample"] = "1.2.2.1"
45 hosts["google.com"] = "8.8.8.8"
46 hosts["google.ae"] = "google.com"
47 hosts["google.ie"] = "google.ae"
48
49 tests := []struct {
50 host string
51 want net.IP
52 }{
53 {host: "sample", want: net.ParseIP("1.1.1.1")},
54 {host: "another.sample", want: net.ParseIP("1.2.2.1")},
55 {host: "google.com", want: net.ParseIP("8.8.8.8")},
56 {host: "google.ae", want: net.ParseIP("8.8.8.8")},
57 {host: "google.ie", want: net.ParseIP("8.8.8.8")},
58 {host: "google.sample", want: nil},
59 }
60 for i, tt := range tests {
61 t.Run(fmt.Sprint(i), func(t *testing.T) {
62 got := hosts.hostIP(tt.host)
63 if !got.Equal(tt.want) {
64 t.Errorf("hostsMapIP() = %v, want %v", got, tt.want)
65 return
66 }
67 })
68 }
69}
70
71func Test_zoneHost(t *testing.T) {
72 type val struct {
73 name string
74 recordName string
75 }
76 tests := []struct {
77 host zoneHost
78 want val
79 }{
80 {}, // test for empty value as well
81 {host: "sample", want: val{name: "sample"}},
82 {host: "another.sample", want: val{name: "sample.", recordName: "another"}},
83 {host: "another.sample.com", want: val{name: "com.", recordName: "another.sample"}},
84 {host: "a.c", want: val{name: "c.", recordName: "a"}},
85 {host: "a.b.c.d", want: val{name: "d.", recordName: "a.b.c"}},
86 }
87 for i, tt := range tests {
88 t.Run(fmt.Sprint(i), func(t *testing.T) {
89 got := val{
90 name: tt.host.name(),
91 recordName: tt.host.recordName(),
92 }
93 if got != tt.want {
94 t.Errorf("host = %+v, want %+v", got, tt.want)
95 return
96 }
97 })
98 }
99}
100
101func TestExtractZones(t *testing.T) {
102 equalZones := func(za, zb []types.Zone) bool {
103 equal := func(a, b types.Zone) bool {
104 if a.Name != b.Name {
105 return false
106 }
107 if !a.DefaultIP.Equal(b.DefaultIP) {
108 return false
109 }
110 for i := range a.Records {
111 a, b := a.Records[i], b.Records[i]
112 if !a.IP.Equal(b.IP) {
113 return false
114 }
115 if a.Name != b.Name {
116 return false
117 }
118 }
119
120 return true
121 }
122
123 for _, a := range za {
124 ib := slices.IndexFunc(zb, func(z types.Zone) bool { return z.Name == a.Name })
125 if ib < 0 {
126 return false
127 }
128 b := zb[ib]
129 if !equal(a, b) {
130 return false
131 }
132 }
133 return true
134 }
135
136 hosts := hostMap{
137 "google.com": "8.8.4.4",
138 "local.google.com": "8.8.8.8",
139 "google.ae": "google.com",
140 "localhost": "127.0.0.1",
141 "host.lima.internal": "192.168.5.2",
142 "host.docker.internal": "host.lima.internal",
143 }
144
145 tests := []struct {
146 wantZones []types.Zone
147 }{
148 {
149 wantZones: []types.Zone{
150 {
151 Name: "ae.",
152 Records: []types.Record{
153 {Name: "google", IP: net.ParseIP("8.8.4.4")},
154 },
155 },
156 {
157 Name: "com.",
158 Records: []types.Record{
159 {Name: "google", IP: net.ParseIP("8.8.4.4")},
160 {Name: "local.google", IP: net.ParseIP("8.8.8.8")},
161 },
162 },
163 {
164 Name: "internal.",
165 Records: []types.Record{
166 {Name: "host.docker", IP: net.ParseIP("192.168.5.2")},
167 {Name: "host.lima", IP: net.ParseIP("192.168.5.2")},
168 },
169 },
170 {
171 Name: "localhost",
172 DefaultIP: net.ParseIP("127.0.0.1"),
173 },
174 },
175 },
176 }
177
178 for i, tt := range tests {
179 t.Run(fmt.Sprint(i), func(t *testing.T) {
180 gotZones := ExtractZones(hosts)
181 for _, zone := range gotZones {
182 slices.SortFunc(zone.Records, func(a, b types.Record) int { return strings.Compare(a.Name, b.Name) })
183 }
184 slices.SortFunc(gotZones, func(a, b types.Zone) int { return strings.Compare(a.Name, b.Name) })
185
186 if !equalZones(gotZones, tt.wantZones) {
187 t.Errorf("extractZones() = %+v, want %+v", gotZones, tt.wantZones)
188 }
189 })
190 }
191}