dns/client_test.go

141 lines
2.9 KiB
Go
Raw Normal View History

2011-07-04 18:18:51 +00:00
package dns
import (
"testing"
2011-07-23 21:43:43 +00:00
"time"
2011-07-04 18:18:51 +00:00
)
func TestClientSync(t *testing.T) {
HandleFunc("miek.nl.", HelloServer)
defer HandleRemove("miek.nl.")
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-04 18:18:51 +00:00
m := new(Msg)
2012-01-08 14:34:42 +00:00
m.SetQuestion("miek.nl.", TypeSOA)
2011-07-04 18:18:51 +00:00
2012-05-26 08:28:32 +00:00
c := new(Client)
r, _, e := c.Exchange(m, addrstr)
if e != nil {
t.Errorf("failed to exchange: %s", e.Error())
}
2011-07-04 18:18:51 +00:00
if r != nil && r.Rcode != RcodeSuccess {
t.Error("failed to get an valid answer")
t.Errorf("%v\n", r)
2011-07-04 18:18:51 +00:00
}
// And now with plain Exchange().
r, e = Exchange(m, addrstr)
if e != nil {
t.Errorf("failed to exchange: %s", e.Error())
}
if r != nil && r.Rcode != RcodeSuccess {
t.Error("failed to get an valid answer")
t.Errorf("%v\n", r)
}
2011-07-04 18:18:51 +00:00
}
2011-07-06 06:51:51 +00:00
func TestClientEDNS0(t *testing.T) {
HandleFunc("miek.nl.", HelloServer)
defer HandleRemove("miek.nl.")
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 06:51:51 +00:00
m := new(Msg)
2012-01-08 14:34:42 +00:00
m.SetQuestion("miek.nl.", TypeDNSKEY)
2011-07-04 18:18:51 +00:00
2011-11-28 05:03:21 +00:00
m.SetEdns0(2048, true)
2011-07-04 18:18:51 +00:00
2012-05-26 08:28:40 +00:00
c := new(Client)
r, _, e := c.Exchange(m, addrstr)
if e != nil {
t.Errorf("failed to exchange: %s", e.Error())
}
2011-07-06 06:51:51 +00:00
if r != nil && r.Rcode != RcodeSuccess {
t.Error("failed to get an valid answer")
t.Errorf("%v\n", r)
2011-07-04 18:18:51 +00:00
}
}
2013-09-06 09:49:07 +00:00
func TestSingleSingleInflight(t *testing.T) {
HandleFunc("miek.nl.", HelloServer)
defer HandleRemove("miek.nl.")
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
if err != nil {
t.Fatalf("Unable to run test server: %s", err)
}
defer s.Shutdown()
m := new(Msg)
m.SetQuestion("miek.nl.", TypeDNSKEY)
c := new(Client)
2013-09-06 09:49:07 +00:00
c.SingleInflight = true
nr := 10
ch := make(chan time.Duration)
for i := 0; i < nr; i++ {
go func() {
_, rtt, _ := c.Exchange(m, addrstr)
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.Error("all rtts should be equal")
}
}
i++
if i == 10 {
break Loop
}
}
}
}
// ExampleUpdateLeaseTSIG shows how to update a lease signed with TSIG.
2013-10-12 14:35:26 +00:00
func ExampleUpdateLeaseTSIG(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)
leaseRr := new(OPT)
leaseRr.Hdr.Name = "."
leaseRr.Hdr.Rrtype = TypeOPT
2013-05-09 06:27:49 +00:00
e := new(EDNS0_UL)
e.Code = EDNS0UL
e.Lease = 120
leaseRr.Option = append(leaseRr.Option, e)
m.Extra = append(m.Extra, leaseRr)
c := new(Client)
m.SetTsig("polvi.", HmacMD5, 300, time.Now().Unix())
c.TsigSecret = map[string]string{"polvi.": "pRZgBrBvI4NAHZYhxmhs/Q=="}
2013-09-29 10:26:02 +00:00
_, _, err := c.Exchange(m, "127.0.0.1:53")
if err != nil {
t.Error(err.Error())
}
}