|
22 | 22 | remove_object,
|
23 | 23 | encode_oid,
|
24 | 24 | remove_constructed,
|
| 25 | + remove_implicit, |
25 | 26 | remove_octet_string,
|
26 | 27 | remove_sequence,
|
| 28 | + encode_implicit, |
27 | 29 | )
|
28 | 30 |
|
29 | 31 |
|
@@ -396,6 +398,128 @@ def test_with_malformed_tag(self):
|
396 | 398 | self.assertIn("constructed tag", str(e.exception))
|
397 | 399 |
|
398 | 400 |
|
| 401 | +class TestRemoveImplicit(unittest.TestCase): |
| 402 | + @classmethod |
| 403 | + def setUpClass(cls): |
| 404 | + cls.exp_tag = 6 |
| 405 | + cls.exp_data = b"\x0a\x0b" |
| 406 | + # data with application tag class |
| 407 | + cls.data_application = b"\x46\x02\x0a\x0b" |
| 408 | + # data with context-specific tag class |
| 409 | + cls.data_context_specific = b"\x86\x02\x0a\x0b" |
| 410 | + # data with private tag class |
| 411 | + cls.data_private = b"\xc6\x02\x0a\x0b" |
| 412 | + |
| 413 | + def test_simple(self): |
| 414 | + tag, body, rest = remove_implicit(self.data_context_specific) |
| 415 | + |
| 416 | + self.assertEqual(tag, self.exp_tag) |
| 417 | + self.assertEqual(body, self.exp_data) |
| 418 | + self.assertEqual(rest, b"") |
| 419 | + |
| 420 | + def test_wrong_expected_class(self): |
| 421 | + with self.assertRaises(ValueError) as e: |
| 422 | + remove_implicit(self.data_context_specific, "foobar") |
| 423 | + |
| 424 | + self.assertIn("invalid `exp_class` value", str(e.exception)) |
| 425 | + |
| 426 | + def test_with_wrong_class(self): |
| 427 | + with self.assertRaises(UnexpectedDER) as e: |
| 428 | + remove_implicit(self.data_application) |
| 429 | + |
| 430 | + self.assertIn( |
| 431 | + "wanted class context-specific, got 0x46 tag", str(e.exception) |
| 432 | + ) |
| 433 | + |
| 434 | + def test_with_application_class(self): |
| 435 | + tag, body, rest = remove_implicit(self.data_application, "application") |
| 436 | + |
| 437 | + self.assertEqual(tag, self.exp_tag) |
| 438 | + self.assertEqual(body, self.exp_data) |
| 439 | + self.assertEqual(rest, b"") |
| 440 | + |
| 441 | + def test_with_private_class(self): |
| 442 | + tag, body, rest = remove_implicit(self.data_private, "private") |
| 443 | + |
| 444 | + self.assertEqual(tag, self.exp_tag) |
| 445 | + self.assertEqual(body, self.exp_data) |
| 446 | + self.assertEqual(rest, b"") |
| 447 | + |
| 448 | + def test_with_data_following(self): |
| 449 | + extra_data = b"\x00\x01" |
| 450 | + |
| 451 | + tag, body, rest = remove_implicit( |
| 452 | + self.data_context_specific + extra_data |
| 453 | + ) |
| 454 | + |
| 455 | + self.assertEqual(tag, self.exp_tag) |
| 456 | + self.assertEqual(body, self.exp_data) |
| 457 | + self.assertEqual(rest, extra_data) |
| 458 | + |
| 459 | + def test_with_constructed(self): |
| 460 | + data = b"\xa6\x02\x0a\x0b" |
| 461 | + |
| 462 | + with self.assertRaises(UnexpectedDER) as e: |
| 463 | + remove_implicit(data) |
| 464 | + |
| 465 | + self.assertIn("wanted type primitive, got 0xa6 tag", str(e.exception)) |
| 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"), "application" |
| 472 | + ) |
| 473 | + |
| 474 | + self.assertEqual(tag, 6) |
| 475 | + self.assertEqual(body, data) |
| 476 | + self.assertEqual(rest, b"") |
| 477 | + |
| 478 | + |
| 479 | +class TestEncodeImplicit(unittest.TestCase): |
| 480 | + @classmethod |
| 481 | + def setUpClass(cls): |
| 482 | + cls.data = b"\x0a\x0b" |
| 483 | + # data with application tag class |
| 484 | + cls.data_application = b"\x46\x02\x0a\x0b" |
| 485 | + # data with context-specific tag class |
| 486 | + cls.data_context_specific = b"\x86\x02\x0a\x0b" |
| 487 | + # data with private tag class |
| 488 | + cls.data_private = b"\xc6\x02\x0a\x0b" |
| 489 | + |
| 490 | + def test_encode_with_default_class(self): |
| 491 | + ret = encode_implicit(6, self.data) |
| 492 | + |
| 493 | + self.assertEqual(ret, self.data_context_specific) |
| 494 | + |
| 495 | + def test_encode_with_application_class(self): |
| 496 | + ret = encode_implicit(6, self.data, "application") |
| 497 | + |
| 498 | + self.assertEqual(ret, self.data_application) |
| 499 | + |
| 500 | + def test_encode_with_context_specific_class(self): |
| 501 | + ret = encode_implicit(6, self.data, "context-specific") |
| 502 | + |
| 503 | + self.assertEqual(ret, self.data_context_specific) |
| 504 | + |
| 505 | + def test_encode_with_private_class(self): |
| 506 | + ret = encode_implicit(6, self.data, "private") |
| 507 | + |
| 508 | + self.assertEqual(ret, self.data_private) |
| 509 | + |
| 510 | + def test_encode_with_invalid_class(self): |
| 511 | + with self.assertRaises(ValueError) as e: |
| 512 | + encode_implicit(6, self.data, "foobar") |
| 513 | + |
| 514 | + self.assertIn("invalid tag class", str(e.exception)) |
| 515 | + |
| 516 | + def test_encode_with_too_large_tag(self): |
| 517 | + with self.assertRaises(ValueError) as e: |
| 518 | + encode_implicit(32, self.data) |
| 519 | + |
| 520 | + self.assertIn("Long tags not supported", str(e.exception)) |
| 521 | + |
| 522 | + |
399 | 523 | class TestRemoveOctetString(unittest.TestCase):
|
400 | 524 | def test_simple(self):
|
401 | 525 | data = b"\x04\x03\xaa\xbb\xcc"
|
|
0 commit comments