dns/dns_test.go

155 lines
4.3 KiB
Go
Raw Normal View History

2011-07-05 04:21:01 +10:00
package dns
import (
2012-01-12 07:03:47 +11:00
"net"
2011-07-05 04:21:01 +10:00
"testing"
)
// 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-05 04:21:01 +10:00
func TestPackUnpack(t *testing.T) {
out := new(Msg)
out.Answer = make([]RR, 1)
key := new(RR_DNSKEY)
key = &RR_DNSKEY{Flags: 257, Protocol: 3, Algorithm: RSASHA1}
2011-08-08 23:12:24 +10:00
key.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeDNSKEY, Class: ClassINET, Ttl: 3600}
2011-07-05 04:21:01 +10:00
key.PublicKey = "AwEAAaHIwpx3w4VHKi6i1LHnTaWeHCL154Jug0Rtc9ji5qwPXpBo6A5sRv7cSsPQKPIwxLpyCrbJ4mr2L0EPOdvP6z6YfljK2ZmTbogU9aSU2fiq/4wjxbdkLyoDVgtO+JsxNN4bjr4WcWhsmk1Hg93FV9ZpkWb0Tbad8DFqNDzr//kZ"
out.Answer[0] = key
2012-10-10 07:15:33 +11:00
msg, err := out.Pack()
if err != nil {
2011-07-05 04:21:01 +10:00
t.Log("Failed to pack msg with DNSKEY")
t.Fail()
}
in := new(Msg)
2012-10-10 07:15:33 +11:00
if in.Unpack(msg) != nil {
2011-07-05 04:21:01 +10:00
t.Log("Failed to unpack msg with DNSKEY")
t.Fail()
}
sig := new(RR_RRSIG)
sig = &RR_RRSIG{TypeCovered: TypeDNSKEY, Algorithm: RSASHA1, Labels: 2,
2011-07-05 04:21:01 +10:00
OrigTtl: 3600, Expiration: 4000, Inception: 4000, KeyTag: 34641, SignerName: "miek.nl.",
Signature: "AwEAAaHIwpx3w4VHKi6i1LHnTaWeHCL154Jug0Rtc9ji5qwPXpBo6A5sRv7cSsPQKPIwxLpyCrbJ4mr2L0EPOdvP6z6YfljK2ZmTbogU9aSU2fiq/4wjxbdkLyoDVgtO+JsxNN4bjr4WcWhsmk1Hg93FV9ZpkWb0Tbad8DFqNDzr//kZ"}
2011-08-08 23:12:24 +10:00
sig.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeRRSIG, Class: ClassINET, Ttl: 3600}
2011-07-05 04:21:01 +10:00
out.Answer[0] = sig
2012-10-10 07:15:33 +11:00
msg, err = out.Pack()
if err != nil {
2011-07-05 04:21:01 +10:00
t.Log("Failed to pack msg with RRSIG")
t.Fail()
}
2012-10-10 07:15:33 +11:00
if in.Unpack(msg) != nil {
2011-07-05 04:21:01 +10:00
t.Log("Failed to unpack msg with RRSIG")
t.Fail()
}
}
2012-01-12 07:03:47 +11:00
func TestPackUnpack2(t *testing.T) {
m := new(Msg)
m.Extra = make([]RR, 1)
m.Answer = make([]RR, 1)
2012-01-13 09:17:34 +11:00
dom := "miek.nl."
rr := new(RR_A)
2012-01-12 07:03:47 +11:00
rr.Hdr = RR_Header{Name: dom, Rrtype: TypeA, Class: ClassINET, Ttl: 0}
rr.A = net.IPv4(127, 0, 0, 1)
x := new(RR_TXT)
x.Hdr = RR_Header{Name: dom, Rrtype: TypeTXT, Class: ClassINET, Ttl: 0}
x.Txt = []string{"heelalaollo"}
2012-01-12 07:03:47 +11:00
m.Extra[0] = x
m.Answer[0] = rr
2012-10-10 07:15:33 +11:00
_, err := m.Pack()
if err != nil {
2012-01-12 07:03:47 +11:00
t.Log("Packing failed")
t.Fail()
return
}
}
2012-02-15 20:22:52 +11:00
func TestBailiwick(t *testing.T) {
2012-02-24 05:37:08 +11: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)
2012-08-08 23:16:22 +10:00
t.Logf("comparelabels %d", CompareLabels(parent, child))
t.Logf("lenlabels %d %d", LenLabels(parent), LenLabels(child))
2012-02-24 05:37:08 +11:00
t.Fail()
}
}
no := map[string]string{
2012-10-16 01:12:37 +11: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-24 05:37:08 +11:00
}
for parent, child := range no {
if IsSubDomain(parent, child) {
t.Logf("%s should not be child of %s\n", child, parent)
2012-08-08 23:16:22 +10:00
t.Logf("comparelabels %d", CompareLabels(parent, child))
t.Logf("lenlabels %d %d", LenLabels(parent), LenLabels(child))
2012-02-24 05:37:08 +11:00
t.Fail()
}
}
2012-02-15 20:22:52 +11:00
}
2012-02-29 06:43:08 +11: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-29 06:47:25 +11:00
t.Fail()
2012-02-29 06:43:08 +11:00
continue
}
2012-10-10 07:15:33 +11:00
if _, err := m.Pack(); err != nil {
2012-02-29 06:43:08 +11:00
t.Log("Packing failed")
t.Fail()
}
}
x := new(Msg)
ns, _ := NewRR("pool.ntp.org. 390 IN NS a.ntpns.org")
2012-08-29 02:25:55 +10:00
ns.(*RR_NS).Ns = "a.ntpns.org"
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-10 07:15:33 +11:00
if _, err := x.Pack(); err == nil {
2012-08-29 02:25:55 +10:00
t.Log("Packing should fail")
t.Fail()
}
x.Answer = make([]RR, 1)
x.Answer[0], err = NewRR(rr[0])
2012-10-10 07:15:33 +11:00
if _, err := x.Pack(); err == nil {
2012-08-31 18:13:21 +10:00
t.Log("Packing should fail")
t.Fail()
}
2012-08-29 16:34:47 +10:00
x.Question = make([]Question, 1)
x.Question[0] = Question{";sd#edddds鍛↙赏‘℅∥↙xzztsestxssweewwsssstx@s@Z嵌e@cn.pool.ntp.org.", TypeA, ClassINET}
2012-10-10 07:15:33 +11:00
if _, err := x.Pack(); err == nil {
2012-08-31 18:13:21 +10:00
t.Log("Packing should fail")
2012-08-29 16:34:47 +10:00
t.Fail()
}
2012-02-29 06:43:08 +11:00
}
2012-08-27 06:10:55 +10:00
func TestCompressLenght(t *testing.T) {
m := new(Msg)
m.SetQuestion("miek.nl", TypeMX)
ul := m.Len()
m.Compress = true
2012-09-11 04:51:19 +10:00
if ul != m.Len() {
2012-08-27 06:10:55 +10:00
t.Fatalf("Should be equal")
}
}