forked from coredns/coredns
/
helpers.go
79 lines (68 loc) · 1.48 KB
/
helpers.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package test
import (
"context"
"net"
"reflect"
"github.com/coredns/coredns/plugin/dnstap/msg"
tap "github.com/dnstap/golang-dnstap"
)
// Context is a message trap.
type Context struct {
context.Context
TrapTapper
}
// TestingData returns the Data matching coredns/test.ResponseWriter.
func TestingData() (d *msg.Builder) {
d = &msg.Builder{
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,
}
}
// MsgEqual compares two dnstap messages ignoring timestamps.
func MsgEqual(a, b *tap.Message) bool {
return reflect.DeepEqual(toComp(a), toComp(b))
}
// TrapTapper traps messages.
type TrapTapper struct {
Trap []*tap.Message
Full bool
}
// Pack returns field Full.
func (t *TrapTapper) Pack() bool {
return t.Full
}
// TapMessage adds the message to the trap.
func (t *TrapTapper) TapMessage(m *tap.Message) {
t.Trap = append(t.Trap, m)
}