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
|
package template
import (
"testing"
"github.com/mholt/caddy"
)
func TestSetup(t *testing.T) {
c := caddy.NewTestController("dns", `template ANY ANY {
rcode
}`)
err := setupTemplate(c)
if err == nil {
t.Errorf("Expected setupTemplate to fail on broken template, got no error")
}
c = caddy.NewTestController("dns", `template ANY ANY {
rcode NXDOMAIN
}`)
err = setupTemplate(c)
if err != nil {
t.Errorf("Expected no errors, got: %v", err)
}
}
func TestSetupParse(t *testing.T) {
serverBlockKeys := []string{"domain.com.:8053", "dynamic.domain.com.:8053"}
tests := []struct {
inputFileRules string
shouldErr bool
}{
// parse errors
{`template`, true},
{`template X`, true},
{`template ANY`, true},
{`template ANY X`, true},
{`template ANY ANY (?P<x>`, true},
{
`template ANY ANY {
}`,
true,
},
{
`template ANY ANY .* {
notavailable
}`,
true,
},
{
`template ANY ANY {
answer
}`,
true,
},
{
`template ANY ANY {
additional
}`,
true,
},
{
`template ANY ANY {
rcode
}`,
true,
},
{
`template ANY ANY {
rcode UNDEFINED
}`,
true,
},
{
`template ANY ANY {
answer "{{"
}`,
true,
},
{
`template ANY ANY {
additional "{{"
}`,
true,
},
{
`template ANY ANY {
authority "{{"
}`,
true,
},
// examples
{
`template ANY A ip-(?P<a>[0-9]*)-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]com {
answer "{{ .Name }} A {{ .Group.a }}.{{ .Group.b }}.{{ .Group.c }}.{{ .Grup.d }}."
}`,
false,
},
{
`template IN ANY "[.](example[.]com[.]dc1[.]example[.]com[.])$" {
rcode NXDOMAIN
answer "{{ index .Match 1 }} 60 IN SOA a.{{ index .Match 1 }} b.{{ index .Match 1 }} (1 60 60 60 60)"
}`,
false,
},
{
`template IN A ^ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$ {
answer "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
}
template IN MX ^ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$ {
answer "{{ .Name }} 60 IN MX 10 {{ .Name }}"
additional "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
}`,
false,
},
{
`template IN MX ^ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$ {
answer "{{ .Name }} 60 IN MX 10 {{ .Name }}"
additional "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
authority "example. 60 IN NS ns0.example."
authority "example. 60 IN NS ns1.example."
additional "ns0.example. 60 IN A 203.0.113.8"
additional "ns1.example. 60 IN A 198.51.100.8"
}`,
false,
},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.inputFileRules)
c.ServerBlockKeys = serverBlockKeys
templates, err := templateParse(c)
if err == nil && test.shouldErr {
t.Fatalf("Test %d expected errors, but got no error\n---\n%s\n---\n%v", i, test.inputFileRules, templates)
} else if err != nil && !test.shouldErr {
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
}
}
}
|