mirror of https://github.com/OpenKMIP/PyKMIP.git
Block Attributes encoding and decoding on non KMIP 2.0 calls
This change adds a check to the read and write methods of the new Attributes object that raises a new VersionNotSupported exception if KMIP 2.0 is not the version used for encoding and decoding. The Attributes object is not defined for older versions of KMIP and therefore cannot be correctly encoded or decoded in those use cases.
This commit is contained in:
parent
24f30d46b2
commit
54f3688a14
|
@ -324,6 +324,12 @@ class ShutdownError(Exception):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class VersionNotSupported(Exception):
|
||||||
|
"""
|
||||||
|
An error generated when an unsupported KMIP version is referenced.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class StreamNotEmptyError(Exception):
|
class StreamNotEmptyError(Exception):
|
||||||
def __init__(self, cls, extra):
|
def __init__(self, cls, extra):
|
||||||
super(StreamNotEmptyError, self).__init__()
|
super(StreamNotEmptyError, self).__init__()
|
||||||
|
|
|
@ -258,7 +258,16 @@ class Attributes(primitives.Struct):
|
||||||
Raises:
|
Raises:
|
||||||
AttributeNotSupported: Raised if an unsupported attribute is
|
AttributeNotSupported: Raised if an unsupported attribute is
|
||||||
encountered while decoding.
|
encountered while decoding.
|
||||||
|
VersionNotSupported: Raised when a KMIP version is provided that
|
||||||
|
does not support the Attributes object.
|
||||||
"""
|
"""
|
||||||
|
if kmip_version < enums.KMIPVersion.KMIP_2_0:
|
||||||
|
raise exceptions.VersionNotSupported(
|
||||||
|
"KMIP {} does not support the Attributes object.".format(
|
||||||
|
kmip_version.value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
super(Attributes, self).read(input_stream, kmip_version=kmip_version)
|
super(Attributes, self).read(input_stream, kmip_version=kmip_version)
|
||||||
local_stream = BytearrayStream(input_stream.read(self.length))
|
local_stream = BytearrayStream(input_stream.read(self.length))
|
||||||
|
|
||||||
|
@ -297,7 +306,16 @@ class Attributes(primitives.Struct):
|
||||||
Raises:
|
Raises:
|
||||||
AttributeNotSupported: Raised if an unsupported attribute is
|
AttributeNotSupported: Raised if an unsupported attribute is
|
||||||
found in the attribute list while encoding.
|
found in the attribute list while encoding.
|
||||||
|
VersionNotSupported: Raised when a KMIP version is provided that
|
||||||
|
does not support the Attributes object.
|
||||||
"""
|
"""
|
||||||
|
if kmip_version < enums.KMIPVersion.KMIP_2_0:
|
||||||
|
raise exceptions.VersionNotSupported(
|
||||||
|
"KMIP {} does not support the Attributes object.".format(
|
||||||
|
kmip_version.value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
local_stream = BytearrayStream()
|
local_stream = BytearrayStream()
|
||||||
|
|
||||||
for attribute in self._attributes:
|
for attribute in self._attributes:
|
||||||
|
|
|
@ -400,6 +400,24 @@ class TestAttributes(TestCase):
|
||||||
self.assertIsInstance(attr_2, primitives.Integer)
|
self.assertIsInstance(attr_2, primitives.Integer)
|
||||||
self.assertEqual(128, attr_2.value)
|
self.assertEqual(128, attr_2.value)
|
||||||
|
|
||||||
|
def test_read_version_not_supported(self):
|
||||||
|
"""
|
||||||
|
Test that a VersionNotSupported error is raised when an unsupported
|
||||||
|
KMIP version is provided while reading in an Attributes structure from
|
||||||
|
a data stream. The Attributes structure is only supported in KMIP 2.0+.
|
||||||
|
"""
|
||||||
|
attrs = objects.Attributes()
|
||||||
|
|
||||||
|
args = (self.full_encoding, )
|
||||||
|
kwargs = {"kmip_version": enums.KMIPVersion.KMIP_1_2}
|
||||||
|
self.assertRaisesRegex(
|
||||||
|
exceptions.VersionNotSupported,
|
||||||
|
"KMIP 1.2 does not support the Attributes object.",
|
||||||
|
attrs.read,
|
||||||
|
*args,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
def test_write(self):
|
def test_write(self):
|
||||||
"""
|
"""
|
||||||
Test that an Attributes structure can be correctly written to a data
|
Test that an Attributes structure can be correctly written to a data
|
||||||
|
@ -489,6 +507,30 @@ class TestAttributes(TestCase):
|
||||||
self.assertEqual(len(self.alt_encoding), len(stream))
|
self.assertEqual(len(self.alt_encoding), len(stream))
|
||||||
self.assertEqual(str(self.alt_encoding), str(stream))
|
self.assertEqual(str(self.alt_encoding), str(stream))
|
||||||
|
|
||||||
|
def test_write_version_not_supported(self):
|
||||||
|
"""
|
||||||
|
Test that a VersionNotSupported error is raised when an unsupported
|
||||||
|
KMIP version is provided while writing an Attributes structure to a
|
||||||
|
data stream. The Attributes structure is only supported in KMIP 2.0+.
|
||||||
|
"""
|
||||||
|
attrs = objects.Attributes(attributes=[
|
||||||
|
primitives.TextString(
|
||||||
|
"default",
|
||||||
|
tag=enums.Tags.OPERATION_POLICY_NAME
|
||||||
|
)
|
||||||
|
])
|
||||||
|
|
||||||
|
stream = utils.BytearrayStream()
|
||||||
|
args = (stream, )
|
||||||
|
kwargs = {"kmip_version": enums.KMIPVersion.KMIP_1_1}
|
||||||
|
self.assertRaisesRegex(
|
||||||
|
exceptions.VersionNotSupported,
|
||||||
|
"KMIP 1.1 does not support the Attributes object.",
|
||||||
|
attrs.write,
|
||||||
|
*args,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
"""
|
"""
|
||||||
Test that repr can be applied to an Attributes structure.
|
Test that repr can be applied to an Attributes structure.
|
||||||
|
|
Loading…
Reference in New Issue