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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 20:56:22 +11:00
|
|
|
// ExampleUpdateLeaseTSIG 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
|
|
|
|
2015-02-19 20:58:33 +11:00
|
|
|
leaseRr := new(OPT)
|
|
|
|
leaseRr.Hdr.Name = "."
|
|
|
|
leaseRr.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
|
2015-02-19 20:58:33 +11:00
|
|
|
leaseRr.Option = append(leaseRr.Option, e)
|
|
|
|
m.Extra = append(m.Extra, leaseRr)
|
2012-10-16 04:37:19 +11:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|