dns/parse_test.go

2018 lines
68 KiB
Go
Raw Normal View History

package dns
2011-01-18 06:29:40 +11:00
import (
"bytes"
"crypto/rsa"
2014-04-05 05:33:50 +11:00
"encoding/hex"
2012-02-18 06:37:19 +11:00
"fmt"
"math/rand"
2012-10-05 17:49:31 +10:00
"net"
"reflect"
"regexp"
2012-10-05 17:49:31 +10:00
"strconv"
2011-07-24 07:43:43 +10:00
"strings"
2011-01-18 06:29:40 +11:00
"testing"
"testing/quick"
2011-01-18 06:29:40 +11:00
)
func TestDotInName(t *testing.T) {
buf := make([]byte, 20)
PackDomainName("aa\\.bb.nl.", buf, 0, nil, false)
2011-07-24 07:43:43 +10:00
// index 3 must be a real dot
if buf[3] != '.' {
t.Error("dot should be a real dot")
2011-07-24 07:43:43 +10:00
}
2011-07-24 07:43:43 +10:00
if buf[6] != 2 {
t.Error("this must have the value 2")
2011-07-24 07:43:43 +10:00
}
dom, _, _ := UnpackDomainName(buf, 0)
2011-07-24 07:43:43 +10:00
// printing it should yield the backspace again
if dom != "aa\\.bb.nl." {
t.Error("dot should have been escaped: ", dom)
2011-07-24 07:43:43 +10:00
}
2011-02-22 04:23:39 +11:00
}
func TestDotLastInLabel(t *testing.T) {
sample := "aa\\..au."
buf := make([]byte, 20)
_, err := PackDomainName(sample, buf, 0, nil, false)
if err != nil {
2015-02-26 19:49:59 +11:00
t.Fatalf("unexpected error packing domain: %v", err)
}
dom, _, _ := UnpackDomainName(buf, 0)
if dom != sample {
t.Fatalf("unpacked domain `%s' doesn't match packed domain", dom)
}
}
2013-09-11 17:22:20 +10:00
func TestTooLongDomainName(t *testing.T) {
l := "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqqrrrsssttt."
dom := l + l + l + l + l + l + l
_, err := NewRR(dom + " IN A 127.0.0.1")
if err == nil {
t.Error("should be too long")
2013-09-11 17:22:20 +10:00
}
_, err = NewRR("..com. IN A 127.0.0.1")
if err == nil {
t.Error("should fail")
2013-09-11 17:22:20 +10:00
}
}
func TestDomainName(t *testing.T) {
2012-02-15 20:28:55 +11:00
tests := []string{"r\\.gieben.miek.nl.", "www\\.www.miek.nl.",
2012-02-24 05:37:08 +11:00
"www.*.miek.nl.", "www.*.miek.nl.",
}
2012-01-13 08:49:26 +11:00
dbuff := make([]byte, 40)
2012-01-13 08:49:26 +11:00
for _, ts := range tests {
2012-10-10 07:15:33 +11:00
if _, err := PackDomainName(ts, dbuff, 0, nil, false); err != nil {
t.Error("not a valid domain name")
2012-01-13 08:49:26 +11:00
continue
}
2012-10-10 07:15:33 +11:00
n, _, err := UnpackDomainName(dbuff, 0)
if err != nil {
t.Error("failed to unpack packed domain name")
2012-01-13 08:49:26 +11:00
continue
}
if ts != n {
t.Errorf("must be equal: in: %s, out: %s", ts, n)
2012-01-13 08:49:26 +11:00
}
}
}
func TestDomainNameAndTXTEscapes(t *testing.T) {
tests := []byte{'.', '(', ')', ';', ' ', '@', '"', '\\', 9, 13, 10, 0, 255}
for _, b := range tests {
rrbytes := []byte{
1, b, 0, // owner
byte(TypeTXT >> 8), byte(TypeTXT),
byte(ClassINET >> 8), byte(ClassINET),
0, 0, 0, 1, // TTL
0, 2, 1, b, // Data
}
rr1, _, err := UnpackRR(rrbytes, 0)
if err != nil {
panic(err)
}
s := rr1.String()
rr2, err := NewRR(s)
if err != nil {
t.Errorf("error parsing unpacked RR's string: %v", err)
}
repacked := make([]byte, len(rrbytes))
if _, err := PackRR(rr2, repacked, 0, nil, false); err != nil {
t.Errorf("error packing parsed RR: %v", err)
}
if !bytes.Equal(repacked, rrbytes) {
t.Error("packed bytes don't match original bytes")
}
}
}
func TestTXTEscapeParsing(t *testing.T) {
test := [][]string{
{`";"`, `";"`},
{`\;`, `";"`},
{`"\t"`, `"t"`},
{`"\r"`, `"r"`},
{`"\ "`, `" "`},
{`"\;"`, `";"`},
{`"\;\""`, `";\""`},
{`"\(a\)"`, `"(a)"`},
{`"\(a)"`, `"(a)"`},
{`"(a\)"`, `"(a)"`},
{`"(a)"`, `"(a)"`},
{`"\048"`, `"0"`},
{`"\` + "\t" + `"`, `"\009"`},
{`"\` + "\n" + `"`, `"\010"`},
{`"\` + "\r" + `"`, `"\013"`},
{`"\` + "\x11" + `"`, `"\017"`},
{`"\'"`, `"'"`},
}
for _, s := range test {
rr, err := NewRR(fmt.Sprintf("example.com. IN TXT %v", s[0]))
if err != nil {
2015-11-27 01:12:38 +11:00
t.Errorf("could not parse %v TXT: %s", s[0], err)
continue
}
txt := sprintTxt(rr.(*TXT).Txt)
if txt != s[1] {
2015-11-27 01:12:38 +11:00
t.Errorf("mismatch after parsing `%v` TXT record: `%v` != `%v`", s[0], txt, s[1])
}
}
}
func GenerateDomain(r *rand.Rand, size int) []byte {
dnLen := size % 70 // artificially limit size so there's less to interpret if a failure occurs
var dn []byte
done := false
for i := 0; i < dnLen && !done; {
max := dnLen - i
if max > 63 {
max = 63
}
lLen := max
if lLen != 0 {
lLen = int(r.Int31()) % max
}
done = lLen == 0
if done {
continue
}
l := make([]byte, lLen+1)
l[0] = byte(lLen)
for j := 0; j < lLen; j++ {
l[j+1] = byte(rand.Int31())
}
dn = append(dn, l...)
i += 1 + lLen
}
return append(dn, 0)
}
func TestDomainQuick(t *testing.T) {
r := rand.New(rand.NewSource(0))
f := func(l int) bool {
db := GenerateDomain(r, l)
ds, _, err := UnpackDomainName(db, 0)
if err != nil {
panic(err)
}
buf := make([]byte, 255)
off, err := PackDomainName(ds, buf, 0, nil, false)
if err != nil {
t.Errorf("error packing domain: %v", err)
t.Errorf(" bytes: %v", db)
t.Errorf("string: %v", ds)
return false
}
if !bytes.Equal(db, buf[:off]) {
t.Errorf("repacked domain doesn't match original:")
t.Errorf("src bytes: %v", db)
t.Errorf(" string: %v", ds)
t.Errorf("out bytes: %v", buf[:off])
return false
}
return true
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func GenerateTXT(r *rand.Rand, size int) []byte {
rdLen := size % 300 // artificially limit size so there's less to interpret if a failure occurs
var rd []byte
for i := 0; i < rdLen; {
max := rdLen - 1
if max > 255 {
max = 255
}
sLen := max
if max != 0 {
sLen = int(r.Int31()) % max
}
s := make([]byte, sLen+1)
s[0] = byte(sLen)
for j := 0; j < sLen; j++ {
s[j+1] = byte(rand.Int31())
}
rd = append(rd, s...)
i += 1 + sLen
}
return rd
}
// Ok, 2 things. 1) this test breaks with the new functionality of splitting up larger txt
// chunks into 255 byte pieces. 2) I don't like the random nature of this thing, because I can't
// place the quotes where they need to be.
// So either add some code the places the quotes in just the right spots, make this non random
// or do something else.
// Disabled for now. (miek)
func testTXTRRQuick(t *testing.T) {
s := rand.NewSource(0)
r := rand.New(s)
typeAndClass := []byte{
byte(TypeTXT >> 8), byte(TypeTXT),
byte(ClassINET >> 8), byte(ClassINET),
0, 0, 0, 1, // TTL
}
f := func(l int) bool {
owner := GenerateDomain(r, l)
rdata := GenerateTXT(r, l)
rrbytes := make([]byte, 0, len(owner)+2+2+4+2+len(rdata))
rrbytes = append(rrbytes, owner...)
rrbytes = append(rrbytes, typeAndClass...)
rrbytes = append(rrbytes, byte(len(rdata)>>8), byte(len(rdata)))
rrbytes = append(rrbytes, rdata...)
rr, _, err := UnpackRR(rrbytes, 0)
if err != nil {
panic(err)
}
buf := make([]byte, len(rrbytes)*3)
off, err := PackRR(rr, buf, 0, nil, false)
if err != nil {
t.Errorf("pack Error: %v\nRR: %v", err, rr)
return false
}
buf = buf[:off]
if !bytes.Equal(buf, rrbytes) {
t.Errorf("packed bytes don't match original bytes")
t.Errorf("src bytes: %v", rrbytes)
t.Errorf(" struct: %v", rr)
t.Errorf("out bytes: %v", buf)
return false
}
if len(rdata) == 0 {
// stringifying won't produce any data to parse
return true
}
rrString := rr.String()
rr2, err := NewRR(rrString)
if err != nil {
t.Errorf("error parsing own output: %v", err)
t.Errorf("struct: %v", rr)
t.Errorf("string: %v", rrString)
return false
}
if rr2.String() != rrString {
t.Errorf("parsed rr.String() doesn't match original string")
t.Errorf("original: %v", rrString)
t.Errorf(" parsed: %v", rr2.String())
return false
}
buf = make([]byte, len(rrbytes)*3)
off, err = PackRR(rr2, buf, 0, nil, false)
if err != nil {
t.Errorf("error packing parsed rr: %v", err)
t.Errorf("unpacked Struct: %v", rr)
t.Errorf(" string: %v", rrString)
t.Errorf(" parsed Struct: %v", rr2)
return false
}
buf = buf[:off]
if !bytes.Equal(buf, rrbytes) {
t.Errorf("parsed packed bytes don't match original bytes")
t.Errorf(" source bytes: %v", rrbytes)
t.Errorf("unpacked struct: %v", rr)
t.Errorf(" string: %v", rrString)
t.Errorf(" parsed struct: %v", rr2)
t.Errorf(" repacked bytes: %v", buf)
return false
}
return true
}
c := &quick.Config{MaxCountScale: 10}
if err := quick.Check(f, c); err != nil {
t.Error(err)
}
}
2012-02-15 19:04:09 +11:00
func TestParseDirectiveMisc(t *testing.T) {
tests := map[string]string{
2012-02-24 05:37:08 +11:00
"$ORIGIN miek.nl.\na IN NS b": "a.miek.nl.\t3600\tIN\tNS\tb.miek.nl.",
"$TTL 2H\nmiek.nl. IN NS b.": "miek.nl.\t7200\tIN\tNS\tb.",
"miek.nl. 1D IN NS b.": "miek.nl.\t86400\tIN\tNS\tb.",
`name. IN SOA a6.nstld.com. hostmaster.nic.name. (
2012-02-15 19:04:09 +11:00
203362132 ; serial
5m ; refresh (5 minutes)
5m ; retry (5 minutes)
2w ; expire (2 weeks)
300 ; minimum (5 minutes)
)`: "name.\t3600\tIN\tSOA\ta6.nstld.com. hostmaster.nic.name. 203362132 300 300 1209600 300",
2012-02-24 05:37:08 +11:00
". 3600000 IN NS ONE.MY-ROOTS.NET.": ".\t3600000\tIN\tNS\tONE.MY-ROOTS.NET.",
"ONE.MY-ROOTS.NET. 3600000 IN A 192.168.1.1": "ONE.MY-ROOTS.NET.\t3600000\tIN\tA\t192.168.1.1",
}
for i, o := range tests {
rr, err := NewRR(i)
if err != nil {
t.Error("failed to parse RR: ", err)
continue
}
if rr.String() != o {
t.Errorf("`%s' should be equal to\n`%s', but is `%s'", i, o, rr.String())
}
}
}
2013-10-13 22:25:08 +11:00
func TestNSEC(t *testing.T) {
2012-02-12 08:59:34 +11:00
nsectests := map[string]string{
"nl. IN NSEC3PARAM 1 0 5 30923C44C6CBBB8F": "nl.\t3600\tIN\tNSEC3PARAM\t1 0 5 30923C44C6CBBB8F",
2012-02-24 05:37:08 +11:00
"p2209hipbpnm681knjnu0m1febshlv4e.nl. IN NSEC3 1 1 5 30923C44C6CBBB8F P90DG1KE8QEAN0B01613LHQDG0SOJ0TA NS SOA TXT RRSIG DNSKEY NSEC3PARAM": "p2209hipbpnm681knjnu0m1febshlv4e.nl.\t3600\tIN\tNSEC3\t1 1 5 30923C44C6CBBB8F P90DG1KE8QEAN0B01613LHQDG0SOJ0TA NS SOA TXT RRSIG DNSKEY NSEC3PARAM",
"localhost.dnssex.nl. IN NSEC www.dnssex.nl. A RRSIG NSEC": "localhost.dnssex.nl.\t3600\tIN\tNSEC\twww.dnssex.nl. A RRSIG NSEC",
"localhost.dnssex.nl. IN NSEC www.dnssex.nl. A RRSIG NSEC TYPE65534": "localhost.dnssex.nl.\t3600\tIN\tNSEC\twww.dnssex.nl. A RRSIG NSEC TYPE65534",
"localhost.dnssex.nl. IN NSEC www.dnssex.nl. A RRSIG NSec Type65534": "localhost.dnssex.nl.\t3600\tIN\tNSEC\twww.dnssex.nl. A RRSIG NSEC TYPE65534",
"44ohaq2njb0idnvolt9ggthvsk1e1uv8.skydns.test. NSEC3 1 0 0 - 44OHAQ2NJB0IDNVOLT9GGTHVSK1E1UVA": "44ohaq2njb0idnvolt9ggthvsk1e1uv8.skydns.test.\t3600\tIN\tNSEC3\t1 0 0 - 44OHAQ2NJB0IDNVOLT9GGTHVSK1E1UVA",
2012-02-24 05:37:08 +11:00
}
2012-02-12 08:59:34 +11:00
for i, o := range nsectests {
rr, err := NewRR(i)
if err != nil {
t.Error("failed to parse RR: ", err)
2012-02-12 08:59:34 +11:00
continue
}
if rr.String() != o {
t.Errorf("`%s' should be equal to\n`%s', but is `%s'", i, o, rr.String())
2012-02-12 08:59:34 +11:00
}
}
rr, err := NewRR("nl. IN NSEC3PARAM 1 0 5 30923C44C6CBBB8F")
if err != nil {
t.Fatal("failed to parse RR: ", err)
}
if nsec3param, ok := rr.(*NSEC3PARAM); ok {
if nsec3param.SaltLength != 8 {
t.Fatalf("nsec3param saltlen %d != 8", nsec3param.SaltLength)
}
} else {
t.Fatal("not nsec3 param: ", err)
}
2012-02-12 08:59:34 +11:00
}
2012-05-01 05:42:58 +10:00
func TestParseLOC(t *testing.T) {
2012-05-01 05:57:42 +10:00
lt := map[string]string{
"SW1A2AA.find.me.uk. LOC 51 30 12.748 N 00 07 39.611 W 0.00m 0.00m 0.00m 0.00m": "SW1A2AA.find.me.uk.\t3600\tIN\tLOC\t51 30 12.748 N 00 07 39.611 W 0m 0.00m 0.00m 0.00m",
"SW1A2AA.find.me.uk. LOC 51 0 0.0 N 00 07 39.611 W 0.00m 0.00m 0.00m 0.00m": "SW1A2AA.find.me.uk.\t3600\tIN\tLOC\t51 00 0.000 N 00 07 39.611 W 0m 0.00m 0.00m 0.00m",
"SW1A2AA.find.me.uk. LOC 51 30 12.748 N 00 07 39.611 W 0.00m": "SW1A2AA.find.me.uk.\t3600\tIN\tLOC\t51 30 12.748 N 00 07 39.611 W 0m 1m 10000m 10m",
// Exercise boundary cases
"SW1A2AA.find.me.uk. LOC 90 0 0.0 N 180 0 0.0 W 42849672.95 90000000.00m 90000000.00m 90000000.00m": "SW1A2AA.find.me.uk.\t3600\tIN\tLOC\t90 00 0.000 N 180 00 0.000 W 42849672.95m 90000000m 90000000m 90000000m",
"SW1A2AA.find.me.uk. LOC 89 59 59.999 N 179 59 59.999 W -100000 90000000.00m 90000000.00m 90000000m": "SW1A2AA.find.me.uk.\t3600\tIN\tLOC\t89 59 59.999 N 179 59 59.999 W -100000m 90000000m 90000000m 90000000m",
// use float64 to have enough precision.
"example.com. LOC 42 21 43.952 N 71 5 6.344 W -24m 1m 200m 10m": "example.com.\t3600\tIN\tLOC\t42 21 43.952 N 71 05 6.344 W -24m 1m 200m 10m",
2012-05-01 05:57:42 +10:00
}
for i, o := range lt {
rr, err := NewRR(i)
if err != nil {
t.Error("failed to parse RR: ", err)
2012-05-01 05:57:42 +10:00
continue
}
if rr.String() != o {
t.Errorf("`%s' should be equal to\n`%s', but is `%s'", i, o, rr.String())
2012-05-01 05:57:42 +10:00
}
2012-05-01 05:42:58 +10:00
}
// Invalid cases (out of range values)
lt = map[string]string{ // Pair of (invalid) RDATA and the bad field name
// One of the subfields is out of range.
"91 0 0.0 N 00 07 39.611 W 0m": "Latitude",
"89 60 0.0 N 00 07 39.611 W 0m": "Latitude",
"89 00 60.0 N 00 07 39.611 W 0m": "Latitude",
"1 00 -1 N 00 07 39.611 W 0m": "Latitude",
"0 0 0.0 N 181 00 0.0 W 0m": "Longitude",
"0 0 0.0 N 179 60 0.0 W 0m": "Longitude",
"0 0 0.0 N 179 00 60.0 W 0m": "Longitude",
"0 0 0.0 N 1 00 -1 W 0m": "Longitude",
// Each subfield is valid, but resulting latitude would be out of range.
"90 01 00.0 N 00 07 39.611 W 0m": "Latitude",
"0 0 0.0 N 180 01 0.0 W 0m": "Longitude",
}
for rdata, field := range lt {
_, err := NewRR(fmt.Sprintf("example.com. LOC %s", rdata))
if err == nil || !strings.Contains(err.Error(), field) {
t.Errorf("expected error to contain %q, but got %v", field, err)
}
}
2012-05-01 05:42:58 +10:00
}
// this tests a subroutine for the LOC RR parser. It's complicated enough to test separately.
func TestStringToCm(t *testing.T) {
tests := []struct {
// Test description: the input token and the expected return values from stringToCm.
token string
e uint8
m uint8
ok bool
}{
{"100", 4, 1, true},
{"0100", 4, 1, true}, // leading 0 (allowed)
{"100.99", 4, 1, true},
{"90000000", 9, 9, true},
{"90000000.00", 9, 9, true},
{"0", 0, 0, true},
{"0.00", 0, 0, true},
{"0.01", 0, 1, true},
{".01", 0, 1, true}, // empty 'meter' part (allowed)
{"0.1", 1, 1, true},
// out of range (too large)
{"90000001", 0, 0, false},
{"90000000.01", 0, 0, false},
// more than 2 digits in 'cmeter' part
{"0.000", 0, 0, false},
{"0.001", 0, 0, false},
{"0.999", 0, 0, false},
// with plus or minus sign (disallowed)
{"-100", 0, 0, false},
{"+100", 0, 0, false},
{"0.-10", 0, 0, false},
{"0.+10", 0, 0, false},
{"0a.00", 0, 0, false}, // invalid string for 'meter' part
{".1x", 0, 0, false}, // invalid string for 'cmeter' part
{".", 0, 0, false}, // empty 'cmeter' part (disallowed)
{"1.", 0, 0, false}, // ditto
{"m", 0, 0, false}, // only the "m" suffix
}
for _, tc := range tests {
tc := tc
t.Run(tc.token, func(t *testing.T) {
// In all cases the expected result is the same with or without the 'm' suffix.
// So we test both cases using the same test code.
for _, sfx := range []string{"", "m"} {
token := tc.token + sfx
e, m, ok := stringToCm(token)
if ok != tc.ok {
t.Fatal("unexpected validation result")
}
if m != tc.m {
t.Fatalf("Expected %d, got %d", tc.m, m)
}
if e != tc.e {
t.Fatalf("Expected %d, got %d", tc.e, e)
}
}
})
}
}
2013-01-16 21:48:43 +11:00
func TestParseDS(t *testing.T) {
dt := map[string]string{
2013-01-29 00:34:18 +11:00
"example.net. 3600 IN DS 40692 12 3 22261A8B0E0D799183E35E24E2AD6BB58533CBA7E3B14D659E9CA09B 2071398F": "example.net.\t3600\tIN\tDS\t40692 12 3 22261A8B0E0D799183E35E24E2AD6BB58533CBA7E3B14D659E9CA09B2071398F",
2013-01-16 21:48:43 +11:00
}
for i, o := range dt {
rr, err := NewRR(i)
if err != nil {
t.Error("failed to parse RR: ", err)
2013-01-16 21:48:43 +11:00
continue
}
if rr.String() != o {
t.Errorf("`%s' should be equal to\n`%s', but is `%s'", i, o, rr.String())
2013-01-16 21:48:43 +11:00
}
}
}
func TestQuotes(t *testing.T) {
tests := map[string]string{
2012-02-24 05:37:08 +11:00
`t.example.com. IN TXT "a bc"`: "t.example.com.\t3600\tIN\tTXT\t\"a bc\"",
`t.example.com. IN TXT "a
bc"`: "t.example.com.\t3600\tIN\tTXT\t\"a\\010 bc\"",
`t.example.com. IN TXT ""`: "t.example.com.\t3600\tIN\tTXT\t\"\"",
`t.example.com. IN TXT "a"`: "t.example.com.\t3600\tIN\tTXT\t\"a\"",
`t.example.com. IN TXT "aa"`: "t.example.com.\t3600\tIN\tTXT\t\"aa\"",
`t.example.com. IN TXT "aaa" ;`: "t.example.com.\t3600\tIN\tTXT\t\"aaa\"",
`t.example.com. IN TXT "abc" "DEF"`: "t.example.com.\t3600\tIN\tTXT\t\"abc\" \"DEF\"",
`t.example.com. IN TXT "abc" ( "DEF" )`: "t.example.com.\t3600\tIN\tTXT\t\"abc\" \"DEF\"",
`t.example.com. IN TXT aaa ;`: "t.example.com.\t3600\tIN\tTXT\t\"aaa\"",
`t.example.com. IN TXT aaa aaa;`: "t.example.com.\t3600\tIN\tTXT\t\"aaa\" \"aaa\"",
`t.example.com. IN TXT aaa aaa`: "t.example.com.\t3600\tIN\tTXT\t\"aaa\" \"aaa\"",
`t.example.com. IN TXT aaa`: "t.example.com.\t3600\tIN\tTXT\t\"aaa\"",
2012-02-24 05:37:08 +11:00
"cid.urn.arpa. NAPTR 100 50 \"s\" \"z3950+I2L+I2C\" \"\" _z3950._tcp.gatech.edu.": "cid.urn.arpa.\t3600\tIN\tNAPTR\t100 50 \"s\" \"z3950+I2L+I2C\" \"\" _z3950._tcp.gatech.edu.",
"cid.urn.arpa. NAPTR 100 50 \"s\" \"rcds+I2C\" \"\" _rcds._udp.gatech.edu.": "cid.urn.arpa.\t3600\tIN\tNAPTR\t100 50 \"s\" \"rcds+I2C\" \"\" _rcds._udp.gatech.edu.",
"cid.urn.arpa. NAPTR 100 50 \"s\" \"http+I2L+I2C+I2R\" \"\" _http._tcp.gatech.edu.": "cid.urn.arpa.\t3600\tIN\tNAPTR\t100 50 \"s\" \"http+I2L+I2C+I2R\" \"\" _http._tcp.gatech.edu.",
"cid.urn.arpa. NAPTR 100 10 \"\" \"\" \"/urn:cid:.+@([^\\.]+\\.)(.*)$/\\2/i\" .": "cid.urn.arpa.\t3600\tIN\tNAPTR\t100 10 \"\" \"\" \"/urn:cid:.+@([^\\.]+\\.)(.*)$/\\2/i\" .",
}
for i, o := range tests {
rr, err := NewRR(i)
if err != nil {
t.Error("failed to parse RR: ", err)
continue
}
if rr.String() != o {
t.Errorf("`%s' should be equal to\n`%s', but is\n`%s'", i, o, rr.String())
}
}
}
2014-06-16 05:39:23 +10:00
func TestParseClass(t *testing.T) {
tests := map[string]string{
"t.example.com. IN A 127.0.0.1": "t.example.com. 3600 IN A 127.0.0.1",
"t.example.com. CS A 127.0.0.1": "t.example.com. 3600 CS A 127.0.0.1",
"t.example.com. CH A 127.0.0.1": "t.example.com. 3600 CH A 127.0.0.1",
// ClassANY can not occur in zone files
// "t.example.com. ANY A 127.0.0.1": "t.example.com. 3600 ANY A 127.0.0.1",
"t.example.com. NONE A 127.0.0.1": "t.example.com. 3600 NONE A 127.0.0.1",
"t.example.com. CLASS255 A 127.0.0.1": "t.example.com. 3600 CLASS255 A 127.0.0.1",
2014-06-16 05:39:23 +10:00
}
for i, o := range tests {
rr, err := NewRR(i)
if err != nil {
t.Error("failed to parse RR: ", err)
2014-06-16 05:39:23 +10:00
continue
}
if rr.String() != o {
t.Errorf("`%s' should be equal to\n`%s', but is\n`%s'", i, o, rr.String())
2014-06-16 05:39:23 +10:00
}
}
}
2013-10-13 22:25:08 +11:00
func TestBrace(t *testing.T) {
tests := map[string]string{
"(miek.nl.) 3600 IN A 127.0.1.1": "miek.nl.\t3600\tIN\tA\t127.0.1.1",
"miek.nl. (3600) IN MX (10) elektron.atoom.net.": "miek.nl.\t3600\tIN\tMX\t10 elektron.atoom.net.",
`miek.nl. IN (
3600 A 127.0.0.1)`: "miek.nl.\t3600\tIN\tA\t127.0.0.1",
"(miek.nl.) (A) (127.0.2.1)": "miek.nl.\t3600\tIN\tA\t127.0.2.1",
"miek.nl A 127.0.3.1": "miek.nl.\t3600\tIN\tA\t127.0.3.1",
2012-02-24 05:37:08 +11:00
"_ssh._tcp.local. 60 IN (PTR) stora._ssh._tcp.local.": "_ssh._tcp.local.\t60\tIN\tPTR\tstora._ssh._tcp.local.",
"miek.nl. NS ns.miek.nl": "miek.nl.\t3600\tIN\tNS\tns.miek.nl.",
`(miek.nl.) (
2013-09-21 01:15:27 +10:00
(IN)
(AAAA)
(::1) )`: "miek.nl.\t3600\tIN\tAAAA\t::1",
`(miek.nl.) (
2013-09-21 01:15:27 +10:00
(IN)
(AAAA)
(::1))`: "miek.nl.\t3600\tIN\tAAAA\t::1",
"miek.nl. IN AAAA ::2": "miek.nl.\t3600\tIN\tAAAA\t::2",
2012-01-13 08:49:26 +11:00
`((m)(i)ek.(n)l.) (SOA) (soa.) (soa.) (
2012-01-12 20:50:01 +11:00
2009032802 ; serial
21600 ; refresh (6 hours)
7(2)00 ; retry (2 hours)
604()800 ; expire (1 week)
3600 ; minimum (1 hour)
)`: "miek.nl.\t3600\tIN\tSOA\tsoa. soa. 2009032802 21600 7200 604800 3600",
"miek\\.nl. IN A 127.0.0.10": "miek\\.nl.\t3600\tIN\tA\t127.0.0.10",
"miek.nl. IN A 127.0.0.11": "miek.nl.\t3600\tIN\tA\t127.0.0.11",
"miek.nl. A 127.0.0.12": "miek.nl.\t3600\tIN\tA\t127.0.0.12",
`miek.nl. 86400 IN SOA elektron.atoom.net. miekg.atoom.net. (
2009032802 ; serial
21600 ; refresh (6 hours)
7200 ; retry (2 hours)
604800 ; expire (1 week)
3600 ; minimum (1 hour)
)`: "miek.nl.\t86400\tIN\tSOA\telektron.atoom.net. miekg.atoom.net. 2009032802 21600 7200 604800 3600",
2011-07-24 07:43:43 +10:00
}
for i, o := range tests {
rr, err := NewRR(i)
if err != nil {
t.Errorf("failed to parse RR: %v\n\t%s", err, i)
continue
2011-12-16 20:26:32 +11:00
}
2011-12-16 08:40:07 +11:00
if rr.String() != o {
t.Errorf("`%s' should be equal to\n`%s', but is `%s'", i, o, rr.String())
2011-12-16 20:26:32 +11:00
}
2011-07-24 07:43:43 +10:00
}
2011-07-16 01:41:26 +10:00
}
func TestParseFailure(t *testing.T) {
tests := []string{"miek.nl. IN A 327.0.0.1",
"miek.nl. IN AAAA ::x",
"miek.nl. IN MX a0 miek.nl.",
"miek.nl aap IN MX mx.miek.nl.",
"miek.nl 200 IN mxx 10 mx.miek.nl.",
"miek.nl. inn MX 10 mx.miek.nl.",
// "miek.nl. IN CNAME ", // actually valid nowadays, zero size rdata
"miek.nl. IN CNAME ..",
"miek.nl. PA MX 10 miek.nl.",
"miek.nl. ) IN MX 10 miek.nl.",
}
for _, s := range tests {
2011-12-16 03:49:43 +11:00
_, err := NewRR(s)
if err == nil {
t.Errorf("should have triggered an error: \"%s\"", s)
}
}
}
2011-07-23 06:06:07 +10:00
func TestOmittedTTL(t *testing.T) {
zone := `
$ORIGIN example.com.
example.com. 42 IN SOA ns1.example.com. hostmaster.example.com. 1 86400 60 86400 3600 ; TTL=42 SOA
example.com. NS 2 ; TTL=42 absolute owner name
@ MD 3 ; TTL=42 current-origin owner name
MF 4 ; TTL=42 leading-space implied owner name
43 TYPE65280 \# 1 05 ; TTL=43 implied owner name explicit TTL
MB 6 ; TTL=43 leading-tab implied owner name
$TTL 1337
example.com. 88 MG 7 ; TTL=88 explicit TTL
example.com. MR 8 ; TTL=1337 after first $TTL
$TTL 314
1 TXT 9 ; TTL=1 implied owner name explicit TTL
example.com. DNAME 10 ; TTL=314 after second $TTL
2012-02-18 06:37:19 +11:00
`
reCaseFromComment := regexp.MustCompile(`TTL=(\d+)\s+(.*)`)
z := NewZoneParser(strings.NewReader(zone), "", "")
var i int
for rr, ok := z.Next(); ok; rr, ok = z.Next() {
i++
expected := reCaseFromComment.FindStringSubmatch(z.Comment())
if len(expected) != 3 {
t.Errorf("regexp didn't match for record %d", i)
continue
}
expectedTTL, _ := strconv.ParseUint(expected[1], 10, 32)
ttl := rr.Header().Ttl
if ttl != uint32(expectedTTL) {
t.Errorf("%s: expected TTL %d, got %d", expected[2], expectedTTL, ttl)
}
}
if err := z.Err(); err != nil {
t.Error(err)
}
if i != 10 {
t.Errorf("expected %d records, got %d", 5, i)
}
2012-02-18 06:37:19 +11:00
}
2012-02-19 21:27:16 +11:00
func TestRelativeNameErrors(t *testing.T) {
var badZones = []struct {
label string
zoneContents string
expectedErr string
}{
{
"relative owner name without origin",
"example.com 3600 IN SOA ns.example.com. hostmaster.example.com. 1 86400 60 86400 3600",
"bad owner name",
},
{
"relative owner name in RDATA",
"example.com. 3600 IN SOA ns hostmaster 1 86400 60 86400 3600",
"bad SOA Ns",
},
{
"origin reference without origin",
"@ 3600 IN SOA ns.example.com. hostmaster.example.com. 1 86400 60 86400 3600",
"bad owner name",
},
{
"relative owner name in $INCLUDE",
"$INCLUDE file.db example.com",
"bad origin name",
},
{
"relative owner name in $ORIGIN",
"$ORIGIN example.com",
"bad origin name",
},
}
for _, errorCase := range badZones {
z := NewZoneParser(strings.NewReader(errorCase.zoneContents), "", "")
z.Next()
if err := z.Err(); err == nil {
t.Errorf("%s: expected error, got nil", errorCase.label)
} else if !strings.Contains(err.Error(), errorCase.expectedErr) {
t.Errorf("%s: expected error `%s`, got `%s`", errorCase.label, errorCase.expectedErr, err)
}
2012-02-24 05:37:08 +11:00
}
2012-02-19 21:27:16 +11:00
}
2012-02-20 04:36:59 +11:00
func TestHIP(t *testing.T) {
h := `www.example.com. IN HIP ( 2 200100107B1A74DF365639CC39F1D578
AwEAAbdxyhNuSutc5EMzxTs9LBPCIkOFH8cIvM4p
9+LrV4e19WzK00+CI6zBCQTdtWsuxKbWIy87UOoJTwkUs7lBu+Upr1gsNrut79ryra+bSRGQ
b1slImA8YVJyuIDsj7kwzG7jnERNqnWxZ48AWkskmdHaVDP4BcelrTI3rMXdXF5D
rvs1.example.com.
rvs2.example.com. )`