Fix "PR c++/92804 ICE trying to use concept as a nested-name-specifier"
[official-gcc.git] / libgo / go / net / conf_test.go
blob081a274ccbbfbabcb78f8a56f3e2a4d289bfa9cf
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
7 package net
9 import (
10 "os"
11 "strings"
12 "testing"
15 type nssHostTest struct {
16 host string
17 localhost string
18 want hostLookupOrder
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{
25 servers: defaultNS,
26 ndots: 1,
27 timeout: 5,
28 attempts: 2,
29 err: os.ErrNotExist,
32 func TestConfHostLookupOrder(t *testing.T) {
33 tests := []struct {
34 name string
35 c *conf
36 resolver *Resolver
37 hostTests []nssHostTest
40 name: "force",
41 c: &conf{
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",
53 c: &conf{
54 netGo: true,
55 nss: nssStr("hosts: dns files"),
56 resolv: defaultResolvConf,
58 hostTests: []nssHostTest{
59 {"x.com", "myhostname", hostLookupDNSFiles},
63 name: "netgo_fallback_on_cgo",
64 c: &conf{
65 netGo: true,
66 nss: nssStr("hosts: dns files something_custom"),
67 resolv: defaultResolvConf,
69 hostTests: []nssHostTest{
70 {"x.com", "myhostname", hostLookupFilesDNS},
74 name: "ubuntu_trusty_avahi",
75 c: &conf{
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",
89 c: &conf{
90 goos: "freebsd",
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",
99 c: &conf{
100 goos: "openbsd",
101 resolv: defaultResolvConf,
103 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFiles}},
106 name: "solaris_no_nsswitch",
107 c: &conf{
108 goos: "solaris",
109 nss: &nssConf{err: os.ErrNotExist},
110 resolv: defaultResolvConf,
112 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
115 name: "openbsd_lookup_bind_file",
116 c: &conf{
117 goos: "openbsd",
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",
127 c: &conf{
128 goos: "openbsd",
129 resolv: &dnsConfig{lookup: []string{"file", "bind"}},
131 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
134 name: "openbsd_lookup_bind",
135 c: &conf{
136 goos: "openbsd",
137 resolv: &dnsConfig{lookup: []string{"bind"}},
139 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNS}},
142 name: "openbsd_lookup_file",
143 c: &conf{
144 goos: "openbsd",
145 resolv: &dnsConfig{lookup: []string{"file"}},
147 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFiles}},
150 name: "openbsd_lookup_yp",
151 c: &conf{
152 goos: "openbsd",
153 resolv: &dnsConfig{lookup: []string{"file", "bind", "yp"}},
155 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
158 name: "openbsd_lookup_two",
159 c: &conf{
160 goos: "openbsd",
161 resolv: &dnsConfig{lookup: []string{"file", "foo"}},
163 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
166 name: "openbsd_lookup_empty",
167 c: &conf{
168 goos: "openbsd",
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",
177 c: &conf{
178 goos: "linux",
179 nss: &nssConf{err: os.ErrNotExist},
180 resolv: defaultResolvConf,
182 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNSFiles}},
185 name: "files_mdns_dns",
186 c: &conf{
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",
197 c: &conf{
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
208 name: "mdns_allow",
209 c: &conf{
210 nss: nssStr("hosts: files mdns dns"),
211 resolv: defaultResolvConf,
212 hasMDNSAllow: true,
214 hostTests: []nssHostTest{
215 {"x.com", "myhostname", hostLookupCgo},
216 {"x.local", "myhostname", hostLookupCgo},
220 name: "files_dns",
221 c: &conf{
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},
232 name: "dns_files",
233 c: &conf{
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",
245 c: &conf{
246 nss: nssStr("hosts: dns files something_custom"),
247 resolv: defaultResolvConf,
249 hostTests: []nssHostTest{
250 {"x.com", "myhostname", hostLookupCgo},
254 name: "myhostname",
255 c: &conf{
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",
281 c: &conf{
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
294 // files.
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,
301 hostTests: []nssHostTest{
302 {"x.com", "myhostname", hostLookupDNSFiles},
303 {"somehostname", "myhostname", hostLookupDNSFiles},
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},
312 hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupCgo}},
314 // Android should always use cgo.
316 name: "android",
317 c: &conf{
318 goos: "android",
319 nss: nssStr(""),
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},
330 c: &conf{
331 goos: "darwin",
332 forceCgoLookupHost: true, // always true for darwin
333 resolv: defaultResolvConf,
334 nss: nssStr(""),
335 netCgo: true,
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) {
360 systemConf()