coredns/test/compression_scrub_test.go
Miek Gieben 898b1ef316 server: actually scrub response (#2225)
* server: actually scrub response

Did all the worked, hooked it up wrongly :(

This also needs test, but those are hard(er) because we only receive
packets after they have been decoded; i.e. we never see the wirefmt.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Add tests

Add a test for checking is compression pointers are set in the packet.
This also adds an undocumented 'large' feature to the erratic plugin to
send large responses that should be compressed.

Commenting the Scrub out in server results in:

=== RUN   TestCompressScrub
--- FAIL: TestCompressScrub (0.00s)
    compression_scrub_test.go:41: Expected returned packet to be < 512, got 839
FAIL
exit status 1
FAIL    github.com/coredns/coredns/test 0.036s

Actually checking the size might be easier, but lets be thorough here
and check the pointers them selves.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Fix tests

Signed-off-by: Miek Gieben <miek@miek.nl>

* plugin erratic: fix e.large

always put an rr in the reply, fix e.large in erractic and add test to
check for it.

Signed-off-by: Miek Gieben <miek@miek.nl>
2018-10-23 09:55:40 -07:00

60 lines
1.5 KiB
Go

package test
import (
"net"
"testing"
"github.com/miekg/dns"
)
func TestCompressScrub(t *testing.T) {
corefile := `example.org:0 {
erratic {
drop 0
delay 0
large
}
}`
i, udp, _, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer i.Stop()
c, err := net.Dial("udp", udp)
if err != nil {
t.Fatalf("Could not dial %s", err)
}
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeA)
q, _ := m.Pack()
c.Write(q)
buf := make([]byte, 1024)
n, err := c.Read(buf)
if err != nil || n == 0 {
t.Errorf("Expected reply, got: %s", err)
return
}
if n >= 512 {
t.Fatalf("Expected returned packet to be < 512, got %d", n)
}
buf = buf[:n]
// If there is compression in the returned packet we should look for compression pointers, if found
// the pointers should return to the domain name in the query (the first domain name that's avaiable for
// compression. This means we're looking for a combo where the pointers is detected and the offset is 12
// the position of the first name after the header. The erratic plugin adds 30 RRs that should all be compressed.
found := 0
for i := 0; i < len(buf)-1; i++ {
if buf[i]&0xC0 == 0xC0 {
off := (int(buf[i])^0xC0)<<8 | int(buf[i+1])
if off == 12 {
found++
}
}
}
if found != 30 {
t.Errorf("Failed to find all compression pointers in the packet, wanted 30, got %d", found)
}
}