forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac-db_test.go
78 lines (70 loc) · 1.55 KB
/
mac-db_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
package rdns
import (
"encoding/hex"
"testing"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestMACParse(t *testing.T) {
var tests = []struct {
input string
out []byte
}{
{
input: "01:23:45:ab:cd:ef",
out: []byte{0x01, 0x23, 0x45, 0xab, 0xcd, 0xef},
},
{
input: "01:23:45:AB:CD:EF",
out: []byte{0x01, 0x23, 0x45, 0xab, 0xcd, 0xef},
},
}
for _, test := range tests {
out, err := parseMAC(test.input)
require.NoError(t, err)
require.Equal(t, test.out, out)
}
}
func TestMACParseFail(t *testing.T) {
var tests = []string{
"",
"01:23:45:ab:cd:ef:ab",
"01:2345:ab:cd:ef:",
"012345abcdef",
"012345abcdef:::::",
":::::012345abcdef",
}
for _, input := range tests {
_, err := parseMAC(input)
require.Errorf(t, err, "value %q", input)
}
}
func TestMACDB(t *testing.T) {
loader := NewStaticLoader([]string{
"# some comment",
" ",
"01:23:45:ab:cd:ef",
"01:01:01:ff:ff:ff",
})
m, err := NewMACDB("testlist", loader)
require.NoError(t, err)
tests := []struct {
mac []byte
match bool
}{
{[]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, false},
{[]byte{0x01, 0x23, 0x45, 0xab, 0xcd, 0xef}, true},
}
for _, test := range tests {
msg := new(dns.Msg)
msg.SetQuestion("example.com.", dns.TypeA)
e := new(dns.EDNS0_LOCAL)
e.Code = 65001
e.Data = test.mac
msg.SetEdns0(4096, false)
edns0 := msg.IsEdns0()
edns0.Option = append(edns0.Option, e)
_, _, _, ok := m.Match(msg)
require.Equal(t, test.match, ok, "value: %s", hex.EncodeToString(test.mac))
}
}