coredns/plugin/erratic/erratic_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

116 lines
3 KiB
Go

package erratic
import (
"context"
"testing"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
)
func TestErraticDrop(t *testing.T) {
e := &Erratic{drop: 2} // 50% drops
tests := []struct {
rrtype uint16
expectedCode int
expectedErr error
drop bool
}{
{rrtype: dns.TypeA, expectedCode: dns.RcodeSuccess, expectedErr: nil, drop: true},
{rrtype: dns.TypeA, expectedCode: dns.RcodeSuccess, expectedErr: nil, drop: false},
{rrtype: dns.TypeAAAA, expectedCode: dns.RcodeSuccess, expectedErr: nil, drop: true},
{rrtype: dns.TypeHINFO, expectedCode: dns.RcodeServerFailure, expectedErr: nil, drop: false},
}
ctx := context.TODO()
for i, tc := range tests {
req := new(dns.Msg)
req.SetQuestion("example.org.", tc.rrtype)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := e.ServeDNS(ctx, rec, req)
if err != tc.expectedErr {
t.Errorf("Test %d: Expected error %q, but got %q", i, tc.expectedErr, err)
}
if code != int(tc.expectedCode) {
t.Errorf("Test %d: Expected status code %d, but got %d", i, tc.expectedCode, code)
}
if tc.drop && rec.Msg != nil {
t.Errorf("Test %d: Expected dropped message, but got %q", i, rec.Msg.Question[0].Name)
}
}
}
func TestErraticTruncate(t *testing.T) {
e := &Erratic{truncate: 2} // 50% drops
tests := []struct {
expectedCode int
expectedErr error
truncate bool
}{
{expectedCode: dns.RcodeSuccess, expectedErr: nil, truncate: true},
{expectedCode: dns.RcodeSuccess, expectedErr: nil, truncate: false},
}
ctx := context.TODO()
for i, tc := range tests {
req := new(dns.Msg)
req.SetQuestion("example.org.", dns.TypeA)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := e.ServeDNS(ctx, rec, req)
if err != tc.expectedErr {
t.Errorf("Test %d: Expected error %q, but got %q", i, tc.expectedErr, err)
}
if code != int(tc.expectedCode) {
t.Errorf("Test %d: Expected status code %d, but got %d", i, tc.expectedCode, code)
}
if tc.truncate && !rec.Msg.Truncated {
t.Errorf("Test %d: Expected truncated message, but got %q", i, rec.Msg.Question[0].Name)
}
}
}
func TestAxfr(t *testing.T) {
e := &Erratic{truncate: 0} // nothing, just check if we can get an axfr
ctx := context.TODO()
req := new(dns.Msg)
req.SetQuestion("example.org.", dns.TypeAXFR)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := e.ServeDNS(ctx, rec, req)
if err != nil {
t.Errorf("Failed to set up AXFR: %s", err)
}
if x := rec.Msg.Answer[0].Header().Rrtype; x != dns.TypeSOA {
t.Errorf("Expected for record to be %d, got %d", dns.TypeSOA, x)
}
}
func TestErratic(t *testing.T) {
e := &Erratic{drop: 0, delay: 0}
ctx := context.TODO()
req := new(dns.Msg)
req.SetQuestion("example.org.", dns.TypeA)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
e.ServeDNS(ctx, rec, req)
if rec.Msg.Answer[0].Header().Rrtype != dns.TypeA {
t.Errorf("Expected A response, got %d type", rec.Msg.Answer[0].Header().Rrtype)
}
}