Skip to content

Commit

Permalink
Add tests for SSlib Signer
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Vrachev <[email protected]>
  • Loading branch information
MVrachev committed Feb 9, 2021
1 parent 919b3d2 commit 0285e39
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions tests/test_signer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python

"""
<Program Name>
test_signer.py
<Author>
Martin Vrachev <[email protected]>
<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 SSlibSigner


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')
cls.FORMAT_ERROR_MSG = 'securesystemslib.exceptions.FormatError was raised!' + \
' Check object\'s format.'


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

self.assertRaises(securesystemslib.exceptions.UnsupportedAlgorithmError,
sslib_signer.sign, self.DATA)
self.rsakey_dict['scheme'] = valid_scheme

# 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

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

# Test generation of ECDSA signatures.

sslib_signer.key_dict = self.ecdsakey_dict
# Creating a signature for 'DATA'.
ecdsa_sig = sslib_signer.sign(self.DATA)

# Verify rsa signature
verified = KEYS.verify_signature(self.ecdsakey_dict,
ecdsa_sig.to_dict(), self.DATA)

# Verify rsa signature
verified = KEYS.verify_signature(self.ecdsakey_dict,
ecdsa_sig.to_dict(), self.DATA)
self.assertTrue(verified, "Incorrect signature.")

# 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)

# Supplying an incorrect number of arguments.
self.assertRaises(TypeError, sslib_signer.sign)
self.ecdsakey_dict['keyval']['private'] = private


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

0 comments on commit 0285e39

Please sign in to comment.