-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
248 lines (218 loc) · 5.06 KB
/
main_test.go
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
247
248
package main
import (
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net"
"strings"
. "testing"
)
var testDomain = "a-test.mysuperfancyapi.com."
var testAnyDomain = "any-test.mysuperfancyapi.com."
func init() {
dnsServerGroups = [][]string{[]string{"8.8.8.8:53"}}
}
type Writer struct {
ReplyCh chan *dns.Msg
}
func (w *Writer) LocalAddr() (a net.Addr) {
return
}
func (w *Writer) RemoteAddr() (a net.Addr) {
return
}
func (w *Writer) WriteMsg(r *dns.Msg) error {
w.ReplyCh <- r
return nil
}
func (w *Writer) Write(b []byte) (i int, err error) {
r := new(dns.Msg)
err = r.Unpack(b)
w.ReplyCh <- r
return
}
func (w *Writer) Close() error {
return nil
}
func (w *Writer) TsigStatus() error {
return nil
}
func (w *Writer) TsigTimersOnly(b bool) {}
func (w *Writer) Hijack() {}
func getWriter() *Writer {
return &Writer{make(chan *dns.Msg, 1)}
}
func startServer(t *T) (*dns.Server, string) {
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
require.Nil(t, err)
server := &dns.Server{PacketConn: pc}
sCh := make(chan bool)
server.NotifyStartedFunc = func() {
sCh <- true
}
go func() {
server.ActivateAndServe()
//once Shutdown is called on the server, we need to close pc
pc.Close()
}()
//block until the server is started
<-sCh
return server, pc.LocalAddr().String()
}
func Test(t *T) {
m1 := new(dns.Msg)
m1.SetQuestion(testDomain, dns.TypeA)
w1 := getWriter()
go func() {
handleRequest(w1, m1)
}()
r1 := <-w1.ReplyCh
require.Len(t, r1.Answer, 1)
m2 := new(dns.Msg)
m2.SetQuestion(testDomain, dns.TypeA)
r2, err := dns.Exchange(m2, "8.8.8.8:53")
require.Nil(t, err)
require.Len(t, r2.Answer, 1)
assert.Equal(t, r2.Rcode, r1.Rcode)
a1 := strings.Split(r1.Answer[0].String(), "\t")
//example: a-test.mysuperfancyapi.com., 245, IN, A, 192.95.20.208
//we want to overwrite the TTL since that will be different
a2 := strings.Split(r2.Answer[0].String(), "\t")
a1[1] = ""
a2[1] = ""
assert.Equal(t, a2, a1)
}
func TestNXDOMAIN(t *T) {
m1 := new(dns.Msg)
m1.SetQuestion("-.", dns.TypeA)
w1 := getWriter()
go func() {
handleRequest(w1, m1)
}()
r1 := <-w1.ReplyCh
assert.Equal(t, dns.RcodeNameError, r1.Rcode)
}
func TestFORMERR(t *T) {
m1 := new(dns.Msg)
w1 := getWriter()
go func() {
handleRequest(w1, m1)
}()
r1 := <-w1.ReplyCh
assert.Equal(t, dns.RcodeFormatError, r1.Rcode)
m1 = new(dns.Msg)
m1.SetQuestion(testDomain, dns.TypeNone)
w1 = getWriter()
go func() {
handleRequest(w1, m1)
}()
r1 = <-w1.ReplyCh
assert.Equal(t, dns.RcodeFormatError, r1.Rcode)
}
func TestInFlight(t *T) {
m1 := new(dns.Msg)
m1.SetQuestion(testDomain, dns.TypeA)
w1 := getWriter()
m2 := new(dns.Msg)
m2.SetQuestion(testDomain, dns.TypeA)
w2 := getWriter()
go func() {
handleRequest(w1, m1)
}()
go func() {
handleRequest(w2, m2)
}()
var r1 *dns.Msg
var r2 *dns.Msg
for r1 == nil || r2 == nil {
select {
case r1 = <-w1.ReplyCh:
case r2 = <-w2.ReplyCh:
}
}
require.Equal(t, dns.RcodeSuccess, r1.Rcode)
require.Len(t, r1.Answer, 1)
assert.Equal(t, r2.Rcode, r2.Rcode)
assert.Equal(t, r1.Answer[0], r2.Answer[0])
assert.Equal(t, m1.Id, r1.Id)
assert.Equal(t, m2.Id, r2.Id)
}
func TestInFlightAAAAAndA(t *T) {
m1 := new(dns.Msg)
m1.SetQuestion(testAnyDomain, dns.TypeAAAA)
w1 := getWriter()
m2 := new(dns.Msg)
m2.SetQuestion(testAnyDomain, dns.TypeA)
w2 := getWriter()
go func() {
handleRequest(w1, m1)
}()
go func() {
handleRequest(w2, m2)
}()
var r1 *dns.Msg
var r2 *dns.Msg
for r1 == nil || r2 == nil {
select {
case r1 = <-w1.ReplyCh:
case r2 = <-w2.ReplyCh:
}
}
require.Len(t, r1.Answer, 1)
require.Len(t, r2.Answer, 1)
assert.NotEqual(t, r1.Answer[0], r2.Answer[0])
}
func TestInFlightEDns0(t *T) {
m1 := new(dns.Msg)
m1.SetQuestion(testAnyDomain, dns.TypeA)
m1.SetEdns0(4096, false)
w1 := getWriter()
m2 := new(dns.Msg)
m2.SetQuestion(testAnyDomain, dns.TypeA)
w2 := getWriter()
go func() {
handleRequest(w1, m1)
}()
go func() {
handleRequest(w2, m2)
}()
var r1 *dns.Msg
var r2 *dns.Msg
for r1 == nil || r2 == nil {
select {
case r1 = <-w1.ReplyCh:
case r2 = <-w2.ReplyCh:
}
}
//note: this test could be flaky since we're relying on google to return
//edns0 response when we send one vs when we don't send one
assert.NotNil(t, r1.IsEdns0())
assert.Nil(t, r2.IsEdns0())
}
func returnTruncated(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Answer = make([]dns.RR, 1)
m.Answer[0] = &dns.A{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}, A: net.ParseIP("127.0.0.1").To4()}
m.Truncated = true
w.WriteMsg(m)
}
func TestTruncated(t *T) {
dns.HandleFunc(testDomain, returnTruncated)
defer dns.HandleRemove(testDomain)
s, addr := startServer(t)
defer s.Shutdown()
r := new(dns.Msg)
r.SetQuestion(testDomain, dns.TypeA)
a := tryProxy(r, addr)
assert.Nil(t, a)
allowTruncated = true
defer func() {
allowTruncated = false
}()
r = new(dns.Msg)
r.SetQuestion(testDomain, dns.TypeA)
a = tryProxy(r, addr)
assert.NotNil(t, a)
assert.True(t, a.Truncated)
}