Debian Codegraph @

tests/test_update.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose with or without fee is hereby granted,
7 # provided that the above copyright notice and this permission notice
8 # appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 import unittest
19 import binascii
20
21 import dns.update
22 import dns.rdata
23 import dns.rdataset
24 import dns.tsigkeyring
25
26
27 def hextowire(hex):
28 return binascii.unhexlify(hex.replace(" ", "").encode())
29
30
31 goodwire = hextowire(
32 "0001 2800 0001 0005 0007 0000"
33 "076578616d706c6500 0006 0001"
34 "03666f6fc00c 00ff 00ff 00000000 0000"
35 "c019 0001 00ff 00000000 0000"
36 "03626172c00c 0001 0001 00000000 0004 0a000005"
37 "05626c617a32c00c 00ff 00fe 00000000 0000"
38 "c049 0001 00fe 00000000 0000"
39 "c019 0001 00ff 00000000 0000"
40 "c019 0001 0001 0000012c 0004 0a000001"
41 "c019 0001 0001 0000012c 0004 0a000002"
42 "c035 0001 0001 0000012c 0004 0a000003"
43 "c035 0001 00fe 00000000 0004 0a000004"
44 "04626c617ac00c 0001 00ff 00000000 0000"
45 "c049 00ff 00ff 00000000 0000"
46 )
47
48 goodwirenone = hextowire(
49 "0001 2800 0001 0000 0001 0000"
50 "076578616d706c6500 0006 0001"
51 "03666f6fc00c 0001 00fe 00000000 0004 01020304"
52 )
53
54 badwirenone = hextowire(
55 "0001 2800 0001 0003 0000 0000"
56 "076578616d706c6500 0006 0001"
57 "03666f6fc00c 00ff 00ff 00000000 0000"
58 "c019 0001 00ff 00000000 0000"
59 "c019 0001 00fe 00000000 0004 01020304"
60 )
61
62 badwireany = hextowire(
63 "0001 2800 0001 0002 0000 0000"
64 "076578616d706c6500 0006 0001"
65 "03666f6fc00c 00ff 00ff 00000000 0000"
66 "c019 0001 00ff 00000000 0004 01020304"
67 )
68
69 badwireanyany = hextowire(
70 "0001 2800 0001 0001 0000 0000"
71 "076578616d706c6500 0006 0001"
72 "03666f6fc00c 00ff 00ff 00000000 0004 01020304"
73 )
74
75 badwirezonetype = hextowire(
76 "0001 2800 0001 0000 0000 0000" "076578616d706c6500 0001 0001"
77 )
78
79 badwirezoneclass = hextowire(
80 "0001 2800 0001 0000 0000 0000" "076578616d706c6500 0006 00ff"
81 )
82
83 badwirezonemulti = hextowire(
84 "0001 2800 0002 0000 0000 0000" "076578616d706c6500 0006 0001" "c019 0006 0001"
85 )
86
87 badwirenozone = hextowire(
88 "0001 2800 0000 0000 0001 0000"
89 "03666f6f076578616d706c6500 0001 0001 00000030 0004 01020304"
90 )
91
92 update_text = """id 1
93 opcode UPDATE
94 rcode NOERROR
95 ;ZONE
96 example. IN SOA
97 ;PREREQ
98 foo ANY ANY
99 foo ANY A
100 bar 0 IN A 10.0.0.5
101 blaz2 NONE ANY
102 blaz2 NONE A
103 ;UPDATE
104 foo ANY A
105 foo 300 IN A 10.0.0.1
106 foo 300 IN A 10.0.0.2
107 bar 300 IN A 10.0.0.3
108 bar 0 NONE A 10.0.0.4
109 blaz ANY A
110 blaz2 ANY ANY
111 """
112
113 added_text = """id 1
114 opcode UPDATE
115 ;ZONE
116 example. IN SOA
117 ;UPDATE
118 foo 300 IN A 10.0.0.1
119 foo 300 IN A 10.0.0.2
120 """
121
122 replaced_text = """id 1
123 opcode UPDATE
124 ;ZONE
125 example. IN SOA
126 ;UPDATE
127 foo ANY A
128 foo 300 IN A 10.0.0.1
129 foo 300 IN A 10.0.0.2
130 """
131
132 deleted_text = """id 1
133 opcode UPDATE
134 ;ZONE
135 example. IN SOA
136 ;UPDATE
137 foo 0 NONE A 10.0.0.1
138 foo 0 NONE A 10.0.0.2
139 """
140
141
142 class UpdateTestCase(unittest.TestCase):
143 def test_to_wire1(self): # type: () -> None
144 update = dns.update.Update("example")
145 update.id = 1
146 update.present("foo")
147 update.present("foo", "a")
148 update.present("bar", "a", "10.0.0.5")
149 update.absent("blaz2")
150 update.absent("blaz2", "a")
151 update.replace("foo", 300, "a", "10.0.0.1", "10.0.0.2")
152 update.add("bar", 300, "a", "10.0.0.3")
153 update.delete("bar", "a", "10.0.0.4")
154 update.delete("blaz", "a")
155 update.delete("blaz2")
156 self.assertEqual(update.to_wire(), goodwire)
157
158 def test_to_wire2(self): # type: () -> None
159 update = dns.update.Update("example")
160 update.id = 1
161 update.present("foo")
162 update.present("foo", "a")
163 update.present("bar", "a", "10.0.0.5")
164 update.absent("blaz2")
165 update.absent("blaz2", "a")
166 update.replace("foo", 300, "a", "10.0.0.1", "10.0.0.2")
167 update.add("bar", 300, dns.rdata.from_text(1, 1, "10.0.0.3"))
168 update.delete("bar", "a", "10.0.0.4")
169 update.delete("blaz", "a")
170 update.delete("blaz2")
171 self.assertEqual(update.to_wire(), goodwire)
172
173 def test_to_wire3(self): # type: () -> None
174 update = dns.update.Update("example")
175 update.id = 1
176 update.present("foo")
177 update.present("foo", "a")
178 update.present("bar", "a", "10.0.0.5")
179 update.absent("blaz2")
180 update.absent("blaz2", "a")
181 update.replace("foo", 300, "a", "10.0.0.1", "10.0.0.2")
182 update.add("bar", dns.rdataset.from_text(1, 1, 300, "10.0.0.3"))
183 update.delete("bar", "a", "10.0.0.4")
184 update.delete("blaz", "a")
185 update.delete("blaz2")
186 self.assertEqual(update.to_wire(), goodwire)
187
188 def test_from_text1(self): # type: () -> None
189 update = dns.message.from_text(update_text)
190 self.assertTrue(isinstance(update, dns.update.UpdateMessage))
191 w = update.to_wire(origin=dns.name.from_text("example"), want_shuffle=False)
192 self.assertEqual(w, goodwire)
193
194 def test_from_wire(self):
195 origin = dns.name.from_text("example")
196 u1 = dns.message.from_wire(goodwire, origin=origin)
197 u2 = dns.message.from_text(update_text, origin=origin)
198 self.assertEqual(u1, u2)
199
200 def test_good_explicit_delete_wire(self):
201 name = dns.name.from_text("foo.example")
202 u = dns.message.from_wire(goodwirenone)
203 self.assertEqual(u.update[0].name, name)
204 self.assertEqual(u.update[0].rdtype, dns.rdatatype.A)
205 self.assertEqual(u.update[0].rdclass, dns.rdataclass.IN)
206 self.assertTrue(u.update[0].deleting)
207 self.assertEqual(u.update[0][0].address, "1.2.3.4")
208
209 def test_none_with_rdata_from_wire(self):
210 def bad():
211 dns.message.from_wire(badwirenone)
212
213 self.assertRaises(dns.exception.FormError, bad)
214
215 def test_any_with_rdata_from_wire(self):
216 def bad():
217 dns.message.from_wire(badwireany)
218
219 self.assertRaises(dns.exception.FormError, bad)
220
221 def test_any_any_with_rdata_from_wire(self):
222 def bad():
223 dns.message.from_wire(badwireanyany)
224
225 self.assertRaises(dns.exception.FormError, bad)
226
227 def test_bad_zone_type_from_wire(self):
228 def bad():
229 dns.message.from_wire(badwirezonetype)
230
231 self.assertRaises(dns.exception.FormError, bad)
232
233 def test_bad_zone_class_from_wire(self):
234 def bad():
235 dns.message.from_wire(badwirezoneclass)
236
237 self.assertRaises(dns.exception.FormError, bad)
238
239 def test_bad_zone_multi_from_wire(self):
240 def bad():
241 dns.message.from_wire(badwirezonemulti)
242
243 self.assertRaises(dns.exception.FormError, bad)
244
245 def test_no_zone_section_from_wire(self):
246 def bad():
247 dns.message.from_wire(badwirenozone)
248
249 self.assertRaises(dns.exception.FormError, bad)
250
251 def test_TSIG(self):
252 keyring = dns.tsigkeyring.from_text({"keyname.": "NjHwPsMKjdN++dOfE5iAiQ=="})
253 update = dns.update.Update("example.", keyring=keyring)
254 update.replace("host.example.", 300, "A", "1.2.3.4")
255 wire = update.to_wire()
256 update2 = dns.message.from_wire(wire, keyring)
257 self.assertEqual(update, update2)
258
259 def test_is_response(self):
260 update = dns.message.from_text(update_text)
261 self.assertTrue(isinstance(update, dns.update.UpdateMessage))
262 r = dns.message.make_response(update)
263 self.assertTrue(isinstance(r, dns.update.UpdateMessage))
264 self.assertTrue(update.is_response(r))
265
266 def test_making_UpdateSection(self):
267 self.assertEqual(
268 dns.update.UpdateSection.make(0), dns.update.UpdateSection.make("ZONE")
269 )
270 with self.assertRaises(ValueError):
271 dns.update.UpdateSection.make(99)
272
273 def test_setters(self):
274 u = dns.update.UpdateMessage(id=1)
275 qrrset = dns.rrset.RRset(
276 dns.name.from_text("example"), dns.rdataclass.IN, dns.rdatatype.SOA
277 )
278 rrset = dns.rrset.from_text("foo", 300, "in", "a", "10.0.0.1")
279 u.zone = [qrrset]
280 self.assertEqual(u.sections[0], [qrrset])
281 self.assertEqual(u.sections[1], [])
282 self.assertEqual(u.sections[2], [])
283 self.assertEqual(u.sections[3], [])
284 u.prerequisite = [rrset]
285 self.assertEqual(u.sections[0], [qrrset])
286 self.assertEqual(u.sections[1], [rrset])
287 self.assertEqual(u.sections[2], [])
288 self.assertEqual(u.sections[3], [])
289 u.update = [rrset]
290 self.assertEqual(u.sections[0], [qrrset])
291 self.assertEqual(u.sections[1], [rrset])
292 self.assertEqual(u.sections[2], [rrset])
293 self.assertEqual(u.sections[3], [])
294
295 def test_added_rdataset(self):
296 u = dns.update.UpdateMessage("example.", id=1)
297 rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.1", "10.0.0.2")
298 u.add("foo", rds)
299 expected = dns.message.from_text(added_text)
300 self.assertEqual(u, expected)
301
302 def test_replaced_rdataset(self):
303 u = dns.update.UpdateMessage("example.", id=1)
304 rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.1", "10.0.0.2")
305 u.replace("foo", rds)
306 expected = dns.message.from_text(replaced_text)
307 self.assertEqual(u, expected)
308
309 def test_delete_rdataset(self):
310 u = dns.update.UpdateMessage("example.", id=1)
311 rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.1", "10.0.0.2")
312 u.delete("foo", rds)
313 expected = dns.message.from_text(deleted_text)
314 self.assertEqual(u, expected)
315
316 def test_added_rdata(self):
317 u = dns.update.UpdateMessage("example.", id=1)
318 rd1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.1")
319 rd2 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.2")
320 u.add("foo", 300, rd1)
321 u.add("foo", 300, rd2)
322 expected = dns.message.from_text(added_text)
323 self.assertEqual(u, expected)
324
325 def test_replaced_rdata(self):
326 u = dns.update.UpdateMessage("example.", id=1)
327 rd1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.1")
328 rd2 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.2")
329 u.replace("foo", 300, rd1)
330 u.add("foo", 300, rd2)
331 expected = dns.message.from_text(replaced_text)
332 self.assertEqual(u, expected)
333
334 def test_deleted_rdata(self):
335 u = dns.update.UpdateMessage("example.", id=1)
336 rd1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.1")
337 rd2 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.2")
338 u.delete("foo", rd1)
339 u.delete("foo", rd2)
340 expected = dns.message.from_text(deleted_text)
341 self.assertEqual(u, expected)
342
343
344 if __name__ == "__main__":
345 unittest.main()