2011-07-05 17:49:13 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HelloServer(w ResponseWriter, req *Msg) {
|
|
|
|
m := new(Msg)
|
|
|
|
m.SetReply(req)
|
|
|
|
|
|
|
|
m.Extra = make([]RR, 1)
|
2012-02-12 20:50:02 +00:00
|
|
|
m.Extra[0] = &RR_TXT{Hdr: RR_Header{Name: m.Question[0].Name, Rrtype: TypeTXT, Class: ClassINET, Ttl: 0}, Txt: []string{"Hello world"}}
|
2012-11-19 15:15:03 +00:00
|
|
|
w.WriteMsg(m)
|
2011-07-05 17:49:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 22:46:35 +00:00
|
|
|
func AnotherHelloServer(w ResponseWriter, req *Msg) {
|
|
|
|
m := new(Msg)
|
|
|
|
m.SetReply(req)
|
|
|
|
|
|
|
|
m.Extra = make([]RR, 1)
|
|
|
|
m.Extra[0] = &RR_TXT{Hdr: RR_Header{Name: m.Question[0].Name, Rrtype: TypeTXT, Class: ClassINET, Ttl: 0}, Txt: []string{"Hello example"}}
|
2012-11-19 15:15:03 +00:00
|
|
|
w.WriteMsg(m)
|
2012-08-24 22:46:35 +00:00
|
|
|
}
|
|
|
|
|
2011-07-05 17:49:13 +00:00
|
|
|
func TestServing(t *testing.T) {
|
|
|
|
HandleFunc("miek.nl.", HelloServer)
|
2012-08-24 22:46:35 +00:00
|
|
|
HandleFunc("example.com.", AnotherHelloServer)
|
2011-07-05 17:49:13 +00:00
|
|
|
go func() {
|
2012-01-27 07:47:21 +00:00
|
|
|
err := ListenAndServe(":8053", "udp", nil)
|
2011-07-05 17:49:13 +00:00
|
|
|
if err != nil {
|
2011-12-10 19:54:26 +00:00
|
|
|
t.Log("ListenAndServe: ", err.Error())
|
2011-07-05 17:49:13 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}()
|
2012-08-24 22:46:35 +00:00
|
|
|
time.Sleep(4e8)
|
|
|
|
c := new(Client)
|
|
|
|
m := new(Msg)
|
|
|
|
|
|
|
|
m.SetQuestion("miek.nl.", TypeTXT)
|
2012-11-19 15:15:03 +00:00
|
|
|
r, _ , _ := c.Exchange(m, "127.0.0.1:8053")
|
2012-08-24 22:46:35 +00:00
|
|
|
txt := r.Extra[0].(*RR_TXT).Txt[0]
|
|
|
|
if txt != "Hello world" {
|
|
|
|
t.Log("Unexpected result for miek.nl", txt, "!= Hello world")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
m.SetQuestion("example.com.", TypeTXT)
|
2012-11-19 15:15:03 +00:00
|
|
|
r, _, _ = c.Exchange(m, "127.0.0.1:8053")
|
2012-08-24 22:46:35 +00:00
|
|
|
txt = r.Extra[0].(*RR_TXT).Txt[0]
|
|
|
|
if txt != "Hello example" {
|
|
|
|
t.Log("Unexpected result for example.com", txt, "!= Hello example")
|
|
|
|
t.Fail()
|
|
|
|
}
|
2011-07-05 17:49:13 +00:00
|
|
|
}
|
2011-07-06 17:21:19 +00:00
|
|
|
|
|
|
|
func BenchmarkServing(b *testing.B) {
|
2011-07-23 21:43:43 +00:00
|
|
|
b.StopTimer()
|
|
|
|
// Again start a server
|
2011-07-06 17:21:19 +00:00
|
|
|
HandleFunc("miek.nl.", HelloServer)
|
|
|
|
go func() {
|
2012-01-27 07:47:21 +00:00
|
|
|
ListenAndServe("127.0.0.1:8053", "udp", nil)
|
2011-07-06 17:21:19 +00:00
|
|
|
}()
|
|
|
|
|
2012-05-26 08:28:32 +00:00
|
|
|
c := new(Client)
|
2011-07-23 21:43:43 +00:00
|
|
|
m := new(Msg)
|
|
|
|
m.SetQuestion("miek.nl", TypeSOA)
|
2011-07-06 17:21:19 +00:00
|
|
|
|
2011-07-23 21:43:43 +00:00
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c.Exchange(m, "127.0.0.1:8053")
|
|
|
|
}
|
2011-07-06 17:21:19 +00:00
|
|
|
}
|
2012-09-17 06:56:27 +00:00
|
|
|
|
|
|
|
func TestDotAsCatchAllWildcard(t *testing.T) {
|
|
|
|
mux := NewServeMux()
|
|
|
|
mux.Handle(".", HandlerFunc(HelloServer))
|
|
|
|
mux.Handle("example.com.", HandlerFunc(AnotherHelloServer))
|
|
|
|
|
2012-09-18 06:19:06 +00:00
|
|
|
handler := mux.match("www.miek.nl.", TypeTXT)
|
2012-09-17 06:56:27 +00:00
|
|
|
if handler == nil {
|
|
|
|
t.Error("wildcard match failed")
|
|
|
|
}
|
|
|
|
|
2012-09-18 06:19:06 +00:00
|
|
|
handler = mux.match("www.example.com.", TypeTXT)
|
|
|
|
if handler == nil {
|
|
|
|
t.Error("example.com match failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = mux.match("a.www.example.com.", TypeTXT)
|
|
|
|
if handler == nil {
|
|
|
|
t.Error("a.www.example.com match failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = mux.match("boe.", TypeTXT)
|
|
|
|
if handler == nil {
|
|
|
|
t.Error("boe. match failed")
|
2012-09-17 06:56:27 +00:00
|
|
|
}
|
|
|
|
}
|