diff options
Diffstat (limited to 'libgo/go/net/dnsconfig_unix_test.go')
-rw-r--r-- | libgo/go/net/dnsconfig_unix_test.go | 82 |
1 files changed, 69 insertions, 13 deletions
diff --git a/libgo/go/net/dnsconfig_unix_test.go b/libgo/go/net/dnsconfig_unix_test.go index c8eed61..9fd6dbf 100644 --- a/libgo/go/net/dnsconfig_unix_test.go +++ b/libgo/go/net/dnsconfig_unix_test.go @@ -7,9 +7,11 @@ package net import ( + "errors" "os" "reflect" "testing" + "time" ) var dnsReadConfigTests = []struct { @@ -19,10 +21,10 @@ var dnsReadConfigTests = []struct { { name: "testdata/resolv.conf", want: &dnsConfig{ - servers: []string{"8.8.8.8", "2001:4860:4860::8888", "fe80::1%lo0"}, - search: []string{"localdomain"}, + servers: []string{"8.8.8.8:53", "[2001:4860:4860::8888]:53", "[fe80::1%lo0]:53"}, + search: []string{"localdomain."}, ndots: 5, - timeout: 10, + timeout: 10 * time.Second, attempts: 3, rotate: true, unknownOpt: true, // the "options attempts 3" line @@ -31,20 +33,20 @@ var dnsReadConfigTests = []struct { { name: "testdata/domain-resolv.conf", want: &dnsConfig{ - servers: []string{"8.8.8.8"}, - search: []string{"localdomain"}, + servers: []string{"8.8.8.8:53"}, + search: []string{"localdomain."}, ndots: 1, - timeout: 5, + timeout: 5 * time.Second, attempts: 2, }, }, { name: "testdata/search-resolv.conf", want: &dnsConfig{ - servers: []string{"8.8.8.8"}, - search: []string{"test", "invalid"}, + servers: []string{"8.8.8.8:53"}, + search: []string{"test.", "invalid."}, ndots: 1, - timeout: 5, + timeout: 5 * time.Second, attempts: 2, }, }, @@ -53,29 +55,35 @@ var dnsReadConfigTests = []struct { want: &dnsConfig{ servers: defaultNS, ndots: 1, - timeout: 5, + timeout: 5 * time.Second, attempts: 2, + search: []string{"domain.local."}, }, }, { name: "testdata/openbsd-resolv.conf", want: &dnsConfig{ ndots: 1, - timeout: 5, + timeout: 5 * time.Second, attempts: 2, lookup: []string{"file", "bind"}, - servers: []string{"169.254.169.254", "10.240.0.1"}, + servers: []string{"169.254.169.254:53", "10.240.0.1:53"}, search: []string{"c.symbolic-datum-552.internal."}, }, }, } func TestDNSReadConfig(t *testing.T) { + origGetHostname := getHostname + defer func() { getHostname = origGetHostname }() + getHostname = func() (string, error) { return "host.domain.local", nil } + for _, tt := range dnsReadConfigTests { conf := dnsReadConfig(tt.name) if conf.err != nil { t.Fatal(conf.err) } + conf.mtime = time.Time{} if !reflect.DeepEqual(conf, tt.want) { t.Errorf("%s:\ngot: %+v\nwant: %+v", tt.name, conf, tt.want) } @@ -83,6 +91,10 @@ func TestDNSReadConfig(t *testing.T) { } func TestDNSReadMissingFile(t *testing.T) { + origGetHostname := getHostname + defer func() { getHostname = origGetHostname }() + getHostname = func() (string, error) { return "host.domain.local", nil } + conf := dnsReadConfig("a-nonexistent-file") if !os.IsNotExist(conf.err) { t.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf.err, os.ErrNotExist) @@ -91,10 +103,54 @@ func TestDNSReadMissingFile(t *testing.T) { want := &dnsConfig{ servers: defaultNS, ndots: 1, - timeout: 5, + timeout: 5 * time.Second, attempts: 2, + search: []string{"domain.local."}, } if !reflect.DeepEqual(conf, want) { t.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf, want) } } + +var dnsDefaultSearchTests = []struct { + name string + err error + want []string +}{ + { + name: "host.long.domain.local", + want: []string{"long.domain.local."}, + }, + { + name: "host.local", + want: []string{"local."}, + }, + { + name: "host", + want: nil, + }, + { + name: "host.domain.local", + err: errors.New("errored"), + want: nil, + }, + { + // ensures we don't return []string{""} + // which causes duplicate lookups + name: "foo.", + want: nil, + }, +} + +func TestDNSDefaultSearch(t *testing.T) { + origGetHostname := getHostname + defer func() { getHostname = origGetHostname }() + + for _, tt := range dnsDefaultSearchTests { + getHostname = func() (string, error) { return tt.name, tt.err } + got := dnsDefaultSearch() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("dnsDefaultSearch with hostname %q and error %+v = %q, wanted %q", tt.name, tt.err, got, tt.want) + } + } +} |