diff --git a/kmip/pie/objects.py b/kmip/pie/objects.py index d11c4a5..7da215b 100644 --- a/kmip/pie/objects.py +++ b/kmip/pie/objects.py @@ -16,11 +16,10 @@ from abc import ABCMeta from abc import abstractmethod +import binascii import six -from kmip.core.enums import CryptographicAlgorithm -from kmip.core.enums import CryptographicUsageMask -from kmip.core.enums import ObjectType +from kmip.core import enums @six.add_metaclass(ABCMeta) @@ -168,10 +167,12 @@ class Key(CryptographicObject): cryptographic_algorithm: A CryptographicAlgorithm enumeration defining the algorithm the key should be used with. cryptographic_length: An int defining the length of the key in bits. + key_format_type: A KeyFormatType enumeration defining the format of + the key value. """ @abstractmethod - def __init__(self, value=None, algorithm=None, length=None): + def __init__(self): """ Create a Key object. """ @@ -179,6 +180,7 @@ class Key(CryptographicObject): self.cryptographic_algorithm = None self.cryptographic_length = None + self.key_format_type = None # All remaining attributes are not considered part of the public API # and are subject to change. @@ -201,6 +203,7 @@ class SymmetricKey(Key): cryptographic_algorithm: The type of algorithm for the SymmetricKey. cryptographic_length: The length in bits of the SymmetricKey value. value: The bytes of the SymmetricKey. + key_format_type: The format of the key value. cryptographic_usage_masks: The list of usage mask flags for SymmetricKey application. names: The string names of the SymmetricKey. @@ -217,12 +220,14 @@ class SymmetricKey(Key): length(int): The length in bits of the key. value(bytes): The bytes representing the key. masks(list): A list of CryptographicUsageMask enumerations defining - how the key will be used. - name(string): The string name of the key. + how the key will be used. Optional, defaults to None. + name(string): The string name of the key. Optional, defaults to + 'Symmetric Key'. """ super(SymmetricKey, self).__init__() - self._object_type = ObjectType.SYMMETRIC_KEY + self._object_type = enums.ObjectType.SYMMETRIC_KEY + self.key_format_type = enums.KeyFormatType.RAW self.value = value self.cryptographic_algorithm = algorithm @@ -255,7 +260,7 @@ class SymmetricKey(Key): if not isinstance(self.value, bytes): raise TypeError("key value must be bytes") elif not isinstance(self.cryptographic_algorithm, - CryptographicAlgorithm): + enums.CryptographicAlgorithm): raise TypeError("key algorithm must be a CryptographicAlgorithm " "enumeration") elif not isinstance(self.cryptographic_length, six.integer_types): @@ -266,7 +271,7 @@ class SymmetricKey(Key): mask_count = len(self.cryptographic_usage_masks) for i in range(mask_count): mask = self.cryptographic_usage_masks[i] - if not isinstance(mask, CryptographicUsageMask): + if not isinstance(mask, enums.CryptographicUsageMask): position = "({0} in list)".format(i) raise TypeError( "key mask {0} must be a CryptographicUsageMask " @@ -288,12 +293,12 @@ class SymmetricKey(Key): def __repr__(self): algorithm = "algorithm={0}".format(self.cryptographic_algorithm) length = "length={0}".format(self.cryptographic_length) - value = "value={0}".format(self.value) + value = "value={0}".format(binascii.hexlify(self.value)) return "SymmetricKey({0}, {1}, {2})".format(algorithm, length, value) def __str__(self): - return str(self.value) + return str(binascii.hexlify(self.value)) def __eq__(self, other): if isinstance(other, SymmetricKey): @@ -315,7 +320,7 @@ class SymmetricKey(Key): return NotImplemented -class PublicKey(CryptographicObject): +class PublicKey(Key): """ The PublicKey class of the simplified KMIP object hierarchy. @@ -327,12 +332,14 @@ class PublicKey(CryptographicObject): cryptographic_algorithm: The type of algorithm for the PublicKey. cryptographic_length: The length in bits of the PublicKey. value: The bytes of the PublicKey. + key_format_type: The format of the key value. cryptographic_usage_masks: The list of usage mask flags for PublicKey application. names: The list of string names of the PublicKey. """ - def __init__(self, algorithm, length, value, masks=None, + def __init__(self, algorithm, length, value, + format_type=enums.KeyFormatType.X_509, masks=None, name='Public Key'): """ Create a PublicKey. @@ -342,17 +349,25 @@ class PublicKey(CryptographicObject): type of algorithm for the key. length(int): The length in bits of the key. value(bytes): The bytes representing the key. + format_type(KeyFormatType): An enumeration defining the format of + the key value. Optional, defaults to enums.KeyFormatType.X_509. masks(list): A list of CryptographicUsageMask enumerations - defining how the key will be used. - name(string): The string name of the key. + defining how the key will be used. Optional, defaults to None. + name(string): The string name of the key. Optional, defaults to + 'Public Key'. """ super(PublicKey, self).__init__() - self._object_type = ObjectType.PUBLIC_KEY + self._object_type = enums.ObjectType.PUBLIC_KEY + self._valid_formats = [ + enums.KeyFormatType.RAW, + enums.KeyFormatType.X_509, + enums.KeyFormatType.PKCS_1] self.value = value self.cryptographic_algorithm = algorithm self.cryptographic_length = length + self.key_format_type = format_type self.names = [name] if masks: @@ -379,18 +394,26 @@ class PublicKey(CryptographicObject): if not isinstance(self.value, bytes): raise TypeError("key value must be bytes") elif not isinstance(self.cryptographic_algorithm, - CryptographicAlgorithm): + enums.CryptographicAlgorithm): raise TypeError("key algorithm must be a CryptographicAlgorithm " "enumeration") elif not isinstance(self.cryptographic_length, six.integer_types): raise TypeError("key length must be an integer") + elif not isinstance(self.key_format_type, enums.KeyFormatType): + raise TypeError("key format type must be a KeyFormatType " + "enumeration") + elif self.key_format_type not in self._valid_formats: + raise ValueError("key format type must be one of {0}".format( + self._valid_formats)) elif not isinstance(self.cryptographic_usage_masks, list): raise TypeError("key usage masks must be a list") + # TODO (peter-hamilton) Verify that the key bytes match the key format + mask_count = len(self.cryptographic_usage_masks) for i in range(mask_count): mask = self.cryptographic_usage_masks[i] - if not isinstance(mask, CryptographicUsageMask): + if not isinstance(mask, enums.CryptographicUsageMask): position = "({0} in list)".format(i) raise TypeError( "key mask {0} must be a CryptographicUsageMask " @@ -407,17 +430,21 @@ class PublicKey(CryptographicObject): def __repr__(self): algorithm = "algorithm={0}".format(self.cryptographic_algorithm) length = "length={0}".format(self.cryptographic_length) - value = "value={0}".format(self.value) + value = "value={0}".format(binascii.hexlify(self.value)) + format_type = "format_type={0}".format(self.key_format_type) - return "PublicKey({0}, {1}, {2})".format(algorithm, length, value) + return "PublicKey({0}, {1}, {2}, {3})".format( + algorithm, length, value, format_type) def __str__(self): - return str(self.value) + return str(binascii.hexlify(self.value)) def __eq__(self, other): if isinstance(other, PublicKey): if self.value != other.value: return False + elif self.key_format_type != other.key_format_type: + return False elif self.cryptographic_algorithm != other.cryptographic_algorithm: return False elif self.cryptographic_length != other.cryptographic_length: @@ -434,7 +461,7 @@ class PublicKey(CryptographicObject): return NotImplemented -class PrivateKey(CryptographicObject): +class PrivateKey(Key): """ The PrivateKey class of the simplified KMIP object hierarchy. @@ -446,12 +473,14 @@ class PrivateKey(CryptographicObject): cryptographic_algorithm: The type of algorithm for the PrivateKey. cryptographic_length: The length in bits of the PrivateKey. value: The bytes of the PrivateKey. + key_format_type: The format of the key value. cryptographic_usage_masks: The list of usage mask flags for PrivateKey - application. - names: The list of string names of the PrivateKey. + application. Optional, defaults to None. + names: The list of string names of the PrivateKey. Optional, defaults + to 'Private Key'. """ - def __init__(self, algorithm, length, value, masks=None, + def __init__(self, algorithm, length, value, format_type, masks=None, name='Private Key'): """ Create a PrivateKey. @@ -461,17 +490,24 @@ class PrivateKey(CryptographicObject): type of algorithm for the key. length(int): The length in bits of the key. value(bytes): The bytes representing the key. + format_type(KeyFormatType): An enumeration defining the format of + the key value. masks(list): A list of CryptographicUsageMask enumerations defining how the key will be used. name(string): The string name of the key. """ super(PrivateKey, self).__init__() - self._object_type = ObjectType.PRIVATE_KEY + self._object_type = enums.ObjectType.PRIVATE_KEY + self._valid_formats = [ + enums.KeyFormatType.RAW, + enums.KeyFormatType.PKCS_1, + enums.KeyFormatType.PKCS_8] self.value = value self.cryptographic_algorithm = algorithm self.cryptographic_length = length + self.key_format_type = format_type self.names = [name] if masks: @@ -498,18 +534,26 @@ class PrivateKey(CryptographicObject): if not isinstance(self.value, bytes): raise TypeError("key value must be bytes") elif not isinstance(self.cryptographic_algorithm, - CryptographicAlgorithm): + enums.CryptographicAlgorithm): raise TypeError("key algorithm must be a CryptographicAlgorithm " "enumeration") elif not isinstance(self.cryptographic_length, six.integer_types): raise TypeError("key length must be an integer") + elif not isinstance(self.key_format_type, enums.KeyFormatType): + raise TypeError("key format type must be a KeyFormatType " + "enumeration") + elif self.key_format_type not in self._valid_formats: + raise ValueError("key format type must be one of {0}".format( + self._valid_formats)) elif not isinstance(self.cryptographic_usage_masks, list): raise TypeError("key usage masks must be a list") + # TODO (peter-hamilton) Verify that the key bytes match the key format + mask_count = len(self.cryptographic_usage_masks) for i in range(mask_count): mask = self.cryptographic_usage_masks[i] - if not isinstance(mask, CryptographicUsageMask): + if not isinstance(mask, enums.CryptographicUsageMask): position = "({0} in list)".format(i) raise TypeError( "key mask {0} must be a CryptographicUsageMask " @@ -526,17 +570,21 @@ class PrivateKey(CryptographicObject): def __repr__(self): algorithm = "algorithm={0}".format(self.cryptographic_algorithm) length = "length={0}".format(self.cryptographic_length) - value = "value={0}".format(self.value) + value = "value={0}".format(binascii.hexlify(self.value)) + format_type = "format_type={0}".format(self.key_format_type) - return "PrivateKey({0}, {1}, {2})".format(algorithm, length, value) + return "PrivateKey({0}, {1}, {2}, {3})".format( + algorithm, length, value, format_type) def __str__(self): - return str(self.value) + return str(binascii.hexlify(self.value)) def __eq__(self, other): if isinstance(other, PrivateKey): if self.value != other.value: return False + elif self.key_format_type != other.key_format_type: + return False elif self.cryptographic_algorithm != other.cryptographic_algorithm: return False elif self.cryptographic_length != other.cryptographic_length: diff --git a/kmip/tests/unit/pie/objects/test_private_key.py b/kmip/tests/unit/pie/objects/test_private_key.py index 0d0996e..d94acfb 100644 --- a/kmip/tests/unit/pie/objects/test_private_key.py +++ b/kmip/tests/unit/pie/objects/test_private_key.py @@ -27,16 +27,14 @@ # License for the specific language governing permissions and limitations # under the License. -from testtools import TestCase +import binascii +import testtools -from kmip.core.enums import CryptographicAlgorithm -from kmip.core.enums import CryptographicUsageMask -from kmip.core.enums import ObjectType - -from kmip.pie.objects import PrivateKey +from kmip.core import enums +from kmip.pie import objects -class TestPrivateKey(TestCase): +class TestPrivateKey(testtools.TestCase): """ Test suite for PrivateKey. """ @@ -170,12 +168,15 @@ class TestPrivateKey(TestCase): """ Test that a PrivateKey object can be instantiated. """ - key = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + key = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) - self.assertEqual(key.cryptographic_algorithm, - CryptographicAlgorithm.RSA) + self.assertEqual( + key.cryptographic_algorithm, enums.CryptographicAlgorithm.RSA) self.assertEqual(key.cryptographic_length, 1024) self.assertEqual(key.value, self.bytes_1024) + self.assertEqual(key.key_format_type, enums.KeyFormatType.PKCS_8) self.assertEqual(key.cryptographic_usage_masks, list()) self.assertEqual(key.names, ['Private Key']) @@ -183,29 +184,33 @@ class TestPrivateKey(TestCase): """ Test that a PrivateKey object can be instantiated with all arguments. """ - key = PrivateKey( - CryptographicAlgorithm.RSA, + key = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, - masks=[CryptographicUsageMask.ENCRYPT, - CryptographicUsageMask.DECRYPT], + enums.KeyFormatType.PKCS_8, + masks=[enums.CryptographicUsageMask.ENCRYPT, + enums.CryptographicUsageMask.DECRYPT], name='Test Private Key') self.assertEqual(key.cryptographic_algorithm, - CryptographicAlgorithm.RSA) + enums.CryptographicAlgorithm.RSA) self.assertEqual(key.cryptographic_length, 1024) self.assertEqual(key.value, self.bytes_1024) + self.assertEqual(key.key_format_type, enums.KeyFormatType.PKCS_8) self.assertEqual(key.cryptographic_usage_masks, - [CryptographicUsageMask.ENCRYPT, - CryptographicUsageMask.DECRYPT]) + [enums.CryptographicUsageMask.ENCRYPT, + enums.CryptographicUsageMask.DECRYPT]) self.assertEqual(key.names, ['Test Private Key']) def test_get_object_type(self): """ Test that the object type can be retrieved from the PrivateKey. """ - expected = ObjectType.PRIVATE_KEY - key = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + expected = enums.ObjectType.PRIVATE_KEY + key = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) observed = key.object_type self.assertEqual(expected, observed) @@ -214,59 +219,85 @@ class TestPrivateKey(TestCase): Test that a TypeError is raised when an invalid algorithm value is used to construct a PrivateKey. """ - args = ('invalid', 1024, self.bytes_1024) - self.assertRaises(TypeError, PrivateKey, *args) + args = ('invalid', 1024, self.bytes_1024, enums.KeyFormatType.PKCS_8) + self.assertRaises(TypeError, objects.PrivateKey, *args) def test_validate_on_invalid_length(self): """ Test that a TypeError is raised when an invalid length value is used to construct a PrivateKey. """ - args = (CryptographicAlgorithm.RSA, 'invalid', self.bytes_1024) - self.assertRaises(TypeError, PrivateKey, *args) + args = (enums.CryptographicAlgorithm.RSA, 'invalid', self.bytes_1024, + enums.KeyFormatType.PKCS_8) + self.assertRaises(TypeError, objects.PrivateKey, *args) def test_validate_on_invalid_value(self): """ Test that a TypeError is raised when an invalid value is used to construct a PrivateKey. """ - args = (CryptographicAlgorithm.RSA, 1024, 0) - self.assertRaises(TypeError, PrivateKey, *args) + args = (enums.CryptographicAlgorithm.RSA, 1024, 0, + enums.KeyFormatType.PKCS_8) + self.assertRaises(TypeError, objects.PrivateKey, *args) + + def test_validate_on_invalid_format_type(self): + """ + Test that a TypeError is raised when an invalid value is used to + construct a PrivateKey. + """ + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + 'invalid') + self.assertRaises(TypeError, objects.PrivateKey, *args) + + def test_validate_on_invalid_format_type_value(self): + """ + Test that a ValueError is raised when an invalid format type is used to + construct a PrivateKey. + """ + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.OPAQUE) + self.assertRaises(ValueError, objects.PrivateKey, *args) def test_validate_on_invalid_masks(self): """ Test that a TypeError is raised when an invalid masks value is used to construct a PrivateKey. """ - args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) kwargs = {'masks': 'invalid'} - self.assertRaises(TypeError, PrivateKey, *args, **kwargs) + self.assertRaises(TypeError, objects.PrivateKey, *args, **kwargs) def test_validate_on_invalid_mask(self): """ Test that a TypeError is raised when an invalid mask value is used to construct a PrivateKey. """ - args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) kwargs = {'masks': ['invalid']} - self.assertRaises(TypeError, PrivateKey, *args, **kwargs) + self.assertRaises(TypeError, objects.PrivateKey, *args, **kwargs) def test_validate_on_invalid_name(self): """ Test that a TypeError is raised when an invalid name value is used to construct a PrivateKey. """ - args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) kwargs = {'name': 0} - self.assertRaises(TypeError, PrivateKey, *args, **kwargs) + self.assertRaises(TypeError, objects.PrivateKey, *args, **kwargs) def test_repr(self): """ Test that repr can be applied to a PrivateKey. """ - key = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - args = "algorithm={0}, length={1}, value={2}".format( - CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + key = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) + args = "algorithm={0}, length={1}, value={2}, format_type={3}".format( + enums.CryptographicAlgorithm.RSA, 1024, + binascii.hexlify(self.bytes_1024), enums.KeyFormatType.PKCS_8) expected = "PrivateKey({0})".format(args) observed = repr(key) self.assertEqual(expected, observed) @@ -275,8 +306,10 @@ class TestPrivateKey(TestCase): """ Test that str can be applied to a PrivateKey. """ - key = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - expected = str(self.bytes_1024) + key = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) + expected = str(binascii.hexlify(self.bytes_1024)) observed = str(key) self.assertEqual(expected, observed) @@ -285,8 +318,12 @@ class TestPrivateKey(TestCase): Test that the equality operator returns True when comparing two PrivateKey objects with the same data. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - b = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) self.assertTrue(a == b) self.assertTrue(b == a) @@ -295,8 +332,12 @@ class TestPrivateKey(TestCase): Test that the equality operator returns False when comparing two PrivateKey objects with different data. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - b = PrivateKey(CryptographicAlgorithm.AES, 1024, self.bytes_1024) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.AES, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) self.assertFalse(a == b) self.assertFalse(b == a) @@ -305,8 +346,12 @@ class TestPrivateKey(TestCase): Test that the equality operator returns False when comparing two PrivateKey objects with different data. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - b = PrivateKey(CryptographicAlgorithm.RSA, 2048, self.bytes_1024) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_1024, + enums.KeyFormatType.PKCS_8) self.assertFalse(a == b) self.assertFalse(b == a) @@ -315,8 +360,26 @@ class TestPrivateKey(TestCase): Test that the equality operator returns False when comparing two PrivateKey objects with different data. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - b = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_2048) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_2048, + enums.KeyFormatType.PKCS_8) + self.assertFalse(a == b) + self.assertFalse(b == a) + + def test_equal_on_not_equal_format_type(self): + """ + Test that the equality operator returns False when comparing two + PrivateKey objects with different data. + """ + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_1) self.assertFalse(a == b) self.assertFalse(b == a) @@ -325,7 +388,9 @@ class TestPrivateKey(TestCase): Test that the equality operator returns False when comparing a PrivateKey object to a non-PrivateKey object. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) b = "invalid" self.assertFalse(a == b) self.assertFalse(b == a) @@ -335,8 +400,12 @@ class TestPrivateKey(TestCase): Test that the inequality operator returns False when comparing two PrivateKey objects with the same internal data. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) - b = PrivateKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) self.assertFalse(a != b) self.assertFalse(b != a) @@ -345,8 +414,12 @@ class TestPrivateKey(TestCase): Test that the equality operator returns True when comparing two PrivateKey objects with different data. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) - b = PrivateKey(CryptographicAlgorithm.AES, 2048, self.bytes_2048) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.AES, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) self.assertTrue(a != b) self.assertTrue(b != a) @@ -355,8 +428,12 @@ class TestPrivateKey(TestCase): Test that the equality operator returns True when comparing two PrivateKey objects with different data. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) - b = PrivateKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_8) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_8) self.assertTrue(a != b) self.assertTrue(b != a) @@ -365,8 +442,26 @@ class TestPrivateKey(TestCase): Test that the equality operator returns True when comparing two PrivateKey objects with different data. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) - b = PrivateKey(CryptographicAlgorithm.RSA, 2048, self.bytes_1024) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_8) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_1024, + enums.KeyFormatType.PKCS_8) + self.assertTrue(a != b) + self.assertTrue(b != a) + + def test_not_equal_on_not_equal_format_type(self): + """ + Test that the equality operator returns True when comparing two + PrivateKey objects with different data. + """ + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_8) + b = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) self.assertTrue(a != b) self.assertTrue(b != a) @@ -375,7 +470,9 @@ class TestPrivateKey(TestCase): Test that the equality operator returns True when comparing a PrivateKey object to a non-PrivateKey object. """ - a = PrivateKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) + a = objects.PrivateKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) b = "invalid" self.assertTrue(a != b) self.assertTrue(b != a) diff --git a/kmip/tests/unit/pie/objects/test_public_key.py b/kmip/tests/unit/pie/objects/test_public_key.py index 08f2d06..ee1e119 100644 --- a/kmip/tests/unit/pie/objects/test_public_key.py +++ b/kmip/tests/unit/pie/objects/test_public_key.py @@ -13,16 +13,14 @@ # License for the specific language governing permissions and limitations # under the License. -from testtools import TestCase +import binascii +import testtools -from kmip.core.enums import CryptographicAlgorithm -from kmip.core.enums import CryptographicUsageMask -from kmip.core.enums import ObjectType - -from kmip.pie.objects import PublicKey +from kmip.core import enums +from kmip.pie import objects -class TestPublicKey(TestCase): +class TestPublicKey(testtools.TestCase): """ Test suite for PublicKey. """ @@ -69,12 +67,14 @@ class TestPublicKey(TestCase): """ Test that a PublicKey object can be instantiated. """ - key = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + key = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - self.assertEqual(key.cryptographic_algorithm, - CryptographicAlgorithm.RSA) + self.assertEqual( + key.cryptographic_algorithm, enums.CryptographicAlgorithm.RSA) self.assertEqual(key.cryptographic_length, 1024) self.assertEqual(key.value, self.bytes_1024) + self.assertEqual(key.key_format_type, enums.KeyFormatType.X_509) self.assertEqual(key.cryptographic_usage_masks, list()) self.assertEqual(key.names, ['Public Key']) @@ -82,29 +82,33 @@ class TestPublicKey(TestCase): """ Test that a PublicKey object can be instantiated with all arguments. """ - key = PublicKey( - CryptographicAlgorithm.RSA, + key = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, - masks=[CryptographicUsageMask.ENCRYPT, - CryptographicUsageMask.DECRYPT], + enums.KeyFormatType.X_509, + masks=[enums.CryptographicUsageMask.ENCRYPT, + enums.CryptographicUsageMask.DECRYPT], name='Test Public Key') self.assertEqual(key.cryptographic_algorithm, - CryptographicAlgorithm.RSA) + enums.CryptographicAlgorithm.RSA) self.assertEqual(key.cryptographic_length, 1024) self.assertEqual(key.value, self.bytes_1024) + self.assertEqual(key.key_format_type, enums.KeyFormatType.X_509) self.assertEqual(key.cryptographic_usage_masks, - [CryptographicUsageMask.ENCRYPT, - CryptographicUsageMask.DECRYPT]) + [enums.CryptographicUsageMask.ENCRYPT, + enums.CryptographicUsageMask.DECRYPT]) self.assertEqual(key.names, ['Test Public Key']) def test_get_object_type(self): """ Test that the object type can be retrieved from the PublicKey. """ - expected = ObjectType.PUBLIC_KEY - key = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + expected = enums.ObjectType.PUBLIC_KEY + key = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) observed = key.object_type self.assertEqual(expected, observed) @@ -113,59 +117,85 @@ class TestPublicKey(TestCase): Test that a TypeError is raised when an invalid algorithm value is used to construct a PublicKey. """ - args = ('invalid', 1024, self.bytes_1024) - self.assertRaises(TypeError, PublicKey, *args) + args = ('invalid', 1024, self.bytes_1024, enums.KeyFormatType.X_509) + self.assertRaises(TypeError, objects.PublicKey, *args) def test_validate_on_invalid_length(self): """ Test that a TypeError is raised when an invalid length value is used to construct a PublicKey. """ - args = (CryptographicAlgorithm.RSA, 'invalid', self.bytes_1024) - self.assertRaises(TypeError, PublicKey, *args) + args = (enums.CryptographicAlgorithm.RSA, 'invalid', self.bytes_1024, + enums.KeyFormatType.X_509) + self.assertRaises(TypeError, objects.PublicKey, *args) def test_validate_on_invalid_value(self): """ Test that a TypeError is raised when an invalid value is used to construct a PublicKey. """ - args = (CryptographicAlgorithm.RSA, 1024, 0) - self.assertRaises(TypeError, PublicKey, *args) + args = (enums.CryptographicAlgorithm.RSA, 1024, 0, + enums.KeyFormatType.X_509) + self.assertRaises(TypeError, objects.PublicKey, *args) + + def test_validate_on_invalid_format_type(self): + """ + Test that a TypeError is raised when an invalid format type is used to + construct a PublicKey. + """ + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + 'invalid') + self.assertRaises(TypeError, objects.PublicKey, *args) + + def test_validate_on_invalid_format_type_value(self): + """ + Test that a ValueError is raised when an invalid format type is used to + construct a PublicKey. + """ + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.OPAQUE) + self.assertRaises(ValueError, objects.PublicKey, *args) def test_validate_on_invalid_masks(self): """ Test that a TypeError is raised when an invalid masks value is used to construct a PublicKey. """ - args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) kwargs = {'masks': 'invalid'} - self.assertRaises(TypeError, PublicKey, *args, **kwargs) + self.assertRaises(TypeError, objects.PublicKey, *args, **kwargs) def test_validate_on_invalid_mask(self): """ Test that a TypeError is raised when an invalid mask value is used to construct a PublicKey. """ - args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) kwargs = {'masks': ['invalid']} - self.assertRaises(TypeError, PublicKey, *args, **kwargs) + self.assertRaises(TypeError, objects.PublicKey, *args, **kwargs) def test_validate_on_invalid_name(self): """ Test that a TypeError is raised when an invalid name value is used to construct a PublicKey. """ - args = (CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + args = (enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) kwargs = {'name': 0} - self.assertRaises(TypeError, PublicKey, *args, **kwargs) + self.assertRaises(TypeError, objects.PublicKey, *args, **kwargs) def test_repr(self): """ Test that repr can be applied to a PublicKey. """ - key = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - args = "algorithm={0}, length={1}, value={2}".format( - CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + key = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) + args = "algorithm={0}, length={1}, value={2}, format_type={3}".format( + enums.CryptographicAlgorithm.RSA, 1024, + binascii.hexlify(self.bytes_1024), enums.KeyFormatType.X_509) expected = "PublicKey({0})".format(args) observed = repr(key) self.assertEqual(expected, observed) @@ -174,8 +204,10 @@ class TestPublicKey(TestCase): """ Test that str can be applied to a PublicKey. """ - key = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - expected = str(self.bytes_1024) + key = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) + expected = str(binascii.hexlify(self.bytes_1024)) observed = str(key) self.assertEqual(expected, observed) @@ -184,8 +216,12 @@ class TestPublicKey(TestCase): Test that the equality operator returns True when comparing two PublicKey objects with the same data. """ - a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - b = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) + b = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) self.assertTrue(a == b) self.assertTrue(b == a) @@ -194,8 +230,12 @@ class TestPublicKey(TestCase): Test that the equality operator returns False when comparing two PublicKey objects with different data. """ - a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - b = PublicKey(CryptographicAlgorithm.AES, 1024, self.bytes_1024) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) + b = objects.PublicKey( + enums.CryptographicAlgorithm.AES, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) self.assertFalse(a == b) self.assertFalse(b == a) @@ -204,8 +244,12 @@ class TestPublicKey(TestCase): Test that the equality operator returns False when comparing two PublicKey objects with different data. """ - a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - b = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_1024) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) + b = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_1024, + enums.KeyFormatType.X_509) self.assertFalse(a == b) self.assertFalse(b == a) @@ -214,8 +258,26 @@ class TestPublicKey(TestCase): Test that the equality operator returns False when comparing two PublicKey objects with different data. """ - a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) - b = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_2048) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) + b = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_2048, + enums.KeyFormatType.X_509) + self.assertFalse(a == b) + self.assertFalse(b == a) + + def test_equal_on_not_equal_format_type(self): + """ + Test that the equality operator returns False when comparing two + PublicKey objects with different data. + """ + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) + b = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_1) self.assertFalse(a == b) self.assertFalse(b == a) @@ -224,7 +286,9 @@ class TestPublicKey(TestCase): Test that the equality operator returns False when comparing a PublicKey object to a non-PublicKey object. """ - a = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.X_509) b = "invalid" self.assertFalse(a == b) self.assertFalse(b == a) @@ -234,8 +298,12 @@ class TestPublicKey(TestCase): Test that the inequality operator returns False when comparing two PublicKey objects with the same internal data. """ - a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) - b = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) + b = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) self.assertFalse(a != b) self.assertFalse(b != a) @@ -244,8 +312,12 @@ class TestPublicKey(TestCase): Test that the equality operator returns True when comparing two PublicKey objects with different data. """ - a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) - b = PublicKey(CryptographicAlgorithm.AES, 2048, self.bytes_2048) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) + b = objects.PublicKey( + enums.CryptographicAlgorithm.AES, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) self.assertTrue(a != b) self.assertTrue(b != a) @@ -254,8 +326,12 @@ class TestPublicKey(TestCase): Test that the equality operator returns True when comparing two PublicKey objects with different data. """ - a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) - b = PublicKey(CryptographicAlgorithm.RSA, 1024, self.bytes_1024) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) + b = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, + enums.KeyFormatType.PKCS_1) self.assertTrue(a != b) self.assertTrue(b != a) @@ -264,8 +340,26 @@ class TestPublicKey(TestCase): Test that the equality operator returns True when comparing two PublicKey objects with different data. """ - a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) - b = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_1024) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) + b = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_1024, + enums.KeyFormatType.PKCS_1) + self.assertTrue(a != b) + self.assertTrue(b != a) + + def test_not_equal_on_not_equal_format_type(self): + """ + Test that the equality operator returns True when comparing two + PublicKey objects with different data. + """ + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) + b = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.X_509) self.assertTrue(a != b) self.assertTrue(b != a) @@ -274,7 +368,9 @@ class TestPublicKey(TestCase): Test that the equality operator returns True when comparing a PublicKey object to a non-PublicKey object. """ - a = PublicKey(CryptographicAlgorithm.RSA, 2048, self.bytes_2048) + a = objects.PublicKey( + enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, + enums.KeyFormatType.PKCS_1) b = "invalid" self.assertTrue(a != b) self.assertTrue(b != a) diff --git a/kmip/tests/unit/pie/objects/test_symmetric_key.py b/kmip/tests/unit/pie/objects/test_symmetric_key.py index 95c1807..3b24e9d 100644 --- a/kmip/tests/unit/pie/objects/test_symmetric_key.py +++ b/kmip/tests/unit/pie/objects/test_symmetric_key.py @@ -13,16 +13,14 @@ # License for the specific language governing permissions and limitations # under the License. -from testtools import TestCase +import binascii +import testtools -from kmip.core.enums import CryptographicAlgorithm -from kmip.core.enums import CryptographicUsageMask -from kmip.core.enums import ObjectType - -from kmip.pie.objects import SymmetricKey +from kmip.core import enums +from kmip.pie import objects -class TestSymmetricKey(TestCase): +class TestSymmetricKey(testtools.TestCase): """ Test suite for SymmetricKey. """ @@ -54,10 +52,11 @@ class TestSymmetricKey(TestCase): """ Test that a SymmetricKey object can be instantiated. """ - key = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) + key = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) self.assertEqual(key.cryptographic_algorithm, - CryptographicAlgorithm.AES) + enums.CryptographicAlgorithm.AES) self.assertEqual(key.cryptographic_length, 128) self.assertEqual(key.value, self.bytes_128a) self.assertEqual(key.cryptographic_usage_masks, list()) @@ -67,29 +66,30 @@ class TestSymmetricKey(TestCase): """ Test that a SymmetricKey object can be instantiated with all arguments. """ - key = SymmetricKey( - CryptographicAlgorithm.AES, + key = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a, - masks=[CryptographicUsageMask.ENCRYPT, - CryptographicUsageMask.DECRYPT], + masks=[enums.CryptographicUsageMask.ENCRYPT, + enums.CryptographicUsageMask.DECRYPT], name='Test Symmetric Key') self.assertEqual(key.cryptographic_algorithm, - CryptographicAlgorithm.AES) + enums.CryptographicAlgorithm.AES) self.assertEqual(key.cryptographic_length, 128) self.assertEqual(key.value, self.bytes_128a) self.assertEqual(key.cryptographic_usage_masks, - [CryptographicUsageMask.ENCRYPT, - CryptographicUsageMask.DECRYPT]) + [enums.CryptographicUsageMask.ENCRYPT, + enums.CryptographicUsageMask.DECRYPT]) self.assertEqual(key.names, ['Test Symmetric Key']) def test_get_object_type(self): """ Test that the object type can be retrieved from the SymmetricKey. """ - expected = ObjectType.SYMMETRIC_KEY - key = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) + expected = enums.ObjectType.SYMMETRIC_KEY + key = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) observed = key.object_type self.assertEqual(expected, observed) @@ -101,82 +101,84 @@ class TestSymmetricKey(TestCase): """ args = ('invalid', 128, self.bytes_128a) - self.assertRaises(TypeError, SymmetricKey, *args) + self.assertRaises(TypeError, objects.SymmetricKey, *args) def test_validate_on_invalid_length(self): """ Test that a TypeError is raised when an invalid length value is used to construct a SymmetricKey. """ - args = (CryptographicAlgorithm.AES, 'invalid', self.bytes_128a) + args = (enums.CryptographicAlgorithm.AES, 'invalid', self.bytes_128a) - self.assertRaises(TypeError, SymmetricKey, *args) + self.assertRaises(TypeError, objects.SymmetricKey, *args) def test_validate_on_invalid_value(self): """ Test that a TypeError is raised when an invalid value is used to construct a SymmetricKey. """ - args = (CryptographicAlgorithm.AES, 128, 0) + args = (enums.CryptographicAlgorithm.AES, 128, 0) - self.assertRaises(TypeError, SymmetricKey, *args) + self.assertRaises(TypeError, objects.SymmetricKey, *args) def test_validate_on_invalid_masks(self): """ Test that a TypeError is raised when an invalid masks value is used to construct a SymmetricKey. """ - args = (CryptographicAlgorithm.AES, 128, self.bytes_128a) + args = (enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) kwargs = {'masks': 'invalid'} - self.assertRaises(TypeError, SymmetricKey, *args, **kwargs) + self.assertRaises(TypeError, objects.SymmetricKey, *args, **kwargs) def test_validate_on_invalid_mask(self): """ Test that a TypeError is raised when an invalid mask value is used to construct a SymmetricKey. """ - args = (CryptographicAlgorithm.AES, 128, self.bytes_128a) + args = (enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) kwargs = {'masks': ['invalid']} - self.assertRaises(TypeError, SymmetricKey, *args, **kwargs) + self.assertRaises(TypeError, objects.SymmetricKey, *args, **kwargs) def test_validate_on_invalid_name(self): """ Test that a TypeError is raised when an invalid name value is used to construct a SymmetricKey. """ - args = (CryptographicAlgorithm.AES, 128, self.bytes_128a) + args = (enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) kwargs = {'name': 0} - self.assertRaises(TypeError, SymmetricKey, *args, **kwargs) + self.assertRaises(TypeError, objects.SymmetricKey, *args, **kwargs) def test_validate_on_invalid_length_value(self): """ Test that a ValueError is raised when an invalid length value is used to construct a SymmetricKey. """ - args = (CryptographicAlgorithm.AES, 256, self.bytes_128a) + args = (enums.CryptographicAlgorithm.AES, 256, self.bytes_128a) - self.assertRaises(ValueError, SymmetricKey, *args) + self.assertRaises(ValueError, objects.SymmetricKey, *args) def test_validate_on_invalid_value_length(self): """ Test that a ValueError is raised when an invalid value is used to construct a SymmetricKey. """ - args = (CryptographicAlgorithm.AES, 128, self.bytes_256a) + args = (enums.CryptographicAlgorithm.AES, 128, self.bytes_256a) - self.assertRaises(ValueError, SymmetricKey, *args) + self.assertRaises(ValueError, objects.SymmetricKey, *args) def test_repr(self): """ Test that repr can be applied to a SymmetricKey. """ - key = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) + key = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) args = "algorithm={0}, length={1}, value={2}".format( - CryptographicAlgorithm.AES, 128, self.bytes_128a) + enums.CryptographicAlgorithm.AES, 128, + binascii.hexlify(self.bytes_128a)) expected = "SymmetricKey({0})".format(args) observed = repr(key) @@ -186,8 +188,9 @@ class TestSymmetricKey(TestCase): """ Test that str can be applied to a SymmetricKey. """ - key = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - expected = str(self.bytes_128a) + key = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + expected = str(binascii.hexlify(self.bytes_128a)) observed = str(key) self.assertEqual(expected, observed) @@ -197,8 +200,10 @@ class TestSymmetricKey(TestCase): Test that the equality operator returns True when comparing two SymmetricKey objects with the same data. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - b = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + b = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) self.assertTrue(a == b) self.assertTrue(b == a) @@ -208,8 +213,10 @@ class TestSymmetricKey(TestCase): Test that the equality operator returns False when comparing two SymmetricKey objects with different data. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - b = SymmetricKey(CryptographicAlgorithm.RSA, 128, self.bytes_128a) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + b = objects.SymmetricKey( + enums.CryptographicAlgorithm.RSA, 128, self.bytes_128a) self.assertFalse(a == b) self.assertFalse(b == a) @@ -219,8 +226,10 @@ class TestSymmetricKey(TestCase): Test that the equality operator returns False when comparing two SymmetricKey objects with different data. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - b = SymmetricKey(CryptographicAlgorithm.AES, 256, self.bytes_256a) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + b = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 256, self.bytes_256a) b.value = self.bytes_128a self.assertFalse(a == b) @@ -231,8 +240,10 @@ class TestSymmetricKey(TestCase): Test that the equality operator returns False when comparing two SymmetricKey objects with different data. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - b = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128b) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + b = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128b) self.assertFalse(a == b) self.assertFalse(b == a) @@ -242,7 +253,8 @@ class TestSymmetricKey(TestCase): Test that the equality operator returns False when comparing a SymmetricKey object to a non-SymmetricKey object. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) b = "invalid" self.assertFalse(a == b) @@ -253,41 +265,49 @@ class TestSymmetricKey(TestCase): Test that the inequality operator returns False when comparing two SymmetricKey objects with the same internal data. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - b = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + b = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) self.assertFalse(a != b) self.assertFalse(b != a) def test_not_equal_on_not_equal_algorithm(self): """ - Test that the equality operator returns True when comparing two + Test that the inequality operator returns True when comparing two SymmetricKey objects with different data. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - b = SymmetricKey(CryptographicAlgorithm.RSA, 128, self.bytes_128a) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + b = objects.SymmetricKey( + enums.CryptographicAlgorithm.RSA, 128, self.bytes_128a) self.assertTrue(a != b) self.assertTrue(b != a) def test_not_equal_on_not_equal_length(self): """ - Test that the equality operator returns True when comparing two + Test that the inequality operator returns True when comparing two SymmetricKey objects with different data. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - b = SymmetricKey(CryptographicAlgorithm.AES, 256, self.bytes_256a) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + b = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 256, self.bytes_256a) self.assertTrue(a != b) self.assertTrue(b != a) def test_not_equal_on_not_equal_value(self): """ - Test that the equality operator returns True when comparing two + Test that the inequality operator returns True when comparing two SymmetricKey objects with different data. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) - b = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128b) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) + b = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128b) self.assertTrue(a != b) self.assertTrue(b != a) @@ -297,7 +317,8 @@ class TestSymmetricKey(TestCase): Test that the equality operator returns True when comparing a SymmetricKey object to a non-SymmetricKey object. """ - a = SymmetricKey(CryptographicAlgorithm.AES, 128, self.bytes_128a) + a = objects.SymmetricKey( + enums.CryptographicAlgorithm.AES, 128, self.bytes_128a) b = "invalid" self.assertTrue(a != b)