...

Source file src/net/conf_test.go

Documentation: net

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

View as plain text