Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Test for recursive DNS queries & compression.
Browse files Browse the repository at this point in the history
Note this doesn't test the golang resolver, where the original error was found, as I can't find a way to point the
golang resolver at a custom dns server during a test.  It does check that a compressed response from an upstream server
is correctly handled.
  • Loading branch information
Tom Wilkie committed Aug 17, 2015
1 parent fc0b789 commit a271cf4
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions nameserver/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"fmt"
"math/rand"
"net"
"strconv"
"testing"
"time"

"github.com/miekg/dns"
"github.com/stretchr/testify/require"

"github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/net/address"
"github.com/weaveworks/weave/router"
)
Expand Down Expand Up @@ -93,3 +95,84 @@ func TestTruncateResponse(t *testing.T) {
require.True(t, response.Len() <= maxSize)
}
}

func TestRecursiveCompress(t *testing.T) {
const (
hostname = "foo.example."
maxsize = 512
)
common.SetLogLevel("debug")

// Construct a response that is >512 when uncompressed, <512 when compressed
response := dns.Msg{}
response.Authoritative = true
response.Answer = []dns.RR{}
header := dns.RR_Header{
Name: hostname,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 10,
}
for {
if response.Len() > maxsize {
response.Compress = true
require.True(t, response.Len() <= maxsize)
break
}
ip := address.Address(rand.Uint32()).IP4()
response.Answer = append(response.Answer, &dns.A{Hdr: header, A: ip})
}

// A dns server that returns the above response
var gotRequest = false
handleRecursive := func(w dns.ResponseWriter, req *dns.Msg) {
gotRequest = true
require.Equal(t, req.Question[0].Name, hostname)
response.SetReply(req)
err := w.WriteMsg(&response)
require.Nil(t, err)
}
mux := dns.NewServeMux()
mux.HandleFunc(topDomain, handleRecursive)
udpListener, err := net.ListenPacket("udp", "0.0.0.0:0")
require.Nil(t, err)
udpServer := &dns.Server{PacketConn: udpListener, Handler: mux}
udpServerPort := udpListener.LocalAddr().(*net.UDPAddr).Port
go udpServer.ActivateAndServe()
defer udpServer.Shutdown()

// The weavedns server
peername, err := router.PeerNameFromString("00:00:00:02:00:00")
require.Nil(t, err)
nameserver := New(peername, nil, nil, "")
dnsserver, err := NewDNSServer(nameserver, "weave.local.", "0.0.0.0:0", 30, 5*time.Second)
require.Nil(t, err)
dnsserver.upstream = &dns.ClientConfig{
Servers: []string{"127.0.0.1"},
Port: strconv.Itoa(udpServerPort),
Ndots: 1,
Timeout: 5,
Attempts: 2,
}
udpPort := dnsserver.servers[0].PacketConn.LocalAddr().(*net.UDPAddr).Port
go dnsserver.ActivateAndServe()
defer dnsserver.Stop()

// Now do lookup, check its what we expected.
// NB this doesn't really test golang's resolver behaves correctly, as I can't see
// a way to point golangs resolver at a specific hosts.
req := new(dns.Msg)
req.Id = dns.Id()
req.RecursionDesired = true
req.Question = make([]dns.Question, 1)
req.Question[0] = dns.Question{
Name: hostname,
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}
c := new(dns.Client)
res, _, err := c.Exchange(req, fmt.Sprintf("127.0.0.1:%d", udpPort))
require.Nil(t, err)
require.True(t, gotRequest)
require.True(t, res.Len() > maxsize)
}

0 comments on commit a271cf4

Please sign in to comment.