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 = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1) b = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.X_509) self.assertTrue(a != b) self.assertTrue(b != a)
def test_equal_on_equal(self): """ Test that the equality operator returns True when comparing two PublicKey objects with the same data. """ a = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509) b = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509) self.assertTrue(a == b) self.assertTrue(b == a)
def test_not_equal_on_equal(self): """ Test that the inequality operator returns False when comparing two PublicKey objects with the same internal data. """ a = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1) b = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1) 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 = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509) b = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.PKCS_1) self.assertFalse(a == b) self.assertFalse(b == a)
def test_add_multiple_names(self): """ Test that multiple names can be added to a managed object. This verifies a few properties. First this verifies that names can be added using simple strings. It also verifies that the index for each subsequent string is set accordingly. Finally this tests that the names can be saved and retrieved from the database. """ expected_names = ['bowser', 'frumpy', 'big fat cat'] key = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1, name=expected_names[0]) key.names.append(expected_names[1]) key.names.append(expected_names[2]) self.assertEquals(3, key.name_index) expected_mo_names = list() for i, name in enumerate(expected_names): expected_mo_names.append(sqltypes.ManagedObjectName(name, i)) self.assertEquals(expected_mo_names, key._names) Session = sessionmaker(bind=self.engine) session = Session() session.add(key) session.commit() session = Session() get_obj = session.query(PublicKey).filter( ManagedObject.unique_identifier == key.unique_identifier).one() session.commit() self.assertEquals(expected_mo_names, get_obj._names)
def test_get(self): """ Test that the object can be saved and then retrieved using SQLAlchemy. This adds is to the database and then retrieves it by ID and verifies some of the attributes. """ test_name = 'bowser' masks = [ enums.CryptographicUsageMask.ENCRYPT, enums.CryptographicUsageMask.WRAP_KEY ] key = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1, masks=masks, name=test_name) Session = sessionmaker(bind=self.engine) session = Session() session.add(key) session.commit() session = Session() get_obj = session.query(PublicKey).filter( ManagedObject.unique_identifier == key.unique_identifier).one() session.commit() self.assertEqual(1, len(get_obj.names)) self.assertEqual([test_name], get_obj.names) self.assertEqual(enums.ObjectType.PUBLIC_KEY, get_obj.object_type) self.assertEqual(self.bytes_2048, get_obj.value) self.assertEqual(enums.CryptographicAlgorithm.RSA, get_obj.cryptographic_algorithm) self.assertEqual(2048, get_obj.cryptographic_length) self.assertEqual(enums.KeyFormatType.PKCS_1, get_obj.key_format_type) self.assertEqual(masks, get_obj.cryptographic_usage_masks)
def test_str(self): """ Test that str can be applied to a PublicKey. """ key = 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)
def test_get_object_type(self): """ Test that the object type can be retrieved from the PublicKey. """ expected = enums.ObjectType.PUBLIC_KEY key = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509) observed = key.object_type self.assertEqual(expected, observed)
def test_equal_on_type_mismatch(self): """ Test that the equality operator returns False when comparing a PublicKey object to a non-PublicKey object. """ a = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509) b = "invalid" self.assertFalse(a == b) self.assertFalse(b == a)
def test_not_equal_on_type_mismatch(self): """ Test that the equality operator returns True when comparing a PublicKey object to a non-PublicKey object. """ a = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1) b = "invalid" self.assertTrue(a != b) self.assertTrue(b != a)
def test_not_equal_on_not_equal_key_wrapping_data(self): """ Test that the inequality operator returns True when comparing two PublicKey objects with different key wrapping data. """ a = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509, key_wrapping_data={}) b = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509, key_wrapping_data={ 'wrapping_method': enums.WrappingMethod.ENCRYPT }) self.assertTrue(a != b) self.assertTrue(b != a)
def test_repr(self): """ Test that repr can be applied to a PublicKey. """ key = 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)
def test_save(self): """ Test that the object can be saved using SQLAlchemy. This will add it to the database, verify that no exceptions are thrown, and check that its unique identifier was set. """ key = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1) Session = sessionmaker(bind=self.engine) session = Session() session.add(key) session.commit() self.assertIsNotNone(key.unique_identifier)
def test_init(self): """ Test that a PublicKey object can be instantiated. """ key = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024) 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'])
def test_repr(self): """ Test that repr can be applied to a PublicKey. """ key = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509) args = "{0}, {1}, {2}, {3}, {4}".format( "algorithm={0}".format(enums.CryptographicAlgorithm.RSA), "length={0}".format(1024), "value={0}".format(binascii.hexlify(self.bytes_1024)), "format_type={0}".format(enums.KeyFormatType.X_509), "key_wrapping_data={0}".format({})) expected = "PublicKey({0})".format(args) observed = repr(key) self.assertEqual(expected, observed)
def test_update_with_remove_and_add_name(self): """ Tests that an OpaqueObject already stored in the database can be updated. This will store an OpaqueObject in the database. It will remove a name and add another one to it in one session, and then retrieve it in another session to verify that it has all of the correct names. This simulates multiple operation being sent for the same object. """ names = ['bowser', 'frumpy', 'big fat cat'] key = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1, name=names[0]) key.names.append(names[1]) key.names.append(names[2]) Session = sessionmaker(bind=self.engine) session = Session() session.add(key) session.commit() session = Session() update_key = session.query(PublicKey).filter( ManagedObject.unique_identifier == key.unique_identifier).one() update_key.names.pop() update_key.names.pop() update_key.names.append('dog') session.commit() expected_names = ['bowser', 'dog'] expected_mo_names = list() expected_mo_names.append( sqltypes.ManagedObjectName(expected_names[0], 0)) expected_mo_names.append( sqltypes.ManagedObjectName(expected_names[1], 3)) session = Session() get_obj = session.query(PublicKey).filter( ManagedObject.unique_identifier == key.unique_identifier).one() session.commit() self.assertEquals(expected_names, get_obj.names) self.assertEquals(expected_mo_names, get_obj._names)
def test_update_with_add_name(self): """ Tests that an OpaqueObject already stored in the database can be updated. This will store an OpaqueObject in the database. It will add a name to it in one session, and then retrieve it in another session to verify that it has all of the correct names. This test and the subsequent test_udpate_* methods are different than the name tests above because these are updating objects already stored in the database. This tests will simulate what happens when the KMIP client calls an add attribute method. """ first_name = 'bowser' key = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1, name=first_name) Session = sessionmaker(bind=self.engine) session = Session() session.add(key) session.commit() added_name = 'frumpy' expected_names = [first_name, added_name] expected_mo_names = list() for i, name in enumerate(expected_names): expected_mo_names.append(sqltypes.ManagedObjectName(name, i)) session = Session() update_key = session.query(PublicKey).filter( ManagedObject.unique_identifier == key.unique_identifier).one() update_key.names.append(added_name) session.commit() session = Session() get_obj = session.query(PublicKey).filter( ManagedObject.unique_identifier == key.unique_identifier).one() session.commit() self.assertEquals(expected_names, get_obj.names) self.assertEquals(expected_mo_names, get_obj._names)
def test_remove_and_add_name(self): """ Tests that names can be removed from the list of names and more added. This will verify that the list of names is correct. It will verify that updating this object removes the name from the database. It will verify that the indices for the removed names are not reused. """ names = ['bowser', 'frumpy', 'big fat cat'] key = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1, name=names[0]) key.names.append(names[1]) key.names.append(names[2]) key.names.pop() key.names.pop() key.names.append('dog') self.assertEquals(4, key.name_index) expected_names = ['bowser', 'dog'] expected_mo_names = list() expected_mo_names.append( sqltypes.ManagedObjectName(expected_names[0], 0)) expected_mo_names.append( sqltypes.ManagedObjectName(expected_names[1], 3)) self.assertEquals(expected_names, key.names) self.assertEquals(expected_mo_names, key._names) Session = sessionmaker(bind=self.engine) session = Session() session.add(key) session.commit() session = Session() get_obj = session.query(PublicKey).filter( ManagedObject.unique_identifier == key.unique_identifier).one() session.commit() self.assertEquals(expected_names, get_obj.names) self.assertEquals(expected_mo_names, get_obj._names)
def test_remove_name(self): """ Tests that a name can be removed from the list of names. This will verify that the list of names is correct. It will verify that updating this object removes the name from the database. """ names = ['bowser', 'frumpy', 'big fat cat'] remove_index = 1 key = PublicKey(enums.CryptographicAlgorithm.RSA, 2048, self.bytes_2048, enums.KeyFormatType.PKCS_1, name=names[0]) key.names.append(names[1]) key.names.append(names[2]) key.names.pop(remove_index) self.assertEquals(3, key.name_index) expected_names = list() expected_mo_names = list() for i, name in enumerate(names): if i != remove_index: expected_names.append(name) expected_mo_names.append(sqltypes.ManagedObjectName(name, i)) self.assertEquals(expected_names, key.names) self.assertEquals(expected_mo_names, key._names) Session = sessionmaker(bind=self.engine) session = Session() session.add(key) session.commit() session = Session() get_obj = session.query(PublicKey).filter( ManagedObject.unique_identifier == key.unique_identifier).one() session.commit() self.assertEquals(expected_names, get_obj.names) self.assertEquals(expected_mo_names, get_obj._names)
def test_init_with_args(self): """ Test that a PublicKey object can be instantiated with all arguments. """ key = PublicKey(enums.CryptographicAlgorithm.RSA, 1024, self.bytes_1024, enums.KeyFormatType.X_509, masks=[ enums.CryptographicUsageMask.ENCRYPT, enums.CryptographicUsageMask.DECRYPT ], name='Test Public Key') 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, [ enums.CryptographicUsageMask.ENCRYPT, enums.CryptographicUsageMask.DECRYPT ]) self.assertEqual(key.names, ['Test Public Key'])