| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io/fs" |
| 8 |
"log" |
| 9 |
"net/netip" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"testing/fstest" |
| 14 |
) |
| 15 |
|
| 16 |
func TestZoneParser(t *testing.T) { |
| 17 |
testcases := []struct { |
| 18 |
name string |
| 19 |
input string |
| 20 |
output []RR |
| 21 |
err error |
| 22 |
}{ |
| 23 |
{ |
| 24 |
"$generate", |
| 25 |
"$ORIGIN example.org.\n$GENERATE 10-12 foo${2,3,d} IN A 127.0.0.$", |
| 26 |
[]RR{ |
| 27 |
&A{Hdr: Header{Name: "foo012.example.org.", Class: ClassINET}, Addr: netip.MustParseAddr("127.0.0.10")}, |
| 28 |
&A{Hdr: Header{Name: "foo013.example.org.", Class: ClassINET}, Addr: netip.MustParseAddr("127.0.0.11")}, |
| 29 |
&A{Hdr: Header{Name: "foo014.example.org.", Class: ClassINET}, Addr: netip.MustParseAddr("127.0.0.12")}, |
| 30 |
}, |
| 31 |
nil, |
| 32 |
}, |
| 33 |
{ |
| 34 |
"aaaa", |
| 35 |
"1.example.org. 600 IN AAAA ::1\n2.example.org. 600 IN AAAA ::FFFF:127.0.0.1", |
| 36 |
[]RR{ |
| 37 |
&AAAA{Hdr: Header{Name: "1.example.org.", Class: ClassINET}, Addr: netip.IPv6Loopback()}, |
| 38 |
&AAAA{Hdr: Header{Name: "2.example.org.", Class: ClassINET}, Addr: netip.MustParseAddr("::FFFF:127.0.0.1")}, |
| 39 |
}, |
| 40 |
nil, |
| 41 |
}, |
| 42 |
{"badaddr1", "1.bad.example.org. 600 IN A ::1", nil, &Error{err: `bad A Addr: "::1"`}}, |
| 43 |
{"baddaddr2", "2.bad.example.org. 600 IN A ::FFFF:127.0.0.1", nil, &Error{err: `bad A Addr:`}}, |
| 44 |
{"badaddr3", "3.bad.example.org. 600 IN AAAA 127.0.0.1", nil, &Error{err: `bad AAAA Addr:`}}, |
| 45 |
{ |
| 46 |
"unknown-rdata", |
| 47 |
"example. 3600 tYpe44 \\# 03 75 0100", |
| 48 |
[]RR{&SSHFP{Hdr: Header{Name: "example.", Class: ClassINET}, Algorithm: 117, Type: 1, FingerPrint: "00"}}, |
| 49 |
nil, |
| 50 |
}, |
| 51 |
{ |
| 52 |
"unknown-without-rdata", |
| 53 |
"example. 3600 CLASS1 TYPE1 \\# 0", |
| 54 |
[]RR{&A{Hdr: Header{Name: "example.", Class: ClassINET}}}, |
| 55 |
nil, |
| 56 |
}, |
| 57 |
{ |
| 58 |
"unknown-toolong", |
| 59 |
"example. 3600 CLASS1 TYPE1 \\# 65536 " + strings.Repeat("00 ", 65536), |
| 60 |
nil, |
| 61 |
&Error{err: "bad RFC3597 Rdata"}, |
| 62 |
}, |
| 63 |
{"openescape", "example.net IN CNAME example.net.", nil, nil}, |
| 64 |
{"bad-openescape", "example.net IN CNAME example.org\\", nil, &Error{err: "bad owner name:"}}, |
| 65 |
{"badtarget-cname", "bad.example.org. CNAME ; bad cname", nil, &Error{err: "missing TTL with no"}}, |
| 66 |
{"badtarget-http", "bad.example.org. HTTPS 10 ; bad https", nil, &Error{err: "missing TTL with no"}}, |
| 67 |
{"badtarget-mx", "bad.example.org. MX 10 ; bad mx", nil, &Error{err: "missing TTL with no"}}, |
| 68 |
{"badtarget-srv", "bad.example.org. SRV 1 0 80 ; bad srv", nil, &Error{err: "missing TTL with no"}}, |
| 69 |
{"nsid-truncated", " NSID :", nil, &Error{err: "bad NSID Nsid"}}, |
| 70 |
{"overflow-ttl", "example. 4294967296 IN A 127.0.0.1", nil, &Error{err: "not a TTL: "}}, |
| 71 |
{ |
| 72 |
"border-fit-ttl", |
| 73 |
"example. 4294967295 IN A 127.0.0.1", |
| 74 |
[]RR{&A{Hdr: Header{Name: "example.", Class: ClassINET}, Addr: netip.MustParseAddr("127.0.0.1")}}, |
| 75 |
nil, |
| 76 |
}, |
| 77 |
{ |
| 78 |
"null RR", |
| 79 |
`example. 0 IN NULL \# 0`, |
| 80 |
[]RR{&NULL{Hdr: Header{Name: "example.", Class: ClassINET}}}, |
| 81 |
nil, |
| 82 |
}, |
| 83 |
} |
| 84 |
for _, tc := range testcases { |
| 85 |
t.Run(tc.name, func(t *testing.T) { |
| 86 |
z := NewZoneParser(strings.NewReader(tc.input), "", "") |
| 87 |
i := 0 |
| 88 |
for rr, ok := z.Next(); ok; rr, ok = z.Next() { |
| 89 |
if !Equal(rr, tc.output[i]) { |
| 90 |
t.Errorf("expected %s to equal to %s", rr, tc.output[i]) |
| 91 |
} |
| 92 |
i++ |
| 93 |
} |
| 94 |
if tc.err != nil { |
| 95 |
if !strings.Contains(z.Err().Error(), tc.err.Error()) { |
| 96 |
t.Errorf("expected err to be %s, got %s", tc.err, z.Err()) |
| 97 |
} |
| 98 |
} |
| 99 |
}) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func TestZoneParserAll(t *testing.T) { |
| 104 |
testcases := []struct { |
| 105 |
name string |
| 106 |
input string |
| 107 |
output []RR |
| 108 |
err error |
| 109 |
}{ |
| 110 |
{ |
| 111 |
"multiple", |
| 112 |
"1.example.org. 600 IN AAAA ::1\n2.example.org. 600 IN AAAA ::FFFF:127.0.0.1", |
| 113 |
[]RR{ |
| 114 |
&AAAA{Hdr: Header{Name: "1.example.org.", Class: ClassINET}, Addr: netip.IPv6Loopback()}, |
| 115 |
&AAAA{Hdr: Header{Name: "2.example.org.", Class: ClassINET}, Addr: netip.MustParseAddr("::FFFF:127.0.0.1")}, |
| 116 |
}, |
| 117 |
nil, |
| 118 |
}, |
| 119 |
{"empty", "", []RR{}, nil}, |
| 120 |
{"error", "1.bad.example.org. 600 IN A ::1", nil, &Error{err: `bad A Addr: "::1"`}}, |
| 121 |
{ |
| 122 |
"multiple-error", |
| 123 |
"1.example.org. 600 IN AAAA ::1\n1.bad.example.org. 600 IN A ::1", |
| 124 |
[]RR{&AAAA{Hdr: Header{Name: "1.example.org.", Class: ClassINET}, Addr: netip.IPv6Loopback()}}, |
| 125 |
&Error{err: `bad A Addr: "::1"`}, |
| 126 |
}, |
| 127 |
} |
| 128 |
for _, tc := range testcases { |
| 129 |
t.Run(tc.name, func(t *testing.T) { |
| 130 |
zp := NewZoneParser(strings.NewReader(tc.input), "", "") |
| 131 |
for rr, err := range zp.All() { |
| 132 |
if tc.err != nil && err != nil { |
| 133 |
if !strings.Contains(err.Error(), tc.err.Error()) { |
| 134 |
t.Fatalf("expected err to be %s, got %s", tc.err, err) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if rr == nil { |
| 139 |
continue |
| 140 |
} |
| 141 |
|
| 142 |
ok := false |
| 143 |
for _, rr1 := range tc.output { |
| 144 |
if Equal(rr, rr1) { |
| 145 |
ok = true |
| 146 |
break |
| 147 |
} |
| 148 |
} |
| 149 |
if !ok { |
| 150 |
t.Fatal("expected RRs to match") |
| 151 |
} |
| 152 |
} |
| 153 |
}) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func ExampleZoneParser_All() { |
| 158 |
f, err := os.Open("example.org") |
| 159 |
if err != nil { |
| 160 |
log.Fatal(err) |
| 161 |
} |
| 162 |
zp := NewZoneParser(f, "example.org", "example.org") |
| 163 |
for rr, err := range zp.All() { |
| 164 |
if err != nil { |
| 165 |
log.Fatal(err) |
| 166 |
} |
| 167 |
fmt.Printf("%s\n", rr) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestZoneParserInclude(t *testing.T) { |
| 172 |
tmpfile, _ := os.CreateTemp("", "dns") |
| 173 |
defer os.Remove(tmpfile.Name()) |
| 174 |
|
| 175 |
if _, err := tmpfile.WriteString("foo\tIN\tA\t127.0.0.1"); err != nil { |
| 176 |
t.Fatalf("unable to write content to tmpfile %q: %s", tmpfile.Name(), err) |
| 177 |
} |
| 178 |
if err := tmpfile.Close(); err != nil { |
| 179 |
t.Fatalf("could not close tmpfile %q: %s", tmpfile.Name(), err) |
| 180 |
} |
| 181 |
|
| 182 |
zone := "$ORIGIN example.org.\n$INCLUDE " + tmpfile.Name() + "\nbar\tIN\tA\t127.0.0.2" |
| 183 |
|
| 184 |
var got int |
| 185 |
z := NewZoneParser(strings.NewReader(zone), "", "") |
| 186 |
z.IncludeAllowFunc = func(string, string) bool { return true } |
| 187 |
for rr, ok := z.Next(); ok; _, ok = z.Next() { |
| 188 |
switch rr.Header().Name { |
| 189 |
case "foo.example.org.", "bar.example.org.": |
| 190 |
default: |
| 191 |
t.Fatalf("expected foo.example.org. or bar.example.org., but got %s", rr.Header().Name) |
| 192 |
} |
| 193 |
got++ |
| 194 |
} |
| 195 |
if err := z.Err(); err != nil { |
| 196 |
t.Fatalf("expected no error, but got %s", err) |
| 197 |
} |
| 198 |
|
| 199 |
if expected := 2; got != expected { |
| 200 |
t.Errorf("failed to parse zone after include, expected %d records, got %d", expected, got) |
| 201 |
} |
| 202 |
|
| 203 |
os.Remove(tmpfile.Name()) |
| 204 |
|
| 205 |
z = NewZoneParser(strings.NewReader(zone), "", "") |
| 206 |
z.IncludeAllowFunc = func(string, string) bool { return true } |
| 207 |
z.Next() |
| 208 |
if err := z.Err(); err == nil || |
| 209 |
!strings.Contains(err.Error(), "failed to open") || |
| 210 |
!strings.Contains(err.Error(), tmpfile.Name()) || |
| 211 |
!strings.Contains(err.Error(), "no such file or directory") { |
| 212 |
t.Fatalf(`expected error to contain: "failed to open", %q and "no such file or directory" but got: %s`, |
| 213 |
tmpfile.Name(), err) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
func TestZoneParserIncludeFS(t *testing.T) { |
| 218 |
fsys := fstest.MapFS{ |
| 219 |
"db.foo": &fstest.MapFile{ |
| 220 |
Data: []byte("foo\tIN\tA\t127.0.0.1"), |
| 221 |
}, |
| 222 |
} |
| 223 |
zone := "$ORIGIN example.org.\n$INCLUDE db.foo\nbar\tIN\tA\t127.0.0.2" |
| 224 |
|
| 225 |
var got int |
| 226 |
z := NewZoneParser(strings.NewReader(zone), "", "") |
| 227 |
z.IncludeAllowFunc = func(string, string) bool { return true } |
| 228 |
z.IncludeFS = fsys |
| 229 |
for rr, ok := z.Next(); ok; _, ok = z.Next() { |
| 230 |
switch rr.Header().Name { |
| 231 |
case "foo.example.org.", "bar.example.org.": |
| 232 |
default: |
| 233 |
t.Fatalf("expected foo.example.org. or bar.example.org., but got %s", rr.Header().Name) |
| 234 |
} |
| 235 |
got++ |
| 236 |
} |
| 237 |
if err := z.Err(); err != nil { |
| 238 |
t.Fatalf("expected no error, but got %s", err) |
| 239 |
} |
| 240 |
|
| 241 |
if expected := 2; got != expected { |
| 242 |
t.Errorf("failed to parse zone after include, expected %d records, got %d", expected, got) |
| 243 |
} |
| 244 |
|
| 245 |
z = NewZoneParser(strings.NewReader(zone), "", "") |
| 246 |
z.IncludeAllowFunc = func(string, string) bool { return true } |
| 247 |
z.IncludeFS = fstest.MapFS{} |
| 248 |
z.Next() |
| 249 |
if err := z.Err(); !errors.Is(err, fs.ErrNotExist) { |
| 250 |
t.Fatalf(`expected fs.ErrNotExist but got: %T %v`, err, err) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
func TestZoneParserIncludeFSPaths(t *testing.T) { |
| 255 |
fsys := fstest.MapFS{ |
| 256 |
"baz/bat/db.foo": &fstest.MapFile{ |
| 257 |
Data: []byte("foo\tIN\tA\t127.0.0.1"), |
| 258 |
}, |
| 259 |
} |
| 260 |
|
| 261 |
for _, p := range []string{ |
| 262 |
"../bat/db.foo", |
| 263 |
"/baz/bat/db.foo", |
| 264 |
} { |
| 265 |
zone := "$ORIGIN example.org.\n$INCLUDE " + p + "\nbar\tIN\tA\t127.0.0.2" |
| 266 |
var got int |
| 267 |
z := NewZoneParser(strings.NewReader(zone), "", "baz/quux/db.bar") |
| 268 |
z.IncludeAllowFunc = func(string, string) bool { return true } |
| 269 |
z.IncludeFS = fsys |
| 270 |
for rr, ok := z.Next(); ok; _, ok = z.Next() { |
| 271 |
switch rr.Header().Name { |
| 272 |
case "foo.example.org.", "bar.example.org.": |
| 273 |
default: |
| 274 |
t.Fatalf("$INCLUDE %q: expected foo.example.org. or bar.example.org., but got %s", p, rr.Header().Name) |
| 275 |
} |
| 276 |
got++ |
| 277 |
} |
| 278 |
if err := z.Err(); err != nil { |
| 279 |
t.Fatalf("$INCLUDE %q: expected no error, but got %s", p, err) |
| 280 |
} |
| 281 |
if expected := 2; got != expected { |
| 282 |
t.Errorf("$INCLUDE %q: failed to parse zone after include, expected %d records, got %d", p, expected, got) |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
func TestZoneParserIncludeDisallowed(t *testing.T) { |
| 288 |
tmpfile, err := os.CreateTemp("", "dns") |
| 289 |
if err != nil { |
| 290 |
t.Fatalf("could not create tmpfile for test: %s", err) |
| 291 |
} |
| 292 |
defer os.Remove(tmpfile.Name()) |
| 293 |
|
| 294 |
if _, err := tmpfile.WriteString("foo\tIN\tA\t127.0.0.1"); err != nil { |
| 295 |
t.Fatalf("unable to write content to tmpfile %q: %s", tmpfile.Name(), err) |
| 296 |
} |
| 297 |
if err := tmpfile.Close(); err != nil { |
| 298 |
t.Fatalf("could not close tmpfile %q: %s", tmpfile.Name(), err) |
| 299 |
} |
| 300 |
|
| 301 |
zp := NewZoneParser(strings.NewReader("$INCLUDE "+tmpfile.Name()), "example.org.", "") |
| 302 |
|
| 303 |
for _, ok := zp.Next(); ok; _, ok = zp.Next() { |
| 304 |
} |
| 305 |
|
| 306 |
const expect = "$INCLUDE directive not allowed" |
| 307 |
if err := zp.Err(); err == nil || !strings.Contains(err.Error(), expect) { |
| 308 |
t.Errorf("expected error to contain %q, got %v", expect, err) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
func TestZoneParserUnexpectedNewline(t *testing.T) { |
| 313 |
zone := ` |
| 314 |
example.com. 60 PX |
| 315 |
1000 TXT 1K |
| 316 |
` |
| 317 |
zp := NewZoneParser(strings.NewReader(zone), "example.com.", "") |
| 318 |
for _, ok := zp.Next(); ok; _, ok = zp.Next() { |
| 319 |
} |
| 320 |
|
| 321 |
const expect = `dns: unexpected newline: "\n" at line: 2:18` |
| 322 |
if err := zp.Err(); err == nil || err.Error() != expect { |
| 323 |
t.Errorf("expected error to contain %q, got %v", expect, err) |
| 324 |
} |
| 325 |
|
| 326 |
// Test that newlines inside braces still work. |
| 327 |
zone = ` |
| 328 |
example.com. 60 PX ( |
| 329 |
1000 TXT 1K ) |
| 330 |
` |
| 331 |
zp = NewZoneParser(strings.NewReader(zone), "example.com.", "") |
| 332 |
|
| 333 |
var count int |
| 334 |
for _, ok := zp.Next(); ok; _, ok = zp.Next() { |
| 335 |
count++ |
| 336 |
} |
| 337 |
|
| 338 |
if count != 1 { |
| 339 |
t.Errorf("expected 1 record, got %d", count) |
| 340 |
} |
| 341 |
|
| 342 |
if err := zp.Err(); err != nil { |
| 343 |
t.Errorf("unexpected error: %v", err) |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
func TestZoneParserEscapedStringOffset(t *testing.T) { |
| 348 |
testcases := []struct { |
| 349 |
input string |
| 350 |
inputOffset int |
| 351 |
expectedOffset int |
| 352 |
expectedOK bool |
| 353 |
}{ |
| 354 |
{"simple string with no escape sequences", 20, 20, true}, |
| 355 |
{"simple string with no escape sequences", 500, -1, true}, |
| 356 |
{`\;\088\\\;\120\\`, 0, 0, true}, |
| 357 |
{`\;\088\\\;\120\\`, 1, 2, true}, |
| 358 |
{`\;\088\\\;\120\\`, 2, 6, true}, |
| 359 |
{`\;\088\\\;\120\\`, 3, 8, true}, |
| 360 |
{`\;\088\\\;\120\\`, 4, 10, true}, |
| 361 |
{`\;\088\\\;\120\\`, 5, 14, true}, |
| 362 |
{`\;\088\\\;\120\\`, 6, 16, true}, |
| 363 |
{`\;\088\\\;\120\\`, 7, -1, true}, |
| 364 |
{`\`, 3, 0, false}, |
| 365 |
{`a\`, 3, 0, false}, |
| 366 |
{`aa\`, 3, 0, false}, |
| 367 |
{`aaa\`, 3, 3, true}, |
| 368 |
{`aaaa\`, 3, 3, true}, |
| 369 |
} |
| 370 |
for i, tc := range testcases { |
| 371 |
outputOffset, outputOK := escapedStringOffset(tc.input, tc.inputOffset) |
| 372 |
if outputOffset != tc.expectedOffset { |
| 373 |
t.Errorf("test %d (input %#q offset %d) returned offset %d but expected %d", |
| 374 |
i, tc.input, tc.inputOffset, outputOffset, tc.expectedOffset, |
| 375 |
) |
| 376 |
} |
| 377 |
if outputOK != tc.expectedOK { |
| 378 |
t.Errorf("test %d (input %#q offset %d) returned ok=%t but expected %t", |
| 379 |
i, tc.input, tc.inputOffset, outputOK, tc.expectedOK, |
| 380 |
) |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
func TestZoneParserEDNS0(t *testing.T) { |
| 386 |
testcases := []struct { |
| 387 |
name string |
| 388 |
in EDNS0 |
| 389 |
exp string |
| 390 |
}{ |
| 391 |
{ |
| 392 |
"zoneversion", &ZONEVERSION{Labels: 4, Type: 0, Version: []byte{1, 2, 3, 4}}, |
| 393 |
". CLASS0 ZONEVERSION 4 SOA-SERIAL 16909060", |
| 394 |
}, |
| 395 |
{ |
| 396 |
"ede-extratext", &EDE{InfoCode: 15, ExtraText: "bla"}, |
| 397 |
`. CLASS0 EDE 15 "Blocked": "bla"`, |
| 398 |
}, |
| 399 |
{ |
| 400 |
"ede", &EDE{InfoCode: 15}, |
| 401 |
`. CLASS0 EDE 15 "Blocked": ""`, |
| 402 |
}, |
| 403 |
} |
| 404 |
for _, tc := range testcases { |
| 405 |
t.Run(tc.name, func(t *testing.T) { |
| 406 |
ednsrr := tc.in.String() |
| 407 |
parsed := dnstestNew(ednsrr) |
| 408 |
s := strings.ReplaceAll(parsed.String(), "\t", " ") |
| 409 |
if s != tc.exp { |
| 410 |
t.Errorf("expected %s, got %s", tc.exp, s) |
| 411 |
} |
| 412 |
}) |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
func BenchmarkZoneParser(b *testing.B) { |
| 417 |
root, err := os.ReadFile("testdata/root.zone") |
| 418 |
if err != nil { |
| 419 |
b.Fatal(err) |
| 420 |
} |
| 421 |
|
| 422 |
r := bytes.NewReader(root) |
| 423 |
for b.Loop() { |
| 424 |
zp := NewZoneParser(r, ".", "testdata/root.zone") |
| 425 |
for _, ok := zp.Next(); ok; _, ok = zp.Next() { |
| 426 |
} |
| 427 |
r.Seek(0, 0) |
| 428 |
} |
| 429 |
} |