|
22 | 22 | remove_object,
|
23 | 23 | encode_oid,
|
24 | 24 | remove_constructed,
|
| 25 | + remove_implicit, |
25 | 26 | remove_octet_string,
|
26 | 27 | remove_sequence,
|
27 | 28 | )
|
@@ -396,6 +397,73 @@ def test_with_malformed_tag(self):
|
396 | 397 | self.assertIn("constructed tag", str(e.exception))
|
397 | 398 |
|
398 | 399 |
|
| 400 | +class TestRemoveImplicit(unittest.TestCase): |
| 401 | + @classmethod |
| 402 | + def setUpClass(cls): |
| 403 | + cls.exp_tag = 6 |
| 404 | + cls.exp_data = b"\x0a\x0b" |
| 405 | + # data with application tag class |
| 406 | + cls.data_application = b"\x46\x02\x0a\x0b" |
| 407 | + # data with context-specific tag class |
| 408 | + cls.data_context_specific = b"\x86\x02\x0a\x0b" |
| 409 | + # data with private tag class |
| 410 | + cls.data_private = b"\xc6\x02\x0a\x0b" |
| 411 | + |
| 412 | + def test_simple(self): |
| 413 | + tag, body, rest = remove_implicit(self.data_context_specific) |
| 414 | + |
| 415 | + self.assertEqual(tag, self.exp_tag) |
| 416 | + self.assertEqual(body, self.exp_data) |
| 417 | + self.assertEqual(rest, b"") |
| 418 | + |
| 419 | + def test_wrong_expected_class(self): |
| 420 | + with self.assertRaises(ValueError) as e: |
| 421 | + remove_implicit(self.data_context_specific, "foobar") |
| 422 | + |
| 423 | + self.assertIn("invalid `exp_class` value", str(e.exception)) |
| 424 | + |
| 425 | + def test_with_wrong_class(self): |
| 426 | + with self.assertRaises(UnexpectedDER) as e: |
| 427 | + remove_implicit(self.data_application) |
| 428 | + |
| 429 | + self.assertIn( |
| 430 | + "wanted class context-specific, got 0x46 tag", str(e.exception) |
| 431 | + ) |
| 432 | + |
| 433 | + def test_with_application_class(self): |
| 434 | + tag, body, rest = remove_implicit(self.data_application, "application") |
| 435 | + |
| 436 | + self.assertEqual(tag, self.exp_tag) |
| 437 | + self.assertEqual(body, self.exp_data) |
| 438 | + self.assertEqual(rest, b"") |
| 439 | + |
| 440 | + def test_with_private_class(self): |
| 441 | + tag, body, rest = remove_implicit(self.data_private, "private") |
| 442 | + |
| 443 | + self.assertEqual(tag, self.exp_tag) |
| 444 | + self.assertEqual(body, self.exp_data) |
| 445 | + self.assertEqual(rest, b"") |
| 446 | + |
| 447 | + def test_with_data_following(self): |
| 448 | + extra_data = b"\x00\x01" |
| 449 | + |
| 450 | + tag, body, rest = remove_implicit( |
| 451 | + self.data_context_specific + extra_data |
| 452 | + ) |
| 453 | + |
| 454 | + self.assertEqual(tag, self.exp_tag) |
| 455 | + self.assertEqual(body, self.exp_data) |
| 456 | + self.assertEqual(rest, extra_data) |
| 457 | + |
| 458 | + def test_with_constructed(self): |
| 459 | + data = b"\xa6\x02\x0a\x0b" |
| 460 | + |
| 461 | + with self.assertRaises(UnexpectedDER) as e: |
| 462 | + remove_implicit(data) |
| 463 | + |
| 464 | + self.assertIn("wanted type primitive, got 0xa6 tag", str(e.exception)) |
| 465 | + |
| 466 | + |
399 | 467 | class TestRemoveOctetString(unittest.TestCase):
|
400 | 468 | def test_simple(self):
|
401 | 469 | data = b"\x04\x03\xaa\xbb\xcc"
|
|
0 commit comments