From d608a3f7e0599c6909b85087d26f4ecee6011f28 Mon Sep 17 00:00:00 2001 From: Martin Vrachev Date: Thu, 4 Feb 2021 18:13:55 +0200 Subject: [PATCH] Add tests for SSlib Signer Signed-off-by: Martin Vrachev --- tests/test_signer.py | 88 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/test_signer.py diff --git a/tests/test_signer.py b/tests/test_signer.py new file mode 100644 index 000000000..3cb644bd6 --- /dev/null +++ b/tests/test_signer.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python + +""" +Program Name: + test_signer.py + +Author: + Martin Vrachev + +Started: + February, 04 2021 + +Copyright + See LICENSE for licensing information. + +Purpose: + Test cases for 'signer.py'. +""" + +import unittest +import securesystemslib.formats +import securesystemslib.keys as KEYS +from securesystemslib.signer import Signature, SSlibSigner +from securesystemslib.exceptions import FormatError, UnsupportedAlgorithmError + +class TestSSlibSigner(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.rsakey_dict = KEYS.generate_rsa_key() + cls.ed25519key_dict = KEYS.generate_ed25519_key() + cls.ecdsakey_dict = KEYS.generate_ecdsa_key() + cls.DATA_STR = 'SOME DATA REQUIRING AUTHENTICITY.' + cls.DATA = securesystemslib.formats.encode_canonical( + cls.DATA_STR).encode('utf-8') + + + def test_sslib_sign(self): + dicts = [self.rsakey_dict, self.ecdsakey_dict, self.ed25519key_dict] + for scheme_dict in dicts: + # Test generation of signatures. + sslib_signer = SSlibSigner(scheme_dict) + sig_obj = sslib_signer.sign(self.DATA) + + # Verify signature + verified = KEYS.verify_signature(scheme_dict, sig_obj.to_dict(), + self.DATA) + self.assertTrue(verified, "Incorrect signature.") + + # 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 + + with self.assertRaises((ValueError, FormatError)): + sslib_signer.sign(self.DATA) + + scheme_dict['keyval']['private'] = private + + # Test for invalid signature scheme. + valid_scheme = scheme_dict['scheme'] + scheme_dict['scheme'] = 'invalid_scheme' + sslib_signer = SSlibSigner(scheme_dict) + + with self.assertRaises((UnsupportedAlgorithmError, FormatError)): + sslib_signer.sign(self.DATA) + + scheme_dict['scheme'] = valid_scheme + + # Supplying an incorrect number of arguments. + with self.assertRaises(TypeError): + sslib_signer.sign() + + + + def test_signature_from_to_json(self): + signature_dict = { + "sig": "30460221009342e4566528fcecf6a7a5d53ebacdb1df151e242f55f8775883469cb01dbc6602210086b426cc826709acfa2c3f9214610cb0a832db94bbd266fd7c5939a48064a851", + "keyid": "11fa391a0ed7a447cbfeb4b2667e286fc248f64d5e6d0eeed2e5e23f97f9f714" + } + sig_obj = Signature.from_dict(signature_dict) + + self.assertEqual(signature_dict, sig_obj.to_dict()) + + +# Run the unit tests. +if __name__ == '__main__': + unittest.main()