diff options
author | Benjamin Prosnitz <bprosnitz@google.com> | 2015-11-30 13:08:46 +0800 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2015-12-02 00:15:41 +0000 |
commit | e614d607598fccd9e2c77cef567d8b184b8e7ae4 (patch) | |
tree | 350bd0083262ee1b82d4cebcd3002b85e87620bc /src/net/dnsclient_unix_test.go | |
parent | e4e4942387018a528149f951da1a062d374d36da (diff) | |
download | go-e614d607598fccd9e2c77cef567d8b184b8e7ae4.tar.gz go-e614d607598fccd9e2c77cef567d8b184b8e7ae4.zip |
net: fall back to hosts file if DNS lookup fails, despite order
Fixes #13090
Change-Id: I5612d792dabdff89bd0cec57dc2cacf9be7ebf64
Reviewed-on: https://go-review.googlesource.com/16341
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/net/dnsclient_unix_test.go')
-rw-r--r-- | src/net/dnsclient_unix_test.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/net/dnsclient_unix_test.go b/src/net/dnsclient_unix_test.go index a999f8f060..a54f7b898d 100644 --- a/src/net/dnsclient_unix_test.go +++ b/src/net/dnsclient_unix_test.go @@ -378,6 +378,48 @@ func TestGoLookupIPWithResolverConfig(t *testing.T) { } } +// Test that goLookupIPOrder falls back to the host file when no DNS servers are available. +func TestGoLookupIPOrderFallbackToFile(t *testing.T) { + if testing.Short() || !*testExternal { + t.Skip("avoid external network") + } + + // Add a config that simulates no dns servers being available. + conf, err := newResolvConfTest() + if err != nil { + t.Fatal(err) + } + if err := conf.writeAndUpdate([]string{}); err != nil { + t.Fatal(err) + } + conf.tryUpdate(conf.path) + // Redirect host file lookups. + defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath) + testHookHostsPath = "testdata/hosts" + + for _, order := range []hostLookupOrder{hostLookupFilesDNS, hostLookupDNSFiles} { + name := fmt.Sprintf("order %v", order) + + // First ensure that we get an error when contacting a non-existant host. + _, err := goLookupIPOrder("notarealhost", order) + if err == nil { + t.Errorf("%s: expected error while looking up name not in hosts file", name) + continue + } + + // Now check that we get an address when the name appears in the hosts file. + addrs, err := goLookupIPOrder("thor", order) // entry is in "testdata/hosts" + if err != nil { + t.Errorf("%s: expected to successfully lookup host entry", name) + continue + } + if got, want := addrs, []IPAddr{IPAddr{IP: IP{127, 0, 0, 1}}}; !reflect.DeepEqual(got, want) { + t.Errorf("%s: address doesn't match expectation. got %v, want %v", name, got, want) + } + } + defer conf.teardown() +} + func BenchmarkGoLookupIP(b *testing.B) { testHookUninstaller.Do(uninstallTestHooks) |