2011-07-04 18:18:51 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2015-03-18 19:12:53 +00:00
|
|
|
"strconv"
|
2011-07-04 18:18:51 +00:00
|
|
|
"testing"
|
2011-07-23 21:43:43 +00:00
|
|
|
"time"
|
2011-07-04 18:18:51 +00:00
|
|
|
)
|
|
|
|
|
2014-08-29 12:59:19 +00:00
|
|
|
func TestClientSync(t *testing.T) {
|
2014-02-15 08:03:40 +00:00
|
|
|
HandleFunc("miek.nl.", HelloServer)
|
2014-08-30 13:45:30 +00:00
|
|
|
defer HandleRemove("miek.nl.")
|
2014-02-15 08:03:40 +00:00
|
|
|
|
2014-08-29 12:59:19 +00:00
|
|
|
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
|
|
|
|
if err != nil {
|
2015-02-26 06:14:21 +00:00
|
|
|
t.Fatalf("Unable to run test server: %v", err)
|
2014-08-29 12:59:19 +00:00
|
|
|
}
|
|
|
|
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)
|
2015-02-26 06:14:21 +00:00
|
|
|
r, _, err := c.Exchange(m, addrstr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to exchange: %v", err)
|
2014-08-29 12:59:19 +00:00
|
|
|
}
|
2011-07-04 18:18:51 +00:00
|
|
|
if r != nil && r.Rcode != RcodeSuccess {
|
2015-02-26 06:14:21 +00:00
|
|
|
t.Errorf("failed to get an valid answer\n%v", r)
|
2011-07-04 18:18:51 +00:00
|
|
|
}
|
2014-08-30 14:06:20 +00:00
|
|
|
// And now with plain Exchange().
|
2015-02-26 06:14:21 +00:00
|
|
|
r, err = Exchange(m, addrstr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to exchange: %v", err)
|
2014-08-30 14:06:20 +00:00
|
|
|
}
|
|
|
|
if r != nil && r.Rcode != RcodeSuccess {
|
2015-02-26 06:14:21 +00:00
|
|
|
t.Errorf("failed to get an valid answer\n%v", r)
|
2014-08-30 14:06:20 +00:00
|
|
|
}
|
2011-07-04 18:18:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 05:56:42 +00:00
|
|
|
func TestClientSyncBadId(t *testing.T) {
|
|
|
|
HandleFunc("miek.nl.", HelloServerBadId)
|
|
|
|
defer HandleRemove("miek.nl.")
|
|
|
|
|
|
|
|
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to run test server: %v", err)
|
|
|
|
}
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
m := new(Msg)
|
|
|
|
m.SetQuestion("miek.nl.", TypeSOA)
|
|
|
|
|
|
|
|
c := new(Client)
|
|
|
|
if _, _, err := c.Exchange(m, addrstr); err != ErrId {
|
|
|
|
t.Errorf("did not find a bad Id")
|
|
|
|
}
|
|
|
|
// And now with plain Exchange().
|
|
|
|
if _, err := Exchange(m, addrstr); err != ErrId {
|
|
|
|
t.Errorf("did not find a bad Id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 06:51:51 +00:00
|
|
|
func TestClientEDNS0(t *testing.T) {
|
2014-08-29 12:59:19 +00:00
|
|
|
HandleFunc("miek.nl.", HelloServer)
|
2014-08-30 13:45:30 +00:00
|
|
|
defer HandleRemove("miek.nl.")
|
2014-08-29 12:59:19 +00:00
|
|
|
|
|
|
|
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
|
|
|
|
if err != nil {
|
2015-02-26 06:14:21 +00:00
|
|
|
t.Fatalf("Unable to run test server: %v", err)
|
2014-08-29 12:59:19 +00:00
|
|
|
}
|
|
|
|
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)
|
2015-02-26 06:14:21 +00:00
|
|
|
r, _, err := c.Exchange(m, addrstr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to exchange: %v", err)
|
2014-08-29 12:59:19 +00:00
|
|
|
}
|
2011-07-06 06:51:51 +00:00
|
|
|
|
|
|
|
if r != nil && r.Rcode != RcodeSuccess {
|
2015-02-26 06:14:21 +00:00
|
|
|
t.Errorf("failed to get an valid answer\n%v", r)
|
2011-07-04 18:18:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 19:12:53 +00:00
|
|
|
// Validates the transmission and parsing of local EDNS0 options.
|
|
|
|
func TestClientEDNS0Local(t *testing.T) {
|
|
|
|
|
|
|
|
optStr1 := "1979:0x0707"
|
|
|
|
optStr2 := strconv.Itoa(EDNS0LOCALSTART) + ":0x0601"
|
|
|
|
|
2015-03-17 17:41:55 +00:00
|
|
|
handler := func(w ResponseWriter, req *Msg) {
|
|
|
|
m := new(Msg)
|
|
|
|
m.SetReply(req)
|
|
|
|
|
|
|
|
m.Extra = make([]RR, 1, 2)
|
2015-03-18 19:12:53 +00:00
|
|
|
m.Extra[0] = &TXT{Hdr: RR_Header{Name: m.Question[0].Name, Rrtype: TypeTXT, Class: ClassINET, Ttl: 0}, Txt: []string{"Hello local edns"}}
|
2015-03-17 17:41:55 +00:00
|
|
|
|
2015-03-18 19:12:53 +00:00
|
|
|
// If the local options are what we expect, then reflect them back.
|
|
|
|
ec1 := req.Extra[0].(*OPT).Option[0].(*EDNS0_LOCAL).String()
|
|
|
|
ec2 := req.Extra[0].(*OPT).Option[1].(*EDNS0_LOCAL).String()
|
|
|
|
if ec1 == optStr1 && ec2 == optStr2 {
|
2015-03-17 17:41:55 +00:00
|
|
|
m.Extra = append(m.Extra, req.Extra[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteMsg(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleFunc("miek.nl.", handler)
|
|
|
|
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.", TypeTXT)
|
|
|
|
|
2015-03-18 19:12:53 +00:00
|
|
|
// Add two local edns options to the query.
|
|
|
|
ec1 := &EDNS0_LOCAL{Code: 1979, Data: []byte{7, 7}}
|
|
|
|
ec2 := &EDNS0_LOCAL{Code: EDNS0LOCALSTART, Data: []byte{6, 1}}
|
2015-03-17 17:41:55 +00:00
|
|
|
o := &OPT{Hdr: RR_Header{Name: ".", Rrtype: TypeOPT}, Option: []EDNS0{ec1, ec2}}
|
|
|
|
m.Extra = append(m.Extra, o)
|
|
|
|
|
|
|
|
c := new(Client)
|
|
|
|
r, _, e := c.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 a valid answer")
|
|
|
|
t.Fail()
|
|
|
|
t.Logf("%v\n", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
txt := r.Extra[0].(*TXT).Txt[0]
|
2015-03-18 19:12:53 +00:00
|
|
|
if txt != "Hello local edns" {
|
|
|
|
t.Log("Unexpected result for miek.nl", txt, "!= Hello local edns")
|
2015-03-17 17:41:55 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2015-03-18 19:12:53 +00:00
|
|
|
// Validate the local options in the reply.
|
|
|
|
got := r.Extra[1].(*OPT).Option[0].(*EDNS0_LOCAL).String()
|
|
|
|
if got != optStr1 {
|
|
|
|
t.Log("failed to get local edns0 answer; got %s, expected %s", got, optStr1)
|
2015-03-17 17:41:55 +00:00
|
|
|
t.Fail()
|
|
|
|
t.Logf("%v\n", r)
|
|
|
|
}
|
|
|
|
|
2015-03-18 19:12:53 +00:00
|
|
|
got = r.Extra[1].(*OPT).Option[1].(*EDNS0_LOCAL).String()
|
|
|
|
if got != optStr2 {
|
|
|
|
t.Log("failed to get local edns0 answer; got %s, expected %s", got, optStr2)
|
2015-03-17 17:41:55 +00:00
|
|
|
t.Fail()
|
|
|
|
t.Logf("%v\n", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 09:49:07 +00:00
|
|
|
func TestSingleSingleInflight(t *testing.T) {
|
2014-08-29 12:59:19 +00:00
|
|
|
HandleFunc("miek.nl.", HelloServer)
|
2014-08-30 13:45:30 +00:00
|
|
|
defer HandleRemove("miek.nl.")
|
2014-08-29 12:59:19 +00:00
|
|
|
|
|
|
|
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
|
|
|
|
if err != nil {
|
2015-02-26 06:14:21 +00:00
|
|
|
t.Fatalf("Unable to run test server: %v", err)
|
2014-08-29 12:59:19 +00:00
|
|
|
}
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2013-08-24 02:28:58 +00:00
|
|
|
m := new(Msg)
|
|
|
|
m.SetQuestion("miek.nl.", TypeDNSKEY)
|
|
|
|
|
|
|
|
c := new(Client)
|
2013-09-06 09:49:07 +00:00
|
|
|
c.SingleInflight = true
|
2013-08-24 02:28:58 +00:00
|
|
|
nr := 10
|
|
|
|
ch := make(chan time.Duration)
|
|
|
|
for i := 0; i < nr; i++ {
|
|
|
|
go func() {
|
2014-08-29 12:59:19 +00:00
|
|
|
_, rtt, _ := c.Exchange(m, addrstr)
|
2013-08-24 02:28:58 +00: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 {
|
2015-02-26 06:14:21 +00:00
|
|
|
t.Errorf("all rtts should be equal. got %d want %d", rtt, first)
|
2013-08-24 02:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
if i == 10 {
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 09:56:22 +00:00
|
|
|
// ExampleUpdateLeaseTSIG shows how to update a lease signed with TSIG.
|
2013-10-12 14:35:26 +00:00
|
|
|
func ExampleUpdateLeaseTSIG(t *testing.T) {
|
2012-10-15 17:37:19 +00: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 08:41:20 +00:00
|
|
|
m.Insert(rrs)
|
2012-10-15 17:37:19 +00:00
|
|
|
|
2015-02-19 09:58:33 +00:00
|
|
|
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
|
2012-10-15 17:37:19 +00:00
|
|
|
e.Lease = 120
|
2015-02-19 09:58:33 +00:00
|
|
|
leaseRr.Option = append(leaseRr.Option, e)
|
|
|
|
m.Extra = append(m.Extra, leaseRr)
|
2012-10-15 17:37:19 +00:00
|
|
|
|
|
|
|
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 {
|
2015-02-26 06:14:21 +00:00
|
|
|
t.Error(err)
|
2012-10-15 17:37:19 +00:00
|
|
|
}
|
|
|
|
}
|