Skip to content

Commit 505e6d1

Browse files
committed
tests: Add tests for root verification
This does much the same tests as test_signed_get_verification_result() above it does, just using two root roles. Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent f2e5531 commit 505e6d1

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

tests/test_api.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import sys
1414
import tempfile
1515
import unittest
16-
from copy import copy
16+
from copy import copy, deepcopy
1717
from datetime import datetime, timedelta
1818
from typing import Any, ClassVar, Dict, Optional
1919

@@ -537,6 +537,85 @@ def test_signed_get_verification_result(self) -> None:
537537

538538
# See test_signed_verify_delegate for more related tests ...
539539

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+
540619
def test_key_class(self) -> None:
541620
# Test if from_securesystemslib_key removes the private key from keyval
542621
# of a securesystemslib key dictionary.

0 commit comments

Comments
 (0)