Debian Codegraph @

msg_test.go

raw
1 package dns_test
2
3 import (
4 "bytes"
5 "context"
6 "encoding/binary"
7 "errors"
8 "fmt"
9 "io"
10 "log"
11 "net/netip"
12 "os"
13 "testing"
14 "time"
15
16 "codeberg.org/miekg/dns"
17 "codeberg.org/miekg/dns/dnstest"
18 "codeberg.org/miekg/dns/internal/bin"
19 "codeberg.org/miekg/dns/internal/dnsfuzz"
20 )
21
22 func ExampleMsg() {
23 m := dns.NewMsg("miek.nl.", dns.TypeMX)
24 c := new(dns.Client)
25 r, _, err := c.Exchange(context.TODO(), m, "udp", "127.0.0.1:53")
26 if err != nil {
27 log.Fatal(err)
28 }
29 if m, ok := r.Answer[0].(*dns.MX); ok {
30 fmt.Println(m.Mx)
31 }
32 if n, ok := r.Pseudo[0].(*dns.NSID); ok {
33 fmt.Println(n.Nsid)
34 }
35 for rr := range r.RRs() {
36 fmt.Println(rr)
37 }
38 }
39
40 func ExampleMsg_dNSSEC() {
41 m := dns.NewMsg("miek.nl.", dns.TypeMX)
42 m.UDPSize = dns.DefaultMsgSize
43 m.Security = true
44 dns.Exchange(context.TODO(), m, "udp", "127.0.0.1:53")
45 // handle returned message.
46 }
47
48 func TestMsgBinary(t *testing.T) {
49 testcases := []struct {
50 name string
51 buf []byte
52 fn func(*dns.Msg) error
53 }{
54 {
55 // m := dns.NewMsg("example.org.", dns.TypeMX)
56 // m.Answer = []dns.RR{dnstest.New("example.org. IN SOA linode.atoom.net. miek\\.miek.nl. 1 3600 3600 3600 3600")}
57 "soa-mbox",
58 []byte{148, 44, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 7, 101, 120, 97, 109, 112, 108, 101, 3, 111, 114, 103, 0, 0, 15, 0, 1, 192, 12, 0, 6, 0, 1, 0, 0, 14, 16, 0, 52, 6, 108, 105, 110, 111, 100, 101, 5, 97, 116, 111, 111, 109, 3, 110, 101, 116, 0, 9, 109, 105, 101, 107, 46, 109, 105, 101, 107, 2, 110, 108, 0, 0, 0, 0, 1, 0, 0, 14, 16, 0, 0, 14, 16, 0, 0, 14, 16, 0, 0, 14, 16},
59 func(m *dns.Msg) error {
60 if len(m.Answer) != 1 {
61 return errors.New("expected answer section")
62 }
63 s, ok := m.Answer[0].(*dns.SOA)
64 if !ok {
65 return errors.New("expected SOA")
66 }
67 if s.Mbox != `miek\.miek.nl.` {
68 return errors.New("SOA Mbox is not correct")
69 }
70 return nil
71 },
72 },
73
74 {
75 "edns0-subnet",
76 []byte{149, 112, 0, 16, 0, 1, 0, 0, 0, 0, 0, 1, 1, 97, 4, 109, 105, 69, 75, 2, 78, 76, 0, 0, 1, 0, 1, 0, 0, 41, 5, 120, 0, 0, 128, 0, 0, 11, 0, 8, 0, 7, 0, 1, 24, 0, 14, 128, 63},
77 func(m *dns.Msg) error {
78 if len(m.Pseudo) == 0 {
79 return errors.New("expected pseudo section")
80 }
81 s, ok := m.Pseudo[0].(*dns.SUBNET)
82 if !ok {
83 return errors.New("expected EDNS0 SUBNET")
84 }
85 const addr = "14.128.63.0"
86 if s.Address != netip.MustParseAddr(addr) {
87 return errors.New("expected address: " + addr)
88 }
89 return nil
90 },
91 },
92 {
93 "edns0-subnet",
94 []byte{255, 234, 0, 16, 0, 1, 0, 0, 0, 0, 0, 1, 7, 99, 111, 114, 101, 68, 110, 83, 2, 105, 111, 0, 0, 28, 0, 1, 0, 0, 41, 5, 120, 0, 0, 128, 0, 0, 11, 0, 8, 0, 7, 0, 1, 24, 0, 62, 212, 234},
95 func(m *dns.Msg) error {
96 if len(m.Pseudo) == 0 {
97 return errors.New("expected pseudo section")
98 }
99 s, ok := m.Pseudo[0].(*dns.SUBNET)
100 if !ok {
101 return errors.New("expected EDNS0 SUBNET")
102 }
103 const addr = "62.212.234.0"
104 if s.Address != netip.MustParseAddr(addr) {
105 return errors.New("expected address: " + addr)
106 }
107 return nil
108 },
109 },
110 {
111 "opt-and-tsig",
112 []byte{148, 7, 1, 32, 0, 1, 0, 0, 0, 0, 0, 2, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 6, 0, 1, 0, 0, 41, 4, 208, 0, 0, 0, 0, 0, 12, 0, 10, 0, 8, 73, 65, 52, 201, 253, 43, 171, 193, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 250, 0, 255, 0, 0, 0, 0, 0, 93, 11, 104, 109, 97, 99, 45, 115, 104, 97, 53, 49, 50, 0, 0, 0, 105, 104, 143, 225, 1, 44, 0, 64, 195, 169, 191, 31, 144, 147, 160, 197, 245, 76, 217, 137, 234, 208, 246, 112, 113, 12, 208, 172, 99, 181, 29, 108, 140, 62, 197, 130, 116, 207, 127, 178, 163, 16, 242, 203, 41, 135, 60, 218, 187, 237, 181, 106, 91, 34, 125, 38, 190, 56, 117, 43, 76, 212, 161, 165, 61, 214, 193, 180, 117, 1, 27, 129, 148, 7, 0, 0, 0, 0},
113 func(m *dns.Msg) error {
114 if len(m.Pseudo) == 0 {
115 return errors.New("expected pseudo section")
116 }
117 _, ok := m.Pseudo[len(m.Pseudo)-1].(*dns.TSIG)
118 if !ok {
119 return errors.New("expected TSIG")
120 }
121 return nil
122 },
123 },
124 {
125 "opt-and-tsig-extra-should-empty",
126 []byte{148, 7, 1, 32, 0, 1, 0, 0, 0, 0, 0, 2, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 6, 0, 1, 0, 0, 41, 4, 208, 0, 0, 0, 0, 0, 12, 0, 10, 0, 8, 73, 65, 52, 201, 253, 43, 171, 193, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 250, 0, 255, 0, 0, 0, 0, 0, 93, 11, 104, 109, 97, 99, 45, 115, 104, 97, 53, 49, 50, 0, 0, 0, 105, 104, 143, 225, 1, 44, 0, 64, 195, 169, 191, 31, 144, 147, 160, 197, 245, 76, 217, 137, 234, 208, 246, 112, 113, 12, 208, 172, 99, 181, 29, 108, 140, 62, 197, 130, 116, 207, 127, 178, 163, 16, 242, 203, 41, 135, 60, 218, 187, 237, 181, 106, 91, 34, 125, 38, 190, 56, 117, 43, 76, 212, 161, 165, 61, 214, 193, 180, 117, 1, 27, 129, 148, 7, 0, 0, 0, 0},
127 func(m *dns.Msg) error {
128 if len(m.Extra) != 0 {
129 return errors.New("expected additional section to be empty")
130 }
131 return nil
132 },
133 },
134 {
135 "unknown-edns0-code20",
136 // edns255 := &dns.ERFC3597{EDNS0Code: 255, Code: hex.EncodeToString([]byte("hallo"))}
137 // m.Pseudo = append(m.Pseudo, edns255)
138 []byte{0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 3, 119, 119, 119, 7, 101, 120, 97, 109, 112, 108, 101, 3, 111, 114, 103, 0, 0, 1, 0, 1, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 9, 0, 255, 0, 5, 104, 97, 108, 108, 111},
139 func(m *dns.Msg) error {
140 if len(m.Pseudo) != 1 {
141 return errors.New("expected pseudo section to carry an option")
142 }
143 x := m.Pseudo[0].(*dns.ERFC3597)
144 if x.EDNS0Code != 255 {
145 return fmt.Errorf("expected code 255, got %d", x.EDNS0Code)
146 }
147 return nil
148 },
149 },
150 {
151 "fuzzing-find-#932",
152 []byte{48, 48, 48, 48, 0, 2, 0, 2, 0, 0, 0, 0, 0, 48, 48, 48, 48, 0, 48, 48, 48, 48, 0, 0, 9, 48, 48, 48, 48, 48, 48, 0, 0, 192, 32, 48, 48, 48, 48, 48, 48, 48, 48, 0, 16, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48},
153 func(m *dns.Msg) error {
154 // the message is a bit fubar, but we did crash on it, when packing again.
155 m.Pack()
156 return nil
157 },
158 },
159 }
160 for _, tc := range testcases {
161 t.Run(tc.name, func(t *testing.T) {
162 m := &dns.Msg{Data: tc.buf}
163 if err := m.Unpack(); err != nil {
164 t.Logf("%v\n", bin.Dump(m.Data))
165 t.Fatal(err)
166 }
167 if err := tc.fn(m); err != nil {
168 t.Logf("%s\n", bin.Dump(m.Data))
169 t.Fatal(err)
170 }
171 })
172 }
173 }
174
175 func TestMsg(t *testing.T) {
176 const msgArcount = 10 // offset in the message where the Arcount is, 2 octets long.
177 a := new(dns.RFC3597)
178 if err := a.ToRFC3597(dnstest.New("example. IN A 192.0.2.1")); err != nil {
179 t.Fatal(err)
180 }
181 null := new(dns.RFC3597)
182 if err := null.ToRFC3597(&dns.NULL{Hdr: dns.Header{Name: "example.", Class: dns.ClassINET}}); err != nil {
183 t.Fatal(err)
184 }
185 testcases := []struct {
186 name string
187 makeFn func() *dns.Msg
188 testFn func(m *dns.Msg) error
189 }{
190 {
191 "extendedrcode",
192 func() *dns.Msg {
193 m := dns.NewMsg("example.org.", dns.TypeMX)
194 m.ID = 3
195 m.Rcode = dns.RcodeBadTime
196 return m
197 },
198 func(r *dns.Msg) error {
199 if r.Rcode != dns.RcodeBadTime {
200 return fmt.Errorf("expected %s, got %s", dns.RcodeToString[dns.RcodeBadTime], dns.RcodeToString[r.Rcode])
201 }
202 return nil
203 },
204 },
205 {
206 "security",
207 func() *dns.Msg { m := dns.NewMsg("example.org.", dns.TypeMX); m.ID = 3; m.Security = true; return m },
208 func(r *dns.Msg) error {
209 if !r.Security {
210 return fmt.Errorf("expected %t, got %t", r.Security, !r.Security)
211 }
212 arcount := binary.BigEndian.Uint16(r.Data[msgArcount:])
213 if arcount != 1 {
214 return fmt.Errorf("expected arcount to be 1, got %d", arcount)
215 }
216 return nil
217 },
218 },
219 {
220 "security+nsd",
221 func() *dns.Msg {
222 m := dns.NewMsg("example.org.", dns.TypeMX)
223 m.ID = 3
224 m.Security = true
225 m.Pseudo = []dns.RR{&dns.NSID{}}
226 return m
227 },
228 func(r *dns.Msg) error {
229 if !r.Security {
230 return fmt.Errorf("expected %t, got %t", r.Security, !r.Security)
231 }
232 arcount := binary.BigEndian.Uint16(r.Data[msgArcount:])
233 if arcount != 1 { // nsid and DO bit are stored in a single record
234 return fmt.Errorf("expected arcount to be 1, got %d", arcount)
235 }
236 if x := len(r.Pseudo); x != 1 {
237 return fmt.Errorf("expected len(pseudo) to be 1, got %d", x)
238 }
239 return nil
240 },
241 },
242 {
243 "security+tsig-nosign+nsid",
244 func() *dns.Msg {
245 m := dns.NewMsg("example.org.", dns.TypeMX)
246 m.ID = 3
247 m.Security = true
248 m.Pseudo = []dns.RR{&dns.NSID{}}
249 m.Pseudo = append(m.Pseudo, dns.NewTSIG("example.", dns.HmacSHA256, 0))
250 return m
251 },
252 func(r *dns.Msg) error {
253 if !r.Security {
254 return fmt.Errorf("expected %t, got %t", !r.Security, r.Security)
255 }
256 arcount := binary.BigEndian.Uint16(r.Data[10:])
257 if arcount != 2 { // nsid and DO bit are stored in a single record + tsig
258 return fmt.Errorf("expected arcount to be 2, got %d", arcount)
259 }
260 if x := len(r.Pseudo); x != 2 {
261 return fmt.Errorf("expected len(pseudo) to be 2, got %d", x)
262 }
263 return nil
264 },
265 },
266 {
267 "tsig",
268 func() *dns.Msg {
269 m := dns.NewMsg("example.org.", dns.TypeMX)
270 m.ID = 3
271 m.Pseudo = []dns.RR{dns.NewTSIG("example.", dns.HmacSHA256, 0)}
272 return m
273 },
274 func(r *dns.Msg) error {
275 if r.Security {
276 return fmt.Errorf("expected %t, got %t", !r.Security, r.Security)
277 }
278 arcount := binary.BigEndian.Uint16(r.Data[10:])
279 if arcount != 1 {
280 return fmt.Errorf("expected arcount to be q, got %d", arcount)
281 }
282 if x := len(r.Pseudo); x != 1 {
283 return fmt.Errorf("expected len(pseudo) to be 1, got %d", x)
284 }
285 if x := len(r.Extra); x != 0 {
286 return fmt.Errorf("expected len(extra) to be 0, got %d", x)
287 }
288 return nil
289 },
290 },
291 {
292 "nsec3",
293 func() *dns.Msg {
294 m := dns.NewMsg("miek.nl.", dns.TypeMX)
295 m.ID = 3
296 nsec3 := dnstest.New("k36vo59bkum4osckkrd8tvibdgr0njbc.nl. 599 IN NSEC3 1 0 0 - K36VONMLM2T8IF3G8P5AV864OHLTB7K7 NS SOA TXT RRSIG DNSKEY NSEC3PARAM")
297 m.Answer = []dns.RR{nsec3}
298 return m
299 },
300 func(r *dns.Msg) error {
301 expect := []byte{0, 3, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 15, 0, 1, 32, 107, 51, 54, 118, 111, 53, 57, 98, 107, 117, 109, 52, 111, 115, 99, 107, 107, 114, 100, 56, 116, 118, 105, 98, 100, 103, 114, 48, 110, 106, 98, 99, 192, 17, 0, 50, 0, 1, 0, 0, 2, 87, 0, 35, 1, 0, 0, 0, 0, 20, 160, 205, 252, 94, 213, 176, 186, 137, 60, 112, 70, 74, 175, 160, 196, 196, 107, 213, 158, 135, 0, 7, 34, 0, 128, 0, 0, 2, 144}
302 if !bytes.Equal(r.Data, expect) {
303 return fmt.Errorf("Msg octets do not match")
304 }
305 return nil
306
307 },
308 },
309 {
310 "pseudo-nsid",
311 func() *dns.Msg {
312 m := dns.NewMsg("miek.nl.", dns.TypeMX)
313 m.ID = 3
314 m.Pseudo = []dns.RR{&dns.NSID{}}
315 return m
316 },
317 func(r *dns.Msg) error {
318 // 41 is OPT after the zeros, 04 -> rdlength, 03 -> code of NSID, 00 -> "rdlength" of NSID
319 expect := []byte{0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 15, 0, 1, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 4, 0, 3, 0, 0}
320 if !bytes.Equal(r.Data, expect) {
321 return fmt.Errorf("Msg octets do not match")
322 }
323 return nil
324 },
325 },
326 {
327 "question-mx",
328 func() *dns.Msg {
329 m := dns.NewMsg("miek.nl.", dns.TypeMX)
330 m.ID = 3
331 return m
332 },
333 func(r *dns.Msg) error {
334 expect := []byte{0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 15, 0, 1}
335 if !bytes.Equal(r.Data, expect) {
336 return fmt.Errorf("Msg octets do not match")
337 }
338 return nil
339 },
340 },
341 {
342 "ede",
343 func() *dns.Msg {
344 m := dns.NewMsg("miek.nl.", dns.TypeMX)
345 m.ID = 3
346 m.Pseudo = []dns.RR{&dns.EDE{
347 InfoCode: dns.ExtendedErrorDNSSECIndeterminate,
348 ExtraText: "Some extra text",
349 }}
350 return m
351 },
352 func(r *dns.Msg) error {
353 expect := []byte{
354 0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0,
355 15, 0, 1, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 21, 0, 15, 0, 17, 0, 5, 83, 111, 109,
356 101, 32, 101, 120, 116, 114, 97, 32, 116, 101, 120, 116,
357 }
358 if !bytes.Equal(r.Data, expect) {
359 return fmt.Errorf("Msg octets do not match")
360 }
361 return nil
362 },
363 },
364 {
365 "rfc3597",
366 func() *dns.Msg {
367 m := dns.NewMsg("example.", dns.TypeA)
368 m.Answer = []dns.RR{
369 dnstest.New("example. IN TYPE65534 \\# 5 084ff00001"),
370 dnstest.New("example. IN TYPE65280 \\# 0"),
371 a,
372 null,
373 }
374 return m
375 },
376 func(r *dns.Msg) error {
377 want := []uint16{65534, 65280, dns.TypeA, dns.TypeNULL}
378 if len(r.Answer) != len(want) {
379 return fmt.Errorf("expected %d answers, got %d", len(want), len(r.Answer))
380 }
381 for i, rr := range r.Answer {
382 if got := dns.RRToType(rr); got != want[i] {
383 return fmt.Errorf("answer %d: expected TYPE%d, got TYPE%d", i, want[i], got)
384 }
385 }
386 return r.Pack()
387 },
388 },
389 {
390 "malformed-a",
391 func() *dns.Msg {
392 // name=. type=A class=IN ttl=0 rdlength={0, 0} -> and no rdata
393 return &dns.Msg{Data: []byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0}}
394 },
395 func(r *dns.Msg) error {
396 return nil
397 },
398 },
399 {
400 "malformed-aaaa",
401 func() *dns.Msg {
402 // name=. type=AAAA class=IN ttl=0 rdlength={0,1} -> and then 2 as malformed address
403 return &dns.Msg{Data: []byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 28, 0, 0, 0, 0, 0, 1, 2}}
404 },
405 func(r *dns.Msg) error {
406 return nil
407 },
408 },
409 }
410
411 for _, tc := range testcases {
412 t.Run(tc.name, func(t *testing.T) {
413 m := tc.makeFn()
414 m.Pack()
415
416 r := new(dns.Msg)
417 r.Data = m.Data
418 r.Unpack()
419 if err := tc.testFn(r); err != nil {
420 t.Fatalf("failed testFn: %s", err)
421 }
422 })
423 }
424
425 }
426
427 func FuzzMsgPack(f *testing.F) {
428 binaries := []string{"dig-mx-miek.nl", "dig+do+nsid-a-miek.nl"}
429 for _, binary := range binaries {
430 buf, _ := os.ReadFile("testdata/" + binary)
431 f.Add(buf)
432 }
433 start := time.Now()
434 f.Fuzz(func(t *testing.T, b []byte) {
435 m := &dns.Msg{Data: b}
436 m.Unpack()
437 dnsfuzz.Stop(t, start)
438 })
439 }
440
441 func TestMsgReadAll(t *testing.T) {
442 m := dns.NewMsg("example.org.", dns.TypeA)
443 m.Pack()
444
445 done := make(chan struct{})
446 go func() {
447 io.ReadAll(m) // should not hang
448 close(done)
449 }()
450
451 select {
452 case <-done:
453 case <-time.After(200 * time.Millisecond):
454 t.Fatal("expected io.ReadAll to complete, but hung")
455 }
456 }