|
25 | 25 | remove_implicit,
|
26 | 26 | remove_octet_string,
|
27 | 27 | remove_sequence,
|
| 28 | + encode_implicit, |
28 | 29 | )
|
29 | 30 |
|
30 | 31 |
|
@@ -463,6 +464,62 @@ def test_with_constructed(self):
|
463 | 464 |
|
464 | 465 | self.assertIn("wanted type primitive, got 0xa6 tag", str(e.exception))
|
465 | 466 |
|
| 467 | + def test_encode_decode(self): |
| 468 | + data = b"some longish string" |
| 469 | + |
| 470 | + tag, body, rest = remove_implicit( |
| 471 | + encode_implicit(6, data, "application"), |
| 472 | + "application" |
| 473 | + ) |
| 474 | + |
| 475 | + self.assertEqual(tag, 6) |
| 476 | + self.assertEqual(body, data) |
| 477 | + self.assertEqual(rest, b"") |
| 478 | + |
| 479 | + |
| 480 | +class TestEncodeImplicit(unittest.TestCase): |
| 481 | + @classmethod |
| 482 | + def setUpClass(cls): |
| 483 | + cls.data = b"\x0a\x0b" |
| 484 | + # data with application tag class |
| 485 | + cls.data_application = b"\x46\x02\x0a\x0b" |
| 486 | + # data with context-specific tag class |
| 487 | + cls.data_context_specific = b"\x86\x02\x0a\x0b" |
| 488 | + # data with private tag class |
| 489 | + cls.data_private = b"\xc6\x02\x0a\x0b" |
| 490 | + |
| 491 | + def test_encode_with_default_class(self): |
| 492 | + ret = encode_implicit(6, self.data) |
| 493 | + |
| 494 | + self.assertEqual(ret, self.data_context_specific) |
| 495 | + |
| 496 | + def test_encode_with_application_class(self): |
| 497 | + ret = encode_implicit(6, self.data, "application") |
| 498 | + |
| 499 | + self.assertEqual(ret, self.data_application) |
| 500 | + |
| 501 | + def test_encode_with_context_specific_class(self): |
| 502 | + ret = encode_implicit(6, self.data, "context-specific") |
| 503 | + |
| 504 | + self.assertEqual(ret, self.data_context_specific) |
| 505 | + |
| 506 | + def test_encode_with_private_class(self): |
| 507 | + ret = encode_implicit(6, self.data, "private") |
| 508 | + |
| 509 | + self.assertEqual(ret, self.data_private) |
| 510 | + |
| 511 | + def test_encode_with_invalid_class(self): |
| 512 | + with self.assertRaises(ValueError) as e: |
| 513 | + encode_implicit(6, self.data, "foobar") |
| 514 | + |
| 515 | + self.assertIn("invalid tag class", str(e.exception)) |
| 516 | + |
| 517 | + def test_encode_with_too_large_tag(self): |
| 518 | + with self.assertRaises(ValueError) as e: |
| 519 | + encode_implicit(32, self.data) |
| 520 | + |
| 521 | + self.assertIn("Long tags not supported", str(e.exception)) |
| 522 | + |
466 | 523 |
|
467 | 524 | class TestRemoveOctetString(unittest.TestCase):
|
468 | 525 | def test_simple(self):
|
|
0 commit comments