Skip to content

Commit

Permalink
Merge pull request #383 from MVrachev/signature-eq
Browse files Browse the repository at this point in the history
Add Signature.__eq__() method
  • Loading branch information
lukpueh authored Feb 8, 2022
2 parents 812fff2 + d5cc660 commit cb72257
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion securesystemslib/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import abc
import securesystemslib.keys as sslib_keys
from typing import Dict
from typing import Any, Dict


class Signature:
Expand All @@ -29,6 +29,13 @@ def __init__(self, keyid: str, sig: str):
self.signature = sig


def __eq__(self, other: Any) -> bool:
if not isinstance(other, Signature):
return False

return self.keyid == other.keyid and self.signature == other.signature


@classmethod
def from_dict(cls, signature_dict: Dict) -> "Signature":
"""Creates a Signature object from its JSON/dict representation.
Expand Down
25 changes: 25 additions & 0 deletions tests/test_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"""Test cases for "signer.py". """

import copy
import sys
import unittest

Expand Down Expand Up @@ -67,6 +68,30 @@ def test_signature_from_to_dict(self):
self.assertDictEqual(signature_dict, sig_obj.to_dict())


def test_signature_eq_(self):
signature_dict = {
"sig": "30460221009342e4566528fcecf6a7a5d53ebacdb1df151e242f55f8775883469cb01dbc6602210086b426cc826709acfa2c3f9214610cb0a832db94bbd266fd7c5939a48064a851",
"keyid": "11fa391a0ed7a447cbfeb4b2667e286fc248f64d5e6d0eeed2e5e23f97f9f714"
}
sig_obj = Signature.from_dict(signature_dict)
sig_obj_2 = copy.deepcopy(sig_obj)

self.assertEqual(sig_obj, sig_obj_2)

# Assert that changing the keyid will make the objects not equal.
sig_obj_2.keyid = None
self.assertNotEqual(sig_obj, sig_obj_2)
sig_obj_2.keyid = sig_obj.keyid

# Assert that changing the signature will make the objects not equal.
sig_obj_2.signature = None
self.assertNotEqual(sig_obj, sig_obj_2)

# Assert that making sig_obj_2 None will make the objects not equal.
sig_obj_2 = None
self.assertNotEqual(sig_obj, sig_obj_2)


# Run the unit tests.
if __name__ == "__main__":
unittest.main()

0 comments on commit cb72257

Please sign in to comment.