1 // Copyright 2013 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.
19 var dnsReadConfigTests = []struct {
24 name: "testdata/resolv.conf",
26 servers: []string{"8.8.8.8:53", "[2001:4860:4860::8888]:53", "[fe80::1%lo0]:53"},
27 search: []string{"localdomain."},
29 timeout: 10 * time.Second,
32 unknownOpt: true, // the "options attempts 3" line
36 name: "testdata/domain-resolv.conf",
38 servers: []string{"8.8.8.8:53"},
39 search: []string{"localdomain."},
41 timeout: 5 * time.Second,
46 name: "testdata/search-resolv.conf",
48 servers: []string{"8.8.8.8:53"},
49 search: []string{"test.", "invalid."},
51 timeout: 5 * time.Second,
56 name: "testdata/search-single-dot-resolv.conf",
58 servers: []string{"8.8.8.8:53"},
61 timeout: 5 * time.Second,
66 name: "testdata/empty-resolv.conf",
70 timeout: 5 * time.Second,
72 search: []string{"domain.local."},
76 name: "testdata/invalid-ndots-resolv.conf",
80 timeout: 5 * time.Second,
82 search: []string{"domain.local."},
86 name: "testdata/large-ndots-resolv.conf",
90 timeout: 5 * time.Second,
92 search: []string{"domain.local."},
96 name: "testdata/negative-ndots-resolv.conf",
100 timeout: 5 * time.Second,
102 search: []string{"domain.local."},
106 name: "testdata/openbsd-resolv.conf",
109 timeout: 5 * time.Second,
111 lookup: []string{"file", "bind"},
112 servers: []string{"169.254.169.254:53", "10.240.0.1:53"},
113 search: []string{"c.symbolic-datum-552.internal."},
117 name: "testdata/single-request-resolv.conf",
122 timeout: 5 * time.Second,
124 search: []string{"domain.local."},
128 name: "testdata/single-request-reopen-resolv.conf",
133 timeout: 5 * time.Second,
135 search: []string{"domain.local."},
139 name: "testdata/linux-use-vc-resolv.conf",
144 timeout: 5 * time.Second,
146 search: []string{"domain.local."},
150 name: "testdata/freebsd-usevc-resolv.conf",
155 timeout: 5 * time.Second,
157 search: []string{"domain.local."},
161 name: "testdata/openbsd-tcp-resolv.conf",
166 timeout: 5 * time.Second,
168 search: []string{"domain.local."},
173 func TestDNSReadConfig(t *testing.T) {
174 origGetHostname := getHostname
175 defer func() { getHostname = origGetHostname }()
176 getHostname = func() (string, error) { return "host.domain.local", nil }
178 for _, tt := range dnsReadConfigTests {
180 if len(want.search) == 0 {
181 want.search = dnsDefaultSearch()
183 conf := dnsReadConfig(tt.name)
187 conf.mtime = time.Time{}
188 if !reflect.DeepEqual(conf, &want) {
189 t.Errorf("%s:\ngot: %+v\nwant: %+v", tt.name, conf, want)
194 func TestDNSReadMissingFile(t *testing.T) {
195 origGetHostname := getHostname
196 defer func() { getHostname = origGetHostname }()
197 getHostname = func() (string, error) { return "host.domain.local", nil }
199 conf := dnsReadConfig("a-nonexistent-file")
200 if !os.IsNotExist(conf.err) {
201 t.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf.err, fs.ErrNotExist)
207 timeout: 5 * time.Second,
209 search: []string{"domain.local."},
211 if !reflect.DeepEqual(conf, want) {
212 t.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf, want)
216 var dnsDefaultSearchTests = []struct {
222 name: "host.long.domain.local",
223 want: []string{"long.domain.local."},
227 want: []string{"local."},
234 name: "host.domain.local",
235 err: errors.New("errored"),
239 // ensures we don't return []string{""}
240 // which causes duplicate lookups
246 func TestDNSDefaultSearch(t *testing.T) {
247 origGetHostname := getHostname
248 defer func() { getHostname = origGetHostname }()
250 for _, tt := range dnsDefaultSearchTests {
251 getHostname = func() (string, error) { return tt.name, tt.err }
252 got := dnsDefaultSearch()
253 if !reflect.DeepEqual(got, tt.want) {
254 t.Errorf("dnsDefaultSearch with hostname %q and error %+v = %q, wanted %q", tt.name, tt.err, got, tt.want)
259 func TestDNSNameLength(t *testing.T) {
260 origGetHostname := getHostname
261 defer func() { getHostname = origGetHostname }()
262 getHostname = func() (string, error) { return "host.domain.local", nil }
265 for i := 0; i < 63; i++ {
268 longDomain := strings.Repeat(char63+".", 5) + "example"
270 for _, tt := range dnsReadConfigTests {
271 conf := dnsReadConfig(tt.name)
276 suffixList := tt.want.search
277 if len(suffixList) == 0 {
278 suffixList = dnsDefaultSearch()
281 var shortestSuffix int
282 for _, suffix := range suffixList {
283 if shortestSuffix == 0 || len(suffix) < shortestSuffix {
284 shortestSuffix = len(suffix)
288 // Test a name that will be maximally long when prefixing the shortest
289 // suffix (accounting for the intervening dot).
290 longName := longDomain[len(longDomain)-254+1+shortestSuffix:]
291 if longName[0] == '.' || longName[1] == '.' {
292 longName = "aa." + longName[3:]
294 for _, fqdn := range conf.nameList(longName) {
296 t.Errorf("got %d; want less than or equal to 254", len(fqdn))
300 // Now test a name that's too long for suffixing.
301 unsuffixable := "a." + longName[1:]
302 unsuffixableResults := conf.nameList(unsuffixable)
303 if len(unsuffixableResults) != 1 {
304 t.Errorf("suffixed names %v; want []", unsuffixableResults[1:])
307 // Now test a name that's too long for DNS.
308 tooLong := "a." + longDomain
309 tooLongResults := conf.nameList(tooLong)
310 if tooLongResults != nil {
311 t.Errorf("suffixed names %v; want nil", tooLongResults)