-
Notifications
You must be signed in to change notification settings - Fork 62
/
padding_test.go
122 lines (108 loc) · 2.7 KB
/
padding_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
package rdns
import (
"testing"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestAnswerPadding(t *testing.T) {
q := new(dns.Msg)
q.SetQuestion("google.com.", dns.TypeA)
a := new(dns.Msg)
a.SetReply(q)
// No EDNS0 in the query, there should be none in the answer either
padAnswer(q, a)
edns0 := a.IsEdns0()
require.Nil(t, edns0, "unexpected EDNS0 option in response")
// With EDNS0 in the query now, should see padding in the response
q.SetEdns0(4096, false)
a.SetReply(q)
padAnswer(q, a)
edns0 = a.IsEdns0()
require.NotNil(t, edns0, "missing EDNS0 in response")
require.Zero(t, a.Len()%ResponsePaddingBlockSize, "response not padded to the correct length")
// Use padding on packet that needs to be smaller than the usual padded size
maxSize := ResponsePaddingBlockSize - 10
q.SetEdns0(uint16(maxSize), false)
a.SetReply(q)
padAnswer(q, a)
edns0 = a.IsEdns0()
require.NotNil(t, edns0, "missing EDNS0 in response")
require.Equal(t, maxSize, a.Len(), "not padded to the correct length")
}
func TestQueryPadding(t *testing.T) {
q := new(dns.Msg)
q.SetQuestion("google.com.", dns.TypeA)
// No padding should be added when there's no EDNS0 in the query
padQuery(q)
edns0 := q.IsEdns0()
require.Nil(t, edns0, "unexpected EDNS0 option in query")
// Now with EDNS0, the query should be padded to the right size
q.SetEdns0(4096, false)
padQuery(q)
edns0 = q.IsEdns0()
require.NotNil(t, edns0, "missing EDNS0 in query")
require.Zero(t, q.Len()%QueryPaddingBlockSize, "query not padded to the correct length")
}
func TestStripPadding(t *testing.T) {
q := new(dns.Msg)
q.SetQuestion("google.com.", dns.TypeA)
q.SetEdns0(4096, false)
len1 := q.Len()
padQuery(q)
stripPadding(q)
len2 := q.Len()
require.Equal(t, len1, len2, "padding not stripped off correctly")
}
func TestStripPaddingMulti(t *testing.T) {
q := new(dns.Msg)
q.SetQuestion("google.com.", dns.TypeA)
q.SetEdns0(4096, false)
edns0 := q.IsEdns0()
tests := []struct {
opts []dns.EDNS0
lenAfterStrip int
}{
{
opts: nil,
lenAfterStrip: 0,
},
{
opts: []dns.EDNS0{},
lenAfterStrip: 0,
},
{
opts: []dns.EDNS0{
&dns.EDNS0_PADDING{},
},
lenAfterStrip: 0,
},
{
opts: []dns.EDNS0{
&dns.EDNS0_PADDING{},
&dns.EDNS0_PADDING{},
},
lenAfterStrip: 0,
},
{
opts: []dns.EDNS0{
&dns.EDNS0_PADDING{},
&dns.EDNS0_PADDING{},
&dns.EDNS0_NSID{},
},
lenAfterStrip: 1,
},
{
opts: []dns.EDNS0{
&dns.EDNS0_NSID{},
&dns.EDNS0_PADDING{},
&dns.EDNS0_PADDING{},
},
lenAfterStrip: 1,
},
}
for _, test := range tests {
edns0.Option = test.opts
stripPadding(q)
require.Len(t, edns0.Option, test.lenAfterStrip)
}
}