| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | package proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	"testing"
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:46:41 +01:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/dnstap/msg"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/dnstap/test"
 | 
					
						
							|  |  |  | 	mwtest "github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tap "github.com/dnstap/golang-dnstap"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | func testCase(t *testing.T, ex Exchanger, q, r *dns.Msg, datq, datr *msg.Builder) {
 | 
					
						
							|  |  |  | 	tapq, _ := datq.ToOutsideQuery(tap.Message_FORWARDER_QUERY)
 | 
					
						
							|  |  |  | 	tapr, _ := datr.ToOutsideResponse(tap.Message_FORWARDER_RESPONSE)
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	ctx := test.Context{}
 | 
					
						
							|  |  |  | 	err := toDnstap(&ctx, "10.240.0.1:40212", ex,
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:46:41 +01:00
										 |  |  | 		request.Request{W: &mwtest.ResponseWriter{}, Req: q}, r, time.Now())
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(ctx.Trap) != 2 {
 | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Messages: %d", len(ctx.Trap))
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	if !test.MsgEqual(ctx.Trap[0], tapq) {
 | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Errorf("Want: %v\nhave: %v", tapq, ctx.Trap[0])
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	if !test.MsgEqual(ctx.Trap[1], tapr) {
 | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Errorf("Want: %v\nhave: %v", tapr, ctx.Trap[1])
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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, tapr := test.TestingData(), test.TestingData()
 | 
					
						
							|  |  |  | 	testCase(t, newDNSEx(), q, r, tapq, tapr)
 | 
					
						
							|  |  |  | 	tapq.SocketProto = tap.SocketProtocol_TCP
 | 
					
						
							|  |  |  | 	tapr.SocketProto = tap.SocketProtocol_TCP
 | 
					
						
							|  |  |  | 	testCase(t, newDNSExWithOption(Options{ForceTCP: true}), q, r, tapq, tapr)
 | 
					
						
							|  |  |  | 	testCase(t, newGoogle("", []string{"8.8.8.8:53", "8.8.4.4:53"}), q, r, tapq, tapr)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestNoDnstap(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:46:41 +01:00
										 |  |  | 	err := toDnstap(context.TODO(), "", nil, request.Request{}, nil, time.Now())
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |