forked from abh/geodns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve_test.go
269 lines (215 loc) · 7.32 KB
/
serve_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
package main
import (
"net"
"strings"
"sync"
"github.com/abh/geodns/Godeps/_workspace/src/github.com/miekg/dns"
. "github.com/abh/geodns/Godeps/_workspace/src/gopkg.in/check.v1"
)
const (
PORT = ":8853"
)
type ServeSuite struct {
}
var _ = Suite(&ServeSuite{})
func (s *ServeSuite) SetUpSuite(c *C) {
// setup and register metrics
metrics := NewMetrics()
go metrics.Updater()
Zones := make(Zones)
setupPgeodnsZone(Zones)
setupRootZone()
zonesReadDir("dns", Zones)
// listenAndServe returns after listning on udp + tcp, so just
// wait for it before continuing
listenAndServe(PORT)
}
func (s *ServeSuite) TestServing(c *C) {
r := exchange(c, "_status.pgeodns.", dns.TypeTXT)
txt := r.Answer[0].(*dns.TXT).Txt[0]
if !strings.HasPrefix(txt, "{") {
c.Log("Unexpected result for _status.pgeodns", txt)
c.Fail()
}
// Allow _country and _status queries as long as the first label is that
r = exchange(c, "_country.foo.pgeodns.", dns.TypeTXT)
txt = r.Answer[0].(*dns.TXT).Txt[0]
// Got appropriate response for _country txt query
if !strings.HasPrefix(txt, "127.0.0.1:") {
c.Log("Unexpected result for _country.foo.pgeodns", txt)
c.Fail()
}
// Make sure A requests for _status doesn't NXDOMAIN
r = exchange(c, "_status.pgeodns.", dns.TypeA)
c.Check(r.Answer, HasLen, 0)
// Got one SOA record
c.Check(r.Ns, HasLen, 1)
// NOERROR for A request
c.Check(r.Rcode, Equals, dns.RcodeSuccess)
r = exchange(c, "bar.test.example.com.", dns.TypeA)
ip := r.Answer[0].(*dns.A).A
c.Check(ip.String(), Equals, "192.168.1.2")
c.Check(int(r.Answer[0].Header().Ttl), Equals, 601)
r = exchange(c, "test.example.com.", dns.TypeSOA)
soa := r.Answer[0].(*dns.SOA)
serial := soa.Serial
c.Check(int(serial), Equals, 3)
// no AAAA records for 'bar', so check we get a soa record back
r = exchange(c, "test.example.com.", dns.TypeAAAA)
soa2 := r.Ns[0].(*dns.SOA)
c.Check(soa, DeepEquals, soa2)
// CNAMEs
r = exchange(c, "www.test.example.com.", dns.TypeA)
c.Check(r.Answer[0].(*dns.CNAME).Target, Equals, "geo.bitnames.com.")
c.Check(int(r.Answer[0].Header().Ttl), Equals, 1800)
//SPF
r = exchange(c, "test.example.com.", dns.TypeSPF)
c.Check(r.Answer[0].(*dns.SPF).Txt[0], Equals, "v=spf1 ~all")
//SRV
r = exchange(c, "_sip._tcp.test.example.com.", dns.TypeSRV)
c.Check(r.Answer[0].(*dns.SRV).Target, Equals, "sipserver.example.com.")
c.Check(r.Answer[0].(*dns.SRV).Port, Equals, uint16(5060))
c.Check(r.Answer[0].(*dns.SRV).Priority, Equals, uint16(10))
c.Check(r.Answer[0].(*dns.SRV).Weight, Equals, uint16(100))
// MX
r = exchange(c, "test.example.com.", dns.TypeMX)
c.Check(r.Answer[0].(*dns.MX).Mx, Equals, "mx.example.net.")
c.Check(r.Answer[1].(*dns.MX).Mx, Equals, "mx2.example.net.")
c.Check(r.Answer[1].(*dns.MX).Preference, Equals, uint16(20))
// Verify the first A record was created
r = exchange(c, "a.b.c.test.example.com.", dns.TypeA)
ip = r.Answer[0].(*dns.A).A
c.Check(ip.String(), Equals, "192.168.1.7")
// Verify sub-labels are created
r = exchange(c, "b.c.test.example.com.", dns.TypeA)
c.Check(r.Answer, HasLen, 0)
c.Check(r.Rcode, Equals, dns.RcodeSuccess)
r = exchange(c, "c.test.example.com.", dns.TypeA)
c.Check(r.Answer, HasLen, 0)
c.Check(r.Rcode, Equals, dns.RcodeSuccess)
// Verify the first A record was created
r = exchange(c, "three.two.one.test.example.com.", dns.TypeA)
ip = r.Answer[0].(*dns.A).A
c.Check(ip.String(), Equals, "192.168.1.5")
// Verify single sub-labels is created and no record is returned
r = exchange(c, "two.one.test.example.com.", dns.TypeA)
c.Check(r.Answer, HasLen, 0)
c.Check(r.Rcode, Equals, dns.RcodeSuccess)
// Verify the A record wasn't over written
r = exchange(c, "one.test.example.com.", dns.TypeA)
ip = r.Answer[0].(*dns.A).A
c.Check(ip.String(), Equals, "192.168.1.6")
}
func (s *ServeSuite) TestServingMixedCase(c *C) {
r := exchange(c, "_sTaTUs.pGEOdns.", dns.TypeTXT)
c.Assert(r.Rcode, Equals, dns.RcodeSuccess)
txt := r.Answer[0].(*dns.TXT).Txt[0]
if !strings.HasPrefix(txt, "{") {
c.Log("Unexpected result for _status.pgeodns", txt)
c.Fail()
}
n := "baR.test.eXAmPLe.cOM."
r = exchange(c, n, dns.TypeA)
ip := r.Answer[0].(*dns.A).A
c.Check(ip.String(), Equals, "192.168.1.2")
c.Check(r.Answer[0].Header().Name, Equals, n)
}
func (s *ServeSuite) TestCname(c *C) {
// Cname, two possible results
results := make(map[string]int)
for i := 0; i < 10; i++ {
r := exchange(c, "www.se.test.example.com.", dns.TypeA)
// only return one CNAME even if there are multiple options
c.Check(r.Answer, HasLen, 1)
target := r.Answer[0].(*dns.CNAME).Target
results[target]++
}
// Two possible results from this cname
c.Check(results, HasLen, 2)
}
func (s *ServeSuite) TestUnknownDomain(c *C) {
r := exchange(c, "no.such.domain.", dns.TypeAAAA)
c.Assert(r.Rcode, Equals, dns.RcodeRefused)
}
func (s *ServeSuite) TestServingAliases(c *C) {
// Alias, no geo matches
r := exchange(c, "bar-alias.test.example.com.", dns.TypeA)
ip := r.Answer[0].(*dns.A).A
c.Check(ip.String(), Equals, "192.168.1.2")
// Alias to a cname record
r = exchange(c, "www-alias.test.example.com.", dns.TypeA)
c.Check(r.Answer[0].(*dns.CNAME).Target, Equals, "geo.bitnames.com.")
// Alias returning a cname, with geo overrides
r = exchangeSubnet(c, "www-alias.test.example.com.", dns.TypeA, "194.239.134.1")
c.Check(r.Answer, HasLen, 1)
if len(r.Answer) > 0 {
c.Check(r.Answer[0].(*dns.CNAME).Target, Equals, "geo-europe.bitnames.com.")
}
// Alias to Ns records
r = exchange(c, "sub-alias.test.example.org.", dns.TypeNS)
c.Check(r.Answer[0].(*dns.NS).Ns, Equals, "ns1.example.com.")
}
func (s *ServeSuite) TestServingEDNS(c *C) {
// MX test
r := exchangeSubnet(c, "test.example.com.", dns.TypeMX, "194.239.134.1")
c.Check(r.Answer, HasLen, 1)
if len(r.Answer) > 0 {
c.Check(r.Answer[0].(*dns.MX).Mx, Equals, "mx-eu.example.net.")
}
c.Log("Testing www.test.example.com from .dk, should match www.europe (a cname)")
r = exchangeSubnet(c, "www.test.example.com.", dns.TypeA, "194.239.134.0")
// www.test from .dk IP address gets at least one answer
c.Check(r.Answer, HasLen, 1)
if len(r.Answer) > 0 {
// EDNS-SUBNET test (request A, respond CNAME)
c.Check(r.Answer[0].(*dns.CNAME).Target, Equals, "geo-europe.bitnames.com.")
}
}
func (s *ServeSuite) TestServeRace(c *C) {
wg := sync.WaitGroup{}
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
s.TestServing(c)
wg.Done()
}()
}
wg.Wait()
}
func (s *ServeSuite) BenchmarkServing(c *C) {
for i := 0; i < c.N; i++ {
exchange(c, "_country.foo.pgeodns.", dns.TypeTXT)
}
}
func exchangeSubnet(c *C, name string, dnstype uint16, ip string) *dns.Msg {
msg := new(dns.Msg)
msg.SetQuestion(name, dnstype)
o := new(dns.OPT)
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
e := new(dns.EDNS0_SUBNET)
e.Code = dns.EDNS0SUBNET
e.SourceScope = 0
e.Address = net.ParseIP(ip)
e.Family = 1 // IP4
e.SourceNetmask = net.IPv4len * 8
o.Option = append(o.Option, e)
msg.Extra = append(msg.Extra, o)
c.Log("msg", msg)
return dorequest(c, msg)
}
func exchange(c *C, name string, dnstype uint16) *dns.Msg {
msg := new(dns.Msg)
msg.SetQuestion(name, dnstype)
return dorequest(c, msg)
}
func dorequest(c *C, msg *dns.Msg) *dns.Msg {
cli := new(dns.Client)
// cli.ReadTimeout = 2 * time.Second
r, _, err := cli.Exchange(msg, "127.0.0.1"+PORT)
if err != nil {
c.Logf("request err '%s': %s", msg.String(), err)
c.Fail()
}
return r
}