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
|
package reverse
import (
"net"
"reflect"
"regexp"
"testing"
"github.com/mholt/caddy"
)
func TestSetupParse(t *testing.T) {
_, net4, _ := net.ParseCIDR("10.1.1.0/24")
_, net6, _ := net.ParseCIDR("fd01::/64")
regexIP4wildcard, _ := regexp.Compile("^.*ip-" + regexMatchV4 + "\\.domain\\.com\\.$")
regexIP6, _ := regexp.Compile("^ip-" + regexMatchV6 + "\\.domain\\.com\\.$")
regexIpv4dynamic, _ := regexp.Compile("^dynamic-" + regexMatchV4 + "-intern\\.dynamic\\.domain\\.com\\.$")
regexIpv6dynamic, _ := regexp.Compile("^dynamic-" + regexMatchV6 + "-intern\\.dynamic\\.domain\\.com\\.$")
regexIpv4vpndynamic, _ := regexp.Compile("^dynamic-" + regexMatchV4 + "-vpn\\.dynamic\\.domain\\.com\\.$")
serverBlockKeys := []string{"domain.com.:8053", "dynamic.domain.com.:8053"}
tests := []struct {
inputFileRules string
shouldErr bool
networks networks
}{
{
// with defaults
`reverse fd01::/64`,
false,
networks{network{
IPnet: net6,
Template: "ip-{ip}.domain.com.",
Zone: "domain.com.",
TTL: 60,
RegexMatchIP: regexIP6,
}},
},
{
`reverse`,
true,
networks{},
},
{
//no cidr
`reverse 10.1.1.1`,
true,
networks{},
},
{
//no cidr
`reverse 10.1.1.0/16 fd00::`,
true,
networks{},
},
{
// invalid key
`reverse 10.1.1.0/24 {
notavailable
}`,
true,
networks{},
},
{
// no domain suffix
`reverse 10.1.1.0/24 {
hostname ip-{ip}.
}`,
true,
networks{},
},
{
// hostname requires an second arg
`reverse 10.1.1.0/24 {
hostname
}`,
true,
networks{},
},
{
// template breaks regex compile
`reverse 10.1.1.0/24 {
hostname ip-{[-x
}`,
true,
networks{},
},
{
// ttl requires an (u)int
`reverse 10.1.1.0/24 {
ttl string
}`,
true,
networks{},
},
{
`reverse fd01::/64 {
hostname dynamic-{ip}-intern.{zone[2]}
ttl 50
}
reverse 10.1.1.0/24 {
hostname dynamic-{ip}-vpn.{zone[2]}
fallthrough
}`,
false,
networks{network{
IPnet: net6,
Template: "dynamic-{ip}-intern.dynamic.domain.com.",
Zone: "dynamic.domain.com.",
TTL: 50,
RegexMatchIP: regexIpv6dynamic,
}, network{
IPnet: net4,
Template: "dynamic-{ip}-vpn.dynamic.domain.com.",
Zone: "dynamic.domain.com.",
TTL: 60,
RegexMatchIP: regexIpv4vpndynamic,
}},
},
{
// multiple networks in one stanza
`reverse fd01::/64 10.1.1.0/24 {
hostname dynamic-{ip}-intern.{zone[2]}
ttl 50
fallthrough
}`,
false,
networks{network{
IPnet: net6,
Template: "dynamic-{ip}-intern.dynamic.domain.com.",
Zone: "dynamic.domain.com.",
TTL: 50,
RegexMatchIP: regexIpv6dynamic,
}, network{
IPnet: net4,
Template: "dynamic-{ip}-intern.dynamic.domain.com.",
Zone: "dynamic.domain.com.",
TTL: 50,
RegexMatchIP: regexIpv4dynamic,
}},
},
{
// fix domain in template
`reverse fd01::/64 {
hostname dynamic-{ip}-intern.dynamic.domain.com
ttl 300
fallthrough
}`,
false,
networks{network{
IPnet: net6,
Template: "dynamic-{ip}-intern.dynamic.domain.com.",
Zone: "dynamic.domain.com.",
TTL: 300,
RegexMatchIP: regexIpv6dynamic,
}},
},
{
`reverse 10.1.1.0/24 {
hostname ip-{ip}.{zone[1]}
ttl 50
wildcard
fallthrough
}`,
false,
networks{network{
IPnet: net4,
Template: "ip-{ip}.domain.com.",
Zone: "domain.com.",
TTL: 50,
RegexMatchIP: regexIP4wildcard,
}},
},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.inputFileRules)
c.ServerBlockKeys = serverBlockKeys
networks, _, err := reverseParse(c)
if err == nil && test.shouldErr {
t.Fatalf("Test %d expected errors, but got no error", i)
} else if err != nil && !test.shouldErr {
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
}
for j, n := range networks {
reflect.DeepEqual(test.networks[j], n)
if !reflect.DeepEqual(test.networks[j], n) {
t.Fatalf("Test %d/%d expected %v, got %v", i, j, test.networks[j], n)
}
}
}
}
|