Debian Codegraph @

tests/test_edns.py

raw
1 # -*- coding: utf-8
2 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
3
4 # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
5 #
6 # Permission to use, copy, modify, and distribute this software and its
7 # documentation for any purpose with or without fee is hereby granted,
8 # provided that the above copyright notice and this permission notice
9 # appear in all copies.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19 import operator
20 import struct
21 import unittest
22 from io import BytesIO
23
24 import dns.edns
25 import dns.wire
26
27
28 class OptionTestCase(unittest.TestCase):
29 def testGenericOption(self):
30 opt = dns.edns.GenericOption(3, b"data")
31 io = BytesIO()
32 opt.to_wire(io)
33 data = io.getvalue()
34 self.assertEqual(data, b"data")
35 self.assertEqual(dns.edns.option_from_wire(3, data, 0, len(data)), opt)
36 self.assertEqual(str(opt), "Generic 3")
37
38 def testECSOption_prefix_length(self):
39 opt = dns.edns.ECSOption("1.2.255.33", 20)
40 io = BytesIO()
41 opt.to_wire(io)
42 data = io.getvalue()
43 self.assertEqual(data, b"\x00\x01\x14\x00\x01\x02\xf0")
44
45 def testECSOption(self):
46 opt = dns.edns.ECSOption("1.2.3.4", 24)
47 io = BytesIO()
48 opt.to_wire(io)
49 data = io.getvalue()
50 self.assertEqual(data, b"\x00\x01\x18\x00\x01\x02\x03")
51 # default srclen
52 opt = dns.edns.ECSOption("1.2.3.4")
53 io = BytesIO()
54 opt.to_wire(io)
55 data = io.getvalue()
56 self.assertEqual(data, b"\x00\x01\x18\x00\x01\x02\x03")
57 self.assertEqual(opt.to_text(), "ECS 1.2.3.4/24 scope/0")
58
59 def testECSOption25(self):
60 opt = dns.edns.ECSOption("1.2.3.255", 25)
61 io = BytesIO()
62 opt.to_wire(io)
63 data = io.getvalue()
64 self.assertEqual(data, b"\x00\x01\x19\x00\x01\x02\x03\x80")
65
66 opt2 = dns.edns.option_from_wire(dns.edns.ECS, data, 0, len(data))
67 self.assertEqual(opt2.otype, dns.edns.ECS)
68 self.assertEqual(opt2.address, "1.2.3.128")
69 self.assertEqual(opt2.srclen, 25)
70 self.assertEqual(opt2.scopelen, 0)
71
72 def testECSOption_v6(self):
73 opt = dns.edns.ECSOption("2001:4b98::1")
74 io = BytesIO()
75 opt.to_wire(io)
76 data = io.getvalue()
77 self.assertEqual(data, b"\x00\x02\x38\x00\x20\x01\x4b\x98\x00\x00\x00")
78
79 opt2 = dns.edns.option_from_wire(dns.edns.ECS, data, 0, len(data))
80 self.assertEqual(opt2.otype, dns.edns.ECS)
81 self.assertEqual(opt2.address, "2001:4b98::")
82 self.assertEqual(opt2.srclen, 56)
83 self.assertEqual(opt2.scopelen, 0)
84
85 def testECSOption_from_text_valid(self):
86 ecs1 = dns.edns.ECSOption.from_text("1.2.3.4/24/0")
87 self.assertEqual(ecs1, dns.edns.ECSOption("1.2.3.4", 24, 0))
88
89 ecs2 = dns.edns.ECSOption.from_text("1.2.3.4/24")
90 self.assertEqual(ecs2, dns.edns.ECSOption("1.2.3.4", 24, 0))
91
92 ecs3 = dns.edns.ECSOption.from_text("ECS 1.2.3.4/24")
93 self.assertEqual(ecs3, dns.edns.ECSOption("1.2.3.4", 24, 0))
94
95 ecs4 = dns.edns.ECSOption.from_text("ECS 1.2.3.4/24/32")
96 self.assertEqual(ecs4, dns.edns.ECSOption("1.2.3.4", 24, 32))
97
98 ecs5 = dns.edns.ECSOption.from_text("2001:4b98::1/64/56")
99 self.assertEqual(ecs5, dns.edns.ECSOption("2001:4b98::1", 64, 56))
100
101 ecs6 = dns.edns.ECSOption.from_text("2001:4b98::1/64")
102 self.assertEqual(ecs6, dns.edns.ECSOption("2001:4b98::1", 64, 0))
103
104 ecs7 = dns.edns.ECSOption.from_text("ECS 2001:4b98::1/0")
105 self.assertEqual(ecs7, dns.edns.ECSOption("2001:4b98::1", 0, 0))
106
107 ecs8 = dns.edns.ECSOption.from_text("ECS 2001:4b98::1/64/128")
108 self.assertEqual(ecs8, dns.edns.ECSOption("2001:4b98::1", 64, 128))
109
110 def testECSOption_from_text_invalid(self):
111 with self.assertRaises(ValueError):
112 dns.edns.ECSOption.from_text("some random text 1.2.3.4/24/0 24")
113
114 with self.assertRaises(ValueError):
115 dns.edns.ECSOption.from_text("1.2.3.4/twentyfour")
116
117 with self.assertRaises(ValueError):
118 dns.edns.ECSOption.from_text("BOGUS 1.2.3.4/5/6/7")
119
120 with self.assertRaises(ValueError):
121 dns.edns.ECSOption.from_text("1.2.3.4/5/6/7")
122
123 with self.assertRaises(ValueError):
124 dns.edns.ECSOption.from_text("1.2.3.4/24/O") # <-- that's not a zero
125
126 with self.assertRaises(ValueError):
127 dns.edns.ECSOption.from_text("")
128
129 with self.assertRaises(ValueError):
130 dns.edns.ECSOption.from_text("1.2.3.4/2001:4b98::1/24")
131
132 def testECSOption_from_wire_invalid(self):
133 with self.assertRaises(ValueError):
134 opt = dns.edns.option_from_wire(
135 dns.edns.ECS, b"\x00\xff\x18\x00\x01\x02\x03", 0, 7
136 )
137
138 def testEDEOption(self):
139 opt = dns.edns.EDEOption(3)
140 io = BytesIO()
141 opt.to_wire(io)
142 data = io.getvalue()
143 self.assertEqual(data, b"\x00\x03")
144 self.assertEqual(str(opt), "EDE 3 (Stale Answer)")
145 # with text
146 opt = dns.edns.EDEOption(16, "test")
147 io = BytesIO()
148 opt.to_wire(io)
149 data = io.getvalue()
150 self.assertEqual(data, b"\x00\x10test")
151
152 def testEDEOption_invalid(self):
153 with self.assertRaises(ValueError):
154 opt = dns.edns.EDEOption(-1)
155 with self.assertRaises(ValueError):
156 opt = dns.edns.EDEOption(65536)
157 with self.assertRaises(ValueError):
158 opt = dns.edns.EDEOption(0, 0)
159
160 def testEDEOption_from_wire(self):
161 data = b"\x00\01"
162 self.assertEqual(
163 dns.edns.option_from_wire(dns.edns.EDE, data, 0, 2), dns.edns.EDEOption(1)
164 )
165 data = b"\x00\01test"
166 self.assertEqual(
167 dns.edns.option_from_wire(dns.edns.EDE, data, 0, 6),
168 dns.edns.EDEOption(1, "test"),
169 )
170 # utf-8 text MAY be null-terminated
171 data = b"\x00\01test\x00"
172 self.assertEqual(
173 dns.edns.option_from_wire(dns.edns.EDE, data, 0, 7),
174 dns.edns.EDEOption(1, "test"),
175 )
176
177 def test_basic_relations(self):
178 o1 = dns.edns.ECSOption.from_text("1.2.3.0/24/0")
179 o2 = dns.edns.ECSOption.from_text("1.2.4.0/24/0")
180 self.assertTrue(o1 == o1)
181 self.assertTrue(o1 != o2)
182 self.assertTrue(o1 < o2)
183 self.assertTrue(o1 <= o2)
184 self.assertTrue(o2 > o1)
185 self.assertTrue(o2 >= o1)
186 o1 = dns.edns.ECSOption.from_text("1.2.4.0/23/0")
187 o2 = dns.edns.ECSOption.from_text("1.2.4.0/24/0")
188 self.assertTrue(o1 < o2)
189 o1 = dns.edns.ECSOption.from_text("1.2.4.0/24/0")
190 o2 = dns.edns.ECSOption.from_text("1.2.4.0/24/1")
191 self.assertTrue(o1 < o2)
192
193 def test_incompatible_relations(self):
194 o1 = dns.edns.GenericOption(3, b"data")
195 o2 = dns.edns.ECSOption.from_text("1.2.3.5/24/0")
196 for oper in [operator.lt, operator.le, operator.ge, operator.gt]:
197 self.assertRaises(TypeError, lambda: oper(o1, o2))
198 self.assertFalse(o1 == o2)
199 self.assertTrue(o1 != o2)
200 self.assertFalse(o1 == 123)
201 self.assertTrue(o1 != 123)
202
203 def testNSIDOption(self):
204 opt = dns.edns.NSIDOption(b"testing")
205 io = BytesIO()
206 opt.to_wire(io)
207 data = io.getvalue()
208 self.assertEqual(data, b"testing")
209 self.assertEqual(str(opt), "NSID testing")
210 opt = dns.edns.NSIDOption(b"\xfe\xff")
211 io = BytesIO()
212 opt.to_wire(io)
213 data = io.getvalue()
214 self.assertEqual(data, b"\xfe\xff")
215 self.assertEqual(str(opt), "NSID feff")
216 o = dns.edns.option_from_wire(dns.edns.OptionType.NSID, data, 0, len(data))
217 self.assertEqual(o.nsid, b"\xfe\xff")
218
219 def testCookieOption(self):
220 opt = dns.edns.CookieOption(b"12345678", b"")
221 io = BytesIO()
222 opt.to_wire(io)
223 data = io.getvalue()
224 self.assertEqual(data, b"12345678")
225 self.assertEqual(str(opt), "COOKIE 3132333435363738")
226 opt = dns.edns.CookieOption(b"12345678", b"abcdefgh")
227 data = opt.to_wire()
228 self.assertEqual(data, b"12345678abcdefgh")
229 self.assertEqual(str(opt), "COOKIE 31323334353637386162636465666768")
230 # maximal server
231 opt = dns.edns.CookieOption(b"12345678", b"abcdefghabcdefghabcdefghabcdefgh")
232 io = BytesIO()
233 opt.to_wire(io)
234 data = io.getvalue()
235 self.assertEqual(data, b"12345678abcdefghabcdefghabcdefghabcdefgh")
236 # from wire
237 opt2 = dns.edns.option_from_wire(dns.edns.OptionType.COOKIE, data, 0, len(data))
238 self.assertEqual(opt.client, opt2.client)
239 self.assertEqual(opt.server, opt2.server)
240 # client too short
241 with self.assertRaises(ValueError):
242 opt = dns.edns.CookieOption(b"1234567", b"")
243 # client too long
244 with self.assertRaises(ValueError):
245 opt = dns.edns.CookieOption(b"123456789", b"")
246 # server too short
247 with self.assertRaises(ValueError):
248 opt = dns.edns.CookieOption(b"12345678", b"a")
249 # server too long
250 with self.assertRaises(ValueError):
251 opt = dns.edns.CookieOption(
252 b"12345678", b"abcdefghabcdefghabcdefghabcdefghi"
253 )
254
255 def testReportChannelOption(self):
256 agent_domain = dns.name.from_text("agent.example.")
257 expected_wire = b"\x05agent\x07example\x00"
258 opt = dns.edns.ReportChannelOption(agent_domain)
259 io = BytesIO()
260 opt.to_wire(io)
261 data = io.getvalue()
262 self.assertEqual(data, expected_wire)
263 self.assertEqual(str(opt), "REPORTCHANNEL agent.example.")
264 opt2 = dns.edns.option_from_wire(
265 dns.edns.OptionType.REPORTCHANNEL, expected_wire, 0, len(expected_wire)
266 )
267 self.assertEqual(opt2.agent_domain, agent_domain)
268
269 def test_option_registration(self):
270 U32OptionType = 9999
271
272 class U32Option(dns.edns.Option):
273 def __init__(self, value=None):
274 super().__init__(U32OptionType)
275 self.value = value
276
277 def to_wire(self, file=None):
278 data = struct.pack("!I", self.value)
279 if file:
280 file.write(data)
281 else:
282 return data
283
284 @classmethod
285 def from_wire_parser(cls, otype, parser):
286 (value,) = parser.get_struct("!I")
287 return cls(value)
288
289 try:
290 dns.edns.register_type(U32Option, U32OptionType)
291 generic = dns.edns.GenericOption(U32OptionType, b"\x00\x00\x00\x01")
292 wire1 = generic.to_wire()
293 u32 = dns.edns.option_from_wire_parser(
294 U32OptionType, dns.wire.Parser(wire1)
295 )
296 self.assertEqual(u32.value, 1)
297 wire2 = u32.to_wire()
298 self.assertEqual(wire1, wire2)
299 self.assertEqual(u32, generic)
300 finally:
301 dns.edns._type_to_class.pop(U32OptionType, None)
302
303 opt = dns.edns.option_from_wire_parser(9999, dns.wire.Parser(wire1))
304 self.assertEqual(opt, generic)
305
306 def test_to_generic(self):
307 nsid = dns.edns.NSIDOption(b"testing")
308 assert nsid.to_generic().data == b"testing"
309
310 ecs = dns.edns.ECSOption("1.2.3.0", 24)
311 assert ecs.to_generic().data == b"\x00\x01\x18\x00\x01\x02\x03"
312
313 generic = dns.edns.GenericOption(12345, "foo")
314 assert generic.to_generic() is generic