github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/net/conf_test.go (about) 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. 4 5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris 6 7 package net 8 9 import ( 10 "os" 11 "strings" 12 "testing" 13 ) 14 15 type nssHostTest struct { 16 host string 17 want hostLookupOrder 18 } 19 20 func nssStr(s string) *nssConf { return parseNSSConf(strings.NewReader(s)) } 21 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, 29 } 30 31 func TestConfHostLookupOrder(t *testing.T) { 32 tests := []struct { 33 name string 34 c *conf 35 hostTests []nssHostTest 36 }{ 37 { 38 name: "force", 39 c: &conf{ 40 forceCgoLookupHost: true, 41 nss: nssStr("foo: bar"), 42 resolv: defaultResolvConf, 43 }, 44 hostTests: []nssHostTest{ 45 {"foo.local", hostLookupCgo}, 46 {"google.com", hostLookupCgo}, 47 }, 48 }, 49 { 50 name: "netgo_dns_before_files", 51 c: &conf{ 52 netGo: true, 53 nss: nssStr("hosts: dns files"), 54 resolv: defaultResolvConf, 55 }, 56 hostTests: []nssHostTest{ 57 {"x.com", hostLookupDNSFiles}, 58 }, 59 }, 60 { 61 name: "netgo_fallback_on_cgo", 62 c: &conf{ 63 netGo: true, 64 nss: nssStr("hosts: dns files something_custom"), 65 resolv: defaultResolvConf, 66 }, 67 hostTests: []nssHostTest{ 68 {"x.com", hostLookupFilesDNS}, 69 }, 70 }, 71 { 72 name: "ubuntu_trusty_avahi", 73 c: &conf{ 74 nss: nssStr("hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4"), 75 resolv: defaultResolvConf, 76 }, 77 hostTests: []nssHostTest{ 78 {"foo.local", hostLookupCgo}, 79 {"foo.local.", hostLookupCgo}, 80 {"foo.LOCAL", hostLookupCgo}, 81 {"foo.LOCAL.", hostLookupCgo}, 82 {"google.com", hostLookupFilesDNS}, 83 }, 84 }, 85 { 86 name: "freebsdlinux_no_resolv_conf", 87 c: &conf{ 88 goos: "freebsd", 89 nss: nssStr("foo: bar"), 90 resolv: defaultResolvConf, 91 }, 92 hostTests: []nssHostTest{{"google.com", hostLookupFilesDNS}}, 93 }, 94 // On OpenBSD, no resolv.conf means no DNS. 95 { 96 name: "openbsd_no_resolv_conf", 97 c: &conf{ 98 goos: "openbsd", 99 resolv: defaultResolvConf, 100 }, 101 hostTests: []nssHostTest{{"google.com", hostLookupFiles}}, 102 }, 103 { 104 name: "solaris_no_nsswitch", 105 c: &conf{ 106 goos: "solaris", 107 nss: &nssConf{err: os.ErrNotExist}, 108 resolv: defaultResolvConf, 109 }, 110 hostTests: []nssHostTest{{"google.com", hostLookupCgo}}, 111 }, 112 { 113 name: "openbsd_lookup_bind_file", 114 c: &conf{ 115 goos: "openbsd", 116 resolv: &dnsConfig{lookup: []string{"bind", "file"}}, 117 }, 118 hostTests: []nssHostTest{ 119 {"google.com", hostLookupDNSFiles}, 120 {"foo.local", hostLookupDNSFiles}, 121 }, 122 }, 123 { 124 name: "openbsd_lookup_file_bind", 125 c: &conf{ 126 goos: "openbsd", 127 resolv: &dnsConfig{lookup: []string{"file", "bind"}}, 128 }, 129 hostTests: []nssHostTest{{"google.com", hostLookupFilesDNS}}, 130 }, 131 { 132 name: "openbsd_lookup_bind", 133 c: &conf{ 134 goos: "openbsd", 135 resolv: &dnsConfig{lookup: []string{"bind"}}, 136 }, 137 hostTests: []nssHostTest{{"google.com", hostLookupDNS}}, 138 }, 139 { 140 name: "openbsd_lookup_file", 141 c: &conf{ 142 goos: "openbsd", 143 resolv: &dnsConfig{lookup: []string{"file"}}, 144 }, 145 hostTests: []nssHostTest{{"google.com", hostLookupFiles}}, 146 }, 147 { 148 name: "openbsd_lookup_yp", 149 c: &conf{ 150 goos: "openbsd", 151 resolv: &dnsConfig{lookup: []string{"file", "bind", "yp"}}, 152 }, 153 hostTests: []nssHostTest{{"google.com", hostLookupCgo}}, 154 }, 155 { 156 name: "openbsd_lookup_two", 157 c: &conf{ 158 goos: "openbsd", 159 resolv: &dnsConfig{lookup: []string{"file", "foo"}}, 160 }, 161 hostTests: []nssHostTest{{"google.com", hostLookupCgo}}, 162 }, 163 { 164 name: "openbsd_lookup_empty", 165 c: &conf{ 166 goos: "openbsd", 167 resolv: &dnsConfig{lookup: nil}, 168 }, 169 hostTests: []nssHostTest{{"google.com", hostLookupDNSFiles}}, 170 }, 171 // glibc lacking an nsswitch.conf, per 172 // http://www.gnu.org/software/libc/manual/html_node/Notes-on-NSS-Configuration-File.html 173 { 174 name: "linux_no_nsswitch.conf", 175 c: &conf{ 176 goos: "linux", 177 nss: &nssConf{err: os.ErrNotExist}, 178 resolv: defaultResolvConf, 179 }, 180 hostTests: []nssHostTest{{"google.com", hostLookupDNSFiles}}, 181 }, 182 { 183 name: "files_mdns_dns", 184 c: &conf{ 185 nss: nssStr("hosts: files mdns dns"), 186 resolv: defaultResolvConf, 187 }, 188 hostTests: []nssHostTest{ 189 {"x.com", hostLookupFilesDNS}, 190 {"x.local", hostLookupCgo}, 191 }, 192 }, 193 { 194 name: "dns_special_hostnames", 195 c: &conf{ 196 nss: nssStr("hosts: dns"), 197 resolv: defaultResolvConf, 198 }, 199 hostTests: []nssHostTest{ 200 {"x.com", hostLookupDNS}, 201 {"x\\.com", hostLookupCgo}, // punt on weird glibc escape 202 {"foo.com%en0", hostLookupCgo}, // and IPv6 zones 203 }, 204 }, 205 { 206 name: "mdns_allow", 207 c: &conf{ 208 nss: nssStr("hosts: files mdns dns"), 209 resolv: defaultResolvConf, 210 hasMDNSAllow: true, 211 }, 212 hostTests: []nssHostTest{ 213 {"x.com", hostLookupCgo}, 214 {"x.local", hostLookupCgo}, 215 }, 216 }, 217 { 218 name: "files_dns", 219 c: &conf{ 220 nss: nssStr("hosts: files dns"), 221 resolv: defaultResolvConf, 222 }, 223 hostTests: []nssHostTest{ 224 {"x.com", hostLookupFilesDNS}, 225 {"x", hostLookupFilesDNS}, 226 {"x.local", hostLookupCgo}, 227 }, 228 }, 229 { 230 name: "dns_files", 231 c: &conf{ 232 nss: nssStr("hosts: dns files"), 233 resolv: defaultResolvConf, 234 }, 235 hostTests: []nssHostTest{ 236 {"x.com", hostLookupDNSFiles}, 237 {"x", hostLookupDNSFiles}, 238 {"x.local", hostLookupCgo}, 239 }, 240 }, 241 { 242 name: "something_custom", 243 c: &conf{ 244 nss: nssStr("hosts: dns files something_custom"), 245 resolv: defaultResolvConf, 246 }, 247 hostTests: []nssHostTest{ 248 {"x.com", hostLookupCgo}, 249 }, 250 }, 251 { 252 name: "myhostname", 253 c: &conf{ 254 nss: nssStr("hosts: files dns myhostname"), 255 resolv: defaultResolvConf, 256 }, 257 hostTests: []nssHostTest{ 258 {"x.com", hostLookupFilesDNS}, 259 {"somehostname", hostLookupCgo}, 260 {"", hostLookupFilesDNS}, // Issue 13623 261 }, 262 }, 263 { 264 name: "ubuntu14.04.02", 265 c: &conf{ 266 nss: nssStr("hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4"), 267 resolv: defaultResolvConf, 268 }, 269 hostTests: []nssHostTest{ 270 {"x.com", hostLookupFilesDNS}, 271 {"somehostname", hostLookupCgo}, 272 }, 273 }, 274 // Debian Squeeze is just "dns,files", but lists all 275 // the default criteria for dns, but then has a 276 // non-standard but redundant notfound=return for the 277 // files. 278 { 279 name: "debian_squeeze", 280 c: &conf{ 281 nss: nssStr("hosts: dns [success=return notfound=continue unavail=continue tryagain=continue] files [notfound=return]"), 282 resolv: defaultResolvConf, 283 }, 284 hostTests: []nssHostTest{ 285 {"x.com", hostLookupDNSFiles}, 286 {"somehostname", hostLookupDNSFiles}, 287 }, 288 }, 289 { 290 name: "resolv.conf-unknown", 291 c: &conf{ 292 nss: nssStr("foo: bar"), 293 resolv: &dnsConfig{servers: defaultNS, ndots: 1, timeout: 5, attempts: 2, unknownOpt: true}, 294 }, 295 hostTests: []nssHostTest{{"google.com", hostLookupCgo}}, 296 }, 297 // Android should always use cgo. 298 { 299 name: "android", 300 c: &conf{ 301 goos: "android", 302 nss: nssStr(""), 303 resolv: defaultResolvConf, 304 }, 305 hostTests: []nssHostTest{ 306 {"x.com", hostLookupCgo}, 307 }, 308 }, 309 } 310 for _, tt := range tests { 311 for _, ht := range tt.hostTests { 312 gotOrder := tt.c.hostLookupOrder(ht.host) 313 if gotOrder != ht.want { 314 t.Errorf("%s: hostLookupOrder(%q) = %v; want %v", tt.name, ht.host, gotOrder, ht.want) 315 } 316 } 317 } 318 319 } 320 321 func TestSystemConf(t *testing.T) { 322 systemConf() 323 }