This repository has been archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
fdns_test.go
231 lines (206 loc) · 5.24 KB
/
fdns_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package fdns
import (
"bytes"
"context"
"reflect"
"testing"
"github.com/klauspost/pgzip"
)
func TestParse(t *testing.T) {
tt := []struct {
name string
input string
domain string
records []string
workers int
exp []string
}{
{
name: "cname 1 worker",
input: `{"timestamp": "1492468299","name": "reseauocoz.cluster007.foo.tld", "type": "cname", "value": "cluster007.bar.tld"}`,
domain: "foo.tld",
records: []string{"cname"},
workers: 1,
exp: []string{"reseauocoz.cluster007.foo.tld"},
},
{
name: "cname 2 workers",
input: `{"timestamp": "1492468299","name": "reseauocoz.cluster007.foo.tld", "type": "cname", "value": "cluster007.bar.tld"}
{"timestamp": "1492468299","name": "reseauocoz.cluster008.foo.tld", "type": "cname", "value": "cluster008.bar.tld"}
{"timestamp": "1492468299","name": "reseauocoz.cluster009.foo.tld", "type": "cname", "value": "cluster009.bar.tld"}`,
domain: "foo.tld",
records: []string{"cname"},
workers: 2,
exp: []string{"reseauocoz.cluster007.foo.tld", "reseauocoz.cluster008.foo.tld", "reseauocoz.cluster009.foo.tld"},
},
{
name: "a 1 worker",
input: `{"timestamp": "1492468299","name": "reseauocoz.cluster007.foo.tld", "type": "a", "value": "127.0.0.1"}`,
domain: "foo.tld",
records: []string{"a"},
workers: 1,
exp: []string{"reseauocoz.cluster007.foo.tld"},
},
{
name: "a 3 workers 2 entries",
input: `{"timestamp": "1492468299","name": "reseauocoz.cluster007.foo.tld", "type": "a", "value": "127.0.0.1"}
{"timestamp": "1492468299","name": "reseauocoz.cluster008.foo.tld", "type": "a", "value": "127.0.0.2"}`,
domain: "foo.tld",
records: []string{"a"},
workers: 3,
exp: []string{"reseauocoz.cluster007.foo.tld", "reseauocoz.cluster008.foo.tld"},
},
{
name: "ns 1 worker",
input: `{"timestamp": "1492468299","name": "ns.cdn.foo.tld", "type": "ns", "value": "ns.internal.foo.tld"}`,
domain: "foo.tld",
records: []string{"ns"},
workers: 1,
exp: []string{"ns.cdn.foo.tld"},
},
{
name: "aaaa 1 worker",
input: `{"timestamp": "1492468299","name": "1087074.ostk.bm2.corp.ne1.foo.tld", "type": "aaaa", "value": "2001:4998:efeb:202::5001"}`,
domain: "foo.tld",
records: []string{"aaaa"},
workers: 1,
exp: []string{"1087074.ostk.bm2.corp.ne1.foo.tld"},
},
{
name: "invalid JSON",
input: `{invalidJSON"timestamp": "1492468299","name": "reseauocoz.cluster007.foo.tld", "type": "cname", "value": "cluster007.bar.tld"}`,
domain: "foo.tld",
records: []string{"cname"},
workers: 1,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
out := make(chan string)
errs := make(chan error)
done := make(chan struct{})
p := Parser{
Domains: []string{tc.domain},
Workers: tc.workers,
Records: tc.records,
}
var res []string
go func() {
for c := range out {
res = append(res, c)
}
done <- struct{}{}
}()
go p.Parse(context.Background(), bytes.NewReader(encode(t, tc.input)), out, errs)
select {
case err := <-errs:
if tc.exp != nil {
t.Fatal(err)
}
case <-done:
}
if !equal(t, tc.exp, res) {
t.Fatalf("expected %q got %q", tc.exp, res)
}
})
}
}
func TestIsInterestingRecord(t *testing.T) {
tt := []struct {
name string
rec string
exp bool
}{
{
name: "interesting",
rec: "a",
exp: true,
},
{
name: "boring",
rec: "ns",
},
}
for _, tc := range tt {
p := &Parser{Records: []string{"a"}}
got := p.IsInterestingRecord(entry{Type: tc.rec})
if tc.exp != got {
t.Fatalf("%s: expected %v got %v", tc.name, tc.exp, got)
}
}
}
func TestIsInterestingDomain(t *testing.T) {
tt := []struct {
name string
domain string
exp bool
}{
{
name: "interesting",
domain: "bazz.example.com",
exp: true,
},
{
name: "boring",
domain: "bar.notexample.com",
},
}
for _, tc := range tt {
p := &Parser{Domains: []string{".example.com"}}
got := p.IsInterestingDomain(entry{Name: tc.domain})
if tc.exp != got {
t.Fatalf("%s: expected %v got %v", tc.name, tc.exp, got)
}
}
}
func TestSubstring(t *testing.T) {
tt := []struct {
sb string
domain string
exp bool
}{
{
sb: "interesting",
domain: "bazz.interestingexample.com",
exp: true,
},
{
sb: "boring",
domain: "bar.notexample.com",
},
}
for _, tc := range tt {
p := &Parser{Substrings: []string{"interesting"}, Records: []string{"a"}}
got := p.Substring(entry{Name: tc.domain, Type: "A", Value: "0.0.0.0"})
if tc.exp != got {
t.Fatalf("expected %v got %v for substring %q and domain %q", tc.exp, got, tc.sb, tc.domain)
}
}
}
func equal(t *testing.T, a, b []string) bool {
t.Helper()
m1 := make(map[string]struct{}, len(a))
for _, v := range a {
m1[v] = struct{}{}
}
m2 := make(map[string]struct{}, len(b))
for _, v := range b {
m2[v] = struct{}{}
}
return reflect.DeepEqual(m1, m2)
}
func encode(t *testing.T, s string) []byte {
t.Helper()
var b bytes.Buffer
gz := pgzip.NewWriter(&b)
if _, err := gz.Write([]byte(s)); err != nil {
t.Fatal(err)
}
if err := gz.Flush(); err != nil {
t.Fatal(err)
}
if err := gz.Close(); err != nil {
t.Fatal(err)
}
return b.Bytes()
}