##// END OF EJS Templates
tests: avoid using a list comprehension to fill a list with fixed values...
Matt Harbison -
r44448:6dbb18e1 default
parent child Browse files
Show More
@@ -1,1275 +1,1275 b''
1 1 from __future__ import absolute_import
2 2
3 3 import os
4 4 import sys
5 5 import unittest
6 6
7 7 # TODO migrate to canned cbor test strings and stop using thirdparty.cbor
8 8 tpp = os.path.normpath(
9 9 os.path.join(os.path.dirname(__file__), '..', 'mercurial', 'thirdparty')
10 10 )
11 11 if not os.path.exists(tpp):
12 12 # skip, not in a repo
13 13 sys.exit(80)
14 14 sys.path[0:0] = [tpp]
15 15 import cbor
16 16
17 17 del sys.path[0]
18 18
19 19 from mercurial.utils import cborutil
20 20
21 21
22 22 class TestCase(unittest.TestCase):
23 23 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
24 24 # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
25 25 # the regex version.
26 26 assertRaisesRegex = ( # camelcase-required
27 27 unittest.TestCase.assertRaisesRegexp
28 28 )
29 29
30 30
31 31 def loadit(it):
32 32 return cbor.loads(b''.join(it))
33 33
34 34
35 35 class BytestringTests(TestCase):
36 36 def testsimple(self):
37 37 self.assertEqual(
38 38 list(cborutil.streamencode(b'foobar')), [b'\x46', b'foobar']
39 39 )
40 40
41 41 self.assertEqual(loadit(cborutil.streamencode(b'foobar')), b'foobar')
42 42
43 43 self.assertEqual(cborutil.decodeall(b'\x46foobar'), [b'foobar'])
44 44
45 45 self.assertEqual(
46 46 cborutil.decodeall(b'\x46foobar\x45fizbi'), [b'foobar', b'fizbi']
47 47 )
48 48
49 49 def testlong(self):
50 50 source = b'x' * 1048576
51 51
52 52 self.assertEqual(loadit(cborutil.streamencode(source)), source)
53 53
54 54 encoded = b''.join(cborutil.streamencode(source))
55 55 self.assertEqual(cborutil.decodeall(encoded), [source])
56 56
57 57 def testfromiter(self):
58 58 # This is the example from RFC 7049 Section 2.2.2.
59 59 source = [b'\xaa\xbb\xcc\xdd', b'\xee\xff\x99']
60 60
61 61 self.assertEqual(
62 62 list(cborutil.streamencodebytestringfromiter(source)),
63 63 [
64 64 b'\x5f',
65 65 b'\x44',
66 66 b'\xaa\xbb\xcc\xdd',
67 67 b'\x43',
68 68 b'\xee\xff\x99',
69 69 b'\xff',
70 70 ],
71 71 )
72 72
73 73 self.assertEqual(
74 74 loadit(cborutil.streamencodebytestringfromiter(source)),
75 75 b''.join(source),
76 76 )
77 77
78 78 self.assertEqual(
79 79 cborutil.decodeall(
80 80 b'\x5f\x44\xaa\xbb\xcc\xdd' b'\x43\xee\xff\x99\xff'
81 81 ),
82 82 [b'\xaa\xbb\xcc\xdd', b'\xee\xff\x99', b''],
83 83 )
84 84
85 85 for i, chunk in enumerate(
86 86 cborutil.decodeall(
87 87 b'\x5f\x44\xaa\xbb\xcc\xdd' b'\x43\xee\xff\x99\xff'
88 88 )
89 89 ):
90 90 self.assertIsInstance(chunk, cborutil.bytestringchunk)
91 91
92 92 if i == 0:
93 93 self.assertTrue(chunk.isfirst)
94 94 else:
95 95 self.assertFalse(chunk.isfirst)
96 96
97 97 if i == 2:
98 98 self.assertTrue(chunk.islast)
99 99 else:
100 100 self.assertFalse(chunk.islast)
101 101
102 102 def testfromiterlarge(self):
103 103 source = [b'a' * 16, b'b' * 128, b'c' * 1024, b'd' * 1048576]
104 104
105 105 self.assertEqual(
106 106 loadit(cborutil.streamencodebytestringfromiter(source)),
107 107 b''.join(source),
108 108 )
109 109
110 110 def testindefinite(self):
111 111 source = b'\x00\x01\x02\x03' + b'\xff' * 16384
112 112
113 113 it = cborutil.streamencodeindefinitebytestring(source, chunksize=2)
114 114
115 115 self.assertEqual(next(it), b'\x5f')
116 116 self.assertEqual(next(it), b'\x42')
117 117 self.assertEqual(next(it), b'\x00\x01')
118 118 self.assertEqual(next(it), b'\x42')
119 119 self.assertEqual(next(it), b'\x02\x03')
120 120 self.assertEqual(next(it), b'\x42')
121 121 self.assertEqual(next(it), b'\xff\xff')
122 122
123 123 dest = b''.join(
124 124 cborutil.streamencodeindefinitebytestring(source, chunksize=42)
125 125 )
126 126 self.assertEqual(cbor.loads(dest), source)
127 127
128 128 self.assertEqual(b''.join(cborutil.decodeall(dest)), source)
129 129
130 130 for chunk in cborutil.decodeall(dest):
131 131 self.assertIsInstance(chunk, cborutil.bytestringchunk)
132 132 self.assertIn(len(chunk), (0, 8, 42))
133 133
134 134 encoded = b'\x5f\xff'
135 135 b = cborutil.decodeall(encoded)
136 136 self.assertEqual(b, [b''])
137 137 self.assertTrue(b[0].isfirst)
138 138 self.assertTrue(b[0].islast)
139 139
140 140 def testdecodevariouslengths(self):
141 141 for i in (0, 1, 22, 23, 24, 25, 254, 255, 256, 65534, 65535, 65536):
142 142 source = b'x' * i
143 143 encoded = b''.join(cborutil.streamencode(source))
144 144
145 145 if len(source) < 24:
146 146 hlen = 1
147 147 elif len(source) < 256:
148 148 hlen = 2
149 149 elif len(source) < 65536:
150 150 hlen = 3
151 151 elif len(source) < 1048576:
152 152 hlen = 5
153 153
154 154 self.assertEqual(
155 155 cborutil.decodeitem(encoded),
156 156 (True, source, hlen + len(source), cborutil.SPECIAL_NONE),
157 157 )
158 158
159 159 def testpartialdecode(self):
160 160 encoded = b''.join(cborutil.streamencode(b'foobar'))
161 161
162 162 self.assertEqual(
163 163 cborutil.decodeitem(encoded[0:1]),
164 164 (False, None, -6, cborutil.SPECIAL_NONE),
165 165 )
166 166 self.assertEqual(
167 167 cborutil.decodeitem(encoded[0:2]),
168 168 (False, None, -5, cborutil.SPECIAL_NONE),
169 169 )
170 170 self.assertEqual(
171 171 cborutil.decodeitem(encoded[0:3]),
172 172 (False, None, -4, cborutil.SPECIAL_NONE),
173 173 )
174 174 self.assertEqual(
175 175 cborutil.decodeitem(encoded[0:4]),
176 176 (False, None, -3, cborutil.SPECIAL_NONE),
177 177 )
178 178 self.assertEqual(
179 179 cborutil.decodeitem(encoded[0:5]),
180 180 (False, None, -2, cborutil.SPECIAL_NONE),
181 181 )
182 182 self.assertEqual(
183 183 cborutil.decodeitem(encoded[0:6]),
184 184 (False, None, -1, cborutil.SPECIAL_NONE),
185 185 )
186 186 self.assertEqual(
187 187 cborutil.decodeitem(encoded[0:7]),
188 188 (True, b'foobar', 7, cborutil.SPECIAL_NONE),
189 189 )
190 190
191 191 def testpartialdecodevariouslengths(self):
192 192 lens = [
193 193 2,
194 194 3,
195 195 10,
196 196 23,
197 197 24,
198 198 25,
199 199 31,
200 200 100,
201 201 254,
202 202 255,
203 203 256,
204 204 257,
205 205 16384,
206 206 65534,
207 207 65535,
208 208 65536,
209 209 65537,
210 210 131071,
211 211 131072,
212 212 131073,
213 213 1048575,
214 214 1048576,
215 215 1048577,
216 216 ]
217 217
218 218 for size in lens:
219 219 if size < 24:
220 220 hlen = 1
221 221 elif size < 2 ** 8:
222 222 hlen = 2
223 223 elif size < 2 ** 16:
224 224 hlen = 3
225 225 elif size < 2 ** 32:
226 226 hlen = 5
227 227 else:
228 228 assert False
229 229
230 230 source = b'x' * size
231 231 encoded = b''.join(cborutil.streamencode(source))
232 232
233 233 res = cborutil.decodeitem(encoded[0:1])
234 234
235 235 if hlen > 1:
236 236 self.assertEqual(
237 237 res, (False, None, -(hlen - 1), cborutil.SPECIAL_NONE)
238 238 )
239 239 else:
240 240 self.assertEqual(
241 241 res,
242 242 (False, None, -(size + hlen - 1), cborutil.SPECIAL_NONE),
243 243 )
244 244
245 245 # Decoding partial header reports remaining header size.
246 246 for i in range(hlen - 1):
247 247 self.assertEqual(
248 248 cborutil.decodeitem(encoded[0 : i + 1]),
249 249 (False, None, -(hlen - i - 1), cborutil.SPECIAL_NONE),
250 250 )
251 251
252 252 # Decoding complete header reports item size.
253 253 self.assertEqual(
254 254 cborutil.decodeitem(encoded[0:hlen]),
255 255 (False, None, -size, cborutil.SPECIAL_NONE),
256 256 )
257 257
258 258 # Decoding single byte after header reports item size - 1
259 259 self.assertEqual(
260 260 cborutil.decodeitem(encoded[0 : hlen + 1]),
261 261 (False, None, -(size - 1), cborutil.SPECIAL_NONE),
262 262 )
263 263
264 264 # Decoding all but the last byte reports -1 needed.
265 265 self.assertEqual(
266 266 cborutil.decodeitem(encoded[0 : hlen + size - 1]),
267 267 (False, None, -1, cborutil.SPECIAL_NONE),
268 268 )
269 269
270 270 # Decoding last byte retrieves value.
271 271 self.assertEqual(
272 272 cborutil.decodeitem(encoded[0 : hlen + size]),
273 273 (True, source, hlen + size, cborutil.SPECIAL_NONE),
274 274 )
275 275
276 276 def testindefinitepartialdecode(self):
277 277 encoded = b''.join(
278 278 cborutil.streamencodebytestringfromiter([b'foobar', b'biz'])
279 279 )
280 280
281 281 # First item should be begin of bytestring special.
282 282 self.assertEqual(
283 283 cborutil.decodeitem(encoded[0:1]),
284 284 (True, None, 1, cborutil.SPECIAL_START_INDEFINITE_BYTESTRING),
285 285 )
286 286
287 287 # Second item should be the first chunk. But only available when
288 288 # we give it 7 bytes (1 byte header + 6 byte chunk).
289 289 self.assertEqual(
290 290 cborutil.decodeitem(encoded[1:2]),
291 291 (False, None, -6, cborutil.SPECIAL_NONE),
292 292 )
293 293 self.assertEqual(
294 294 cborutil.decodeitem(encoded[1:3]),
295 295 (False, None, -5, cborutil.SPECIAL_NONE),
296 296 )
297 297 self.assertEqual(
298 298 cborutil.decodeitem(encoded[1:4]),
299 299 (False, None, -4, cborutil.SPECIAL_NONE),
300 300 )
301 301 self.assertEqual(
302 302 cborutil.decodeitem(encoded[1:5]),
303 303 (False, None, -3, cborutil.SPECIAL_NONE),
304 304 )
305 305 self.assertEqual(
306 306 cborutil.decodeitem(encoded[1:6]),
307 307 (False, None, -2, cborutil.SPECIAL_NONE),
308 308 )
309 309 self.assertEqual(
310 310 cborutil.decodeitem(encoded[1:7]),
311 311 (False, None, -1, cborutil.SPECIAL_NONE),
312 312 )
313 313
314 314 self.assertEqual(
315 315 cborutil.decodeitem(encoded[1:8]),
316 316 (True, b'foobar', 7, cborutil.SPECIAL_NONE),
317 317 )
318 318
319 319 # Third item should be second chunk. But only available when
320 320 # we give it 4 bytes (1 byte header + 3 byte chunk).
321 321 self.assertEqual(
322 322 cborutil.decodeitem(encoded[8:9]),
323 323 (False, None, -3, cborutil.SPECIAL_NONE),
324 324 )
325 325 self.assertEqual(
326 326 cborutil.decodeitem(encoded[8:10]),
327 327 (False, None, -2, cborutil.SPECIAL_NONE),
328 328 )
329 329 self.assertEqual(
330 330 cborutil.decodeitem(encoded[8:11]),
331 331 (False, None, -1, cborutil.SPECIAL_NONE),
332 332 )
333 333
334 334 self.assertEqual(
335 335 cborutil.decodeitem(encoded[8:12]),
336 336 (True, b'biz', 4, cborutil.SPECIAL_NONE),
337 337 )
338 338
339 339 # Fourth item should be end of indefinite stream marker.
340 340 self.assertEqual(
341 341 cborutil.decodeitem(encoded[12:13]),
342 342 (True, None, 1, cborutil.SPECIAL_INDEFINITE_BREAK),
343 343 )
344 344
345 345 # Now test the behavior when going through the decoder.
346 346
347 347 self.assertEqual(
348 348 cborutil.sansiodecoder().decode(encoded[0:1]), (False, 1, 0)
349 349 )
350 350 self.assertEqual(
351 351 cborutil.sansiodecoder().decode(encoded[0:2]), (False, 1, 6)
352 352 )
353 353 self.assertEqual(
354 354 cborutil.sansiodecoder().decode(encoded[0:3]), (False, 1, 5)
355 355 )
356 356 self.assertEqual(
357 357 cborutil.sansiodecoder().decode(encoded[0:4]), (False, 1, 4)
358 358 )
359 359 self.assertEqual(
360 360 cborutil.sansiodecoder().decode(encoded[0:5]), (False, 1, 3)
361 361 )
362 362 self.assertEqual(
363 363 cborutil.sansiodecoder().decode(encoded[0:6]), (False, 1, 2)
364 364 )
365 365 self.assertEqual(
366 366 cborutil.sansiodecoder().decode(encoded[0:7]), (False, 1, 1)
367 367 )
368 368 self.assertEqual(
369 369 cborutil.sansiodecoder().decode(encoded[0:8]), (True, 8, 0)
370 370 )
371 371
372 372 self.assertEqual(
373 373 cborutil.sansiodecoder().decode(encoded[0:9]), (True, 8, 3)
374 374 )
375 375 self.assertEqual(
376 376 cborutil.sansiodecoder().decode(encoded[0:10]), (True, 8, 2)
377 377 )
378 378 self.assertEqual(
379 379 cborutil.sansiodecoder().decode(encoded[0:11]), (True, 8, 1)
380 380 )
381 381 self.assertEqual(
382 382 cborutil.sansiodecoder().decode(encoded[0:12]), (True, 12, 0)
383 383 )
384 384
385 385 self.assertEqual(
386 386 cborutil.sansiodecoder().decode(encoded[0:13]), (True, 13, 0)
387 387 )
388 388
389 389 decoder = cborutil.sansiodecoder()
390 390 decoder.decode(encoded[0:8])
391 391 values = decoder.getavailable()
392 392 self.assertEqual(values, [b'foobar'])
393 393 self.assertTrue(values[0].isfirst)
394 394 self.assertFalse(values[0].islast)
395 395
396 396 self.assertEqual(decoder.decode(encoded[8:12]), (True, 4, 0))
397 397 values = decoder.getavailable()
398 398 self.assertEqual(values, [b'biz'])
399 399 self.assertFalse(values[0].isfirst)
400 400 self.assertFalse(values[0].islast)
401 401
402 402 self.assertEqual(decoder.decode(encoded[12:]), (True, 1, 0))
403 403 values = decoder.getavailable()
404 404 self.assertEqual(values, [b''])
405 405 self.assertFalse(values[0].isfirst)
406 406 self.assertTrue(values[0].islast)
407 407
408 408
409 409 class StringTests(TestCase):
410 410 def testdecodeforbidden(self):
411 411 encoded = b'\x63foo'
412 412 with self.assertRaisesRegex(
413 413 cborutil.CBORDecodeError, 'string major type not supported'
414 414 ):
415 415 cborutil.decodeall(encoded)
416 416
417 417
418 418 class IntTests(TestCase):
419 419 def testsmall(self):
420 420 self.assertEqual(list(cborutil.streamencode(0)), [b'\x00'])
421 421 self.assertEqual(cborutil.decodeall(b'\x00'), [0])
422 422
423 423 self.assertEqual(list(cborutil.streamencode(1)), [b'\x01'])
424 424 self.assertEqual(cborutil.decodeall(b'\x01'), [1])
425 425
426 426 self.assertEqual(list(cborutil.streamencode(2)), [b'\x02'])
427 427 self.assertEqual(cborutil.decodeall(b'\x02'), [2])
428 428
429 429 self.assertEqual(list(cborutil.streamencode(3)), [b'\x03'])
430 430 self.assertEqual(cborutil.decodeall(b'\x03'), [3])
431 431
432 432 self.assertEqual(list(cborutil.streamencode(4)), [b'\x04'])
433 433 self.assertEqual(cborutil.decodeall(b'\x04'), [4])
434 434
435 435 # Multiple value decode works.
436 436 self.assertEqual(
437 437 cborutil.decodeall(b'\x00\x01\x02\x03\x04'), [0, 1, 2, 3, 4]
438 438 )
439 439
440 440 def testnegativesmall(self):
441 441 self.assertEqual(list(cborutil.streamencode(-1)), [b'\x20'])
442 442 self.assertEqual(cborutil.decodeall(b'\x20'), [-1])
443 443
444 444 self.assertEqual(list(cborutil.streamencode(-2)), [b'\x21'])
445 445 self.assertEqual(cborutil.decodeall(b'\x21'), [-2])
446 446
447 447 self.assertEqual(list(cborutil.streamencode(-3)), [b'\x22'])
448 448 self.assertEqual(cborutil.decodeall(b'\x22'), [-3])
449 449
450 450 self.assertEqual(list(cborutil.streamencode(-4)), [b'\x23'])
451 451 self.assertEqual(cborutil.decodeall(b'\x23'), [-4])
452 452
453 453 self.assertEqual(list(cborutil.streamencode(-5)), [b'\x24'])
454 454 self.assertEqual(cborutil.decodeall(b'\x24'), [-5])
455 455
456 456 # Multiple value decode works.
457 457 self.assertEqual(
458 458 cborutil.decodeall(b'\x20\x21\x22\x23\x24'), [-1, -2, -3, -4, -5]
459 459 )
460 460
461 461 def testrange(self):
462 462 for i in range(-70000, 70000, 10):
463 463 encoded = b''.join(cborutil.streamencode(i))
464 464
465 465 self.assertEqual(encoded, cbor.dumps(i))
466 466 self.assertEqual(cborutil.decodeall(encoded), [i])
467 467
468 468 def testdecodepartialubyte(self):
469 469 encoded = b''.join(cborutil.streamencode(250))
470 470
471 471 self.assertEqual(
472 472 cborutil.decodeitem(encoded[0:1]),
473 473 (False, None, -1, cborutil.SPECIAL_NONE),
474 474 )
475 475 self.assertEqual(
476 476 cborutil.decodeitem(encoded[0:2]),
477 477 (True, 250, 2, cborutil.SPECIAL_NONE),
478 478 )
479 479
480 480 def testdecodepartialbyte(self):
481 481 encoded = b''.join(cborutil.streamencode(-42))
482 482 self.assertEqual(
483 483 cborutil.decodeitem(encoded[0:1]),
484 484 (False, None, -1, cborutil.SPECIAL_NONE),
485 485 )
486 486 self.assertEqual(
487 487 cborutil.decodeitem(encoded[0:2]),
488 488 (True, -42, 2, cborutil.SPECIAL_NONE),
489 489 )
490 490
491 491 def testdecodepartialushort(self):
492 492 encoded = b''.join(cborutil.streamencode(2 ** 15))
493 493
494 494 self.assertEqual(
495 495 cborutil.decodeitem(encoded[0:1]),
496 496 (False, None, -2, cborutil.SPECIAL_NONE),
497 497 )
498 498 self.assertEqual(
499 499 cborutil.decodeitem(encoded[0:2]),
500 500 (False, None, -1, cborutil.SPECIAL_NONE),
501 501 )
502 502 self.assertEqual(
503 503 cborutil.decodeitem(encoded[0:5]),
504 504 (True, 2 ** 15, 3, cborutil.SPECIAL_NONE),
505 505 )
506 506
507 507 def testdecodepartialshort(self):
508 508 encoded = b''.join(cborutil.streamencode(-1024))
509 509
510 510 self.assertEqual(
511 511 cborutil.decodeitem(encoded[0:1]),
512 512 (False, None, -2, cborutil.SPECIAL_NONE),
513 513 )
514 514 self.assertEqual(
515 515 cborutil.decodeitem(encoded[0:2]),
516 516 (False, None, -1, cborutil.SPECIAL_NONE),
517 517 )
518 518 self.assertEqual(
519 519 cborutil.decodeitem(encoded[0:3]),
520 520 (True, -1024, 3, cborutil.SPECIAL_NONE),
521 521 )
522 522
523 523 def testdecodepartialulong(self):
524 524 encoded = b''.join(cborutil.streamencode(2 ** 28))
525 525
526 526 self.assertEqual(
527 527 cborutil.decodeitem(encoded[0:1]),
528 528 (False, None, -4, cborutil.SPECIAL_NONE),
529 529 )
530 530 self.assertEqual(
531 531 cborutil.decodeitem(encoded[0:2]),
532 532 (False, None, -3, cborutil.SPECIAL_NONE),
533 533 )
534 534 self.assertEqual(
535 535 cborutil.decodeitem(encoded[0:3]),
536 536 (False, None, -2, cborutil.SPECIAL_NONE),
537 537 )
538 538 self.assertEqual(
539 539 cborutil.decodeitem(encoded[0:4]),
540 540 (False, None, -1, cborutil.SPECIAL_NONE),
541 541 )
542 542 self.assertEqual(
543 543 cborutil.decodeitem(encoded[0:5]),
544 544 (True, 2 ** 28, 5, cborutil.SPECIAL_NONE),
545 545 )
546 546
547 547 def testdecodepartiallong(self):
548 548 encoded = b''.join(cborutil.streamencode(-1048580))
549 549
550 550 self.assertEqual(
551 551 cborutil.decodeitem(encoded[0:1]),
552 552 (False, None, -4, cborutil.SPECIAL_NONE),
553 553 )
554 554 self.assertEqual(
555 555 cborutil.decodeitem(encoded[0:2]),
556 556 (False, None, -3, cborutil.SPECIAL_NONE),
557 557 )
558 558 self.assertEqual(
559 559 cborutil.decodeitem(encoded[0:3]),
560 560 (False, None, -2, cborutil.SPECIAL_NONE),
561 561 )
562 562 self.assertEqual(
563 563 cborutil.decodeitem(encoded[0:4]),
564 564 (False, None, -1, cborutil.SPECIAL_NONE),
565 565 )
566 566 self.assertEqual(
567 567 cborutil.decodeitem(encoded[0:5]),
568 568 (True, -1048580, 5, cborutil.SPECIAL_NONE),
569 569 )
570 570
571 571 def testdecodepartialulonglong(self):
572 572 encoded = b''.join(cborutil.streamencode(2 ** 32))
573 573
574 574 self.assertEqual(
575 575 cborutil.decodeitem(encoded[0:1]),
576 576 (False, None, -8, cborutil.SPECIAL_NONE),
577 577 )
578 578 self.assertEqual(
579 579 cborutil.decodeitem(encoded[0:2]),
580 580 (False, None, -7, cborutil.SPECIAL_NONE),
581 581 )
582 582 self.assertEqual(
583 583 cborutil.decodeitem(encoded[0:3]),
584 584 (False, None, -6, cborutil.SPECIAL_NONE),
585 585 )
586 586 self.assertEqual(
587 587 cborutil.decodeitem(encoded[0:4]),
588 588 (False, None, -5, cborutil.SPECIAL_NONE),
589 589 )
590 590 self.assertEqual(
591 591 cborutil.decodeitem(encoded[0:5]),
592 592 (False, None, -4, cborutil.SPECIAL_NONE),
593 593 )
594 594 self.assertEqual(
595 595 cborutil.decodeitem(encoded[0:6]),
596 596 (False, None, -3, cborutil.SPECIAL_NONE),
597 597 )
598 598 self.assertEqual(
599 599 cborutil.decodeitem(encoded[0:7]),
600 600 (False, None, -2, cborutil.SPECIAL_NONE),
601 601 )
602 602 self.assertEqual(
603 603 cborutil.decodeitem(encoded[0:8]),
604 604 (False, None, -1, cborutil.SPECIAL_NONE),
605 605 )
606 606 self.assertEqual(
607 607 cborutil.decodeitem(encoded[0:9]),
608 608 (True, 2 ** 32, 9, cborutil.SPECIAL_NONE),
609 609 )
610 610
611 611 with self.assertRaisesRegex(
612 612 cborutil.CBORDecodeError, 'input data not fully consumed'
613 613 ):
614 614 cborutil.decodeall(encoded[0:1])
615 615
616 616 with self.assertRaisesRegex(
617 617 cborutil.CBORDecodeError, 'input data not fully consumed'
618 618 ):
619 619 cborutil.decodeall(encoded[0:2])
620 620
621 621 def testdecodepartiallonglong(self):
622 622 encoded = b''.join(cborutil.streamencode(-7000000000))
623 623
624 624 self.assertEqual(
625 625 cborutil.decodeitem(encoded[0:1]),
626 626 (False, None, -8, cborutil.SPECIAL_NONE),
627 627 )
628 628 self.assertEqual(
629 629 cborutil.decodeitem(encoded[0:2]),
630 630 (False, None, -7, cborutil.SPECIAL_NONE),
631 631 )
632 632 self.assertEqual(
633 633 cborutil.decodeitem(encoded[0:3]),
634 634 (False, None, -6, cborutil.SPECIAL_NONE),
635 635 )
636 636 self.assertEqual(
637 637 cborutil.decodeitem(encoded[0:4]),
638 638 (False, None, -5, cborutil.SPECIAL_NONE),
639 639 )
640 640 self.assertEqual(
641 641 cborutil.decodeitem(encoded[0:5]),
642 642 (False, None, -4, cborutil.SPECIAL_NONE),
643 643 )
644 644 self.assertEqual(
645 645 cborutil.decodeitem(encoded[0:6]),
646 646 (False, None, -3, cborutil.SPECIAL_NONE),
647 647 )
648 648 self.assertEqual(
649 649 cborutil.decodeitem(encoded[0:7]),
650 650 (False, None, -2, cborutil.SPECIAL_NONE),
651 651 )
652 652 self.assertEqual(
653 653 cborutil.decodeitem(encoded[0:8]),
654 654 (False, None, -1, cborutil.SPECIAL_NONE),
655 655 )
656 656 self.assertEqual(
657 657 cborutil.decodeitem(encoded[0:9]),
658 658 (True, -7000000000, 9, cborutil.SPECIAL_NONE),
659 659 )
660 660
661 661
662 662 class ArrayTests(TestCase):
663 663 def testempty(self):
664 664 self.assertEqual(list(cborutil.streamencode([])), [b'\x80'])
665 665 self.assertEqual(loadit(cborutil.streamencode([])), [])
666 666
667 667 self.assertEqual(cborutil.decodeall(b'\x80'), [[]])
668 668
669 669 def testbasic(self):
670 670 source = [b'foo', b'bar', 1, -10]
671 671
672 672 chunks = [b'\x84', b'\x43', b'foo', b'\x43', b'bar', b'\x01', b'\x29']
673 673
674 674 self.assertEqual(list(cborutil.streamencode(source)), chunks)
675 675
676 676 self.assertEqual(cborutil.decodeall(b''.join(chunks)), [source])
677 677
678 678 def testemptyfromiter(self):
679 679 self.assertEqual(
680 680 b''.join(cborutil.streamencodearrayfromiter([])), b'\x9f\xff'
681 681 )
682 682
683 683 with self.assertRaisesRegex(
684 684 cborutil.CBORDecodeError, 'indefinite length uint not allowed'
685 685 ):
686 686 cborutil.decodeall(b'\x9f\xff')
687 687
688 688 def testfromiter1(self):
689 689 source = [b'foo']
690 690
691 691 self.assertEqual(
692 692 list(cborutil.streamencodearrayfromiter(source)),
693 693 [b'\x9f', b'\x43', b'foo', b'\xff',],
694 694 )
695 695
696 696 dest = b''.join(cborutil.streamencodearrayfromiter(source))
697 697 self.assertEqual(cbor.loads(dest), source)
698 698
699 699 with self.assertRaisesRegex(
700 700 cborutil.CBORDecodeError, 'indefinite length uint not allowed'
701 701 ):
702 702 cborutil.decodeall(dest)
703 703
704 704 def testtuple(self):
705 705 source = (b'foo', None, 42)
706 706 encoded = b''.join(cborutil.streamencode(source))
707 707
708 708 self.assertEqual(cbor.loads(encoded), list(source))
709 709
710 710 self.assertEqual(cborutil.decodeall(encoded), [list(source)])
711 711
712 712 def testpartialdecode(self):
713 713 source = list(range(4))
714 714 encoded = b''.join(cborutil.streamencode(source))
715 715 self.assertEqual(
716 716 cborutil.decodeitem(encoded[0:1]),
717 717 (True, 4, 1, cborutil.SPECIAL_START_ARRAY),
718 718 )
719 719 self.assertEqual(
720 720 cborutil.decodeitem(encoded[0:2]),
721 721 (True, 4, 1, cborutil.SPECIAL_START_ARRAY),
722 722 )
723 723
724 724 source = list(range(23))
725 725 encoded = b''.join(cborutil.streamencode(source))
726 726 self.assertEqual(
727 727 cborutil.decodeitem(encoded[0:1]),
728 728 (True, 23, 1, cborutil.SPECIAL_START_ARRAY),
729 729 )
730 730 self.assertEqual(
731 731 cborutil.decodeitem(encoded[0:2]),
732 732 (True, 23, 1, cborutil.SPECIAL_START_ARRAY),
733 733 )
734 734
735 735 source = list(range(24))
736 736 encoded = b''.join(cborutil.streamencode(source))
737 737 self.assertEqual(
738 738 cborutil.decodeitem(encoded[0:1]),
739 739 (False, None, -1, cborutil.SPECIAL_NONE),
740 740 )
741 741 self.assertEqual(
742 742 cborutil.decodeitem(encoded[0:2]),
743 743 (True, 24, 2, cborutil.SPECIAL_START_ARRAY),
744 744 )
745 745 self.assertEqual(
746 746 cborutil.decodeitem(encoded[0:3]),
747 747 (True, 24, 2, cborutil.SPECIAL_START_ARRAY),
748 748 )
749 749
750 750 source = list(range(256))
751 751 encoded = b''.join(cborutil.streamencode(source))
752 752 self.assertEqual(
753 753 cborutil.decodeitem(encoded[0:1]),
754 754 (False, None, -2, cborutil.SPECIAL_NONE),
755 755 )
756 756 self.assertEqual(
757 757 cborutil.decodeitem(encoded[0:2]),
758 758 (False, None, -1, cborutil.SPECIAL_NONE),
759 759 )
760 760 self.assertEqual(
761 761 cborutil.decodeitem(encoded[0:3]),
762 762 (True, 256, 3, cborutil.SPECIAL_START_ARRAY),
763 763 )
764 764 self.assertEqual(
765 765 cborutil.decodeitem(encoded[0:4]),
766 766 (True, 256, 3, cborutil.SPECIAL_START_ARRAY),
767 767 )
768 768
769 769 def testnested(self):
770 770 source = [[], [], [[], [], []]]
771 771 encoded = b''.join(cborutil.streamencode(source))
772 772 self.assertEqual(cborutil.decodeall(encoded), [source])
773 773
774 774 source = [True, None, [True, 0, 2], [None], [], [[[]], -87]]
775 775 encoded = b''.join(cborutil.streamencode(source))
776 776 self.assertEqual(cborutil.decodeall(encoded), [source])
777 777
778 778 # A set within an array.
779 779 source = [None, {b'foo', b'bar', None, False}, set()]
780 780 encoded = b''.join(cborutil.streamencode(source))
781 781 self.assertEqual(cborutil.decodeall(encoded), [source])
782 782
783 783 # A map within an array.
784 784 source = [None, {}, {b'foo': b'bar', True: False}, [{}]]
785 785 encoded = b''.join(cborutil.streamencode(source))
786 786 self.assertEqual(cborutil.decodeall(encoded), [source])
787 787
788 788 def testindefinitebytestringvalues(self):
789 789 # Single value array whose value is an empty indefinite bytestring.
790 790 encoded = b'\x81\x5f\x40\xff'
791 791
792 792 with self.assertRaisesRegex(
793 793 cborutil.CBORDecodeError,
794 794 'indefinite length bytestrings not ' 'allowed as array values',
795 795 ):
796 796 cborutil.decodeall(encoded)
797 797
798 798
799 799 class SetTests(TestCase):
800 800 def testempty(self):
801 801 self.assertEqual(
802 802 list(cborutil.streamencode(set())), [b'\xd9\x01\x02', b'\x80',]
803 803 )
804 804
805 805 self.assertEqual(cborutil.decodeall(b'\xd9\x01\x02\x80'), [set()])
806 806
807 807 def testset(self):
808 808 source = {b'foo', None, 42}
809 809 encoded = b''.join(cborutil.streamencode(source))
810 810
811 811 self.assertEqual(cbor.loads(encoded), source)
812 812
813 813 self.assertEqual(cborutil.decodeall(encoded), [source])
814 814
815 815 def testinvalidtag(self):
816 816 # Must use array to encode sets.
817 817 encoded = b'\xd9\x01\x02\xa0'
818 818
819 819 with self.assertRaisesRegex(
820 820 cborutil.CBORDecodeError,
821 821 'expected array after finite set ' 'semantic tag',
822 822 ):
823 823 cborutil.decodeall(encoded)
824 824
825 825 def testpartialdecode(self):
826 826 # Semantic tag item will be 3 bytes. Set header will be variable
827 827 # depending on length.
828 828 encoded = b''.join(cborutil.streamencode({i for i in range(23)}))
829 829 self.assertEqual(
830 830 cborutil.decodeitem(encoded[0:1]),
831 831 (False, None, -2, cborutil.SPECIAL_NONE),
832 832 )
833 833 self.assertEqual(
834 834 cborutil.decodeitem(encoded[0:2]),
835 835 (False, None, -1, cborutil.SPECIAL_NONE),
836 836 )
837 837 self.assertEqual(
838 838 cborutil.decodeitem(encoded[0:3]),
839 839 (False, None, -1, cborutil.SPECIAL_NONE),
840 840 )
841 841 self.assertEqual(
842 842 cborutil.decodeitem(encoded[0:4]),
843 843 (True, 23, 4, cborutil.SPECIAL_START_SET),
844 844 )
845 845 self.assertEqual(
846 846 cborutil.decodeitem(encoded[0:5]),
847 847 (True, 23, 4, cborutil.SPECIAL_START_SET),
848 848 )
849 849
850 850 encoded = b''.join(cborutil.streamencode({i for i in range(24)}))
851 851 self.assertEqual(
852 852 cborutil.decodeitem(encoded[0:1]),
853 853 (False, None, -2, cborutil.SPECIAL_NONE),
854 854 )
855 855 self.assertEqual(
856 856 cborutil.decodeitem(encoded[0:2]),
857 857 (False, None, -1, cborutil.SPECIAL_NONE),
858 858 )
859 859 self.assertEqual(
860 860 cborutil.decodeitem(encoded[0:3]),
861 861 (False, None, -1, cborutil.SPECIAL_NONE),
862 862 )
863 863 self.assertEqual(
864 864 cborutil.decodeitem(encoded[0:4]),
865 865 (False, None, -1, cborutil.SPECIAL_NONE),
866 866 )
867 867 self.assertEqual(
868 868 cborutil.decodeitem(encoded[0:5]),
869 869 (True, 24, 5, cborutil.SPECIAL_START_SET),
870 870 )
871 871 self.assertEqual(
872 872 cborutil.decodeitem(encoded[0:6]),
873 873 (True, 24, 5, cborutil.SPECIAL_START_SET),
874 874 )
875 875
876 876 encoded = b''.join(cborutil.streamencode({i for i in range(256)}))
877 877 self.assertEqual(
878 878 cborutil.decodeitem(encoded[0:1]),
879 879 (False, None, -2, cborutil.SPECIAL_NONE),
880 880 )
881 881 self.assertEqual(
882 882 cborutil.decodeitem(encoded[0:2]),
883 883 (False, None, -1, cborutil.SPECIAL_NONE),
884 884 )
885 885 self.assertEqual(
886 886 cborutil.decodeitem(encoded[0:3]),
887 887 (False, None, -1, cborutil.SPECIAL_NONE),
888 888 )
889 889 self.assertEqual(
890 890 cborutil.decodeitem(encoded[0:4]),
891 891 (False, None, -2, cborutil.SPECIAL_NONE),
892 892 )
893 893 self.assertEqual(
894 894 cborutil.decodeitem(encoded[0:5]),
895 895 (False, None, -1, cborutil.SPECIAL_NONE),
896 896 )
897 897 self.assertEqual(
898 898 cborutil.decodeitem(encoded[0:6]),
899 899 (True, 256, 6, cborutil.SPECIAL_START_SET),
900 900 )
901 901
902 902 def testinvalidvalue(self):
903 903 encoded = b''.join(
904 904 [
905 905 b'\xd9\x01\x02', # semantic tag
906 906 b'\x81', # array of size 1
907 907 b'\x5f\x43foo\xff', # indefinite length bytestring "foo"
908 908 ]
909 909 )
910 910
911 911 with self.assertRaisesRegex(
912 912 cborutil.CBORDecodeError,
913 913 'indefinite length bytestrings not ' 'allowed as set values',
914 914 ):
915 915 cborutil.decodeall(encoded)
916 916
917 917 encoded = b''.join([b'\xd9\x01\x02', b'\x81', b'\x80',]) # empty array
918 918
919 919 with self.assertRaisesRegex(
920 920 cborutil.CBORDecodeError, 'collections not allowed as set values'
921 921 ):
922 922 cborutil.decodeall(encoded)
923 923
924 924 encoded = b''.join([b'\xd9\x01\x02', b'\x81', b'\xa0',]) # empty map
925 925
926 926 with self.assertRaisesRegex(
927 927 cborutil.CBORDecodeError, 'collections not allowed as set values'
928 928 ):
929 929 cborutil.decodeall(encoded)
930 930
931 931 encoded = b''.join(
932 932 [
933 933 b'\xd9\x01\x02',
934 934 b'\x81',
935 935 b'\xd9\x01\x02\x81\x01', # set with integer 1
936 936 ]
937 937 )
938 938
939 939 with self.assertRaisesRegex(
940 940 cborutil.CBORDecodeError, 'collections not allowed as set values'
941 941 ):
942 942 cborutil.decodeall(encoded)
943 943
944 944
945 945 class BoolTests(TestCase):
946 946 def testbasic(self):
947 947 self.assertEqual(list(cborutil.streamencode(True)), [b'\xf5'])
948 948 self.assertEqual(list(cborutil.streamencode(False)), [b'\xf4'])
949 949
950 950 self.assertIs(loadit(cborutil.streamencode(True)), True)
951 951 self.assertIs(loadit(cborutil.streamencode(False)), False)
952 952
953 953 self.assertEqual(cborutil.decodeall(b'\xf4'), [False])
954 954 self.assertEqual(cborutil.decodeall(b'\xf5'), [True])
955 955
956 956 self.assertEqual(
957 957 cborutil.decodeall(b'\xf4\xf5\xf5\xf4'), [False, True, True, False]
958 958 )
959 959
960 960
961 961 class NoneTests(TestCase):
962 962 def testbasic(self):
963 963 self.assertEqual(list(cborutil.streamencode(None)), [b'\xf6'])
964 964
965 965 self.assertIs(loadit(cborutil.streamencode(None)), None)
966 966
967 967 self.assertEqual(cborutil.decodeall(b'\xf6'), [None])
968 968 self.assertEqual(cborutil.decodeall(b'\xf6\xf6'), [None, None])
969 969
970 970
971 971 class MapTests(TestCase):
972 972 def testempty(self):
973 973 self.assertEqual(list(cborutil.streamencode({})), [b'\xa0'])
974 974 self.assertEqual(loadit(cborutil.streamencode({})), {})
975 975
976 976 self.assertEqual(cborutil.decodeall(b'\xa0'), [{}])
977 977
978 978 def testemptyindefinite(self):
979 979 self.assertEqual(
980 980 list(cborutil.streamencodemapfromiter([])), [b'\xbf', b'\xff']
981 981 )
982 982
983 983 self.assertEqual(loadit(cborutil.streamencodemapfromiter([])), {})
984 984
985 985 with self.assertRaisesRegex(
986 986 cborutil.CBORDecodeError, 'indefinite length uint not allowed'
987 987 ):
988 988 cborutil.decodeall(b'\xbf\xff')
989 989
990 990 def testone(self):
991 991 source = {b'foo': b'bar'}
992 992 self.assertEqual(
993 993 list(cborutil.streamencode(source)),
994 994 [b'\xa1', b'\x43', b'foo', b'\x43', b'bar'],
995 995 )
996 996
997 997 self.assertEqual(loadit(cborutil.streamencode(source)), source)
998 998
999 999 self.assertEqual(cborutil.decodeall(b'\xa1\x43foo\x43bar'), [source])
1000 1000
1001 1001 def testmultiple(self):
1002 1002 source = {
1003 1003 b'foo': b'bar',
1004 1004 b'baz': b'value1',
1005 1005 }
1006 1006
1007 1007 self.assertEqual(loadit(cborutil.streamencode(source)), source)
1008 1008
1009 1009 self.assertEqual(
1010 1010 loadit(cborutil.streamencodemapfromiter(source.items())), source
1011 1011 )
1012 1012
1013 1013 encoded = b''.join(cborutil.streamencode(source))
1014 1014 self.assertEqual(cborutil.decodeall(encoded), [source])
1015 1015
1016 1016 def testcomplex(self):
1017 1017 source = {
1018 1018 b'key': 1,
1019 1019 2: -10,
1020 1020 }
1021 1021
1022 1022 self.assertEqual(loadit(cborutil.streamencode(source)), source)
1023 1023
1024 1024 self.assertEqual(
1025 1025 loadit(cborutil.streamencodemapfromiter(source.items())), source
1026 1026 )
1027 1027
1028 1028 encoded = b''.join(cborutil.streamencode(source))
1029 1029 self.assertEqual(cborutil.decodeall(encoded), [source])
1030 1030
1031 1031 def testnested(self):
1032 1032 source = {b'key1': None, b'key2': {b'sub1': b'sub2'}, b'sub2': {}}
1033 1033 encoded = b''.join(cborutil.streamencode(source))
1034 1034
1035 1035 self.assertEqual(cborutil.decodeall(encoded), [source])
1036 1036
1037 1037 source = {
1038 1038 b'key1': [],
1039 1039 b'key2': [None, False],
1040 1040 b'key3': {b'foo', b'bar'},
1041 1041 b'key4': {},
1042 1042 }
1043 1043 encoded = b''.join(cborutil.streamencode(source))
1044 1044 self.assertEqual(cborutil.decodeall(encoded), [source])
1045 1045
1046 1046 def testillegalkey(self):
1047 1047 encoded = b''.join(
1048 1048 [
1049 1049 # map header + len 1
1050 1050 b'\xa1',
1051 1051 # indefinite length bytestring "foo" in key position
1052 1052 b'\x5f\x03foo\xff',
1053 1053 ]
1054 1054 )
1055 1055
1056 1056 with self.assertRaisesRegex(
1057 1057 cborutil.CBORDecodeError,
1058 1058 'indefinite length bytestrings not ' 'allowed as map keys',
1059 1059 ):
1060 1060 cborutil.decodeall(encoded)
1061 1061
1062 1062 encoded = b''.join([b'\xa1', b'\x80', b'\x43foo',]) # empty array
1063 1063
1064 1064 with self.assertRaisesRegex(
1065 1065 cborutil.CBORDecodeError, 'collections not supported as map keys'
1066 1066 ):
1067 1067 cborutil.decodeall(encoded)
1068 1068
1069 1069 def testillegalvalue(self):
1070 1070 encoded = b''.join(
1071 1071 [
1072 1072 b'\xa1', # map headers
1073 1073 b'\x43foo', # key
1074 1074 b'\x5f\x03bar\xff', # indefinite length value
1075 1075 ]
1076 1076 )
1077 1077
1078 1078 with self.assertRaisesRegex(
1079 1079 cborutil.CBORDecodeError,
1080 1080 'indefinite length bytestrings not ' 'allowed as map values',
1081 1081 ):
1082 1082 cborutil.decodeall(encoded)
1083 1083
1084 1084 def testpartialdecode(self):
1085 1085 source = {b'key1': b'value1'}
1086 1086 encoded = b''.join(cborutil.streamencode(source))
1087 1087
1088 1088 self.assertEqual(
1089 1089 cborutil.decodeitem(encoded[0:1]),
1090 1090 (True, 1, 1, cborutil.SPECIAL_START_MAP),
1091 1091 )
1092 1092 self.assertEqual(
1093 1093 cborutil.decodeitem(encoded[0:2]),
1094 1094 (True, 1, 1, cborutil.SPECIAL_START_MAP),
1095 1095 )
1096 1096
1097 1097 source = {b'key%d' % i: None for i in range(23)}
1098 1098 encoded = b''.join(cborutil.streamencode(source))
1099 1099 self.assertEqual(
1100 1100 cborutil.decodeitem(encoded[0:1]),
1101 1101 (True, 23, 1, cborutil.SPECIAL_START_MAP),
1102 1102 )
1103 1103
1104 1104 source = {b'key%d' % i: None for i in range(24)}
1105 1105 encoded = b''.join(cborutil.streamencode(source))
1106 1106 self.assertEqual(
1107 1107 cborutil.decodeitem(encoded[0:1]),
1108 1108 (False, None, -1, cborutil.SPECIAL_NONE),
1109 1109 )
1110 1110 self.assertEqual(
1111 1111 cborutil.decodeitem(encoded[0:2]),
1112 1112 (True, 24, 2, cborutil.SPECIAL_START_MAP),
1113 1113 )
1114 1114 self.assertEqual(
1115 1115 cborutil.decodeitem(encoded[0:3]),
1116 1116 (True, 24, 2, cborutil.SPECIAL_START_MAP),
1117 1117 )
1118 1118
1119 1119 source = {b'key%d' % i: None for i in range(256)}
1120 1120 encoded = b''.join(cborutil.streamencode(source))
1121 1121 self.assertEqual(
1122 1122 cborutil.decodeitem(encoded[0:1]),
1123 1123 (False, None, -2, cborutil.SPECIAL_NONE),
1124 1124 )
1125 1125 self.assertEqual(
1126 1126 cborutil.decodeitem(encoded[0:2]),
1127 1127 (False, None, -1, cborutil.SPECIAL_NONE),
1128 1128 )
1129 1129 self.assertEqual(
1130 1130 cborutil.decodeitem(encoded[0:3]),
1131 1131 (True, 256, 3, cborutil.SPECIAL_START_MAP),
1132 1132 )
1133 1133 self.assertEqual(
1134 1134 cborutil.decodeitem(encoded[0:4]),
1135 1135 (True, 256, 3, cborutil.SPECIAL_START_MAP),
1136 1136 )
1137 1137
1138 1138 source = {b'key%d' % i: None for i in range(65536)}
1139 1139 encoded = b''.join(cborutil.streamencode(source))
1140 1140 self.assertEqual(
1141 1141 cborutil.decodeitem(encoded[0:1]),
1142 1142 (False, None, -4, cborutil.SPECIAL_NONE),
1143 1143 )
1144 1144 self.assertEqual(
1145 1145 cborutil.decodeitem(encoded[0:2]),
1146 1146 (False, None, -3, cborutil.SPECIAL_NONE),
1147 1147 )
1148 1148 self.assertEqual(
1149 1149 cborutil.decodeitem(encoded[0:3]),
1150 1150 (False, None, -2, cborutil.SPECIAL_NONE),
1151 1151 )
1152 1152 self.assertEqual(
1153 1153 cborutil.decodeitem(encoded[0:4]),
1154 1154 (False, None, -1, cborutil.SPECIAL_NONE),
1155 1155 )
1156 1156 self.assertEqual(
1157 1157 cborutil.decodeitem(encoded[0:5]),
1158 1158 (True, 65536, 5, cborutil.SPECIAL_START_MAP),
1159 1159 )
1160 1160 self.assertEqual(
1161 1161 cborutil.decodeitem(encoded[0:6]),
1162 1162 (True, 65536, 5, cborutil.SPECIAL_START_MAP),
1163 1163 )
1164 1164
1165 1165
1166 1166 class SemanticTagTests(TestCase):
1167 1167 def testdecodeforbidden(self):
1168 1168 for i in range(500):
1169 1169 if i == cborutil.SEMANTIC_TAG_FINITE_SET:
1170 1170 continue
1171 1171
1172 1172 tag = cborutil.encodelength(cborutil.MAJOR_TYPE_SEMANTIC, i)
1173 1173
1174 1174 encoded = tag + cborutil.encodelength(cborutil.MAJOR_TYPE_UINT, 42)
1175 1175
1176 1176 # Partial decode is incomplete.
1177 1177 if i < 24:
1178 1178 pass
1179 1179 elif i < 256:
1180 1180 self.assertEqual(
1181 1181 cborutil.decodeitem(encoded[0:1]),
1182 1182 (False, None, -1, cborutil.SPECIAL_NONE),
1183 1183 )
1184 1184 elif i < 65536:
1185 1185 self.assertEqual(
1186 1186 cborutil.decodeitem(encoded[0:1]),
1187 1187 (False, None, -2, cborutil.SPECIAL_NONE),
1188 1188 )
1189 1189 self.assertEqual(
1190 1190 cborutil.decodeitem(encoded[0:2]),
1191 1191 (False, None, -1, cborutil.SPECIAL_NONE),
1192 1192 )
1193 1193
1194 1194 with self.assertRaisesRegex(
1195 1195 cborutil.CBORDecodeError, r'semantic tag \d+ not allowed'
1196 1196 ):
1197 1197 cborutil.decodeitem(encoded)
1198 1198
1199 1199
1200 1200 class SpecialTypesTests(TestCase):
1201 1201 def testforbiddentypes(self):
1202 1202 for i in range(256):
1203 1203 if i == cborutil.SUBTYPE_FALSE:
1204 1204 continue
1205 1205 elif i == cborutil.SUBTYPE_TRUE:
1206 1206 continue
1207 1207 elif i == cborutil.SUBTYPE_NULL:
1208 1208 continue
1209 1209
1210 1210 encoded = cborutil.encodelength(cborutil.MAJOR_TYPE_SPECIAL, i)
1211 1211
1212 1212 with self.assertRaisesRegex(
1213 1213 cborutil.CBORDecodeError, r'special type \d+ not allowed'
1214 1214 ):
1215 1215 cborutil.decodeitem(encoded)
1216 1216
1217 1217
1218 1218 class SansIODecoderTests(TestCase):
1219 1219 def testemptyinput(self):
1220 1220 decoder = cborutil.sansiodecoder()
1221 1221 self.assertEqual(decoder.decode(b''), (False, 0, 0))
1222 1222
1223 1223
1224 1224 class BufferingDecoderTests(TestCase):
1225 1225 def testsimple(self):
1226 1226 source = [
1227 1227 b'foobar',
1228 1228 b'x' * 128,
1229 1229 {b'foo': b'bar'},
1230 1230 True,
1231 1231 False,
1232 1232 None,
1233 [None for i in range(128)],
1233 [None] * 128,
1234 1234 ]
1235 1235
1236 1236 encoded = b''.join(cborutil.streamencode(source))
1237 1237
1238 1238 for step in range(1, 32):
1239 1239 decoder = cborutil.bufferingdecoder()
1240 1240 start = 0
1241 1241
1242 1242 while start < len(encoded):
1243 1243 decoder.decode(encoded[start : start + step])
1244 1244 start += step
1245 1245
1246 1246 self.assertEqual(decoder.getavailable(), [source])
1247 1247
1248 1248 def testbytearray(self):
1249 1249 source = b''.join(cborutil.streamencode(b'foobar'))
1250 1250
1251 1251 decoder = cborutil.bufferingdecoder()
1252 1252 decoder.decode(bytearray(source))
1253 1253
1254 1254 self.assertEqual(decoder.getavailable(), [b'foobar'])
1255 1255
1256 1256
1257 1257 class DecodeallTests(TestCase):
1258 1258 def testemptyinput(self):
1259 1259 self.assertEqual(cborutil.decodeall(b''), [])
1260 1260
1261 1261 def testpartialinput(self):
1262 1262 encoded = b''.join(
1263 1263 [b'\x82', b'\x01',] # array of 2 elements # integer 1
1264 1264 )
1265 1265
1266 1266 with self.assertRaisesRegex(
1267 1267 cborutil.CBORDecodeError, 'input data not complete'
1268 1268 ):
1269 1269 cborutil.decodeall(encoded)
1270 1270
1271 1271
1272 1272 if __name__ == '__main__':
1273 1273 import silenttestrunner
1274 1274
1275 1275 silenttestrunner.main(__name__)
General Comments 0
You need to be logged in to leave comments. Login now