package net
-import "time"
+import (
+ "os"
+ "time"
+)
-var defaultNS = []string{"127.0.0.1", "::1"}
+var (
+ defaultNS = []string{"127.0.0.1", "::1"}
+ getHostname = os.Hostname // variable for testing
+)
type dnsConfig struct {
servers []string // servers to use
}
// See resolv.conf(5) on a Linux machine.
-// TODO(rsc): Supposed to call uname() and chop the beginning
-// of the host name to get the default search domain.
func dnsReadConfig(filename string) *dnsConfig {
conf := &dnsConfig{
ndots: 1,
file, err := open(filename)
if err != nil {
conf.servers = defaultNS
+ conf.search = dnsDefaultSearch()
conf.err = err
return conf
}
conf.mtime = fi.ModTime()
} else {
conf.servers = defaultNS
+ conf.search = dnsDefaultSearch()
conf.err = err
return conf
}
if len(conf.servers) == 0 {
conf.servers = defaultNS
}
+ if len(conf.search) == 0 {
+ conf.search = dnsDefaultSearch()
+ }
return conf
}
+func dnsDefaultSearch() []string {
+ hn, err := getHostname()
+ if err != nil {
+ // best effort
+ return nil
+ }
+ if i := byteIndex(hn, '.'); i >= 0 && i < len(hn)-1 {
+ return []string{hn[i+1:]}
+ }
+ return nil
+}
+
func hasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}
package net
import (
+ "errors"
"os"
"reflect"
"testing"
ndots: 1,
timeout: 5,
attempts: 2,
+ search: []string{"domain.local"},
},
},
{
}
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 {
}
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)
ndots: 1,
timeout: 5,
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)
+ }
+ }
+}