dns/client_test.go

162 lines
3.1 KiB
Go
Raw Normal View History

2013-05-13 00:09:52 +10: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-05 04:18:51 +10:00
package dns
import (
"testing"
2011-07-24 07:43:43 +10:00
"time"
2011-07-05 04:18:51 +10:00
)
func TestClientSync(t *testing.T) {
m := new(Msg)
2012-01-09 01:34:42 +11:00
m.SetQuestion("miek.nl.", TypeSOA)
2011-07-05 04:18:51 +10:00
2012-05-26 18:28:32 +10:00
c := new(Client)
2012-11-20 02:19:17 +11:00
r, _, _ := c.Exchange(m, "37.251.95.53:53")
2011-07-05 04:18:51 +10:00
if r != nil && r.Rcode != RcodeSuccess {
t.Log("Failed to get an valid answer")
t.Fail()
t.Logf("%v\n", r)
}
}
2011-07-06 16:51:51 +10:00
func TestClientEDNS0(t *testing.T) {
m := new(Msg)
2012-01-09 01:34:42 +11:00
m.SetQuestion("miek.nl.", TypeDNSKEY)
2011-07-05 04:18:51 +10:00
2011-11-28 16:03:21 +11:00
m.SetEdns0(2048, true)
2011-09-11 21:01:18 +10:00
//edns.Option = make([]Option, 1)
//edns.SetNsid("") // Empty to request it
2011-07-05 04:18:51 +10:00
2012-05-26 18:28:40 +10:00
c := new(Client)
2012-11-20 02:19:17 +11:00
r, _, _ := c.Exchange(m, "37.251.95.53:53")
2011-07-06 16:51:51 +10:00
if r != nil && r.Rcode != RcodeSuccess {
t.Log("Failed to get an valid answer")
t.Fail()
t.Logf("%v\n", r)
2011-07-05 04:18:51 +10:00
}
}
2013-09-06 19:49:07 +10:00
func TestSingleSingleInflight(t *testing.T) {
m := new(Msg)
m.SetQuestion("miek.nl.", TypeDNSKEY)
c := new(Client)
2013-09-06 19:49:07 +10:00
c.SingleInflight = true
nr := 10
ch := make(chan time.Duration)
for i := 0; i < nr; i++ {
go func() {
_, rtt, _ := c.Exchange(m, "37.251.95.53:53")
ch <- rtt
}()
}
i := 0
var first time.Duration
// With inflight *all* rtt are identical, and by doing actual lookups
// the changes that this is a coincidence is small.
Loop:
for {
select {
case rtt := <-ch:
if i == 0 {
first = rtt
} else {
if first != rtt {
t.Log("All rtt should be equal")
t.Fail()
}
}
i++
if i == 10 {
break Loop
}
}
}
}
/*
func TestClientTsigAXFR(t *testing.T) {
2011-07-05 04:18:51 +10:00
m := new(Msg)
m.SetAxfr("miek.nl.")
2012-05-20 20:44:49 +10:00
m.SetTsig("axfr.", HmacMD5, 300, time.Now().Unix())
2011-07-24 07:43:43 +10:00
2012-05-26 18:28:32 +10:00
c := new(Client)
2012-03-03 07:19:37 +11:00
c.TsigSecret = map[string]string{"axfr.": "so6ZGir4GPAqINNh9U5c3A=="}
2011-07-24 07:43:43 +10:00
c.Net = "tcp"
2012-12-02 19:31:42 +11:00
if a, err := c.TransferIn(m, "37.251.95.53:53"); err != nil {
2012-10-10 07:45:19 +11:00
t.Log("Failed to setup axfr: " + err.Error())
2013-08-26 14:38:10 +10:00
t.Fatal()
2012-05-20 20:44:49 +10:00
} else {
for ex := range a {
if ex.Error != nil {
t.Logf("Error %s\n", ex.Error.Error())
t.Fail()
break
}
2012-08-31 22:59:56 +10:00
for _, rr := range ex.RR {
t.Logf("%s\n", rr.String())
2012-05-20 20:44:49 +10:00
}
2012-01-13 09:17:34 +11:00
}
2011-09-12 01:01:55 +10:00
}
}
*/
2011-09-12 01:01:55 +10:00
/*
2011-09-12 01:01:55 +10:00
func TestClientAXFRMultipleMessages(t *testing.T) {
m := new(Msg)
m.SetAxfr("dnsex.nl.")
2012-05-26 18:28:32 +10:00
c := new(Client)
2011-09-12 01:01:55 +10:00
c.Net = "tcp"
2012-12-02 19:31:42 +11:00
if a, err := c.TransferIn(m, "37.251.95.53:53"); err != nil {
2011-12-11 06:54:26 +11:00
t.Log("Failed to setup axfr" + err.Error())
2011-09-12 01:01:55 +10:00
t.Fail()
2012-01-13 09:17:34 +11:00
return
2012-05-20 20:44:49 +10:00
} else {
for ex := range a {
if ex.Error != nil {
t.Logf("Error %s\n", ex.Error.Error())
t.Fail()
break
}
2012-01-13 09:17:34 +11:00
}
2011-09-11 08:31:03 +10:00
}
2011-07-05 04:18:51 +10:00
}
*/
// not really a test, but shows how to use update leases
func TestUpdateLeaseTSIG(t *testing.T) {
m := new(Msg)
m.SetUpdate("t.local.ip6.io.")
rr, _ := NewRR("t.local.ip6.io. 30 A 127.0.0.1")
rrs := make([]RR, 1)
rrs[0] = rr
m.Insert(rrs)
lease_rr := new(OPT)
lease_rr.Hdr.Name = "."
lease_rr.Hdr.Rrtype = TypeOPT
2013-05-09 16:27:49 +10:00
e := new(EDNS0_UL)
e.Code = EDNS0UL
e.Lease = 120
lease_rr.Option = append(lease_rr.Option, e)
m.Extra = append(m.Extra, lease_rr)
c := new(Client)
m.SetTsig("polvi.", HmacMD5, 300, time.Now().Unix())
c.TsigSecret = map[string]string{"polvi.": "pRZgBrBvI4NAHZYhxmhs/Q=="}
2013-09-29 20:26:02 +10:00
_, _, err := c.Exchange(m, "127.0.0.1:53")
if err != nil {
2013-09-29 20:30:03 +10:00
t.Log(err.Error())
t.Fail()
}
}