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.
5 // +build aix darwin dragonfly freebsd hurd linux netbsd openbsd solaris
18 var dnsReadConfigTests
= []struct {
23 name
: "testdata/resolv.conf",
25 servers
: []string{"8.8.8.8:53", "[2001:4860:4860::8888]:53", "[fe80::1%lo0]:53"},
26 search
: []string{"localdomain."},
28 timeout
: 10 * time
.Second
,
31 unknownOpt
: true, // the "options attempts 3" line
35 name
: "testdata/domain-resolv.conf",
37 servers
: []string{"8.8.8.8:53"},
38 search
: []string{"localdomain."},
40 timeout
: 5 * time
.Second
,
45 name
: "testdata/search-resolv.conf",
47 servers
: []string{"8.8.8.8:53"},
48 search
: []string{"test.", "invalid."},
50 timeout
: 5 * time
.Second
,
55 name
: "testdata/empty-resolv.conf",
59 timeout
: 5 * time
.Second
,
61 search
: []string{"domain.local."},
65 name
: "testdata/invalid-ndots-resolv.conf",
69 timeout
: 5 * time
.Second
,
71 search
: []string{"domain.local."},
75 name
: "testdata/large-ndots-resolv.conf",
79 timeout
: 5 * time
.Second
,
81 search
: []string{"domain.local."},
85 name
: "testdata/negative-ndots-resolv.conf",
89 timeout
: 5 * time
.Second
,
91 search
: []string{"domain.local."},
95 name
: "testdata/openbsd-resolv.conf",
98 timeout
: 5 * time
.Second
,
100 lookup
: []string{"file", "bind"},
101 servers
: []string{"169.254.169.254:53", "10.240.0.1:53"},
102 search
: []string{"c.symbolic-datum-552.internal."},
106 name
: "testdata/single-request-resolv.conf",
111 timeout
: 5 * time
.Second
,
113 search
: []string{"domain.local."},
117 name
: "testdata/single-request-reopen-resolv.conf",
122 timeout
: 5 * time
.Second
,
124 search
: []string{"domain.local."},
128 name
: "testdata/linux-use-vc-resolv.conf",
133 timeout
: 5 * time
.Second
,
135 search
: []string{"domain.local."},
139 name
: "testdata/freebsd-usevc-resolv.conf",
144 timeout
: 5 * time
.Second
,
146 search
: []string{"domain.local."},
150 name
: "testdata/openbsd-tcp-resolv.conf",
155 timeout
: 5 * time
.Second
,
157 search
: []string{"domain.local."},
162 func TestDNSReadConfig(t
*testing
.T
) {
163 origGetHostname
:= getHostname
164 defer func() { getHostname
= origGetHostname
}()
165 getHostname
= func() (string, error
) { return "host.domain.local", nil }
167 for _
, tt
:= range dnsReadConfigTests
{
168 conf
:= dnsReadConfig(tt
.name
)
172 conf
.mtime
= time
.Time
{}
173 if !reflect
.DeepEqual(conf
, tt
.want
) {
174 t
.Errorf("%s:\ngot: %+v\nwant: %+v", tt
.name
, conf
, tt
.want
)
179 func TestDNSReadMissingFile(t
*testing
.T
) {
180 origGetHostname
:= getHostname
181 defer func() { getHostname
= origGetHostname
}()
182 getHostname
= func() (string, error
) { return "host.domain.local", nil }
184 conf
:= dnsReadConfig("a-nonexistent-file")
185 if !os
.IsNotExist(conf
.err
) {
186 t
.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf
.err
, os
.ErrNotExist
)
192 timeout
: 5 * time
.Second
,
194 search
: []string{"domain.local."},
196 if !reflect
.DeepEqual(conf
, want
) {
197 t
.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf
, want
)
201 var dnsDefaultSearchTests
= []struct {
207 name
: "host.long.domain.local",
208 want
: []string{"long.domain.local."},
212 want
: []string{"local."},
219 name
: "host.domain.local",
220 err
: errors
.New("errored"),
224 // ensures we don't return []string{""}
225 // which causes duplicate lookups
231 func TestDNSDefaultSearch(t
*testing
.T
) {
232 origGetHostname
:= getHostname
233 defer func() { getHostname
= origGetHostname
}()
235 for _
, tt
:= range dnsDefaultSearchTests
{
236 getHostname
= func() (string, error
) { return tt
.name
, tt
.err
}
237 got
:= dnsDefaultSearch()
238 if !reflect
.DeepEqual(got
, tt
.want
) {
239 t
.Errorf("dnsDefaultSearch with hostname %q and error %+v = %q, wanted %q", tt
.name
, tt
.err
, got
, tt
.want
)
244 func TestDNSNameLength(t
*testing
.T
) {
245 origGetHostname
:= getHostname
246 defer func() { getHostname
= origGetHostname
}()
247 getHostname
= func() (string, error
) { return "host.domain.local", nil }
250 for i
:= 0; i
< 63; i
++ {
253 longDomain
:= strings
.Repeat(char63
+".", 5) + "example"
255 for _
, tt
:= range dnsReadConfigTests
{
256 conf
:= dnsReadConfig(tt
.name
)
261 var shortestSuffix
int
262 for _
, suffix
:= range tt
.want
.search
{
263 if shortestSuffix
== 0 ||
len(suffix
) < shortestSuffix
{
264 shortestSuffix
= len(suffix
)
268 // Test a name that will be maximally long when prefixing the shortest
269 // suffix (accounting for the intervening dot).
270 longName
:= longDomain
[len(longDomain
)-254+1+shortestSuffix
:]
271 if longName
[0] == '.' || longName
[1] == '.' {
272 longName
= "aa." + longName
[3:]
274 for _
, fqdn
:= range conf
.nameList(longName
) {
276 t
.Errorf("got %d; want less than or equal to 254", len(fqdn
))
280 // Now test a name that's too long for suffixing.
281 unsuffixable
:= "a." + longName
[1:]
282 unsuffixableResults
:= conf
.nameList(unsuffixable
)
283 if len(unsuffixableResults
) != 1 {
284 t
.Errorf("suffixed names %v; want []", unsuffixableResults
[1:])
287 // Now test a name that's too long for DNS.
288 tooLong
:= "a." + longDomain
289 tooLongResults
:= conf
.nameList(tooLong
)
290 if tooLongResults
!= nil {
291 t
.Errorf("suffixed names %v; want nil", tooLongResults
)