-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
response.go
352 lines (304 loc) · 8.93 KB
/
response.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package mockhelp
import (
"bytes"
"errors"
"fmt"
"strings"
"github.com/miekg/dns"
"golang.org/x/exp/maps"
)
func NewMatcherResponse(settings MatcherResponseSettings) *MatcherResponse {
err := settings.validate()
if err != nil {
panic(err)
}
hasAnswerTypes := make(map[uint16]struct{})
for _, answerType := range settings.OnlyHasAnswerTypes {
hasAnswerTypes[answerType] = struct{}{}
}
ignoreAnswerTypes := make(map[uint16]struct{})
for _, answerType := range settings.IgnoreAnswerTypes {
ignoreAnswerTypes[answerType] = struct{}{}
}
return &MatcherResponse{
response: settings.Response.Copy(),
onlyHasAnswerTypes: hasAnswerTypes,
ignoreAnswerTypes: ignoreAnswerTypes,
}
}
type MatcherResponseSettings struct {
// Response is the base expected response.
// By default, the following are ignored:
// - MsgHdr.Id field
// - TTL field set in the answer headers
// - Rdlength field set in the answer headers
// - A and AAAA IP values, except checking both are empty or both non empty.
// - Extra EDNS0 Padding length.
Response *dns.Msg
// OnlyHasAnswerTypes checks the response has at least one answer
// for each of the types specified and no other answer types.
OnlyHasAnswerTypes []uint16
// IgnoreAnswerTypes removes the answers matching one of the types
// specified from the received AND expected answers.
IgnoreAnswerTypes []uint16
}
var (
errResponseNotSet = errors.New("response is not set")
errAnswerTypeNotValid = errors.New("answer type is not valid")
)
func (m MatcherResponseSettings) validate() (err error) {
if m.Response == nil {
return fmt.Errorf("%w", errResponseNotSet)
}
for _, answerType := range m.OnlyHasAnswerTypes {
_, ok := dns.TypeToString[answerType]
if !ok {
return fmt.Errorf("only has answer types: %w: %d",
errAnswerTypeNotValid, answerType)
}
}
for _, answerType := range m.IgnoreAnswerTypes {
_, ok := dns.TypeToString[answerType]
if !ok {
return fmt.Errorf("ignore answer types: %w: %d",
errAnswerTypeNotValid, answerType)
}
}
return nil
}
// MatcherResponse matches a message with a response
// and ignores the following:
// - MsgHdr.Id field
// - TTL field set in the answer headers
// - Rdlength field set in the answer headers
// - A and AAAA IP values, except checking both are empty or both non empty.
// - Extra EDNS0 Padding length.
// See MatcherResponseSettings for additional settings.
type MatcherResponse struct {
mismatchReason string
response *dns.Msg
onlyHasAnswerTypes map[uint16]struct{}
ignoreAnswerTypes map[uint16]struct{}
}
func (m *MatcherResponse) String() string {
return m.mismatchReason
}
func (m *MatcherResponse) Matches(x interface{}) bool {
msg, ok := x.(*dns.Msg)
if !ok {
m.mismatchReason = "not a *dns.Msg"
return false
}
// Copy since these are mutated by this function
received := msg.Copy()
expected := m.response.Copy()
m.mismatchReason = checkOnlyHasAnswers(received, m.onlyHasAnswerTypes)
if m.mismatchReason != "" {
return false
}
filterAnswers(expected, m.ignoreAnswerTypes)
filterAnswers(received, m.ignoreAnswerTypes)
m.mismatchReason = checkAnswers(expected, received)
if m.mismatchReason != "" {
return false
}
m.mismatchReason = checkExtras(expected, received)
if m.mismatchReason != "" {
return false
}
expectedPacked, _ := expected.Pack()
receivedPacked, _ := received.Pack()
match := bytes.Equal(expectedPacked, receivedPacked)
if !match {
m.mismatchReason = fmt.Sprintf("packed mismatch:\n"+
"==> expected: %x\n==> received: %x\n",
expectedPacked, receivedPacked)
}
return match
}
func checkOnlyHasAnswers(response *dns.Msg,
onlyHasAnswerTypes map[uint16]struct{}) (
mismatchReason string) {
answerTypesNotPresent := maps.Clone(onlyHasAnswerTypes)
for _, answer := range response.Answer {
answerType := answer.Header().Rrtype
_, expectedType := onlyHasAnswerTypes[answerType]
if !expectedType {
return fmt.Sprintf("unexpected answer type: %s",
dns.TypeToString[answerType])
}
delete(answerTypesNotPresent, answerType)
}
if len(answerTypesNotPresent) == 0 {
return ""
}
answerTypesMissing := make([]string, 0, len(answerTypesNotPresent))
for answerType := range answerTypesNotPresent {
answerTypesMissing = append(answerTypesMissing,
dns.TypeToString[answerType])
}
return fmt.Sprintf("missing answer types: %s",
strings.Join(answerTypesMissing, ", "))
}
func filterAnswers(response *dns.Msg,
ignoreAnswerTypes map[uint16]struct{}) {
filteredAnswers := make([]dns.RR, 0, len(response.Answer))
for _, answer := range response.Answer {
answerType := answer.Header().Rrtype
_, ignored := ignoreAnswerTypes[answerType]
if ignored {
continue
}
filteredAnswers = append(filteredAnswers, answer)
}
response.Answer = filteredAnswers
}
func checkAnswers(expected, received *dns.Msg) (
mismatchReason string) {
if len(received.Answer) != len(expected.Answer) {
return fmt.Sprintf("answers count mismatch: "+
"expected %d, received %d",
len(expected.Answer), len(received.Answer))
}
// Clear randomly set fields
received.MsgHdr.Id = 0
expected.MsgHdr.Id = 0
for i := range received.Answer {
if !answersAreEqual(expected.Answer[i], received.Answer[i]) {
return fmt.Sprintf("answer %d of %d mismatch:\n"+
"==> expected: %s\nreceived: %s\n",
i+1, len(expected.Answer), expected.Answer[i], received.Answer[i])
}
}
return ""
}
func checkExtras(expected, received *dns.Msg) (
mismatchReason string) {
if len(received.Extra) != len(expected.Extra) {
return fmt.Sprintf("extra count mismatch: "+
"expected %d, received %d",
len(expected.Extra), len(received.Extra))
}
for i := range received.Extra {
if !extrasAreEqual(expected.Extra[i], received.Extra[i]) {
return fmt.Sprintf("extra %d of %d mismatch:\n"+
"==> expected: %s\n==> received: %s",
i+1, len(expected.Extra), expected.Extra[i], received.Extra[i])
}
}
return ""
}
func answersAreEqual(expected, actual dns.RR) (equal bool) {
receivedHeader := actual.Header()
expectedHeader := expected.Header()
if receivedHeader.Rrtype != expectedHeader.Rrtype {
return false
}
receivedHeader.Ttl, expectedHeader.Ttl = 0, 0
receivedHeader.Rdlength, expectedHeader.Rdlength = 0, 0
switch receivedHeader.Rrtype {
case dns.TypeA:
return answersAEqual(expected, actual)
case dns.TypeAAAA:
return answersAAAAEqual(expected, actual)
default:
panic(fmt.Sprintf("unexpected answer type: %s",
dns.TypeToString[receivedHeader.Rrtype]))
}
}
func answersAEqual(expected, actual dns.RR) (equal bool) {
receivedAnswer, ok := actual.(*dns.A)
if !ok {
return false
}
expectedAnswer, ok := expected.(*dns.A)
if !ok {
return false
}
// Only check the length
receivedIP := receivedAnswer.A
expectedIP := expectedAnswer.A
if len(expectedIP) == 0 && len(receivedIP) > 0 {
return false
} else if len(expectedIP) > 0 && len(receivedIP) == 0 {
return false
}
// Clear values for packed bytes comparison in caller function
receivedAnswer.A = nil
expectedAnswer.A = nil
return true
}
func answersAAAAEqual(expected, actual dns.RR) (equal bool) {
receivedAnswer, ok := actual.(*dns.AAAA)
if !ok {
return false
}
expectedAnswer, ok := expected.(*dns.AAAA)
if !ok {
return false
}
// Only check the length
receivedIP := receivedAnswer.AAAA
expectedIP := expectedAnswer.AAAA
if len(expectedIP) == 0 && len(receivedIP) > 0 {
return false
} else if len(expectedIP) > 0 && len(receivedIP) == 0 {
return false
}
// Clear values for packed bytes comparison in caller function
receivedAnswer.AAAA = nil
expectedAnswer.AAAA = nil
return true
}
func extrasAreEqual(expected, actual dns.RR) (equal bool) {
receivedHeader := actual.Header()
expectedHeader := expected.Header()
if receivedHeader.Rrtype != expectedHeader.Rrtype {
return false
}
switch receivedHeader.Rrtype {
case dns.TypeOPT:
return extrasOPTEqual(expected, actual)
default:
panic(fmt.Sprintf("unexpected extra type: %s",
dns.TypeToString[receivedHeader.Rrtype]))
}
}
func extrasOPTEqual(expected, actual dns.RR) (equal bool) {
receivedExtra, ok := actual.(*dns.OPT)
if !ok {
return false
}
expectedExtra, ok := expected.(*dns.OPT)
if !ok {
return false
}
if len(expectedExtra.Option) != len(receivedExtra.Option) {
return false
}
for i, expectedOption := range expectedExtra.Option {
receivedOption := receivedExtra.Option[i]
switch expectedOption.Option() {
case dns.EDNS0PADDING:
return edns0PaddingEqual(expectedOption, receivedOption)
default:
panic(fmt.Sprintf("unexpected option type: %T", expectedOption))
}
}
return true
}
func edns0PaddingEqual(expected, actual dns.EDNS0) (equal bool) {
receivedPadding, ok := actual.(*dns.EDNS0_PADDING)
if !ok {
return false
}
expectedPadding, ok := expected.(*dns.EDNS0_PADDING)
if !ok {
return false
}
// Ignore the EDNS0 Padding which can change depending on the
// rest of the message due to compression + encryption.
expectedPadding.Padding = []byte{0}
receivedPadding.Padding = []byte{0}
return true
}