| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | package test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net" | 
					
						
							|  |  |  | 	"reflect" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/dnstap/msg" | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-20 11:01:06 +01:00
										 |  |  | 	"context" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	tap "github.com/dnstap/golang-dnstap" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | // Context is a message trap. | 
					
						
							|  |  |  | type Context struct { | 
					
						
							|  |  |  | 	context.Context | 
					
						
							|  |  |  | 	TrapTapper | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TestingData returns the Data matching coredns/test.ResponseWriter. | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | func TestingData() (d *msg.Builder) { | 
					
						
							|  |  |  | 	d = &msg.Builder{ | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 		SocketFam:   tap.SocketFamily_INET, | 
					
						
							|  |  |  | 		SocketProto: tap.SocketProtocol_UDP, | 
					
						
							|  |  |  | 		Address:     net.ParseIP("10.240.0.1"), | 
					
						
							|  |  |  | 		Port:        40212, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type comp struct { | 
					
						
							|  |  |  | 	Type  *tap.Message_Type | 
					
						
							|  |  |  | 	SF    *tap.SocketFamily | 
					
						
							|  |  |  | 	SP    *tap.SocketProtocol | 
					
						
							|  |  |  | 	QA    []byte | 
					
						
							|  |  |  | 	RA    []byte | 
					
						
							|  |  |  | 	QP    *uint32 | 
					
						
							|  |  |  | 	RP    *uint32 | 
					
						
							|  |  |  | 	QTSec bool | 
					
						
							|  |  |  | 	RTSec bool | 
					
						
							|  |  |  | 	RM    []byte | 
					
						
							|  |  |  | 	QM    []byte | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func toComp(m *tap.Message) comp { | 
					
						
							|  |  |  | 	return comp{ | 
					
						
							|  |  |  | 		Type:  m.Type, | 
					
						
							|  |  |  | 		SF:    m.SocketFamily, | 
					
						
							|  |  |  | 		SP:    m.SocketProtocol, | 
					
						
							|  |  |  | 		QA:    m.QueryAddress, | 
					
						
							|  |  |  | 		RA:    m.ResponseAddress, | 
					
						
							|  |  |  | 		QP:    m.QueryPort, | 
					
						
							|  |  |  | 		RP:    m.ResponsePort, | 
					
						
							|  |  |  | 		QTSec: m.QueryTimeSec != nil, | 
					
						
							|  |  |  | 		RTSec: m.ResponseTimeSec != nil, | 
					
						
							|  |  |  | 		RM:    m.ResponseMessage, | 
					
						
							|  |  |  | 		QM:    m.QueryMessage, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | // MsgEqual compares two dnstap messages ignoring timestamps. | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | func MsgEqual(a, b *tap.Message) bool { | 
					
						
							|  |  |  | 	return reflect.DeepEqual(toComp(a), toComp(b)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | // TrapTapper traps messages. | 
					
						
							|  |  |  | type TrapTapper struct { | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	Trap []*tap.Message | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 	Full bool | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | // Pack returns field Full. | 
					
						
							|  |  |  | func (t *TrapTapper) Pack() bool { | 
					
						
							|  |  |  | 	return t.Full | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | // TapMessage adds the message to the trap. | 
					
						
							|  |  |  | func (t *TrapTapper) TapMessage(m *tap.Message) { | 
					
						
							|  |  |  | 	t.Trap = append(t.Trap, m) | 
					
						
							| 
									
										
										
										
											2017-09-01 12:41:41 +02:00
										 |  |  | } |