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 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."},
107 func TestDNSReadConfig(t
*testing
.T
) {
108 origGetHostname
:= getHostname
109 defer func() { getHostname
= origGetHostname
}()
110 getHostname
= func() (string, error
) { return "host.domain.local", nil }
112 for _
, tt
:= range dnsReadConfigTests
{
113 conf
:= dnsReadConfig(tt
.name
)
117 conf
.mtime
= time
.Time
{}
118 if !reflect
.DeepEqual(conf
, tt
.want
) {
119 t
.Errorf("%s:\ngot: %+v\nwant: %+v", tt
.name
, conf
, tt
.want
)
124 func TestDNSReadMissingFile(t
*testing
.T
) {
125 origGetHostname
:= getHostname
126 defer func() { getHostname
= origGetHostname
}()
127 getHostname
= func() (string, error
) { return "host.domain.local", nil }
129 conf
:= dnsReadConfig("a-nonexistent-file")
130 if !os
.IsNotExist(conf
.err
) {
131 t
.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf
.err
, os
.ErrNotExist
)
137 timeout
: 5 * time
.Second
,
139 search
: []string{"domain.local."},
141 if !reflect
.DeepEqual(conf
, want
) {
142 t
.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf
, want
)
146 var dnsDefaultSearchTests
= []struct {
152 name
: "host.long.domain.local",
153 want
: []string{"long.domain.local."},
157 want
: []string{"local."},
164 name
: "host.domain.local",
165 err
: errors
.New("errored"),
169 // ensures we don't return []string{""}
170 // which causes duplicate lookups
176 func TestDNSDefaultSearch(t
*testing
.T
) {
177 origGetHostname
:= getHostname
178 defer func() { getHostname
= origGetHostname
}()
180 for _
, tt
:= range dnsDefaultSearchTests
{
181 getHostname
= func() (string, error
) { return tt
.name
, tt
.err
}
182 got
:= dnsDefaultSearch()
183 if !reflect
.DeepEqual(got
, tt
.want
) {
184 t
.Errorf("dnsDefaultSearch with hostname %q and error %+v = %q, wanted %q", tt
.name
, tt
.err
, got
, tt
.want
)
189 func TestDNSNameLength(t
*testing
.T
) {
190 origGetHostname
:= getHostname
191 defer func() { getHostname
= origGetHostname
}()
192 getHostname
= func() (string, error
) { return "host.domain.local", nil }
195 for i
:= 0; i
< 63; i
++ {
198 longDomain
:= strings
.Repeat(char63
+".", 5) + "example"
200 for _
, tt
:= range dnsReadConfigTests
{
201 conf
:= dnsReadConfig(tt
.name
)
206 var shortestSuffix
int
207 for _
, suffix
:= range tt
.want
.search
{
208 if shortestSuffix
== 0 ||
len(suffix
) < shortestSuffix
{
209 shortestSuffix
= len(suffix
)
213 // Test a name that will be maximally long when prefixing the shortest
214 // suffix (accounting for the intervening dot).
215 longName
:= longDomain
[len(longDomain
)-254+1+shortestSuffix
:]
216 if longName
[0] == '.' || longName
[1] == '.' {
217 longName
= "aa." + longName
[3:]
219 for _
, fqdn
:= range conf
.nameList(longName
) {
221 t
.Errorf("got %d; want less than or equal to 254", len(fqdn
))
225 // Now test a name that's too long for suffixing.
226 unsuffixable
:= "a." + longName
[1:]
227 unsuffixableResults
:= conf
.nameList(unsuffixable
)
228 if len(unsuffixableResults
) != 1 {
229 t
.Errorf("suffixed names %v; want []", unsuffixableResults
[1:])
232 // Now test a name that's too long for DNS.
233 tooLong
:= "a." + longDomain
234 tooLongResults
:= conf
.nameList(tooLong
)
235 if tooLongResults
!= nil {
236 t
.Errorf("suffixed names %v; want nil", tooLongResults
)