Merge pull request #83 from OpenKMIP/feat/update-attribute-value-factory

Updating the AttributeValue factory
This commit is contained in:
Peter Hamilton 2015-09-29 13:11:08 -04:00
commit 55b35b0353
2 changed files with 433 additions and 347 deletions

View File

@ -13,26 +13,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from kmip.core.enums import AttributeType from kmip.core import attributes
from kmip.core.enums import Tags from kmip.core import enums
from kmip.core import primitives
from kmip.core.primitives import DateTime
from kmip.core.attributes import ApplicationSpecificInformation
from kmip.core.attributes import ContactInformation
from kmip.core.attributes import CryptographicAlgorithm
from kmip.core.attributes import CryptographicLength
from kmip.core.attributes import CryptographicUsageMask
from kmip.core.attributes import CryptographicParameters
from kmip.core.attributes import CustomAttribute
from kmip.core.attributes import Digest
from kmip.core.attributes import Name
from kmip.core.attributes import ObjectGroup
from kmip.core.attributes import UniqueIdentifier
from kmip.core.attributes import ObjectType
from kmip.core.attributes import OperationPolicyName
from kmip.core.attributes import HashingAlgorithm
from kmip.core import utils from kmip.core import utils
@ -40,184 +23,138 @@ class AttributeValueFactory(object):
def create_attribute_value(self, name, value): def create_attribute_value(self, name, value):
# Switch on the name of the attribute # Switch on the name of the attribute
if name is AttributeType.UNIQUE_IDENTIFIER: if name is enums.AttributeType.UNIQUE_IDENTIFIER:
value = self._create_unique_identifier(value) return attributes.UniqueIdentifier(value)
elif name is AttributeType.NAME: elif name is enums.AttributeType.NAME:
value = self._create_name(value) return self._create_name(value)
elif name is AttributeType.OBJECT_TYPE: elif name is enums.AttributeType.OBJECT_TYPE:
value = self._create_object_type(value) return attributes.ObjectType()
elif name is AttributeType.CRYPTOGRAPHIC_ALGORITHM: elif name is enums.AttributeType.CRYPTOGRAPHIC_ALGORITHM:
value = self._create_cryptographic_algorithm(value) return attributes.CryptographicAlgorithm(value)
elif name is AttributeType.CRYPTOGRAPHIC_LENGTH: elif name is enums.AttributeType.CRYPTOGRAPHIC_LENGTH:
value = self._create_cryptographic_length(value) return self._create_cryptographic_length(value)
elif name is AttributeType.CRYPTOGRAPHIC_PARAMETERS: elif name is enums.AttributeType.CRYPTOGRAPHIC_PARAMETERS:
value = self._create_cryptographic_parameters(value) return self._create_cryptographic_parameters(value)
elif name is AttributeType.CRYPTOGRAPHIC_DOMAIN_PARAMETERS: elif name is enums.AttributeType.CRYPTOGRAPHIC_DOMAIN_PARAMETERS:
value = self._create_cryptographic_domain_parameters(value) raise NotImplementedError()
elif name is AttributeType.CERTIFICATE_TYPE: elif name is enums.AttributeType.CERTIFICATE_TYPE:
value = self._create_certificate_type(value) raise NotImplementedError()
elif name is AttributeType.CERTIFICATE_LENGTH: elif name is enums.AttributeType.CERTIFICATE_LENGTH:
value = self._create_certificate_length(value) return primitives.Integer(value, enums.Tags.CERTIFICATE_LENGTH)
elif name is AttributeType.X_509_CERTIFICATE_IDENTIFIER: elif name is enums.AttributeType.X_509_CERTIFICATE_IDENTIFIER:
value = self._create_x_509_certificate_identifier(value) raise NotImplementedError()
elif name is AttributeType.X_509_CERTIFICATE_SUBJECT: elif name is enums.AttributeType.X_509_CERTIFICATE_SUBJECT:
value = self._create_x_509_certificate_subject(value) raise NotImplementedError()
elif name is AttributeType.X_509_CERTIFICATE_ISSUER: elif name is enums.AttributeType.X_509_CERTIFICATE_ISSUER:
value = self._create_x_509_certificate_issuer(value) raise NotImplementedError()
elif name is AttributeType.CERTIFICATE_IDENTIFIER: elif name is enums.AttributeType.CERTIFICATE_IDENTIFIER:
value = self._create_certificate_identifier(value) raise NotImplementedError()
elif name is AttributeType.CERTIFICATE_SUBJECT: elif name is enums.AttributeType.CERTIFICATE_SUBJECT:
value = self._create_certificate_subject(value) raise NotImplementedError()
elif name is AttributeType.CERTIFICATE_ISSUER: elif name is enums.AttributeType.CERTIFICATE_ISSUER:
value = self._create_certificate_issuer(value) raise NotImplementedError()
elif name is AttributeType.DIGITAL_SIGNATURE_ALGORITHM: elif name is enums.AttributeType.DIGITAL_SIGNATURE_ALGORITHM:
value = self._create_digital_signature_algorithm(value) raise NotImplementedError()
elif name is AttributeType.DIGEST: elif name is enums.AttributeType.DIGEST:
value = self._create_digest() return attributes.Digest()
elif name is AttributeType.OPERATION_POLICY_NAME: elif name is enums.AttributeType.OPERATION_POLICY_NAME:
value = self._create_operation_policy_name(value) return attributes.OperationPolicyName(value)
elif name is AttributeType.CRYPTOGRAPHIC_USAGE_MASK: elif name is enums.AttributeType.CRYPTOGRAPHIC_USAGE_MASK:
value = self._create_cryptographic_usage_mask(value) return self._create_cryptographic_usage_mask(value)
elif name is AttributeType.LEASE_TIME: elif name is enums.AttributeType.LEASE_TIME:
value = self._create_lease_time(value) return primitives.Interval(value, enums.Tags.LEASE_TIME)
elif name is AttributeType.USAGE_LIMITS: elif name is enums.AttributeType.USAGE_LIMITS:
value = self._create_usage_limits(value) raise NotImplementedError()
elif name is AttributeType.STATE: elif name is enums.AttributeType.STATE:
value = self._create_state(value) raise NotImplementedError()
elif name is AttributeType.INITIAL_DATE: elif name is enums.AttributeType.INITIAL_DATE:
value = self._create_initial_date(value) return primitives.DateTime(value, enums.Tags.INITIAL_DATE)
elif name is AttributeType.ACTIVATION_DATE: elif name is enums.AttributeType.ACTIVATION_DATE:
value = self._create_activation_date(value) return primitives.DateTime(value, enums.Tags.ACTIVATION_DATE)
elif name is AttributeType.PROCESS_START_DATE: elif name is enums.AttributeType.PROCESS_START_DATE:
value = self._create_process_start_date(value) return primitives.DateTime(value, enums.Tags.PROCESS_START_DATE)
elif name is AttributeType.PROTECT_STOP_DATE: elif name is enums.AttributeType.PROTECT_STOP_DATE:
value = self._create_protect_stop_date(value) return primitives.DateTime(value, enums.Tags.PROTECT_STOP_DATE)
elif name is AttributeType.DEACTIVATION_DATE: elif name is enums.AttributeType.DEACTIVATION_DATE:
value = self._create_deactivation_date(value) return primitives.DateTime(value, enums.Tags.DEACTIVATION_DATE)
elif name is AttributeType.DESTROY_DATE: elif name is enums.AttributeType.DESTROY_DATE:
value = self._create_destroy_date(value) return primitives.DateTime(value, enums.Tags.DESTROY_DATE)
elif name is AttributeType.COMPROMISE_OCCURRENCE_DATE: elif name is enums.AttributeType.COMPROMISE_OCCURRENCE_DATE:
value = self._create_compromise_occurrence_date(value) return primitives.DateTime(
elif name is AttributeType.COMPROMISE_DATE: value, enums.Tags.COMPROMISE_OCCURRENCE_DATE)
value = self._create_compromise_date(value) elif name is enums.AttributeType.COMPROMISE_DATE:
elif name is AttributeType.REVOCATION_REASON: return primitives.DateTime(value, enums.Tags.COMPROMISE_DATE)
value = self._create_revocation_reason(value) elif name is enums.AttributeType.REVOCATION_REASON:
elif name is AttributeType.ARCHIVE_DATE: raise NotImplementedError()
value = self._create_archive_date(value) elif name is enums.AttributeType.ARCHIVE_DATE:
elif name is AttributeType.OBJECT_GROUP: return primitives.DateTime(value, enums.Tags.ARCHIVE_DATE)
value = self._create_object_group(value) elif name is enums.AttributeType.OBJECT_GROUP:
elif name is AttributeType.FRESH: return self._create_object_group(value)
value = self._create_fresh(value) elif name is enums.AttributeType.FRESH:
elif name is AttributeType.LINK: return primitives.Boolean(value, enums.Tags.FRESH)
value = self._create_link(value) elif name is enums.AttributeType.LINK:
elif name is AttributeType.APPLICATION_SPECIFIC_INFORMATION: raise NotImplementedError()
value = self._create_application_specific_information(value) elif name is enums.AttributeType.APPLICATION_SPECIFIC_INFORMATION:
elif name is AttributeType.CONTACT_INFORMATION: return self._create_application_specific_information(value)
value = self._create_contact_information(value) elif name is enums.AttributeType.CONTACT_INFORMATION:
elif name is AttributeType.LAST_CHANGE_DATE: return self._create_contact_information(value)
value = self._create_last_change_date(value) elif name is enums.AttributeType.LAST_CHANGE_DATE:
elif name is AttributeType.CUSTOM_ATTRIBUTE: return primitives.DateTime(value, enums.Tags.LAST_CHANGE_DATE)
value = self._create_custom_attribute(value) elif name is enums.AttributeType.CUSTOM_ATTRIBUTE:
return attributes.CustomAttribute(value)
else: else:
if not isinstance(name, str): if not isinstance(name, str):
raise ValueError('Unrecognized attribute type: ' raise ValueError('Unrecognized attribute type: '
'{0}'.format(name)) '{0}'.format(name))
elif name.startswith('x-'): elif name.startswith('x-'):
# Custom attribute indicated # Custom attribute indicated
value = self._create_custom_attribute(value) return attributes.CustomAttribute(value)
return value
def _create_unique_identifier(self, uuid):
return UniqueIdentifier(uuid)
def _create_name(self, name): def _create_name(self, name):
if name is not None: if name is not None:
name_value = name.name_value name_value = name.name_value
name_type = name.name_type name_type = name.name_type
return Name.create(name_value, name_type) return attributes.Name.create(name_value, name_type)
else: else:
return Name() return attributes.Name()
def _create_object_type(self, obj):
return ObjectType()
def _create_cryptographic_algorithm(self, alg):
return CryptographicAlgorithm(alg)
def _create_cryptographic_length(self, length): def _create_cryptographic_length(self, length):
if length is not None and not isinstance(length, int): if length is not None and not isinstance(length, int):
msg = utils.build_er_error(CryptographicLength, msg = utils.build_er_error(attributes.CryptographicLength,
'constructor argument type', int, 'constructor argument type', int,
type(length)) type(length))
raise TypeError(msg) raise TypeError(msg)
return CryptographicLength(length) return attributes.CryptographicLength(length)
def _create_cryptographic_parameters(self, params): def _create_cryptographic_parameters(self, params):
block_cipher_mode = None bcm = None
if 'block_cipher_mode' in params: if 'block_cipher_mode' in params:
block_cipher_mode = CryptographicParameters.BlockCipherMode( bcm = attributes.CryptographicParameters.BlockCipherMode(
params.get('block_cipher_mode')) params.get('block_cipher_mode'))
padding_method = None padding_method = None
if 'padding_method' in params: if 'padding_method' in params:
padding_method = CryptographicParameters.PaddingMethod( padding_method = attributes.CryptographicParameters.PaddingMethod(
params.get('padding_method')) params.get('padding_method'))
key_role_type = None key_role_type = None
if 'key_role_type' in params: if 'key_role_type' in params:
key_role_type = CryptographicParameters.KeyRoleType( key_role_type = attributes.CryptographicParameters.KeyRoleType(
params.get('key_role_type')) params.get('key_role_type'))
hashing_algorithm = None hashing_algorithm = None
if 'hashing_algorithm' in params: if 'hashing_algorithm' in params:
hashing_algorithm = HashingAlgorithm( hashing_algorithm = attributes.HashingAlgorithm(
params.get("hashing_algorithm")) params.get("hashing_algorithm"))
return CryptographicParameters( return attributes.CryptographicParameters(
block_cipher_mode=block_cipher_mode, block_cipher_mode=bcm,
padding_method=padding_method, padding_method=padding_method,
hashing_algorithm=hashing_algorithm, hashing_algorithm=hashing_algorithm,
key_role_type=key_role_type) key_role_type=key_role_type)
def _create_cryptographic_domain_parameters(self, params):
raise NotImplementedError()
def _create_certificate_type(self, cert):
raise NotImplementedError()
def _create_certificate_length(self, length):
raise NotImplementedError()
def _create_x_509_certificate_identifier(self, ident):
raise NotImplementedError()
def _create_x_509_certificate_subject(self, subject):
raise NotImplementedError()
def _create_x_509_certificate_issuer(self, issuer):
raise NotImplementedError()
def _create_certificate_identifier(self, ident):
raise NotImplementedError()
def _create_certificate_subject(self, subject):
raise NotImplementedError()
def _create_certificate_issuer(self, issuer):
raise NotImplementedError()
def _create_digital_signature_algorithm(self, alg):
raise NotImplementedError()
def _create_digest(self):
return Digest()
def _create_operation_policy_name(self, name):
return OperationPolicyName(name)
def _create_cryptographic_usage_mask(self, flags): def _create_cryptographic_usage_mask(self, flags):
mask = None mask = None
if flags is not None: if flags is not None:
@ -225,98 +162,49 @@ class AttributeValueFactory(object):
for flag in flags: for flag in flags:
mask |= flag.value mask |= flag.value
return CryptographicUsageMask(mask) return attributes.CryptographicUsageMask(mask)
def _create_lease_time(self, lease):
raise NotImplementedError()
def _create_usage_limits(self, limits):
raise NotImplementedError()
def _create_state(self, state):
raise NotImplementedError()
def _create_initial_date(self, date):
return DateTime(value=date, tag=Tags.INITIAL_DATE)
def _create_activation_date(self, date):
return DateTime(value=date, tag=Tags.ACTIVATION_DATE)
def _create_process_start_date(self, date):
return DateTime(value=date, tag=Tags.PROCESS_START_DATE)
def _create_protect_stop_date(self, date):
return DateTime(value=date, tag=Tags.PROTECT_STOP_DATE)
def _create_deactivation_date(self, date):
return DateTime(value=date, tag=Tags.DEACTIVATION_DATE)
def _create_destroy_date(self, date):
return DateTime(value=date, tag=Tags.DESTROY_DATE)
def _create_compromise_occurrence_date(self, date):
return DateTime(value=date, tag=Tags.COMPROMISE_OCCURRENCE_DATE)
def _create_compromise_date(self, date):
return DateTime(value=date, tag=Tags.COMPROMISE_DATE)
def _create_revocation_reason(self, reason):
raise NotImplementedError()
def _create_archive_date(self, date):
return DateTime(value=date, tag=Tags.ARCHIVE_DATE)
def _create_object_group(self, group): def _create_object_group(self, group):
if group is not None and not isinstance(group, str): if group is not None and not isinstance(group, str):
msg = utils.build_er_error(ObjectGroup, msg = utils.build_er_error(attributes.ObjectGroup,
'constructor argument type', str, 'constructor argument type', str,
type(group)) type(group))
raise TypeError(msg) raise TypeError(msg)
return ObjectGroup(group) return attributes.ObjectGroup(group)
def _create_fresh(self, fresh):
raise NotImplementedError()
def _create_link(self, link):
raise NotImplementedError()
def _create_application_specific_information(self, info): def _create_application_specific_information(self, info):
if info is None: if info is None:
return ApplicationSpecificInformation() return attributes.ApplicationSpecificInformation()
else: else:
application_namespace = info.get('application_namespace') application_namespace = info.get('application_namespace')
application_data = info.get('application_data') application_data = info.get('application_data')
if not isinstance(application_namespace, str): if not isinstance(application_namespace, str):
msg = utils.build_er_error(ApplicationSpecificInformation, msg = utils.build_er_error(
'constructor argument type', attributes.ApplicationSpecificInformation,
str, type(application_namespace)) 'constructor argument type',
str, type(application_namespace))
raise TypeError(msg) raise TypeError(msg)
if not isinstance(application_data, str): if not isinstance(application_data, str):
msg = utils.build_er_error(ApplicationSpecificInformation, msg = utils.build_er_error(
'constructor argument type', attributes.ApplicationSpecificInformation,
str, type(application_data)) 'constructor argument type',
str, type(application_data))
raise TypeError(msg) raise TypeError(msg)
return ApplicationSpecificInformation.create(application_namespace, return attributes.ApplicationSpecificInformation.create(
application_data) application_namespace, application_data)
def _create_contact_information(self, info): def _create_contact_information(self, info):
if info is None: if info is None:
return ContactInformation() return attributes.ContactInformation()
else: else:
if not isinstance(info, str): if not isinstance(info, str):
msg = utils.build_er_error(ContactInformation, msg = utils.build_er_error(attributes.ContactInformation,
'constructor argument type', str, 'constructor argument type', str,
type(info)) type(info))
raise TypeError(msg) raise TypeError(msg)
return ContactInformation(info) return attributes.ContactInformation(info)
def _create_last_change_date(self, date):
raise NotImplementedError()
def _create_custom_attribute(self, data):
return CustomAttribute(data)

View File

@ -13,178 +13,376 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from testtools import TestCase import testtools
from kmip.core.enums import AttributeType
from kmip.core.enums import BlockCipherMode
from kmip.core.enums import HashingAlgorithm
from kmip.core.enums import PaddingMethod
from kmip.core.enums import KeyRoleType
from kmip.core.enums import Tags
from kmip.core import attributes from kmip.core import attributes
from kmip.core.attributes import CryptographicParameters from kmip.core import enums
from kmip.core.attributes import OperationPolicyName from kmip.core import primitives
from kmip.core.primitives import DateTime from kmip.core.factories import attribute_values
from kmip.core.factories.attribute_values import AttributeValueFactory
class TestAttributeValueFactory(TestCase): class TestAttributeValueFactory(testtools.TestCase):
def setUp(self): def setUp(self):
super(TestAttributeValueFactory, self).setUp() super(TestAttributeValueFactory, self).setUp()
self.factory = AttributeValueFactory() self.factory = attribute_values.AttributeValueFactory()
def tearDown(self): def tearDown(self):
super(TestAttributeValueFactory, self).tearDown() super(TestAttributeValueFactory, self).tearDown()
# TODO (peter-hamilton) Consider even further modularity def test_create_unique_identifier(self):
def _test_operation_policy_name(self, opn, value): """
if value is None: Test that a UniqueIdentifier attribute can be created.
value = '' """
self.skip('')
msg = "expected {0}, received {1}".format(OperationPolicyName, opn) def test_create_name(self):
self.assertIsInstance(opn, OperationPolicyName, msg) """
Test that a Name attribute can be created.
"""
self.skip('')
msg = "expected {0}, received {1}".format(value, opn.value) def test_create_object_type(self):
self.assertEqual(value, opn.value, msg) """
Test that an ObjectType attribute can be created.
"""
self.skip('')
def _test_create_attribute_value_operation_policy_name(self, value): def test_create_cryptographic_algorithm(self):
opn = self.factory.create_attribute_value( """
AttributeType.OPERATION_POLICY_NAME, value) Test that a CryptographicAlgorithm attribute can be created.
self._test_operation_policy_name(opn, value) """
self.skip('')
def _test_create_operation_policy_name(self, value): def test_create_cryptographic_length(self):
opn = self.factory._create_operation_policy_name(value) """
self._test_operation_policy_name(opn, value) Test that a CryptographicLength attribute can be created.
"""
self.skip('')
def test_create_attribute_value_operation_policy_name(self): def test_create_cryptographic_parameters(self):
self._test_create_attribute_value_operation_policy_name('test') """
Test that a CryptographicParameters attribute can be created.
"""
value = {
'block_cipher_mode': enums.BlockCipherMode.NIST_KEY_WRAP,
'padding_method': enums.PaddingMethod.ANSI_X9_23,
'key_role_type': enums.KeyRoleType.KEK,
'hashing_algorithm': enums.HashingAlgorithm.SHA_512}
params = self.factory.create_attribute_value(
enums.AttributeType.CRYPTOGRAPHIC_PARAMETERS, value)
def test_create_attribute_value_operation_policy_name_on_none(self): # TODO (peter-hamilton): Update assertEquals after structure changes
self._test_create_attribute_value_operation_policy_name(None) self.assertIsInstance(params, attributes.CryptographicParameters)
self.assertEqual(
attributes.CryptographicParameters.BlockCipherMode(
enums.BlockCipherMode.NIST_KEY_WRAP),
params.block_cipher_mode)
self.assertEqual(
attributes.CryptographicParameters.PaddingMethod(
enums.PaddingMethod.ANSI_X9_23),
params.padding_method)
self.assertEqual(
attributes.CryptographicParameters.KeyRoleType(
enums.KeyRoleType.KEK),
params.key_role_type)
self.assertEqual(
attributes.HashingAlgorithm(enums.HashingAlgorithm.SHA_512),
params.hashing_algorithm)
def test_create_cryptographic_domain_parameters(self):
"""
Test that a CryptographicDomainParameters attribute can be created.
"""
kwargs = {'name': enums.AttributeType.CRYPTOGRAPHIC_DOMAIN_PARAMETERS,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_certificate_type(self):
"""
Test that a CertificateType attribute can be created.
"""
kwargs = {'name': enums.AttributeType.CERTIFICATE_TYPE,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_certificate_length(self):
"""
Test that a CertificateLength attribute can be created.
"""
length = self.factory.create_attribute_value(
enums.AttributeType.CERTIFICATE_LENGTH, 0)
self.assertIsInstance(length, primitives.Integer)
self.assertEqual(0, length.value)
self.assertEqual(enums.Tags.CERTIFICATE_LENGTH, length.tag)
def test_create_x509_certificate_identifier(self):
"""
Test that an X509CertificateIdentifier attribute can be created.
"""
kwargs = {'name': enums.AttributeType.X_509_CERTIFICATE_IDENTIFIER,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_x509_certificate_subject(self):
"""
Test that an X509CertificateSubject attribute can be created.
"""
kwargs = {'name': enums.AttributeType.X_509_CERTIFICATE_SUBJECT,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_x509_certificate_issuer(self):
"""
Test that an X509CertificateIssuer attribute can be created.
"""
kwargs = {'name': enums.AttributeType.X_509_CERTIFICATE_ISSUER,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_certificate_identifier(self):
"""
Test that a CertificateIdentifier attribute can be created.
"""
kwargs = {'name': enums.AttributeType.CERTIFICATE_IDENTIFIER,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_certificate_subject(self):
"""
Test that a CertificateSubject attribute can be created.
"""
kwargs = {'name': enums.AttributeType.CERTIFICATE_SUBJECT,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_certificate_issuer(self):
"""
Test that a CertificateIssuer attribute can be created.
"""
kwargs = {'name': enums.AttributeType.CERTIFICATE_ISSUER,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_digital_signature_algorithm(self):
"""
Test that a DigitalSignatureAlgorithm attribute can be created.
"""
kwargs = {'name': enums.AttributeType.DIGITAL_SIGNATURE_ALGORITHM,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_digest(self):
"""
Test that a Digest attribute can be created.
"""
digest = self.factory.create_attribute_value(
enums.AttributeType.DIGEST, None)
self.assertIsInstance(digest, attributes.Digest)
def test_create_operation_policy_name(self): def test_create_operation_policy_name(self):
self._test_create_operation_policy_name('test') """
Test that an OperationPolicyName attribute can be created.
"""
attribute = self.factory.create_attribute_value(
enums.AttributeType.OPERATION_POLICY_NAME, 'test')
self.assertIsInstance(attribute, attributes.OperationPolicyName)
self.assertEqual('test', attribute.value)
def test_create_operation_policy_name_on_none(self): def test_create_cryptographic_usage_mask(self):
self._test_create_operation_policy_name(None) """
Test that a CryptographicUsageMask attribute can be created.
"""
self.skip('')
def _test_cryptograpic_parameters(self, obj, block_cipher_mode, def test_create_lease_time(self):
padding_method, key_role_type, """
hashing_algorithm): Test that a LeaseTime attribute can be created.
msg = "expected {0}, received {1}" """
self.assertIsInstance(obj, CryptographicParameters, msg.format( lease = self.factory.create_attribute_value(
CryptographicParameters, obj.__class__)) enums.AttributeType.LEASE_TIME, 0)
self.assertIsInstance(lease, primitives.Interval)
self.assertEqual(0, lease.value)
self.assertEqual(enums.Tags.LEASE_TIME, lease.tag)
self.assertEqual(block_cipher_mode, obj.block_cipher_mode, msg.format( def test_create_usage_limits(self):
block_cipher_mode, obj.block_cipher_mode)) """
Test that a UsageLimits attribute can be created.
"""
kwargs = {'name': enums.AttributeType.USAGE_LIMITS,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
self.assertEqual(padding_method, obj.padding_method, msg.format( def test_create_state(self):
padding_method, obj.padding_method)) """
Test that a State attribute can be created.
self.assertEqual(key_role_type, obj.key_role_type, msg.format( """
key_role_type, obj.hashing_algorithm)) kwargs = {'name': enums.AttributeType.STATE,
'value': None}
self.assertEqual(hashing_algorithm, obj.hashing_algorithm, msg.format( self.assertRaises(
hashing_algorithm, obj.hashing_algorithm)) NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_cryptograpic_parameters_none(self):
cp = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{})
self._test_cryptograpic_parameters(cp, None, None, None, None)
def test_create_cryptograpic_parameters_block_cipher_mode(self):
cp = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'block_cipher_mode': BlockCipherMode.NIST_KEY_WRAP})
self._test_cryptograpic_parameters(
cp, CryptographicParameters.BlockCipherMode(
BlockCipherMode.NIST_KEY_WRAP),
None, None, None)
def test_create_cryptograpic_parameters_padding_method(self):
cp = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'padding_method': PaddingMethod.ANSI_X9_23})
# noqa - E128 continuation line under-indented for visual indent
self._test_cryptograpic_parameters(cp, None,
CryptographicParameters.PaddingMethod(PaddingMethod.ANSI_X9_23),
None, None) # noqa
def test_create_cryptograpic_parameters_key_role_type(self):
cp = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'key_role_type': KeyRoleType.KEK})
# noqa - E128 continuation line under-indented for visual indent
self._test_cryptograpic_parameters(cp, None, None,
CryptographicParameters.KeyRoleType(KeyRoleType.KEK),
None) # noqa
def test_create_cryptograpic_parameters_hashing_algorithm(self):
cp = self.factory.create_attribute_value(
AttributeType.CRYPTOGRAPHIC_PARAMETERS,
{'hashing_algorithm': HashingAlgorithm.SHA_512})
# noqa - E128 continuation line under-indented for visual indent
self._test_cryptograpic_parameters(cp, None, None, None,
attributes.HashingAlgorithm(HashingAlgorithm.SHA_512)) # noqa
def _test_date_value(self, date, value, tag):
msg = "expected {0}, received {1}"
self.assertIsInstance(date, DateTime, msg.format(
DateTime, date.__class__))
self.assertEqual(date.value, value, msg.format(value, date.value))
self.assertEqual(date.tag, tag, msg.format(tag, date.tag))
def test_create_initial_date(self): def test_create_initial_date(self):
"""
Test that an InitialDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.INITIAL_DATE, 0) enums.AttributeType.INITIAL_DATE, 0)
self._test_date_value(date, 0, Tags.INITIAL_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.INITIAL_DATE, date.tag)
def test_create_activation_date(self): def test_create_activation_date(self):
"""
Test that an ActivationDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.ACTIVATION_DATE, 0) enums.AttributeType.ACTIVATION_DATE, 0)
self._test_date_value(date, 0, Tags.ACTIVATION_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.ACTIVATION_DATE, date.tag)
def test_create_process_start_date(self): def test_create_process_start_date(self):
"""
Test that a ProcessStartDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.PROCESS_START_DATE, 0) enums.AttributeType.PROCESS_START_DATE, 0)
self._test_date_value(date, 0, Tags.PROCESS_START_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.PROCESS_START_DATE, date.tag)
def test_create_protect_stop_date(self): def test_create_protect_stop_date(self):
"""
Test that a ProtectStopDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.PROTECT_STOP_DATE, 0) enums.AttributeType.PROTECT_STOP_DATE, 0)
self._test_date_value(date, 0, Tags.PROTECT_STOP_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.PROTECT_STOP_DATE, date.tag)
def test_create_deactivation_date(self): def test_create_deactivation_date(self):
"""
Test that a DeactivationDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.DEACTIVATION_DATE, 0) enums.AttributeType.DEACTIVATION_DATE, 0)
self._test_date_value(date, 0, Tags.DEACTIVATION_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.DEACTIVATION_DATE, date.tag)
def test_create_destroy_date(self): def test_create_destroy_date(self):
"""
Test that a DestroyDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.DESTROY_DATE, 0) enums.AttributeType.DESTROY_DATE, 0)
self._test_date_value(date, 0, Tags.DESTROY_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.DESTROY_DATE, date.tag)
def test_create_compromise_occurance_date(self): def test_create_compromise_occurance_date(self):
"""
Test that a CompromiseOccurrenceDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.COMPROMISE_OCCURRENCE_DATE, 0) enums.AttributeType.COMPROMISE_OCCURRENCE_DATE, 0)
self._test_date_value(date, 0, Tags.COMPROMISE_OCCURRENCE_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.COMPROMISE_OCCURRENCE_DATE, date.tag)
def test_create_compromise_date(self): def test_create_compromise_date(self):
"""
Test that a CompromiseDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.COMPROMISE_DATE, 0) enums.AttributeType.COMPROMISE_DATE, 0)
self._test_date_value(date, 0, Tags.COMPROMISE_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.COMPROMISE_DATE, date.tag)
def test_create_revocation_reason(self):
"""
Test that a RevocationReason attribute can be created.
"""
kwargs = {'name': enums.AttributeType.REVOCATION_REASON,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_archive_date(self): def test_create_archive_date(self):
"""
Test that an ArchiveDate attribute can be created.
"""
date = self.factory.create_attribute_value( date = self.factory.create_attribute_value(
AttributeType.ARCHIVE_DATE, 0) enums.AttributeType.ARCHIVE_DATE, 0)
self._test_date_value(date, 0, Tags.ARCHIVE_DATE) self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.ARCHIVE_DATE, date.tag)
def test_create_object_group(self):
"""
Test that an ObjectGroup attribute can be created.
"""
self.skip('')
def test_create_fresh(self):
"""
Test that a Fresh attribute can be created.
"""
fresh = self.factory.create_attribute_value(
enums.AttributeType.FRESH, True)
self.assertIsInstance(fresh, primitives.Boolean)
self.assertEqual(True, fresh.value)
self.assertEqual(enums.Tags.FRESH, fresh.tag)
def test_create_link(self):
"""
Test that a Link attribute can be created.
"""
kwargs = {'name': enums.AttributeType.LINK,
'value': None}
self.assertRaises(
NotImplementedError, self.factory.create_attribute_value, **kwargs)
def test_create_application_specific_information(self):
"""
Test that an ApplicationSpecificInformation attribute can be created.
"""
self.skip('')
def test_create_contact_information(self):
"""
Test that a ContactInformation attribute can be created.
"""
self.skip('')
def test_create_last_change_date(self):
"""
Test that an LastChangeDate attribute can be created.
"""
date = self.factory.create_attribute_value(
enums.AttributeType.LAST_CHANGE_DATE, 0)
self.assertIsInstance(date, primitives.DateTime)
self.assertEqual(0, date.value)
self.assertEqual(enums.Tags.LAST_CHANGE_DATE, date.tag)
def test_create_custom_attribute(self):
"""
Test that a CustomAttribute can be created.
"""
custom = self.factory.create_attribute_value(
enums.AttributeType.CUSTOM_ATTRIBUTE, None)
self.assertIsInstance(custom, attributes.CustomAttribute)