1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
15 type nssHostTest
struct {
21 func nssStr(s
string) *nssConf
{ return parseNSSConf(strings
.NewReader(s
)) }
23 // represents a dnsConfig returned by parsing a nonexistent resolv.conf
24 var defaultResolvConf
= &dnsConfig
{
32 func TestConfHostLookupOrder(t
*testing
.T
) {
36 hostTests
[]nssHostTest
41 forceCgoLookupHost
: true,
42 nss
: nssStr("foo: bar"),
43 resolv
: defaultResolvConf
,
45 hostTests
: []nssHostTest
{
46 {"foo.local", "myhostname", hostLookupCgo
},
47 {"google.com", "myhostname", hostLookupCgo
},
51 name
: "netgo_dns_before_files",
54 nss
: nssStr("hosts: dns files"),
55 resolv
: defaultResolvConf
,
57 hostTests
: []nssHostTest
{
58 {"x.com", "myhostname", hostLookupDNSFiles
},
62 name
: "netgo_fallback_on_cgo",
65 nss
: nssStr("hosts: dns files something_custom"),
66 resolv
: defaultResolvConf
,
68 hostTests
: []nssHostTest
{
69 {"x.com", "myhostname", hostLookupFilesDNS
},
73 name
: "ubuntu_trusty_avahi",
75 nss
: nssStr("hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4"),
76 resolv
: defaultResolvConf
,
78 hostTests
: []nssHostTest
{
79 {"foo.local", "myhostname", hostLookupCgo
},
80 {"foo.local.", "myhostname", hostLookupCgo
},
81 {"foo.LOCAL", "myhostname", hostLookupCgo
},
82 {"foo.LOCAL.", "myhostname", hostLookupCgo
},
83 {"google.com", "myhostname", hostLookupFilesDNS
},
87 name
: "freebsdlinux_no_resolv_conf",
90 nss
: nssStr("foo: bar"),
91 resolv
: defaultResolvConf
,
93 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupFilesDNS
}},
95 // On OpenBSD, no resolv.conf means no DNS.
97 name
: "openbsd_no_resolv_conf",
100 resolv
: defaultResolvConf
,
102 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupFiles
}},
105 name
: "solaris_no_nsswitch",
108 nss
: &nssConf
{err
: os
.ErrNotExist
},
109 resolv
: defaultResolvConf
,
111 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupCgo
}},
114 name
: "openbsd_lookup_bind_file",
117 resolv
: &dnsConfig
{lookup
: []string{"bind", "file"}},
119 hostTests
: []nssHostTest
{
120 {"google.com", "myhostname", hostLookupDNSFiles
},
121 {"foo.local", "myhostname", hostLookupDNSFiles
},
125 name
: "openbsd_lookup_file_bind",
128 resolv
: &dnsConfig
{lookup
: []string{"file", "bind"}},
130 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupFilesDNS
}},
133 name
: "openbsd_lookup_bind",
136 resolv
: &dnsConfig
{lookup
: []string{"bind"}},
138 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupDNS
}},
141 name
: "openbsd_lookup_file",
144 resolv
: &dnsConfig
{lookup
: []string{"file"}},
146 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupFiles
}},
149 name
: "openbsd_lookup_yp",
152 resolv
: &dnsConfig
{lookup
: []string{"file", "bind", "yp"}},
154 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupCgo
}},
157 name
: "openbsd_lookup_two",
160 resolv
: &dnsConfig
{lookup
: []string{"file", "foo"}},
162 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupCgo
}},
165 name
: "openbsd_lookup_empty",
168 resolv
: &dnsConfig
{lookup
: nil},
170 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupDNSFiles
}},
172 // glibc lacking an nsswitch.conf, per
173 // http://www.gnu.org/software/libc/manual/html_node/Notes-on-NSS-Configuration-File.html
175 name
: "linux_no_nsswitch.conf",
178 nss
: &nssConf
{err
: os
.ErrNotExist
},
179 resolv
: defaultResolvConf
,
181 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupDNSFiles
}},
184 name
: "files_mdns_dns",
186 nss
: nssStr("hosts: files mdns dns"),
187 resolv
: defaultResolvConf
,
189 hostTests
: []nssHostTest
{
190 {"x.com", "myhostname", hostLookupFilesDNS
},
191 {"x.local", "myhostname", hostLookupCgo
},
195 name
: "dns_special_hostnames",
197 nss
: nssStr("hosts: dns"),
198 resolv
: defaultResolvConf
,
200 hostTests
: []nssHostTest
{
201 {"x.com", "myhostname", hostLookupDNS
},
202 {"x\\.com", "myhostname", hostLookupCgo
}, // punt on weird glibc escape
203 {"foo.com%en0", "myhostname", hostLookupCgo
}, // and IPv6 zones
209 nss
: nssStr("hosts: files mdns dns"),
210 resolv
: defaultResolvConf
,
213 hostTests
: []nssHostTest
{
214 {"x.com", "myhostname", hostLookupCgo
},
215 {"x.local", "myhostname", hostLookupCgo
},
221 nss
: nssStr("hosts: files dns"),
222 resolv
: defaultResolvConf
,
224 hostTests
: []nssHostTest
{
225 {"x.com", "myhostname", hostLookupFilesDNS
},
226 {"x", "myhostname", hostLookupFilesDNS
},
227 {"x.local", "myhostname", hostLookupCgo
},
233 nss
: nssStr("hosts: dns files"),
234 resolv
: defaultResolvConf
,
236 hostTests
: []nssHostTest
{
237 {"x.com", "myhostname", hostLookupDNSFiles
},
238 {"x", "myhostname", hostLookupDNSFiles
},
239 {"x.local", "myhostname", hostLookupCgo
},
243 name
: "something_custom",
245 nss
: nssStr("hosts: dns files something_custom"),
246 resolv
: defaultResolvConf
,
248 hostTests
: []nssHostTest
{
249 {"x.com", "myhostname", hostLookupCgo
},
255 nss
: nssStr("hosts: files dns myhostname"),
256 resolv
: defaultResolvConf
,
258 hostTests
: []nssHostTest
{
259 {"x.com", "myhostname", hostLookupFilesDNS
},
260 {"myhostname", "myhostname", hostLookupCgo
},
261 {"myHostname", "myhostname", hostLookupCgo
},
262 {"myhostname.dot", "myhostname.dot", hostLookupCgo
},
263 {"myHostname.dot", "myhostname.dot", hostLookupCgo
},
264 {"gateway", "myhostname", hostLookupCgo
},
265 {"Gateway", "myhostname", hostLookupCgo
},
266 {"localhost", "myhostname", hostLookupCgo
},
267 {"Localhost", "myhostname", hostLookupCgo
},
268 {"anything.localhost", "myhostname", hostLookupCgo
},
269 {"Anything.localhost", "myhostname", hostLookupCgo
},
270 {"localhost.localdomain", "myhostname", hostLookupCgo
},
271 {"Localhost.Localdomain", "myhostname", hostLookupCgo
},
272 {"anything.localhost.localdomain", "myhostname", hostLookupCgo
},
273 {"Anything.Localhost.Localdomain", "myhostname", hostLookupCgo
},
274 {"somehostname", "myhostname", hostLookupFilesDNS
},
275 {"", "myhostname", hostLookupFilesDNS
}, // Issue 13623
279 name
: "ubuntu14.04.02",
281 nss
: nssStr("hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4"),
282 resolv
: defaultResolvConf
,
284 hostTests
: []nssHostTest
{
285 {"x.com", "myhostname", hostLookupFilesDNS
},
286 {"somehostname", "myhostname", hostLookupFilesDNS
},
287 {"myhostname", "myhostname", hostLookupCgo
},
290 // Debian Squeeze is just "dns,files", but lists all
291 // the default criteria for dns, but then has a
292 // non-standard but redundant notfound=return for the
295 name
: "debian_squeeze",
297 nss
: nssStr("hosts: dns [success=return notfound=continue unavail=continue tryagain=continue] files [notfound=return]"),
298 resolv
: defaultResolvConf
,
300 hostTests
: []nssHostTest
{
301 {"x.com", "myhostname", hostLookupDNSFiles
},
302 {"somehostname", "myhostname", hostLookupDNSFiles
},
306 name
: "resolv.conf-unknown",
308 nss
: nssStr("foo: bar"),
309 resolv
: &dnsConfig
{servers
: defaultNS
, ndots
: 1, timeout
: 5, attempts
: 2, unknownOpt
: true},
311 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupCgo
}},
313 // Android should always use cgo.
319 resolv
: defaultResolvConf
,
321 hostTests
: []nssHostTest
{
322 {"x.com", "myhostname", hostLookupCgo
},
327 origGetHostname
:= getHostname
328 defer func() { getHostname
= origGetHostname
}()
330 for _
, tt
:= range tests
{
331 for _
, ht
:= range tt
.hostTests
{
332 getHostname
= func() (string, error
) { return ht
.localhost
, nil }
334 gotOrder
:= tt
.c
.hostLookupOrder(ht
.host
)
335 if gotOrder
!= ht
.want
{
336 t
.Errorf("%s: hostLookupOrder(%q) = %v; want %v", tt
.name
, ht
.host
, gotOrder
, ht
.want
)
343 func TestSystemConf(t
*testing
.T
) {