Merge pull request #75 from OpenKMIP/maint/split-primitive-tests

Splitting primitive object test suites
This commit is contained in:
Peter Hamilton 2015-08-24 09:03:06 -04:00
commit f3c202cf3c
11 changed files with 1596 additions and 1460 deletions

View File

@ -0,0 +1,212 @@
# Copyright (c) 2014 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 errors
from kmip.core import primitives
from kmip.core import utils
class TestBase(testtools.TestCase):
def setUp(self):
super(TestBase, self).setUp()
self.stream = utils.BytearrayStream()
self.bad_init = 'Bad Base initialization: attribute {0} missing'
self.bad_write = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.Base.{0}', 'write', '{1}', '{2}')
self.bad_encoding = errors.ErrorStrings.BAD_ENCODING.format(
'primitives.Base.{0}', 'write')
self.bad_match = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.Base.{0}', 'comparison', '{1}', '{2}')
def tearDown(self):
super(TestBase, self).tearDown()
def test_is_oversized(self):
base = primitives.Base()
base.is_oversized(self.stream)
def test_is_oversized_error(self):
self.stream.write(b'\x00')
base = primitives.Base()
self.assertRaises(
errors.StreamNotEmptyError, base.is_oversized, self.stream)
def test_read_tag(self):
encoding = (b'\x42\x00\x00')
base = primitives.Base()
self.stream = utils.BytearrayStream(encoding)
base.read_tag(self.stream)
def test_read_tag_invalid(self):
encoding = (b'\x42\x00\x01')
base = primitives.Base()
self.stream = utils.BytearrayStream(encoding)
self.assertRaises(errors.ReadValueError, base.read_tag, self.stream)
def test_read_type(self):
self.stream.write(b'\x00')
base = primitives.Base()
base.read_type(self.stream)
def test_read_type_error(self):
self.stream.write(b'\x01')
base = primitives.Base()
self.assertRaises(errors.ReadValueError, base.read_type, self.stream)
def test_read_type_underflow(self):
base = primitives.Base()
self.assertRaises(errors.ReadValueError, base.read_type, self.stream)
def test_read_type_overflow(self):
self.stream.write(b'\x00\x00')
base = primitives.Base()
base.read_type(self.stream)
def test_read_length(self):
self.stream.write(b'\x00\x00\x00\x04')
base = primitives.Base()
base.length = 4
base.read_length(self.stream)
def test_read_length_underflow(self):
self.stream.write(b'\x00')
base = primitives.Base()
base.length = 4
self.assertRaises(errors.ReadValueError, base.read_length, self.stream)
def test_read_length_overflow(self):
self.stream.write(b'\x00\x00\x00\x04\x00')
base = primitives.Base()
base.length = 4
base.read_length(self.stream)
def test_read_value(self):
base = primitives.Base()
self.assertRaises(
NotImplementedError, base.read_value, self.stream)
def test_read(self):
self.stream.write(b'\x42\x00\x00\x00\x00\x00\x00\x04')
base = primitives.Base()
base.length = 4
base.read(self.stream)
def test_write_tag(self):
encoding = (b'\x42\x00\x00')
base = primitives.Base()
base.write_tag(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(
len_exp, len_rcv,
self.bad_write.format(
'tag', '{0} bytes'.format(len_exp),
'{0} bytes'.format(len_rcv)))
self.assertEqual(encoding, result, self.bad_encoding.format('tag'))
def test_write_type(self):
encoding = b'\x00'
base = primitives.Base()
base.write_type(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(
len_exp, len_rcv,
self.bad_write.format(
'type', '{0} bytes'.format(len_exp),
'{0} bytes'.format(len_rcv)))
self.assertEqual(encoding, result, self.bad_encoding.format('type'))
def test_write_type_invalid(self):
base = primitives.Base()
base.type = ''
self.assertRaises(TypeError, base.write_type, self.stream)
def test_write_length(self):
encoding = b'\x00\x00\x00\x04'
base = primitives.Base()
base.length = 4
base.write_length(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(
len_exp, len_rcv,
self.bad_write.format(
'length', '{0} bytes'.format(len_exp),
'{0} bytes'.format(len_rcv)))
self.assertEqual(encoding, result, self.bad_encoding.format('length'))
def test_write_length_invalid(self):
base = primitives.Base()
base.length = ''
self.assertRaises(TypeError, base.write_length, self.stream)
def test_write_length_overflow(self):
self.skip('No easy way to test with a number requiring more than '
'2 ** 0xffffffff bytes for representation. Test preserved '
'for completeness.')
def test_write_value(self):
base = primitives.Base()
self.assertRaises(
NotImplementedError, base.write_value, self.stream)
def test_write(self):
encoding = b'\x42\x00\x00\x00\x00\x00\x00\x04'
base = primitives.Base()
base.length = 4
base.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(
len_exp, len_rcv,
self.bad_write.format(
'type/length', '{0} bytes'.format(len_exp),
'{0} bytes'.format(len_rcv)))
self.assertEqual(
encoding, result, self.bad_encoding.format('type/length'))
def test_is_tag_next(self):
encoding = (b'\x42\x00\x00')
base = primitives.Base()
self.stream = utils.BytearrayStream(encoding)
self.assertTrue(
base.is_tag_next(base.tag, self.stream),
self.bad_match.format('tag', 'match', 'mismatch'))
def test_is_tag_next_invalid(self):
encoding = (b'\x42\x00\x01')
base = primitives.Base()
self.stream = utils.BytearrayStream(encoding)
self.assertFalse(
base.is_tag_next(base.tag, self.stream),
self.bad_match.format('tag', 'mismatch', 'match'))

View File

@ -0,0 +1,232 @@
# Copyright (c) 2015 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 import errors
from kmip.core import primitives
from kmip.core import utils
class TestBigInteger(testtools.TestCase):
def setUp(self):
super(TestBigInteger, self).setUp()
self.stream = utils.BytearrayStream()
self.max_byte_long = 18446744073709551615
self.max_long = 9223372036854775807
self.bad_value = (
'Bad primitives.BigInteger.{0} after init: expected {1}, '
'received {2}')
self.bad_write = (
'Bad primitives.BigInteger write: expected {0} bytes, '
'received {1} bytes')
self.bad_encoding = (
'Bad primitives.BigInteger write: encoding mismatch')
self.bad_read = (
'Bad primitives.BigInteger.value read: expected {0}, '
'received {1}')
def tearDown(self):
super(TestBigInteger, self).tearDown()
def test_big_integer(self):
self.skip('primitives.BigInteger implementation incomplete')
i = primitives.BigInteger(0)
self.assertEqual(0, i.value,
self.bad_value.format('value', 0, i.value))
self.assertEqual(1, i.length,
self.bad_value.format('length', 1, i.length))
self.assertEqual(i.BLOCK_SIZE - 1, i.padding_length,
self.bad_value.format('padding_length',
i.BLOCK_SIZE - 1,
i.padding_length))
def test_big_integer_unset(self):
self.skip('primitives.BigInteger implementation incomplete')
i = primitives.BigInteger()
self.assertEqual(None, i.value,
self.bad_value.format('value', None, i.value))
self.assertEqual(None, i.length,
self.bad_value.format('length', None, i.length))
self.assertEqual(None, i.padding_length,
self.bad_value.format('padding_length', None,
i.padding_length))
def test_validate_on_valid(self):
self.skip('primitives.BigInteger implementation incomplete')
i = primitives.BigInteger()
i.value = 0
i.length = i.BLOCK_SIZE
i.padding_length = 0
# Check no exception thrown
i.validate()
def test_validate_on_valid_long(self):
self.skip('primitives.BigInteger implementation incomplete')
i = primitives.BigInteger()
i.value = self.max_long + 1
i.length = i.BLOCK_SIZE
i.padding_length = 0
# Check no exception thrown
i.validate()
def test_validate_on_valid_unset(self):
self.skip('primitives.BigInteger implementation incomplete')
i = primitives.BigInteger()
# Check no exception thrown
i.validate()
def test_validate_on_invalid_type(self):
self.skip('primitives.BigInteger implementation incomplete')
i = primitives.BigInteger()
i.value = 'test'
self.assertRaises(errors.StateTypeError, i.validate)
def test_write(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x01')
i = primitives.BigInteger(1)
i.TAG = enums.Tags.ACTIVATION_DATE
i.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_zero(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
i = primitives.BigInteger(0)
i.TAG = enums.Tags.ACTIVATION_DATE
i.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_max_positive_value(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x08\x7f\xff\xff\xff\xff\xff\xff'
b'\xff')
i = primitives.BigInteger(self.max_long)
i.TAG = enums.Tags.ACTIVATION_DATE
i.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_min_negative_value(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x08\xff\xff\xff\xff\xff\xff\xff'
b'\xff')
i = primitives.BigInteger(-1)
i.TAG = enums.Tags.ACTIVATION_DATE
i.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_read(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x01')
self.stream = utils.BytearrayStream(encoding)
i = primitives.BigInteger()
i.TAG = enums.Tags.ACTIVATION_DATE
i.read(self.stream)
self.assertEqual(1, i.value, self.bad_read.format(1, i.value))
def test_read_zero(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.BigInteger()
i.TAG = enums.Tags.ACTIVATION_DATE
i.read(self.stream)
self.assertEqual(0, i.value, self.bad_read.format(0, i.value))
def test_read_max_positive_value(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x08\x7f\xff\xff\xff\xff\xff\xff'
b'\xff')
self.stream = utils.BytearrayStream(encoding)
i = primitives.BigInteger()
i.TAG = enums.Tags.ACTIVATION_DATE
i.read(self.stream)
self.assertEqual(self.max_long, i.value,
self.bad_read.format(1, i.value))
def test_read_min_negative_value(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x08\xff\xff\xff\xff\xff\xff\xff'
b'\xff')
self.stream = utils.BytearrayStream(encoding)
i = primitives.BigInteger()
i.TAG = enums.Tags.ACTIVATION_DATE
i.read(self.stream)
self.assertEqual(-1, i.value,
self.bad_read.format(1, i.value))
def test_read_on_invalid_length(self):
self.skip('primitives.BigInteger implementation incomplete')
encoding = (
b'\x42\x00\x01\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.BigInteger()
i.TAG = enums.Tags.ACTIVATION_DATE
self.assertRaises(errors.InvalidLengthError, i.read, self.stream)

View File

@ -15,15 +15,15 @@
from testtools import TestCase
from kmip.core.primitives import Boolean
from kmip.core.utils import BytearrayStream
from kmip.core import primitives
from kmip.core import utils
class TestBoolean(TestCase):
def setUp(self):
super(TestBoolean, self).setUp()
self.stream = BytearrayStream()
self.stream = utils.BytearrayStream()
def tearDown(self):
super(TestBoolean, self).tearDown()
@ -32,28 +32,28 @@ class TestBoolean(TestCase):
"""
Test that a Boolean object can be instantiated.
"""
boolean = Boolean(False)
boolean = primitives.Boolean(False)
self.assertEqual(False, boolean.value)
def test_init_unset(self):
"""
Test that a Boolean object can be instantiated with no input.
"""
boolean = Boolean()
boolean = primitives.Boolean()
self.assertEqual(True, boolean.value)
def test_validate_on_valid(self):
"""
Test that a Boolean object can be validated on good input.
"""
boolean = Boolean(True)
boolean = primitives.Boolean(True)
boolean.validate()
def test_validate_on_valid_unset(self):
"""
Test that a Boolean object with no preset value can be validated.
"""
boolean = Boolean()
boolean = primitives.Boolean()
boolean.validate()
def test_validate_on_invalid_type(self):
@ -61,20 +61,19 @@ class TestBoolean(TestCase):
Test that a TypeError is raised when a Boolean object is built with an
invalid value.
"""
self.assertRaises(TypeError, Boolean, 'invalid')
self.assertRaises(TypeError, primitives.Boolean, 'invalid')
def test_read_true(self):
"""
Test that a Boolean object representing the value True can be read
from a byte stream.
"""
encoding = (b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00'
b'\x00\x01')
stream = BytearrayStream(encoding)
boolean = Boolean()
encoding = (
b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x01')
stream = utils.BytearrayStream(encoding)
boolean = primitives.Boolean()
boolean.read(stream)
self.assertTrue(boolean.value)
def test_read_false(self):
@ -82,13 +81,12 @@ class TestBoolean(TestCase):
Test that a Boolean object representing the value False can be read
from a byte stream.
"""
encoding = (b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00'
b'\x00\x00')
stream = BytearrayStream(encoding)
boolean = Boolean()
encoding = (
b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
stream = utils.BytearrayStream(encoding)
boolean = primitives.Boolean()
boolean.read(stream)
self.assertFalse(boolean.value)
def test_read_bad_encoding(self):
@ -97,21 +95,20 @@ class TestBoolean(TestCase):
on a bad encoding.
"""
encoding = (b'\x42\x00\x00\x06\x00\x00\x00\x08')
stream = BytearrayStream(encoding)
boolean = Boolean()
stream = utils.BytearrayStream(encoding)
boolean = primitives.Boolean()
self.assertRaises(Exception, boolean.read, stream)
def test_read_bad_value(self):
"""
Test that a ValueError is raised when the Boolean read operations
reads a valid integer but invalid boolean.
reads a valid integer but invalid Boolean.
"""
encoding = (b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00'
b'\x00\x02')
stream = BytearrayStream(encoding)
boolean = Boolean()
encoding = (
b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x02')
stream = utils.BytearrayStream(encoding)
boolean = primitives.Boolean()
self.assertRaises(ValueError, boolean.read, stream)
def test_write_true(self):
@ -119,13 +116,12 @@ class TestBoolean(TestCase):
Test that a Boolean object representing the value True can be written
to a byte stream.
"""
encoding = (b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00'
b'\x00\x01')
stream = BytearrayStream()
boolean = Boolean(True)
encoding = (
b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x01')
stream = utils.BytearrayStream()
boolean = primitives.Boolean(True)
boolean.write(stream)
self.assertEqual(encoding, stream.read())
def test_write_false(self):
@ -133,94 +129,75 @@ class TestBoolean(TestCase):
Test that a Boolean object representing the value False can be written
to a byte stream.
"""
encoding = (b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00'
b'\x00\x00')
stream = BytearrayStream()
boolean = Boolean(False)
encoding = (
b'\x42\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
stream = utils.BytearrayStream()
boolean = primitives.Boolean(False)
boolean.write(stream)
self.assertEqual(encoding, stream.read())
def test_write_bad_value(self):
"""
Test that an Exception is raised when the Boolean write operation fails
on a bad boolean value.
on a bad Boolean value.
"""
stream = BytearrayStream()
boolean = Boolean()
stream = utils.BytearrayStream()
boolean = primitives.Boolean()
boolean.value = 'invalid'
self.assertRaises(Exception, boolean.write, stream)
def test_repr_default(self):
"""
Test that the representation of a Boolean object is formatted properly
and can be used by eval to create a new Boolean object.
"""
boolean = Boolean()
boolean = primitives.Boolean()
self.assertEqual("Boolean(value=True)", repr(boolean))
self.assertEqual(boolean, eval(repr(boolean)))
def test_repr_true(self):
"""
Test that the representation of a Boolean object representing the
value True is formatted properly and can be used by eval to create a
new Boolean object.
value True is formatted properly.
"""
boolean = Boolean(True)
boolean = primitives.Boolean(True)
self.assertEqual("Boolean(value=True)", repr(boolean))
self.assertEqual(boolean, eval(repr(boolean)))
self.assertTrue(eval(repr(boolean)).value)
def test_repr_false(self):
"""
Test that the representation of a Boolean object representing the
value False is formatted properly and can be used by eval to create a
new Boolean object.
value False is formatted properly.
"""
boolean = Boolean(False)
boolean = primitives.Boolean(False)
self.assertEqual("Boolean(value=False)", repr(boolean))
self.assertEqual(boolean, eval(repr(boolean)))
self.assertFalse(eval(repr(boolean)).value)
def test_str_default(self):
"""
Test that the string representation of a Boolean object is formatted
properly.
"""
boolean = Boolean()
self.assertEqual("True", str(boolean))
self.assertEqual("True", str(primitives.Boolean()))
def test_str_true(self):
"""
Test that the string representation of a Boolean object representing
the value True is formatted properly.
"""
boolean = Boolean(True)
self.assertEqual("True", str(boolean))
self.assertEqual("True", str(primitives.Boolean(True)))
def test_str_false(self):
"""
Test that the string representation of a Boolean object representing
the value False is formatted properly.
"""
boolean = Boolean(False)
self.assertEqual("False", str(boolean))
self.assertEqual("False", str(primitives.Boolean(False)))
def test_equal_on_equal(self):
"""
Test that the equality operator returns True when comparing two
Boolean objects.
"""
a = Boolean(False)
b = Boolean(False)
a = primitives.Boolean(False)
b = primitives.Boolean(False)
self.assertTrue(a == b)
self.assertTrue(b == a)
@ -230,8 +207,8 @@ class TestBoolean(TestCase):
Test that the equality operator returns True when comparing two
Boolean objects.
"""
a = Boolean()
b = Boolean()
a = primitives.Boolean()
b = primitives.Boolean()
self.assertTrue(a == b)
self.assertTrue(b == a)
@ -241,8 +218,8 @@ class TestBoolean(TestCase):
Test that the equality operator returns False when comparing two
Boolean objects with different values.
"""
a = Boolean(True)
b = Boolean(False)
a = primitives.Boolean(True)
b = primitives.Boolean(False)
self.assertFalse(a == b)
self.assertFalse(b == a)
@ -252,7 +229,7 @@ class TestBoolean(TestCase):
Test that the equality operator returns False when comparing a
Boolean object to a non-Boolean object.
"""
a = Boolean()
a = primitives.Boolean()
b = 'invalid'
self.assertFalse(a == b)
@ -263,8 +240,8 @@ class TestBoolean(TestCase):
Test that the inequality operator returns False when comparing
two Boolean objects with the same values.
"""
a = Boolean(False)
b = Boolean(False)
a = primitives.Boolean(False)
b = primitives.Boolean(False)
self.assertFalse(a != b)
self.assertFalse(b != a)
@ -274,8 +251,8 @@ class TestBoolean(TestCase):
Test that the inequality operator returns False when comparing
two Boolean objects.
"""
a = Boolean()
b = Boolean()
a = primitives.Boolean()
b = primitives.Boolean()
self.assertFalse(a != b)
self.assertFalse(b != a)
@ -285,8 +262,8 @@ class TestBoolean(TestCase):
Test that the inequality operator returns True when comparing two
Boolean objects with different values.
"""
a = Boolean(True)
b = Boolean(False)
a = primitives.Boolean(True)
b = primitives.Boolean(False)
self.assertTrue(a != b)
self.assertTrue(b != a)
@ -296,7 +273,7 @@ class TestBoolean(TestCase):
Test that the inequality operator returns True when comparing a
Boolean object to a non-Boolean object.
"""
a = Boolean()
a = primitives.Boolean()
b = 'invalid'
self.assertTrue(a != b)

View File

@ -0,0 +1,222 @@
# Copyright (c) 2015 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 errors
from kmip.core import primitives
from kmip.core import utils
class TestByteString(testtools.TestCase):
def setUp(self):
super(TestByteString, self).setUp()
self.stream = utils.BytearrayStream()
self.bad_type = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString.{0}', 'type', '{1}', '{2}')
self.bad_value = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString.{0}', 'value', '{1}', '{2}')
self.bad_read = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString.{0}', '', '{1}', '{2}')
self.bad_write = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString.{0}', 'write', '{1}', '{2}')
self.bad_encoding = errors.ErrorStrings.BAD_ENCODING.format(
'primitives.ByteString', '')
self.bad_length = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString', 'length', '{0} bytes', '{1} bytes')
def tearDown(self):
super(TestByteString, self).tearDown()
def test_init(self):
value = b'\x01\x02\x03'
bs = primitives.ByteString(value)
self.assertIsInstance(bs.value, bytes,
self.bad_type.format('value', bytes,
type(bs.value)))
self.assertEqual(value, bs.value,
self.bad_value.format('value', value, bs.value))
def test_init_unset(self):
bs = primitives.ByteString()
self.assertIsInstance(bs.value, bytes,
self.bad_type.format('value', type(None),
type(bs.value)))
self.assertEqual(bytes(), bs.value,
self.bad_value.format('value', None, bs.value))
def test_validate_on_valid(self):
bs = primitives.ByteString()
bs.value = b'\x00'
# Check no exception thrown.
bs.validate()
def test_validate_on_valid_unset(self):
bs = primitives.ByteString()
# Check no exception thrown.
bs.validate()
def test_validate_on_invalid_type(self):
bs = primitives.ByteString()
bs.value = 0
self.assertRaises(TypeError, bs.validate)
def test_read_value(self):
encoding = b'\x01\x02\x03\x00\x00\x00\x00\x00'
self.stream = utils.BytearrayStream(encoding)
bs = primitives.ByteString()
bs.length = 0x03
bs.read_value(self.stream)
expected = b'\x01\x02\x03'
self.assertEqual(expected, bs.value,
self.bad_read.format('value', expected, bs.value))
def test_read_value_no_padding(self):
encoding = b'\x01\x02\x03\x04\x05\x06\x07\x08'
self.stream = utils.BytearrayStream(encoding)
bs = primitives.ByteString()
bs.length = 0x08
bs.read_value(self.stream)
expected = b'\x01\x02\x03\x04\x05\x06\x07\x08'
self.assertEqual(expected, bs.value,
self.bad_read.format('value', expected, bs.value))
def test_read_value_max_padding(self):
encoding = b'\x01\x00\x00\x00\x00\x00\x00\x00'
self.stream = utils.BytearrayStream(encoding)
bs = primitives.ByteString()
bs.length = 0x01
bs.read_value(self.stream)
expected = b'\x01'
self.assertEqual(expected, bs.value,
self.bad_read.format('value', expected, bs.value))
def test_read_value_zero(self):
encoding = b'\x00\x00\x00\x00\x00\x00\x00\x00'
self.stream = utils.BytearrayStream(encoding)
bs = primitives.ByteString()
bs.length = 0x01
bs.read_value(self.stream)
expected = b'\x00'
self.assertEqual(expected, bs.value,
self.bad_read.format('value', expected, bs.value))
def test_read(self):
encoding = (
b'\x42\x00\x00\x08\x00\x00\x00\x03\x01\x02\x03\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream(encoding)
bs = primitives.ByteString()
bs.read(self.stream)
expected = b'\x01\x02\x03'
self.assertEqual(expected, bs.value,
self.bad_read.format('value', expected, bs.value))
def test_read_on_invalid_padding(self):
encoding = (
b'\x42\x00\x00\x08\x00\x00\x00\x03\x01\x02\x03\xff\xff\xff\xff'
b'\xff')
self.stream = utils.BytearrayStream(encoding)
bs = primitives.ByteString()
self.assertRaises(errors.ReadValueError, bs.read, self.stream)
def test_write_value(self):
encoding = b'\x01\x02\x03\x00\x00\x00\x00\x00'
self.stream = utils.BytearrayStream()
value = b'\x01\x02\x03'
bs = primitives.ByteString(value)
bs.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_no_padding(self):
encoding = b'\x01\x02\x03\x04\x05\x06\x07\x08'
self.stream = utils.BytearrayStream()
value = b'\x01\x02\x03\x04\x05\x06\x07\x08'
bs = primitives.ByteString(value)
bs.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_max_padding(self):
encoding = b'\x01\x00\x00\x00\x00\x00\x00\x00'
self.stream = utils.BytearrayStream()
value = b'\x01'
bs = primitives.ByteString(value)
bs.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_zero(self):
encoding = b'\x00\x00\x00\x00\x00\x00\x00\x00'
self.stream = utils.BytearrayStream()
value = b'\x00'
bs = primitives.ByteString(value)
bs.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write(self):
encoding = (
b'\x42\x00\x00\x08\x00\x00\x00\x03\x01\x02\x03\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream()
value = b'\x01\x02\x03'
bs = primitives.ByteString(value)
bs.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)

View File

@ -0,0 +1,49 @@
# Copyright (c) 2015 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 utils
class TestDateTime(testtools.TestCase):
def setUp(self):
super(TestDateTime, self).setUp()
self.stream = utils.BytearrayStream()
def tearDown(self):
super(TestDateTime, self).tearDown()
def test_init(self):
self.skip('')
def test_init_unset(self):
self.skip('')
def test_validate_on_valid(self):
self.skip('')
def test_validate_on_valid_unset(self):
self.skip('')
def test_validate_on_invalid_type(self):
self.skip('')
def test_read(self):
self.skip('')
def test_write(self):
self.skip('')

View File

@ -0,0 +1,131 @@
# Copyright (c) 2015 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 import errors
from kmip.core import primitives
from kmip.core import utils
class TestEnumeration(testtools.TestCase):
def setUp(self):
super(TestEnumeration, self).setUp()
self.stream = utils.BytearrayStream()
primitives.Enumeration.ENUM_TYPE = enums.Types
self.bad_type = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.Enumeration.{0}', 'type', '{1}', '{2}')
self.bad_value = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.Enumeration.{0}', 'value', '{1}', '{2}')
self.bad_write = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.Enumeration', 'write', '{0} bytes', '{1} bytes')
self.bad_encoding = errors.ErrorStrings.BAD_ENCODING.format(
'primitives.Enumeration', 'write')
def tearDown(self):
super(TestEnumeration, self).tearDown()
def test_init(self):
e = primitives.Enumeration(enums.Types.DEFAULT)
self.assertIsInstance(e.enum, enums.Types,
self.bad_type.format('enum', enums.Types,
type(e.enum)))
self.assertEqual(
enums.Types.DEFAULT, e.enum,
self.bad_value.format('enum', enums.Types.DEFAULT, e.enum))
default = enums.Types.DEFAULT
self.assertEqual(default.value, e.value,
self.bad_value.format('value', default.value,
e.value))
def test_init_unset(self):
e = primitives.Enumeration()
self.assertEqual(None, e.enum,
self.bad_value.format('enum', None, e.enum))
self.assertEqual(0, e.value,
self.bad_value.format('value', 0, e.value))
def test_validate_on_valid(self):
e = primitives.Enumeration()
e.enum = enums.Types.DEFAULT
# Check no exception thrown
e.validate()
def test_validate_on_valid_unset(self):
e = primitives.Enumeration()
# Check no exception thrown
e.validate()
def test_validate_on_invalid_type(self):
e = primitives.Enumeration()
e.enum = 0
self.assertRaises(TypeError, e.validate)
def test_read(self):
encoding = (
b'\x42\x00\x00\x05\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream(encoding)
e = primitives.Enumeration()
e.read(self.stream)
self.assertIsInstance(e.enum, enums.Types,
self.bad_type.format('enum', enums.Types,
type(e.enum)))
self.assertEqual(enums.Types.DEFAULT, e.enum,
self.bad_value.format('enum', enums.Types.DEFAULT,
type(e.enum)))
default = enums.Types.DEFAULT
self.assertEqual(default.value, e.value,
self.bad_value.format('value', default.value,
e.value))
def test_write(self):
encoding = (
b'\x42\x00\x00\x05\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
e = primitives.Enumeration(enums.Types.DEFAULT)
e.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_unsigned(self):
"""
Test that a large primitives.Enumeration value is written correctly as
an unsigned integer.
"""
encoding = (
b'\x42\x00\x00\x05\x00\x00\x00\x04\x80\x00\x00\x00\x00\x00\x00'
b'\x00')
e = primitives.Enumeration(enums.OpaqueDataType.NONE)
e.write(self.stream)
result = self.stream.read()
self.assertEqual(len(encoding), len(result))
self.assertEqual(encoding, result)

View File

@ -0,0 +1,225 @@
# Copyright (c) 2015 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 errors
from kmip.core import primitives
from kmip.core import utils
class TestInteger(testtools.TestCase):
def setUp(self):
super(TestInteger, self).setUp()
self.stream = utils.BytearrayStream()
self.max_byte_int = 4294967295
self.max_int = 2147483647
self.bad_value = (
'Bad primitives.Integer.{0} after init: expected {1}, '
'received {2}')
self.bad_write = (
'Bad primitives.Integer write: expected {0} bytes, '
'received {1} bytes')
self.bad_encoding = 'Bad primitives.Integer write: encoding mismatch'
self.bad_read = (
'Bad primitives.Integer.value read: expected {0}, received {1}')
def tearDown(self):
super(TestInteger, self).tearDown()
def test_init(self):
i = primitives.Integer(0)
self.assertEqual(0, i.value,
self.bad_value.format('value', 0, i.value))
self.assertEqual(i.LENGTH, i.length,
self.bad_value.format('length', i.LENGTH, i.length))
self.assertEqual(i.LENGTH, i.padding_length,
self.bad_value.format('padding_length', i.LENGTH,
i.padding_length))
def test_init_unset(self):
i = primitives.Integer()
self.assertEqual(0, i.value,
self.bad_value.format('value', 0, i.value))
self.assertEqual(i.LENGTH, i.length,
self.bad_value.format('length', i.LENGTH, i.length))
self.assertEqual(i.LENGTH, i.padding_length,
self.bad_value.format('padding_length', i.LENGTH,
i.padding_length))
def test_validate_on_valid(self):
i = primitives.Integer()
i.value = 0
# Check no exception thrown
i.validate()
def test_validate_on_valid_unset(self):
i = primitives.Integer()
# Check no exception thrown
i.validate()
def test_validate_on_invalid_type(self):
"""
Test that a TypeError is thrown on input of invalid type (e.g., str).
"""
self.assertRaises(TypeError, primitives.Integer, 'invalid')
def test_validate_on_invalid_value_too_big(self):
"""
Test that a ValueError is thrown on input that is too large.
"""
self.assertRaises(
ValueError, primitives.Integer, primitives.Integer.MAX + 1)
def test_validate_on_invalid_value_too_small(self):
"""
Test that a ValueError is thrown on input that is too small.
"""
self.assertRaises(
ValueError, primitives.Integer, primitives.Integer.MIN - 1)
def test_read_value(self):
encoding = (b'\x00\x00\x00\x01\x00\x00\x00\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer()
i.read_value(self.stream)
self.assertEqual(1, i.value, self.bad_read.format(1, i.value))
def test_read_value_zero(self):
encoding = (b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer()
i.read_value(self.stream)
self.assertEqual(0, i.value, self.bad_read.format(0, i.value))
def test_read_value_max_positive(self):
encoding = (b'\x7f\xff\xff\xff\x00\x00\x00\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer()
i.read_value(self.stream)
self.assertEqual(self.max_int, i.value,
self.bad_read.format(1, i.value))
def test_read_value_min_negative(self):
encoding = (b'\xff\xff\xff\xff\x00\x00\x00\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer()
i.read_value(self.stream)
self.assertEqual(-1, i.value,
self.bad_read.format(1, i.value))
def test_read(self):
encoding = (
b'\x42\x00\x00\x02\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer()
i.read(self.stream)
self.assertEqual(1, i.value, self.bad_read.format(1, i.value))
def test_read_on_invalid_length(self):
encoding = (
b'\x42\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer()
self.assertRaises(errors.ReadValueError, i.read, self.stream)
def test_read_on_invalid_padding(self):
encoding = (
b'\x42\x00\x00\x02\x00\x00\x00\x04\x00\x00\x00\x00\xff\xff\xff'
b'\xff')
self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer()
self.assertRaises(errors.ReadValueError, i.read, self.stream)
def test_write_value(self):
encoding = (b'\x00\x00\x00\x01\x00\x00\x00\x00')
i = primitives.Integer(1)
i.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_zero(self):
encoding = (b'\x00\x00\x00\x00\x00\x00\x00\x00')
i = primitives.Integer(0)
i.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_max_positive(self):
encoding = (b'\x7f\xff\xff\xff\x00\x00\x00\x00')
i = primitives.Integer(self.max_int)
i.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_min_negative(self):
encoding = (b'\xff\xff\xff\xff\x00\x00\x00\x00')
i = primitives.Integer(-1)
i.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write(self):
encoding = (
b'\x42\x00\x00\x02\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00'
b'\x00')
i = primitives.Integer(1)
i.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)

View File

@ -0,0 +1,49 @@
# Copyright (c) 2015 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 utils
class TestInterval(testtools.TestCase):
def setUp(self):
super(TestInterval, self).setUp()
self.stream = utils.BytearrayStream()
def tearDown(self):
super(TestInterval, self).tearDown()
def test_init(self):
self.skip('')
def test_init_unset(self):
self.skip('')
def test_validate_on_valid(self):
self.skip('')
def test_validate_on_valid_unset(self):
self.skip('')
def test_validate_on_invalid_type(self):
self.skip('')
def test_read(self):
self.skip('')
def test_write(self):
self.skip('')

View File

@ -0,0 +1,209 @@
# Copyright (c) 2015 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 errors
from kmip.core import primitives
from kmip.core import utils
class TestLongInteger(testtools.TestCase):
def setUp(self):
super(TestLongInteger, self).setUp()
self.stream = utils.BytearrayStream()
self.max_byte_long = 18446744073709551615
self.max_long = 9223372036854775807
self.bad_value = (
'Bad primitives.LongInteger.{0} after init: expected {1}, '
'received {2}')
self.bad_write = (
'Bad primitives.LongInteger write: expected {0} bytes, '
'received {1} bytes')
self.bad_encoding = (
'Bad primitives.LongInteger write: encoding mismatch')
self.bad_read = (
'Bad primitives.LongInteger.value read: expected {0}, '
'received {1}')
def tearDown(self):
super(TestLongInteger, self).tearDown()
def test_init(self):
i = primitives.LongInteger(0)
self.assertEqual(0, i.value,
self.bad_value.format('value', 0, i.value))
self.assertEqual(i.LENGTH, i.length,
self.bad_value.format('length', i.LENGTH, i.length))
def test_init_unset(self):
i = primitives.LongInteger()
self.assertEqual(None, i.value,
self.bad_value.format('value', None, i.value))
self.assertEqual(i.LENGTH, i.length,
self.bad_value.format('length', i.LENGTH, i.length))
def test_validate_on_valid(self):
i = primitives.LongInteger()
i.value = 0
# Check no exception thrown
i.validate()
def test_validate_on_valid_long(self):
i = primitives.LongInteger()
i.value = self.max_long + 1
# Check no exception thrown
i.validate()
def test_validate_on_valid_unset(self):
i = primitives.LongInteger()
# Check no exception thrown
i.validate()
def test_validate_on_invalid_type(self):
i = primitives.LongInteger()
i.value = 'test'
self.assertRaises(errors.StateTypeError, i.validate)
def test_validate_on_invalid_value(self):
self.assertRaises(errors.StateOverflowError, primitives.LongInteger,
self.max_byte_long + 1)
def test_read_value(self):
encoding = (b'\x00\x00\x00\x00\x00\x00\x00\x01')
self.stream = utils.BytearrayStream(encoding)
i = primitives.LongInteger()
i.read_value(self.stream)
self.assertEqual(1, i.value, self.bad_read.format(1, i.value))
def test_read_value_zero(self):
encoding = (b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.LongInteger()
i.read_value(self.stream)
self.assertEqual(0, i.value, self.bad_read.format(0, i.value))
def test_read_value_max_positive(self):
encoding = (b'\x7f\xff\xff\xff\xff\xff\xff\xff')
self.stream = utils.BytearrayStream(encoding)
i = primitives.LongInteger()
i.read_value(self.stream)
self.assertEqual(self.max_long, i.value,
self.bad_read.format(1, i.value))
def test_read_value_min_negative(self):
encoding = (b'\xff\xff\xff\xff\xff\xff\xff\xff')
self.stream = utils.BytearrayStream(encoding)
i = primitives.LongInteger()
i.read_value(self.stream)
self.assertEqual(-1, i.value,
self.bad_read.format(1, i.value))
def test_read(self):
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x01')
self.stream = utils.BytearrayStream(encoding)
i = primitives.LongInteger()
i.read(self.stream)
self.assertEqual(1, i.value, self.bad_read.format(1, i.value))
def test_read_on_invalid_length(self):
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream(encoding)
i = primitives.LongInteger()
self.assertRaises(errors.ReadValueError, i.read, self.stream)
def test_write_value(self):
encoding = (b'\x00\x00\x00\x00\x00\x00\x00\x01')
i = primitives.LongInteger(1)
i.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_zero(self):
encoding = (b'\x00\x00\x00\x00\x00\x00\x00\x00')
i = primitives.LongInteger(0)
i.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_max_positive(self):
encoding = (b'\x7f\xff\xff\xff\xff\xff\xff\xff')
i = primitives.LongInteger(self.max_long)
i.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_min_negative(self):
encoding = (b'\xff\xff\xff\xff\xff\xff\xff\xff')
i = primitives.LongInteger(-1)
i.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write(self):
encoding = (
b'\x42\x00\x00\x03\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00'
b'\x01')
i = primitives.LongInteger(1)
i.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv, self.bad_write.format(len_exp,
len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,206 @@
# Copyright (c) 2015 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 six
import testtools
from kmip.core import errors
from kmip.core import primitives
from kmip.core import utils
class TestTextString(testtools.TestCase):
def setUp(self):
super(TestTextString, self).setUp()
self.stream = utils.BytearrayStream()
self.bad_type = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString.{0}', 'type', '{1}', '{2}')
self.bad_value = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString.{0}', 'value', '{1}', '{2}')
self.bad_read = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString.{0}', '', '{1}', '{2}')
self.bad_write = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString.{0}', 'write', '{1}', '{2}')
self.bad_encoding = errors.ErrorStrings.BAD_ENCODING.format(
'primitives.TextString', '')
self.bad_length = errors.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString', 'length', '{0} bytes', '{1} bytes')
def tearDown(self):
super(TestTextString, self).tearDown()
def test_init(self):
value = 'Hello World'
ts = primitives.TextString(value)
self.assertIsInstance(ts.value, str,
self.bad_type.format('value', str,
type(ts.value)))
self.assertEqual(value, ts.value,
self.bad_value.format('value', value, ts.value))
def test_init_unset(self):
text_string = primitives.TextString()
expected = six.string_types
observed = text_string.value
msg = "expected {0}, observed {1}".format(expected, observed)
self.assertIsInstance(observed, expected, msg)
expected = ''
msg = "expected {0}, observed {1}".format(expected, observed)
self.assertEqual(expected, observed, msg)
def test_validate_on_valid(self):
ts = primitives.TextString()
ts.value = 'Hello World'
# Check no exception thrown.
ts.validate()
def test_validate_on_valid_unset(self):
ts = primitives.TextString()
# Check no exception thrown.
ts.validate()
def test_validate_on_invalid_type(self):
ts = primitives.TextString()
ts.value = 0
self.assertRaises(TypeError, ts.validate)
def test_read_value(self):
encoding = (
b'\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream(encoding)
ts = primitives.TextString()
ts.length = 0x0B
ts.read_value(self.stream)
expected = 'Hello World'
self.assertEqual(expected, ts.value,
self.bad_read.format('value', expected, ts.value))
def test_read_value_no_padding(self):
encoding = (b'\x48\x65\x6C\x6C\x6F\x20\x57\x6F')
self.stream = utils.BytearrayStream(encoding)
ts = primitives.TextString()
ts.length = 0x08
ts.read_value(self.stream)
expected = 'Hello Wo'
self.assertEqual(expected, ts.value,
self.bad_read.format('value', expected, ts.value))
def test_read_value_max_padding(self):
encoding = (b'\x48\x00\x00\x00\x00\x00\x00\x00')
self.stream = utils.BytearrayStream(encoding)
ts = primitives.TextString()
ts.length = 0x01
ts.read_value(self.stream)
expected = 'H'
self.assertEqual(expected, ts.value,
self.bad_read.format('value', expected, ts.value))
def test_read(self):
encoding = (
b'\x42\x00\x00\x07\x00\x00\x00\x0B\x48\x65\x6C\x6C\x6F\x20\x57'
b'\x6F\x72\x6C\x64\x00\x00\x00\x00\x00')
self.stream = utils.BytearrayStream(encoding)
ts = primitives.TextString()
ts.read(self.stream)
expected = 'Hello World'
self.assertEqual(expected, ts.value,
self.bad_read.format('value', expected, ts.value))
def test_read_on_invalid_padding(self):
encoding = (
b'\x42\x00\x00\x07\x00\x00\x00\x0B\x48\x65\x6C\x6C\x6F\x20\x57'
b'\x6F\x72\x6C\x64\xff\xff\xff\xff\xff')
self.stream = utils.BytearrayStream(encoding)
ts = primitives.TextString()
self.assertRaises(errors.ReadValueError, ts.read, self.stream)
def test_write_value(self):
encoding = (
b'\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64\x00\x00\x00\x00'
b'\x00')
self.stream = utils.BytearrayStream()
value = 'Hello World'
ts = primitives.TextString(value)
ts.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_no_padding(self):
encoding = (b'\x48\x65\x6C\x6C\x6F\x20\x57\x6F')
self.stream = utils.BytearrayStream()
value = 'Hello Wo'
ts = primitives.TextString(value)
ts.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write_value_max_padding(self):
encoding = (b'\x48\x00\x00\x00\x00\x00\x00\x00')
self.stream = utils.BytearrayStream()
value = 'H'
ts = primitives.TextString(value)
ts.write_value(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)
def test_write(self):
encoding = (
b'\x42\x00\x00\x07\x00\x00\x00\x0B\x48\x65\x6C\x6C\x6F\x20\x57'
b'\x6F\x72\x6C\x64\x00\x00\x00\x00\x00')
self.stream = utils.BytearrayStream()
value = 'Hello World'
ts = primitives.TextString(value)
ts.write(self.stream)
result = self.stream.read()
len_exp = len(encoding)
len_rcv = len(result)
self.assertEqual(len_exp, len_rcv,
self.bad_length.format(len_exp, len_rcv))
self.assertEqual(encoding, result, self.bad_encoding)