blob: 17d03f4b5fc55f739a5e6017ea2eb941f5dacfb7 [file] [log] [blame]
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -07001// 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.
4
5// +build darwin dragonfly freebsd linux netbsd openbsd solaris
6
7package net
8
9import (
10 "os"
11 "strings"
12 "testing"
13)
14
15type nssHostTest struct {
Dan Peterson4bd95702016-11-23 11:35:17 -070016 host string
17 localhost string
18 want hostLookupOrder
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070019}
20
21func nssStr(s string) *nssConf { return parseNSSConf(strings.NewReader(s)) }
22
Alex A Skinnerf3901352015-04-25 20:50:21 -040023// represents a dnsConfig returned by parsing a nonexistent resolv.conf
24var defaultResolvConf = &dnsConfig{
25 servers: defaultNS,
26 ndots: 1,
27 timeout: 5,
28 attempts: 2,
29 err: os.ErrNotExist,
30}
31
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070032func TestConfHostLookupOrder(t *testing.T) {
33 tests := []struct {
34 name string
35 c *conf
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070036 hostTests []nssHostTest
37 }{
38 {
39 name: "force",
40 c: &conf{
41 forceCgoLookupHost: true,
42 nss: nssStr("foo: bar"),
Alex A Skinnerf3901352015-04-25 20:50:21 -040043 resolv: defaultResolvConf,
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070044 },
45 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -070046 {"foo.local", "myhostname", hostLookupCgo},
47 {"google.com", "myhostname", hostLookupCgo},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070048 },
49 },
50 {
Vishvananda Ishaya9dee7772016-02-16 17:58:11 -080051 name: "netgo_dns_before_files",
52 c: &conf{
53 netGo: true,
54 nss: nssStr("hosts: dns files"),
55 resolv: defaultResolvConf,
56 },
57 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -070058 {"x.com", "myhostname", hostLookupDNSFiles},
Vishvananda Ishaya9dee7772016-02-16 17:58:11 -080059 },
60 },
61 {
62 name: "netgo_fallback_on_cgo",
63 c: &conf{
64 netGo: true,
65 nss: nssStr("hosts: dns files something_custom"),
66 resolv: defaultResolvConf,
67 },
68 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -070069 {"x.com", "myhostname", hostLookupFilesDNS},
Vishvananda Ishaya9dee7772016-02-16 17:58:11 -080070 },
71 },
72 {
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070073 name: "ubuntu_trusty_avahi",
74 c: &conf{
Alex A Skinnerf3901352015-04-25 20:50:21 -040075 nss: nssStr("hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4"),
76 resolv: defaultResolvConf,
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070077 },
78 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -070079 {"foo.local", "myhostname", hostLookupCgo},
80 {"foo.local.", "myhostname", hostLookupCgo},
81 {"foo.LOCAL", "myhostname", hostLookupCgo},
82 {"foo.LOCAL.", "myhostname", hostLookupCgo},
83 {"google.com", "myhostname", hostLookupFilesDNS},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070084 },
85 },
86 {
87 name: "freebsdlinux_no_resolv_conf",
88 c: &conf{
Alex A Skinnerf3901352015-04-25 20:50:21 -040089 goos: "freebsd",
90 nss: nssStr("foo: bar"),
91 resolv: defaultResolvConf,
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070092 },
Dan Peterson4bd95702016-11-23 11:35:17 -070093 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070094 },
95 // On OpenBSD, no resolv.conf means no DNS.
96 {
97 name: "openbsd_no_resolv_conf",
98 c: &conf{
Alex A Skinnerf3901352015-04-25 20:50:21 -040099 goos: "openbsd",
100 resolv: defaultResolvConf,
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700101 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700102 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFiles}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700103 },
104 {
105 name: "solaris_no_nsswitch",
106 c: &conf{
Alex A Skinnerf3901352015-04-25 20:50:21 -0400107 goos: "solaris",
108 nss: &nssConf{err: os.ErrNotExist},
109 resolv: defaultResolvConf,
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700110 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700111 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700112 },
113 {
114 name: "openbsd_lookup_bind_file",
115 c: &conf{
116 goos: "openbsd",
117 resolv: &dnsConfig{lookup: []string{"bind", "file"}},
118 },
119 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700120 {"google.com", "myhostname", hostLookupDNSFiles},
121 {"foo.local", "myhostname", hostLookupDNSFiles},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700122 },
123 },
124 {
125 name: "openbsd_lookup_file_bind",
126 c: &conf{
127 goos: "openbsd",
128 resolv: &dnsConfig{lookup: []string{"file", "bind"}},
129 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700130 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700131 },
132 {
133 name: "openbsd_lookup_bind",
134 c: &conf{
135 goos: "openbsd",
136 resolv: &dnsConfig{lookup: []string{"bind"}},
137 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700138 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNS}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700139 },
140 {
141 name: "openbsd_lookup_file",
142 c: &conf{
143 goos: "openbsd",
144 resolv: &dnsConfig{lookup: []string{"file"}},
145 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700146 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFiles}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700147 },
148 {
149 name: "openbsd_lookup_yp",
150 c: &conf{
151 goos: "openbsd",
152 resolv: &dnsConfig{lookup: []string{"file", "bind", "yp"}},
153 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700154 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700155 },
156 {
157 name: "openbsd_lookup_two",
158 c: &conf{
159 goos: "openbsd",
160 resolv: &dnsConfig{lookup: []string{"file", "foo"}},
161 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700162 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700163 },
164 {
165 name: "openbsd_lookup_empty",
166 c: &conf{
167 goos: "openbsd",
168 resolv: &dnsConfig{lookup: nil},
169 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700170 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNSFiles}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700171 },
172 // glibc lacking an nsswitch.conf, per
173 // http://www.gnu.org/software/libc/manual/html_node/Notes-on-NSS-Configuration-File.html
174 {
175 name: "linux_no_nsswitch.conf",
176 c: &conf{
Alex A Skinnerf3901352015-04-25 20:50:21 -0400177 goos: "linux",
178 nss: &nssConf{err: os.ErrNotExist},
179 resolv: defaultResolvConf,
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700180 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700181 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNSFiles}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700182 },
183 {
184 name: "files_mdns_dns",
Alex A Skinnerf3901352015-04-25 20:50:21 -0400185 c: &conf{
186 nss: nssStr("hosts: files mdns dns"),
187 resolv: defaultResolvConf,
188 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700189 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700190 {"x.com", "myhostname", hostLookupFilesDNS},
191 {"x.local", "myhostname", hostLookupCgo},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700192 },
193 },
194 {
195 name: "dns_special_hostnames",
Alex A Skinnerf3901352015-04-25 20:50:21 -0400196 c: &conf{
197 nss: nssStr("hosts: dns"),
198 resolv: defaultResolvConf,
199 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700200 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700201 {"x.com", "myhostname", hostLookupDNS},
202 {"x\\.com", "myhostname", hostLookupCgo}, // punt on weird glibc escape
203 {"foo.com%en0", "myhostname", hostLookupCgo}, // and IPv6 zones
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700204 },
205 },
206 {
207 name: "mdns_allow",
208 c: &conf{
209 nss: nssStr("hosts: files mdns dns"),
Alex A Skinnerf3901352015-04-25 20:50:21 -0400210 resolv: defaultResolvConf,
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700211 hasMDNSAllow: true,
212 },
213 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700214 {"x.com", "myhostname", hostLookupCgo},
215 {"x.local", "myhostname", hostLookupCgo},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700216 },
217 },
218 {
219 name: "files_dns",
Alex A Skinnerf3901352015-04-25 20:50:21 -0400220 c: &conf{
221 nss: nssStr("hosts: files dns"),
222 resolv: defaultResolvConf,
223 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700224 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700225 {"x.com", "myhostname", hostLookupFilesDNS},
226 {"x", "myhostname", hostLookupFilesDNS},
227 {"x.local", "myhostname", hostLookupCgo},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700228 },
229 },
230 {
231 name: "dns_files",
Alex A Skinnerf3901352015-04-25 20:50:21 -0400232 c: &conf{
233 nss: nssStr("hosts: dns files"),
234 resolv: defaultResolvConf,
235 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700236 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700237 {"x.com", "myhostname", hostLookupDNSFiles},
238 {"x", "myhostname", hostLookupDNSFiles},
239 {"x.local", "myhostname", hostLookupCgo},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700240 },
241 },
242 {
243 name: "something_custom",
Alex A Skinnerf3901352015-04-25 20:50:21 -0400244 c: &conf{
245 nss: nssStr("hosts: dns files something_custom"),
246 resolv: defaultResolvConf,
247 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700248 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700249 {"x.com", "myhostname", hostLookupCgo},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700250 },
251 },
252 {
253 name: "myhostname",
Alex A Skinnerf3901352015-04-25 20:50:21 -0400254 c: &conf{
255 nss: nssStr("hosts: files dns myhostname"),
256 resolv: defaultResolvConf,
257 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700258 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700259 {"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
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700276 },
277 },
278 {
279 name: "ubuntu14.04.02",
Alex A Skinnerf3901352015-04-25 20:50:21 -0400280 c: &conf{
281 nss: nssStr("hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4"),
282 resolv: defaultResolvConf,
283 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700284 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700285 {"x.com", "myhostname", hostLookupFilesDNS},
286 {"somehostname", "myhostname", hostLookupFilesDNS},
287 {"myhostname", "myhostname", hostLookupCgo},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700288 },
289 },
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
293 // files.
294 {
295 name: "debian_squeeze",
Alex A Skinnerf3901352015-04-25 20:50:21 -0400296 c: &conf{
297 nss: nssStr("hosts: dns [success=return notfound=continue unavail=continue tryagain=continue] files [notfound=return]"),
298 resolv: defaultResolvConf,
299 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700300 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700301 {"x.com", "myhostname", hostLookupDNSFiles},
302 {"somehostname", "myhostname", hostLookupDNSFiles},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700303 },
304 },
305 {
306 name: "resolv.conf-unknown",
307 c: &conf{
308 nss: nssStr("foo: bar"),
Alex A Skinnerf3901352015-04-25 20:50:21 -0400309 resolv: &dnsConfig{servers: defaultNS, ndots: 1, timeout: 5, attempts: 2, unknownOpt: true},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700310 },
Dan Peterson4bd95702016-11-23 11:35:17 -0700311 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700312 },
Brad Fitzpatrick7165c9b2015-05-06 09:32:11 -0700313 // Android should always use cgo.
314 {
315 name: "android",
316 c: &conf{
317 goos: "android",
318 nss: nssStr(""),
319 resolv: defaultResolvConf,
320 },
321 hostTests: []nssHostTest{
Dan Peterson4bd95702016-11-23 11:35:17 -0700322 {"x.com", "myhostname", hostLookupCgo},
Brad Fitzpatrick7165c9b2015-05-06 09:32:11 -0700323 },
324 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700325 }
Dan Peterson4bd95702016-11-23 11:35:17 -0700326
327 origGetHostname := getHostname
328 defer func() { getHostname = origGetHostname }()
329
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700330 for _, tt := range tests {
331 for _, ht := range tt.hostTests {
Dan Peterson4bd95702016-11-23 11:35:17 -0700332 getHostname = func() (string, error) { return ht.localhost, nil }
333
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700334 gotOrder := tt.c.hostLookupOrder(ht.host)
335 if gotOrder != ht.want {
Mikio Haraf77e10f2015-05-01 12:38:42 +0900336 t.Errorf("%s: hostLookupOrder(%q) = %v; want %v", tt.name, ht.host, gotOrder, ht.want)
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700337 }
338 }
339 }
340
341}
Brad Fitzpatrickb615ad82015-06-25 12:52:54 +0200342
343func TestSystemConf(t *testing.T) {
344 systemConf()
345}