Debian Codegraph @

tests/test_svcb.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 import io
4 import unittest
5
6 import dns.rdata
7 import dns.rdtypes.svcbbase
8 import dns.rrset
9 from dns.tokenizer import Tokenizer
10 from tests.util import here
11
12
13 class SVCBTestCase(unittest.TestCase):
14 def check_valid_inputs(self, inputs):
15 expected = inputs[0]
16 for text in inputs:
17 rr = dns.rdata.from_text("IN", "SVCB", text)
18 new_text = rr.to_text()
19 self.assertEqual(expected, new_text)
20
21 def check_invalid_inputs(self, inputs):
22 for text in inputs:
23 with self.assertRaises((dns.exception.SyntaxError, ValueError)):
24 dns.rdata.from_text("IN", "SVCB", text)
25
26 def test_svcb_general_invalid(self):
27 invalid_inputs = (
28 # Duplicate keys
29 "1 . alpn=h2 alpn=h3",
30 "1 . alpn=h2 key1=h3",
31 # Quoted keys
32 '1 . "alpn=h2"',
33 # Invalid space
34 "1 . alpn= h2",
35 "1 . alpn =h2",
36 "1 . alpn = h2",
37 '1 . alpn= "h2"',
38 "1 . =alpn",
39 )
40 self.check_invalid_inputs(invalid_inputs)
41
42 def test_svcb_mandatory(self):
43 valid_inputs = (
44 '1 . mandatory="alpn,no-default-alpn" alpn="h2" no-default-alpn',
45 "1 . mandatory=alpn,no-default-alpn alpn=h2 no-default-alpn",
46 "1 . mandatory=key1,key2 alpn=h2 no-default-alpn",
47 "1 . mandatory=alpn,no-default-alpn key1=\\002h2 key2",
48 "1 . key0=\\000\\001\\000\\002 alpn=h2 no-default-alpn",
49 "1 . alpn=h2 no-default-alpn mandatory=alpn,no-default-alpn",
50 )
51 self.check_valid_inputs(valid_inputs)
52
53 invalid_inputs = (
54 # empty
55 "1 . mandatory=",
56 "1 . mandatory",
57 # unknown key
58 "1 . mandatory=foo",
59 # key 0
60 "1 . mandatory=key0",
61 "1 . mandatory=key0,alpn",
62 # missing key
63 "1 . mandatory=alpn",
64 # duplicate
65 "1 . mandatory=alpn,alpn alpn=h2",
66 # invalid escaping
67 "1 . mandatory=\\alpn alpn=h2",
68 # empty wire format
69 "1 . key0",
70 "1 . key0=",
71 # 0 in wire format
72 "1 . key0=\\000\\000",
73 # invalid length in wire format
74 "1 . key0=\\000",
75 # out of order in wire format
76 "1 . key0=\\000\\002\\000\\001 alpn=h2 no-default-alpn",
77 # leading zeros
78 "1 . mandatory=key1,key002 alpn=h2 no-default-alpn",
79 )
80 self.check_invalid_inputs(invalid_inputs)
81
82 def test_svcb_alpn(self):
83 valid_inputs_two_items = (
84 '1 . alpn="h2,h3"',
85 "1 . alpn=h2,h3",
86 "1 . alpn=h\\050,h3",
87 '1 . alpn="h\\050,h3"',
88 "1 . alpn=\\h2,h3",
89 '1 . alpn="h2\\,h3"',
90 "1 . alpn=h2\\,h3",
91 "1 . alpn=h2\\044h3",
92 "1 . key1=\\002h2\\002h3",
93 )
94 self.check_valid_inputs(valid_inputs_two_items)
95
96 valid_inputs_one_item = (
97 '1 . alpn="h2\\\\,h3"',
98 "1 . alpn=h2\\\\,h3",
99 "1 . alpn=h2\\092\\044h3",
100 "1 . key1=\\005h2,h3",
101 )
102 self.check_valid_inputs(valid_inputs_one_item)
103
104 invalid_inputs = (
105 "1 . alpn",
106 "1 . alpn=",
107 "1 . alpn=h2,,h3",
108 "1 . alpn=01234567890abcdef01234567890abcdef01234567890abcdef"
109 "01234567890abcdef01234567890abcdef01234567890abcdef"
110 "01234567890abcdef01234567890abcdef01234567890abcdef"
111 "01234567890abcdef01234567890abcdef01234567890abcdef"
112 "01234567890abcdef01234567890abcdef01234567890abcdef"
113 "01234567890abcdef",
114 '1 . alpn=",h2,h3"',
115 '1 . alpn="h2,h3,"',
116 "1 . key1",
117 "1 . key1=",
118 "1 . key1=\\000",
119 "1 . key1=\\002x",
120 )
121 self.check_invalid_inputs(invalid_inputs)
122
123 def test_svcb_no_default_alpn(self):
124 valid_inputs = (
125 '1 . alpn="h2" no-default-alpn',
126 '1 . alpn="h2" no-default-alpn=""',
127 '1 . alpn="h2" key2',
128 '1 . alpn="h2" key2=""',
129 )
130 self.check_valid_inputs(valid_inputs)
131
132 invalid_inputs = (
133 "1 . no-default-alpn",
134 '1 . no-default-alpn=""',
135 "1 . key2",
136 '1 . key2=""',
137 "1 . alpn=h2 no-default-alpn=foo",
138 "1 . alpn=h2 no-default-alpn=",
139 "1 . alpn=h2 key2=foo",
140 "1 . alpn=h2 key2=",
141 )
142 self.check_invalid_inputs(invalid_inputs)
143
144 def test_svcb_port(self):
145 valid_inputs = (
146 '1 . port="53"',
147 "1 . port=53",
148 "1 . key3=\\000\\053",
149 )
150 self.check_valid_inputs(valid_inputs)
151
152 invalid_inputs = (
153 "1 . port",
154 "1 . port=",
155 "1 . port=53x",
156 "1 . port=x53",
157 "1 . port=53,54",
158 "1 . port=53\\,54",
159 "1 . port=65536",
160 "1 . key3",
161 "1 . key3=",
162 "1 . key3=\\000",
163 )
164 self.check_invalid_inputs(invalid_inputs)
165
166 def test_svcb_ipv4hint(self):
167 valid_inputs = (
168 '1 . ipv4hint="0.0.0.0,1.1.1.1"',
169 "1 . ipv4hint=0.0.0.0,1.1.1.1",
170 "1 . key4=\\000\\000\\000\\000\\001\\001\\001\\001",
171 )
172 self.check_valid_inputs(valid_inputs)
173
174 invalid_inputs = (
175 "1 . ipv4hint",
176 "1 . ipv4hint=",
177 "1 . ipv4hint=1234",
178 "1 . ipv4hint=1\\.2.3.4",
179 "1 . ipv4hint=1.2.3.4\\,2.3.4.5",
180 "1 . key4=",
181 "1 . key4=123",
182 )
183 self.check_invalid_inputs(invalid_inputs)
184
185 def test_svcb_ech(self):
186 valid_inputs = (
187 '1 . ech="Zm9vMA=="',
188 "1 . ech=Zm9vMA==",
189 "1 . key5=foo0",
190 "1 . key5=\\102\\111\\111\\048",
191 )
192 self.check_valid_inputs(valid_inputs)
193
194 invalid_inputs = (
195 "1 . ech",
196 "1 . ech=",
197 "1 . ech=Zm9vMA",
198 "1 . ech=\\090m9vMA==",
199 "1 . key5",
200 "1 . key5=",
201 )
202 self.check_invalid_inputs(invalid_inputs)
203
204 def test_svcb_ipv6hint(self):
205 valid_inputs = (
206 '1 . ipv6hint="::4,1::"',
207 "1 . ipv6hint=::4,1::",
208 "1 . key6=\\000\\000\\000\\000\\000\\000\\000\\000"
209 "\\000\\000\\000\\000\\000\\000\\000\\004"
210 "\\000\\001\\000\\000\\000\\000\\000\\000"
211 "\\000\\000\\000\\000\\000\\000\\000\\000",
212 )
213 self.check_valid_inputs(valid_inputs)
214
215 invalid_inputs = (
216 "1 . ipv6hint",
217 "1 . ipv6hint=",
218 "1 . ipv6hint=1234",
219 "1 . ipv6hint=1\\::2",
220 "1 . ipv6hint=::1\\,::2",
221 "1 . ipv6hint",
222 "1 . key6",
223 "1 . key6=",
224 "1 . key6=123",
225 )
226 self.check_invalid_inputs(invalid_inputs)
227
228 def test_svcb_unknown(self):
229 valid_inputs_one_key = (
230 '1 . key23="key45"',
231 "1 . key23=key45",
232 "1 . key23=key\\052\\053",
233 '1 . key23="key\\052\\053"',
234 "1 . key23=\\107\\101\\121\\052\\053",
235 )
236 self.check_valid_inputs(valid_inputs_one_key)
237
238 valid_inputs_one_key_empty = (
239 "1 . key23",
240 '1 . key23=""',
241 )
242 self.check_valid_inputs(valid_inputs_one_key_empty)
243
244 invalid_inputs_one_key = (
245 "1 . key65536=foo",
246 "1 . key24= key48",
247 )
248 self.check_invalid_inputs(invalid_inputs_one_key)
249
250 valid_inputs_two_keys = (
251 "1 . key24 key48",
252 '1 . key24="" key48',
253 )
254 self.check_valid_inputs(valid_inputs_two_keys)
255
256 def test_svcb_wire(self):
257 valid_inputs = (
258 '1 . mandatory="alpn,port" alpn="h2" port="257"',
259 "\\# 24 0001 00 0000000400010003 00010003026832 000300020101",
260 )
261 self.check_valid_inputs(valid_inputs)
262
263 everything = (
264 '100 foo.com. mandatory="alpn,port" alpn="h2,h3" '
265 ' no-default-alpn port="12345" ech="abcd" '
266 " ipv4hint=1.2.3.4,4.3.2.1 ipv6hint=1::2,3::4"
267 ' key12345="foo"'
268 )
269 rr = dns.rdata.from_text("IN", "SVCB", everything)
270 rr2 = dns.rdata.from_text("IN", "SVCB", rr.to_generic().to_text())
271 self.assertEqual(rr, rr2)
272
273 invalid_inputs = (
274 # As above, but the keys are out of order.
275 "\\# 24 0001 00 0000000400010003 000300020101 00010003026832",
276 # As above, but the mandatory keys don't match
277 "\\# 24 0001 00 0000000400010002 00010003026832 000300020101",
278 "\\# 24 0001 00 0000000400010004 00010003026832 000300020101",
279 # Alias form shouldn't have parameters.
280 "\\# 08 0000 000300020101",
281 # no-default-alpn requires alpn
282 "\\# 07 0001 00 00020000",
283 )
284 self.check_invalid_inputs(invalid_inputs)
285
286 def test_misc_escape(self):
287 rdata = dns.rdata.from_text("in", "svcb", "1 . alpn=\\010\\010")
288 expected = '1 . alpn="\\\\010\\\\010"'
289 self.assertEqual(rdata.to_text(), expected)
290 with self.assertRaises(dns.exception.SyntaxError):
291 dns.rdata.from_text("in", "svcb", "1 . alpn=\\0")
292 with self.assertRaises(dns.exception.SyntaxError):
293 dns.rdata.from_text("in", "svcb", "1 . alpn=\\00")
294 with self.assertRaises(dns.exception.SyntaxError):
295 dns.rdata.from_text("in", "svcb", "1 . alpn=\\00q")
296 with self.assertRaises(dns.exception.SyntaxError):
297 dns.rdata.from_text("in", "svcb", "1 . alpn=\\256")
298 # This doesn't usually get exercised, so we do it directly.
299 gp = dns.rdtypes.svcbbase.GenericParam.from_value("\\001\\002")
300 expected = '"\\001\\002"'
301 self.assertEqual(gp.to_text(), expected)
302
303 def test_svcb_spec_test_vectors(self):
304 text_file = here("svcb_test_vectors.text")
305 text_tokenizer = Tokenizer(
306 open(text_file, encoding="utf-8"), filename=text_file
307 )
308 generic_file = here("svcb_test_vectors.generic")
309 generic_tokenizer = Tokenizer(
310 open(generic_file, encoding="utf-8"), filename=generic_file
311 )
312
313 while True:
314 while True:
315 text_token = text_tokenizer.get()
316 if text_token.is_eol():
317 continue
318 break
319 while True:
320 generic_token = generic_tokenizer.get()
321 if generic_token.is_eol():
322 continue
323 break
324 self.assertEqual(text_token.ttype, generic_token.ttype)
325 if text_token.is_eof():
326 break
327 self.assertTrue(text_token.is_identifier)
328 text_tokenizer.unget(text_token)
329 generic_tokenizer.unget(generic_token)
330 text_rdata = dns.rdata.from_text("IN", "SVCB", text_tokenizer)
331 generic_rdata = dns.rdata.from_text("IN", "SVCB", generic_tokenizer)
332 self.assertEqual(text_rdata, generic_rdata)
333
334 def test_svcb_spec_failure_cases(self):
335 failure_cases = (
336 # This example has multiple instances of the same SvcParamKey
337 "1 foo.example.com. key123=abc key123=def",
338 # In the next examples the SvcParamKeys are missing their values.
339 "1 foo.example.com. mandatory",
340 "1 foo.example.com. alpn",
341 "1 foo.example.com. port",
342 "1 foo.example.com. ipv4hint",
343 "1 foo.example.com. ipv6hint",
344 # The "no-default-alpn" SvcParamKey value MUST be empty (Section 6.1).
345 "1 foo.example.com. no-default-alpn=abc",
346 # In this record a mandatory SvcParam is missing (Section 7).
347 "1 foo.example.com. mandatory=key123",
348 # The "mandatory" SvcParamKey MUST not be included in mandatory list
349 # (Section 7).
350 "1 foo.example.com. mandatory=mandatory",
351 # Here there are multiple instances of the same SvcParamKey in the
352 # mandatory list (Section 7).
353 "1 foo.example.com. mandatory=key123,key123 key123=abc",
354 )
355 self.check_invalid_inputs(failure_cases)
356
357 def test_alias_mode(self):
358 rd = dns.rdata.from_text("in", "svcb", "0 .")
359 self.assertEqual(len(rd.params), 0)
360 self.assertEqual(rd.target, dns.name.root)
361 self.assertEqual(rd.to_text(), "0 .")
362 rd = dns.rdata.from_text("in", "svcb", "0 elsewhere.")
363 self.assertEqual(rd.target, dns.name.from_text("elsewhere."))
364 self.assertEqual(len(rd.params), 0)
365 # provoke 'parameters in AliasMode' from text.
366 with self.assertRaises(dns.exception.SyntaxError):
367 dns.rdata.from_text("in", "svcb", "0 elsewhere. alpn=h2")
368 # provoke 'parameters in AliasMode' from wire too.
369 wire = bytes.fromhex("0000000000000400010003")
370 with self.assertRaises(dns.exception.FormError):
371 dns.rdata.from_wire("in", "svcb", wire, 0, len(wire))
372
373 def test_immutability(self):
374 alpn = dns.rdtypes.svcbbase.ALPNParam.from_value(["h2", "h3"])
375 with self.assertRaises(TypeError):
376 alpn.ids[0] = "foo"
377 with self.assertRaises(TypeError):
378 del alpn.ids[0]
379 with self.assertRaises(TypeError):
380 alpn.ids = "foo"
381 with self.assertRaises(TypeError):
382 del alpn.ids
383
384 def test_alias_not_compressed(self):
385 rrs = dns.rrset.from_text("elsewhere.", 300, "in", "svcb", "0 elseWhere.")
386 output = io.BytesIO()
387 compress = {}
388 rrs.to_wire(output, compress)
389 wire = output.getvalue()
390 # Just one of these assertions is enough, but we do both to show
391 # the bug we're checking is fixed.
392 assert not wire.endswith(b"\xc0\x00")
393 assert wire.endswith(b"\x09elseWhere\x00")