dns/client_test.go

169 lines
3.3 KiB
Go
Raw Normal View History

2013-05-12 14:09:52 +00: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-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 newTestServer(t *testing.T) {
// Defined in server_test.go
HandleFunc("miek.nl.", HelloServer)
HandleFunc("example.com.", AnotherHelloServer)
go func() {
err := ListenAndServe(":8063", "udp", nil)
if err != nil {
t.Log("ListenAndServe: ", err.Error())
t.Fatal()
}
}()
time.Sleep(4e8)
}
2011-07-04 18:18:51 +00:00
func TestClientSync(t *testing.T) {
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, _, _ := c.Exchange(m, "127.0.0.1:6053")
2011-07-04 18:18:51 +00:00
if r != nil && r.Rcode != RcodeSuccess {
t.Log("failed to get an valid answer")
2011-07-04 18:18:51 +00:00
t.Fail()
t.Logf("%v\n", r)
}
}
2011-07-06 06:51:51 +00:00
func TestClientEDNS0(t *testing.T) {
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, _, _ := c.Exchange(m, "127.0.0.1:6053")
2011-07-06 06:51:51 +00:00
if r != nil && r.Rcode != RcodeSuccess {
t.Log("failed to get an valid answer")
2011-07-06 06:51:51 +00:00
t.Fail()
t.Logf("%v\n", r)
2011-07-04 18:18:51 +00:00
}
}
2013-09-06 09:49:07 +00:00
func TestSingleSingleInflight(t *testing.T) {
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, "127.0.0.1:6053")
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 rtts should be equal")
t.Fail()
}
}
i++
if i == 10 {
break Loop
}
}
}
}
/*
func TestClientTsigAXFR(t *testing.T) {
2011-07-04 18:18:51 +00:00
m := new(Msg)
2013-10-12 14:35:26 +00:00
m.SetAxfr("example.nl.")
2012-05-20 10:44:49 +00:00
m.SetTsig("axfr.", HmacMD5, 300, time.Now().Unix())
2011-07-23 21:43:43 +00:00
2013-10-12 13:00:10 +00:00
tr := new(Transfer)
tr.TsigSecret = map[string]string{"axfr.": "so6ZGir4GPAqINNh9U5c3A=="}
2011-07-23 21:43:43 +00:00
2013-10-12 14:35:26 +00:00
if a, err := tr.In(m, "176.58.119.54:53"); err != nil {
t.Log("failed to setup axfr: " + err.Error())
2013-08-26 04:38:10 +00:00
t.Fatal()
2012-05-20 10:44:49 +00:00
} else {
for ex := range a {
if ex.Error != nil {
t.Logf("error %s\n", ex.Error.Error())
2012-05-20 10:44:49 +00:00
t.Fail()
break
}
2012-08-31 12:59:56 +00:00
for _, rr := range ex.RR {
t.Logf("%s\n", rr.String())
2012-05-20 10:44:49 +00:00
}
2012-01-12 22:17:34 +00:00
}
2011-09-11 15:01:55 +00:00
}
}
2013-10-12 14:35:26 +00:00
func TestClientAXFRMultipleEnvelopes(t *testing.T) {
2011-09-11 15:01:55 +00:00
m := new(Msg)
2013-10-12 14:35:26 +00:00
m.SetAxfr("nlnetlabs.nl.")
2011-09-11 15:01:55 +00:00
2013-10-12 14:35:26 +00:00
tr := new(Transfer)
if a, err := tr.In(m, "213.154.224.1:53"); err != nil {
2011-12-10 19:54:26 +00:00
t.Log("Failed to setup axfr" + err.Error())
2011-09-11 15:01:55 +00:00
t.Fail()
2012-01-12 22:17:34 +00:00
return
2012-05-20 10:44:49 +00:00
} else {
for ex := range a {
if ex.Error != nil {
t.Logf("Error %s\n", ex.Error.Error())
t.Fail()
break
}
2012-01-12 22:17:34 +00:00
}
2011-09-10 22:31:03 +00:00
}
2011-07-04 18:18:51 +00:00
}
*/
// ExapleUpdateLeaseTSIG 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)
lease_rr := new(OPT)
lease_rr.Hdr.Name = "."
lease_rr.Hdr.Rrtype = TypeOPT
2013-05-09 06:27:49 +00: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 10:26:02 +00:00
_, _, err := c.Exchange(m, "127.0.0.1:53")
if err != nil {
2013-09-29 10:30:03 +00:00
t.Log(err.Error())
t.Fail()
}
}