| 1 | package dns |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "io" |
| 6 | "io/fs" |
| 7 | "net" |
| 8 | "os" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "testing/fstest" |
| 12 | ) |
| 13 | |
| 14 | func TestZoneParserGenerate(t *testing.T) { |
| 15 | zone := "$ORIGIN example.org.\n$GENERATE 10-12 foo${2,3,d} IN A 127.0.0.$" |
| 16 | |
| 17 | wantRRs := []RR{ |
| 18 | &A{Hdr: RR_Header{Name: "foo012.example.org."}, A: net.ParseIP("127.0.0.10")}, |
| 19 | &A{Hdr: RR_Header{Name: "foo013.example.org."}, A: net.ParseIP("127.0.0.11")}, |
| 20 | &A{Hdr: RR_Header{Name: "foo014.example.org."}, A: net.ParseIP("127.0.0.12")}, |
| 21 | } |
| 22 | |
| 23 | wantIdx := 0 |
| 24 | |
| 25 | z := NewZoneParser(strings.NewReader(zone), "", "") |
| 26 | |
| 27 | for rr, ok := z.Next(); ok; rr, ok = z.Next() { |
| 28 | if wantIdx >= len(wantRRs) { |
| 29 | t.Fatalf("expected %d RRs, but got more", len(wantRRs)) |
| 30 | } |
| 31 | if got, want := rr.Header().Name, wantRRs[wantIdx].Header().Name; got != want { |
| 32 | t.Fatalf("expected name %s, but got %s", want, got) |
| 33 | } |
| 34 | a, okA := rr.(*A) |
| 35 | if !okA { |
| 36 | t.Fatalf("expected *A RR, but got %T", rr) |
| 37 | } |
| 38 | if got, want := a.A, wantRRs[wantIdx].(*A).A; !got.Equal(want) { |
| 39 | t.Fatalf("expected A with IP %v, but got %v", got, want) |
| 40 | } |
| 41 | wantIdx++ |
| 42 | } |
| 43 | |
| 44 | if err := z.Err(); err != nil { |
| 45 | t.Fatalf("expected no error, but got %s", err) |
| 46 | } |
| 47 | |
| 48 | if wantIdx != len(wantRRs) { |
| 49 | t.Errorf("too few records, expected %d, got %d", len(wantRRs), wantIdx) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestZoneParserInclude(t *testing.T) { |
| 54 | tmpfile, err := os.CreateTemp("", "dns") |
| 55 | if err != nil { |
| 56 | t.Fatalf("could not create tmpfile for test: %s", err) |
| 57 | } |
| 58 | defer os.Remove(tmpfile.Name()) |
| 59 | |
| 60 | if _, err := tmpfile.WriteString("foo\tIN\tA\t127.0.0.1"); err != nil { |
| 61 | t.Fatalf("unable to write content to tmpfile %q: %s", tmpfile.Name(), err) |
| 62 | } |
| 63 | if err := tmpfile.Close(); err != nil { |
| 64 | t.Fatalf("could not close tmpfile %q: %s", tmpfile.Name(), err) |
| 65 | } |
| 66 | |
| 67 | zone := "$ORIGIN example.org.\n$INCLUDE " + tmpfile.Name() + "\nbar\tIN\tA\t127.0.0.2" |
| 68 | |
| 69 | var got int |
| 70 | z := NewZoneParser(strings.NewReader(zone), "", "") |
| 71 | z.SetIncludeAllowed(true) |
| 72 | for rr, ok := z.Next(); ok; _, ok = z.Next() { |
| 73 | switch rr.Header().Name { |
| 74 | case "foo.example.org.", "bar.example.org.": |
| 75 | default: |
| 76 | t.Fatalf("expected foo.example.org. or bar.example.org., but got %s", rr.Header().Name) |
| 77 | } |
| 78 | got++ |
| 79 | } |
| 80 | if err := z.Err(); err != nil { |
| 81 | t.Fatalf("expected no error, but got %s", err) |
| 82 | } |
| 83 | |
| 84 | if expected := 2; got != expected { |
| 85 | t.Errorf("failed to parse zone after include, expected %d records, got %d", expected, got) |
| 86 | } |
| 87 | |
| 88 | os.Remove(tmpfile.Name()) |
| 89 | |
| 90 | z = NewZoneParser(strings.NewReader(zone), "", "") |
| 91 | z.SetIncludeAllowed(true) |
| 92 | z.Next() |
| 93 | if err := z.Err(); err == nil || |
| 94 | !strings.Contains(err.Error(), "failed to open") || |
| 95 | !strings.Contains(err.Error(), tmpfile.Name()) || |
| 96 | !strings.Contains(err.Error(), "no such file or directory") { |
| 97 | t.Fatalf(`expected error to contain: "failed to open", %q and "no such file or directory" but got: %s`, |
| 98 | tmpfile.Name(), err) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestZoneParserIncludeFS(t *testing.T) { |
| 103 | fsys := fstest.MapFS{ |
| 104 | "db.foo": &fstest.MapFile{ |
| 105 | Data: []byte("foo\tIN\tA\t127.0.0.1"), |
| 106 | }, |
| 107 | } |
| 108 | zone := "$ORIGIN example.org.\n$INCLUDE db.foo\nbar\tIN\tA\t127.0.0.2" |
| 109 | |
| 110 | var got int |
| 111 | z := NewZoneParser(strings.NewReader(zone), "", "") |
| 112 | z.SetIncludeAllowed(true) |
| 113 | z.SetIncludeFS(fsys) |
| 114 | for rr, ok := z.Next(); ok; _, ok = z.Next() { |
| 115 | switch rr.Header().Name { |
| 116 | case "foo.example.org.", "bar.example.org.": |
| 117 | default: |
| 118 | t.Fatalf("expected foo.example.org. or bar.example.org., but got %s", rr.Header().Name) |
| 119 | } |
| 120 | got++ |
| 121 | } |
| 122 | if err := z.Err(); err != nil { |
| 123 | t.Fatalf("expected no error, but got %s", err) |
| 124 | } |
| 125 | |
| 126 | if expected := 2; got != expected { |
| 127 | t.Errorf("failed to parse zone after include, expected %d records, got %d", expected, got) |
| 128 | } |
| 129 | |
| 130 | fsys = fstest.MapFS{} |
| 131 | |
| 132 | z = NewZoneParser(strings.NewReader(zone), "", "") |
| 133 | z.SetIncludeAllowed(true) |
| 134 | z.SetIncludeFS(fsys) |
| 135 | z.Next() |
| 136 | if err := z.Err(); !errors.Is(err, fs.ErrNotExist) { |
| 137 | t.Fatalf(`expected fs.ErrNotExist but got: %T %v`, err, err) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestZoneParserIncludeFSPaths(t *testing.T) { |
| 142 | fsys := fstest.MapFS{ |
| 143 | "baz/bat/db.foo": &fstest.MapFile{ |
| 144 | Data: []byte("foo\tIN\tA\t127.0.0.1"), |
| 145 | }, |
| 146 | } |
| 147 | |
| 148 | for _, p := range []string{ |
| 149 | "../bat/db.foo", |
| 150 | "/baz/bat/db.foo", |
| 151 | } { |
| 152 | zone := "$ORIGIN example.org.\n$INCLUDE " + p + "\nbar\tIN\tA\t127.0.0.2" |
| 153 | var got int |
| 154 | z := NewZoneParser(strings.NewReader(zone), "", "baz/quux/db.bar") |
| 155 | z.SetIncludeAllowed(true) |
| 156 | z.SetIncludeFS(fsys) |
| 157 | for rr, ok := z.Next(); ok; _, ok = z.Next() { |
| 158 | switch rr.Header().Name { |
| 159 | case "foo.example.org.", "bar.example.org.": |
| 160 | default: |
| 161 | t.Fatalf("$INCLUDE %q: expected foo.example.org. or bar.example.org., but got %s", p, rr.Header().Name) |
| 162 | } |
| 163 | got++ |
| 164 | } |
| 165 | if err := z.Err(); err != nil { |
| 166 | t.Fatalf("$INCLUDE %q: expected no error, but got %s", p, err) |
| 167 | } |
| 168 | if expected := 2; got != expected { |
| 169 | t.Errorf("$INCLUDE %q: failed to parse zone after include, expected %d records, got %d", p, expected, got) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestZoneParserIncludeDisallowed(t *testing.T) { |
| 175 | tmpfile, err := os.CreateTemp("", "dns") |
| 176 | if err != nil { |
| 177 | t.Fatalf("could not create tmpfile for test: %s", err) |
| 178 | } |
| 179 | defer os.Remove(tmpfile.Name()) |
| 180 | |
| 181 | if _, err := tmpfile.WriteString("foo\tIN\tA\t127.0.0.1"); err != nil { |
| 182 | t.Fatalf("unable to write content to tmpfile %q: %s", tmpfile.Name(), err) |
| 183 | } |
| 184 | if err := tmpfile.Close(); err != nil { |
| 185 | t.Fatalf("could not close tmpfile %q: %s", tmpfile.Name(), err) |
| 186 | } |
| 187 | |
| 188 | zp := NewZoneParser(strings.NewReader("$INCLUDE "+tmpfile.Name()), "example.org.", "") |
| 189 | |
| 190 | for _, ok := zp.Next(); ok; _, ok = zp.Next() { |
| 191 | } |
| 192 | |
| 193 | const expect = "$INCLUDE directive not allowed" |
| 194 | if err := zp.Err(); err == nil || !strings.Contains(err.Error(), expect) { |
| 195 | t.Errorf("expected error to contain %q, got %v", expect, err) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func TestZoneParserAddressAAAA(t *testing.T) { |
| 200 | tests := []struct { |
| 201 | record string |
| 202 | want *AAAA |
| 203 | }{ |
| 204 | { |
| 205 | record: "1.example.org. 600 IN AAAA ::1", |
| 206 | want: &AAAA{Hdr: RR_Header{Name: "1.example.org."}, AAAA: net.IPv6loopback}, |
| 207 | }, |
| 208 | { |
| 209 | record: "2.example.org. 600 IN AAAA ::FFFF:127.0.0.1", |
| 210 | want: &AAAA{Hdr: RR_Header{Name: "2.example.org."}, AAAA: net.ParseIP("::FFFF:127.0.0.1")}, |
| 211 | }, |
| 212 | } |
| 213 | |
| 214 | for _, tc := range tests { |
| 215 | got, err := NewRR(tc.record) |
| 216 | if err != nil { |
| 217 | t.Fatalf("expected no error, but got %s", err) |
| 218 | } |
| 219 | aaaa, ok := got.(*AAAA) |
| 220 | if !ok { |
| 221 | t.Fatalf("expected *AAAA RR, but got %T", got) |
| 222 | } |
| 223 | if !aaaa.AAAA.Equal(tc.want.AAAA) { |
| 224 | t.Fatalf("expected AAAA with IP %v, but got %v", tc.want.AAAA, aaaa.AAAA) |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func TestZoneParserTargetBad(t *testing.T) { |
| 230 | records := []string{ |
| 231 | "bad.example.org. CNAME ; bad cname", |
| 232 | "bad.example.org. HTTPS 10 ; bad https", |
| 233 | "bad.example.org. MX 10 ; bad mx", |
| 234 | "bad.example.org. SRV 1 0 80 ; bad srv", |
| 235 | } |
| 236 | |
| 237 | for _, record := range records { |
| 238 | const expect = "bad " |
| 239 | if got, err := NewRR(record); err == nil || !strings.Contains(err.Error(), expect) { |
| 240 | t.Errorf("NewRR(%v) = %v, want err to contain %q", record, got, expect) |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | func TestZoneParserAddressBad(t *testing.T) { |
| 246 | records := []string{ |
| 247 | "1.bad.example.org. 600 IN A ::1", |
| 248 | "2.bad.example.org. 600 IN A ::FFFF:127.0.0.1", |
| 249 | "3.bad.example.org. 600 IN AAAA 127.0.0.1", |
| 250 | } |
| 251 | |
| 252 | for _, record := range records { |
| 253 | const expect = "bad A" |
| 254 | if got, err := NewRR(record); err == nil || !strings.Contains(err.Error(), expect) { |
| 255 | t.Errorf("NewRR(%v) = %v, want err to contain %q", record, got, expect) |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func TestParseTA(t *testing.T) { |
| 261 | rr, err := NewRR(` Ta 0 0 0`) |
| 262 | if err != nil { |
| 263 | t.Fatalf("expected no error, but got %s", err) |
| 264 | } |
| 265 | if rr == nil { |
| 266 | t.Fatal(`expected a normal RR, but got nil`) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | var errTestReadError = &Error{"test error"} |
| 271 | |
| 272 | type errReader struct{} |
| 273 | |
| 274 | func (errReader) Read(p []byte) (int, error) { return 0, errTestReadError } |
| 275 | |
| 276 | func TestParseZoneReadError(t *testing.T) { |
| 277 | rr, err := ReadRR(errReader{}, "") |
| 278 | if err == nil || !strings.Contains(err.Error(), errTestReadError.Error()) { |
| 279 | t.Errorf("expected error to contain %q, but got %v", errTestReadError, err) |
| 280 | } |
| 281 | if rr != nil { |
| 282 | t.Errorf("expected a nil RR, but got %v", rr) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | func TestUnexpectedNewline(t *testing.T) { |
| 287 | zone := ` |
| 288 | example.com. 60 PX |
| 289 | 1000 TXT 1K |
| 290 | ` |
| 291 | zp := NewZoneParser(strings.NewReader(zone), "example.com.", "") |
| 292 | for _, ok := zp.Next(); ok; _, ok = zp.Next() { |
| 293 | } |
| 294 | |
| 295 | const expect = `dns: unexpected newline: "\n" at line: 2:18` |
| 296 | if err := zp.Err(); err == nil || err.Error() != expect { |
| 297 | t.Errorf("expected error to contain %q, got %v", expect, err) |
| 298 | } |
| 299 | |
| 300 | // Test that newlines inside braces still work. |
| 301 | zone = ` |
| 302 | example.com. 60 PX ( |
| 303 | 1000 TXT 1K ) |
| 304 | ` |
| 305 | zp = NewZoneParser(strings.NewReader(zone), "example.com.", "") |
| 306 | |
| 307 | var count int |
| 308 | for _, ok := zp.Next(); ok; _, ok = zp.Next() { |
| 309 | count++ |
| 310 | } |
| 311 | |
| 312 | if count != 1 { |
| 313 | t.Errorf("expected 1 record, got %d", count) |
| 314 | } |
| 315 | |
| 316 | if err := zp.Err(); err != nil { |
| 317 | t.Errorf("unexpected error: %v", err) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | func TestParseRFC3597InvalidLength(t *testing.T) { |
| 322 | // We need to space separate the 00s otherwise it will exceed the maximum token size |
| 323 | // of the zone lexer. |
| 324 | _, err := NewRR("example. 3600 CLASS1 TYPE1 \\# 65536 " + strings.Repeat("00 ", 65536)) |
| 325 | if err == nil { |
| 326 | t.Error("should not have parsed excessively long RFC3579 record") |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | func TestParseKnownRRAsRFC3597(t *testing.T) { |
| 331 | t.Run("with RDATA", func(t *testing.T) { |
| 332 | // This was found by oss-fuzz. |
| 333 | _, err := NewRR("example. 3600 tYpe44 \\# 03 75 0100") |
| 334 | if err != nil { |
| 335 | t.Errorf("failed to parse RFC3579 format: %v", err) |
| 336 | } |
| 337 | |
| 338 | rr, err := NewRR("example. 3600 CLASS1 TYPE1 \\# 4 7f000001") |
| 339 | if err != nil { |
| 340 | t.Fatalf("failed to parse RFC3579 format: %v", err) |
| 341 | } |
| 342 | |
| 343 | if rr.Header().Rrtype != TypeA { |
| 344 | t.Errorf("expected TypeA (1) Rrtype, but got %v", rr.Header().Rrtype) |
| 345 | } |
| 346 | |
| 347 | a, ok := rr.(*A) |
| 348 | if !ok { |
| 349 | t.Fatalf("expected *A RR, but got %T", rr) |
| 350 | } |
| 351 | |
| 352 | localhost := net.IPv4(127, 0, 0, 1) |
| 353 | if !a.A.Equal(localhost) { |
| 354 | t.Errorf("expected A with IP %v, but got %v", localhost, a.A) |
| 355 | } |
| 356 | }) |
| 357 | t.Run("without RDATA", func(t *testing.T) { |
| 358 | rr, err := NewRR("example. 3600 CLASS1 TYPE1 \\# 0") |
| 359 | if err != nil { |
| 360 | t.Fatalf("failed to parse RFC3579 format: %v", err) |
| 361 | } |
| 362 | |
| 363 | if rr.Header().Rrtype != TypeA { |
| 364 | t.Errorf("expected TypeA (1) Rrtype, but got %v", rr.Header().Rrtype) |
| 365 | } |
| 366 | |
| 367 | a, ok := rr.(*A) |
| 368 | if !ok { |
| 369 | t.Fatalf("expected *A RR, but got %T", rr) |
| 370 | } |
| 371 | |
| 372 | if len(a.A) != 0 { |
| 373 | t.Errorf("expected A with empty IP, but got %v", a.A) |
| 374 | } |
| 375 | }) |
| 376 | } |
| 377 | |
| 378 | func TestParseOpenEscape(t *testing.T) { |
| 379 | if _, err := NewRR("example.net IN CNAME example.net."); err != nil { |
| 380 | t.Fatalf("expected no error, but got: %s", err) |
| 381 | } |
| 382 | if _, err := NewRR("example.net IN CNAME example.org\\"); err == nil { |
| 383 | t.Fatalf("expected an error, but got none") |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | func BenchmarkNewRR(b *testing.B) { |
| 388 | const name1 = "12345678901234567890123456789012345.12345678.123." |
| 389 | const s = name1 + " 3600 IN MX 10 " + name1 |
| 390 | |
| 391 | for n := 0; n < b.N; n++ { |
| 392 | _, err := NewRR(s) |
| 393 | if err != nil { |
| 394 | b.Fatal(err) |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | func BenchmarkReadRR(b *testing.B) { |
| 400 | const name1 = "12345678901234567890123456789012345.12345678.123." |
| 401 | const s = name1 + " 3600 IN MX 10 " + name1 + "\n" |
| 402 | |
| 403 | for n := 0; n < b.N; n++ { |
| 404 | r := struct{ io.Reader }{strings.NewReader(s)} |
| 405 | // r is now only an io.Reader and won't benefit from the |
| 406 | // io.ByteReader special-case in zlexer.Next. |
| 407 | |
| 408 | _, err := ReadRR(r, "") |
| 409 | if err != nil { |
| 410 | b.Fatal(err) |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | const benchZone = ` |
| 416 | foo. IN A 10.0.0.1 ; this is comment 1 |
| 417 | foo. IN A ( |
| 418 | 10.0.0.2 ; this is comment 2 |
| 419 | ) |
| 420 | ; this is comment 3 |
| 421 | foo. IN A 10.0.0.3 |
| 422 | foo. IN A ( 10.0.0.4 ); this is comment 4 |
| 423 | |
| 424 | foo. IN A 10.0.0.5 |
| 425 | ; this is comment 5 |
| 426 | |
| 427 | foo. IN A 10.0.0.6 |
| 428 | |
| 429 | foo. IN DNSKEY 256 3 5 AwEAAb+8l ; this is comment 6 |
| 430 | foo. IN NSEC miek.nl. TXT RRSIG NSEC; this is comment 7 |
| 431 | foo. IN TXT "THIS IS TEXT MAN"; this is comment 8 |
| 432 | ` |
| 433 | |
| 434 | func BenchmarkZoneParser(b *testing.B) { |
| 435 | for n := 0; n < b.N; n++ { |
| 436 | zp := NewZoneParser(strings.NewReader(benchZone), "example.org.", "") |
| 437 | |
| 438 | for _, ok := zp.Next(); ok; _, ok = zp.Next() { |
| 439 | } |
| 440 | |
| 441 | if err := zp.Err(); err != nil { |
| 442 | b.Fatal(err) |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | func TestEscapedStringOffset(t *testing.T) { |
| 448 | var cases = []struct { |
| 449 | input string |
| 450 | inputOffset int |
| 451 | expectedOffset int |
| 452 | expectedOK bool |
| 453 | }{ |
| 454 | {"simple string with no escape sequences", 20, 20, true}, |
| 455 | {"simple string with no escape sequences", 500, -1, true}, |
| 456 | {`\;\088\\\;\120\\`, 0, 0, true}, |
| 457 | {`\;\088\\\;\120\\`, 1, 2, true}, |
| 458 | {`\;\088\\\;\120\\`, 2, 6, true}, |
| 459 | {`\;\088\\\;\120\\`, 3, 8, true}, |
| 460 | {`\;\088\\\;\120\\`, 4, 10, true}, |
| 461 | {`\;\088\\\;\120\\`, 5, 14, true}, |
| 462 | {`\;\088\\\;\120\\`, 6, 16, true}, |
| 463 | {`\;\088\\\;\120\\`, 7, -1, true}, |
| 464 | {`\`, 3, 0, false}, |
| 465 | {`a\`, 3, 0, false}, |
| 466 | {`aa\`, 3, 0, false}, |
| 467 | {`aaa\`, 3, 3, true}, |
| 468 | {`aaaa\`, 3, 3, true}, |
| 469 | } |
| 470 | for i, test := range cases { |
| 471 | outputOffset, outputOK := escapedStringOffset(test.input, test.inputOffset) |
| 472 | if outputOffset != test.expectedOffset { |
| 473 | t.Errorf( |
| 474 | "Test %d (input %#q offset %d) returned offset %d but expected %d", |
| 475 | i, test.input, test.inputOffset, outputOffset, test.expectedOffset, |
| 476 | ) |
| 477 | } |
| 478 | if outputOK != test.expectedOK { |
| 479 | t.Errorf( |
| 480 | "Test %d (input %#q offset %d) returned ok=%t but expected %t", |
| 481 | i, test.input, test.inputOffset, outputOK, test.expectedOK, |
| 482 | ) |
| 483 | } |
| 484 | } |
| 485 | } |