|
13 | 13 | import sys
|
14 | 14 | import tempfile
|
15 | 15 | import unittest
|
16 |
| -from copy import copy |
| 16 | +from copy import copy, deepcopy |
17 | 17 | from datetime import datetime, timedelta
|
18 | 18 | from typing import Any, ClassVar, Dict, Optional
|
19 | 19 |
|
@@ -537,6 +537,85 @@ def test_signed_get_verification_result(self) -> None:
|
537 | 537 |
|
538 | 538 | # See test_signed_verify_delegate for more related tests ...
|
539 | 539 |
|
| 540 | + def test_root_get_root_verification_result(self) -> None: |
| 541 | + # Setup: Load test metadata and keys |
| 542 | + root_path = os.path.join(self.repo_dir, "metadata", "root.json") |
| 543 | + root = Metadata[Root].from_file(root_path) |
| 544 | + |
| 545 | + key1_id = root.signed.roles[Root.type].keyids[0] |
| 546 | + key1 = root.signed.get_key(key1_id) |
| 547 | + |
| 548 | + key2_id = root.signed.roles[Timestamp.type].keyids[0] |
| 549 | + key2 = root.signed.get_key(key2_id) |
| 550 | + priv_key2 = self.keystore[Timestamp.type] |
| 551 | + |
| 552 | + priv_key4 = self.keystore[Snapshot.type] |
| 553 | + |
| 554 | + # other_root is only used as the other verifying role |
| 555 | + other_root: Metadata[Root] = deepcopy(root) |
| 556 | + |
| 557 | + # Test: Verify with two roles that are the same |
| 558 | + result = root.signed.get_root_verification_result( |
| 559 | + other_root.signed, root.signed_bytes, root.signatures |
| 560 | + ) |
| 561 | + self.assertTrue(result) |
| 562 | + self.assertEqual(result.signed, {key1_id: key1}) |
| 563 | + self.assertEqual(result.unsigned, {}) |
| 564 | + |
| 565 | + # Test: Add a signer to other root (threshold still 1) |
| 566 | + other_root.signed.add_key(key2, Root.type) |
| 567 | + result = root.signed.get_root_verification_result( |
| 568 | + other_root.signed, root.signed_bytes, root.signatures |
| 569 | + ) |
| 570 | + self.assertTrue(result) |
| 571 | + self.assertEqual(result.signed, {key1_id: key1}) |
| 572 | + self.assertEqual(result.unsigned, {key2_id: key2}) |
| 573 | + |
| 574 | + # Test: Increase threshold in other root |
| 575 | + other_root.signed.roles[Root.type].threshold += 1 |
| 576 | + result = root.signed.get_root_verification_result( |
| 577 | + other_root.signed, root.signed_bytes, root.signatures |
| 578 | + ) |
| 579 | + self.assertFalse(result) |
| 580 | + self.assertEqual(result.signed, {key1_id: key1}) |
| 581 | + self.assertEqual(result.unsigned, {key2_id: key2}) |
| 582 | + |
| 583 | + # Test: Sign root with both keys |
| 584 | + root.sign(SSlibSigner(priv_key2), append=True) |
| 585 | + result = root.signed.get_root_verification_result( |
| 586 | + other_root.signed, root.signed_bytes, root.signatures |
| 587 | + ) |
| 588 | + self.assertTrue(result) |
| 589 | + self.assertEqual(result.signed, {key1_id: key1, key2_id: key2}) |
| 590 | + self.assertEqual(result.unsigned, {}) |
| 591 | + |
| 592 | + # Test: Sign root with an unrelated key |
| 593 | + root.sign(SSlibSigner(priv_key4), append=True) |
| 594 | + result = root.signed.get_root_verification_result( |
| 595 | + other_root.signed, root.signed_bytes, root.signatures |
| 596 | + ) |
| 597 | + self.assertTrue(result) |
| 598 | + self.assertEqual(result.signed, {key1_id: key1, key2_id: key2}) |
| 599 | + self.assertEqual(result.unsigned, {}) |
| 600 | + |
| 601 | + # Test: Remove key1 from other root |
| 602 | + other_root.signed.revoke_key(key1_id, Root.type) |
| 603 | + result = root.signed.get_root_verification_result( |
| 604 | + other_root.signed, root.signed_bytes, root.signatures |
| 605 | + ) |
| 606 | + self.assertFalse(result) |
| 607 | + self.assertEqual(result.signed, {key1_id: key1, key2_id: key2}) |
| 608 | + self.assertEqual(result.unsigned, {}) |
| 609 | + |
| 610 | + # Test: Lower threshold in other root |
| 611 | + other_root.signed.roles[Root.type].threshold -= 1 |
| 612 | + result = root.signed.get_root_verification_result( |
| 613 | + other_root.signed, root.signed_bytes, root.signatures |
| 614 | + ) |
| 615 | + self.assertTrue(result) |
| 616 | + self.assertEqual(result.signed, {key1_id: key1, key2_id: key2}) |
| 617 | + self.assertEqual(result.unsigned, {}) |
| 618 | + |
540 | 619 | def test_key_class(self) -> None:
|
541 | 620 | # Test if from_securesystemslib_key removes the private key from keyval
|
542 | 621 | # of a securesystemslib key dictionary.
|
|
0 commit comments