Skip to content

Commit 2bc97c4

Browse files
committed
tests: add dsse smoke test
Test Envelope (dsse) and common metadata abstraction for exemplary root metadata: sign, verify, verify_delegate. Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
1 parent 9beb8a3 commit 2bc97c4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/test_api.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
from tuf.api import exceptions
3131
from tuf.api.metadata import (
3232
TOP_LEVEL_ROLE_NAMES,
33+
BaseMetadata,
3334
DelegatedRole,
3435
Delegations,
36+
Envelope,
3537
Key,
3638
Metadata,
3739
Root,
@@ -48,6 +50,47 @@
4850
logger = logging.getLogger(__name__)
4951

5052

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+
5194
# pylint: disable=too-many-public-methods
5295
class TestMetadata(unittest.TestCase):
5396
"""Tests for public API of all classes in 'tuf/api/metadata.py'."""

0 commit comments

Comments
 (0)