| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | package dnstap
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	"github.com/coredns/coredns/plugin/dnstap/msg"
 | 
					
						
							|  |  |  | 	test "github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2021-01-24 18:28:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	tap "github.com/dnstap/golang-dnstap"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func testCase(t *testing.T, tapq, tapr *tap.Message, q, r *dns.Msg) {
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | 	w := writer{t: t}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	w.queue = append(w.queue, tapq, tapr)
 | 
					
						
							|  |  |  | 	h := Dnstap{
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		Next: test.HandlerFunc(func(_ context.Context,
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 			w dns.ResponseWriter, _ *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return 0, w.WriteMsg(r)
 | 
					
						
							|  |  |  | 		}),
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		io: &w,
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	_, err := h.ServeDNS(context.TODO(), &test.ResponseWriter{}, q)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type writer struct {
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | 	t     *testing.T
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	queue []*tap.Message
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | func (w *writer) Dnstap(e tap.Dnstap) {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	if len(w.queue) == 0 {
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		w.t.Error("Message not expected")
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ex := w.queue[0]
 | 
					
						
							|  |  |  | 	got := e.Message
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if string(ex.QueryAddress) != string(got.QueryAddress) {
 | 
					
						
							| 
									
										
										
										
											2021-01-20 15:59:03 +08:00
										 |  |  | 		w.t.Errorf("Expected source address %s, got %s", ex.QueryAddress, got.QueryAddress)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	if string(ex.ResponseAddress) != string(got.ResponseAddress) {
 | 
					
						
							| 
									
										
										
										
											2021-01-20 15:59:03 +08:00
										 |  |  | 		w.t.Errorf("Expected response address %s, got %s", ex.ResponseAddress, got.ResponseAddress)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	if *ex.QueryPort != *got.QueryPort {
 | 
					
						
							|  |  |  | 		w.t.Errorf("Expected port %d, got %d", *ex.QueryPort, *got.QueryPort)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if *ex.SocketFamily != *got.SocketFamily {
 | 
					
						
							|  |  |  | 		w.t.Errorf("Expected socket family %d, got %d", *ex.SocketFamily, *got.SocketFamily)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	w.queue = w.queue[1:]
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDnstap(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	q := test.Case{Qname: "example.org", Qtype: dns.TypeA}.Msg()
 | 
					
						
							|  |  |  | 	r := test.Case{
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 		Qname: "example.org.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 			test.A("example.org. 3600	IN	A 10.0.0.1"),
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	}.Msg()
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	tapq := testMessage() // leave type unset for deepEqual
 | 
					
						
							|  |  |  | 	msg.SetType(tapq, tap.Message_CLIENT_QUERY)
 | 
					
						
							|  |  |  | 	tapr := testMessage()
 | 
					
						
							|  |  |  | 	msg.SetType(tapr, tap.Message_CLIENT_RESPONSE)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	testCase(t, tapq, tapr, q, r)
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | func testMessage() *tap.Message {
 | 
					
						
							|  |  |  | 	inet := tap.SocketFamily_INET
 | 
					
						
							|  |  |  | 	udp := tap.SocketProtocol_UDP
 | 
					
						
							|  |  |  | 	port := uint32(40212)
 | 
					
						
							|  |  |  | 	return &tap.Message{
 | 
					
						
							|  |  |  | 		SocketFamily:   &inet,
 | 
					
						
							|  |  |  | 		SocketProtocol: &udp,
 | 
					
						
							|  |  |  | 		QueryAddress:   net.ParseIP("10.240.0.1"),
 | 
					
						
							|  |  |  | 		QueryPort:      &port,
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |