-
Notifications
You must be signed in to change notification settings - Fork 62
/
cache_test.go
214 lines (187 loc) · 5.13 KB
/
cache_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
package rdns
import (
"net"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestCache(t *testing.T) {
var ci ClientInfo
q := new(dns.Msg)
answerTTL := uint32(3600)
r := &TestResolver{
ResolveFunc: func(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
a := new(dns.Msg)
a.SetReply(q)
a.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{
Name: q.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: answerTTL,
},
A: net.IP{127, 0, 0, 1},
},
}
return a, nil
},
}
opt := CacheOptions{
GCPeriod: time.Minute,
}
c := NewCache("test-cache", r, opt)
// First query should be a cache-miss and be passed on to the upstream resolver
q.SetQuestion("example.com.", dns.TypeA)
a, err := c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
require.Equal(t, uint32(3600), a.Answer[0].Header().Ttl)
time.Sleep(time.Second)
// Second one should come from the cache and should have a lower TTL
a, err = c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
require.True(t, a.Answer[0].Header().Ttl < answerTTL)
// Different question should go through to upstream again, low TTL
answerTTL = 1
q.SetQuestion("example2.com.", dns.TypeA)
a, err = c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 2, r.HitCount())
require.Equal(t, answerTTL, a.Answer[0].Header().Ttl)
time.Sleep(time.Second)
// TTL should have expired now, so this should be a cache-miss and be sent upstream
q.SetQuestion("example2.com.", dns.TypeA)
_, err = c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 3, r.HitCount())
}
func TestCacheNXDOMAIN(t *testing.T) {
var ci ClientInfo
q := new(dns.Msg)
r := &TestResolver{
ResolveFunc: func(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
a := new(dns.Msg)
a.SetReply(q)
a.SetRcode(q, dns.RcodeNameError)
return a, nil
},
}
opt := CacheOptions{
GCPeriod: time.Minute,
}
c := NewCache("test-cache", r, opt)
// First query should be a cache-miss and be passed on to the upstream resolver
// Since it's an NXDOMAIN it should end up in the cache as well, with default TTL
q.SetQuestion("example.com.", dns.TypeA)
_, err := c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
// Second one should be returned from the cache
_, err = c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
}
func TestCacheHardenBelowNXDOMAIN(t *testing.T) {
var ci ClientInfo
q := new(dns.Msg)
r := &TestResolver{
ResolveFunc: func(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
a := new(dns.Msg)
a.SetReply(q)
a.SetRcode(q, dns.RcodeNameError)
return a, nil
},
}
opt := CacheOptions{
GCPeriod: time.Minute,
HardenBelowNXDOMAIN: true,
}
c := NewCache("test-cache", r, opt)
// Cache an NXDOMAIN for the parent domain
q.SetQuestion("example.com.", dns.TypeA)
_, err := c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
// A sub-domain query should also return NXDOMAIN based on the cached
// record for the parent if HardenBelowNXDOMAIN is enabled.
q.SetQuestion("not.exist.example.com.", dns.TypeA)
a, err := c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
require.Equal(t, dns.RcodeNameError, a.Rcode)
}
func TestRoundRobinShuffle(t *testing.T) {
msg := &dns.Msg{
Question: []dns.Question{
{Name: "example.com."},
},
Answer: []dns.RR{
&dns.CNAME{
Hdr: dns.RR_Header{
Name: "test",
Rrtype: dns.TypeCNAME,
Class: dns.ClassINET,
},
Target: "test",
},
&dns.A{
Hdr: dns.RR_Header{
Name: "test",
Rrtype: dns.TypeA,
Class: dns.ClassINET,
},
A: net.IP{0, 0, 0, 1},
},
&dns.A{
Hdr: dns.RR_Header{
Name: "test",
Rrtype: dns.TypeA,
Class: dns.ClassINET,
},
A: net.IP{0, 0, 0, 2},
},
},
}
// Shift the A records once
msg1 := msg.Copy()
AnswerShuffleRoundRobin(msg1)
require.Equal(t, dns.TypeCNAME, msg.Answer[0].Header().Rrtype)
require.Equal(t, dns.TypeA, msg.Answer[1].Header().Rrtype)
require.Equal(t, dns.TypeA, msg.Answer[2].Header().Rrtype)
a1 := msg1.Answer[1].(*dns.A)
a2 := msg1.Answer[2].(*dns.A)
require.Equal(t, net.IP{0, 0, 0, 2}, a1.A)
require.Equal(t, net.IP{0, 0, 0, 1}, a2.A)
// Shift the A records again
msg2 := msg.Copy()
AnswerShuffleRoundRobin(msg2)
a1 = msg2.Answer[1].(*dns.A)
a2 = msg2.Answer[2].(*dns.A)
require.Equal(t, net.IP{0, 0, 0, 1}, a1.A)
require.Equal(t, net.IP{0, 0, 0, 2}, a2.A)
}
// Truncated responses should not be cached
func TestCacheNoTruncated(t *testing.T) {
var ci ClientInfo
q := new(dns.Msg)
r := &TestResolver{
ResolveFunc: func(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
a := new(dns.Msg)
a.SetReply(q)
a.Truncated = true
return a, nil
},
}
c := NewCache("test-cache", r, CacheOptions{})
// Both queries should hit the upstream resolver
q.SetQuestion("example.com.", dns.TypeA)
_, err := c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r.HitCount())
_, err = c.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 2, r.HitCount())
}