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 //go:build aix || darwin || dragonfly || freebsd || hurd || linux || netbsd || openbsd || solaris
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/empty-resolv.conf",
60 timeout
: 5 * time
.Second
,
62 search
: []string{"domain.local."},
66 name
: "testdata/invalid-ndots-resolv.conf",
70 timeout
: 5 * time
.Second
,
72 search
: []string{"domain.local."},
76 name
: "testdata/large-ndots-resolv.conf",
80 timeout
: 5 * time
.Second
,
82 search
: []string{"domain.local."},
86 name
: "testdata/negative-ndots-resolv.conf",
90 timeout
: 5 * time
.Second
,
92 search
: []string{"domain.local."},
96 name
: "testdata/openbsd-resolv.conf",
99 timeout
: 5 * time
.Second
,
101 lookup
: []string{"file", "bind"},
102 servers
: []string{"169.254.169.254:53", "10.240.0.1:53"},
103 search
: []string{"c.symbolic-datum-552.internal."},
107 name
: "testdata/single-request-resolv.conf",
112 timeout
: 5 * time
.Second
,
114 search
: []string{"domain.local."},
118 name
: "testdata/single-request-reopen-resolv.conf",
123 timeout
: 5 * time
.Second
,
125 search
: []string{"domain.local."},
129 name
: "testdata/linux-use-vc-resolv.conf",
134 timeout
: 5 * time
.Second
,
136 search
: []string{"domain.local."},
140 name
: "testdata/freebsd-usevc-resolv.conf",
145 timeout
: 5 * time
.Second
,
147 search
: []string{"domain.local."},
151 name
: "testdata/openbsd-tcp-resolv.conf",
156 timeout
: 5 * time
.Second
,
158 search
: []string{"domain.local."},
163 func TestDNSReadConfig(t
*testing
.T
) {
164 origGetHostname
:= getHostname
165 defer func() { getHostname
= origGetHostname
}()
166 getHostname
= func() (string, error
) { return "host.domain.local", nil }
168 for _
, tt
:= range dnsReadConfigTests
{
169 conf
:= dnsReadConfig(tt
.name
)
173 conf
.mtime
= time
.Time
{}
174 if !reflect
.DeepEqual(conf
, tt
.want
) {
175 t
.Errorf("%s:\ngot: %+v\nwant: %+v", tt
.name
, conf
, tt
.want
)
180 func TestDNSReadMissingFile(t
*testing
.T
) {
181 origGetHostname
:= getHostname
182 defer func() { getHostname
= origGetHostname
}()
183 getHostname
= func() (string, error
) { return "host.domain.local", nil }
185 conf
:= dnsReadConfig("a-nonexistent-file")
186 if !os
.IsNotExist(conf
.err
) {
187 t
.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf
.err
, fs
.ErrNotExist
)
193 timeout
: 5 * time
.Second
,
195 search
: []string{"domain.local."},
197 if !reflect
.DeepEqual(conf
, want
) {
198 t
.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf
, want
)
202 var dnsDefaultSearchTests
= []struct {
208 name
: "host.long.domain.local",
209 want
: []string{"long.domain.local."},
213 want
: []string{"local."},
220 name
: "host.domain.local",
221 err
: errors
.New("errored"),
225 // ensures we don't return []string{""}
226 // which causes duplicate lookups
232 func TestDNSDefaultSearch(t
*testing
.T
) {
233 origGetHostname
:= getHostname
234 defer func() { getHostname
= origGetHostname
}()
236 for _
, tt
:= range dnsDefaultSearchTests
{
237 getHostname
= func() (string, error
) { return tt
.name
, tt
.err
}
238 got
:= dnsDefaultSearch()
239 if !reflect
.DeepEqual(got
, tt
.want
) {
240 t
.Errorf("dnsDefaultSearch with hostname %q and error %+v = %q, wanted %q", tt
.name
, tt
.err
, got
, tt
.want
)
245 func TestDNSNameLength(t
*testing
.T
) {
246 origGetHostname
:= getHostname
247 defer func() { getHostname
= origGetHostname
}()
248 getHostname
= func() (string, error
) { return "host.domain.local", nil }
251 for i
:= 0; i
< 63; i
++ {
254 longDomain
:= strings
.Repeat(char63
+".", 5) + "example"
256 for _
, tt
:= range dnsReadConfigTests
{
257 conf
:= dnsReadConfig(tt
.name
)
262 var shortestSuffix
int
263 for _
, suffix
:= range tt
.want
.search
{
264 if shortestSuffix
== 0 ||
len(suffix
) < shortestSuffix
{
265 shortestSuffix
= len(suffix
)
269 // Test a name that will be maximally long when prefixing the shortest
270 // suffix (accounting for the intervening dot).
271 longName
:= longDomain
[len(longDomain
)-254+1+shortestSuffix
:]
272 if longName
[0] == '.' || longName
[1] == '.' {
273 longName
= "aa." + longName
[3:]
275 for _
, fqdn
:= range conf
.nameList(longName
) {
277 t
.Errorf("got %d; want less than or equal to 254", len(fqdn
))
281 // Now test a name that's too long for suffixing.
282 unsuffixable
:= "a." + longName
[1:]
283 unsuffixableResults
:= conf
.nameList(unsuffixable
)
284 if len(unsuffixableResults
) != 1 {
285 t
.Errorf("suffixed names %v; want []", unsuffixableResults
[1:])
288 // Now test a name that's too long for DNS.
289 tooLong
:= "a." + longDomain
290 tooLongResults
:= conf
.nameList(tooLong
)
291 if tooLongResults
!= nil {
292 t
.Errorf("suffixed names %v; want nil", tooLongResults
)