-
Notifications
You must be signed in to change notification settings - Fork 74
/
setup_test.go
168 lines (162 loc) · 3.42 KB
/
setup_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
package wgsd
import (
"net"
"reflect"
"testing"
"github.com/coredns/caddy"
)
func TestSetup(t *testing.T) {
_, prefix1, _ := net.ParseCIDR("1.1.1.1/32")
_, prefix2, _ := net.ParseCIDR("2.2.2.2/32")
_, prefix3, _ := net.ParseCIDR("3.3.3.3/32")
_, prefix4, _ := net.ParseCIDR("4.4.4.4/32")
endpoint1 := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51820}
testCases := []struct {
name string
input string
shouldErr bool
expectedZones Zones
}{
{
"valid input",
"wgsd example.com. wg0",
false,
Zones{
Z: map[string]*Zone{
"example.com.": {
name: "example.com.",
device: "wg0",
},
},
Names: []string{"example.com."},
},
},
{
"missing token",
"wgsd example.com.",
true,
Zones{},
},
{
"too many tokens",
"wgsd example.com. wg0 extra",
true,
Zones{},
},
{
"valid self allowed-ips",
`wgsd example.com. wg0 {
self 1.1.1.1/32 2.2.2.2/32
}`,
false,
Zones{
Z: map[string]*Zone{
"example.com.": {
name: "example.com.",
device: "wg0",
serveSelf: true,
selfAllowedIPs: []net.IPNet{*prefix1, *prefix2},
},
},
Names: []string{"example.com."},
},
},
{
"invalid self-allowed-ips",
`wgsd example.com. wg0 {
self 1.1.11/32 2.2.2.2/32
}`,
true,
Zones{},
},
{
"valid self-endpoint",
`wgsd example.com. wg0 {
self 127.0.0.1:51820
}`,
false,
Zones{
Z: map[string]*Zone{
"example.com.": {
name: "example.com.",
device: "wg0",
serveSelf: true,
selfEndpoint: endpoint1,
},
},
Names: []string{"example.com."},
},
},
{
"invalid self-endpoint",
`wgsd example.com. wg0 {
self hostname:51820
}`,
true,
Zones{},
},
{
"multiple blocks",
`wgsd example.com. wg0 {
self 127.0.0.1:51820 1.1.1.1/32 2.2.2.2/32
}
wgsd example2.com. wg1 {
self 127.0.0.1:51820 3.3.3.3/32 4.4.4.4/32
}`,
false,
Zones{
Z: map[string]*Zone{
"example.com.": {
name: "example.com.",
device: "wg0",
serveSelf: true,
selfEndpoint: endpoint1,
selfAllowedIPs: []net.IPNet{*prefix1, *prefix2},
},
"example2.com.": {
name: "example2.com.",
device: "wg1",
serveSelf: true,
selfEndpoint: endpoint1,
selfAllowedIPs: []net.IPNet{*prefix3, *prefix4},
},
},
Names: []string{"example.com.", "example2.com."},
},
},
{
"all options",
`wgsd example.com. wg0 {
self 127.0.0.1:51820 1.1.1.1/32 2.2.2.2/32
}`,
false,
Zones{
Z: map[string]*Zone{
"example.com.": {
name: "example.com.",
device: "wg0",
serveSelf: true,
selfEndpoint: endpoint1,
selfAllowedIPs: []net.IPNet{*prefix1, *prefix2},
},
},
Names: []string{"example.com."},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c := caddy.NewTestController("dns", tc.input)
zones, err := parse(c)
if err == nil && tc.shouldErr {
t.Fatal("expected errors, but got no error")
} else if err != nil && !tc.shouldErr {
t.Fatalf("expected no errors, but got '%v'", err)
} else {
if !reflect.DeepEqual(tc.expectedZones, zones) {
t.Fatalf("expected %v, got %v", tc.expectedZones, zones)
}
}
})
}
}