Source file
src/net/conf_test.go
1
2
3
4
5
6
7 package net
8
9 import (
10 "io/fs"
11 "strings"
12 "testing"
13 )
14
15 type nssHostTest struct {
16 host string
17 localhost string
18 want hostLookupOrder
19 }
20
21 func nssStr(s string) *nssConf { return parseNSSConf(strings.NewReader(s)) }
22
23
24 var defaultResolvConf = &dnsConfig{
25 servers: defaultNS,
26 ndots: 1,
27 timeout: 5,
28 attempts: 2,
29 err: fs.ErrNotExist,
30 }
31
32 func TestConfHostLookupOrder(t *testing.T) {
33 tests := []struct {
34 name string
35 c *conf
36 resolver *Resolver
37 hostTests []nssHostTest
38 }{
39 {
40 name: "force",
41 c: &conf{
42 forceCgoLookupHost: true,
43 nss: nssStr("foo: bar"),
44 resolv: defaultResolvConf,
45 },
46 hostTests: []nssHostTest{
47 {"foo.local", "myhostname", hostLookupCgo},
48 {"google.com", "myhostname", hostLookupCgo},
49 },
50 },
51 {
52 name: "netgo_dns_before_files",
53 c: &conf{
54 netGo: true,
55 nss: nssStr("hosts: dns files"),
56 resolv: defaultResolvConf,
57 },
58 hostTests: []nssHostTest{
59 {"x.com", "myhostname", hostLookupDNSFiles},
60 },
61 },
62 {
63 name: "netgo_fallback_on_cgo",
64 c: &conf{
65 netGo: true,
66 nss: nssStr("hosts: dns files something_custom"),
67 resolv: defaultResolvConf,
68 },
69 hostTests: []nssHostTest{
70 {"x.com", "myhostname", hostLookupFilesDNS},
71 },
72 },
73 {
74 name: "ubuntu_trusty_avahi",
75 c: &conf{
76 nss: nssStr("hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4"),
77 resolv: defaultResolvConf,
78 },
79 hostTests: []nssHostTest{
80 {"foo.local", "myhostname", hostLookupCgo},
81 {"foo.local.", "myhostname", hostLookupCgo},
82 {"foo.LOCAL", "myhostname", hostLookupCgo},
83 {"foo.LOCAL.", "myhostname", hostLookupCgo},
84 {"google.com", "myhostname", hostLookupFilesDNS},
85 },
86 },
87 {
88 name: "freebsdlinux_no_resolv_conf",
89 c: &conf{
90 goos: "freebsd",
91 nss: nssStr("foo: bar"),
92 resolv: defaultResolvConf,
93 },
94 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
95 },
96
97 {
98 name: "openbsd_no_resolv_conf",
99 c: &conf{
100 goos: "openbsd",
101 resolv: defaultResolvConf,
102 },
103 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFiles}},
104 },
105 {
106 name: "solaris_no_nsswitch",
107 c: &conf{
108 goos: "solaris",
109 nss: &nssConf{err: fs.ErrNotExist},
110 resolv: defaultResolvConf,
111 },
112 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
113 },
114 {
115 name: "openbsd_lookup_bind_file",
116 c: &conf{
117 goos: "openbsd",
118 resolv: &dnsConfig{lookup: []string{"bind", "file"}},
119 },
120 hostTests: []nssHostTest{
121 {"google.com", "myhostname", hostLookupDNSFiles},
122 {"foo.local", "myhostname", hostLookupDNSFiles},
123 },
124 },
125 {
126 name: "openbsd_lookup_file_bind",
127 c: &conf{
128 goos: "openbsd",
129 resolv: &dnsConfig{lookup: []string{"file", "bind"}},
130 },
131 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
132 },
133 {
134 name: "openbsd_lookup_bind",
135 c: &conf{
136 goos: "openbsd",
137 resolv: &dnsConfig{lookup: []string{"bind"}},
138 },
139 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNS}},
140 },
141 {
142 name: "openbsd_lookup_file",
143 c: &conf{
144 goos: "openbsd",
145 resolv: &dnsConfig{lookup: []string{"file"}},
146 },
147 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFiles}},
148 },
149 {
150 name: "openbsd_lookup_yp",
151 c: &conf{
152 goos: "openbsd",
153 resolv: &dnsConfig{lookup: []string{"file", "bind", "yp"}},
154 },
155 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
156 },
157 {
158 name: "openbsd_lookup_two",
159 c: &conf{
160 goos: "openbsd",
161 resolv: &dnsConfig{lookup: []string{"file", "foo"}},
162 },
163 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
164 },
165 {
166 name: "openbsd_lookup_empty",
167 c: &conf{
168 goos: "openbsd",
169 resolv: &dnsConfig{lookup: nil},
170 },
171 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNSFiles}},
172 },
173 {
174 name: "linux_no_nsswitch.conf",
175 c: &conf{
176 goos: "linux",
177 nss: &nssConf{err: fs.ErrNotExist},
178 resolv: defaultResolvConf,
179 },
180 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
181 },
182 {
183 name: "linux_empty_nsswitch.conf",
184 c: &conf{
185 goos: "linux",
186 nss: nssStr(""),
187 resolv: defaultResolvConf,
188 },
189 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
190 },
191 {
192 name: "files_mdns_dns",
193 c: &conf{
194 nss: nssStr("hosts: files mdns dns"),
195 resolv: defaultResolvConf,
196 },
197 hostTests: []nssHostTest{
198 {"x.com", "myhostname", hostLookupFilesDNS},
199 {"x.local", "myhostname", hostLookupCgo},
200 },
201 },
202 {
203 name: "dns_special_hostnames",
204 c: &conf{
205 nss: nssStr("hosts: dns"),
206 resolv: defaultResolvConf,
207 },
208 hostTests: []nssHostTest{
209 {"x.com", "myhostname", hostLookupDNS},
210 {"x\\.com", "myhostname", hostLookupCgo},
211 {"foo.com%en0", "myhostname", hostLookupCgo},
212 },
213 },
214 {
215 name: "mdns_allow",
216 c: &conf{
217 nss: nssStr("hosts: files mdns dns"),
218 resolv: defaultResolvConf,
219 hasMDNSAllow: true,
220 },
221 hostTests: []nssHostTest{
222 {"x.com", "myhostname", hostLookupCgo},
223 {"x.local", "myhostname", hostLookupCgo},
224 },
225 },
226 {
227 name: "files_dns",
228 c: &conf{
229 nss: nssStr("hosts: files dns"),
230 resolv: defaultResolvConf,
231 },
232 hostTests: []nssHostTest{
233 {"x.com", "myhostname", hostLookupFilesDNS},
234 {"x", "myhostname", hostLookupFilesDNS},
235 {"x.local", "myhostname", hostLookupCgo},
236 },
237 },
238 {
239 name: "dns_files",
240 c: &conf{
241 nss: nssStr("hosts: dns files"),
242 resolv: defaultResolvConf,
243 },
244 hostTests: []nssHostTest{
245 {"x.com", "myhostname", hostLookupDNSFiles},
246 {"x", "myhostname", hostLookupDNSFiles},
247 {"x.local", "myhostname", hostLookupCgo},
248 },
249 },
250 {
251 name: "something_custom",
252 c: &conf{
253 nss: nssStr("hosts: dns files something_custom"),
254 resolv: defaultResolvConf,
255 },
256 hostTests: []nssHostTest{
257 {"x.com", "myhostname", hostLookupCgo},
258 },
259 },
260 {
261 name: "myhostname",
262 c: &conf{
263 nss: nssStr("hosts: files dns myhostname"),
264 resolv: defaultResolvConf,
265 },
266 hostTests: []nssHostTest{
267 {"x.com", "myhostname", hostLookupFilesDNS},
268 {"myhostname", "myhostname", hostLookupCgo},
269 {"myHostname", "myhostname", hostLookupCgo},
270 {"myhostname.dot", "myhostname.dot", hostLookupCgo},
271 {"myHostname.dot", "myhostname.dot", hostLookupCgo},
272 {"gateway", "myhostname", hostLookupCgo},
273 {"Gateway", "myhostname", hostLookupCgo},
274 {"localhost", "myhostname", hostLookupCgo},
275 {"Localhost", "myhostname", hostLookupCgo},
276 {"anything.localhost", "myhostname", hostLookupCgo},
277 {"Anything.localhost", "myhostname", hostLookupCgo},
278 {"localhost.localdomain", "myhostname", hostLookupCgo},
279 {"Localhost.Localdomain", "myhostname", hostLookupCgo},
280 {"anything.localhost.localdomain", "myhostname", hostLookupCgo},
281 {"Anything.Localhost.Localdomain", "myhostname", hostLookupCgo},
282 {"somehostname", "myhostname", hostLookupFilesDNS},
283 {"", "myhostname", hostLookupFilesDNS},
284 },
285 },
286 {
287 name: "ubuntu14.04.02",
288 c: &conf{
289 nss: nssStr("hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4"),
290 resolv: defaultResolvConf,
291 },
292 hostTests: []nssHostTest{
293 {"x.com", "myhostname", hostLookupFilesDNS},
294 {"somehostname", "myhostname", hostLookupFilesDNS},
295 {"myhostname", "myhostname", hostLookupCgo},
296 },
297 },
298
299
300
301
302 {
303 name: "debian_squeeze",
304 c: &conf{
305 nss: nssStr("hosts: dns [success=return notfound=continue unavail=continue tryagain=continue] files [notfound=return]"),
306 resolv: defaultResolvConf,
307 },
308 hostTests: []nssHostTest{
309 {"x.com", "myhostname", hostLookupDNSFiles},
310 {"somehostname", "myhostname", hostLookupDNSFiles},
311 },
312 },
313 {
314 name: "resolv.conf-unknown",
315 c: &conf{
316 nss: nssStr("foo: bar"),
317 resolv: &dnsConfig{servers: defaultNS, ndots: 1, timeout: 5, attempts: 2, unknownOpt: true},
318 },
319 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
320 },
321
322 {
323 name: "android",
324 c: &conf{
325 goos: "android",
326 nss: nssStr(""),
327 resolv: defaultResolvConf,
328 },
329 hostTests: []nssHostTest{
330 {"x.com", "myhostname", hostLookupCgo},
331 },
332 },
333
334 {
335 name: "resolver-prefergo",
336 resolver: &Resolver{PreferGo: true},
337 c: &conf{
338 goos: "darwin",
339 forceCgoLookupHost: true,
340 resolv: defaultResolvConf,
341 nss: nssStr(""),
342 netCgo: true,
343 },
344 hostTests: []nssHostTest{
345 {"localhost", "myhostname", hostLookupFilesDNS},
346 },
347 },
348 }
349
350 origGetHostname := getHostname
351 defer func() { getHostname = origGetHostname }()
352
353 for _, tt := range tests {
354 for _, ht := range tt.hostTests {
355 getHostname = func() (string, error) { return ht.localhost, nil }
356
357 gotOrder := tt.c.hostLookupOrder(tt.resolver, ht.host)
358 if gotOrder != ht.want {
359 t.Errorf("%s: hostLookupOrder(%q) = %v; want %v", tt.name, ht.host, gotOrder, ht.want)
360 }
361 }
362 }
363
364 }
365
366 func TestSystemConf(t *testing.T) {
367 systemConf()
368 }
369
View as plain text