Update ChangeLog and version files for release
[official-gcc.git] / libgo / go / net / conf_test.go
blob86904bffde7a7e2a2dc06c67ac252854535e3c9c
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
7 package net
9 import (
10 "os"
11 "strings"
12 "testing"
15 type nssHostTest struct {
16 host string
17 want hostLookupOrder
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{
24 servers: defaultNS,
25 ndots: 1,
26 timeout: 5,
27 attempts: 2,
28 err: os.ErrNotExist,
31 func TestConfHostLookupOrder(t *testing.T) {
32 tests := []struct {
33 name string
34 c *conf
35 goos string
36 hostTests []nssHostTest
39 name: "force",
40 c: &conf{
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",
52 c: &conf{
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",
66 c: &conf{
67 goos: "freebsd",
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",
76 c: &conf{
77 goos: "openbsd",
78 resolv: defaultResolvConf,
80 hostTests: []nssHostTest{{"google.com", hostLookupFiles}},
83 name: "solaris_no_nsswitch",
84 c: &conf{
85 goos: "solaris",
86 nss: &nssConf{err: os.ErrNotExist},
87 resolv: defaultResolvConf,
89 hostTests: []nssHostTest{{"google.com", hostLookupCgo}},
92 name: "openbsd_lookup_bind_file",
93 c: &conf{
94 goos: "openbsd",
95 resolv: &dnsConfig{lookup: []string{"bind", "file"}},
97 hostTests: []nssHostTest{
98 {"google.com", hostLookupDNSFiles},
99 {"foo.local", hostLookupDNSFiles},
103 name: "openbsd_lookup_file_bind",
104 c: &conf{
105 goos: "openbsd",
106 resolv: &dnsConfig{lookup: []string{"file", "bind"}},
108 hostTests: []nssHostTest{{"google.com", hostLookupFilesDNS}},
111 name: "openbsd_lookup_bind",
112 c: &conf{
113 goos: "openbsd",
114 resolv: &dnsConfig{lookup: []string{"bind"}},
116 hostTests: []nssHostTest{{"google.com", hostLookupDNS}},
119 name: "openbsd_lookup_file",
120 c: &conf{
121 goos: "openbsd",
122 resolv: &dnsConfig{lookup: []string{"file"}},
124 hostTests: []nssHostTest{{"google.com", hostLookupFiles}},
127 name: "openbsd_lookup_yp",
128 c: &conf{
129 goos: "openbsd",
130 resolv: &dnsConfig{lookup: []string{"file", "bind", "yp"}},
132 hostTests: []nssHostTest{{"google.com", hostLookupCgo}},
135 name: "openbsd_lookup_two",
136 c: &conf{
137 goos: "openbsd",
138 resolv: &dnsConfig{lookup: []string{"file", "foo"}},
140 hostTests: []nssHostTest{{"google.com", hostLookupCgo}},
143 name: "openbsd_lookup_empty",
144 c: &conf{
145 goos: "openbsd",
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",
154 c: &conf{
155 goos: "linux",
156 nss: &nssConf{err: os.ErrNotExist},
157 resolv: defaultResolvConf,
159 hostTests: []nssHostTest{{"google.com", hostLookupDNSFiles}},
162 name: "files_mdns_dns",
163 c: &conf{
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",
174 c: &conf{
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
185 name: "mdns_allow",
186 c: &conf{
187 nss: nssStr("hosts: files mdns dns"),
188 resolv: defaultResolvConf,
189 hasMDNSAllow: true,
191 hostTests: []nssHostTest{
192 {"x.com", hostLookupCgo},
193 {"x.local", hostLookupCgo},
197 name: "files_dns",
198 c: &conf{
199 nss: nssStr("hosts: files dns"),
200 resolv: defaultResolvConf,
202 hostTests: []nssHostTest{
203 {"x.com", hostLookupFilesDNS},
204 {"x", hostLookupFilesDNS},
205 {"x.local", hostLookupCgo},
209 name: "dns_files",
210 c: &conf{
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",
222 c: &conf{
223 nss: nssStr("hosts: dns files something_custom"),
224 resolv: defaultResolvConf,
226 hostTests: []nssHostTest{
227 {"x.com", hostLookupCgo},
231 name: "myhostname",
232 c: &conf{
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",
243 c: &conf{
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
255 // files.
257 name: "debian_squeeze",
258 c: &conf{
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",
269 c: &conf{
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.
277 name: "android",
278 c: &conf{
279 goos: "android",
280 nss: nssStr(""),
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) {
300 systemConf()