aboutsummaryrefslogtreecommitdiff
path: root/middleware/dnstap/handler_test.go
blob: dfdde582da4fb1db34dcfb8fb88304089f4f4264 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package dnstap

import (
	"errors"
	"fmt"
	"testing"

	"github.com/coredns/coredns/middleware/dnstap/test"
	mwtest "github.com/coredns/coredns/middleware/test"

	tap "github.com/dnstap/golang-dnstap"
	"github.com/golang/protobuf/proto"
	"github.com/miekg/dns"
	"golang.org/x/net/context"
)

func testCase(t *testing.T, tapq, tapr *tap.Message, q, r *dns.Msg) {
	w := writer{}
	w.queue = append(w.queue, tapq, tapr)
	h := Dnstap{
		Next: mwtest.HandlerFunc(func(_ context.Context,
			w dns.ResponseWriter, _ *dns.Msg) (int, error) {

			return 0, w.WriteMsg(r)
		}),
		Out:  &w,
		Pack: false,
	}
	_, err := h.ServeDNS(context.TODO(), &mwtest.ResponseWriter{}, q)
	if err != nil {
		t.Fatal(err)
	}
}

type writer struct {
	queue []*tap.Message
}

func (w *writer) Write(b []byte) (int, error) {
	e := tap.Dnstap{}
	if err := proto.Unmarshal(b, &e); err != nil {
		return 0, err
	}
	if len(w.queue) == 0 {
		return 0, errors.New("message not expected")
	}
	if !test.MsgEqual(w.queue[0], e.Message) {
		return 0, fmt.Errorf("want: %v, have: %v", w.queue[0], e.Message)
	}
	w.queue = w.queue[1:]
	return len(b), nil
}

func TestDnstap(t *testing.T) {
	q := mwtest.Case{Qname: "example.org", Qtype: dns.TypeA}.Msg()
	r := mwtest.Case{
		Qname: "example.org.", Qtype: dns.TypeA,
		Answer: []dns.RR{
			mwtest.A("example.org. 3600	IN	A 10.0.0.1"),
		},
	}.Msg()
	tapq := test.TestingData().ToClientQuery()
	tapr := test.TestingData().ToClientResponse()
	testCase(t, tapq, tapr, q, r)
}