forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
names_test.go
124 lines (110 loc) · 3.13 KB
/
names_test.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// +build !integration
// Unit tests and benchmarks for the dns package.
// This file contains tests for queries' RR type
//
// TODO:
// * Add validation of special fields provided in MX, SOA, NS...
// * Use struct DnsTestMsg fields question, answers, authorities,... for struct DnsTestMessage
package dns
import (
"testing"
"github.com/elastic/beats/libbeat/common"
"github.com/stretchr/testify/assert"
)
type DnsTestMsg struct {
rawData []byte
question common.MapStr
answers []common.MapStr
authorities []common.MapStr
additionals []common.MapStr
opt common.MapStr
}
// DNS messages for testing.
var (
// An array of all test messages.
dnsTestRRs = []DnsTestMsg{
unhandledRR,
unknownRR,
opt,
}
unhandledRR = DnsTestMsg{ // RR specified in a RFC but not implemented in the package dns
rawData: []byte{
0x21, 0x51, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x65, 0x6c, 0x61,
0x73, 0x74, 0x69, 0x63, 0x02, 0x63, 0x6f, 0x00, 0x00, 0x1e, 0x00, 0x01,
},
question: common.MapStr{
"type": "NXT",
"name": "elastic.co.",
},
}
unknownRR = DnsTestMsg{ // RR unspecified in any known RFC
rawData: []byte{
0x21, 0x51, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x65, 0x6c, 0x61,
0x73, 0x74, 0x69, 0x63, 0x02, 0x63, 0x6f, 0x00, 0xff, 0x00, 0x00, 0x01,
},
question: common.MapStr{
"type": "65280",
"name": "elastic.co.",
},
}
opt = DnsTestMsg{
rawData: []byte{
0x50, 0x12, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x77, 0x77, 0x77,
0x04, 0x69, 0x65, 0x74, 0x66, 0x03, 0x6f, 0x72, 0x67, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
0x29, 0x10, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
},
question: common.MapStr{
"type": "A",
"name": "www.ietf.org.",
},
opt: common.MapStr{
"version": "0",
"do": true,
},
}
)
// oracleRRs and rrs should be sorted in the same order
func assertRRs(t testing.TB, oracleRRs []common.MapStr, rrs []common.MapStr) {
assert.Equal(t, len(oracleRRs), len(rrs))
for i, oracleRR := range oracleRRs {
rr := rrs[i]
for k, v := range oracleRR {
assert.NotNil(t, rr[k])
assert.Equal(t, v, rr[k])
}
}
}
func assertDnsMessage(t testing.TB, q DnsTestMsg) {
dns, err := decodeDnsData(TransportUdp, q.rawData)
if err != nil {
t.Error("failed to decode dns data")
}
mapStr := common.MapStr{}
addDnsToMapStr(mapStr, dns, true, true)
if q.question != nil {
for k, v := range q.question {
assert.NotNil(t, mapStr["question"].(common.MapStr)[k])
assert.Equal(t, v, mapStr["question"].(common.MapStr)[k])
}
}
if len(q.answers) > 0 {
assertRRs(t, q.answers, mapStr["answer"].([]common.MapStr))
}
if len(q.authorities) > 0 {
assertRRs(t, q.authorities, mapStr["authorities"].([]common.MapStr))
}
if len(q.additionals) > 0 {
assertRRs(t, q.additionals, mapStr["additionals"].([]common.MapStr))
}
if q.opt != nil {
for k, v := range q.opt {
assert.NotNil(t, mapStr["opt"].(common.MapStr)[k])
assert.Equal(t, v, mapStr["opt"].(common.MapStr)[k])
}
}
}
func TestAllRR(t *testing.T) {
for _, q := range dnsTestRRs {
assertDnsMessage(t, q)
}
}