forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
116 lines (94 loc) · 3.11 KB
/
router_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
package rdns
import (
"net"
"testing"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestRouterType(t *testing.T) {
r1 := new(TestResolver)
r2 := new(TestResolver)
q := new(dns.Msg)
var ci ClientInfo
route1, _ := NewRoute("", "", []string{"MX"}, nil, "", "", "", "", "", "", r1)
route2, _ := NewRoute("", "", nil, nil, "", "", "", "", "", "", r2)
router := NewRouter("my-router")
router.Add(route1, route2)
// Not MX record, should go to r2
q.SetQuestion("acme.test.", dns.TypeA)
_, err := router.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 0, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
// MX record, should go to r1
q.SetQuestion("acme.test.", dns.TypeMX)
_, err = router.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
}
func TestRouterClass(t *testing.T) {
r1 := new(TestResolver)
r2 := new(TestResolver)
q := new(dns.Msg)
var ci ClientInfo
route1, _ := NewRoute("", "ANY", nil, nil, "", "", "", "", "", "", r1)
route2, _ := NewRoute("", "", nil, nil, "", "", "", "", "", "", r2)
router := NewRouter("my-router")
router.Add(route1, route2)
// ClassINET question, should go to r2
q.SetQuestion("acme.test.", dns.TypeA)
_, err := router.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 0, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
// ClassAny should go to r1
q.Question = make([]dns.Question, 1)
q.Question[0] = dns.Question{"miek.nl.", dns.TypeMX, dns.ClassANY}
_, err = router.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
}
func TestRouterName(t *testing.T) {
r1 := new(TestResolver)
r2 := new(TestResolver)
q := new(dns.Msg)
var ci ClientInfo
route1, _ := NewRoute(`\.acme\.test\.$`, "", nil, nil, "", "", "", "", "", "", r1)
route2, _ := NewRoute("", "", nil, nil, "", "", "", "", "", "", r2)
router := NewRouter("my-router")
router.Add(route1, route2)
// No match, should go to r2
q.SetQuestion("bla.test.", dns.TypeA)
_, err := router.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 0, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
// Match, should go to r1
q.SetQuestion("x.acme.test.", dns.TypeMX)
_, err = router.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 1, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
}
func TestRouterSource(t *testing.T) {
r1 := new(TestResolver)
r2 := new(TestResolver)
q := new(dns.Msg)
q.SetQuestion("acme.test.", dns.TypeA)
route1, _ := NewRoute("", "", nil, nil, "", "", "192.168.1.100/32", "", "", "", r1)
route2, _ := NewRoute("", "", nil, nil, "", "", "", "", "", "", r2)
router := NewRouter("my-router")
router.Add(route1, route2)
// No match, should go to r2
_, err := router.Resolve(q, ClientInfo{SourceIP: net.ParseIP("192.168.1.50")})
require.NoError(t, err)
require.Equal(t, 0, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
// Match, should go to r1
_, err = router.Resolve(q, ClientInfo{SourceIP: net.ParseIP("192.168.1.100")})
require.NoError(t, err)
require.Equal(t, 1, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
}