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 {
20 func nssStr(s
string) *nssConf
{ return parseNSSConf(strings
.NewReader(s
)) }
22 // represents a dnsConfig returned by parsing a nonexistent resolv.conf
23 var defaultResolvConf
= &dnsConfig
{
31 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", hostLookupCgo
},
47 {"google.com", hostLookupCgo
},
51 name
: "ubuntu_trusty_avahi",
53 nss
: nssStr("hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4"),
54 resolv
: defaultResolvConf
,
56 hostTests
: []nssHostTest
{
57 {"foo.local", hostLookupCgo
},
58 {"foo.local.", hostLookupCgo
},
59 {"foo.LOCAL", hostLookupCgo
},
60 {"foo.LOCAL.", hostLookupCgo
},
61 {"google.com", hostLookupFilesDNS
},
65 name
: "freebsdlinux_no_resolv_conf",
68 nss
: nssStr("foo: bar"),
69 resolv
: defaultResolvConf
,
71 hostTests
: []nssHostTest
{{"google.com", hostLookupFilesDNS
}},
73 // On OpenBSD, no resolv.conf means no DNS.
75 name
: "openbsd_no_resolv_conf",
78 resolv
: defaultResolvConf
,
80 hostTests
: []nssHostTest
{{"google.com", hostLookupFiles
}},
83 name
: "solaris_no_nsswitch",
86 nss
: &nssConf
{err
: os
.ErrNotExist
},
87 resolv
: defaultResolvConf
,
89 hostTests
: []nssHostTest
{{"google.com", hostLookupCgo
}},
92 name
: "openbsd_lookup_bind_file",
95 resolv
: &dnsConfig
{lookup
: []string{"bind", "file"}},
97 hostTests
: []nssHostTest
{
98 {"google.com", hostLookupDNSFiles
},
99 {"foo.local", hostLookupDNSFiles
},
103 name
: "openbsd_lookup_file_bind",
106 resolv
: &dnsConfig
{lookup
: []string{"file", "bind"}},
108 hostTests
: []nssHostTest
{{"google.com", hostLookupFilesDNS
}},
111 name
: "openbsd_lookup_bind",
114 resolv
: &dnsConfig
{lookup
: []string{"bind"}},
116 hostTests
: []nssHostTest
{{"google.com", hostLookupDNS
}},
119 name
: "openbsd_lookup_file",
122 resolv
: &dnsConfig
{lookup
: []string{"file"}},
124 hostTests
: []nssHostTest
{{"google.com", hostLookupFiles
}},
127 name
: "openbsd_lookup_yp",
130 resolv
: &dnsConfig
{lookup
: []string{"file", "bind", "yp"}},
132 hostTests
: []nssHostTest
{{"google.com", hostLookupCgo
}},
135 name
: "openbsd_lookup_two",
138 resolv
: &dnsConfig
{lookup
: []string{"file", "foo"}},
140 hostTests
: []nssHostTest
{{"google.com", hostLookupCgo
}},
143 name
: "openbsd_lookup_empty",
146 resolv
: &dnsConfig
{lookup
: nil},
148 hostTests
: []nssHostTest
{{"google.com", hostLookupDNSFiles
}},
150 // glibc lacking an nsswitch.conf, per
151 // http://www.gnu.org/software/libc/manual/html_node/Notes-on-NSS-Configuration-File.html
153 name
: "linux_no_nsswitch.conf",
156 nss
: &nssConf
{err
: os
.ErrNotExist
},
157 resolv
: defaultResolvConf
,
159 hostTests
: []nssHostTest
{{"google.com", hostLookupDNSFiles
}},
162 name
: "files_mdns_dns",
164 nss
: nssStr("hosts: files mdns dns"),
165 resolv
: defaultResolvConf
,
167 hostTests
: []nssHostTest
{
168 {"x.com", hostLookupFilesDNS
},
169 {"x.local", hostLookupCgo
},
173 name
: "dns_special_hostnames",
175 nss
: nssStr("hosts: dns"),
176 resolv
: defaultResolvConf
,
178 hostTests
: []nssHostTest
{
179 {"x.com", hostLookupDNS
},
180 {"x\\.com", hostLookupCgo
}, // punt on weird glibc escape
181 {"foo.com%en0", hostLookupCgo
}, // and IPv6 zones
187 nss
: nssStr("hosts: files mdns dns"),
188 resolv
: defaultResolvConf
,
191 hostTests
: []nssHostTest
{
192 {"x.com", hostLookupCgo
},
193 {"x.local", hostLookupCgo
},
199 nss
: nssStr("hosts: files dns"),
200 resolv
: defaultResolvConf
,
202 hostTests
: []nssHostTest
{
203 {"x.com", hostLookupFilesDNS
},
204 {"x", hostLookupFilesDNS
},
205 {"x.local", hostLookupCgo
},
211 nss
: nssStr("hosts: dns files"),
212 resolv
: defaultResolvConf
,
214 hostTests
: []nssHostTest
{
215 {"x.com", hostLookupDNSFiles
},
216 {"x", hostLookupDNSFiles
},
217 {"x.local", hostLookupCgo
},
221 name
: "something_custom",
223 nss
: nssStr("hosts: dns files something_custom"),
224 resolv
: defaultResolvConf
,
226 hostTests
: []nssHostTest
{
227 {"x.com", hostLookupCgo
},
233 nss
: nssStr("hosts: files dns myhostname"),
234 resolv
: defaultResolvConf
,
236 hostTests
: []nssHostTest
{
237 {"x.com", hostLookupFilesDNS
},
238 {"somehostname", hostLookupCgo
},
242 name
: "ubuntu14.04.02",
244 nss
: nssStr("hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4"),
245 resolv
: defaultResolvConf
,
247 hostTests
: []nssHostTest
{
248 {"x.com", hostLookupFilesDNS
},
249 {"somehostname", hostLookupCgo
},
252 // Debian Squeeze is just "dns,files", but lists all
253 // the default criteria for dns, but then has a
254 // non-standard but redundant notfound=return for the
257 name
: "debian_squeeze",
259 nss
: nssStr("hosts: dns [success=return notfound=continue unavail=continue tryagain=continue] files [notfound=return]"),
260 resolv
: defaultResolvConf
,
262 hostTests
: []nssHostTest
{
263 {"x.com", hostLookupDNSFiles
},
264 {"somehostname", hostLookupDNSFiles
},
268 name
: "resolv.conf-unknown",
270 nss
: nssStr("foo: bar"),
271 resolv
: &dnsConfig
{servers
: defaultNS
, ndots
: 1, timeout
: 5, attempts
: 2, unknownOpt
: true},
273 hostTests
: []nssHostTest
{{"google.com", hostLookupCgo
}},
275 // Android should always use cgo.
281 resolv
: defaultResolvConf
,
283 hostTests
: []nssHostTest
{
284 {"x.com", hostLookupCgo
},
288 for _
, tt
:= range tests
{
289 for _
, ht
:= range tt
.hostTests
{
290 gotOrder
:= tt
.c
.hostLookupOrder(ht
.host
)
291 if gotOrder
!= ht
.want
{
292 t
.Errorf("%s: hostLookupOrder(%q) = %v; want %v", tt
.name
, ht
.host
, gotOrder
, ht
.want
)
299 func TestSystemConf(t
*testing
.T
) {