Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Signed-off-by: E3E <[email protected]>
  • Loading branch information
NicholasTanz committed Mar 13, 2024
1 parent a3fbb7a commit fa7c92d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions securesystemslib/dsse.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def to_dict(self) -> dict:
"""Returns the JSON-serializable dictionary representation of self."""

signatures = []
for signature in list(self.signatures.values()):
for signature in self.signatures.values():
sig_dict = signature.to_dict()
sig_dict["sig"] = b64enc(bytes.fromhex(sig_dict["sig"]))
signatures.append(sig_dict)
Expand Down Expand Up @@ -148,7 +148,7 @@ def verify(self, keys: List[Key], threshold: int) -> Dict[str, Key]:
if len(keys) < threshold:
raise ValueError("Number of keys can't be less than threshold")

for signature in list(self.signatures.values()):
for signature in self.signatures.values():
for key in keys:
# If Signature keyid doesn't match with Key, skip.
if not key.keyid == signature.keyid:
Expand Down
18 changes: 16 additions & 2 deletions tests/test_dsse.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,28 @@ def setUpClass(cls):
}
cls.pae = b"DSSEv1 29 http://example.com/HelloWorld 11 hello world"

def test_envelope_from_dict_with_duplicate_signatures(self):
"""Test envelope from_dict generates error with duplicate signature keyids"""
envelope_dict = copy.deepcopy(self.envelope_dict)

# add duplicate keyid.
envelope_dict["signatures"].append(copy.deepcopy(self.signature_dict))

# assert that calling from_dict will raise an error.
expected_error_message = f"Multiple signatures found for keyid {self.signature_dict['keyid']}"
with self.assertRaises(ValueError) as context:
Envelope.from_dict(envelope_dict)

self.assertEqual(str(context.exception), expected_error_message)

def test_envelope_from_to_dict(self):
"""Test envelope to_dict and from_dict methods."""

envelope_dict = copy.deepcopy(self.envelope_dict)

# create envelope object from its dict.
envelope_obj = Envelope.from_dict(envelope_dict)
for signature in list(envelope_obj.signatures.values()):
for signature in envelope_obj.signatures.values():
self.assertIsInstance(signature, Signature)

# Assert envelope dict created by to_dict will be equal.
Expand Down Expand Up @@ -105,7 +119,7 @@ def test_sign_and_verify(self):

# Check for signatures of Envelope.
self.assertEqual(len(self.key_dicts), len(envelope_obj.signatures))
for signature in list(envelope_obj.signatures.values()):
for signature in envelope_obj.signatures.values():
self.assertIsInstance(signature, Signature)

# Test for invalid threshold value for keys_list.
Expand Down

0 comments on commit fa7c92d

Please sign in to comment.