Merge pull request #384 from OpenKMIP/feat/remove-unused-errors

Remove unused error code
This commit is contained in:
Peter Hamilton 2017-12-08 14:20:43 -05:00 committed by GitHub
commit 07be1cd585
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 247 additions and 220 deletions

View File

@ -16,13 +16,12 @@
import six import six
from kmip.core import enums from kmip.core import enums
from kmip.core import exceptions
from kmip.core.enums import HashingAlgorithm as HashingAlgorithmEnum from kmip.core.enums import HashingAlgorithm as HashingAlgorithmEnum
from kmip.core.enums import KeyFormatType as KeyFormatTypeEnum from kmip.core.enums import KeyFormatType as KeyFormatTypeEnum
from kmip.core.enums import Tags from kmip.core.enums import Tags
from kmip.core.errors import ErrorStrings
from kmip.core.misc import KeyFormatType from kmip.core.misc import KeyFormatType
from kmip.core.primitives import Boolean from kmip.core.primitives import Boolean
@ -138,7 +137,7 @@ class Name(Struct):
def __validate(self): def __validate(self):
name = Name.__name__ name = Name.__name__
msg = ErrorStrings.BAD_EXP_RECV msg = exceptions.ErrorStrings.BAD_EXP_RECV
if self.name_value and \ if self.name_value and \
not isinstance(self.name_value, Name.NameValue) and \ not isinstance(self.name_value, Name.NameValue) and \
not isinstance(self.name_value, str): not isinstance(self.name_value, str):
@ -165,7 +164,7 @@ class Name(Struct):
value = cls.NameValue(name_value) value = cls.NameValue(name_value)
else: else:
name = 'Name' name = 'Name'
msg = ErrorStrings.BAD_EXP_RECV msg = exceptions.ErrorStrings.BAD_EXP_RECV
member = 'name_value' member = 'name_value'
raise TypeError(msg.format('{0}.{1}'.format(name, member), raise TypeError(msg.format('{0}.{1}'.format(name, member),
'name_value', type(Name.NameValue), 'name_value', type(Name.NameValue),
@ -177,7 +176,7 @@ class Name(Struct):
n_type = cls.NameType(name_type) n_type = cls.NameType(name_type)
else: else:
name = 'Name' name = 'Name'
msg = ErrorStrings.BAD_EXP_RECV msg = exceptions.ErrorStrings.BAD_EXP_RECV
member = 'name_type' member = 'name_type'
raise TypeError(msg.format('{0}.{1}'.format(name, member), raise TypeError(msg.format('{0}.{1}'.format(name, member),
'name_type', type(Name.NameType), 'name_type', type(Name.NameType),

View File

@ -1,130 +0,0 @@
# 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.
class ErrorStrings:
BAD_EXP_RECV = "Bad {0} {1}: expected {2}, received {3}"
BAD_ENCODING = "Bad {0} {1}: encoding mismatch"
class BaseError(Exception):
"""Base class for exceptions defined in this module."""
def __init__(self, args):
[setattr(self, k, v) for k, v in args.items() if k is not 'self']
class KMIPServerError(BaseError):
"""Base Exception for KMIP server errors."""
def __init__(self, args):
super(KMIPServerError, self).__init__(args)
class KMIPServerZombieError(KMIPServerError):
"""KMIP server error for hung and persistent live KMIP servers."""
def __init__(self, pid):
message = 'KMIP server alive after termination: PID {0}'.format(pid)
super(KMIPServerZombieError, self).__init__({'message': message})
def __str__(self):
return self.message
class KMIPServerSuicideError(KMIPServerError):
"""KMIP server error for prematurely dead KMIP servers."""
def __init__(self, pid):
message = 'KMIP server dead prematurely: PID {0}'.format(pid)
super(KMIPServerSuicideError, self).__init__({'message': message})
def __str__(self):
return self.message
class InitError(BaseError):
"""Exception thrown for bad initializations."""
def __init__(self, cls, exp, recv):
super(InitError, self).__init__(locals())
def __str__(self):
msg = "Tried to initialize {0} instance with bad type: "
msg += "expected {1}, received {2}"
return msg.format(self.cls, self.exp, self.recv)
class WriteValueError(BaseError):
def __init__(self, cls, attr, value):
super(WriteValueError, self).__init__(locals())
def __str__(self):
msg = "Tried to write {0}.{1} with invalid value: {2}"
return msg.format(self.cls, self.attr, self.value)
class WriteTypeError(BaseError):
def __init__(self, cls, attr, value):
super(WriteTypeError, self).__init__(locals())
def __str__(self):
msg = "Tried to write {0}.{1} with invalid type: {2}"
return msg.format(self.cls, self.attr, self.value)
class WriteOverflowError(BaseError):
def __init__(self, cls, attr, exp, recv):
super(WriteOverflowError, self).__init__(locals())
def __str__(self):
msg = "Tried to write {0}.{1} with too many bytes: "
msg += "expected {2}, received {3}"
return msg.format(self.cls, self.attr, self.exp, self.recv)
class ReadValueError(BaseError):
def __init__(self, cls, attr, exp, recv):
super(ReadValueError, self).__init__(locals())
def __str__(self):
msg = "Tried to read {0}.{1}: expected {2}, received {3}"
return msg.format(self.cls, self.attr, self.exp, self.recv)
class InvalidLengthError(ValueError):
def __init__(self, cls, exp, recv):
msg = "Invalid length read for {0}: expected {1}, received {2}"
super(InvalidLengthError, self).__init__(msg.format(cls, exp, recv))
class StreamNotEmptyError(BaseError):
def __init__(self, cls, extra):
super(StreamNotEmptyError, self).__init__(locals())
def __str__(self):
msg = "Invalid length used to read {0}, bytes remaining: {1}"
return msg.format(self.cls, self.extra)
class StateTypeError(TypeError):
def __init__(self, cls, exp, recv):
msg = "Tried to initialize {0} instance with bad type: "
msg += "expected {1}, received {2}"
super(StateTypeError, self).__init__(msg.format(cls, exp, recv))
class StateOverflowError(ValueError):
def __init__(self, cls, attr, exp, recv):
msg = "Tried to write {0}.{1} with too many bytes: "
msg += "expected {2}, received {3}"
super(StateOverflowError, self).__init__(msg.format(cls, attr, exp,
recv))

View File

@ -290,3 +290,73 @@ class InvalidPrimitiveLength(Exception):
lengths. lengths.
""" """
pass pass
class StreamNotEmptyError(Exception):
def __init__(self, cls, extra):
super(StreamNotEmptyError, self).__init__()
self.cls = cls
self.extra = extra
def __str__(self):
msg = "Invalid length used to read {0}, bytes remaining: {1}"
return msg.format(self.cls, self.extra)
class ReadValueError(Exception):
def __init__(self, cls, attr, exp, recv):
super(ReadValueError, self).__init__()
self.cls = cls
self.attr = attr
self.exp = exp
self.recv = recv
def __str__(self):
msg = "Tried to read {0}.{1}: expected {2}, received {3}"
return msg.format(self.cls, self.attr, self.exp, self.recv)
class WriteOverflowError(Exception):
def __init__(self, cls, attr, exp, recv):
super(WriteOverflowError, self).__init__()
self.cls = cls
self.attr = attr
self.exp = exp
self.recv = recv
def __str__(self):
msg = "Tried to write {0}.{1} with too many bytes: "
msg += "expected {2}, received {3}"
return msg.format(self.cls, self.attr, self.exp, self.recv)
class KMIPServerZombieError(Exception):
"""KMIP server error for hung and persistent live KMIP servers."""
def __init__(self, pid):
super(KMIPServerZombieError, self).__init__()
self.message = 'KMIP server alive after termination: PID {0}'.format(
pid
)
def __str__(self):
return self.message
class KMIPServerSuicideError(Exception):
"""KMIP server error for prematurely dead KMIP servers."""
def __init__(self, pid):
super(KMIPServerSuicideError, self).__init__()
self.message = 'KMIP server dead prematurely: PID {0}'.format(pid)
def __str__(self):
return self.message
class ErrorStrings:
BAD_EXP_RECV = "Bad {0} {1}: expected {2}, received {3}"
BAD_ENCODING = "Bad {0} {1}: encoding mismatch"

View File

@ -17,7 +17,6 @@ from kmip.core.attributes import CryptographicAlgorithm
from kmip.core.attributes import CryptographicLength from kmip.core.attributes import CryptographicLength
from kmip.core.enums import ObjectType from kmip.core.enums import ObjectType
from kmip.core.errors import ErrorStrings
from kmip.core.misc import KeyFormatType from kmip.core.misc import KeyFormatType
@ -36,12 +35,13 @@ from kmip.core.secrets import SymmetricKey
from kmip.core.secrets import Template from kmip.core.secrets import Template
from kmip.core import utils from kmip.core import utils
from kmip.core import exceptions
class SecretFactory(object): class SecretFactory(object):
def __init__(self): def __init__(self):
self.base_error = ErrorStrings.BAD_EXP_RECV self.base_error = exceptions.ErrorStrings.BAD_EXP_RECV
self.template_input = self.base_error.format('Template', '{0}', '{1}', self.template_input = self.base_error.format('Template', '{0}', '{1}',
'{2}') '{2}')

View File

@ -27,8 +27,8 @@ from kmip.core.enums import Tags
from kmip.core.enums import Types from kmip.core.enums import Types
from kmip.core.enums import CredentialType from kmip.core.enums import CredentialType
from kmip.core.enums import RevocationReasonCode as RevocationReasonCodeEnum from kmip.core.enums import RevocationReasonCode as RevocationReasonCodeEnum
from kmip.core import exceptions
from kmip.core.errors import ErrorStrings
from kmip.core.misc import KeyFormatType from kmip.core.misc import KeyFormatType
from kmip.core import primitives from kmip.core import primitives
@ -474,8 +474,12 @@ class KeyBlock(Struct):
member = 'KeyBlock.key_format_type' member = 'KeyBlock.key_format_type'
exp_type = KeyFormatType exp_type = KeyFormatType
rcv_type = type(self.key_format_type) rcv_type = type(self.key_format_type)
msg = ErrorStrings.BAD_EXP_RECV.format(member, 'type', msg = exceptions.ErrorStrings.BAD_EXP_RECV.format(
exp_type, rcv_type) member,
'type',
exp_type,
rcv_type
)
raise TypeError(msg) raise TypeError(msg)

View File

@ -22,10 +22,7 @@ import time
from struct import pack, unpack from struct import pack, unpack
from kmip.core.errors import ErrorStrings
from kmip.core import enums from kmip.core import enums
from kmip.core import errors
from kmip.core import exceptions from kmip.core import exceptions
from kmip.core import utils from kmip.core import utils
@ -45,7 +42,7 @@ class Base(object):
def is_oversized(self, stream): def is_oversized(self, stream):
extra = len(stream.peek()) extra = len(stream.peek())
if extra > 0: if extra > 0:
raise errors.StreamNotEmptyError(Base.__name__, extra) raise exceptions.StreamNotEmptyError(Base.__name__, extra)
def read_tag(self, istream): def read_tag(self, istream):
# Read in the bytes for the tag # Read in the bytes for the tag
@ -56,8 +53,12 @@ class Base(object):
# Verify that the tag matches for the current object # Verify that the tag matches for the current object
if enum_tag is not self.tag: if enum_tag is not self.tag:
raise errors.ReadValueError(Base.__name__, 'tag', raise exceptions.ReadValueError(
hex(self.tag.value), hex(tag)) Base.__name__,
'tag',
hex(self.tag.value),
hex(tag)
)
def read_type(self, istream): def read_type(self, istream):
# Read in the bytes for the type # Read in the bytes for the type
@ -65,15 +66,23 @@ class Base(object):
num_bytes = len(tts) num_bytes = len(tts)
if num_bytes != self.TYPE_SIZE: if num_bytes != self.TYPE_SIZE:
min_bytes = 'a minimum of {0} bytes'.format(self.TYPE_SIZE) min_bytes = 'a minimum of {0} bytes'.format(self.TYPE_SIZE)
raise errors.ReadValueError(Base.__name__, 'type', min_bytes, raise exceptions.ReadValueError(
'{0} bytes'.format(num_bytes)) Base.__name__,
'type',
min_bytes,
'{0} bytes'.format(num_bytes)
)
typ = unpack('!B', tts)[0] typ = unpack('!B', tts)[0]
enum_typ = enums.Types(typ) enum_typ = enums.Types(typ)
if enum_typ is not self.type: if enum_typ is not self.type:
raise errors.ReadValueError(Base.__name__, 'type', raise exceptions.ReadValueError(
self.type.value, typ) Base.__name__,
'type',
self.type.value,
typ
)
def read_length(self, istream): def read_length(self, istream):
# Read in the bytes for the length # Read in the bytes for the length
@ -81,8 +90,12 @@ class Base(object):
num_bytes = len(lst) num_bytes = len(lst)
if num_bytes != self.LENGTH_SIZE: if num_bytes != self.LENGTH_SIZE:
min_bytes = 'a minimum of {0} bytes'.format(self.LENGTH_SIZE) min_bytes = 'a minimum of {0} bytes'.format(self.LENGTH_SIZE)
raise errors.ReadValueError(Base.__name__, 'length', min_bytes, raise exceptions.ReadValueError(
'{0} bytes'.format(num_bytes)) Base.__name__,
'length',
min_bytes,
'{0} bytes'.format(num_bytes)
)
self.length = unpack('!I', lst)[0] self.length = unpack('!I', lst)[0]
def read_value(self, istream): def read_value(self, istream):
@ -99,20 +112,24 @@ class Base(object):
def write_type(self, ostream): def write_type(self, ostream):
if type(self.type) is not enums.Types: if type(self.type) is not enums.Types:
msg = ErrorStrings.BAD_EXP_RECV msg = exceptions.ErrorStrings.BAD_EXP_RECV
raise TypeError(msg.format(Base.__name__, 'type', raise TypeError(msg.format(Base.__name__, 'type',
enums.Types, type(self.type))) enums.Types, type(self.type)))
ostream.write(pack('!B', self.type.value)) ostream.write(pack('!B', self.type.value))
def write_length(self, ostream): def write_length(self, ostream):
if type(self.length) is not int: if type(self.length) is not int:
msg = ErrorStrings.BAD_EXP_RECV msg = exceptions.ErrorStrings.BAD_EXP_RECV
raise TypeError(msg.format(Base.__name__, 'length', raise TypeError(msg.format(Base.__name__, 'length',
int, type(self.length))) int, type(self.length)))
num_bytes = utils.count_bytes(self.length) num_bytes = utils.count_bytes(self.length)
if num_bytes > self.LENGTH_SIZE: if num_bytes > self.LENGTH_SIZE:
raise errors.WriteOverflowError(Base.__name__, 'length', raise exceptions.WriteOverflowError(
self.LENGTH_SIZE, num_bytes) Base.__name__,
'length',
self.LENGTH_SIZE,
num_bytes
)
ostream.write(pack('!I', self.length)) ostream.write(pack('!I', self.length))
def write_value(self, ostream): def write_value(self, ostream):
@ -188,15 +205,23 @@ class Integer(Base):
def read_value(self, istream): def read_value(self, istream):
if self.length is not self.LENGTH: if self.length is not self.LENGTH:
raise errors.ReadValueError(Integer.__name__, 'length', raise exceptions.ReadValueError(
self.LENGTH, self.length) Integer.__name__,
'length',
self.LENGTH,
self.length
)
self.value = unpack(self.pack_string, istream.read(self.length))[0] self.value = unpack(self.pack_string, istream.read(self.length))[0]
pad = unpack(self.pack_string, istream.read(self.padding_length))[0] pad = unpack(self.pack_string, istream.read(self.padding_length))[0]
if pad is not 0: if pad is not 0:
raise errors.ReadValueError(Integer.__name__, 'pad', 0, raise exceptions.ReadValueError(
pad) Integer.__name__,
'pad',
0,
pad
)
self.validate() self.validate()
def read(self, istream): def read(self, istream):
@ -799,8 +824,12 @@ class TextString(Base):
for _ in range(self.padding_length): for _ in range(self.padding_length):
pad = unpack('!B', istream.read(1))[0] pad = unpack('!B', istream.read(1))[0]
if pad is not 0: if pad is not 0:
raise errors.ReadValueError(TextString.__name__, 'pad', 0, raise exceptions.ReadValueError(
pad) TextString.__name__,
'pad',
0,
pad
)
def read(self, istream): def read(self, istream):
super(TextString, self).read(istream) super(TextString, self).read(istream)
@ -826,7 +855,7 @@ class TextString(Base):
def __validate(self): def __validate(self):
if self.value is not None: if self.value is not None:
if not isinstance(self.value, six.string_types): if not isinstance(self.value, six.string_types):
msg = ErrorStrings.BAD_EXP_RECV msg = exceptions.ErrorStrings.BAD_EXP_RECV
raise TypeError(msg.format('TextString', 'value', str, raise TypeError(msg.format('TextString', 'value', str,
type(self.value))) type(self.value)))
@ -890,8 +919,12 @@ class ByteString(Base):
for _ in range(self.padding_length): for _ in range(self.padding_length):
pad = unpack('!B', istream.read(1))[0] pad = unpack('!B', istream.read(1))[0]
if pad is not 0: if pad is not 0:
raise errors.ReadValueError(TextString.__name__, 'pad', 0, raise exceptions.ReadValueError(
pad) TextString.__name__,
'pad',
0,
pad
)
def read(self, istream): def read(self, istream):
super(ByteString, self).read(istream) super(ByteString, self).read(istream)
@ -919,7 +952,7 @@ class ByteString(Base):
if self.value is not None: if self.value is not None:
data_type = type(self.value) data_type = type(self.value)
if data_type is not bytes: if data_type is not bytes:
msg = ErrorStrings.BAD_EXP_RECV msg = exceptions.ErrorStrings.BAD_EXP_RECV
raise TypeError(msg.format('ByteString', 'value', bytes, raise TypeError(msg.format('ByteString', 'value', bytes,
data_type)) data_type))

View File

@ -16,7 +16,7 @@
from binascii import hexlify from binascii import hexlify
import io import io
from kmip.core.errors import ErrorStrings from kmip.core import exceptions
def bit_length(num): def bit_length(num):
@ -52,9 +52,8 @@ def is_stream_empty(stream):
def build_er_error(class_object, descriptor, expected, received, def build_er_error(class_object, descriptor, expected, received,
attribute=None): attribute=None):
msg = ErrorStrings.BAD_EXP_RECV msg = exceptions.ErrorStrings.BAD_EXP_RECV
class_string = ''
if attribute is None: if attribute is None:
class_string = '{0}'.format(class_object.__name__) class_string = '{0}'.format(class_object.__name__)
else: else:

View File

@ -35,8 +35,8 @@ from kmip.core.enums import KeyFormatType as KeyFormatTypeEnum
from kmip.core.enums import ResultStatus from kmip.core.enums import ResultStatus
from kmip.core.enums import ResultReason from kmip.core.enums import ResultReason
from kmip.core.errors import KMIPServerSuicideError from kmip.core.exceptions import KMIPServerSuicideError
from kmip.core.errors import KMIPServerZombieError from kmip.core.exceptions import KMIPServerZombieError
from kmip.core.factories.attributes import AttributeFactory from kmip.core.factories.attributes import AttributeFactory
from kmip.core.factories.credentials import CredentialFactory from kmip.core.factories.credentials import CredentialFactory

View File

@ -35,9 +35,7 @@ from kmip.core.enums import CryptographicAlgorithm as CryptoAlgorithmEnum
from kmip.core.enums import CryptographicUsageMask from kmip.core.enums import CryptographicUsageMask
from kmip.core.enums import NameType from kmip.core.enums import NameType
from kmip.core import errors from kmip.core import exceptions
from kmip.core.errors import ErrorStrings
from kmip.core import objects from kmip.core import objects
from kmip.core.messages import contents from kmip.core.messages import contents
@ -60,7 +58,7 @@ class TestRequestMessage(TestCase):
super(TestRequestMessage, self).setUp() super(TestRequestMessage, self).setUp()
self.stream = BytearrayStream() self.stream = BytearrayStream()
self.attribute_factory = AttributeFactory() self.attribute_factory = AttributeFactory()
self.msg = errors.ErrorStrings.BAD_EXP_RECV self.msg = exceptions.ErrorStrings.BAD_EXP_RECV
self.create = ( self.create = (
b'\x42\x00\x78\x01\x00\x00\x01\x20\x42\x00\x77\x01\x00\x00\x00\x38' b'\x42\x00\x78\x01\x00\x00\x01\x20\x42\x00\x77\x01\x00\x00\x00\x38'
b'\x42\x00\x69\x01\x00\x00\x00\x20\x42\x00\x6A\x02\x00\x00\x00\x04' b'\x42\x00\x69\x01\x00\x00\x00\x20\x42\x00\x6A\x02\x00\x00\x00\x04'
@ -735,28 +733,44 @@ class TestRequestMessage(TestCase):
names = template_attribute.names names = template_attribute.names
exp_type = list exp_type = list
rcv_type = type(names) rcv_type = type(names)
msg = ErrorStrings.BAD_EXP_RECV.format('TemplateAttribute.names', msg = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'type', '{0}', '{0}') 'TemplateAttribute.names',
'type',
'{0}',
'{0}'
)
self.assertIsInstance(names, exp_type, self.assertIsInstance(names, exp_type,
msg.format(exp_type, rcv_type)) msg.format(exp_type, rcv_type))
exp_length = 0 exp_length = 0
rcv_length = len(names) rcv_length = len(names)
msg = ErrorStrings.BAD_EXP_RECV.format('TemplateAttribute.names', msg = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'length', '{0}', '{0}') 'TemplateAttribute.names',
'length',
'{0}',
'{0}'
)
self.assertEqual(exp_length, rcv_length, self.assertEqual(exp_length, rcv_length,
msg.format(exp_length, rcv_length)) msg.format(exp_length, rcv_length))
attributes = template_attribute.attributes attributes = template_attribute.attributes
exp_type = list exp_type = list
rcv_type = type(attributes) rcv_type = type(attributes)
msg = ErrorStrings.BAD_EXP_RECV.format( msg = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'TemplateAttribute.attributes', 'type', '{0}', '{1}') 'TemplateAttribute.attributes',
'type',
'{0}',
'{1}'
)
self.assertIsInstance(names, exp_type, self.assertIsInstance(names, exp_type,
msg.format(exp_type, rcv_type)) msg.format(exp_type, rcv_type))
exp_length = 0 exp_length = 0
rcv_length = len(attributes) rcv_length = len(attributes)
msg = ErrorStrings.BAD_EXP_RECV.format( msg = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'TemplateAttribute.attributes', 'length', '{0}', '{1}') 'TemplateAttribute.attributes',
'length',
'{0}',
'{1}'
)
self.assertEqual(exp_length, rcv_length, self.assertEqual(exp_length, rcv_length,
msg.format(exp_length, rcv_length)) msg.format(exp_length, rcv_length))
@ -1144,7 +1158,7 @@ class TestResponseMessage(TestCase):
super(TestResponseMessage, self).setUp() super(TestResponseMessage, self).setUp()
self.stream = BytearrayStream() self.stream = BytearrayStream()
self.secret_factory = SecretFactory() self.secret_factory = SecretFactory()
self.msg = errors.ErrorStrings.BAD_EXP_RECV self.msg = exceptions.ErrorStrings.BAD_EXP_RECV
self.create = ( self.create = (
b'\x42\x00\x7B\x01\x00\x00\x00\xC0\x42\x00\x7A\x01\x00\x00\x00\x48' b'\x42\x00\x7B\x01\x00\x00\x00\xC0\x42\x00\x7A\x01\x00\x00\x00\x48'
b'\x42\x00\x69\x01\x00\x00\x00\x20\x42\x00\x6A\x02\x00\x00\x00\x04' b'\x42\x00\x69\x01\x00\x00\x00\x20\x42\x00\x6A\x02\x00\x00\x00\x04'

View File

@ -15,7 +15,7 @@
import testtools import testtools
from kmip.core import errors from kmip.core import exceptions
from kmip.core import primitives from kmip.core import primitives
from kmip.core import utils from kmip.core import utils
@ -26,11 +26,11 @@ class TestBase(testtools.TestCase):
super(TestBase, self).setUp() super(TestBase, self).setUp()
self.stream = utils.BytearrayStream() self.stream = utils.BytearrayStream()
self.bad_init = 'Bad Base initialization: attribute {0} missing' self.bad_init = 'Bad Base initialization: attribute {0} missing'
self.bad_write = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_write = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.Base.{0}', 'write', '{1}', '{2}') 'primitives.Base.{0}', 'write', '{1}', '{2}')
self.bad_encoding = errors.ErrorStrings.BAD_ENCODING.format( self.bad_encoding = exceptions.ErrorStrings.BAD_ENCODING.format(
'primitives.Base.{0}', 'write') 'primitives.Base.{0}', 'write')
self.bad_match = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_match = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.Base.{0}', 'comparison', '{1}', '{2}') 'primitives.Base.{0}', 'comparison', '{1}', '{2}')
def tearDown(self): def tearDown(self):
@ -44,7 +44,10 @@ class TestBase(testtools.TestCase):
self.stream.write(b'\x00') self.stream.write(b'\x00')
base = primitives.Base() base = primitives.Base()
self.assertRaises( self.assertRaises(
errors.StreamNotEmptyError, base.is_oversized, self.stream) exceptions.StreamNotEmptyError,
base.is_oversized,
self.stream
)
def test_read_tag(self): def test_read_tag(self):
encoding = (b'\x42\x00\x00') encoding = (b'\x42\x00\x00')
@ -56,7 +59,11 @@ class TestBase(testtools.TestCase):
encoding = (b'\x42\x00\x01') encoding = (b'\x42\x00\x01')
base = primitives.Base() base = primitives.Base()
self.stream = utils.BytearrayStream(encoding) self.stream = utils.BytearrayStream(encoding)
self.assertRaises(errors.ReadValueError, base.read_tag, self.stream) self.assertRaises(
exceptions.ReadValueError,
base.read_tag,
self.stream
)
def test_read_type(self): def test_read_type(self):
self.stream.write(b'\x00') self.stream.write(b'\x00')
@ -66,11 +73,19 @@ class TestBase(testtools.TestCase):
def test_read_type_error(self): def test_read_type_error(self):
self.stream.write(b'\x01') self.stream.write(b'\x01')
base = primitives.Base() base = primitives.Base()
self.assertRaises(errors.ReadValueError, base.read_type, self.stream) self.assertRaises(
exceptions.ReadValueError,
base.read_type,
self.stream
)
def test_read_type_underflow(self): def test_read_type_underflow(self):
base = primitives.Base() base = primitives.Base()
self.assertRaises(errors.ReadValueError, base.read_type, self.stream) self.assertRaises(
exceptions.ReadValueError,
base.read_type,
self.stream
)
def test_read_type_overflow(self): def test_read_type_overflow(self):
self.stream.write(b'\x00\x00') self.stream.write(b'\x00\x00')
@ -87,7 +102,11 @@ class TestBase(testtools.TestCase):
self.stream.write(b'\x00') self.stream.write(b'\x00')
base = primitives.Base() base = primitives.Base()
base.length = 4 base.length = 4
self.assertRaises(errors.ReadValueError, base.read_length, self.stream) self.assertRaises(
exceptions.ReadValueError,
base.read_length,
self.stream
)
def test_read_length_overflow(self): def test_read_length_overflow(self):
self.stream.write(b'\x00\x00\x00\x04\x00') self.stream.write(b'\x00\x00\x00\x04\x00')

View File

@ -15,7 +15,7 @@
import testtools import testtools
from kmip.core import errors from kmip.core import exceptions
from kmip.core import primitives from kmip.core import primitives
from kmip.core import utils from kmip.core import utils
@ -25,17 +25,17 @@ class TestByteString(testtools.TestCase):
def setUp(self): def setUp(self):
super(TestByteString, self).setUp() super(TestByteString, self).setUp()
self.stream = utils.BytearrayStream() self.stream = utils.BytearrayStream()
self.bad_type = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_type = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString.{0}', 'type', '{1}', '{2}') 'primitives.ByteString.{0}', 'type', '{1}', '{2}')
self.bad_value = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_value = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString.{0}', 'value', '{1}', '{2}') 'primitives.ByteString.{0}', 'value', '{1}', '{2}')
self.bad_read = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_read = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString.{0}', '', '{1}', '{2}') 'primitives.ByteString.{0}', '', '{1}', '{2}')
self.bad_write = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_write = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString.{0}', 'write', '{1}', '{2}') 'primitives.ByteString.{0}', 'write', '{1}', '{2}')
self.bad_encoding = errors.ErrorStrings.BAD_ENCODING.format( self.bad_encoding = exceptions.ErrorStrings.BAD_ENCODING.format(
'primitives.ByteString', '') 'primitives.ByteString', '')
self.bad_length = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_length = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.ByteString', 'length', '{0} bytes', '{1} bytes') 'primitives.ByteString', 'length', '{0} bytes', '{1} bytes')
def tearDown(self): def tearDown(self):
@ -142,7 +142,7 @@ class TestByteString(testtools.TestCase):
self.stream = utils.BytearrayStream(encoding) self.stream = utils.BytearrayStream(encoding)
bs = primitives.ByteString() bs = primitives.ByteString()
self.assertRaises(errors.ReadValueError, bs.read, self.stream) self.assertRaises(exceptions.ReadValueError, bs.read, self.stream)
def test_write_value(self): def test_write_value(self):
encoding = b'\x01\x02\x03\x00\x00\x00\x00\x00' encoding = b'\x01\x02\x03\x00\x00\x00\x00\x00'

View File

@ -15,7 +15,7 @@
import testtools import testtools
from kmip.core import errors from kmip.core import exceptions
from kmip.core import primitives from kmip.core import primitives
from kmip.core import utils from kmip.core import utils
@ -146,7 +146,11 @@ class TestInteger(testtools.TestCase):
self.stream = utils.BytearrayStream(encoding) self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer() i = primitives.Integer()
self.assertRaises(errors.ReadValueError, i.read, self.stream) self.assertRaises(
exceptions.ReadValueError,
i.read,
self.stream
)
def test_read_on_invalid_padding(self): def test_read_on_invalid_padding(self):
encoding = ( encoding = (
@ -155,7 +159,11 @@ class TestInteger(testtools.TestCase):
self.stream = utils.BytearrayStream(encoding) self.stream = utils.BytearrayStream(encoding)
i = primitives.Integer() i = primitives.Integer()
self.assertRaises(errors.ReadValueError, i.read, self.stream) self.assertRaises(
exceptions.ReadValueError,
i.read,
self.stream
)
def test_write_value(self): def test_write_value(self):
encoding = (b'\x00\x00\x00\x01\x00\x00\x00\x00') encoding = (b'\x00\x00\x00\x01\x00\x00\x00\x00')

View File

@ -16,7 +16,7 @@
import six import six
import testtools import testtools
from kmip.core import errors from kmip.core import exceptions
from kmip.core import primitives from kmip.core import primitives
from kmip.core import utils from kmip.core import utils
@ -26,17 +26,17 @@ class TestTextString(testtools.TestCase):
def setUp(self): def setUp(self):
super(TestTextString, self).setUp() super(TestTextString, self).setUp()
self.stream = utils.BytearrayStream() self.stream = utils.BytearrayStream()
self.bad_type = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_type = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString.{0}', 'type', '{1}', '{2}') 'primitives.TextString.{0}', 'type', '{1}', '{2}')
self.bad_value = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_value = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString.{0}', 'value', '{1}', '{2}') 'primitives.TextString.{0}', 'value', '{1}', '{2}')
self.bad_read = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_read = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString.{0}', '', '{1}', '{2}') 'primitives.TextString.{0}', '', '{1}', '{2}')
self.bad_write = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_write = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString.{0}', 'write', '{1}', '{2}') 'primitives.TextString.{0}', 'write', '{1}', '{2}')
self.bad_encoding = errors.ErrorStrings.BAD_ENCODING.format( self.bad_encoding = exceptions.ErrorStrings.BAD_ENCODING.format(
'primitives.TextString', '') 'primitives.TextString', '')
self.bad_length = errors.ErrorStrings.BAD_EXP_RECV.format( self.bad_length = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'primitives.TextString', 'length', '{0} bytes', '{1} bytes') 'primitives.TextString', 'length', '{0} bytes', '{1} bytes')
def tearDown(self): def tearDown(self):
@ -139,7 +139,7 @@ class TestTextString(testtools.TestCase):
self.stream = utils.BytearrayStream(encoding) self.stream = utils.BytearrayStream(encoding)
ts = primitives.TextString() ts = primitives.TextString()
self.assertRaises(errors.ReadValueError, ts.read, self.stream) self.assertRaises(exceptions.ReadValueError, ts.read, self.stream)
def test_write_value(self): def test_write_value(self):
encoding = ( encoding = (

View File

@ -15,8 +15,7 @@
from testtools import TestCase from testtools import TestCase
from kmip.core.errors import ErrorStrings from kmip.core import exceptions
from kmip.core import utils from kmip.core import utils
@ -62,12 +61,24 @@ class TestBytearrayStream(TestCase):
super(TestBytearrayStream, self).setUp() super(TestBytearrayStream, self).setUp()
self.stream = utils.BytearrayStream() self.stream = utils.BytearrayStream()
self.bad_type = ErrorStrings.BAD_EXP_RECV.format('BytearrayStream.{0}', self.bad_type = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'type', '{1}', '{2}') 'BytearrayStream.{0}',
self.bad_len = ErrorStrings.BAD_EXP_RECV.format('BytearrayStream.{0}', 'type',
'length', '{1}', '{2}') '{1}',
self.bad_val = ErrorStrings.BAD_EXP_RECV.format('BytearrayStream.{0}', '{2}'
'value', '{1}', '{2}') )
self.bad_len = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'BytearrayStream.{0}',
'length',
'{1}',
'{2}'
)
self.bad_val = exceptions.ErrorStrings.BAD_EXP_RECV.format(
'BytearrayStream.{0}',
'value',
'{1}',
'{2}'
)
def tearDown(self): def tearDown(self):
super(TestBytearrayStream, self).tearDown() super(TestBytearrayStream, self).tearDown()