-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns_test.go
272 lines (243 loc) · 9.64 KB
/
dns_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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"errors"
"net"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRcodeString(t *testing.T) {
r, _ := newRCode("NOERROR")
assert.Equal(t, "NOERROR", r.String())
}
func TestNewRcode(t *testing.T) {
// Valid case
r, err := newRCode("NOERROR")
require.NoError(t, err)
assert.Equal(t, NOERROR, r)
// Invalid case
r, err = newRCode("TestValue")
require.Error(t, err)
assert.Equal(t, OTHER, r)
}
type dnsClientTest struct {
rcode int
doFail bool
}
func (d *dnsClientTest) query(_ *dns.Msg, _ string) (*dns.Msg, time.Duration, error) {
var td time.Duration = 1000000000
if d.doFail {
return nil, td, errors.New("dummy error message")
}
rawResponse := new(dns.Msg)
rawResponse.Rcode = d.rcode
rawResponse.Answer = append(rawResponse.Answer, &dns.A{A: net.ParseIP("127.0.0.1"), Hdr: dns.RR_Header{Name: "thebeat.co"}})
return rawResponse, td, nil
}
func TestQuery(t *testing.T) {
t.Parallel() // marks TLog as capable of running in parallel with other tests
rc := NOERROR
dr := &dnsRequest{"thebeat.co", "A", nil, []string{}, &rc}
s := newDNSStream(dr, 100)
c := dnsClientTest{dns.RcodeSuccess, false}
var expectedRTT time.Duration = 1000000000
err := s.query(&c)
require.NoError(t, err)
assert.Equal(t, expectedRTT, s.rtt)
assert.InDelta(t, 1, s.verificationStatus, 0.0001)
}
func TestQueryNoResponse(t *testing.T) {
t.Parallel() // marks TLog as capable of running in parallel with other tests
rc := NOERROR
dr := &dnsRequest{"thebeat.co", "A", nil, []string{}, &rc}
s := newDNSStream(dr, 100)
c := dnsClientTest{dns.RcodeSuccess, true}
err := s.query(&c)
require.Error(t, err)
// Make sure we exited function and we didn't update rtt
assert.Equal(t, time.Duration(0), s.rtt)
}
func TestQueryValidationFails(t *testing.T) {
t.Parallel() // marks TLog as capable of running in parallel with other tests
rc := NOERROR
dr := &dnsRequest{"thebeat.co", "A", nil, []string{}, &rc}
s := newDNSStream(dr, 100)
c := dnsClientTest{dns.RcodeNameError, false}
var expectedRTT time.Duration = 1000000000
err := s.query(&c)
require.NoError(t, err)
assert.Equal(t, expectedRTT, s.rtt)
// We expect validationStatus to be 0 since rcode is not as expected
assert.InDelta(t, 0, s.verificationStatus, 0.0001)
}
func TestConstructResolver(t *testing.T) {
// Test case where user specifies custom resolver
resolver := "1.2.3.4"
dr := &dnsRequest{"thebeat.co", "A", &resolver, []string{}, nil}
d := newDNSStream(dr, 100)
res, err := d.constructResolver()
require.NoError(t, err)
expectedResolver := "1.2.3.4:53"
assert.Equal(t, expectedResolver, res)
// Test case where user doesn't specify resolver
// We could do a bit more testing here and mock '/etc/resolv.conf' file
// but for start this seemed okay
dr = &dnsRequest{"thebeat.co", "A", nil, []string{}, nil}
d = newDNSStream(dr, 100)
_, err = d.constructResolver()
require.NoError(t, err)
}
func TestConstructQuery(t *testing.T) {
t.Parallel() // marks TLog as capable of running in parallel with other tests
tests := []struct {
testName string
testDomain string
testQtype string
testReturnedQtype uint16
}{
{"test A type", "thebeat.co", "A", dns.TypeA},
{"test CNAME type", "thebeat.co", "CNAME", dns.TypeCNAME},
{"test MX type", "thebeat.co", "MX", dns.TypeMX},
{"test NS type", "thebeat.co", "NS", dns.TypeNS},
}
for _, tt := range tests {
tt := tt // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(tt.testName, func(t *testing.T) {
t.Parallel() // marks each test case as capable of running in parallel with each other
dr := &dnsRequest{tt.testDomain, tt.testQtype, nil, []string{}, nil}
s := newDNSStream(dr, 100)
dm := s.constructQuery()
// we need recursion
assert.True(t, dm.MsgHdr.RecursionDesired)
// domain should fqdn
assert.Equal(t, tt.testDomain+".", dm.Question[0].Name)
// type should be A
assert.Equal(t, tt.testReturnedQtype, dm.Question[0].Qtype)
})
}
}
func newTestDNSStream(domain, qtype, ip string, rcode int, expectedAnswers []string, expectedRcode *rCode) *dnsStream {
dr := &dnsRequest{domain, qtype, nil, expectedAnswers, expectedRcode}
s := newDNSStream(dr, 100)
rawResponse := new(dns.Msg)
rawResponse.Rcode = rcode
switch qtype {
case "A":
rawResponse.Answer = append(rawResponse.Answer, &dns.A{A: net.ParseIP(ip), Hdr: dns.RR_Header{Name: domain}})
case "AAAA":
rawResponse.Answer = append(rawResponse.Answer, &dns.AAAA{AAAA: net.ParseIP(ip), Hdr: dns.RR_Header{Name: domain}})
case "NS":
rawResponse.Answer = append(rawResponse.Answer, &dns.NS{Ns: ip, Hdr: dns.RR_Header{Name: domain}})
case "MX":
rawResponse.Answer = append(rawResponse.Answer, &dns.MX{Mx: ip, Hdr: dns.RR_Header{Name: domain}})
}
s.response.rawResponse = rawResponse
return s
}
func TestParseResponseSuccessful(t *testing.T) {
t.Parallel() // marks TLog as capable of running in parallel with other tests
tests := []struct {
testName string
testDomain string
testQtype string
testIP string
}{
{"test A answer", "thebeat.co", "A", "127.0.0.1"},
{"test AAAA answer", "thebeat.co", "AAAA", "::1"},
{"test NS answer", "thebeat.co", "NS", "127.0.0.1"},
{"test MX answer", "thebeat.co", "MX", "127.0.0.1"},
}
for _, tt := range tests {
tt := tt // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(tt.testName, func(t *testing.T) {
t.Parallel() // marks each test case as capable of running in parallel with each other
s := newTestDNSStream(tt.testDomain, tt.testQtype, tt.testIP, dns.RcodeSuccess, []string{}, nil)
s.parseResponse()
assert.Len(t, s.response.answers, 1)
assert.Equal(t, tt.testIP, s.response.answers[0])
})
}
}
func TestParseResponseUnSuccessful(t *testing.T) {
t.Parallel() // marks TLog as capable of running in parallel with other tests
tests := []struct {
testName string
testRCode int
testExpectedRCode rCode
}{
{"test NXDOMAIN answer", dns.RcodeNameError, NXDOMAIN},
{"test SERVFAIL answer", dns.RcodeServerFailure, SERVFAIL},
{"test Other type answer", dns.RcodeFormatError, OTHER},
}
for _, tt := range tests {
tt := tt // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(tt.testName, func(t *testing.T) {
t.Parallel() // marks each test case as capable of running in parallel with each other
s := newTestDNSStream("thebeat.co", "A", "127.0.0.1", tt.testRCode, []string{}, nil)
s.parseResponse()
assert.Empty(t, s.response.answers)
assert.Equal(t, tt.testExpectedRCode, s.response.code)
})
}
}
func TestIsResponseLegit(t *testing.T) {
t.Parallel() // marks TLog as capable of running in parallel with other tests
tests := []struct {
name string
domain string
qtype string
mockedAnswer string
rcode int
expectedRcode rCode
expectedAnswers []string
testResponse bool
}{
{"test no expected rcode & no expected answers", "thebeat.co", "A", "127.0.0.1", dns.RcodeSuccess, -1, []string{}, true},
{"test expected rcode and different rcode & no expected answers", "thebeat.co", "A", "127.0.0.1", dns.RcodeSuccess, NXDOMAIN, []string{}, false},
{"test expected rcode and different rcode & matching expected answers", "thebeat.co", "A", "127.0.0.1", dns.RcodeSuccess, NXDOMAIN, []string{"127.0.0.1"}, false},
{"test expected rcode and same rcode & no expected answers", "thebeat.co", "A", "127.0.0.1", dns.RcodeSuccess, NOERROR, []string{}, true},
{"test expected rcode and same rcode & matching expected answers", "thebeat.co", "A", "127.0.0.1", dns.RcodeSuccess, NOERROR, []string{"127.0.0.1"}, true},
{"test no expected rcode & matching expected answers", "thebeat.co", "A", "127.0.0.1", dns.RcodeSuccess, -1, []string{"127.0.0.1"}, true},
{"test no expected rcode & no matching expected answers", "thebeat.co", "A", "127.0.0.1", dns.RcodeSuccess, -1, []string{"128.0.0.1"}, false},
{"test no expected rcode & no matching expected answers", "thebeat.co", "A", "127.0.0.1", dns.RcodeSuccess, -1, []string{"128.0.0.1", "127.0.0.1"}, false},
}
for _, tt := range tests {
tt := tt // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(tt.name, func(t *testing.T) {
t.Parallel() // marks each test case as capable of running in parallel with each other
expRcode := &tt.expectedRcode
if tt.expectedRcode == -1 {
expRcode = nil
}
s := newTestDNSStream(tt.domain, tt.qtype, tt.mockedAnswer, tt.rcode, tt.expectedAnswers, expRcode)
s.parseResponse()
res := s.isResponseLegit()
assert.Equal(t, tt.testResponse, res)
})
}
}
func TestAreEqual(t *testing.T) {
t.Parallel() // marks TLog as capable of running in parallel with other tests
tests := []struct {
name string
a []string
b []string
testResponse bool
}{
{"test empty lists", []string{}, []string{}, true},
{"test same elements", []string{"a", "b"}, []string{"a", "b"}, true},
{"test same elements but our of order", []string{"b", "a"}, []string{"a", "b"}, true},
{"test subset elements first", []string{"a", "b", "c"}, []string{"a", "b"}, false},
{"test subset elements second", []string{"a", "b"}, []string{"a", "b", "c"}, false},
}
for _, tt := range tests {
tt := tt // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(tt.name, func(t *testing.T) {
t.Parallel() // marks each test case as capable of running in parallel with each other
res := areEqual(tt.a, tt.b)
assert.Equal(t, tt.testResponse, res)
})
}
}