|
15 | 15 | import unittest |
16 | 16 | from copy import copy |
17 | 17 | from datetime import datetime, timedelta |
| 18 | +from pathlib import Path |
18 | 19 | from typing import Any, ClassVar, Dict, Optional |
19 | 20 |
|
20 | 21 | from securesystemslib import exceptions as sslib_exceptions |
|
33 | 34 |
|
34 | 35 | from tests import utils |
35 | 36 | from tuf.api import exceptions |
| 37 | +from tuf.api.dsse import SimpleEnvelope |
36 | 38 | from tuf.api.metadata import ( |
37 | 39 | TOP_LEVEL_ROLE_NAMES, |
38 | 40 | DelegatedRole, |
@@ -918,6 +920,95 @@ def test_get_roles_in_succinct_roles(self) -> None: |
918 | 920 | self.assertEqual(role_name, f"bin-{expected_bin_suffix}") |
919 | 921 |
|
920 | 922 |
|
| 923 | +class TestSimpleEnvelope(unittest.TestCase): |
| 924 | + """Tests for public API in 'tuf/api/dsse.py'.""" |
| 925 | + |
| 926 | + @classmethod |
| 927 | + def setUpClass(cls) -> None: |
| 928 | + repo_data_dir = Path(utils.TESTS_DIR) / "repository_data" |
| 929 | + cls.metadata_dir = repo_data_dir / "repository" / "metadata" |
| 930 | + cls.signer_store = {} |
| 931 | + for role in [Snapshot, Targets, Timestamp]: |
| 932 | + key_path = repo_data_dir / "keystore" / f"{role.type}_key" |
| 933 | + key = import_ed25519_privatekey_from_file( |
| 934 | + str(key_path), |
| 935 | + password="password", |
| 936 | + ) |
| 937 | + cls.signer_store[role.type] = SSlibSigner(key) |
| 938 | + |
| 939 | + def test_serialization(self) -> None: |
| 940 | + """Basic de/serialization test. |
| 941 | +
|
| 942 | + 1. Load test metadata for each role |
| 943 | + 2. Wrap metadata payloads in envelope serializing the payload |
| 944 | + 3. Serialize envelope |
| 945 | + 4. De-serialize envelope |
| 946 | + 5. De-serialize payload |
| 947 | +
|
| 948 | + """ |
| 949 | + for role in [Root, Timestamp, Snapshot, Targets]: |
| 950 | + metadata_path = self.metadata_dir / f"{role.type}.json" |
| 951 | + metadata = Metadata.from_file(str(metadata_path)) |
| 952 | + self.assertIsInstance(metadata.signed, role) |
| 953 | + |
| 954 | + envelope = SimpleEnvelope.from_signed(metadata.signed) |
| 955 | + envelope_bytes = envelope.to_bytes() |
| 956 | + |
| 957 | + envelope2 = SimpleEnvelope.from_bytes(envelope_bytes) |
| 958 | + payload = envelope2.get_signed() |
| 959 | + self.assertEqual(metadata.signed, payload) |
| 960 | + |
| 961 | + def test_fail_envelope_serialization(self) -> None: |
| 962 | + envelope = SimpleEnvelope(b"foo", "bar", ["baz"]) |
| 963 | + with self.assertRaises(SerializationError): |
| 964 | + envelope.to_bytes() |
| 965 | + |
| 966 | + def test_fail_envelope_deserialization(self) -> None: |
| 967 | + with self.assertRaises(DeserializationError): |
| 968 | + SimpleEnvelope.from_bytes(b"[") |
| 969 | + |
| 970 | + def test_fail_payload_serialization(self) -> None: |
| 971 | + with self.assertRaises(SerializationError): |
| 972 | + SimpleEnvelope.from_signed("foo") # type: ignore |
| 973 | + |
| 974 | + def test_fail_payload_deserialization(self) -> None: |
| 975 | + payloads = [b"[", b'{"_type": "foo"}'] |
| 976 | + for payload in payloads: |
| 977 | + envelope = SimpleEnvelope(payload, "bar", []) |
| 978 | + with self.assertRaises(DeserializationError): |
| 979 | + envelope.get_signed() |
| 980 | + |
| 981 | + def test_verify_delegate(self) -> None: |
| 982 | + """Basic verification test. |
| 983 | +
|
| 984 | + 1. Load test metadata for each role |
| 985 | + 2. Wrap non-root payloads in envelope serializing the payload |
| 986 | + 3. Sign with correct delegated key |
| 987 | + 4. Verify delegate with root |
| 988 | +
|
| 989 | + """ |
| 990 | + root_path = self.metadata_dir / "root.json" |
| 991 | + root = Metadata[Root].from_file(str(root_path)).signed |
| 992 | + |
| 993 | + for role in [Timestamp, Snapshot, Targets]: |
| 994 | + metadata_path = self.metadata_dir / f"{role.type}.json" |
| 995 | + metadata = Metadata.from_file(str(metadata_path)) |
| 996 | + self.assertIsInstance(metadata.signed, role) |
| 997 | + |
| 998 | + signer = self.signer_store[role.type] |
| 999 | + self.assertIn( |
| 1000 | + signer.key_dict["keyid"], root.roles[role.type].keyids |
| 1001 | + ) |
| 1002 | + |
| 1003 | + envelope = SimpleEnvelope.from_signed(metadata.signed) |
| 1004 | + envelope.sign(signer) |
| 1005 | + self.assertTrue(len(envelope.signatures) == 1) |
| 1006 | + |
| 1007 | + root.verify_delegate( |
| 1008 | + role.type, envelope.pae(), envelope.signatures_dict |
| 1009 | + ) |
| 1010 | + |
| 1011 | + |
921 | 1012 | # Run unit test. |
922 | 1013 | if __name__ == "__main__": |
923 | 1014 | utils.configure_test_logging(sys.argv) |
|
0 commit comments