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 hurd 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
) {
37 hostTests
[]nssHostTest
42 forceCgoLookupHost
: true,
43 nss
: nssStr("foo: bar"),
44 resolv
: defaultResolvConf
,
46 hostTests
: []nssHostTest
{
47 {"foo.local", "myhostname", hostLookupCgo
},
48 {"google.com", "myhostname", hostLookupCgo
},
52 name
: "netgo_dns_before_files",
55 nss
: nssStr("hosts: dns files"),
56 resolv
: defaultResolvConf
,
58 hostTests
: []nssHostTest
{
59 {"x.com", "myhostname", hostLookupDNSFiles
},
63 name
: "netgo_fallback_on_cgo",
66 nss
: nssStr("hosts: dns files something_custom"),
67 resolv
: defaultResolvConf
,
69 hostTests
: []nssHostTest
{
70 {"x.com", "myhostname", hostLookupFilesDNS
},
74 name
: "ubuntu_trusty_avahi",
76 nss
: nssStr("hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4"),
77 resolv
: defaultResolvConf
,
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
},
88 name
: "freebsdlinux_no_resolv_conf",
91 nss
: nssStr("foo: bar"),
92 resolv
: defaultResolvConf
,
94 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupFilesDNS
}},
96 // On OpenBSD, no resolv.conf means no DNS.
98 name
: "openbsd_no_resolv_conf",
101 resolv
: defaultResolvConf
,
103 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupFiles
}},
106 name
: "solaris_no_nsswitch",
109 nss
: &nssConf
{err
: os
.ErrNotExist
},
110 resolv
: defaultResolvConf
,
112 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupCgo
}},
115 name
: "openbsd_lookup_bind_file",
118 resolv
: &dnsConfig
{lookup
: []string{"bind", "file"}},
120 hostTests
: []nssHostTest
{
121 {"google.com", "myhostname", hostLookupDNSFiles
},
122 {"foo.local", "myhostname", hostLookupDNSFiles
},
126 name
: "openbsd_lookup_file_bind",
129 resolv
: &dnsConfig
{lookup
: []string{"file", "bind"}},
131 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupFilesDNS
}},
134 name
: "openbsd_lookup_bind",
137 resolv
: &dnsConfig
{lookup
: []string{"bind"}},
139 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupDNS
}},
142 name
: "openbsd_lookup_file",
145 resolv
: &dnsConfig
{lookup
: []string{"file"}},
147 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupFiles
}},
150 name
: "openbsd_lookup_yp",
153 resolv
: &dnsConfig
{lookup
: []string{"file", "bind", "yp"}},
155 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupCgo
}},
158 name
: "openbsd_lookup_two",
161 resolv
: &dnsConfig
{lookup
: []string{"file", "foo"}},
163 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupCgo
}},
166 name
: "openbsd_lookup_empty",
169 resolv
: &dnsConfig
{lookup
: nil},
171 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupDNSFiles
}},
173 // glibc lacking an nsswitch.conf, per
174 // https://www.gnu.org/software/libc/manual/html_node/Notes-on-NSS-Configuration-File.html
176 name
: "linux_no_nsswitch.conf",
179 nss
: &nssConf
{err
: os
.ErrNotExist
},
180 resolv
: defaultResolvConf
,
182 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupDNSFiles
}},
185 name
: "files_mdns_dns",
187 nss
: nssStr("hosts: files mdns dns"),
188 resolv
: defaultResolvConf
,
190 hostTests
: []nssHostTest
{
191 {"x.com", "myhostname", hostLookupFilesDNS
},
192 {"x.local", "myhostname", hostLookupCgo
},
196 name
: "dns_special_hostnames",
198 nss
: nssStr("hosts: dns"),
199 resolv
: defaultResolvConf
,
201 hostTests
: []nssHostTest
{
202 {"x.com", "myhostname", hostLookupDNS
},
203 {"x\\.com", "myhostname", hostLookupCgo
}, // punt on weird glibc escape
204 {"foo.com%en0", "myhostname", hostLookupCgo
}, // and IPv6 zones
210 nss
: nssStr("hosts: files mdns dns"),
211 resolv
: defaultResolvConf
,
214 hostTests
: []nssHostTest
{
215 {"x.com", "myhostname", hostLookupCgo
},
216 {"x.local", "myhostname", hostLookupCgo
},
222 nss
: nssStr("hosts: files dns"),
223 resolv
: defaultResolvConf
,
225 hostTests
: []nssHostTest
{
226 {"x.com", "myhostname", hostLookupFilesDNS
},
227 {"x", "myhostname", hostLookupFilesDNS
},
228 {"x.local", "myhostname", hostLookupCgo
},
234 nss
: nssStr("hosts: dns files"),
235 resolv
: defaultResolvConf
,
237 hostTests
: []nssHostTest
{
238 {"x.com", "myhostname", hostLookupDNSFiles
},
239 {"x", "myhostname", hostLookupDNSFiles
},
240 {"x.local", "myhostname", hostLookupCgo
},
244 name
: "something_custom",
246 nss
: nssStr("hosts: dns files something_custom"),
247 resolv
: defaultResolvConf
,
249 hostTests
: []nssHostTest
{
250 {"x.com", "myhostname", hostLookupCgo
},
256 nss
: nssStr("hosts: files dns myhostname"),
257 resolv
: defaultResolvConf
,
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
}, // Issue 13623
280 name
: "ubuntu14.04.02",
282 nss
: nssStr("hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4"),
283 resolv
: defaultResolvConf
,
285 hostTests
: []nssHostTest
{
286 {"x.com", "myhostname", hostLookupFilesDNS
},
287 {"somehostname", "myhostname", hostLookupFilesDNS
},
288 {"myhostname", "myhostname", hostLookupCgo
},
291 // Debian Squeeze is just "dns,files", but lists all
292 // the default criteria for dns, but then has a
293 // non-standard but redundant notfound=return for the
296 name
: "debian_squeeze",
298 nss
: nssStr("hosts: dns [success=return notfound=continue unavail=continue tryagain=continue] files [notfound=return]"),
299 resolv
: defaultResolvConf
,
301 hostTests
: []nssHostTest
{
302 {"x.com", "myhostname", hostLookupDNSFiles
},
303 {"somehostname", "myhostname", hostLookupDNSFiles
},
307 name
: "resolv.conf-unknown",
309 nss
: nssStr("foo: bar"),
310 resolv
: &dnsConfig
{servers
: defaultNS
, ndots
: 1, timeout
: 5, attempts
: 2, unknownOpt
: true},
312 hostTests
: []nssHostTest
{{"google.com", "myhostname", hostLookupCgo
}},
314 // Android should always use cgo.
320 resolv
: defaultResolvConf
,
322 hostTests
: []nssHostTest
{
323 {"x.com", "myhostname", hostLookupCgo
},
326 // Issue 24393: make sure "Resolver.PreferGo = true" acts like netgo.
328 name
: "resolver-prefergo",
329 resolver
: &Resolver
{PreferGo
: true},
332 forceCgoLookupHost
: true, // always true for darwin
333 resolv
: defaultResolvConf
,
337 hostTests
: []nssHostTest
{
338 {"localhost", "myhostname", hostLookupFilesDNS
},
343 origGetHostname
:= getHostname
344 defer func() { getHostname
= origGetHostname
}()
346 for _
, tt
:= range tests
{
347 for _
, ht
:= range tt
.hostTests
{
348 getHostname
= func() (string, error
) { return ht
.localhost
, nil }
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
)
359 func TestSystemConf(t
*testing
.T
) {