-
Notifications
You must be signed in to change notification settings - Fork 32
/
client_test.go
172 lines (140 loc) · 4.34 KB
/
client_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
package retryabledns
import (
"testing"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDialerLocalAddr(t *testing.T) {
/** Works without LocalAddrIP **/
options := Options{
BaseResolvers: []string{"1.1.1.1:53", "udp:8.8.8.8"},
MaxRetries: 3,
}
err := options.Validate()
require.Nil(t, err)
client, _ := NewWithOptions(options)
d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
// From current dig result
require.True(t, len(d.A) > 0)
/** Errors with invalid LocalAddrIP **/
options = Options{
BaseResolvers: []string{"1.1.1.1:53", "udp:8.8.8.8"},
MaxRetries: 3,
}
options.SetLocalAddrIP("1.2.3.4")
err = options.Validate()
require.Nil(t, err)
client, _ = NewWithOptions(options)
_, err = client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.NotNil(t, err)
/** Does not error with valid Local IP **/
// options = Options{
// BaseResolvers: []string{"1.1.1.1:53", "udp:8.8.8.8"},
// MaxRetries: 3,
// }
// err = options.SetLocalAddrIPFromNetInterface("en0")
// require.Nil(t, err)
// err = options.Validate()
// require.Nil(t, err)
// client, _ = NewWithOptions(options)
// _, err = client.QueryMultiple("example.com", []uint16{dns.TypeA})
// require.Nil(t, err)
// // From current dig result
// require.True(t, len(d.A) > 0)
}
func TestConsistentResolve(t *testing.T) {
client, _ := New([]string{"8.8.8.8:53", "1.1.1.1:53"}, 5)
var last string
for i := 0; i < 10; i++ {
d, err := client.Resolve("example.com")
require.Nil(t, err, "could not resolve dns")
if last != "" {
require.Equal(t, last, d.A[0], "got another data from previous")
} else {
last = d.A[0]
}
}
}
func TestUDP(t *testing.T) {
client, _ := New([]string{"1.1.1.1:53", "udp:8.8.8.8"}, 5)
d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
// From current dig result
require.True(t, len(d.A) > 0)
}
func TestTCP(t *testing.T) {
client, _ := New([]string{"tcp:1.1.1.1:53", "tcp:8.8.8.8"}, 5)
d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
// From current dig result
require.True(t, len(d.A) > 0)
}
func TestDOH(t *testing.T) {
client, _ := New([]string{"doh:https://doh.opendns.com/dns-query:post", "doh:https://doh.opendns.com/dns-query:get"}, 5)
d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
// From current dig result
require.True(t, len(d.A) > 0)
}
func TestDOT(t *testing.T) {
client, _ := New([]string{"dot:dns.google:853", "dot:1dot1dot1dot1.cloudflare-dns.com"}, 5)
d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
// From current dig result
require.True(t, len(d.A) > 0)
}
func TestQueryMultiple(t *testing.T) {
client, _ := New([]string{"8.8.8.8:53", "1.1.1.1:53"}, 5)
// Test various query types
d, err := client.QueryMultiple("scanme.sh", []uint16{
dns.TypeA,
dns.TypeAAAA,
dns.TypeSOA,
})
require.Nil(t, err)
// From current dig result
require.True(t, len(d.A) > 0)
require.True(t, len(d.AAAA) > 0)
require.True(t, len(d.SOA) > 0)
require.NotZero(t, d.TTL)
}
func TestRetries(t *testing.T) {
client, _ := New([]string{"127.0.0.1"}, 5)
// Test that error is returned on max retries, should conn refused 5 times then err
_, err := client.QueryMultiple("scanme.sh", []uint16{dns.TypeA})
require.ErrorIs(t, err, ErrRetriesExceeded)
msg := &dns.Msg{}
msg.Id = dns.Id()
msg.SetEdns0(4096, false)
msg.Question = make([]dns.Question, 1)
msg.RecursionDesired = true
question := dns.Question{
Name: "scanme.sh",
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}
msg.Question[0] = question
// Test with raw Do() interface as well
_, err = client.Do(msg)
require.True(t, err == ErrRetriesExceeded)
}
func TestNoRecords(t *testing.T) {
client, err := New([]string{"8.8.8.8:53", "1.1.1.1:53"}, 5)
require.NoError(t, err)
// Test various query types
res, err := client.QueryMultiple("donotexist.scanme.sh", []uint16{
dns.TypeA,
dns.TypeAAAA,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.Empty(t, res.A)
assert.Empty(t, res.AAAA)
}
func TestTrace(t *testing.T) {
client, _ := New([]string{"8.8.8.8:53", "1.1.1.1:53"}, 5)
_, err := client.Trace("www.projectdiscovery.io", dns.TypeA, 100)
require.Nil(t, err, "could not resolve dns")
}