-
Notifications
You must be signed in to change notification settings - Fork 2
/
zone_test.go
207 lines (193 loc) · 4.14 KB
/
zone_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
package newdns
import (
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func TestZoneValidate(t *testing.T) {
table := []struct {
zne Zone
err string
}{
{
zne: Zone{
Name: "foo",
},
err: "name not fully qualified: foo",
},
{
zne: Zone{
Name: "example.com.",
MasterNameServer: "foo",
},
err: "master server not full qualified: foo",
},
{
zne: Zone{
Name: "example.com.",
MasterNameServer: "n1.example.com.",
},
err: "missing name servers",
},
{
zne: Zone{
Name: "example.com.",
MasterNameServer: "n1.example.com.",
AllNameServers: []string{
"foo",
},
},
err: "name server not fully qualified: foo",
},
{
zne: Zone{
Name: "example.com.",
MasterNameServer: "n2.example.com.",
AllNameServers: []string{
"n1.example.com.",
},
},
err: "master name server not listed as name server: n2.example.com.",
},
{
zne: Zone{
Name: "example.com.",
MasterNameServer: "n1.example.com.",
AllNameServers: []string{
"n1.example.com.",
},
},
},
{
zne: Zone{
Name: "example.com.",
MasterNameServer: "n1.example.com.",
AllNameServers: []string{
"n1.example.com.",
},
AdminEmail: "[email protected]",
},
err: "admin email cannot be converted to a domain name: [email protected]",
},
{
zne: Zone{
Name: "example.com.",
MasterNameServer: "n1.example.com.",
AllNameServers: []string{
"n1.example.com.",
},
Refresh: 1,
Retry: 2,
},
err: "retry must be less than refresh: 2",
},
{
zne: Zone{
Name: "example.com.",
MasterNameServer: "n1.example.com.",
AllNameServers: []string{
"n1.example.com.",
},
Expire: 1,
Retry: 2,
},
err: "expire must be bigger than the sum of refresh and retry: 1",
},
}
for i, item := range table {
err := item.zne.Validate()
if err != nil {
assert.EqualValues(t, item.err, err.Error(), i)
} else {
assert.Equal(t, item.err, "", item)
}
}
}
func TestZoneLookup(t *testing.T) {
zone := Zone{
Name: "example.com.",
MasterNameServer: "ns1.example.com.",
AllNameServers: []string{
"ns1.example.com.",
"ns2.example.com.",
},
Handler: func(name string) ([]Set, error) {
if name == "error" {
return nil, io.EOF
}
if name == "invalid1" {
return []Set{
{Name: "foo"},
}, nil
}
if name == "invalid2" {
return []Set{
{Name: "foo.", Type: A, Records: []Record{{Address: "1.2.3.4"}}},
}, nil
}
if name == "multiple" {
return []Set{
{Name: "foo.example.com.", Type: A, Records: []Record{{Address: "1.2.3.4"}}},
{Name: "foo.example.com.", Type: A, Records: []Record{{Address: "1.2.3.4"}}},
}, nil
}
if name == "" {
return []Set{
{Name: "example.com.", Type: CNAME, Records: []Record{{Address: "cool.com."}}},
}, nil
}
if name == "cname" {
return []Set{
{Name: "cname.example.com.", Type: A, Records: []Record{{Address: "1.2.3.4"}}},
{Name: "cname.example.com.", Type: CNAME, Records: []Record{{Address: "cool.com."}}},
}, nil
}
return nil, nil
},
}
err := zone.Validate()
assert.NoError(t, err)
table := []struct {
name string
err string
}{
{
name: "foo",
err: "invalid name: foo",
},
{
name: "foo.",
err: "name does not belong to zone: foo.",
},
{
name: "error.example.com.",
err: "zone handler error: EOF",
},
{
name: "invalid1.example.com.",
err: "invalid set: invalid name: foo",
},
{
name: "invalid2.example.com.",
err: "set does not belong to zone: foo.",
},
{
name: "multiple.example.com.",
err: "multiple sets for same type",
},
{
name: "example.com.",
err: "invalid CNAME set at apex: example.com.",
},
{
name: "cname.example.com.",
err: "other sets with CNAME set: cname.example.com.",
},
}
for i, item := range table {
res, exists, err := zone.Lookup(item.name, A)
assert.Equal(t, item.err, err.Error(), i)
assert.False(t, exists, i)
assert.Nil(t, res, i)
}
}