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

Commit

Permalink
Start of test for recursive compressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Wilkie committed Aug 16, 2015
1 parent fc0b789 commit 544142c
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 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,68 @@ 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
handleRecursive := func(w dns.ResponseWriter, req *dns.Msg) {
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),
Ndotes: 1,
Timeouts: 5,
Attemtps: 2,
}

udpPort := dnsserver.servers[0].PacketConn.LocalAddr().(*net.UDPAddr).Port
//tcpPort := dnsserver.servers[1].Listener.Addr().(*net.TCPAddr).Port
go dnsserver.ActivateAndServe()
defer dnsserver.Stop()

results := net.LookupAddr(hostname)
}

0 comments on commit 544142c

Please sign in to comment.