diff --git a/tests/test_signer.py b/tests/test_signer.py index b0bc16d9..bae7b506 100644 --- a/tests/test_signer.py +++ b/tests/test_signer.py @@ -20,10 +20,10 @@ import unittest import securesystemslib.formats import securesystemslib.keys as KEYS -from securesystemslib.signer import SSlibSigner +from securesystemslib.signer import Signature, SSlibSigner +import securesystemslib.exceptions as exceptions - -class TestSSlibSigner(unittest.TestCase):\ +class TestSSlibSigner(unittest.TestCase): @classmethod def setUpClass(cls): @@ -38,63 +38,48 @@ def setUpClass(cls): def test_sslib_sign(self): - # Test generation of RSA signatures. - sslib_signer = SSlibSigner(self.rsakey_dict) - rsa_sig_obj = sslib_signer.sign(self.DATA) - - # Verify rsa signature - verified = KEYS.verify_signature(self.rsakey_dict, - rsa_sig_obj.to_dict(), self.DATA) - - # Verify rsa signature - verified = KEYS.verify_signature(self.rsakey_dict, - rsa_sig_obj.to_dict(), self.DATA) - self.assertTrue(verified, "Incorrect signature.") - - # Test for invalid signature scheme. - valid_scheme = self.rsakey_dict['scheme'] - self.rsakey_dict['scheme'] = 'invalid_scheme' - sslib_signer.key_dict = self.rsakey_dict + for scheme_dict in [self.rsakey_dict, self.ecdsakey_dict]: + # Test generation of signatures. + sslib_signer = SSlibSigner(scheme_dict) + sig_obj = sslib_signer.sign(self.DATA) - self.assertRaises(securesystemslib.exceptions.UnsupportedAlgorithmError, - sslib_signer.sign, self.DATA) - self.rsakey_dict['scheme'] = valid_scheme + # Verify signature + verified = KEYS.verify_signature(scheme_dict, sig_obj.to_dict(), self.DATA) + self.assertTrue(verified, "Incorrect signature.") - # Removing private key from 'rsakey_dict' - should raise a TypeError. - private = self.rsakey_dict['keyval']['private'] - self.rsakey_dict['keyval']['private'] = '' - sslib_signer.key_dict = self.rsakey_dict + # Removing private key from 'scheme_dict' - should raise a ValueError. + private = scheme_dict['keyval']['private'] + scheme_dict['keyval']['private'] = '' + sslib_signer.key_dict = scheme_dict - self.assertRaises(ValueError, sslib_signer.sign, self.DATA) + with self.assertRaises(ValueError): + sslib_signer.sign(self.DATA) - # Supplying an incorrect number of arguments. - self.assertRaises(TypeError, KEYS.create_signature) - self.rsakey_dict['keyval']['private'] = private + scheme_dict['keyval']['private'] = private - # Test generation of ECDSA signatures. + # Supplying an incorrect number of arguments. + with self.assertRaises(TypeError): + sslib_signer.sign() - sslib_signer.key_dict = self.ecdsakey_dict - # Creating a signature for 'DATA'. - ecdsa_sig = sslib_signer.sign(self.DATA) + # Test for invalid signature scheme. + valid_scheme = self.rsakey_dict['scheme'] + self.rsakey_dict['scheme'] = 'invalid_scheme' + sslib_signer = SSlibSigner(self.rsakey_dict) - # Verify rsa signature - verified = KEYS.verify_signature(self.ecdsakey_dict, - ecdsa_sig.to_dict(), self.DATA) + with self.assertRaises(exceptions.UnsupportedAlgorithmError): + sslib_signer.sign(self.DATA) - # Verify rsa signature - verified = KEYS.verify_signature(self.ecdsakey_dict, - ecdsa_sig.to_dict(), self.DATA) - self.assertTrue(verified, "Incorrect signature.") + self.rsakey_dict['scheme'] = valid_scheme - # Removing private key from 'ecdsakey_dict' - should raise a TypeError. - private = self.ecdsakey_dict['keyval']['private'] - self.ecdsakey_dict['keyval']['private'] = '' - self.assertRaises(ValueError, sslib_signer.sign, self.DATA) + def test_signature_from_to_json(self): + signature_dict = { + "sig": "30460221009342e4566528fcecf6a7a5d53ebacdb1df151e242f55f8775883469cb01dbc6602210086b426cc826709acfa2c3f9214610cb0a832db94bbd266fd7c5939a48064a851", + "keyid": "11fa391a0ed7a447cbfeb4b2667e286fc248f64d5e6d0eeed2e5e23f97f9f714" + } + sig_obj = Signature.from_dict(signature_dict) - # Supplying an incorrect number of arguments. - self.assertRaises(TypeError, sslib_signer.sign) - self.ecdsakey_dict['keyval']['private'] = private + self.assertEqual(signature_dict, sig_obj.to_dict()) # Run the unit tests.