diff --git a/parse_test.go b/parse_test.go index 12de8224..ab9f37e9 100644 --- a/parse_test.go +++ b/parse_test.go @@ -336,6 +336,7 @@ func TestNSEC(t *testing.T) { "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", } for i, o := range nsectests { rr, e := NewRR(i) @@ -513,6 +514,8 @@ func TestParseFailure(t *testing.T) { "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.", @@ -761,6 +764,32 @@ func TestEmpty(t *testing.T) { } } +func TestLowercaseTokens(t *testing.T) { + var testrecords = []string{ + "example.org. 300 IN a 1.2.3.4", + "example.org. 300 in A 1.2.3.4", + "example.org. 300 in a 1.2.3.4", + "example.org. 300 a 1.2.3.4", + "example.org. 300 A 1.2.3.4", + "example.org. IN a 1.2.3.4", + "example.org. in A 1.2.3.4", + "example.org. in a 1.2.3.4", + "example.org. a 1.2.3.4", + "example.org. A 1.2.3.4", + "example.org. a 1.2.3.4", + "$ORIGIN example.org.\n a 1.2.3.4", + "$Origin example.org.\n a 1.2.3.4", + "$origin example.org.\n a 1.2.3.4", + "example.org. Class1 Type1 1.2.3.4", + } + for _, testrr := range testrecords { + _, err := NewRR(testrr) + if err != nil { + t.Errorf("failed to parse %#v, got %s", testrr, err.Error()) + } + } +} + func ExampleGenerate() { // From the manual: http://www.bind9.net/manual/bind/9.3.2/Bv9ARM.ch06.html#id2566761 zone := "$GENERATE 1-2 0 NS SERVER$.EXAMPLE.\n$GENERATE 1-8 $ CNAME $.0" @@ -1051,7 +1080,7 @@ func TestTXT(t *testing.T) { func TestTypeXXXX(t *testing.T) { _, err := NewRR("example.com IN TYPE1234 \\# 4 aabbccdd") if err != nil { - t.Logf("failed to parse TYPE1234 RR: ", err.Error()) + t.Logf("failed to parse TYPE1234 RR: %s", err.Error()) t.Fail() } _, err = NewRR("example.com IN TYPE655341 \\# 8 aabbccddaabbccdd") diff --git a/zscan.go b/zscan.go index 7a935736..91df3429 100644 --- a/zscan.go +++ b/zscan.go @@ -88,14 +88,15 @@ func (e *ParseError) Error() (s string) { } type lex struct { - token string // text of the token - length int // lenght of the token - err bool // when true, token text has lexer error - value uint8 // value: _STRING, _BLANK, etc. - line int // line in the file - column int // column in the file - torc uint16 // type or class as parsed in the lexer, we only need to look this up in the grammar - comment string // any comment text seen + token string // text of the token + tokenUpper string // uppercase text of the token + length int // lenght of the token + err bool // when true, token text has lexer error + value uint8 // value: _STRING, _BLANK, etc. + line int // line in the file + column int // column in the file + torc uint16 // type or class as parsed in the lexer, we only need to look this up in the grammar + comment string // any comment text seen } // *Tokens are returned when a zone file is parsed. @@ -536,9 +537,10 @@ func zlexer(s *scan, c chan lex) { // If we have a string and its the first, make it an owner l.value = _OWNER l.token = string(str[:stri]) + l.tokenUpper = strings.ToUpper(l.token) l.length = stri // escape $... start with a \ not a $, so this will work - switch l.token { + switch l.tokenUpper { case "$TTL": l.value = _DIRTTL case "$ORIGIN": @@ -553,14 +555,15 @@ func zlexer(s *scan, c chan lex) { } else { l.value = _STRING l.token = string(str[:stri]) + l.tokenUpper = strings.ToUpper(l.token) l.length = stri if !rrtype { - if t, ok := StringToType[l.token]; ok { + if t, ok := StringToType[l.tokenUpper]; ok { l.value = _RRTYPE l.torc = t rrtype = true } else { - if strings.HasPrefix(l.token, "TYPE") { + if strings.HasPrefix(l.tokenUpper, "TYPE") { if t, ok := typeToInt(l.token); !ok { l.token = "unknown RR type" l.err = true @@ -572,11 +575,11 @@ func zlexer(s *scan, c chan lex) { } } } - if t, ok := StringToClass[l.token]; ok { + if t, ok := StringToClass[l.tokenUpper]; ok { l.value = _CLASS l.torc = t } else { - if strings.HasPrefix(l.token, "CLASS") { + if strings.HasPrefix(l.tokenUpper, "CLASS") { if t, ok := classToInt(l.token); !ok { l.token = "unknown class" l.err = true @@ -668,9 +671,11 @@ func zlexer(s *scan, c chan lex) { if stri != 0 { l.value = _STRING l.token = string(str[:stri]) + l.tokenUpper = strings.ToUpper(l.token) + l.length = stri if !rrtype { - if t, ok := StringToType[strings.ToUpper(l.token)]; ok { + if t, ok := StringToType[l.tokenUpper]; ok { l.value = _RRTYPE l.torc = t rrtype = true diff --git a/zscan_rr.go b/zscan_rr.go index dc553855..c58c91c6 100644 --- a/zscan_rr.go +++ b/zscan_rr.go @@ -1145,9 +1145,9 @@ func setRRSIG(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) { rr := new(RRSIG) rr.Hdr = h l := <-c - if t, ok := StringToType[strings.ToUpper(l.token)]; !ok { - if strings.HasPrefix(strings.ToUpper(l.token), "TYPE") { - if t, ok = typeToInt(l.token); !ok { + if t, ok := StringToType[l.tokenUpper]; !ok { + if strings.HasPrefix(l.tokenUpper, "TYPE") { + if t, ok = typeToInt(l.tokenUpper); !ok { return nil, &ParseError{f, "bad RRSIG Typecovered", l}, "" } else { rr.TypeCovered = t @@ -1261,8 +1261,8 @@ func setNSEC(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) { case _BLANK: // Ok case _STRING: - if k, ok = StringToType[strings.ToUpper(l.token)]; !ok { - if k, ok = typeToInt(l.token); !ok { + if k, ok = StringToType[l.tokenUpper]; !ok { + if k, ok = typeToInt(l.tokenUpper); !ok { return nil, &ParseError{f, "bad NSEC TypeBitMap", l}, "" } } @@ -1323,8 +1323,8 @@ func setNSEC3(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) { case _BLANK: // Ok case _STRING: - if k, ok = StringToType[strings.ToUpper(l.token)]; !ok { - if k, ok = typeToInt(l.token); !ok { + if k, ok = StringToType[l.tokenUpper]; !ok { + if k, ok = typeToInt(l.tokenUpper); !ok { return nil, &ParseError{f, "bad NSEC3 TypeBitMap", l}, "" } } @@ -1580,7 +1580,7 @@ func setDS(h RR_Header, c chan lex, f string) (RR, *ParseError, string) { <-c // _BLANK l = <-c if i, e := strconv.Atoi(l.token); e != nil { - if i, ok := StringToAlgorithm[strings.ToUpper(l.token)]; !ok { + if i, ok := StringToAlgorithm[l.tokenUpper]; !ok { return nil, &ParseError{f, "bad DS Algorithm", l}, "" } else { rr.Algorithm = i @@ -1681,7 +1681,7 @@ func setCDS(h RR_Header, c chan lex, f string) (RR, *ParseError, string) { <-c // _BLANK l = <-c if i, e := strconv.Atoi(l.token); e != nil { - if i, ok := StringToAlgorithm[strings.ToUpper(l.token)]; !ok { + if i, ok := StringToAlgorithm[l.tokenUpper]; !ok { return nil, &ParseError{f, "bad CDS Algorithm", l}, "" } else { rr.Algorithm = i @@ -1716,7 +1716,7 @@ func setDLV(h RR_Header, c chan lex, f string) (RR, *ParseError, string) { <-c // _BLANK l = <-c if i, e := strconv.Atoi(l.token); e != nil { - if i, ok := StringToAlgorithm[strings.ToUpper(l.token)]; !ok { + if i, ok := StringToAlgorithm[l.tokenUpper]; !ok { return nil, &ParseError{f, "bad DLV Algorithm", l}, "" } else { rr.Algorithm = i @@ -1751,7 +1751,7 @@ func setTA(h RR_Header, c chan lex, f string) (RR, *ParseError, string) { <-c // _BLANK l = <-c if i, e := strconv.Atoi(l.token); e != nil { - if i, ok := StringToAlgorithm[strings.ToUpper(l.token)]; !ok { + if i, ok := StringToAlgorithm[l.tokenUpper]; !ok { return nil, &ParseError{f, "bad TA Algorithm", l}, "" } else { rr.Algorithm = i