| 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.All() { |
| 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) 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, unpackerr error) error { |
| 60 |
if unpackerr != nil { |
| 61 |
return unpackerr |
| 62 |
} |
| 63 |
if len(m.Answer) != 1 { |
| 64 |
return errors.New("expected answer section") |
| 65 |
} |
| 66 |
s, ok := m.Answer[0].(*dns.SOA) |
| 67 |
if !ok { |
| 68 |
return errors.New("expected SOA") |
| 69 |
} |
| 70 |
if s.Mbox != `miek\.miek.nl.` { |
| 71 |
return errors.New("SOA Mbox is not correct") |
| 72 |
} |
| 73 |
return nil |
| 74 |
}, |
| 75 |
}, |
| 76 |
|
| 77 |
{ |
| 78 |
"edns0-subnet", |
| 79 |
[]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}, |
| 80 |
func(m *dns.Msg, unpackerr error) error { |
| 81 |
if unpackerr != nil { |
| 82 |
return unpackerr |
| 83 |
} |
| 84 |
if len(m.Pseudo) == 0 { |
| 85 |
return errors.New("expected pseudo section") |
| 86 |
} |
| 87 |
s, ok := m.Pseudo[0].(*dns.SUBNET) |
| 88 |
if !ok { |
| 89 |
return errors.New("expected EDNS0 SUBNET") |
| 90 |
} |
| 91 |
const addr = "14.128.63.0" |
| 92 |
if s.Address != netip.MustParseAddr(addr) { |
| 93 |
return errors.New("expected address: " + addr) |
| 94 |
} |
| 95 |
return nil |
| 96 |
}, |
| 97 |
}, |
| 98 |
{ |
| 99 |
"edns0-subnet", |
| 100 |
[]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}, |
| 101 |
func(m *dns.Msg, unpackerr error) error { |
| 102 |
if unpackerr != nil { |
| 103 |
return unpackerr |
| 104 |
} |
| 105 |
if len(m.Pseudo) == 0 { |
| 106 |
return errors.New("expected pseudo section") |
| 107 |
} |
| 108 |
s, ok := m.Pseudo[0].(*dns.SUBNET) |
| 109 |
if !ok { |
| 110 |
return errors.New("expected EDNS0 SUBNET") |
| 111 |
} |
| 112 |
const addr = "62.212.234.0" |
| 113 |
if s.Address != netip.MustParseAddr(addr) { |
| 114 |
return errors.New("expected address: " + addr) |
| 115 |
} |
| 116 |
return nil |
| 117 |
}, |
| 118 |
}, |
| 119 |
{ |
| 120 |
"opt-and-tsig", |
| 121 |
[]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}, |
| 122 |
func(m *dns.Msg, unpackerr error) error { |
| 123 |
if unpackerr != nil { |
| 124 |
return unpackerr |
| 125 |
} |
| 126 |
if len(m.Pseudo) == 0 { |
| 127 |
return errors.New("expected pseudo section") |
| 128 |
} |
| 129 |
_, ok := m.Pseudo[1].(*dns.TSIG) // 0 is COOKIE |
| 130 |
if !ok { |
| 131 |
return errors.New("expected TSIG") |
| 132 |
} |
| 133 |
return nil |
| 134 |
}, |
| 135 |
}, |
| 136 |
{ |
| 137 |
"opt-and-extra-tsig", |
| 138 |
[]byte{148, 7, 1, 32, 0, 1, 0, 0, 0, 0, 0, 3, 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, 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}, |
| 139 |
func(m *dns.Msg, unpackerr error) error { |
| 140 |
if unpackerr == nil { |
| 141 |
return errors.New("expected Unpack failure 'multiple TSIG RRs'") |
| 142 |
} |
| 143 |
return nil |
| 144 |
}, |
| 145 |
}, |
| 146 |
{ |
| 147 |
"opt-and-tsig-extra-should-empty", |
| 148 |
[]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}, |
| 149 |
func(m *dns.Msg, unpackerr error) error { |
| 150 |
if unpackerr != nil { |
| 151 |
return unpackerr |
| 152 |
} |
| 153 |
if len(m.Extra) != 0 { |
| 154 |
return errors.New("expected additional section to be empty") |
| 155 |
} |
| 156 |
return nil |
| 157 |
}, |
| 158 |
}, |
| 159 |
{ |
| 160 |
"unknown-edns0-code20", |
| 161 |
// edns255 := &dns.ERFC3597{EDNS0Code: 255, Code: hex.EncodeToString([]byte("hallo"))} |
| 162 |
// m.Pseudo = append(m.Pseudo, edns255) |
| 163 |
[]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}, |
| 164 |
func(m *dns.Msg, unpackerr error) error { |
| 165 |
if unpackerr != nil { |
| 166 |
return unpackerr |
| 167 |
} |
| 168 |
if len(m.Pseudo) != 1 { |
| 169 |
return errors.New("expected pseudo section to carry an option") |
| 170 |
} |
| 171 |
x := m.Pseudo[0].(*dns.ERFC3597) |
| 172 |
if x.EDNS0Code != 255 { |
| 173 |
return fmt.Errorf("expected code 255, got %d", x.EDNS0Code) |
| 174 |
} |
| 175 |
return nil |
| 176 |
}, |
| 177 |
}, |
| 178 |
{ |
| 179 |
"fuzzing-find-#932", |
| 180 |
[]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}, |
| 181 |
func(m *dns.Msg, unpackerr error) error { |
| 182 |
if unpackerr != nil { |
| 183 |
return unpackerr |
| 184 |
} |
| 185 |
// the message is a bit fubar, but we did crash on it, when packing again. |
| 186 |
m.Pack() |
| 187 |
return nil |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
"question without class", // dns.NewMsg("example.", dns.TypeAAAA) -> println(bin.Bytes(m.Data)) and stripped away the class; last 2 bytes. |
| 192 |
[]byte{175, 231, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 7, 101, 120, 97, 109, 112, 108, 101, 0, 0, 28}, |
| 193 |
func(m *dns.Msg, unpackerr error) error { |
| 194 |
if unpackerr == nil { |
| 195 |
return fmt.Errorf("unpacking without class should error") |
| 196 |
} |
| 197 |
return nil |
| 198 |
}, |
| 199 |
}, |
| 200 |
/* |
| 201 |
;; OPT PSEUDOSECTION: |
| 202 |
; EDNS: version 0; flags:; udp: 1232 |
| 203 |
;; QUESTION SECTION: |
| 204 |
;example. IN A |
| 205 |
;; ADDITIONAL SECTION: |
| 206 |
example. 0 IN TXT "preserve-me" |
| 207 |
;; TSIG PSEUDOSECTION: |
| 208 |
; key.example. 0 CLASS255 TSIG hmac-sha256. 20260920150926 300 32 FEEB41C4A3597B9CC34F26CF8B249B34B7F617923DD12C67BFCD05DC3DCC7946 4660 0 0 |
| 209 |
*/ |
| 210 |
{ |
| 211 |
"Extra opt+txt+sig", |
| 212 |
[]byte{18, 52, 1, 0, 0, 1, 0, 0, 0, 0, 0, 3, 7, 101, 120, 97, 109, 112, 108, 101, 0, 0, 1, 0, 1, 0, 0, 41, 4, 208, 0, 0, 0, 0, 0, 0, 7, 101, 120, 97, 109, 112, 108, 101, 0, 0, 16, 0, 1, 0, 0, 0, 0, 0, 12, 11, 112, 114, 101, 115, 101, 114, 118, 101, 45, 109, 101, 3, 107, 101, 121, 7, 101, 120, 97, 109, 112, 108, 101, 0, 0, 250, 0, 255, 0, 0, 0, 0, 0, 61, 11, 104, 109, 97, 99, 45, 115, 104, 97, 50, 53, 54, 0, 0, 0, 106, 175, 247, 38, 1, 44, 0, 32, 254, 235, 65, 196, 163, 89, 123, 156, 195, 79, 38, 207, 139, 36, 155, 52, 183, 246, 23, 146, 61, 209, 44, 103, 191, 205, 5, 220, 61, 204, 121, 70, 18, 52, 0, 0, 0, 0}, |
| 213 |
func(m *dns.Msg, unpackerr error) error { |
| 214 |
if m.UDPSize != 1232 { |
| 215 |
t.Fatalf("expected UDPSize to be 1232, got %d", m.UDPSize) |
| 216 |
} |
| 217 |
if len(m.Pseudo) != 1 { |
| 218 |
t.Fatalf("expected Pseudo count to be 1, got %d", len(m.Pseudo)) |
| 219 |
} |
| 220 |
if _, ok := m.Pseudo[0].(*dns.TSIG); !ok { |
| 221 |
t.Fatalf("expected TSIG, got %T", m.Pseudo[0]) |
| 222 |
} |
| 223 |
if len(m.Extra) != 1 { |
| 224 |
t.Fatalf("expected Extra count to be 1, got %d", len(m.Extra)) |
| 225 |
} |
| 226 |
txt, ok := m.Extra[0].(*dns.TXT) |
| 227 |
if !ok || len(txt.Txt) != 1 || txt.Txt[0] != "preserve-me" { |
| 228 |
t.Fatal("failed to find TXT record") |
| 229 |
} |
| 230 |
return nil |
| 231 |
}, |
| 232 |
}, |
| 233 |
} |
| 234 |
for _, tc := range testcases { |
| 235 |
t.Run(tc.name, func(t *testing.T) { |
| 236 |
m := &dns.Msg{Data: tc.buf} |
| 237 |
err := m.Unpack() |
| 238 |
if err := tc.fn(m, err); err != nil { |
| 239 |
t.Logf("%s\n", bin.Dump(m.Data)) |
| 240 |
t.Fatal(err) |
| 241 |
} |
| 242 |
}) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func TestMsg(t *testing.T) { |
| 247 |
const msgArcount = 10 // offset in the message where the Arcount is, 2 octets long. |
| 248 |
a := new(dns.RFC3597) |
| 249 |
if err := a.ToRFC3597(dnstest.New("example. IN A 192.0.2.1")); err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
null := new(dns.RFC3597) |
| 253 |
if err := null.ToRFC3597(&dns.NULL{Hdr: dns.Header{Name: "example.", Class: dns.ClassINET}}); err != nil { |
| 254 |
t.Fatal(err) |
| 255 |
} |
| 256 |
testcases := []struct { |
| 257 |
name string |
| 258 |
makeFn func() *dns.Msg |
| 259 |
testFn func(m *dns.Msg) error |
| 260 |
}{ |
| 261 |
{ |
| 262 |
"extendedrcode", |
| 263 |
func() *dns.Msg { |
| 264 |
m := dns.NewMsg("example.org.", dns.TypeMX) |
| 265 |
m.ID = 3 |
| 266 |
m.Rcode = dns.RcodeBadTime |
| 267 |
return m |
| 268 |
}, |
| 269 |
func(r *dns.Msg) error { |
| 270 |
if r.Rcode != dns.RcodeBadTime { |
| 271 |
return fmt.Errorf("expected %s, got %s", dns.RcodeToString[dns.RcodeBadTime], dns.RcodeToString[r.Rcode]) |
| 272 |
} |
| 273 |
return nil |
| 274 |
}, |
| 275 |
}, |
| 276 |
{ |
| 277 |
"security", |
| 278 |
func() *dns.Msg { m := dns.NewMsg("example.org.", dns.TypeMX); m.ID = 3; m.Security = true; return m }, |
| 279 |
func(r *dns.Msg) error { |
| 280 |
if !r.Security { |
| 281 |
return fmt.Errorf("expected %t, got %t", r.Security, !r.Security) |
| 282 |
} |
| 283 |
arcount := binary.BigEndian.Uint16(r.Data[msgArcount:]) |
| 284 |
if arcount != 1 { |
| 285 |
return fmt.Errorf("expected arcount to be 1, got %d", arcount) |
| 286 |
} |
| 287 |
return nil |
| 288 |
}, |
| 289 |
}, |
| 290 |
{ |
| 291 |
"ednsz", |
| 292 |
func() *dns.Msg { |
| 293 |
m := dns.NewMsg("example.org.", dns.TypeMX) |
| 294 |
m.ID = 3 |
| 295 |
m.Z = 0x0080 |
| 296 |
return m |
| 297 |
}, |
| 298 |
func(r *dns.Msg) error { |
| 299 |
if r.Z != 0x0080 { |
| 300 |
return fmt.Errorf("expected %#04x, got %#04x", 0x0080, r.Z) |
| 301 |
} |
| 302 |
arcount := binary.BigEndian.Uint16(r.Data[msgArcount:]) |
| 303 |
if arcount != 1 { |
| 304 |
return fmt.Errorf("expected arcount to be 1, got %d", arcount) |
| 305 |
} |
| 306 |
return nil |
| 307 |
}, |
| 308 |
}, |
| 309 |
{ |
| 310 |
"ednsz-security", |
| 311 |
func() *dns.Msg { |
| 312 |
m := dns.NewMsg("example.org.", dns.TypeMX) |
| 313 |
m.ID = 3 |
| 314 |
m.Security, m.CompactAnswers, m.Delegation = true, true, true |
| 315 |
return m |
| 316 |
}, |
| 317 |
func(r *dns.Msg) error { |
| 318 |
if r.Z != 0 { |
| 319 |
return fmt.Errorf("expected %#04x, got %#04x", 0, r.Z) |
| 320 |
} |
| 321 |
return nil |
| 322 |
}, |
| 323 |
}, |
| 324 |
{ |
| 325 |
"security+nsd", |
| 326 |
func() *dns.Msg { |
| 327 |
m := dns.NewMsg("example.org.", dns.TypeMX) |
| 328 |
m.ID = 3 |
| 329 |
m.Security = true |
| 330 |
m.Pseudo = []dns.RR{&dns.NSID{}} |
| 331 |
return m |
| 332 |
}, |
| 333 |
func(r *dns.Msg) error { |
| 334 |
if !r.Security { |
| 335 |
return fmt.Errorf("expected %t, got %t", r.Security, !r.Security) |
| 336 |
} |
| 337 |
arcount := binary.BigEndian.Uint16(r.Data[msgArcount:]) |
| 338 |
if arcount != 1 { // nsid and DO bit are stored in a single record |
| 339 |
return fmt.Errorf("expected arcount to be 1, got %d", arcount) |
| 340 |
} |
| 341 |
if x := len(r.Pseudo); x != 1 { |
| 342 |
return fmt.Errorf("expected len(pseudo) to be 1, got %d", x) |
| 343 |
} |
| 344 |
return nil |
| 345 |
}, |
| 346 |
}, |
| 347 |
{ |
| 348 |
"security+tsig-nosign+nsid", |
| 349 |
func() *dns.Msg { |
| 350 |
m := dns.NewMsg("example.org.", dns.TypeMX) |
| 351 |
m.ID = 3 |
| 352 |
m.Security = true |
| 353 |
m.Pseudo = []dns.RR{&dns.NSID{}} |
| 354 |
m.Pseudo = append(m.Pseudo, dns.NewTSIG("example.", dns.HmacSHA256, 0)) |
| 355 |
return m |
| 356 |
}, |
| 357 |
func(r *dns.Msg) error { |
| 358 |
if !r.Security { |
| 359 |
return fmt.Errorf("expected %t, got %t", !r.Security, r.Security) |
| 360 |
} |
| 361 |
arcount := binary.BigEndian.Uint16(r.Data[10:]) |
| 362 |
if arcount != 2 { // nsid and DO bit are stored in a single record + tsig |
| 363 |
return fmt.Errorf("expected arcount to be 2, got %d", arcount) |
| 364 |
} |
| 365 |
if x := len(r.Pseudo); x != 2 { |
| 366 |
return fmt.Errorf("expected len(pseudo) to be 2, got %d", x) |
| 367 |
} |
| 368 |
return nil |
| 369 |
}, |
| 370 |
}, |
| 371 |
{ |
| 372 |
"edns0+tsig", |
| 373 |
func() *dns.Msg { |
| 374 |
m := dns.NewMsg("example.", dns.TypeA) |
| 375 |
m.UDPSize = dns.DefaultMsgSize |
| 376 |
m.Pseudo = append(m.Pseudo, |
| 377 |
&dns.COOKIE{Cookie: "0102030405060708"}, |
| 378 |
dns.NewTSIG("example.", dns.HmacSHA256, 0), |
| 379 |
) |
| 380 |
if err := dns.TSIGSign(m, dns.HmacTSIG{Secret: []byte("Wg==")}, &dns.TSIGOption{}); err != nil { |
| 381 |
panic(err) |
| 382 |
} |
| 383 |
return m |
| 384 |
}, |
| 385 |
func(r *dns.Msg) error { |
| 386 |
if len(r.Pseudo) != 2 { |
| 387 |
return fmt.Errorf("expected two pseudo records, got %d", len(r.Pseudo)) |
| 388 |
} |
| 389 |
if _, ok := r.Pseudo[0].(*dns.COOKIE); !ok { |
| 390 |
return fmt.Errorf("expected COOKIE first, got %T", r.Pseudo[0]) |
| 391 |
} |
| 392 |
if _, ok := r.Pseudo[1].(*dns.TSIG); !ok { |
| 393 |
return fmt.Errorf("expected TSIG last, got %T", r.Pseudo[1]) |
| 394 |
} |
| 395 |
if err := dns.TSIGVerify(r, dns.HmacTSIG{Secret: []byte("Wg==")}, &dns.TSIGOption{}); err != nil { |
| 396 |
return fmt.Errorf("failed to verify TSIG: %w", err) |
| 397 |
} |
| 398 |
if err := r.Pack(); err != nil { |
| 399 |
return fmt.Errorf("failed to repack message: %w", err) |
| 400 |
} |
| 401 |
return nil |
| 402 |
}, |
| 403 |
}, |
| 404 |
{ |
| 405 |
"tsig", |
| 406 |
func() *dns.Msg { |
| 407 |
m := dns.NewMsg("example.org.", dns.TypeMX) |
| 408 |
m.ID = 3 |
| 409 |
m.Pseudo = []dns.RR{dns.NewTSIG("example.", dns.HmacSHA256, 0)} |
| 410 |
return m |
| 411 |
}, |
| 412 |
func(r *dns.Msg) error { |
| 413 |
if r.Security { |
| 414 |
return fmt.Errorf("expected %t, got %t", !r.Security, r.Security) |
| 415 |
} |
| 416 |
arcount := binary.BigEndian.Uint16(r.Data[10:]) |
| 417 |
if arcount != 1 { |
| 418 |
return fmt.Errorf("expected arcount to be q, got %d", arcount) |
| 419 |
} |
| 420 |
if x := len(r.Pseudo); x != 1 { |
| 421 |
return fmt.Errorf("expected len(pseudo) to be 1, got %d", x) |
| 422 |
} |
| 423 |
if x := len(r.Extra); x != 0 { |
| 424 |
return fmt.Errorf("expected len(extra) to be 0, got %d", x) |
| 425 |
} |
| 426 |
return nil |
| 427 |
}, |
| 428 |
}, |
| 429 |
{ |
| 430 |
"nsec3", |
| 431 |
func() *dns.Msg { |
| 432 |
m := dns.NewMsg("miek.nl.", dns.TypeMX) |
| 433 |
m.ID = 3 |
| 434 |
nsec3 := dnstest.New("k36vo59bkum4osckkrd8tvibdgr0njbc.nl. 599 IN NSEC3 1 0 0 - K36VONMLM2T8IF3G8P5AV864OHLTB7K7 NS SOA TXT RRSIG DNSKEY NSEC3PARAM") |
| 435 |
m.Answer = []dns.RR{nsec3} |
| 436 |
return m |
| 437 |
}, |
| 438 |
func(r *dns.Msg) error { |
| 439 |
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} |
| 440 |
if !bytes.Equal(r.Data, expect) { |
| 441 |
return fmt.Errorf("Msg octets do not match") |
| 442 |
} |
| 443 |
return nil |
| 444 |
|
| 445 |
}, |
| 446 |
}, |
| 447 |
{ |
| 448 |
"pseudo-nsid", |
| 449 |
func() *dns.Msg { |
| 450 |
m := dns.NewMsg("miek.nl.", dns.TypeMX) |
| 451 |
m.ID = 3 |
| 452 |
m.Pseudo = []dns.RR{&dns.NSID{}} |
| 453 |
return m |
| 454 |
}, |
| 455 |
func(r *dns.Msg) error { |
| 456 |
// 41 is OPT after the zeros, 04 -> rdlength, 03 -> code of NSID, 00 -> "rdlength" of NSID |
| 457 |
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} |
| 458 |
if !bytes.Equal(r.Data, expect) { |
| 459 |
return fmt.Errorf("Msg octets do not match") |
| 460 |
} |
| 461 |
return nil |
| 462 |
}, |
| 463 |
}, |
| 464 |
{ |
| 465 |
"question-mx", |
| 466 |
func() *dns.Msg { |
| 467 |
m := dns.NewMsg("miek.nl.", dns.TypeMX) |
| 468 |
m.ID = 3 |
| 469 |
return m |
| 470 |
}, |
| 471 |
func(r *dns.Msg) error { |
| 472 |
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} |
| 473 |
if !bytes.Equal(r.Data, expect) { |
| 474 |
return fmt.Errorf("Msg octets do not match") |
| 475 |
} |
| 476 |
return nil |
| 477 |
}, |
| 478 |
}, |
| 479 |
{ |
| 480 |
"ede", |
| 481 |
func() *dns.Msg { |
| 482 |
m := dns.NewMsg("miek.nl.", dns.TypeMX) |
| 483 |
m.ID = 3 |
| 484 |
m.Pseudo = []dns.RR{&dns.EDE{ |
| 485 |
InfoCode: dns.ExtendedErrorDNSSECIndeterminate, |
| 486 |
ExtraText: "Some extra text", |
| 487 |
}} |
| 488 |
return m |
| 489 |
}, |
| 490 |
func(r *dns.Msg) error { |
| 491 |
expect := []byte{ |
| 492 |
0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, |
| 493 |
15, 0, 1, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 21, 0, 15, 0, 17, 0, 5, 83, 111, 109, |
| 494 |
101, 32, 101, 120, 116, 114, 97, 32, 116, 101, 120, 116, |
| 495 |
} |
| 496 |
if !bytes.Equal(r.Data, expect) { |
| 497 |
return fmt.Errorf("Msg octets do not match") |
| 498 |
} |
| 499 |
return nil |
| 500 |
}, |
| 501 |
}, |
| 502 |
{ |
| 503 |
"rfc3597", |
| 504 |
func() *dns.Msg { |
| 505 |
m := dns.NewMsg("example.", dns.TypeA) |
| 506 |
m.Answer = []dns.RR{ |
| 507 |
dnstest.New("example. IN TYPE65534 \\# 5 084ff00001"), |
| 508 |
dnstest.New("example. IN TYPE65280 \\# 0"), |
| 509 |
a, |
| 510 |
null, |
| 511 |
} |
| 512 |
return m |
| 513 |
}, |
| 514 |
func(r *dns.Msg) error { |
| 515 |
want := []uint16{65534, 65280, dns.TypeA, dns.TypeNULL} |
| 516 |
if len(r.Answer) != len(want) { |
| 517 |
return fmt.Errorf("expected %d answers, got %d", len(want), len(r.Answer)) |
| 518 |
} |
| 519 |
for i, rr := range r.Answer { |
| 520 |
if got := dns.RRToType(rr); got != want[i] { |
| 521 |
return fmt.Errorf("answer %d: expected TYPE%d, got TYPE%d", i, want[i], got) |
| 522 |
} |
| 523 |
} |
| 524 |
return r.Pack() |
| 525 |
}, |
| 526 |
}, |
| 527 |
{ |
| 528 |
"malformed-a", |
| 529 |
func() *dns.Msg { |
| 530 |
// name=. type=A class=IN ttl=0 rdlength={0, 0} -> and no rdata |
| 531 |
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}} |
| 532 |
}, |
| 533 |
func(r *dns.Msg) error { |
| 534 |
return nil |
| 535 |
}, |
| 536 |
}, |
| 537 |
{ |
| 538 |
"malformed-aaaa", |
| 539 |
func() *dns.Msg { |
| 540 |
// name=. type=AAAA class=IN ttl=0 rdlength={0,1} -> and then 2 as malformed address |
| 541 |
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}} |
| 542 |
}, |
| 543 |
func(r *dns.Msg) error { |
| 544 |
return nil |
| 545 |
}, |
| 546 |
}, |
| 547 |
} |
| 548 |
|
| 549 |
for _, tc := range testcases { |
| 550 |
t.Run(tc.name, func(t *testing.T) { |
| 551 |
m := tc.makeFn() |
| 552 |
m.Pack() |
| 553 |
|
| 554 |
r := new(dns.Msg) |
| 555 |
r.Data = m.Data |
| 556 |
r.Unpack() |
| 557 |
if err := tc.testFn(r); err != nil { |
| 558 |
t.Fatalf("failed testFn: %s", err) |
| 559 |
} |
| 560 |
}) |
| 561 |
} |
| 562 |
|
| 563 |
} |
| 564 |
|
| 565 |
func FuzzMsgPack(f *testing.F) { |
| 566 |
binaries := []string{"dig-mx-miek.nl", "dig+do+nsid-a-miek.nl"} |
| 567 |
for _, binary := range binaries { |
| 568 |
buf, _ := os.ReadFile("testdata/" + binary) |
| 569 |
f.Add(buf) |
| 570 |
} |
| 571 |
start := time.Now() |
| 572 |
f.Fuzz(func(t *testing.T, b []byte) { |
| 573 |
m := &dns.Msg{Data: b} |
| 574 |
m.Unpack() |
| 575 |
dnsfuzz.Stop(t, start) |
| 576 |
}) |
| 577 |
} |
| 578 |
|
| 579 |
func TestMsgReadAll(t *testing.T) { |
| 580 |
m := dns.NewMsg("example.org.", dns.TypeA) |
| 581 |
m.Pack() |
| 582 |
|
| 583 |
done := make(chan struct{}) |
| 584 |
go func() { |
| 585 |
io.ReadAll(m) // should not hang |
| 586 |
close(done) |
| 587 |
}() |
| 588 |
|
| 589 |
select { |
| 590 |
case <-done: |
| 591 |
case <-time.After(200 * time.Millisecond): |
| 592 |
t.Fatal("expected io.ReadAll to complete, but hung") |
| 593 |
} |
| 594 |
} |