···
"github.com/stretchr/testify/assert"
func TestMessageDecode(t *testing.T) {
11
-
0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65,
12
-
0x77, 0x73, 0x0b, 0x79, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x03,
13
-
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00,
14
-
0x00, 0x00, 0x01, 0x00, 0x04, 0xd1, 0xd8, 0xe6, 0xcf,
20
+
name: "Valid DNS message with one question",
21
+
input: func() []byte {
22
+
buf := new(bytes.Buffer)
23
+
binary.Write(buf, binary.BigEndian, uint16(1234))
24
+
binary.Write(buf, binary.BigEndian, uint16(0x0100))
25
+
binary.Write(buf, binary.BigEndian, uint16(1))
26
+
binary.Write(buf, binary.BigEndian, uint16(0))
27
+
binary.Write(buf, binary.BigEndian, uint16(0))
28
+
binary.Write(buf, binary.BigEndian, uint16(0))
29
+
buf.Write([]byte{3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0})
30
+
binary.Write(buf, binary.BigEndian, uint16(1))
31
+
binary.Write(buf, binary.BigEndian, uint16(1))
42
+
Question: []Question{
44
+
QName: "www.example.com",
53
+
name: "Valid DNS message with one answer",
54
+
input: func() []byte {
55
+
buf := new(bytes.Buffer)
56
+
binary.Write(buf, binary.BigEndian, uint16(5678))
57
+
binary.Write(buf, binary.BigEndian, uint16(0x8180))
58
+
binary.Write(buf, binary.BigEndian, uint16(0))
59
+
binary.Write(buf, binary.BigEndian, uint16(1))
60
+
binary.Write(buf, binary.BigEndian, uint16(0))
61
+
binary.Write(buf, binary.BigEndian, uint16(0))
62
+
buf.Write([]byte{3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0})
63
+
binary.Write(buf, binary.BigEndian, uint16(1))
64
+
binary.Write(buf, binary.BigEndian, uint16(1))
65
+
binary.Write(buf, binary.BigEndian, uint32(3600))
66
+
binary.Write(buf, binary.BigEndian, uint16(4))
67
+
binary.Write(buf, binary.BigEndian, uint32(0x0A000001))
81
+
Answer: []ResourceRecord{
83
+
Name: "www.example.com",
88
+
RData: &A{net.IP([]byte{10, 0, 0, 1})},
95
+
name: "Invalid input - empty buffer",
19
-
assert.Equal(t, uint16(0x8e19), msg.Header.ID)
20
-
assert.Equal(t, true, msg.Header.QR)
21
-
assert.Equal(t, OPCode(0), msg.Header.OPCode)
22
-
assert.Equal(t, false, msg.Header.AA)
23
-
assert.Equal(t, false, msg.Header.TC)
24
-
assert.Equal(t, true, msg.Header.RD)
25
-
assert.Equal(t, true, msg.Header.RA)
26
-
assert.Equal(t, uint8(0), msg.Header.Z)
27
-
assert.Equal(t, RCode(0), msg.Header.RCode)
28
-
assert.Equal(t, uint16(1), msg.Header.QDCount)
29
-
assert.Equal(t, uint16(1), msg.Header.ANCount)
30
-
assert.Equal(t, uint16(0), msg.Header.NSCount)
31
-
assert.Equal(t, uint16(0), msg.Header.ARCount)
101
+
for _, tt := range tests {
102
+
t.Run(tt.name, func(t *testing.T) {
104
+
err := m.Decode(tt.input)
33
-
assert.Equal(t, 1, len(msg.Question))
35
-
question := msg.Question[0]
36
-
assert.Equal(t, "news.ycombinator.com", question.QName)
37
-
assert.Equal(t, DNSType(1), question.QType)
38
-
assert.Equal(t, DNSClass(1), question.QClass)
40
-
assert.Equal(t, 1, len(msg.Answer))
41
-
answer := msg.Answer[0]
42
-
assert.Equal(t, answer.Name, "news.ycombinator.com")
43
-
assert.Equal(t, DNSType(1), answer.RType)
44
-
assert.Equal(t, DNSClass(1), answer.RClass)
45
-
assert.Equal(t, uint32(1), answer.TTL)
46
-
assert.Equal(t, uint16(4), answer.RDLength)
49
-
func TestMessageDecodeWithU14Offset(t *testing.T) {
51
-
0x0e, 0xc3, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00,
52
-
0x00, 0x0d, 0x00, 0x0e, 0x06, 0x6e, 0x73, 0x2d,
53
-
0x33, 0x37, 0x32, 0x09, 0x61, 0x77, 0x73, 0x64,
54
-
0x6e, 0x73, 0x2d, 0x34, 0x36, 0x03, 0x63, 0x6f,
55
-
0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x1d,
56
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
57
-
0x00, 0x14, 0x01, 0x61, 0x0c, 0x67, 0x74, 0x6c,
58
-
0x64, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
59
-
0x73, 0x03, 0x6e, 0x65, 0x74, 0x00, 0xc0, 0x1d,
60
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
61
-
0x00, 0x04, 0x01, 0x62, 0xc0, 0x34, 0xc0, 0x1d,
62
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
63
-
0x00, 0x04, 0x01, 0x63, 0xc0, 0x34, 0xc0, 0x1d,
64
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
65
-
0x00, 0x04, 0x01, 0x64, 0xc0, 0x34, 0xc0, 0x1d,
66
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
67
-
0x00, 0x04, 0x01, 0x65, 0xc0, 0x34, 0xc0, 0x1d,
68
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
69
-
0x00, 0x04, 0x01, 0x66, 0xc0, 0x34, 0xc0, 0x1d,
70
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
71
-
0x00, 0x04, 0x01, 0x67, 0xc0, 0x34, 0xc0, 0x1d,
72
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
73
-
0x00, 0x04, 0x01, 0x68, 0xc0, 0x34, 0xc0, 0x1d,
74
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
75
-
0x00, 0x04, 0x01, 0x69, 0xc0, 0x34, 0xc0, 0x1d,
76
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
77
-
0x00, 0x04, 0x01, 0x6a, 0xc0, 0x34, 0xc0, 0x1d,
78
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
79
-
0x00, 0x04, 0x01, 0x6b, 0xc0, 0x34, 0xc0, 0x1d,
80
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
81
-
0x00, 0x04, 0x01, 0x6c, 0xc0, 0x34, 0xc0, 0x1d,
82
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
83
-
0x00, 0x04, 0x01, 0x6d, 0xc0, 0x34, 0xc0, 0x32,
84
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
85
-
0x00, 0x04, 0xc0, 0x05, 0x06, 0x1e, 0xc0, 0x52,
86
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
87
-
0x00, 0x04, 0xc0, 0x21, 0x0e, 0x1e, 0xc0, 0x62,
88
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
89
-
0x00, 0x04, 0xc0, 0x1a, 0x5c, 0x1e, 0xc0, 0x72,
90
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
91
-
0x00, 0x04, 0xc0, 0x1f, 0x50, 0x1e, 0xc0, 0x82,
92
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
93
-
0x00, 0x04, 0xc0, 0x0c, 0x5e, 0x1e, 0xc0, 0x92,
94
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
95
-
0x00, 0x04, 0xc0, 0x23, 0x33, 0x1e, 0xc0, 0xa2,
96
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
97
-
0x00, 0x04, 0xc0, 0x2a, 0x5d, 0x1e, 0xc0, 0xb2,
98
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
99
-
0x00, 0x04, 0xc0, 0x36, 0x70, 0x1e, 0xc0, 0xc2,
100
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
101
-
0x00, 0x04, 0xc0, 0x2b, 0xac, 0x1e, 0xc0, 0xd2,
102
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
103
-
0x00, 0x04, 0xc0, 0x30, 0x4f, 0x1e, 0xc0, 0xe2,
104
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
105
-
0x00, 0x04, 0xc0, 0x34, 0xb2, 0x1e, 0xc0, 0xf2,
106
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
107
-
0x00, 0x04, 0xc0, 0x29, 0xa2, 0x1e, 0xc1, 0x02,
108
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
109
-
0x00, 0x04, 0xc0, 0x37, 0x53, 0x1e, 0xc0, 0x32,
110
-
0x00, 0x1c, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
111
-
0x00, 0x10, 0x20, 0x01, 0x05, 0x03, 0xa8, 0x3e,
112
-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
107
+
assert.Error(t, err)
109
+
assert.NoError(t, err)
110
+
assert.Equal(t, tt.expected.Header, m.Header)
111
+
assert.Equal(t, tt.expected.Question, m.Question)
112
+
assert.Equal(t, tt.expected.Answer, m.Answer)
113
+
assert.Equal(t, tt.expected.Authority, m.Authority)
114
+
assert.Equal(t, tt.expected.Additional, m.Additional)
117
-
_ = msg.Decode(bytes)
118
-
// assert_no_error(t, err)
121
-
assert.Equal(t, uint16(0x0ec3), msg.Header.ID)
122
-
assert.Equal(t, true, msg.Header.QR)
123
-
assert.Equal(t, QUERY, msg.Header.OPCode)
124
-
assert.Equal(t, false, msg.Header.AA)
125
-
assert.Equal(t, false, msg.Header.TC)
126
-
assert.Equal(t, false, msg.Header.RD)
127
-
assert.Equal(t, false, msg.Header.RA)
128
-
assert.Equal(t, uint8(0), msg.Header.Z)
129
-
assert.Equal(t, NOERROR, msg.Header.RCode)
130
-
assert.Equal(t, uint16(1), msg.Header.QDCount)
131
-
assert.Equal(t, uint16(0), msg.Header.ANCount)
132
-
assert.Equal(t, uint16(13), msg.Header.NSCount)
133
-
assert.Equal(t, uint16(14), msg.Header.ARCount)
136
-
assert.Equal(t, 1, len(msg.Question))
137
-
question := msg.Question[0]
138
-
assert.Equal(t, "ns-372.awsdns-46.com", question.QName)
139
-
assert.Equal(t, AType, question.QType)
140
-
assert.Equal(t, IN, question.QClass)
142
-
assert.Equal(t, 13, len(msg.Authority))
143
-
assert.Equal(t, 14, len(msg.Additional))
146
-
func TestMessageEncode(t *testing.T) {
148
-
0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65,
149
-
0x77, 0x73, 0x0b, 0x79, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x03,
150
-
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00,
151
-
0x00, 0x00, 0x01, 0x00, 0x04, 0xd1, 0xd8, 0xe6, 0xcf,
155
-
err := msg.Decode(bytes)
156
-
assert.NoError(t, err)
158
-
actual := msg.Encode()
159
-
assert.Equal(t, bytes, actual)
162
-
func TestMessageEncode2(t *testing.T) {
164
-
0xfc, 0xa9, 0x81, 0x80, 0x00, 0x01, 0x00, 0x05,
165
-
0x00, 0x00, 0x00, 0x00, 0x03, 0x6f, 0x6c, 0x64,
166
-
0x06, 0x72, 0x65, 0x64, 0x64, 0x69, 0x74, 0x03,
167
-
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
168
-
0xc0, 0x0c, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00,
169
-
0x2a, 0x17, 0x00, 0x17, 0x06, 0x72, 0x65, 0x64,
170
-
0x64, 0x69, 0x74, 0x03, 0x6d, 0x61, 0x70, 0x06,
171
-
0x66, 0x61, 0x73, 0x74, 0x6c, 0x79, 0x03, 0x6e,
172
-
0x65, 0x74, 0x00, 0xc0, 0x2c, 0x00, 0x01, 0x00,
173
-
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x04, 0x97,
174
-
0x65, 0x01, 0x8c, 0xc0, 0x2c, 0x00, 0x01, 0x00,
175
-
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x04, 0x97,
176
-
0x65, 0xc1, 0x8c, 0xc0, 0x2c, 0x00, 0x01, 0x00,
177
-
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x04, 0x97,
178
-
0x65, 0x41, 0x8c, 0xc0, 0x2c, 0x00, 0x01, 0x00,
179
-
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x04, 0x97,
184
-
err := msg.Decode(bytes)
185
-
assert.NoError(t, err)
187
-
actual := msg.Encode()
188
-
assert.Equal(t, bytes, actual)
func FuzzDecodeMessage(f *testing.F) {
···
for _, tc := range testcases {
201
-
f.Add(tc) // Use f.Add to provide a seed corpus
f.Fuzz(func(t *testing.T, msg []byte) {