Adding an AttributePolicy system

This change adds a policy system that will be used by the KmipEngine to
track and organize rules for individual KMIP attributes. Comparison
operators for the Integer primitive and ProtocolVersion struct are added
to support the AttributePolicy. Tests for all new changes are included.
This commit is contained in:
Peter 2016-03-18 12:46:40 -04:00
parent 20f078a6d8
commit 0a499b7b12
6 changed files with 1568 additions and 2 deletions

View File

@ -118,6 +118,58 @@ class ProtocolVersion(Struct):
else:
return NotImplemented
def __lt__(self, other):
if isinstance(other, ProtocolVersion):
if self.protocol_version_major < other.protocol_version_major:
return True
elif self.protocol_version_major > other.protocol_version_major:
return False
elif self.protocol_version_minor < other.protocol_version_minor:
return True
else:
return False
else:
return NotImplemented
def __gt__(self, other):
if isinstance(other, ProtocolVersion):
if self.protocol_version_major > other.protocol_version_major:
return True
elif self.protocol_version_major < other.protocol_version_major:
return False
elif self.protocol_version_minor > other.protocol_version_minor:
return True
else:
return False
else:
return NotImplemented
def __le__(self, other):
if isinstance(other, ProtocolVersion):
if self.protocol_version_major < other.protocol_version_major:
return True
elif self.protocol_version_major > other.protocol_version_major:
return False
elif self.protocol_version_minor <= other.protocol_version_minor:
return True
else:
return False
else:
return NotImplemented
def __ge__(self, other):
if isinstance(other, ProtocolVersion):
if self.protocol_version_major > other.protocol_version_major:
return True
elif self.protocol_version_major < other.protocol_version_major:
return False
elif self.protocol_version_minor >= other.protocol_version_minor:
return True
else:
return False
else:
return NotImplemented
def __repr__(self):
major = self.protocol_version_major.value
minor = self.protocol_version_minor.value

View File

@ -231,10 +231,10 @@ class Integer(Base):
raise ValueError('integer value less than accepted min')
def __repr__(self):
return "{0}(value={1})".format(type(self).__name__, repr(self.value))
return "{0}(value={1})".format(type(self).__name__, self.value)
def __str__(self):
return "{0}".format(repr(self.value))
return str(self.value)
def __eq__(self, other):
if isinstance(other, Integer):
@ -248,6 +248,30 @@ class Integer(Base):
else:
return NotImplemented
def __lt__(self, other):
if isinstance(other, Integer):
return self.value < other.value
else:
return NotImplemented
def __gt__(self, other):
if isinstance(other, Integer):
return self.value > other.value
else:
return NotImplemented
def __le__(self, other):
if isinstance(other, Integer):
return self.__eq__(other) or self.__lt__(other)
else:
return NotImplemented
def __ge__(self, other):
if isinstance(other, Integer):
return self.__eq__(other) or self.__gt__(other)
else:
return NotImplemented
class LongInteger(Base):
"""

File diff suppressed because it is too large Load Diff

View File

@ -169,6 +169,78 @@ class TestProtocolVersion(TestCase):
self.assertTrue(a != b)
def test_less_than(self):
"""
Test that the less than operator returns True/False when comparing
two different ProtocolVersions.
"""
a = ProtocolVersion.create(1, 0)
b = ProtocolVersion.create(1, 1)
c = ProtocolVersion.create(2, 0)
d = ProtocolVersion.create(0, 2)
self.assertTrue(a < b)
self.assertFalse(b < a)
self.assertFalse(a < a)
self.assertTrue(a < c)
self.assertFalse(c < a)
self.assertFalse(c < d)
self.assertTrue(d < c)
def test_greater_than(self):
"""
Test that the greater than operator returns True/False when
comparing two different ProtocolVersions.
"""
a = ProtocolVersion.create(1, 0)
b = ProtocolVersion.create(1, 1)
c = ProtocolVersion.create(2, 0)
d = ProtocolVersion.create(0, 2)
self.assertFalse(a > b)
self.assertTrue(b > a)
self.assertFalse(a > a)
self.assertFalse(a > c)
self.assertTrue(c > a)
self.assertTrue(c > d)
self.assertFalse(d > c)
def test_less_than_or_equal(self):
"""
Test that the less than or equal operator returns True/False when
comparing two different ProtocolVersions.
"""
a = ProtocolVersion.create(1, 0)
b = ProtocolVersion.create(1, 1)
c = ProtocolVersion.create(2, 0)
d = ProtocolVersion.create(0, 2)
self.assertTrue(a <= b)
self.assertFalse(b <= a)
self.assertTrue(a <= a)
self.assertTrue(a <= c)
self.assertFalse(c <= a)
self.assertFalse(c <= d)
self.assertTrue(d <= c)
def test_greater_than_or_equal(self):
"""
Test that the greater than or equal operator returns True/False when
comparing two different ProtocolVersions.
"""
a = ProtocolVersion.create(1, 0)
b = ProtocolVersion.create(1, 1)
c = ProtocolVersion.create(2, 0)
d = ProtocolVersion.create(0, 2)
self.assertFalse(a >= b)
self.assertTrue(b >= a)
self.assertTrue(a >= a)
self.assertFalse(a >= c)
self.assertTrue(c >= a)
self.assertTrue(c >= d)
self.assertFalse(d >= c)
def test_repr(self):
a = ProtocolVersion.create(1, 0)

View File

@ -223,3 +223,159 @@ class TestInteger(testtools.TestCase):
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_repr(self):
"""
Test that the representation of an Integer is formatted properly.
"""
integer = primitives.Integer()
value = "value={0}".format(integer.value)
self.assertEqual(
"Integer({0})".format(value), repr(integer))
def test_str(self):
"""
Test that the string representation of an Integer is formatted
properly.
"""
self.assertEqual("0", str(primitives.Integer()))
def test_equal_on_equal(self):
"""
Test that the equality operator returns True when comparing two
Integers.
"""
a = primitives.Integer(1)
b = primitives.Integer(1)
self.assertTrue(a == b)
self.assertTrue(b == a)
def test_equal_on_equal_and_empty(self):
"""
Test that the equality operator returns True when comparing two
Integers.
"""
a = primitives.Integer()
b = primitives.Integer()
self.assertTrue(a == b)
self.assertTrue(b == a)
def test_equal_on_not_equal(self):
"""
Test that the equality operator returns False when comparing two
Integers with different values.
"""
a = primitives.Integer(1)
b = primitives.Integer(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 an
Integer to a non-Integer object.
"""
a = primitives.Integer()
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 Integers with the same values.
"""
a = primitives.Integer(1)
b = primitives.Integer(1)
self.assertFalse(a != b)
self.assertFalse(b != a)
def test_not_equal_on_equal_and_empty(self):
"""
Test that the inequality operator returns False when comparing
two Integers.
"""
a = primitives.Integer()
b = primitives.Integer()
self.assertFalse(a != b)
self.assertFalse(b != a)
def test_not_equal_on_not_equal(self):
"""
Test that the inequality operator returns True when comparing two
Integers with different values.
"""
a = primitives.Integer(1)
b = primitives.Integer(2)
self.assertTrue(a != b)
self.assertTrue(b != a)
def test_not_equal_on_type_mismatch(self):
"""
Test that the inequality operator returns True when comparing an
Integer to a non-Integer object.
"""
a = primitives.Integer()
b = 'invalid'
self.assertTrue(a != b)
self.assertTrue(b != a)
def test_less_than(self):
"""
Test that the less than operator returns True/False when comparing
two Integers with different values.
"""
a = primitives.Integer(1)
b = primitives.Integer(2)
self.assertTrue(a < b)
self.assertFalse(b < a)
self.assertFalse(a < a)
def test_greater_than(self):
"""
Test that the greater than operator returns True/False when comparing
two Integers with different values.
"""
a = primitives.Integer(1)
b = primitives.Integer(2)
self.assertFalse(a > b)
self.assertTrue(b > a)
self.assertFalse(b > b)
def test_less_than_or_equal(self):
"""
Test that the less than or equal operator returns True/False when
comparing two Integers with different values.
"""
a = primitives.Integer(1)
b = primitives.Integer(2)
c = primitives.Integer(1)
self.assertTrue(a <= b)
self.assertFalse(b <= c)
self.assertTrue(a <= c)
self.assertTrue(a <= a)
def test_greater_than_or_equal(self):
"""
Test that the greater than or equal operator returns True/False when
comparing two Integers with different values.
"""
a = primitives.Integer(1)
b = primitives.Integer(2)
c = primitives.Integer(1)
self.assertFalse(a >= b)
self.assertTrue(b >= c)
self.assertTrue(a >= c)
self.assertTrue(a >= a)

View File

@ -0,0 +1,114 @@
# Copyright (c) 2016 The Johns Hopkins University/Applied Physics Laboratory
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import testtools
from kmip.core import enums
from kmip.core.messages import contents
from kmip.services.server import policy
class TestAttributePolicy(testtools.TestCase):
"""
A test engine for AttributePolicy.
"""
def setUp(self):
super(TestAttributePolicy, self).setUp()
def tearDown(self):
super(TestAttributePolicy, self).tearDown()
def test_init(self):
"""
Test that an AttributePolicy can be built without any errors.
"""
policy.AttributePolicy(contents.ProtocolVersion.create(1, 0))
def test_is_attribute_supported(self):
"""
Test that is_attribute_supported returns the expected results in all
cases.
"""
rules = policy.AttributePolicy(contents.ProtocolVersion.create(1, 0))
attribute_a = 'Unique Identifier'
attribute_b = 'Certificate Length'
attribute_c = 'invalid'
result = rules.is_attribute_supported(attribute_a)
self.assertTrue(result)
result = rules.is_attribute_supported(attribute_b)
self.assertFalse(result)
result = rules.is_attribute_supported(attribute_c)
self.assertFalse(result)
def test_is_attribute_deprecated(self):
"""
Test that is_attribute_deprecated returns the expected results in all
cases.
"""
rules = policy.AttributePolicy(contents.ProtocolVersion.create(1, 0))
attribute_a = 'Name'
attribute_b = 'Certificate Subject'
result = rules.is_attribute_deprecated(attribute_a)
self.assertFalse(result)
result = rules.is_attribute_deprecated(attribute_b)
self.assertFalse(result)
rules = policy.AttributePolicy(contents.ProtocolVersion.create(1, 1))
result = rules.is_attribute_deprecated(attribute_b)
self.assertTrue(result)
def test_is_attribute_applicable_to_object_type(self):
"""
Test that is_attribute_applicable_to_object_type returns the
expected results in all cases.
"""
rules = policy.AttributePolicy(contents.ProtocolVersion.create(1, 0))
attribute = 'Cryptographic Algorithm'
object_type_a = enums.ObjectType.SYMMETRIC_KEY
object_type_b = enums.ObjectType.OPAQUE_DATA
result = rules.is_attribute_applicable_to_object_type(
attribute,
object_type_a
)
self.assertTrue(result)
result = rules.is_attribute_applicable_to_object_type(
attribute,
object_type_b
)
self.assertFalse(result)
def test_is_attribute_multivalued(self):
"""
Test that is_attribute_multivalued returns the expected results in
all cases.
"""
rules = policy.AttributePolicy(contents.ProtocolVersion.create(1, 0))
attribute_a = 'Object Type'
attribute_b = 'Link'
result = rules.is_attribute_multivalued(attribute_a)
self.assertFalse(result)
result = rules.is_attribute_multivalued(attribute_b)
self.assertTrue(result)