-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzone_test.go
More file actions
257 lines (245 loc) · 7.03 KB
/
Copy pathzone_test.go
File metadata and controls
257 lines (245 loc) · 7.03 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package dnsutils_test
import (
"bytes"
_ "embed"
"encoding/json"
"github.com/miekg/dns"
"github.com/mimuret/dnsutils"
. "github.com/mimuret/dnsutils/testtool"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
//go:embed testdata/example.jp.normal
var testZoneNormal []byte
//go:embed testdata/example.jp.error
var testZoneError []byte
//go:embed testdata/example.jp.out-of-zone
var testZoneOutofZoneError []byte
//go:embed testdata/example.jp.normalize
var testZoneNormalize []byte
//go:embed testdata/example.jp.name-error
var testZoneNameError []byte
//go:embed testdata/example.jp.parse
var testZoneParseError []byte
//go:embed testdata/example.jp.normal.json
var testZoneJsonNormal []byte
//go:embed testdata/example.jp.name-error.json
var testZoneJsonNameError []byte
var _ = Describe("Zone", func() {
var (
err error
z *dnsutils.Zone
)
BeforeEach(func() {
z, err = dnsutils.NewZone("example.jp", dns.ClassINET, nil)
Expect(err).To(Succeed())
})
Context("Test for NewZone", func() {
When("valid zone name", func() {
It("returns zone", func() {
Expect(z).NotTo(BeNil())
})
})
When("invalid zone name", func() {
BeforeEach(func() {
_, err = dnsutils.NewZone("...", dns.ClassINET, nil)
})
It("returns ErrBadName", func() {
Expect(err).To(Equal(dnsutils.ErrBadName))
})
})
})
Context("Test for GetName", func() {
It("returns canonical zone name", func() {
Expect(z.GetName()).To(Equal("example.jp."))
})
})
Context("Test for GetRootNode", func() {
It("returns root NameNode", func() {
Expect(z.GetRootNode().GetName()).To(Equal("example.jp."))
})
})
Context("Test for Read", func() {
When("valid data", func() {
BeforeEach(func() {
testZoneNormalBuf := bytes.NewBuffer(testZoneNormal)
z = &dnsutils.Zone{}
err = z.Read(testZoneNormalBuf)
})
It("can read data", func() {
Expect(err).To(Succeed())
Expect(z.GetName()).To(Equal("example.jp."))
Expect(z.GetClass()).To(Equal(dns.Class(dns.ClassINET)))
Expect(z.GetRootNode()).NotTo(BeNil())
nn, ok := z.GetRootNode().GetNameNode("test.hoge.example.jp")
Expect(ok).To(BeTrue())
Expect(nn.GetName()).To(Equal("test.hoge.example.jp."))
set := nn.GetRRSet(dns.TypeA)
Expect(set).NotTo(BeNil())
Expect(set.GetRRtype()).To(Equal(dns.TypeA))
Expect(set.GetRRs()).To(Equal([]dns.RR{
MustNewRR("test.hoge.example.jp. 3600 IN A 192.168.2.1"),
MustNewRR("test.hoge.example.jp. 3600 IN A 192.168.2.2"),
}))
})
})
When("normalize", func() {
BeforeEach(func() {
testZoneNormalBuf := bytes.NewBuffer(testZoneNormalize)
z = &dnsutils.Zone{}
err = z.Read(testZoneNormalBuf)
Expect(err).To(Succeed())
err = dnsutils.ZoneNormalize(z)
})
It("can read data", func() {
Expect(err).To(Succeed())
Expect(z.GetName()).To(Equal("example.jp."))
Expect(z.GetClass()).To(Equal(dns.Class(dns.ClassINET)))
Expect(z.GetRootNode()).NotTo(BeNil())
_, ok := z.GetRootNode().GetNameNode("sub1.example.jp.")
Expect(ok).To(BeTrue())
_, ok = z.GetRootNode().GetNameNode("ns.sub1.example.jp.")
Expect(ok).To(BeTrue())
_, ok = z.GetRootNode().GetNameNode("sub2.example.jp.")
Expect(ok).To(BeTrue())
_, ok = z.GetRootNode().GetNameNode("ns.sub2.example.jp.")
Expect(ok).To(BeFalse())
_, ok = z.GetRootNode().GetNameNode("sub3.example.jp.")
Expect(ok).To(BeTrue())
_, ok = z.GetRootNode().GetNameNode("ns.sub.sub3.example.jp.")
Expect(ok).To(BeTrue())
})
})
When("out of zone data", func() {
BeforeEach(func() {
testZoneNormalBuf := bytes.NewBuffer(testZoneOutofZoneError)
z = &dnsutils.Zone{}
err = z.Read(testZoneNormalBuf)
})
It("can't read not valid data", func() {
Expect(err).To(HaveOccurred())
})
})
When("include can't add RR (duplicate cname)", func() {
BeforeEach(func() {
testZoneNormalBuf := bytes.NewBuffer(testZoneError)
z = &dnsutils.Zone{}
err = z.Read(testZoneNormalBuf)
})
It("can't read not valid data", func() {
Expect(err).To(HaveOccurred())
})
})
When("include invalid format", func() {
BeforeEach(func() {
testZoneNormalBuf := bytes.NewBuffer(testZoneParseError)
z = &dnsutils.Zone{}
err = z.Read(testZoneNormalBuf)
})
It("can't parse record", func() {
Expect(err).To(HaveOccurred())
})
})
When("include not zone subdomain", func() {
BeforeEach(func() {
testZoneNormalBuf := bytes.NewBuffer(testZoneNameError)
z = &dnsutils.Zone{}
err = z.Read(testZoneNormalBuf)
})
It("can't read", func() {
Expect(err).To(HaveOccurred())
})
})
})
Context("Test for UnmarshalJSON", func() {
BeforeEach(func() {
z = &dnsutils.Zone{}
})
When("valid date", func() {
BeforeEach(func() {
err = json.Unmarshal(testZoneJsonNormal, z)
})
It("returns not error", func() {
Expect(err).To(Succeed())
testZoneNormalBuf := bytes.NewBuffer(testZoneNormal)
z2, _ := dnsutils.NewZone("example.jp", dns.ClassINET, nil)
err := z2.Read(testZoneNormalBuf)
Expect(err).To(Succeed())
Expect(z2).To(Equal(z2))
})
})
When("json type invalid", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`{"name":"example.jp.","class":1}`), z)
})
It("returns not error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(MatchRegexp("failed to parse json format"))
})
})
When("name invalid", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`{"name":"example..jp.","class":"IN"}`), z)
})
It("returns not error", func() {
Expect(err).To(Equal(dnsutils.ErrBadName))
})
})
When("class invalid", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`{"name":"example.jp.","class":"HOGE"}`), z)
})
It("returns not error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(MatchRegexp("invalid class"))
})
})
When("rrset invalid", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(testZoneJsonNameError), z)
})
It("returns not error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(MatchRegexp("failed to set node"))
})
})
})
Context("Test for ZoneText", func() {
var (
b *bytes.Buffer
)
BeforeEach(func() {
testZoneNormalBuf := bytes.NewBuffer(testZoneNormal)
z = &dnsutils.Zone{}
err = z.Read(testZoneNormalBuf)
Expect(err).To(Succeed())
b = bytes.NewBuffer(nil)
err = dnsutils.ZoneText(z, b)
})
It("returns", func() {
Expect(err).To(Succeed())
z2 := &dnsutils.Zone{}
err := z2.Read(b)
Expect(err).To(Succeed())
Expect(dnsutils.IsEqualsAllTree(z.GetRootNode(), z2.GetRootNode(), true)).To(BeTrue())
})
})
Context("Test for MarshalJSON", func() {
var (
z *dnsutils.Zone
err error
bs []byte
)
BeforeEach(func() {
testZoneNormalBuf := bytes.NewBuffer(testZoneNormal)
z = &dnsutils.Zone{}
err = z.Read(testZoneNormalBuf)
Expect(err).To(Succeed())
bs, err = json.Marshal(z)
})
It("returns json data", func() {
Expect(err).To(Succeed())
Expect(bs).To(MatchJSON(testZoneJsonNormal))
})
})
})