-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_test.go
More file actions
54 lines (44 loc) · 1.2 KB
/
filter_test.go
File metadata and controls
54 lines (44 loc) · 1.2 KB
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
package main
import (
"testing"
"github.com/miekg/dns"
"gopkg.in/yaml.v2"
)
func TestUnmarshalFilter(t *testing.T) {
var filter nameFilter
var yamlBytes = []byte(`[ sie-network.net, dnsdb.info ]`)
err := yaml.Unmarshal(yamlBytes, &filter)
if err != nil {
t.Error(err)
}
}
func testLookup(f nameFilter, name string, res bool) func(t *testing.T) {
return func(t *testing.T) {
msg := &dns.Msg{Question: []dns.Question{{Name: name}}}
m, err := msg.Pack()
if err != nil {
t.Errorf("Failed to pack message: %v", err)
}
match, err := f.FilterMsgQname(m)
if err != nil {
t.Errorf("FilterMsgQname error: %v", err)
}
if match != res {
t.Errorf("FilterMsgQname(%s) returned %v, expected %v", name, match, res)
}
}
}
func TestFilterQuery(t *testing.T) {
var filter nameFilter
var yamlBytes = []byte(`[ sie-network.net, dnsdb.info ]`)
err := yaml.Unmarshal(yamlBytes, &filter)
if err != nil {
t.Error(err)
}
for _, n := range []string{"sie-network.net.", "a1.sie-network.net.", "a.b.c.d.e.dnsdb.info."} {
t.Run("match "+n, testLookup(filter, n, true))
}
for _, n := range []string{"sie-network.com.", "foo.net.", "info."} {
t.Run("nomatch "+n, testLookup(filter, n, false))
}
}