Update ApplicationSpecificInformation support to modern style

This change updates the ApplicationSpecificInformation attribute
implementation to reflect modern library style. Property methods
have been added alongside tighter read/write error checking. The
unit tests for the attribute have been updated to reflect all of
the new changes.
This commit is contained in:
Peter Hamilton 2019-09-27 10:44:22 -04:00 committed by Peter Hamilton
parent 0361bf9d44
commit b7d2cc1382
5 changed files with 405 additions and 543 deletions

View File

@ -16,14 +16,15 @@
import six
from kmip.core import enums
from kmip.core import exceptions
from kmip.core.enums import HashingAlgorithm as HashingAlgorithmEnum
from kmip.core.enums import KeyFormatType as KeyFormatTypeEnum
from kmip.core.enums import Tags
from kmip.core import exceptions
from kmip.core.misc import KeyFormatType
from kmip.core import primitives
from kmip.core.primitives import Boolean
from kmip.core.primitives import ByteString
from kmip.core.primitives import Enumeration
@ -31,7 +32,9 @@ from kmip.core.primitives import Integer
from kmip.core.primitives import Struct
from kmip.core.primitives import TextString
from kmip.core import utils
from kmip.core.utils import BytearrayStream
from enum import Enum
@ -1070,48 +1073,7 @@ class ObjectGroup(TextString):
super(ObjectGroup, self).__init__(value, Tags.OBJECT_GROUP)
# 3.36
class ApplicationNamespace(TextString):
"""
The name of a namespace supported by the KMIP server.
A part of ApplicationSpecificInformation, sets of these are also potential
responses to a Query request. See Sections 3.36 and 4.25 of the KMIP v1.1
specification for more information.
"""
def __init__(self, value=None):
"""
Construct an ApplicationNamespace object.
Args:
value (str): A string representing a namespace. Optional, defaults
to None.
"""
super(ApplicationNamespace, self).__init__(
value, Tags.APPLICATION_NAMESPACE)
class ApplicationData(TextString):
"""
A string representing data specific to an application namespace.
A part of ApplicationSpecificInformation. See Section 3.36 of the KMIP v1.1
specification for more information.
"""
def __init__(self, value=None):
"""
Construct an ApplicationData object.
Args:
value (str): A string representing data for a particular namespace.
Optional, defaults to None.
"""
super(ApplicationData, self).__init__(value, Tags.APPLICATION_DATA)
class ApplicationSpecificInformation(Struct):
class ApplicationSpecificInformation(primitives.Struct):
"""
A structure used to store data specific to the applications that use a
Managed Object.
@ -1131,98 +1093,168 @@ class ApplicationSpecificInformation(Struct):
Construct an ApplicationSpecificInformation object.
Args:
application_namespace (ApplicationNamespace): The name of a
namespace supported by the server. Optional, defaults to None.
application_data (ApplicationData): String data relevant to the
specified namespace. Optional, defaults to None.
application_namespace (string): The name of a namespace supported
by the server. Optional, defaults to None. Required for
read/write.
application_data (string): String data relevant to the specified
namespace. Optional, defaults to None. Required for read/write.
"""
super(ApplicationSpecificInformation, self).__init__(
Tags.APPLICATION_SPECIFIC_INFORMATION)
enums.Tags.APPLICATION_SPECIFIC_INFORMATION
)
if application_namespace is None:
self.application_namespace = ApplicationNamespace()
self._application_namespace = None
self._application_data = None
self.application_namespace = application_namespace
self.application_data = application_data
@property
def application_namespace(self):
if self._application_namespace:
return self._application_namespace.value
return None
@application_namespace.setter
def application_namespace(self, value):
if value is None:
self._application_namespace = None
elif isinstance(value, six.string_types):
self._application_namespace = primitives.TextString(
value=value,
tag=enums.Tags.APPLICATION_NAMESPACE
)
else:
self.application_namespace = application_namespace
raise TypeError("The application namespace must be a string.")
if application_data is None:
self.application_data = ApplicationData()
@property
def application_data(self):
if self._application_data:
return self._application_data.value
return None
@application_data.setter
def application_data(self, value):
if value is None:
self._application_data = None
elif isinstance(value, six.string_types):
self._application_data = primitives.TextString(
value=value,
tag=enums.Tags.APPLICATION_DATA
)
else:
self.application_data = application_data
raise TypeError("The application data must be a string.")
self.validate()
def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
"""
Read the data encoding the ApplicationSpecificInformation object and
decode it into its constituent parts.
Read the data encoding the ApplicationSpecificInformation attribute
and decode it.
Args:
istream (Stream): A data stream containing encoded object data,
supporting a read method; usually a BytearrayStream object.
input_buffer (stream): A data stream containing encoded object
data, supporting a read method; usually a BytearrayStream
object.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be decoded. Optional,
defaults to KMIP 1.0.
"""
super(ApplicationSpecificInformation, self).read(
istream,
input_buffer,
kmip_version=kmip_version
)
tstream = BytearrayStream(istream.read(self.length))
local_buffer = utils.BytearrayStream(input_buffer.read(self.length))
self.application_namespace.read(tstream, kmip_version=kmip_version)
self.application_data.read(tstream, kmip_version=kmip_version)
if self.is_tag_next(enums.Tags.APPLICATION_NAMESPACE, local_buffer):
self._application_namespace = primitives.TextString(
tag=enums.Tags.APPLICATION_NAMESPACE
)
self._application_namespace.read(
local_buffer,
kmip_version=kmip_version
)
else:
raise exceptions.InvalidKmipEncoding(
"The ApplicationSpecificInformation encoding is missing the "
"ApplicationNamespace field."
)
if self.is_tag_next(enums.Tags.APPLICATION_DATA, local_buffer):
self._application_data = primitives.TextString(
tag=enums.Tags.APPLICATION_DATA
)
self._application_data.read(
local_buffer,
kmip_version=kmip_version
)
else:
raise exceptions.InvalidKmipEncoding(
"The ApplicationSpecificInformation encoding is missing the "
"ApplicationData field."
)
self.is_oversized(tstream)
self.validate()
self.is_oversized(local_buffer)
def write(self, ostream, kmip_version=enums.KMIPVersion.KMIP_1_0):
def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
"""
Write the data encoding the ApplicationSpecificInformation object to a
stream.
buffer.
Args:
ostream (Stream): A data stream in which to encode object data,
supporting a write method; usually a BytearrayStream object.
output_buffer (stream): A data stream in which to encode object
data, supporting a write method; usually a BytearrayStream
object.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be encoded. Optional,
defaults to KMIP 1.0.
"""
tstream = BytearrayStream()
local_buffer = utils.BytearrayStream()
self.application_namespace.write(tstream, kmip_version=kmip_version)
self.application_data.write(tstream, kmip_version=kmip_version)
if self._application_namespace:
self._application_namespace.write(
local_buffer,
kmip_version=kmip_version
)
else:
raise exceptions.InvalidField(
"The ApplicationSpecificInformation object is missing the "
"ApplicationNamespace field."
)
if self._application_data:
self._application_data.write(
local_buffer,
kmip_version=kmip_version
)
else:
raise exceptions.InvalidField(
"The ApplicationSpecificInformation object is missing the "
"ApplicationData field."
)
self.length = tstream.length()
self.length = local_buffer.length()
super(ApplicationSpecificInformation, self).write(
ostream,
output_buffer,
kmip_version=kmip_version
)
ostream.write(tstream.buffer)
def validate(self):
"""
Error check the types of the different attributes of the
ApplicationSpecificInformation object.
"""
self.__validate()
output_buffer.write(local_buffer.buffer)
def __repr__(self):
application_namespace = "application_namespace={0}".format(
repr(self.application_namespace)
)
application_data = "application_data={0}".format(
repr(self.application_data)
)
return "ApplicationSpecificInformation({0}, {1})".format(
application_namespace,
application_data
)
args = [
"application_namespace={}".format(
repr(self.application_namespace)
),
"application_data={}".format(repr(self.application_data))
]
return "ApplicationSpecificInformation({})".format(", ".join(args))
def __str__(self):
return str({
"application_namespace": str(self.application_namespace),
"application_data": str(self.application_data)
})
value = ", ".join(
[
'"application_namespace": {}'.format(
self.application_namespace
),
'"application_data": {}'.format(self.application_data)
]
)
return "{" + value + "}"
def __eq__(self, other):
if isinstance(other, ApplicationSpecificInformation):
@ -1240,45 +1272,6 @@ class ApplicationSpecificInformation(Struct):
else:
return NotImplemented
def __validate(self):
if not isinstance(self.application_namespace, ApplicationNamespace):
msg = "invalid application namespace"
msg += "; expected {0}, received {1}".format(
ApplicationNamespace, self.application_namespace)
raise TypeError(msg)
if not isinstance(self.application_data, ApplicationData):
msg = "invalid application data"
msg += "; expected {0}, received {1}".format(
ApplicationData, self.application_data)
raise TypeError(msg)
@classmethod
def create(cls, application_namespace, application_data):
"""
Construct an ApplicationSpecificInformation object from provided data
and namespace values.
Args:
application_namespace (str): The name of the application namespace.
application_data (str): Application data related to the namespace.
Returns:
ApplicationSpecificInformation: The newly created set of
application information.
Example:
>>> x = ApplicationSpecificInformation.create('namespace', 'data')
>>> x.application_namespace.value
'namespace'
>>> x.application_data.value
'data'
"""
namespace = ApplicationNamespace(application_namespace)
data = ApplicationData(application_data)
return ApplicationSpecificInformation(
application_namespace=namespace, application_data=data)
# 3.37
class ContactInformation(TextString):

View File

@ -13,16 +13,14 @@
# License for the specific language governing permissions and limitations
# under the License.
from testtools import TestCase
import testtools
from kmip.core.attributes import ApplicationData
from kmip.core.attributes import ApplicationNamespace
from kmip.core.attributes import ApplicationSpecificInformation
from kmip.core.utils import BytearrayStream
from kmip.core import attributes
from kmip.core import exceptions
from kmip.core import utils
class TestApplicationSpecificInformation(TestCase):
class TestApplicationSpecificInformation(testtools.TestCase):
"""
A test suite for the ApplicationSpecificInformation class.
"""
@ -30,364 +28,338 @@ class TestApplicationSpecificInformation(TestCase):
def setUp(self):
super(TestApplicationSpecificInformation, self).setUp()
self.encoding_default = BytearrayStream((
b'\x42\x00\x04\x01\x00\x00\x00\x10\x42\x00\x03\x07\x00\x00\x00\x00'
b'\x42\x00\x02\x07\x00\x00\x00\x00'))
self.encoding = BytearrayStream((
b'\x42\x00\x04\x01\x00\x00\x00\x28\x42\x00\x03\x07\x00\x00\x00\x03'
b'\x73\x73\x6C\x00\x00\x00\x00\x00\x42\x00\x02\x07\x00\x00\x00\x0F'
b'\x77\x77\x77\x2E\x65\x78\x61\x6D\x70\x6C\x65\x2E\x63\x6F\x6D'
b'\x00'))
# This encoding was taken from test case 3.1.2 from the KMIP 1.1 test
# document.
#
# This encoding matches the following set of values:
# Application Specific Information
# Application Namespace - ssl
# Application Data - www.example.com
self.full_encoding = utils.BytearrayStream(
b'\x42\x00\x04\x01\x00\x00\x00\x28'
b'\x42\x00\x03\x07\x00\x00\x00\x03\x73\x73\x6C\x00\x00\x00\x00\x00'
b'\x42\x00\x02\x07\x00\x00\x00\x0F'
b'\x77\x77\x77\x2E\x65\x78\x61\x6D\x70\x6C\x65\x2E\x63\x6F\x6D\x00'
)
# This encoding was adapted from test case 3.1.2 from the KMIP 1.1 test
# document.
#
# This encoding matches the following set of values:
# Application Specific Information
# Application Data - www.example.com
self.no_application_namespace_encoding = utils.BytearrayStream(
b'\x42\x00\x04\x01\x00\x00\x00\x18'
b'\x42\x00\x02\x07\x00\x00\x00\x0F'
b'\x77\x77\x77\x2E\x65\x78\x61\x6D\x70\x6C\x65\x2E\x63\x6F\x6D\x00'
)
# This encoding was adapted from test case 3.1.2 from the KMIP 1.1 test
# document.
#
# This encoding matches the following set of values:
# Application Specific Information
# Application Namespace - ssl
self.no_application_data_encoding = utils.BytearrayStream(
b'\x42\x00\x04\x01\x00\x00\x00\x10'
b'\x42\x00\x03\x07\x00\x00\x00\x03\x73\x73\x6C\x00\x00\x00\x00\x00'
)
def tearDown(self):
super(TestApplicationSpecificInformation, self).tearDown()
def _test_init(self, application_namespace, application_data):
application_specific_information = ApplicationSpecificInformation(
application_namespace=application_namespace,
application_data=application_data)
if application_namespace is None:
self.assertEqual(
ApplicationNamespace(),
application_specific_information.application_namespace)
else:
self.assertEqual(
application_namespace,
application_specific_information.application_namespace)
if application_data is None:
self.assertEqual(
ApplicationData(),
application_specific_information.application_data)
else:
self.assertEqual(
application_data,
application_specific_information.application_data)
def test_init_with_none(self):
def test_init(self):
"""
Test that an ApplicationSpecificInformation object can be constructed
with no specified values.
Test that an ApplicationSpecificInformation object can be constructed.
"""
self._test_init(None, None)
app_specific_info = attributes.ApplicationSpecificInformation()
def test_init_with_args(self):
"""
Test that an ApplicationSpecificInformation object can be constructed
with valid values.
"""
application_namespace = ApplicationNamespace("namespace")
application_data = ApplicationData("data")
self._test_init(application_namespace, application_data)
self.assertIsNone(app_specific_info.application_namespace)
self.assertIsNone(app_specific_info.application_data)
def test_validate_on_invalid_application_namespace(self):
"""
Test that a TypeError exception is raised when an invalid
ApplicationNamespace value is used to construct an
ApplicationSpecificInformation object.
"""
application_namespace = "invalid"
application_data = ApplicationData()
args = [application_namespace, application_data]
app_specific_info = attributes.ApplicationSpecificInformation(
application_namespace="namespace",
application_data="data"
)
self.assertEqual("namespace", app_specific_info.application_namespace)
self.assertEqual("data", app_specific_info.application_data)
def test_invalid_application_namespace(self):
"""
Test that a TypeError is raised when an invalid value is used to set
the application namespace of an ApplicationSpecificInformation object.
"""
kwargs = {"application_namespace": []}
self.assertRaisesRegex(
TypeError, "invalid application namespace",
ApplicationSpecificInformation, *args)
def test_validate_on_invalid_application_data(self):
"""
Test that a TypeError exception is raised when an invalid
ApplicationData value is used to construct an
ApplicationSpecificInformation object.
"""
application_namespace = ApplicationNamespace()
application_data = "invalid"
args = [application_namespace, application_data]
TypeError,
"The application namespace must be a string.",
attributes.ApplicationSpecificInformation,
**kwargs
)
args = (
attributes.ApplicationSpecificInformation(),
"application_namespace",
[]
)
self.assertRaisesRegex(
TypeError, "invalid application data",
ApplicationSpecificInformation, *args)
TypeError,
"The application namespace must be a string.",
setattr,
*args
)
def _test_read(self, stream, application_namespace, application_data):
application_specific_information = ApplicationSpecificInformation()
application_specific_information.read(stream)
if application_namespace is None:
application_namespace = ApplicationNamespace()
if application_data is None:
application_data = ApplicationData()
msg = "application namespace encoding mismatch"
msg += "; expected {0}, observed {1}".format(
application_namespace,
application_specific_information.application_namespace)
self.assertEqual(
application_namespace,
application_specific_information.application_namespace, msg)
msg = "application data encoding mismatch"
msg += "; expected {0}, observed {1}".format(
application_data,
application_specific_information.application_data)
self.assertEqual(
application_data,
application_specific_information.application_data, msg)
def test_read_with_none(self):
def test_invalid_application_data(self):
"""
Test that an ApplicationSpecificInformation object with no data can be
read from a data stream.
Test that a TypeError is raised when an invalid value is used to set
the application data of an ApplicationSpecificInformation object.
"""
self._test_read(self.encoding_default, None, None)
kwargs = {"application_data": []}
self.assertRaisesRegex(
TypeError,
"The application data must be a string.",
attributes.ApplicationSpecificInformation,
**kwargs
)
def test_read_with_args(self):
args = (
attributes.ApplicationSpecificInformation(),
"application_data",
[]
)
self.assertRaisesRegex(
TypeError,
"The application data must be a string.",
setattr,
*args
)
def test_read(self):
"""
Test that an ApplicationSpecificInformation object with data can be
read from a data stream.
Test that an ApplicationSpecificInformation object can be read from a
buffer.
"""
application_namespace = ApplicationNamespace("ssl")
application_data = ApplicationData("www.example.com")
self._test_read(self.encoding, application_namespace, application_data)
app_specific_info = attributes.ApplicationSpecificInformation()
def _test_write(self, stream_expected, application_namespace,
application_data):
stream_observed = BytearrayStream()
application_specific_information = ApplicationSpecificInformation(
application_namespace=application_namespace,
application_data=application_data)
application_specific_information.write(stream_observed)
self.assertIsNone(app_specific_info.application_namespace)
self.assertIsNone(app_specific_info.application_data)
length_expected = len(stream_expected)
length_observed = len(stream_observed)
app_specific_info.read(self.full_encoding)
msg = "encoding lengths not equal"
msg += "; expected {0}, observed {1}".format(
length_expected, length_observed)
self.assertEqual(length_expected, length_observed, msg)
self.assertEqual("ssl", app_specific_info.application_namespace)
self.assertEqual("www.example.com", app_specific_info.application_data)
msg = "encoding mismatch"
msg += ";\nexpected:\n{0}\nobserved:\n{1}".format(
stream_expected, stream_observed)
self.assertEqual(stream_expected, stream_observed, msg)
def test_write_with_none(self):
def test_read_missing_application_namespace(self):
"""
Test that an ApplicationSpecificInformation object with no data can be
written to a data stream.
Test that an InvalidKmipEncoding error is raised during the decoding of
an ApplicationSpecificInformation object with the application namespace
is missing from the encoding.
"""
self._test_write(self.encoding_default, None, None)
app_specific_info = attributes.ApplicationSpecificInformation()
def test_write_with_args(self):
self.assertIsNone(app_specific_info.application_namespace)
args = (self.no_application_namespace_encoding, )
self.assertRaisesRegex(
exceptions.InvalidKmipEncoding,
"The ApplicationSpecificInformation encoding is missing the "
"ApplicationNamespace field.",
app_specific_info.read,
*args
)
def test_read_missing_application_data(self):
"""
Test that an ApplicationSpecificInformation object with data can be
written to a data stream.
Test that an InvalidKmipEncoding error is raised during the decoding of
an ApplicationSpecificInformation object with the application data is
missing from the encoding.
"""
application_namespace = ApplicationNamespace("ssl")
application_data = ApplicationData("www.example.com")
self._test_write(self.encoding, application_namespace,
application_data)
app_specific_info = attributes.ApplicationSpecificInformation()
self.assertIsNone(app_specific_info.application_data)
args = (self.no_application_data_encoding, )
self.assertRaisesRegex(
exceptions.InvalidKmipEncoding,
"The ApplicationSpecificInformation encoding is missing the "
"ApplicationData field.",
app_specific_info.read,
*args
)
def test_write(self):
"""
Test that an ApplicationSpecificInformation object can be written to a
buffer.
"""
app_specific_info = attributes.ApplicationSpecificInformation(
application_namespace="ssl",
application_data="www.example.com"
)
buff = utils.BytearrayStream()
app_specific_info.write(buff)
self.assertEqual(len(self.full_encoding), len(buff))
self.assertEqual(str(self.full_encoding), str(buff))
def test_write_missing_application_namespace(self):
"""
Test that an InvalidField error is raised during the encoding of an
ApplicationSpecificInformation object when the object is missing the
application namespace field.
"""
app_specific_info = attributes.ApplicationSpecificInformation(
application_data="www.example.com"
)
buff = utils.BytearrayStream()
args = (buff, )
self.assertRaisesRegex(
exceptions.InvalidField,
"The ApplicationSpecificInformation object is missing the "
"ApplicationNamespace field.",
app_specific_info.write,
*args
)
def test_write_missing_application_data(self):
"""
Test that an InvalidField error is raised during the encoding of an
ApplicationSpecificInformation object when the object is missing the
application data field.
"""
app_specific_info = attributes.ApplicationSpecificInformation(
application_namespace="ssl"
)
buff = utils.BytearrayStream()
args = (buff, )
self.assertRaisesRegex(
exceptions.InvalidField,
"The ApplicationSpecificInformation object is missing the "
"ApplicationData field.",
app_specific_info.write,
*args
)
def test_repr(self):
"""
Test that an ApplicationSpecificInformation object can be represented
using repr correctly.
Test that repr can be applied to an ApplicationSpecificInformation
object.
"""
application_specific_info = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace("ssl"),
application_data=ApplicationData("www.example.com")
app_specific_info = attributes.ApplicationSpecificInformation(
application_namespace="ssl",
application_data="www.example.com"
)
s = repr(application_specific_info)
args = [
"application_namespace='ssl'",
"application_data='www.example.com'"
]
self.assertEqual(
"ApplicationSpecificInformation("
"application_namespace=ApplicationNamespace(value='ssl'), "
"application_data=ApplicationData(value='www.example.com'))",
s
"ApplicationSpecificInformation({})".format(", ".join(args)),
repr(app_specific_info)
)
def test_str(self):
"""
Test that an ApplicationSpecificInformation object can be turned into
a string correctly.
Test that str can be applied to an ApplicationSpecificInformation
object.
"""
application_specific_info = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace("ssl"),
application_data=ApplicationData("www.example.com")
app_specific_info = attributes.ApplicationSpecificInformation(
application_namespace="ssl",
application_data="www.example.com"
)
s = str(application_specific_info)
args = [
("application_namespace", "ssl"),
("application_data", "www.example.com")
]
value = "{}".format(
", ".join(['"{}": {}'.format(arg[0], arg[1]) for arg in args])
)
self.assertEqual(
str({'application_namespace': 'ssl',
'application_data': 'www.example.com'}
),
s
"{" + value + "}",
str(app_specific_info)
)
def test_equal_on_equal(self):
def test_comparison(self):
"""
Test that the equality operator returns True when comparing two
ApplicationSpecificInformation objects with the same data.
Test that the equality/inequality operators return True/False when
comparing two ApplicationSpecificInformation objects with the same
data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
a = attributes.ApplicationSpecificInformation()
b = attributes.ApplicationSpecificInformation()
self.assertTrue(a == b)
self.assertTrue(b == a)
self.assertFalse(a != b)
self.assertFalse(b != a)
a = attributes.ApplicationSpecificInformation(
application_namespace="test_namespace",
application_data="test_data"
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
b = attributes.ApplicationSpecificInformation(
application_namespace="test_namespace",
application_data="test_data"
)
self.assertTrue(a == b)
self.assertTrue(b == a)
def test_equal_on_not_equal_namespace(self):
"""
Test that the equality operator returns False when comparing two
ApplicationSpecificInformation objects with different data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace_1'),
application_data=ApplicationData('test_data')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace_2'),
application_data=ApplicationData('test_data')
)
self.assertFalse(a == b)
self.assertFalse(b == a)
def test_equal_on_not_equal_data(self):
"""
Test that the equality operator returns False when comparing two
ApplicationSpecificInformation objects with different data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data_1')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data_2')
)
self.assertFalse(a == b)
self.assertFalse(b == a)
def test_equal_on_type_mismatch(self):
"""
Test that the equality operator returns False when comparing a
ApplicationSpecificInformation object to a
non-ApplicationSpecificInformation object.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
b = "invalid"
self.assertFalse(a == b)
self.assertFalse(b == a)
def test_not_equal_on_equal(self):
"""
Test that the inequality operator returns False when comparing
two ApplicationSpecificInformation objects with the same internal
data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
self.assertFalse(a != b)
self.assertFalse(b != a)
def test_not_equal_on_not_equal_namespace(self):
def test_comparison_on_different_application_namespaces(self):
"""
Test that the inequality operator returns True when comparing two
ApplicationSpecificInformation objects with different data.
Test that the equality/inequality operators return False/True when
comparing two ApplicationSpecificInformation objects with different
data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace_1'),
application_data=ApplicationData('test_data')
a = attributes.ApplicationSpecificInformation(
application_namespace="test_namespace_1"
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace_2'),
application_data=ApplicationData('test_data')
b = attributes.ApplicationSpecificInformation(
application_namespace="test_namespace_2"
)
self.assertFalse(a == b)
self.assertFalse(b == a)
self.assertTrue(a != b)
self.assertTrue(b != a)
def test_not_equal_on_not_equal_data(self):
def test_comparison_on_different_application_data(self):
"""
Test that the inequality operator returns True when comparing two
ApplicationSpecificInformation objects with different data.
Test that the equality/inequality operators return False/True when
comparing two ApplicationSpecificInformation objects with different
data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data_1')
a = attributes.ApplicationSpecificInformation(
application_data="test_data_1"
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data_2')
b = attributes.ApplicationSpecificInformation(
application_data="test_data_2"
)
self.assertFalse(a == b)
self.assertFalse(b == a)
self.assertTrue(a != b)
self.assertTrue(b != a)
def test_not_equal_on_type_mismatch(self):
def test_comparison_on_type_mismatch(self):
"""
Test that the equality operator returns True when comparing a
ApplicationSpecificInformation object to a
Test that the equality/inequality operators return False/True when
comparing an ApplicationSpecificInformation object to a
non-ApplicationSpecificInformation object.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
a = attributes.ApplicationSpecificInformation(
application_namespace="test_namespace",
application_data="test_data"
)
b = "invalid"
self.assertTrue(a != b)
self.assertTrue(b != a)
def _test_create(self, application_namespace, application_data):
application_specific_info = ApplicationSpecificInformation.create(
application_namespace, application_data)
self.assertIsInstance(
application_specific_info, ApplicationSpecificInformation)
expected = ApplicationNamespace(application_namespace)
observed = application_specific_info.application_namespace
msg = "expected {0}, observed {1}".format(expected, observed)
self.assertEqual(expected, observed, msg)
expected = ApplicationData(application_data)
observed = application_specific_info.application_data
msg = "expected {0}, observed {1}".format(expected, observed)
self.assertEqual(expected, observed, msg)
def test_create_with_none(self):
"""
Test that an ApplicationSpecificInformation object with no data can be
created using the create class method.
"""
self._test_create(None, None)
def test_create_with_args(self):
"""
Test that an ApplicationSpecificInformation object with data can be
created using the create class method.
"""
application_namespace = "ssl"
application_data = "www.example.com"
self._test_create(application_namespace, application_data)
self.assertFalse(a == b)
self.assertFalse(b == a)

View File

@ -15,8 +15,6 @@
from testtools import TestCase
from kmip.core.attributes import ApplicationData
from kmip.core.attributes import ApplicationNamespace
from kmip.core.attributes import CertificateType
from kmip.core.attributes import CryptographicParameters
from kmip.core.attributes import DerivationParameters
@ -378,104 +376,6 @@ class TestDigestValue(TestCase):
self._test_init(b'\x00\x01\x02\x03')
class TestApplicationNamespace(TestCase):
"""
A test suite for the ApplicationNamespace class.
Since ApplicationNamespace is a simple wrapper for the TextString
primitive, only a few tests pertaining to construction are needed.
"""
def setUp(self):
super(TestApplicationNamespace, self).setUp()
def tearDown(self):
super(TestApplicationNamespace, self).tearDown()
def _test_init(self, value):
if (isinstance(value, str)) or (value is None):
application_namespace = ApplicationNamespace(value)
if value is None:
value = ''
msg = "expected {0}, observed {1}".format(
value, application_namespace.value)
self.assertEqual(value, application_namespace.value, msg)
else:
self.assertRaises(TypeError, ApplicationNamespace, value)
def test_init_with_none(self):
"""
Test that an ApplicationNamespace object can be constructed with no
specified value.
"""
self._test_init(None)
def test_init_with_valid(self):
"""
Test that an ApplicationNamespace object can be constructed with a
valid, string-type value.
"""
self._test_init("valid")
def test_init_with_invalid(self):
"""
Test that a TypeError exception is raised when a non-string value is
used to construct an ApplicationNamespace object.
"""
self._test_init(0)
class TestApplicationData(TestCase):
"""
A test suite for the ApplicationData class.
Since ApplicationData is a simple wrapper for the TextString primitive,
only a few tests pertaining to construction are needed.
"""
def setUp(self):
super(TestApplicationData, self).setUp()
def tearDown(self):
super(TestApplicationData, self).tearDown()
def _test_init(self, value):
if (isinstance(value, str)) or (value is None):
application_data = ApplicationData(value)
if value is None:
value = ''
msg = "expected {0}, observed {1}".format(
value, application_data.value)
self.assertEqual(value, application_data.value, msg)
else:
self.assertRaises(TypeError, ApplicationData, value)
def test_init_with_none(self):
"""
Test that an ApplicationData object can be constructed with no
specified value.
"""
self._test_init(None)
def test_init_with_valid(self):
"""
Test that an ApplicationData object can be constructed with a
valid, string-type value.
"""
self._test_init("valid")
def test_init_with_invalid(self):
"""
Test that a TypeError exception is raised when a non-string value is
used to construct an ApplicationData object.
"""
self._test_init(0)
class TestCryptographicParameters(TestCase):
"""
Test suite for the CryptographicParameters struct.

View File

@ -996,8 +996,8 @@ class TestGetAttributesResponsePayload(testtools.TestCase):
'Application Specific Information'
),
attribute_value=attributes.ApplicationSpecificInformation(
attributes.ApplicationNamespace('ssl'),
attributes.ApplicationData('www.example.com')
application_namespace="ssl",
application_data="www.example.com"
)
),
objects.Attribute(
@ -1166,8 +1166,8 @@ class TestGetAttributesResponsePayload(testtools.TestCase):
'Application Specific Information'
),
attribute_value=attributes.ApplicationSpecificInformation(
attributes.ApplicationNamespace('ssl'),
attributes.ApplicationData('www.example.com')
application_namespace="ssl",
application_data="www.example.com"
)
),
payload.attributes
@ -1271,8 +1271,8 @@ class TestGetAttributesResponsePayload(testtools.TestCase):
'Application Specific Information'
),
attribute_value=attributes.ApplicationSpecificInformation(
attributes.ApplicationNamespace('ssl'),
attributes.ApplicationData('www.example.com')
application_namespace="ssl",
application_data="www.example.com"
)
),
objects.Attribute(

View File

@ -22,9 +22,6 @@ from kmip.core.factories.secrets import SecretFactory
from kmip.core.factories.attributes import AttributeFactory
from kmip.core import attributes as attr
from kmip.core.attributes import ApplicationData
from kmip.core.attributes import ApplicationNamespace
from kmip.core.attributes import ApplicationSpecificInformation
from kmip.core.attributes import ContactInformation
from kmip.core.attributes import CryptographicAlgorithm
from kmip.core.attributes import CryptographicLength
@ -1017,10 +1014,10 @@ class TestRequestMessage(TestCase):
'Information')
ap_n_name = 'ssl'
ap_n_value = 'www.example.com'
ap_n = ApplicationNamespace(ap_n_name)
ap_d = ApplicationData(ap_n_value)
value = ApplicationSpecificInformation(application_namespace=ap_n,
application_data=ap_d)
value = attr.ApplicationSpecificInformation(
application_namespace=ap_n_name,
application_data=ap_n_value
)
attribute = objects.Attribute(attribute_name=name,
attribute_value=value)
attributes.append(attribute)