2013-05-12 14:09:52 +00:00
|
|
|
|
// Copyright 2011 Miek Gieben. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2011-07-04 18:21:01 +00:00
|
|
|
|
package dns
|
|
|
|
|
|
|
|
|
|
import (
|
2012-01-11 20:03:47 +00:00
|
|
|
|
"net"
|
2011-07-04 18:21:01 +00:00
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2012-12-06 09:48:55 +00:00
|
|
|
|
// Query with way to long name
|
|
|
|
|
//./q mx bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.bla.miek.nl.miek.nl.miek123.nl.
|
|
|
|
|
|
2011-07-04 18:21:01 +00:00
|
|
|
|
func TestPackUnpack(t *testing.T) {
|
|
|
|
|
out := new(Msg)
|
|
|
|
|
out.Answer = make([]RR, 1)
|
2012-12-09 18:23:25 +00:00
|
|
|
|
key := new(DNSKEY)
|
|
|
|
|
key = &DNSKEY{Flags: 257, Protocol: 3, Algorithm: RSASHA1}
|
2011-08-08 13:12:24 +00:00
|
|
|
|
key.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeDNSKEY, Class: ClassINET, Ttl: 3600}
|
2011-07-04 18:21:01 +00:00
|
|
|
|
key.PublicKey = "AwEAAaHIwpx3w4VHKi6i1LHnTaWeHCL154Jug0Rtc9ji5qwPXpBo6A5sRv7cSsPQKPIwxLpyCrbJ4mr2L0EPOdvP6z6YfljK2ZmTbogU9aSU2fiq/4wjxbdkLyoDVgtO+JsxNN4bjr4WcWhsmk1Hg93FV9ZpkWb0Tbad8DFqNDzr//kZ"
|
|
|
|
|
|
|
|
|
|
out.Answer[0] = key
|
2012-10-09 20:15:33 +00:00
|
|
|
|
msg, err := out.Pack()
|
|
|
|
|
if err != nil {
|
2011-07-04 18:21:01 +00:00
|
|
|
|
t.Log("Failed to pack msg with DNSKEY")
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
in := new(Msg)
|
2012-10-09 20:15:33 +00:00
|
|
|
|
if in.Unpack(msg) != nil {
|
2011-07-04 18:21:01 +00:00
|
|
|
|
t.Log("Failed to unpack msg with DNSKEY")
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-09 18:23:25 +00:00
|
|
|
|
sig := new(RRSIG)
|
|
|
|
|
sig = &RRSIG{TypeCovered: TypeDNSKEY, Algorithm: RSASHA1, Labels: 2,
|
2011-07-04 18:21:01 +00:00
|
|
|
|
OrigTtl: 3600, Expiration: 4000, Inception: 4000, KeyTag: 34641, SignerName: "miek.nl.",
|
|
|
|
|
Signature: "AwEAAaHIwpx3w4VHKi6i1LHnTaWeHCL154Jug0Rtc9ji5qwPXpBo6A5sRv7cSsPQKPIwxLpyCrbJ4mr2L0EPOdvP6z6YfljK2ZmTbogU9aSU2fiq/4wjxbdkLyoDVgtO+JsxNN4bjr4WcWhsmk1Hg93FV9ZpkWb0Tbad8DFqNDzr//kZ"}
|
2011-08-08 13:12:24 +00:00
|
|
|
|
sig.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeRRSIG, Class: ClassINET, Ttl: 3600}
|
2011-07-04 18:21:01 +00:00
|
|
|
|
|
|
|
|
|
out.Answer[0] = sig
|
2012-10-09 20:15:33 +00:00
|
|
|
|
msg, err = out.Pack()
|
|
|
|
|
if err != nil {
|
2011-07-04 18:21:01 +00:00
|
|
|
|
t.Log("Failed to pack msg with RRSIG")
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-09 20:15:33 +00:00
|
|
|
|
if in.Unpack(msg) != nil {
|
2011-07-04 18:21:01 +00:00
|
|
|
|
t.Log("Failed to unpack msg with RRSIG")
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-11 20:03:47 +00:00
|
|
|
|
func TestPackUnpack2(t *testing.T) {
|
|
|
|
|
m := new(Msg)
|
|
|
|
|
m.Extra = make([]RR, 1)
|
|
|
|
|
m.Answer = make([]RR, 1)
|
2012-01-12 22:17:34 +00:00
|
|
|
|
dom := "miek.nl."
|
2012-12-09 18:23:25 +00:00
|
|
|
|
rr := new(A)
|
2012-01-11 20:03:47 +00:00
|
|
|
|
rr.Hdr = RR_Header{Name: dom, Rrtype: TypeA, Class: ClassINET, Ttl: 0}
|
|
|
|
|
rr.A = net.IPv4(127, 0, 0, 1)
|
|
|
|
|
|
2012-12-09 18:23:25 +00:00
|
|
|
|
x := new(TXT)
|
2012-01-11 20:03:47 +00:00
|
|
|
|
x.Hdr = RR_Header{Name: dom, Rrtype: TypeTXT, Class: ClassINET, Ttl: 0}
|
2012-02-12 20:50:02 +00:00
|
|
|
|
x.Txt = []string{"heelalaollo"}
|
2012-01-11 20:03:47 +00:00
|
|
|
|
|
|
|
|
|
m.Extra[0] = x
|
|
|
|
|
m.Answer[0] = rr
|
2012-10-09 20:15:33 +00:00
|
|
|
|
_, err := m.Pack()
|
|
|
|
|
if err != nil {
|
2012-01-11 20:03:47 +00:00
|
|
|
|
t.Log("Packing failed")
|
|
|
|
|
t.Fail()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-15 09:22:52 +00:00
|
|
|
|
func TestBailiwick(t *testing.T) {
|
2012-02-23 18:37:08 +00:00
|
|
|
|
yes := map[string]string{
|
|
|
|
|
"miek.nl": "ns.miek.nl",
|
|
|
|
|
".": "miek.nl",
|
|
|
|
|
}
|
|
|
|
|
for parent, child := range yes {
|
|
|
|
|
if !IsSubDomain(parent, child) {
|
|
|
|
|
t.Logf("%s should be child of %s\n", child, parent)
|
2013-06-22 07:21:15 +00:00
|
|
|
|
t.Logf("comparelabels %d", CompareDomainName(parent, child))
|
|
|
|
|
t.Logf("lenlabels %d %d", CountLabel(parent), CountLabel(child))
|
2012-02-23 18:37:08 +00:00
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
no := map[string]string{
|
2012-10-15 14:12:37 +00:00
|
|
|
|
"www.miek.nl": "ns.miek.nl",
|
|
|
|
|
"m\\.iek.nl": "ns.miek.nl",
|
|
|
|
|
"w\\.iek.nl": "w.iek.nl",
|
|
|
|
|
"p\\\\.iek.nl": "ns.p.iek.nl", // p\\.iek.nl , literal \ in domain name
|
|
|
|
|
"miek.nl": ".",
|
2012-02-23 18:37:08 +00:00
|
|
|
|
}
|
|
|
|
|
for parent, child := range no {
|
|
|
|
|
if IsSubDomain(parent, child) {
|
|
|
|
|
t.Logf("%s should not be child of %s\n", child, parent)
|
2013-06-22 07:21:15 +00:00
|
|
|
|
t.Logf("comparelabels %d", CompareDomainName(parent, child))
|
|
|
|
|
t.Logf("lenlabels %d %d", CountLabel(parent), CountLabel(child))
|
2012-02-23 18:37:08 +00:00
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-15 09:22:52 +00:00
|
|
|
|
}
|
2012-02-28 19:43:08 +00:00
|
|
|
|
|
|
|
|
|
func TestPack(t *testing.T) {
|
|
|
|
|
rr := []string{"US. 86400 IN NSEC 0-.us. NS SOA RRSIG NSEC DNSKEY TYPE65534"}
|
|
|
|
|
m := new(Msg)
|
|
|
|
|
var err error
|
|
|
|
|
m.Answer = make([]RR, 1)
|
|
|
|
|
for _, r := range rr {
|
|
|
|
|
m.Answer[0], err = NewRR(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Logf("Failed to create RR: %s\n", err.Error())
|
2012-02-28 19:47:25 +00:00
|
|
|
|
t.Fail()
|
2012-02-28 19:43:08 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
2012-10-09 20:15:33 +00:00
|
|
|
|
if _, err := m.Pack(); err != nil {
|
2013-06-27 19:01:32 +00:00
|
|
|
|
t.Logf("Packing failed: %s\n", err.Error())
|
2012-02-28 19:43:08 +00:00
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-28 16:21:23 +00:00
|
|
|
|
x := new(Msg)
|
|
|
|
|
ns, _ := NewRR("pool.ntp.org. 390 IN NS a.ntpns.org")
|
2012-12-09 18:23:25 +00:00
|
|
|
|
ns.(*NS).Ns = "a.ntpns.org"
|
2012-08-28 16:21:23 +00:00
|
|
|
|
x.Ns = append(m.Ns, ns)
|
|
|
|
|
x.Ns = append(m.Ns, ns)
|
|
|
|
|
x.Ns = append(m.Ns, ns)
|
|
|
|
|
// This crashes due to the fact the a.ntpns.org isn't a FQDN
|
|
|
|
|
// How to recover() from a remove panic()?
|
2012-10-09 20:15:33 +00:00
|
|
|
|
if _, err := x.Pack(); err == nil {
|
2012-08-28 16:25:55 +00:00
|
|
|
|
t.Log("Packing should fail")
|
2012-08-28 16:21:23 +00:00
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
x.Answer = make([]RR, 1)
|
|
|
|
|
x.Answer[0], err = NewRR(rr[0])
|
2012-10-09 20:15:33 +00:00
|
|
|
|
if _, err := x.Pack(); err == nil {
|
2012-08-31 08:13:21 +00:00
|
|
|
|
t.Log("Packing should fail")
|
2012-08-28 16:21:23 +00:00
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
2012-08-29 06:34:47 +00:00
|
|
|
|
x.Question = make([]Question, 1)
|
|
|
|
|
x.Question[0] = Question{";sd#eddddséâèµââ
â¥âxzztsestxssweewwsssstx@s@Zåµe@cn.pool.ntp.org.", TypeA, ClassINET}
|
2012-10-09 20:15:33 +00:00
|
|
|
|
if _, err := x.Pack(); err == nil {
|
2012-08-31 08:13:21 +00:00
|
|
|
|
t.Log("Packing should fail")
|
2012-08-29 06:34:47 +00:00
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
2012-02-28 19:43:08 +00:00
|
|
|
|
}
|
2012-08-26 20:10:55 +00:00
|
|
|
|
|
2013-06-26 19:33:01 +00:00
|
|
|
|
func TestCompressLength(t *testing.T) {
|
2012-08-26 20:10:55 +00:00
|
|
|
|
m := new(Msg)
|
|
|
|
|
m.SetQuestion("miek.nl", TypeMX)
|
|
|
|
|
ul := m.Len()
|
|
|
|
|
m.Compress = true
|
2012-09-10 18:51:19 +00:00
|
|
|
|
if ul != m.Len() {
|
2012-08-26 20:10:55 +00:00
|
|
|
|
t.Fatalf("Should be equal")
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-30 14:55:50 +00:00
|
|
|
|
|
2013-06-26 19:33:01 +00:00
|
|
|
|
// Does the predicted length match final packed length
|
|
|
|
|
func TestMsgLenTest(t *testing.T) {
|
2013-06-26 21:18:09 +00:00
|
|
|
|
makeMsg := func(question string, ans, ns, e []RR) *Msg {
|
|
|
|
|
msg := new(Msg)
|
|
|
|
|
msg.SetQuestion(Fqdn(question), TypeANY)
|
|
|
|
|
msg.Answer = append(msg.Answer, ans...)
|
|
|
|
|
msg.Ns = append(msg.Ns, ns...)
|
|
|
|
|
msg.Extra = append(msg.Extra, e...)
|
|
|
|
|
msg.Compress = true
|
|
|
|
|
return msg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name1 := "12345678901234567890123456789012345.12345678.123."
|
|
|
|
|
rrA, _ := NewRR(name1 + " 3600 IN A 192.0.2.1")
|
|
|
|
|
rrMx, _ := NewRR(name1 + " 3600 IN MX 10 " + name1)
|
|
|
|
|
tests := []*Msg{
|
|
|
|
|
makeMsg(name1, []RR{rrA}, nil, nil),
|
|
|
|
|
makeMsg(name1, []RR{rrMx, rrMx}, nil, nil)}
|
2013-06-26 19:33:01 +00:00
|
|
|
|
|
|
|
|
|
for _, msg := range tests {
|
2013-06-26 21:18:09 +00:00
|
|
|
|
predicted := msg.Len()
|
|
|
|
|
buf, err := msg.Pack()
|
2013-06-26 19:33:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
2013-06-26 21:18:09 +00:00
|
|
|
|
if predicted != len(buf) {
|
|
|
|
|
t.Errorf("Predicted length is wrong: predicted %s (len=%d) %d, actual %d\n",
|
|
|
|
|
msg.Question[0].Name, len(msg.Answer), predicted, len(buf))
|
2013-06-26 19:33:01 +00:00
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 09:47:53 +00:00
|
|
|
|
func BenchmarkMsgLen(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
makeMsg := func(question string, ans, ns, e []RR) *Msg {
|
|
|
|
|
msg := new(Msg)
|
|
|
|
|
msg.SetQuestion(Fqdn(question), TypeANY)
|
|
|
|
|
msg.Answer = append(msg.Answer, ans...)
|
|
|
|
|
msg.Ns = append(msg.Ns, ns...)
|
|
|
|
|
msg.Extra = append(msg.Extra, e...)
|
|
|
|
|
msg.Compress = true
|
|
|
|
|
return msg
|
|
|
|
|
}
|
|
|
|
|
name1 := "12345678901234567890123456789012345.12345678.123."
|
|
|
|
|
rrMx, _ := NewRR(name1 + " 3600 IN MX 10 " + name1)
|
|
|
|
|
msg := makeMsg(name1, []RR{rrMx, rrMx}, nil, nil)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
msg.Len()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkMsgLenPack(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
makeMsg := func(question string, ans, ns, e []RR) *Msg {
|
|
|
|
|
msg := new(Msg)
|
|
|
|
|
msg.SetQuestion(Fqdn(question), TypeANY)
|
|
|
|
|
msg.Answer = append(msg.Answer, ans...)
|
|
|
|
|
msg.Ns = append(msg.Ns, ns...)
|
|
|
|
|
msg.Extra = append(msg.Extra, e...)
|
|
|
|
|
msg.Compress = true
|
|
|
|
|
return msg
|
|
|
|
|
}
|
|
|
|
|
name1 := "12345678901234567890123456789012345.12345678.123."
|
|
|
|
|
rrMx, _ := NewRR(name1 + " 3600 IN MX 10 " + name1)
|
|
|
|
|
msg := makeMsg(name1, []RR{rrMx, rrMx}, nil, nil)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
b, _ := msg.Pack()
|
|
|
|
|
_ = len(b)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-30 14:55:50 +00:00
|
|
|
|
func TestToRFC3597(t *testing.T) {
|
|
|
|
|
a, _ := NewRR("miek.nl. IN A 10.0.1.1")
|
2013-02-08 15:47:21 +00:00
|
|
|
|
x := new(RFC3597)
|
|
|
|
|
x.ToRFC3597(a)
|
2013-01-30 14:55:50 +00:00
|
|
|
|
if x.String() != `miek.nl. 3600 IN A \# 4 0a000101` {
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-26 04:38:10 +00:00
|
|
|
|
|
2013-08-27 04:24:06 +00:00
|
|
|
|
func TestNoRdataPack(t *testing.T) {
|
2013-08-26 04:38:10 +00:00
|
|
|
|
data := make([]byte, 1024)
|
2013-08-27 04:11:24 +00:00
|
|
|
|
for typ, fn := range rr_mk {
|
|
|
|
|
r := fn()
|
2013-08-26 04:38:10 +00:00
|
|
|
|
*r.Header() = RR_Header{Name: "miek.nl.", Rrtype: typ, Class: ClassINET, Ttl: 3600}
|
|
|
|
|
_, e := PackRR(r, data, 0, nil, false)
|
|
|
|
|
if e != nil {
|
2013-08-27 04:24:06 +00:00
|
|
|
|
t.Logf("Failed to pack RR with zero rdata: %s: %s\n", TypeToString[typ], e.Error())
|
2013-08-26 04:38:10 +00:00
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-27 04:24:06 +00:00
|
|
|
|
|
|
|
|
|
// TODO(miek): fix dns buffer too small errors this throws
|
2013-08-31 19:24:52 +00:00
|
|
|
|
func TestNoRdataUnpack(t *testing.T) {
|
2013-08-27 04:24:06 +00:00
|
|
|
|
data := make([]byte, 1024)
|
|
|
|
|
for typ, fn := range rr_mk {
|
2013-08-31 19:24:52 +00:00
|
|
|
|
if typ == TypeSOA || typ == TypeTSIG || typ == TypeWKS {
|
2013-08-31 19:26:24 +00:00
|
|
|
|
// SOA, TSIG will not be seen (like this) in dyn. updates?
|
2013-08-31 19:24:52 +00:00
|
|
|
|
// WKS is an bug, but...deprecated record.
|
|
|
|
|
continue
|
|
|
|
|
}
|
2013-08-27 04:24:06 +00:00
|
|
|
|
r := fn()
|
|
|
|
|
*r.Header() = RR_Header{Name: "miek.nl.", Rrtype: typ, Class: ClassINET, Ttl: 3600}
|
|
|
|
|
off, e := PackRR(r, data, 0, nil, false)
|
|
|
|
|
if e != nil {
|
|
|
|
|
// Should always works, TestNoDataPack should have catched this
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
rr, _, e := UnpackRR(data[:off], 0)
|
|
|
|
|
if e != nil {
|
|
|
|
|
t.Logf("Failed to unpack RR with zero rdata: %s: %s\n", TypeToString[typ], e.Error())
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
t.Logf("%s\n", rr)
|
|
|
|
|
}
|
|
|
|
|
}
|