Skip to content

Commit bfea673

Browse files
committed
tests: Update the root verification tests
Change tests so the previous root version is what the code expects. Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent 161c3e3 commit bfea673

1 file changed

Lines changed: 32 additions & 19 deletions

File tree

tests/test_api.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
logger = logging.getLogger(__name__)
5555

5656

57-
# pylint: disable=too-many-public-methods
57+
# pylint: disable=too-many-public-methods,too-many-statements
5858
class TestMetadata(unittest.TestCase):
5959
"""Tests for public API of all classes in 'tuf/api/metadata.py'."""
6060

@@ -551,30 +551,43 @@ def test_root_get_root_verification_result(self) -> None:
551551

552552
priv_key4 = self.keystore[Snapshot.type]
553553

554-
# other_root is only used as the other verifying role
555-
other_root: Metadata[Root] = deepcopy(root)
554+
# Test: Verify with no previous root version
555+
result = root.signed.get_root_verification_result(
556+
None, root.signed_bytes, root.signatures
557+
)
558+
self.assertTrue(result)
559+
self.assertEqual(result.signed, {key1_id: key1})
560+
self.assertEqual(result.unsigned, {})
561+
562+
# Test: Verify with other root that is not version N-1
563+
prev_root: Metadata[Root] = deepcopy(root)
564+
with self.assertRaises(ValueError):
565+
result = root.signed.get_root_verification_result(
566+
prev_root.signed, root.signed_bytes, root.signatures
567+
)
556568

557-
# Test: Verify with two roles that are the same
569+
# Test: Verify with previous root
570+
prev_root.signed.version -= 1
558571
result = root.signed.get_root_verification_result(
559-
other_root.signed, root.signed_bytes, root.signatures
572+
prev_root.signed, root.signed_bytes, root.signatures
560573
)
561574
self.assertTrue(result)
562575
self.assertEqual(result.signed, {key1_id: key1})
563576
self.assertEqual(result.unsigned, {})
564577

565-
# Test: Add a signer to other root (threshold still 1)
566-
other_root.signed.add_key(key2, Root.type)
578+
# Test: Add a signer to previous root (threshold still 1)
579+
prev_root.signed.add_key(key2, Root.type)
567580
result = root.signed.get_root_verification_result(
568-
other_root.signed, root.signed_bytes, root.signatures
581+
prev_root.signed, root.signed_bytes, root.signatures
569582
)
570583
self.assertTrue(result)
571584
self.assertEqual(result.signed, {key1_id: key1})
572585
self.assertEqual(result.unsigned, {key2_id: key2})
573586

574-
# Test: Increase threshold in other root
575-
other_root.signed.roles[Root.type].threshold += 1
587+
# Test: Increase threshold in previous root
588+
prev_root.signed.roles[Root.type].threshold += 1
576589
result = root.signed.get_root_verification_result(
577-
other_root.signed, root.signed_bytes, root.signatures
590+
prev_root.signed, root.signed_bytes, root.signatures
578591
)
579592
self.assertFalse(result)
580593
self.assertEqual(result.signed, {key1_id: key1})
@@ -583,7 +596,7 @@ def test_root_get_root_verification_result(self) -> None:
583596
# Test: Sign root with both keys
584597
root.sign(SSlibSigner(priv_key2), append=True)
585598
result = root.signed.get_root_verification_result(
586-
other_root.signed, root.signed_bytes, root.signatures
599+
prev_root.signed, root.signed_bytes, root.signatures
587600
)
588601
self.assertTrue(result)
589602
self.assertEqual(result.signed, {key1_id: key1, key2_id: key2})
@@ -592,25 +605,25 @@ def test_root_get_root_verification_result(self) -> None:
592605
# Test: Sign root with an unrelated key
593606
root.sign(SSlibSigner(priv_key4), append=True)
594607
result = root.signed.get_root_verification_result(
595-
other_root.signed, root.signed_bytes, root.signatures
608+
prev_root.signed, root.signed_bytes, root.signatures
596609
)
597610
self.assertTrue(result)
598611
self.assertEqual(result.signed, {key1_id: key1, key2_id: key2})
599612
self.assertEqual(result.unsigned, {})
600613

601-
# Test: Remove key1 from other root
602-
other_root.signed.revoke_key(key1_id, Root.type)
614+
# Test: Remove key1 from previous root
615+
prev_root.signed.revoke_key(key1_id, Root.type)
603616
result = root.signed.get_root_verification_result(
604-
other_root.signed, root.signed_bytes, root.signatures
617+
prev_root.signed, root.signed_bytes, root.signatures
605618
)
606619
self.assertFalse(result)
607620
self.assertEqual(result.signed, {key1_id: key1, key2_id: key2})
608621
self.assertEqual(result.unsigned, {})
609622

610-
# Test: Lower threshold in other root
611-
other_root.signed.roles[Root.type].threshold -= 1
623+
# Test: Lower threshold in previous root
624+
prev_root.signed.roles[Root.type].threshold -= 1
612625
result = root.signed.get_root_verification_result(
613-
other_root.signed, root.signed_bytes, root.signatures
626+
prev_root.signed, root.signed_bytes, root.signatures
614627
)
615628
self.assertTrue(result)
616629
self.assertEqual(result.signed, {key1_id: key1, key2_id: key2})

0 commit comments

Comments
 (0)