Source file
src/net/conf_test.go
Documentation: net
1
2
3
4
5
6
7 package net
8
9 import (
10 "os"
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: os.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: os.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
175 {
176 name: "linux_no_nsswitch.conf",
177 c: &conf{
178 goos: "linux",
179 nss: &nssConf{err: os.ErrNotExist},
180 resolv: defaultResolvConf,
181 },
182 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNSFiles}},
183 },
184 {
185 name: "files_mdns_dns",
186 c: &conf{
187 nss: nssStr("hosts: files mdns dns"),
188 resolv: defaultResolvConf,
189 },
190 hostTests: []nssHostTest{
191 {"x.com", "myhostname", hostLookupFilesDNS},
192 {"x.local", "myhostname", hostLookupCgo},
193 },
194 },
195 {
196 name: "dns_special_hostnames",
197 c: &conf{
198 nss: nssStr("hosts: dns"),
199 resolv: defaultResolvConf,
200 },
201 hostTests: []nssHostTest{
202 {"x.com", "myhostname", hostLookupDNS},
203 {"x\\.com", "myhostname", hostLookupCgo},
204 {"foo.com%en0", "myhostname", hostLookupCgo},
205 },
206 },
207 {
208 name: "mdns_allow",
209 c: &conf{
210 nss: nssStr("hosts: files mdns dns"),
211 resolv: defaultResolvConf,
212 hasMDNSAllow: true,
213 },
214 hostTests: []nssHostTest{
215 {"x.com", "myhostname", hostLookupCgo},
216 {"x.local", "myhostname", hostLookupCgo},
217 },
218 },
219 {
220 name: "files_dns",
221 c: &conf{
222 nss: nssStr("hosts: files dns"),
223 resolv: defaultResolvConf,
224 },
225 hostTests: []nssHostTest{
226 {"x.com", "myhostname", hostLookupFilesDNS},
227 {"x", "myhostname", hostLookupFilesDNS},
228 {"x.local", "myhostname", hostLookupCgo},
229 },
230 },
231 {
232 name: "dns_files",
233 c: &conf{
234 nss: nssStr("hosts: dns files"),
235 resolv: defaultResolvConf,
236 },
237 hostTests: []nssHostTest{
238 {"x.com", "myhostname", hostLookupDNSFiles},
239 {"x", "myhostname", hostLookupDNSFiles},
240 {"x.local", "myhostname", hostLookupCgo},
241 },
242 },
243 {
244 name: "something_custom",
245 c: &conf{
246 nss: nssStr("hosts: dns files something_custom"),
247 resolv: defaultResolvConf,
248 },
249 hostTests: []nssHostTest{
250 {"x.com", "myhostname", hostLookupCgo},
251 },
252 },
253 {
254 name: "myhostname",
255 c: &conf{
256 nss: nssStr("hosts: files dns myhostname"),
257 resolv: defaultResolvConf,
258 },
259 hostTests: []nssHostTest{
260 {"x.com", "myhostname", hostLookupFilesDNS},
261 {"myhostname", "myhostname", hostLookupCgo},
262 {"myHostname", "myhostname", hostLookupCgo},
263 {"myhostname.dot", "myhostname.dot", hostLookupCgo},
264 {"myHostname.dot", "myhostname.dot", hostLookupCgo},
265 {"gateway", "myhostname", hostLookupCgo},
266 {"Gateway", "myhostname", hostLookupCgo},
267 {"localhost", "myhostname", hostLookupCgo},
268 {"Localhost", "myhostname", hostLookupCgo},
269 {"anything.localhost", "myhostname", hostLookupCgo},
270 {"Anything.localhost", "myhostname", hostLookupCgo},
271 {"localhost.localdomain", "myhostname", hostLookupCgo},
272 {"Localhost.Localdomain", "myhostname", hostLookupCgo},
273 {"anything.localhost.localdomain", "myhostname", hostLookupCgo},
274 {"Anything.Localhost.Localdomain", "myhostname", hostLookupCgo},
275 {"somehostname", "myhostname", hostLookupFilesDNS},
276 {"", "myhostname", hostLookupFilesDNS},
277 },
278 },
279 {
280 name: "ubuntu14.04.02",
281 c: &conf{
282 nss: nssStr("hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4"),
283 resolv: defaultResolvConf,
284 },
285 hostTests: []nssHostTest{
286 {"x.com", "myhostname", hostLookupFilesDNS},
287 {"somehostname", "myhostname", hostLookupFilesDNS},
288 {"myhostname", "myhostname", hostLookupCgo},
289 },
290 },
291
292
293
294
295 {
296 name: "debian_squeeze",
297 c: &conf{
298 nss: nssStr("hosts: dns [success=return notfound=continue unavail=continue tryagain=continue] files [notfound=return]"),
299 resolv: defaultResolvConf,
300 },
301 hostTests: []nssHostTest{
302 {"x.com", "myhostname", hostLookupDNSFiles},
303 {"somehostname", "myhostname", hostLookupDNSFiles},
304 },
305 },
306 {
307 name: "resolv.conf-unknown",
308 c: &conf{
309 nss: nssStr("foo: bar"),
310 resolv: &dnsConfig{servers: defaultNS, ndots: 1, timeout: 5, attempts: 2, unknownOpt: true},
311 },
312 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
313 },
314
315 {
316 name: "android",
317 c: &conf{
318 goos: "android",
319 nss: nssStr(""),
320 resolv: defaultResolvConf,
321 },
322 hostTests: []nssHostTest{
323 {"x.com", "myhostname", hostLookupCgo},
324 },
325 },
326
327 {
328 name: "resolver-prefergo",
329 resolver: &Resolver{PreferGo: true},
330 c: &conf{
331 goos: "darwin",
332 forceCgoLookupHost: true,
333 resolv: defaultResolvConf,
334 nss: nssStr(""),
335 netCgo: true,
336 },
337 hostTests: []nssHostTest{
338 {"localhost", "myhostname", hostLookupFilesDNS},
339 },
340 },
341 }
342
343 origGetHostname := getHostname
344 defer func() { getHostname = origGetHostname }()
345
346 for _, tt := range tests {
347 for _, ht := range tt.hostTests {
348 getHostname = func() (string, error) { return ht.localhost, nil }
349
350 gotOrder := tt.c.hostLookupOrder(tt.resolver, ht.host)
351 if gotOrder != ht.want {
352 t.Errorf("%s: hostLookupOrder(%q) = %v; want %v", tt.name, ht.host, gotOrder, ht.want)
353 }
354 }
355 }
356
357 }
358
359 func TestSystemConf(t *testing.T) {
360 systemConf()
361 }
362
View as plain text