-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_node_test.go
309 lines (303 loc) · 10.9 KB
/
name_node_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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package dnsutils_test
import (
"fmt"
"sort"
"testing"
"github.com/miekg/dns"
"github.com/mimuret/dnsutils"
. "github.com/mimuret/dnsutils/testtool"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestDNSUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "dnsutils Suite")
}
var _ = Describe("NameNode", func() {
var (
aRRSet, aaaaRRSet, blueRRSet *dnsutils.RRSet
root, www1, www2, www3, www4, blue, alpha, beta *dnsutils.NameNode
a11 = MustNewRR("example.jp. IN A 192.168.0.1")
a12 = MustNewRR("example.jp. IN A 192.168.0.2")
aaaa11 = MustNewRR("example.jp. IN AAAA 2001:db8::1")
aaaa12 = MustNewRR("example.jp. IN AAAA 2001:db8::2")
blueA = MustNewRR("blue.www4.example.jp. IN AAAA 2001:db8::1")
txt1 = MustNewRR(`example.jp. 300 IN TXT "hogehoge"`)
txt2 = MustNewRR(`example.jp. 300 IN TXT "hugahuga"`)
cname = MustNewRR(`example.jp. 300 IN CNAME www.example.jp.`)
dname = MustNewRR(`example.jp. 300 IN DNAME example.net.`)
)
BeforeEach(func() {
root = MustNewNameNode("example.jp", dns.ClassINET)
www1 = MustNewNameNode("www1.example.jp", dns.ClassINET)
www2 = MustNewNameNode("www2.example.jp", dns.ClassINET)
www3 = MustNewNameNode("www3.example.jp", dns.ClassINET)
www4 = MustNewNameNode("www4.example.jp", dns.ClassINET)
root.AddChildNameNode(www1)
root.AddChildNameNode(www2)
root.AddChildNameNode(www3)
root.AddChildNameNode(www4)
blue = MustNewNameNode("blue.www4.example.jp", dns.ClassINET)
www4.AddChildNameNode(blue)
alpha = MustNewNameNode("alpha.blue.www4.example.jp", dns.ClassINET)
blue.AddChildNameNode(alpha)
beta = MustNewNameNode("beta.alpha.blue.www4.example.jp", dns.ClassINET)
alpha.AddChildNameNode(beta)
aRRSet = MustNewRRSet("example.jp", 0, dns.ClassINET, dns.TypeA, []dns.RR{a11, a12})
aaaaRRSet = MustNewRRSet("example.jp", 0, dns.ClassINET, dns.TypeAAAA, []dns.RR{aaaa11, aaaa12})
root.SetRRSet(aRRSet)
root.SetRRSet(aaaaRRSet)
blueRRSet = MustNewRRSet("blue.www4.example.jp", 0, dns.ClassINET, dns.TypeA, []dns.RR{blueA})
blue.SetRRSet(blueRRSet)
})
Context("NewNameNode", func() {
When("name is domina name", func() {
It("returned NameNode", func() {
nn, err := dnsutils.NewNameNode("example.jp", dns.ClassINET)
Expect(err).To(Succeed())
Expect(nn).NotTo(BeNil())
Expect(nn.GetName()).To(Equal("example.jp."))
})
})
When("name is not domina name", func() {
It("returned NameNode", func() {
_, err := dnsutils.NewNameNode("..", dns.ClassINET)
Expect(err).To(HaveOccurred())
})
})
})
Context("Test for NameNode", func() {
It("GetName returned canonical Name", func() {
nn := MustNewNameNode("example.jp", dns.ClassINET)
Expect(nn).NotTo(BeNil())
Expect(nn.GetName()).To(Equal("example.jp."))
})
})
Context("Test for GetNameNode", func() {
It("returns current node with true (strict match)", func() {
nameNode, ok := root.GetNameNode("example.jp")
Expect(ok).To(BeTrue())
Expect(nameNode).To(Equal(root))
})
It("returns child node with true (strict match)", func() {
nameNode, ok := root.GetNameNode("www1.example.jp")
Expect(ok).To(BeTrue())
Expect(nameNode).To(Equal(www1))
})
It("returns grand child node with true (strict match)", func() {
nameNode, ok := root.GetNameNode("blue.www4.example.jp")
Expect(ok).To(BeTrue())
Expect(nameNode).To(Equal(blue))
})
It("returns nearly node with false (loose match)", func() {
nameNode, ok := root.GetNameNode("apple.www1.example.jp")
Expect(ok).To(BeFalse())
Expect(nameNode).To(Equal(www1))
})
It("returns grand child node with false (loose match)", func() {
nameNode, ok := root.GetNameNode("apple.blue.www4.example.jp")
Expect(ok).To(BeFalse())
Expect(nameNode).To(Equal(blue))
})
It("returns nil with false (if name is not node subdomain name and equals to node domain name)", func() {
nameNode, ok := root.GetNameNode("example2.jp")
Expect(ok).To(BeFalse())
Expect(nameNode).To(BeNil())
nameNode, ok = root.GetNameNode("jp")
Expect(ok).To(BeFalse())
Expect(nameNode).To(BeNil())
})
})
Context("Test for CopyChildNodes", func() {
It("returns child node", func() {
Expect(root.CopyChildNodes()).To(Equal(map[string]dnsutils.NameNodeInterface{
"www1.example.jp.": www1,
"www2.example.jp.": www2,
"www3.example.jp.": www3,
"www4.example.jp.": www4,
}))
})
})
Context("Test for CopyRRSetMap", func() {
It("returns RRSetInterface", func() {
Expect(root.CopyRRSetMap()).To(Equal(map[uint16]dnsutils.RRSetInterface{
dns.TypeA: aRRSet,
dns.TypeAAAA: aaaaRRSet,
}))
})
})
Context("Test for GetRRSet", func() {
It("returns RRSetInterface if exist", func() {
Expect(root.GetRRSet(dns.TypeA)).To(Equal(aRRSet))
})
It("returns nil if not exist", func() {
Expect(root.GetRRSet(dns.TypeCNAME)).To(BeNil())
})
})
Context("Test for SetValue", func() {
When("name not equals", func() {
It("returns ErrNameNotEqual", func() {
a := MustNewNameNode("example.net.", dns.ClassINET)
b := MustNewNameNode("www.example.net.", dns.ClassINET)
Expect(a.SetValue(b)).To(Equal(dnsutils.ErrNameNotEqual))
})
})
When("name not class", func() {
It("returns ErrNameNotEqual", func() {
a := MustNewNameNode("example.net.", dns.ClassINET)
b := MustNewNameNode("example.net.", dns.ClassCHAOS)
Expect(a.SetValue(b)).To(Equal(dnsutils.ErrClassNotEqual))
})
})
When("name not class", func() {
It("returns nil", func() {
a := MustNewNameNode("example.net.", dns.ClassINET)
b := MustNewNameNode("example.net.", dns.ClassINET)
brr := MustNewRR("example.net. 300 IN A 192.169.0.0")
b.SetRRSet(dnsutils.NewRRSetFromRR(brr))
Expect(a.SetValue(b)).To(BeNil())
Expect(a.GetRRSet(dns.TypeA).GetRRs()).To(Equal([]dns.RR{brr}))
})
})
})
Context("Test for IterateNameRRSet", func() {
It("can iterate all rrset", func() {
rrsetMap := map[uint16]dnsutils.RRSetInterface{}
root.IterateNameRRSet(func(set dnsutils.RRSetInterface) error {
rrsetMap[set.GetRRtype()] = set
return nil
})
Expect(rrsetMap).To(Equal(map[uint16]dnsutils.RRSetInterface{
dns.TypeA: aRRSet,
dns.TypeAAAA: aaaaRRSet,
}))
})
It("can return err", func() {
err := root.IterateNameRRSet(func(set dnsutils.RRSetInterface) error {
return fmt.Errorf("error")
})
Expect(err).To(HaveOccurred())
})
})
Context("Test for IterateNameNode", func() {
It("can iterate all node", func() {
names := sort.StringSlice{}
target := sort.StringSlice{"example.jp.", "www1.example.jp.", "www2.example.jp.", "www3.example.jp.", "www4.example.jp.", "blue.www4.example.jp.", "alpha.blue.www4.example.jp.", "beta.alpha.blue.www4.example.jp."}
root.IterateNameNode(func(n dnsutils.NameNodeInterface) error {
names = append(names, n.GetName())
return nil
})
names.Sort()
target.Sort()
Expect(names).To(Equal(target))
})
It("can return err", func() {
err := root.IterateNameNode(func(n dnsutils.NameNodeInterface) error {
return fmt.Errorf("error")
})
Expect(err).To(HaveOccurred())
err = root.IterateNameNode(func(n dnsutils.NameNodeInterface) error {
if n.GetName() != "example.jp." {
return fmt.Errorf("error")
}
return nil
})
Expect(err).To(HaveOccurred())
})
})
Context("Test AddChildNode", func() {
It("can set directly child node", func() {
www5 := MustNewNameNode("www5.example.jp", dns.ClassINET)
err := root.AddChildNameNode(www5)
Expect(err).To(Succeed())
nameNode, ok := root.GetNameNode("www5.example.jp")
Expect(ok).To(BeTrue())
Expect(nameNode).To(Equal(www5))
})
It("can't set not directly child node", func() {
red := MustNewNameNode("red.www5.example.jp", dns.ClassINET)
err := root.AddChildNameNode(red)
Expect(err).To(HaveOccurred())
})
It("can't set child node if already exist", func() {
err := root.AddChildNameNode(www4)
Expect(err).To(HaveOccurred())
})
})
Context("Test RemoveChildNameNode", func() {
It("can remove directly child node", func() {
err := root.RemoveChildNameNode("www1.example.jp")
Expect(err).To(Succeed())
_, ok := root.GetNameNode("www1.example.jp")
Expect(ok).To(BeFalse())
})
It("can not be remove not subomain", func() {
err := root.RemoveChildNameNode("example.jp")
Expect(err).To(HaveOccurred())
err = root.RemoveChildNameNode("example2.jp")
Expect(err).To(HaveOccurred())
err = root.RemoveChildNameNode("jp")
Expect(err).To(HaveOccurred())
})
})
Context("Test for SetRRSet", func() {
It("can set rrset", func() {
set := dnsutils.NewRRSetFromRR(txt1)
err := root.SetRRSet(set)
Expect(err).To(Succeed())
Expect(root.GetRRSet(dns.TypeTXT)).To(Equal(set))
set = dnsutils.NewRRSetFromRR(txt2)
dnsutils.NewRRSetFromRR(txt2)
err = root.SetRRSet(set)
Expect(err).To(Succeed())
Expect(root.GetRRSet(dns.TypeTXT)).To(Equal(set))
})
It("not able to set rrset, if rrset name is ignore ", func() {
set := MustNewRRSet("example2.jp.", 300, dns.ClassINET, dns.TypeTXT, nil)
err := root.SetRRSet(set)
Expect(err).To(HaveOccurred())
set = MustNewRRSet("jp.", 300, dns.ClassINET, dns.TypeTXT, nil)
err = root.SetRRSet(set)
Expect(err).To(HaveOccurred())
set = MustNewRRSet("www.example.jp.", 300, dns.ClassINET, dns.TypeTXT, nil)
err = root.SetRRSet(set)
Expect(err).To(HaveOccurred())
})
It("not able to set both cname and other ", func() {
set := dnsutils.NewRRSetFromRR(cname)
err := root.SetRRSet(set)
Expect(err).To(HaveOccurred())
})
It("not able to set both dname and other ", func() {
set := dnsutils.NewRRSetFromRR(dname)
err := root.SetRRSet(set)
Expect(err).To(HaveOccurred())
})
})
Context("Test for RemoveRRSet", func() {
It("can removeset rrset", func() {
r1 := MustNewRR("www1.example.net. 30 IN A 192.168.0.1")
set := dnsutils.NewRRSetFromRRs([]dns.RR{r1})
node := MustNewNameNode("www1.example.net.", dns.ClassINET)
err := node.SetRRSet(set)
Expect(err).To(Succeed())
Expect(node.GetRRSet(dns.TypeA)).To(Equal(set))
node.RemoveRRSet(dns.TypeAAAA)
Expect(node.GetRRSet(dns.TypeA)).To(Equal(set))
node.RemoveRRSet(dns.TypeA)
Expect(node.GetRRSet(dns.TypeA)).To(BeNil())
})
})
Context("Test for RRSetLen", func() {
It("returns the number of not empty rrset", func() {
Expect(root.RRSetLen()).To(Equal(2))
set := MustNewRRSet("example.jp.", 300, dns.ClassINET, dns.TypeTXT, nil)
err := root.SetRRSet(set)
Expect(err).To(Succeed())
Expect(root.RRSetLen()).To(Equal(2))
err = set.AddRR(txt1)
Expect(err).To(Succeed())
Expect(root.RRSetLen()).To(Equal(3))
})
})
})