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
|
|
|
)
|
|
|
|
|
2014-08-29 22:59:19 +10:00
|
|
|
func TestClientSync(t *testing.T) {
|
2014-02-15 19:03:40 +11:00
|
|
|
HandleFunc("miek.nl.", HelloServer)
|
2014-08-30 23:45:30 +10:00
|
|
|
defer HandleRemove("miek.nl.")
|
2014-02-15 19:03:40 +11:00
|
|
|
|
2014-08-29 22:59:19 +10:00
|
|
|
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to run test server: %s", err)
|
|
|
|
}
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2011-07-05 04:18:51 +10:00
|
|
|
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)
|
2014-08-29 22:59:19 +10:00
|
|
|
r, _, e := c.Exchange(m, addrstr)
|
|
|
|
if e != nil {
|
|
|
|
t.Logf("failed to exchange: %s", e.Error())
|
|
|
|
t.Fail()
|
|
|
|
}
|
2011-07-05 04:18:51 +10:00
|
|
|
if r != nil && r.Rcode != RcodeSuccess {
|
2014-08-19 18:39:04 +10:00
|
|
|
t.Log("failed to get an valid answer")
|
2011-07-05 04:18:51 +10:00
|
|
|
t.Fail()
|
|
|
|
t.Logf("%v\n", r)
|
|
|
|
}
|
2014-08-31 00:06:20 +10:00
|
|
|
// And now with plain Exchange().
|
|
|
|
r, e = Exchange(m, addrstr)
|
|
|
|
if e != nil {
|
|
|
|
t.Logf("failed to exchange: %s", e.Error())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-06 16:51:51 +10:00
|
|
|
func TestClientEDNS0(t *testing.T) {
|
2014-08-29 22:59:19 +10:00
|
|
|
HandleFunc("miek.nl.", HelloServer)
|
2014-08-30 23:45:30 +10:00
|
|
|
defer HandleRemove("miek.nl.")
|
2014-08-29 22:59:19 +10:00
|
|
|
|
|
|
|
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to run test server: %s", err)
|
|
|
|
}
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2011-07-06 16:51:51 +10:00
|
|
|
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-07-05 04:18:51 +10:00
|
|
|
|
2012-05-26 18:28:40 +10:00
|
|
|
c := new(Client)
|
2014-08-29 22:59:19 +10:00
|
|
|
r, _, e := c.Exchange(m, addrstr)
|
|
|
|
if e != nil {
|
|
|
|
t.Logf("failed to exchange: %s", e.Error())
|
|
|
|
t.Fail()
|
|
|
|
}
|
2011-07-06 16:51:51 +10:00
|
|
|
|
|
|
|
if r != nil && r.Rcode != RcodeSuccess {
|
2014-08-19 18:39:04 +10:00
|
|
|
t.Log("failed to get an valid answer")
|
2011-07-06 16:51:51 +10:00
|
|
|
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) {
|
2014-08-29 22:59:19 +10:00
|
|
|
HandleFunc("miek.nl.", HelloServer)
|
2014-08-30 23:45:30 +10:00
|
|
|
defer HandleRemove("miek.nl.")
|
2014-08-29 22:59:19 +10:00
|
|
|
|
|
|
|
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to run test server: %s", err)
|
|
|
|
}
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2013-08-24 12:28:58 +10:00
|
|
|
m := new(Msg)
|
|
|
|
m.SetQuestion("miek.nl.", TypeDNSKEY)
|
|
|
|
|
|
|
|
c := new(Client)
|
2013-09-06 19:49:07 +10:00
|
|
|
c.SingleInflight = true
|
2013-08-24 12:28:58 +10:00
|
|
|
nr := 10
|
|
|
|
ch := make(chan time.Duration)
|
|
|
|
for i := 0; i < nr; i++ {
|
|
|
|
go func() {
|
2014-08-29 22:59:19 +10:00
|
|
|
_, rtt, _ := c.Exchange(m, addrstr)
|
2013-08-24 12:28:58 +10:00
|
|
|
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 {
|
2014-08-19 18:39:04 +10:00
|
|
|
t.Log("all rtts should be equal")
|
2013-08-24 12:28:58 +10:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
if i == 10 {
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 04:29:57 +10:00
|
|
|
/*
|
2011-07-06 04:52:35 +10:00
|
|
|
func TestClientTsigAXFR(t *testing.T) {
|
2011-07-05 04:18:51 +10:00
|
|
|
m := new(Msg)
|
2013-10-13 01:35:26 +11:00
|
|
|
m.SetAxfr("example.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
|
|
|
|
2013-10-13 00:00:10 +11:00
|
|
|
tr := new(Transfer)
|
|
|
|
tr.TsigSecret = map[string]string{"axfr.": "so6ZGir4GPAqINNh9U5c3A=="}
|
2011-07-24 07:43:43 +10:00
|
|
|
|
2013-10-13 01:35:26 +11:00
|
|
|
if a, err := tr.In(m, "176.58.119.54:53"); err != nil {
|
2014-08-19 18:39:04 +10: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 {
|
2014-08-19 18:39:04 +10:00
|
|
|
t.Logf("error %s\n", ex.Error.Error())
|
2012-05-20 20:44:49 +10:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 01:35:26 +11:00
|
|
|
func TestClientAXFRMultipleEnvelopes(t *testing.T) {
|
2011-09-12 01:01:55 +10:00
|
|
|
m := new(Msg)
|
2013-10-13 01:35:26 +11:00
|
|
|
m.SetAxfr("nlnetlabs.nl.")
|
2011-09-12 01:01:55 +10:00
|
|
|
|
2013-10-13 01:35:26 +11:00
|
|
|
tr := new(Transfer)
|
|
|
|
if a, err := tr.In(m, "213.154.224.1: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
|
|
|
}
|
2013-10-03 04:29:57 +10:00
|
|
|
*/
|
2012-10-16 04:37:19 +11:00
|
|
|
|
2014-02-15 19:03:40 +11:00
|
|
|
// ExapleUpdateLeaseTSIG shows how to update a lease signed with TSIG.
|
2013-10-13 01:35:26 +11:00
|
|
|
func ExampleUpdateLeaseTSIG(t *testing.T) {
|
2012-10-16 04:37:19 +11:00
|
|
|
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
|
2012-10-16 19:41:20 +11:00
|
|
|
m.Insert(rrs)
|
2012-10-16 04:37:19 +11:00
|
|
|
|
2012-12-10 05:23:25 +11:00
|
|
|
lease_rr := new(OPT)
|
2012-10-16 04:37:19 +11:00
|
|
|
lease_rr.Hdr.Name = "."
|
|
|
|
lease_rr.Hdr.Rrtype = TypeOPT
|
2013-05-09 16:27:49 +10:00
|
|
|
e := new(EDNS0_UL)
|
|
|
|
e.Code = EDNS0UL
|
2012-10-16 04:37:19 +11:00
|
|
|
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())
|
2012-10-16 04:37:19 +11:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|