-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martin Vrachev <[email protected]>
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |