1package magna
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7)
8
9func TestMessageDecode(t *testing.T) {
10 bytes := []byte{
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,
15 }
16
17 var msg Message
18 msg.Decode(bytes)
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)
32
33 assert.Equal(t, 1, len(msg.Question))
34
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)
39
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)
47}
48
49func TestMessageDecodeWithU14Offset(t *testing.T) {
50 bytes := []byte{
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,
113 0x00, 0x30,
114 }
115
116 var msg Message
117 _ = msg.Decode(bytes)
118 // assert_no_error(t, err)
119
120 // Header Section
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)
134
135 // Query Section
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)
141
142 assert.Equal(t, 13, len(msg.Authority))
143 assert.Equal(t, 14, len(msg.Additional))
144}
145
146func TestMessageEncode(t *testing.T) {
147 bytes := []byte{
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,
152 }
153
154 var msg Message
155 err := msg.Decode(bytes)
156 assert.NoError(t, err)
157
158 actual := msg.Encode()
159 assert.Equal(t, bytes, actual)
160}
161
162func TestMessageEncode2(t *testing.T) {
163 bytes := []byte{
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,
180 0x65, 0x81, 0x8c,
181 }
182
183 var msg Message
184 err := msg.Decode(bytes)
185 assert.NoError(t, err)
186
187 actual := msg.Encode()
188 assert.Equal(t, bytes, actual)
189}
190
191func FuzzDecodeMessage(f *testing.F) {
192 testcases := [][]byte{
193 {
194 0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65,
195 0x77, 0x73, 0x0b, 0x79, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x03,
196 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00,
197 0x00, 0x00, 0x01, 0x00, 0x04, 0xd1, 0xd8, 0xe6, 0xcf,
198 },
199 }
200 for _, tc := range testcases {
201 f.Add(tc) // Use f.Add to provide a seed corpus
202 }
203 f.Fuzz(func(t *testing.T, msg []byte) {
204 var m Message
205 m.Decode(msg)
206 })
207}