summaryrefslogtreecommitdiff
path: root/src/net/dnsclient_unix_test.go
blob: 1167c26b39dacbfb98863ad4bd5af2633e6dda49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package net

import (
	"io"
	"io/ioutil"
	"os"
	"path"
	"reflect"
	"testing"
	"time"
)

var dnsTransportFallbackTests = []struct {
	server  string
	name    string
	qtype   uint16
	timeout int
	rcode   int
}{
	// Querying "com." with qtype=255 usually makes an answer
	// which requires more than 512 bytes.
	{"8.8.8.8:53", "com.", dnsTypeALL, 2, dnsRcodeSuccess},
	{"8.8.4.4:53", "com.", dnsTypeALL, 4, dnsRcodeSuccess},
}

func TestDNSTransportFallback(t *testing.T) {
	if testing.Short() || !*testExternal {
		t.Skip("skipping test to avoid external network")
	}

	for _, tt := range dnsTransportFallbackTests {
		timeout := time.Duration(tt.timeout) * time.Second
		msg, err := exchange(tt.server, tt.name, tt.qtype, timeout)
		if err != nil {
			t.Error(err)
			continue
		}
		switch msg.rcode {
		case tt.rcode, dnsRcodeServerFailure:
		default:
			t.Errorf("got %v from %v; want %v", msg.rcode, tt.server, tt.rcode)
			continue
		}
	}
}

// See RFC 6761 for further information about the reserved, pseudo
// domain names.
var specialDomainNameTests = []struct {
	name  string
	qtype uint16
	rcode int
}{
	// Name resoltion APIs and libraries should not recongnize the
	// followings as special.
	{"1.0.168.192.in-addr.arpa.", dnsTypePTR, dnsRcodeNameError},
	{"test.", dnsTypeALL, dnsRcodeNameError},
	{"example.com.", dnsTypeALL, dnsRcodeSuccess},

	// Name resoltion APIs and libraries should recongnize the
	// followings as special and should not send any queries.
	// Though, we test those names here for verifying nagative
	// answers at DNS query-response interaction level.
	{"localhost.", dnsTypeALL, dnsRcodeNameError},
	{"invalid.", dnsTypeALL, dnsRcodeNameError},
}

func TestSpecialDomainName(t *testing.T) {
	if testing.Short() || !*testExternal {
		t.Skip("skipping test to avoid external network")
	}

	server := "8.8.8.8:53"
	for _, tt := range specialDomainNameTests {
		msg, err := exchange(server, tt.name, tt.qtype, 0)
		if err != nil {
			t.Error(err)
			continue
		}
		switch msg.rcode {
		case tt.rcode, dnsRcodeServerFailure:
		default:
			t.Errorf("got %v from %v; want %v", msg.rcode, server, tt.rcode)
			continue
		}
	}
}

type resolvConfTest struct {
	*testing.T
	dir     string
	path    string
	started bool
	quitc   chan chan struct{}
}

func newResolvConfTest(t *testing.T) *resolvConfTest {
	dir, err := ioutil.TempDir("", "resolvConfTest")
	if err != nil {
		t.Fatalf("could not create temp dir: %v", err)
	}

	// Disable the default loadConfig
	onceLoadConfig.Do(func() {})

	r := &resolvConfTest{
		T:     t,
		dir:   dir,
		path:  path.Join(dir, "resolv.conf"),
		quitc: make(chan chan struct{}),
	}

	return r
}

func (r *resolvConfTest) Start() {
	loadConfig(r.path, 100*time.Millisecond, r.quitc)
	r.started = true
}

func (r *resolvConfTest) SetConf(s string) {
	// Make sure the file mtime will be different once we're done here,
	// even on systems with coarse (1s) mtime resolution.
	time.Sleep(time.Second)

	f, err := os.OpenFile(r.path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
	if err != nil {
		r.Fatalf("failed to create temp file %s: %v", r.path, err)
	}
	if _, err := io.WriteString(f, s); err != nil {
		f.Close()
		r.Fatalf("failed to write temp file: %v", err)
	}
	f.Close()

	if r.started {
		cfg.ch <- struct{}{} // fill buffer
		cfg.ch <- struct{}{} // wait for reload to begin
		cfg.ch <- struct{}{} // wait for reload to complete
	}
}

func (r *resolvConfTest) WantServers(want []string) {
	cfg.mu.RLock()
	defer cfg.mu.RUnlock()
	if got := cfg.dnsConfig.servers; !reflect.DeepEqual(got, want) {
		r.Fatalf("Unexpected dns server loaded, got %v want %v", got, want)
	}
}

func (r *resolvConfTest) Close() {
	resp := make(chan struct{})
	r.quitc <- resp
	<-resp
	if err := os.RemoveAll(r.dir); err != nil {
		r.Logf("failed to remove temp dir %s: %v", r.dir, err)
	}
}

func TestReloadResolvConfFail(t *testing.T) {
	if testing.Short() || !*testExternal {
		t.Skip("skipping test to avoid external network")
	}

	r := newResolvConfTest(t)
	defer r.Close()

	// resolv.conf.tmp does not exist yet
	r.Start()
	if _, err := goLookupIP("golang.org"); err == nil {
		t.Fatal("goLookupIP(missing) succeeded")
	}

	r.SetConf("nameserver 8.8.8.8")
	if _, err := goLookupIP("golang.org"); err != nil {
		t.Fatalf("goLookupIP(missing; good) failed: %v", err)
	}

	// Using a bad resolv.conf while we had a good
	// one before should not update the config
	r.SetConf("")
	if _, err := goLookupIP("golang.org"); err != nil {
		t.Fatalf("goLookupIP(missing; good; bad) failed: %v", err)
	}
}

func TestReloadResolvConfChange(t *testing.T) {
	if testing.Short() || !*testExternal {
		t.Skip("skipping test to avoid external network")
	}

	r := newResolvConfTest(t)
	defer r.Close()

	r.SetConf("nameserver 8.8.8.8")
	r.Start()

	if _, err := goLookupIP("golang.org"); err != nil {
		t.Fatalf("goLookupIP(good) failed: %v", err)
	}
	r.WantServers([]string{"8.8.8.8"})

	// Using a bad resolv.conf when we had a good one
	// before should not update the config
	r.SetConf("")
	if _, err := goLookupIP("golang.org"); err != nil {
		t.Fatalf("goLookupIP(good; bad) failed: %v", err)
	}

	// A new good config should get picked up
	r.SetConf("nameserver 8.8.4.4")
	r.WantServers([]string{"8.8.4.4"})
}

func BenchmarkGoLookupIP(b *testing.B) {
	for i := 0; i < b.N; i++ {
		goLookupIP("www.example.com")
	}
}

func BenchmarkGoLookupIPNoSuchHost(b *testing.B) {
	for i := 0; i < b.N; i++ {
		goLookupIP("some.nonexistent")
	}
}

func BenchmarkGoLookupIPWithBrokenNameServer(b *testing.B) {
	onceLoadConfig.Do(loadDefaultConfig)
	if cfg.dnserr != nil || cfg.dnsConfig == nil {
		b.Fatalf("loadConfig failed: %v", cfg.dnserr)
	}
	// This looks ugly but it's safe as long as benchmarks are run
	// sequentially in package testing.
	orig := cfg.dnsConfig
	cfg.dnsConfig.servers = append([]string{"203.0.113.254"}, cfg.dnsConfig.servers...) // use TEST-NET-3 block, see RFC 5737
	for i := 0; i < b.N; i++ {
		goLookupIP("www.example.com")
	}
	cfg.dnsConfig = orig
}