net: make go DNS use localhost if resolv.conf is missing or empty

Per resolv.conf man page, "If this file does not exist, only the name
server on the local machine will be queried."

This behavior also occurs if file is present but unreadable,
or if no nameservers are listed.

Fixes #10566

Change-Id: Id5716da0eae534d5ebfafea111bbc657f302e307
Reviewed-on: https://go-review.googlesource.com/9380
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Alex A Skinner 2015-04-25 20:50:21 -04:00 committed by Brad Fitzpatrick
parent 7556948ebf
commit f390135733
8 changed files with 134 additions and 75 deletions

View file

@ -7,6 +7,7 @@
package net
import (
"os"
"reflect"
"testing"
)
@ -50,6 +51,7 @@ var dnsReadConfigTests = []struct {
{
name: "testdata/empty-resolv.conf",
want: &dnsConfig{
servers: defaultNS,
ndots: 1,
timeout: 5,
attempts: 2,
@ -70,12 +72,29 @@ var dnsReadConfigTests = []struct {
func TestDNSReadConfig(t *testing.T) {
for _, tt := range dnsReadConfigTests {
conf, err := dnsReadConfig(tt.name)
if err != nil {
t.Fatal(err)
conf := dnsReadConfig(tt.name)
if conf.err != nil {
t.Fatal(conf.err)
}
if !reflect.DeepEqual(conf, tt.want) {
t.Errorf("%s:\n got: %+v\nwant: %+v", tt.name, conf, tt.want)
}
}
}
func TestDNSReadMissingFile(t *testing.T) {
conf := dnsReadConfig("a-nonexistent-file")
if !os.IsNotExist(conf.err) {
t.Errorf("Missing resolv.conf:\n got: %v\nwant: %v", conf.err, os.ErrNotExist)
}
conf.err = nil
want := &dnsConfig{
servers: defaultNS,
ndots: 1,
timeout: 5,
attempts: 2,
}
if !reflect.DeepEqual(conf, want) {
t.Errorf("Missing resolv.conf:\n got: %+v\nwant: %+v", conf, want)
}
}