forked from ginuerzh/gost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver_test.go
270 lines (252 loc) · 6.24 KB
/
resolver_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
package gost
import (
"bytes"
"fmt"
"io"
"net"
"testing"
"time"
)
var dnsTests = []struct {
ns NameServer
host string
pass bool
}{
{NameServer{Addr: "1.1.1.1"}, "192.168.1.1", true},
{NameServer{Addr: "1.1.1.1"}, "github", true},
{NameServer{Addr: "1.1.1.1"}, "github.com", true},
{NameServer{Addr: "1.1.1.1:53"}, "github.com", true},
{NameServer{Addr: "1.1.1.1:53", Protocol: "tcp"}, "github.com", true},
{NameServer{Addr: "1.1.1.1:853", Protocol: "tls"}, "github.com", true},
{NameServer{Addr: "1.1.1.1:853", Protocol: "tls", Hostname: "example.com"}, "github.com", false},
{NameServer{Addr: "1.1.1.1:853", Protocol: "tls", Hostname: "cloudflare-dns.com"}, "github.com", true},
{NameServer{Addr: "https://cloudflare-dns.com/dns-query", Protocol: "https"}, "github.com", true},
{NameServer{Addr: "https://1.0.0.1/dns-query", Protocol: "https"}, "github.com", true},
{NameServer{Addr: "1.1.1.1:12345"}, "github.com", false},
{NameServer{Addr: "1.1.1.1:12345", Protocol: "tcp"}, "github.com", false},
{NameServer{Addr: "1.1.1.1:12345", Protocol: "tls"}, "github.com", false},
{NameServer{Addr: "https://1.0.0.1:12345/dns-query", Protocol: "https"}, "github.com", false},
}
func dnsResolverRoundtrip(t *testing.T, r Resolver, host string) error {
ips, err := r.Resolve(host)
t.Log(host, ips, err)
if err != nil {
return err
}
return nil
}
func TestDNSResolver(t *testing.T) {
for i, tc := range dnsTests {
tc := tc
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
ns := tc.ns
t.Log(ns)
r := NewResolver(0, ns)
resolv := r.(*resolver)
resolv.domain = "com"
if err := r.Init(); err != nil {
t.Error("got error:", err)
}
err := dnsResolverRoundtrip(t, r, tc.host)
if err != nil {
if tc.pass {
t.Error("got error:", err)
}
} else {
if !tc.pass {
t.Error("should failed")
}
}
})
}
}
var resolverCacheTests = []struct {
name string
ips []net.IP
ttl time.Duration
result []net.IP
}{
{"", nil, 0, nil},
{"", []net.IP{net.IPv4(192, 168, 1, 1)}, 0, nil},
{"", []net.IP{net.IPv4(192, 168, 1, 1)}, 10 * time.Second, nil},
{"example.com", nil, 10 * time.Second, nil},
{"example.com", []net.IP{}, 10 * time.Second, nil},
{"example.com", []net.IP{net.IPv4(192, 168, 1, 1)}, 0, nil},
{"example.com", []net.IP{net.IPv4(192, 168, 1, 1)}, -1, nil},
{"example.com", []net.IP{net.IPv4(192, 168, 1, 1)}, 10 * time.Second,
[]net.IP{net.IPv4(192, 168, 1, 1)}},
{"example.com", []net.IP{net.IPv4(192, 168, 1, 1), net.IPv4(192, 168, 1, 2)}, 10 * time.Second,
[]net.IP{net.IPv4(192, 168, 1, 1), net.IPv4(192, 168, 1, 2)}},
}
/*
func TestResolverCache(t *testing.T) {
isEqual := func(a, b []net.IP) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil || len(a) != len(b) {
return false
}
for i := range a {
if !a[i].Equal(b[i]) {
return false
}
}
return true
}
for i, tc := range resolverCacheTests {
tc := tc
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
r := newResolver(tc.ttl)
r.cache.storeCache(tc.name, tc.ips, tc.ttl)
ips := r.cache.loadCache(tc.name, tc.ttl)
if !isEqual(tc.result, ips) {
t.Error("unexpected cache value:", tc.name, ips, tc.ttl)
}
})
}
}
*/
var resolverReloadTests = []struct {
r io.Reader
timeout time.Duration
ttl time.Duration
domain string
period time.Duration
ns *NameServer
stopped bool
}{
{
r: nil,
},
{
r: bytes.NewBufferString(""),
},
{
r: bytes.NewBufferString("reload 10s"),
period: 10 * time.Second,
},
{
r: bytes.NewBufferString("timeout 10s\nreload 10s\n"),
timeout: 10 * time.Second,
period: 10 * time.Second,
},
{
r: bytes.NewBufferString("ttl 10s\ntimeout 10s\nreload 10s\n"),
timeout: 10 * time.Second,
period: 10 * time.Second,
ttl: 10 * time.Second,
},
{
r: bytes.NewBufferString("domain example.com\nttl 10s\ntimeout 10s\nreload 10s\n"),
timeout: 10 * time.Second,
period: 10 * time.Second,
ttl: 10 * time.Second,
domain: "example.com",
},
{
r: bytes.NewBufferString("1.1.1.1"),
ns: &NameServer{
Addr: "1.1.1.1",
},
stopped: true,
},
{
r: bytes.NewBufferString("\n# comment\ntimeout 10s\nsearch\nnameserver \nnameserver 1.1.1.1 udp"),
ns: &NameServer{
Protocol: "udp",
Addr: "1.1.1.1",
},
timeout: 10 * time.Second,
stopped: true,
},
{
r: bytes.NewBufferString("1.1.1.1 tcp"),
ns: &NameServer{
Addr: "1.1.1.1",
Protocol: "tcp",
},
stopped: true,
},
{
r: bytes.NewBufferString("1.1.1.1:853 tls cloudflare-dns.com"),
ns: &NameServer{
Addr: "1.1.1.1:853",
Protocol: "tls",
Hostname: "cloudflare-dns.com",
},
stopped: true,
},
{
r: bytes.NewBufferString("1.1.1.1:853 tls"),
ns: &NameServer{
Addr: "1.1.1.1:853",
Protocol: "tls",
},
stopped: true,
},
{
r: bytes.NewBufferString("1.0.0.1:53 https"),
stopped: true,
},
{
r: bytes.NewBufferString("https://1.0.0.1/dns-query"),
ns: &NameServer{
Addr: "https://1.0.0.1/dns-query",
Protocol: "https",
},
stopped: true,
},
}
func TestResolverReload(t *testing.T) {
for i, tc := range resolverReloadTests {
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
r := newResolver(0)
if err := r.Reload(tc.r); err != nil {
t.Error(err)
}
t.Log(r.String())
if r.TTL() != tc.ttl {
t.Errorf("ttl value should be %v, got %v",
tc.ttl, r.TTL())
}
if r.Period() != tc.period {
t.Errorf("period value should be %v, got %v",
tc.period, r.period)
}
if r.domain != tc.domain {
t.Errorf("domain value should be %v, got %v",
tc.domain, r.domain)
}
var ns *NameServer
if len(r.servers) > 0 {
ns = &r.servers[0]
}
if !compareNameServer(ns, tc.ns) {
t.Errorf("nameserver not equal, should be %v, got %v",
tc.ns, r.servers)
}
if tc.stopped {
r.Stop()
if r.Period() >= 0 {
t.Errorf("period of the stopped reloader should be minus value")
}
}
if r.Stopped() != tc.stopped {
t.Errorf("stopped value should be %v, got %v",
tc.stopped, r.Stopped())
}
})
}
}
func compareNameServer(n1, n2 *NameServer) bool {
if n1 == n2 {
return true
}
if n1 == nil || n2 == nil {
return false
}
return n1.Addr == n2.Addr &&
n1.Hostname == n2.Hostname &&
n1.Protocol == n2.Protocol
}