|
30 | 30 | from tuf.api import exceptions
|
31 | 31 | from tuf.api.metadata import (
|
32 | 32 | TOP_LEVEL_ROLE_NAMES,
|
| 33 | + BaseMetadata, |
33 | 34 | DelegatedRole,
|
34 | 35 | Delegations,
|
| 36 | + Envelope, |
35 | 37 | Key,
|
36 | 38 | Metadata,
|
37 | 39 | Root,
|
|
48 | 50 | logger = logging.getLogger(__name__)
|
49 | 51 |
|
50 | 52 |
|
| 53 | +class TestEnvelope(unittest.TestCase): |
| 54 | + """Smoke test for Envelope (DSSE) and common metadata abstraction.""" |
| 55 | + |
| 56 | + def test_envelope(self) -> None: |
| 57 | + # Generate key and root metadata, and sign and serialize as dsse |
| 58 | + sslib_key = generate_ed25519_key() |
| 59 | + root = Root() |
| 60 | + envelope = Envelope.from_signed(root) |
| 61 | + signer = SSlibSigner(sslib_key) |
| 62 | + envelope.sign(signer) |
| 63 | + data = envelope.to_bytes() |
| 64 | + |
| 65 | + # Deserialize dsse and verify signature successfully |
| 66 | + envelope2 = Envelope.from_bytes(data) |
| 67 | + self.assertEqual(envelope2, envelope) |
| 68 | + key = SSlibKey.from_securesystemslib_key(sslib_key) |
| 69 | + envelope2.verify([key], 1) |
| 70 | + |
| 71 | + # Create new envelope with bad signature, and fail |
| 72 | + envelope3 = Envelope.from_signed(Targets()) |
| 73 | + envelope3.signatures = envelope2.signatures |
| 74 | + with self.assertRaises(sslib_exceptions.VerificationError): |
| 75 | + envelope3.verify([key], 1) |
| 76 | + |
| 77 | + # Add root key to root so that we can verify with 'verify_delegate' |
| 78 | + root.add_key(key, Root.type) |
| 79 | + |
| 80 | + # Sign and serialize traditional metadata and dsse w/ common interface |
| 81 | + metadata = [] |
| 82 | + for object_ in [Metadata(root), Envelope.from_signed(root)]: |
| 83 | + object_.sign(signer) |
| 84 | + metadata.append((type(object_), object_.to_bytes())) |
| 85 | + |
| 86 | + # Deserialize and verify both metadata types w/ common interface |
| 87 | + for type_, bytes_ in metadata: |
| 88 | + object_ = BaseMetadata.from_bytes(bytes_) |
| 89 | + object_.verify_delegate(Root.type, object_) |
| 90 | + # Assert we get the correct instance |
| 91 | + self.assertTrue(isinstance(object_, type_)) |
| 92 | + |
| 93 | + |
51 | 94 | # pylint: disable=too-many-public-methods
|
52 | 95 | class TestMetadata(unittest.TestCase):
|
53 | 96 | """Tests for public API of all classes in 'tuf/api/metadata.py'."""
|
|
0 commit comments