This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_test.go
235 lines (207 loc) · 6.93 KB
/
generate_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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package dns
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestGenerateRangeGuard(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "dns")
if err != nil {
t.Fatalf("could not create tmpdir for test: %v", err)
}
defer os.RemoveAll(tmpdir)
for i := 0; i <= 1; i++ {
path := filepath.Join(tmpdir, fmt.Sprintf("%04d.conf", i))
data := []byte(fmt.Sprintf("dhcp-%04d A 10.0.0.%d", i, i))
if err := ioutil.WriteFile(path, data, 0644); err != nil {
t.Fatalf("could not create tmpfile for test: %v", err)
}
}
var tests = [...]struct {
zone string
fail bool
}{
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,4,d} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,0,x} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 128-129 dhcp-${-128,4,d} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 128-129 dhcp-${-129,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-2 dhcp-${2147483647,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${2147483646,4,d} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1/step dhcp-${0,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1/ dhcp-${0,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-10/2 dhcp-${0,4,d} A 10.0.0.$
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1/0 dhcp-${0,4,d} A 10.0.0.$
`, true},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 $$INCLUDE ` + tmpdir + string(filepath.Separator) + `${0,4,d}.conf
`, false},
{`@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,4,d} A 10.0.0.$
$GENERATE 0-2 dhcp-${0,4,d} A 10.1.0.$
`, false},
}
for i := range tests {
z := NewZoneParser(strings.NewReader(tests[i].zone), "test.", "test")
z.SetIncludeAllowed(true)
for _, ok := z.Next(); ok; _, ok = z.Next() {
}
err := z.Err()
if err != nil && !tests[i].fail {
t.Errorf("expected \n\n%s\nto be parsed, but got %v", tests[i].zone, err)
} else if err == nil && tests[i].fail {
t.Errorf("expected \n\n%s\nto fail, but got no error", tests[i].zone)
}
}
}
func TestGenerateIncludeDepth(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "dns")
if err != nil {
t.Fatalf("could not create tmpfile for test: %v", err)
}
defer os.Remove(tmpfile.Name())
zone := `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 $$INCLUDE ` + tmpfile.Name() + `
`
if _, err := tmpfile.WriteString(zone); err != nil {
t.Fatalf("could not write to tmpfile for test: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("could not close tmpfile for test: %v", err)
}
zp := NewZoneParser(strings.NewReader(zone), ".", tmpfile.Name())
zp.SetIncludeAllowed(true)
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
const expected = "too deeply nested $INCLUDE"
if err := zp.Err(); err == nil || !strings.Contains(err.Error(), expected) {
t.Errorf("expected error to include %q, got %v", expected, err)
}
}
func TestGenerateIncludeDisallowed(t *testing.T) {
const zone = `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 $$INCLUDE test.conf
`
zp := NewZoneParser(strings.NewReader(zone), ".", "")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
const expected = "$INCLUDE directive not allowed"
if err := zp.Err(); err == nil || !strings.Contains(err.Error(), expected) {
t.Errorf("expected error to include %q, got %v", expected, err)
}
}
func TestGenerateSurfacesErrors(t *testing.T) {
const zone = `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,4,dd} A 10.0.0.$
`
zp := NewZoneParser(strings.NewReader(zone), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
const expected = `test: dns: bad base in $GENERATE: "${0,4,dd}" at line: 2:20`
if err := zp.Err(); err == nil || err.Error() != expected {
t.Errorf("expected specific error, wanted %q, got %v", expected, err)
}
}
func TestGenerateSurfacesLexerErrors(t *testing.T) {
const zone = `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 0-1 dhcp-${0,4,d} A 10.0.0.$ )
`
zp := NewZoneParser(strings.NewReader(zone), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
const expected = `test: dns: bad data in $GENERATE directive: "extra closing brace" at line: 2:40`
if err := zp.Err(); err == nil || err.Error() != expected {
t.Errorf("expected specific error, wanted %q, got %v", expected, err)
}
}
func TestGenerateModToPrintf(t *testing.T) {
tests := []struct {
mod string
wantFmt string
wantOffset int64
wantErr bool
}{
{"0,0,d", "%d", 0, false},
{"0,0", "%d", 0, false},
{"0", "%d", 0, false},
{"3,2,d", "%02d", 3, false},
{"3,2", "%02d", 3, false},
{"3", "%d", 3, false},
{"0,0,o", "%o", 0, false},
{"0,0,x", "%x", 0, false},
{"0,0,X", "%X", 0, false},
{"0,0,z", "", 0, true},
{"0,0,0,d", "", 0, true},
{"-100,0,d", "%d", -100, false},
}
for _, test := range tests {
gotFmt, gotOffset, errMsg := modToPrintf(test.mod)
switch {
case errMsg != "" && !test.wantErr:
t.Errorf("modToPrintf(%q) - expected empty-error, but got %v", test.mod, errMsg)
case errMsg == "" && test.wantErr:
t.Errorf("modToPrintf(%q) - expected error, but got empty-error", test.mod)
case gotFmt != test.wantFmt:
t.Errorf("modToPrintf(%q) - expected format %q, but got %q", test.mod, test.wantFmt, gotFmt)
case gotOffset != test.wantOffset:
t.Errorf("modToPrintf(%q) - expected offset %d, but got %d", test.mod, test.wantOffset, gotOffset)
}
}
}
func BenchmarkGenerate(b *testing.B) {
const zone = `@ IN SOA ns.test. hostmaster.test. ( 1 8h 2h 7d 1d )
$GENERATE 32-158 dhcp-${-32,4,d} A 10.0.0.$
`
for n := 0; n < b.N; n++ {
zp := NewZoneParser(strings.NewReader(zone), ".", "")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
if err := zp.Err(); err != nil {
b.Fatal(err)
}
}
}
func TestCrasherString(t *testing.T) {
tests := []struct {
in string
err string
}{
{"$GENERATE 0-300103\"$$GENERATE 2-2", "bad range in $GENERATE"},
{"$GENERATE 0-5414137360", "bad range in $GENERATE"},
{"$GENERATE 11522-3668518066406258", "bad range in $GENERATE"},
{"$GENERATE 0-200\"(;00000000000000\n$$GENERATE 0-0", "dns: garbage after $GENERATE range: \"\\\"\" at line: 1:16"},
{"$GENERATE 6-2048 $$GENERATE 6-036160 $$$$ORIGIN \\$", `dns: nested $GENERATE directive not allowed: "6-036160" at line: 1:19`},
}
for _, tc := range tests {
t.Run(tc.in, func(t *testing.T) {
_, err := NewRR(tc.in)
if err == nil {
t.Errorf("Expecting error for crasher line %s", tc.in)
}
if !strings.Contains(err.Error(), tc.err) {
t.Errorf("Expecting error %s, got %s", tc.err, err.Error())
}
})
}
}