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