|
22 | 22 | remove_bitstring,
|
23 | 23 | remove_object,
|
24 | 24 | encode_oid,
|
| 25 | + remove_constructed, |
| 26 | + remove_octet_string, |
| 27 | + remove_sequence, |
25 | 28 | )
|
26 | 29 |
|
27 | 30 |
|
@@ -72,6 +75,18 @@ def test_encoding_of_128(self):
|
72 | 75 | self.assertEqual(val, 128)
|
73 | 76 | self.assertFalse(rem)
|
74 | 77 |
|
| 78 | + def test_wrong_tag(self): |
| 79 | + with self.assertRaises(UnexpectedDER) as e: |
| 80 | + remove_integer(b'\x01\x02\x00\x80') |
| 81 | + |
| 82 | + self.assertIn("wanted type 'integer'", str(e.exception)) |
| 83 | + |
| 84 | + def test_wrong_length(self): |
| 85 | + with self.assertRaises(UnexpectedDER) as e: |
| 86 | + remove_integer(b'\x02\x03\x00\x80') |
| 87 | + |
| 88 | + self.assertIn("Length longer", str(e.exception)) |
| 89 | + |
75 | 90 |
|
76 | 91 | class TestReadLength(unittest.TestCase):
|
77 | 92 | # DER requires the lengths between 0 and 127 to be encoded using the short
|
@@ -369,6 +384,70 @@ def test_with_too_long_length(self):
|
369 | 384 | remove_object(b"\x06\x03\x88\x37")
|
370 | 385 |
|
371 | 386 |
|
| 387 | +class TestRemoveConstructed(unittest.TestCase): |
| 388 | + def test_simple(self): |
| 389 | + data = b'\xa1\x02\xff\xaa' |
| 390 | + |
| 391 | + tag, body, rest = remove_constructed(data) |
| 392 | + |
| 393 | + self.assertEqual(tag, 0x01) |
| 394 | + self.assertEqual(body, b'\xff\xaa') |
| 395 | + self.assertEqual(rest, b'') |
| 396 | + |
| 397 | + def test_with_malformed_tag(self): |
| 398 | + data = b'\x01\x02\xff\xaa' |
| 399 | + |
| 400 | + with self.assertRaises(UnexpectedDER) as e: |
| 401 | + tag, body, rest = remove_constructed(data) |
| 402 | + |
| 403 | + self.assertIn("constructed tag", str(e.exception)) |
| 404 | + |
| 405 | + |
| 406 | +class TestRemoveOctetString(unittest.TestCase): |
| 407 | + def test_simple(self): |
| 408 | + data = b'\x04\x03\xaa\xbb\xcc' |
| 409 | + body, rest = remove_octet_string(data) |
| 410 | + self.assertEqual(body, b'\xaa\xbb\xcc') |
| 411 | + self.assertEqual(rest, b'') |
| 412 | + |
| 413 | + def test_with_malformed_tag(self): |
| 414 | + data = b'\x03\x03\xaa\xbb\xcc' |
| 415 | + with self.assertRaises(UnexpectedDER) as e: |
| 416 | + body, rest = remove_octet_string(data) |
| 417 | + |
| 418 | + self.assertIn("octetstring", str(e.exception)) |
| 419 | + |
| 420 | + |
| 421 | +class TestRemoveSequence(unittest.TestCase): |
| 422 | + def test_simple(self): |
| 423 | + data = b'\x30\x02\xff\xaa' |
| 424 | + body, rest = remove_sequence(data) |
| 425 | + self.assertEqual(body, b'\xff\xaa') |
| 426 | + self.assertEqual(rest, b'') |
| 427 | + |
| 428 | + def test_with_empty_string(self): |
| 429 | + with self.assertRaises(UnexpectedDER) as e: |
| 430 | + remove_sequence(b'') |
| 431 | + |
| 432 | + self.assertIn("Empty string", str(e.exception)) |
| 433 | + |
| 434 | + def test_with_wrong_tag(self): |
| 435 | + data = b'\x20\x02\xff\xaa' |
| 436 | + |
| 437 | + with self.assertRaises(UnexpectedDER) as e: |
| 438 | + remove_sequence(data) |
| 439 | + |
| 440 | + self.assertIn("wanted type 'sequence'", str(e.exception)) |
| 441 | + |
| 442 | + def test_with_wrong_length(self): |
| 443 | + data = b'\x30\x03\xff\xaa' |
| 444 | + |
| 445 | + with self.assertRaises(UnexpectedDER) as e: |
| 446 | + remove_sequence(data) |
| 447 | + |
| 448 | + self.assertIn("Length longer", str(e.exception)) |
| 449 | + |
| 450 | + |
372 | 451 | @st.composite
|
373 | 452 | def st_oid(draw, max_value=2 ** 512, max_size=50):
|
374 | 453 | """
|
|
0 commit comments