Skip to content

Commit d00af4c

Browse files
author
Jussi Kukkonen
committed
tests: Improve verify_delegate() tests
Make sure verify_delegate() succeeds when threshold is reached even if some signatures fail to verify. Make sure higher threshold (2/2) works. Change error type for "Call is valid only on delegator metadata" error. Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent 37a4d41 commit d00af4c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

tests/test_api.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
)
5252

5353
from securesystemslib.signer import (
54-
SSlibSigner
54+
SSlibSigner,
55+
Signature
5556
)
5657

5758
logger = logging.getLogger(__name__)
@@ -357,7 +358,7 @@ def test_metadata_verify_delegate(self):
357358
role1.verify_delegate('role2', role2)
358359

359360
# only root and targets can verify delegates
360-
with self.assertRaises(ValueError):
361+
with self.assertRaises(TypeError):
361362
snapshot.verify_delegate('snapshot', snapshot)
362363
# verify fails for roles that are not delegated by delegator
363364
with self.assertRaises(ValueError):
@@ -376,12 +377,25 @@ def test_metadata_verify_delegate(self):
376377
with self.assertRaises(exceptions.UnsignedMetadataError):
377378
root.verify_delegate('timestamp', snapshot)
378379

380+
# Add a key to snapshot role, make sure the new sig fails to verify
381+
ts_keyid = next(iter(root.signed.roles["timestamp"].keyids))
382+
root.signed.add_key("snapshot", root.signed.keys[ts_keyid])
383+
snapshot.signatures[ts_keyid] = Signature(ts_keyid, "ff"*64)
384+
385+
# verify succeeds if threshold is reached even if some signatures
386+
# fail to verify
387+
root.verify_delegate('snapshot', snapshot)
388+
379389
# verify fails if threshold of signatures is not reached
380390
root.signed.roles['snapshot'].threshold = 2
381391
with self.assertRaises(exceptions.UnsignedMetadataError):
382392
root.verify_delegate('snapshot', snapshot)
383393

384-
# TODO test successful verify with higher thresholds
394+
# verify succeeds when we correct the new signature and reach the
395+
# threshold of 2 keys
396+
snapshot.sign(SSlibSigner(self.keystore['timestamp']), append=True)
397+
root.verify_delegate('snapshot', snapshot)
398+
385399

386400
def test_key_class(self):
387401
keys = {

tuf/api/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def verify_delegate(
298298
roles = self.signed.delegations.roles
299299
role = next((r for r in roles if r.name == role_name), None)
300300
else:
301-
raise ValueError("Call is valid only on delegator metadata")
301+
raise TypeError("Call is valid only on delegator metadata")
302302

303303
if role is None:
304304
raise ValueError(f"No delegation found for {role_name}")

0 commit comments

Comments
 (0)