-
Notifications
You must be signed in to change notification settings - Fork 15
/
server_test.go
233 lines (212 loc) · 6.13 KB
/
server_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
package ens
import (
"testing"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
type Record struct {
domain string
class uint16
resource uint16
record string
}
type Zone struct {
name string
records []Record
}
// Mock server
type MockServer struct {
zones []Zone
}
var server = MockServer{
zones: []Zone{
{name: "example.com.", records: []Record{
{"example.com.", dns.ClassINET, dns.TypeSOA, "example.com. 3600 IN SOA ns1.example.com. hostmaster.example.com. 2 19762 1800 1814400 14400"},
{"example.com.", dns.ClassINET, dns.TypeNS, "example.com. 3600 IN NS ns1.example.com."},
{"example.com.", dns.ClassINET, dns.TypeNS, "example.com. 3600 IN NS ns2.example.com."},
{"www.example.com.", dns.ClassINET, dns.TypeCNAME, "www.example.com. 3600 IN CNAME example.com."},
{"ns1.example.com.", dns.ClassINET, dns.TypeA, "ns1.example.com. 3600 IN A 1.1.1.1"},
{"ns2.example.com.", dns.ClassINET, dns.TypeA, "ns2.example.com. 3600 IN A 1.1.1.2"},
{"example.com.", dns.ClassINET, dns.TypeA, "example.com. 3600 IN A 1.1.2.1"},
{"*.example.com.", dns.ClassINET, dns.TypeA, "*.example.com. 3600 IN A 1.1.2.2"},
{"foo.example.com.", dns.ClassINET, dns.TypeDNAME, "foo.example.com. 3600 IN DNAME bar.example.com."},
{"bar.example.com.", dns.ClassINET, dns.TypeA, "bar.example.com. 3600 IN A 1.1.2.3"},
{"foo.bar.example.com.", dns.ClassINET, dns.TypeA, "foo.bar.example.com. 3600 IN A 1.1.2.4"},
}},
{name: "example.net.", records: []Record{}},
{name: "mine.", records: []Record{}},
},
}
func (m MockServer) IsAuthoritative(qname string) bool {
for _, zone := range m.zones {
if zone.name == qname {
return true
}
}
return false
}
func (m MockServer) Query(zone string, domain string, qtype uint16, do bool) ([]dns.RR, error) {
results := make([]dns.RR, 0)
for _, serverZone := range m.zones {
if serverZone.name == zone {
for _, serverRecord := range serverZone.records {
if serverRecord.domain == domain && serverRecord.resource == qtype {
rr, err := dns.NewRR(serverRecord.record)
if err != nil {
return nil, err
}
results = append(results, rr)
}
}
break
}
}
return results, nil
}
func (m MockServer) NumRecords(zone string, domain string) (uint16, error) {
records := uint16(0)
for _, serverZone := range m.zones {
if serverZone.name == zone {
for _, serverRecord := range serverZone.records {
if serverRecord.domain == domain {
records++
}
}
break
}
}
return records, nil
}
func (m MockServer) HasRecords(zone string, domain string) (bool, error) {
numRecords, err := m.NumRecords(zone, domain)
if err != nil {
return false, err
}
return numRecords > 0, nil
}
// Helper to set up test records
func newRR(input string) dns.RR {
rr, _ := dns.NewRR(input)
return rr
}
func TestQuery(t *testing.T) {
tests := []struct {
zone string
domain string
resource uint16
do bool
rrs []dns.RR
}{
{"example.com.", "example.com.", dns.TypeNS, false, []dns.RR{
newRR("example.com. 3600 IN NS ns1.example.com."),
newRR("example.com. 3600 IN NS ns2.example.com."),
}},
{"example.net.", "example.net.", dns.TypeNS, false, []dns.RR{}},
}
for i, tt := range tests {
rrs, err := server.Query(tt.zone, tt.domain, tt.resource, tt.do)
if err != nil {
t.Errorf("Test %d errored unexpectedly\n", i)
}
if len(rrs) != len(tt.rrs) {
t.Errorf("Test %d returned %d records (expected %d)\n", i, len(rrs), len(tt.rrs))
}
}
}
var exampleComAuth = []dns.RR{
test.NS("example.com. 3600 IN NS ns1.example.com."),
test.NS("example.com. 3600 IN NS ns2.example.com."),
}
func TestLookup(t *testing.T) {
tests := []test.Case{
{
Qname: "example.com.", Qtype: dns.TypeSOA,
Answer: []dns.RR{
test.SOA("example.com. 3600 IN SOA ns1.example.com. hostmaster.example.com. 2 19762 1800 1814400 14400"),
},
Ns: []dns.RR{
test.NS("example.com. 3600 IN NS ns1.example.com."),
test.NS("example.com. 3600 IN NS ns2.example.com."),
},
},
{
Qname: "example.com.", Qtype: dns.TypeNS,
Answer: []dns.RR{
test.NS("example.com. 3600 IN NS ns1.example.com."),
test.NS("example.com. 3600 IN NS ns2.example.com."),
},
Extra: []dns.RR{
test.A("ns1.example.com. 3600 IN A 1.1.1.1"),
test.A("ns2.example.com. 3600 IN A 1.1.1.2"),
},
},
{
Qname: "example.com.", Qtype: dns.TypeA,
Answer: []dns.RR{
test.A("example.com. 3600 IN A 1.1.2.1"),
},
Ns: exampleComAuth,
},
{
Qname: "www.example.com.", Qtype: dns.TypeA,
Answer: []dns.RR{
test.A("example.com. 3600 IN A 1.1.2.1"),
test.CNAME("www.example.com. 3600 IN CNAME example.com."),
},
Ns: exampleComAuth,
},
{
Qname: "wildcard.example.com.", Qtype: dns.TypeA,
Answer: []dns.RR{
test.A("wildcard.example.com. 3600 IN A 1.1.2.2"),
},
Ns: exampleComAuth,
},
{
Qname: "foo.foo.example.com.", Qtype: dns.TypeA,
Answer: []dns.RR{
test.A("foo.bar.example.com. 3600 IN A 1.1.2.4"),
test.DNAME("foo.example.com. 3600 IN DNAME bar.example.com."),
test.CNAME("foo.foo.example.com. 3600 IN CNAME foo.bar.example.com."),
},
Ns: exampleComAuth,
},
}
for _, tc := range tests {
r := tc.Msg()
rec := dnstest.NewRecorder(&test.ResponseWriter{})
state := request.Request{W: rec, Req: r}
a := new(dns.Msg)
a.SetReply(r)
a.Compress = true
a.Authoritative = true
a.Answer, a.Ns, a.Extra, _ = Lookup(server, state)
state.SizeAndDo(a)
rec.WriteMsg(a)
resp := rec.Msg
test.SortAndCheck(resp, tc)
}
}
func TestAuthoritativeDomain(t *testing.T) {
tests := []struct {
name string
result string
}{
{"", ""},
{".", ""},
{"example.com.", "example.com."},
{"sub.example.com.", "example.com."},
{"foo.com.", ""},
{"check.", ""},
{"mine.", "mine."},
{"my.mine.", "mine."},
}
for _, tt := range tests {
result := lowestAuthoritativeDomain(server, tt.name)
if tt.result != result {
t.Errorf("Failure: %v => %v (expected %v)\n", tt.name, result, tt.result)
}
}
}