mirror of
https://github.com/OpenKMIP/PyKMIP.git
synced 2025-05-06 16:00:07 +02:00
Merge pull request #37 from OpenKMIP/bug/fix-integer-accepting-longs
Fixing bug with Integer accepting long values
This commit is contained in:
commit
8516c52820
@ -165,6 +165,10 @@ class Struct(Base):
|
|||||||
class Integer(Base):
|
class Integer(Base):
|
||||||
LENGTH = 4
|
LENGTH = 4
|
||||||
|
|
||||||
|
# Set for signed 32-bit integers
|
||||||
|
MIN = -2147483648
|
||||||
|
MAX = 2147483647
|
||||||
|
|
||||||
def __init__(self, value=None, tag=Tags.DEFAULT, signed=True):
|
def __init__(self, value=None, tag=Tags.DEFAULT, signed=True):
|
||||||
super(Integer, self).__init__(tag, type=Types.INTEGER)
|
super(Integer, self).__init__(tag, type=Types.INTEGER)
|
||||||
|
|
||||||
@ -207,19 +211,23 @@ class Integer(Base):
|
|||||||
self.write_value(ostream)
|
self.write_value(ostream)
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.__validate()
|
"""
|
||||||
|
Verify that the value of the Integer object is valid.
|
||||||
|
|
||||||
def __validate(self):
|
Raises:
|
||||||
|
TypeError: if the value is not of type int or long
|
||||||
|
ValueError: if the value cannot be represented by a signed 32-bit
|
||||||
|
integer
|
||||||
|
"""
|
||||||
if self.value is not None:
|
if self.value is not None:
|
||||||
data_type = type(self.value)
|
if type(self.value) not in six.integer_types:
|
||||||
if data_type is not int:
|
raise TypeError('expected (one of): {0}, observed: {1}'.format(
|
||||||
raise errors.StateTypeError(Integer.__name__, int,
|
six.integer_types, type(self.value)))
|
||||||
data_type)
|
else:
|
||||||
num_bytes = utils.count_bytes(self.value)
|
if self.value > Integer.MAX:
|
||||||
if num_bytes > self.length:
|
raise ValueError('integer value greater than accepted max')
|
||||||
raise errors.StateOverflowError(Integer.__name__,
|
elif self.value < Integer.MIN:
|
||||||
'value', self.length,
|
raise ValueError('integer value less than accepted min')
|
||||||
num_bytes)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{0}(value={1})".format(type(self).__name__, repr(self.value))
|
return "{0}(value={1})".format(type(self).__name__, repr(self.value))
|
||||||
|
@ -297,14 +297,22 @@ class TestInteger(TestCase):
|
|||||||
i.validate()
|
i.validate()
|
||||||
|
|
||||||
def test_validate_on_invalid_type(self):
|
def test_validate_on_invalid_type(self):
|
||||||
i = Integer()
|
"""
|
||||||
i.value = 'test'
|
Test that a TypeError is thrown on input of invalid type (e.g., str).
|
||||||
|
"""
|
||||||
|
self.assertRaises(TypeError, Integer, 'invalid')
|
||||||
|
|
||||||
self.assertRaises(errors.StateTypeError, i.validate)
|
def test_validate_on_invalid_value_too_big(self):
|
||||||
|
"""
|
||||||
|
Test that a ValueError is thrown on input that is too large.
|
||||||
|
"""
|
||||||
|
self.assertRaises(ValueError, Integer, Integer.MAX + 1)
|
||||||
|
|
||||||
def test_validate_on_invalid_value(self):
|
def test_validate_on_invalid_value_too_small(self):
|
||||||
self.assertRaises(errors.StateOverflowError, Integer,
|
"""
|
||||||
self.max_byte_int + 1)
|
Test that a ValueError is thrown on input that is too small.
|
||||||
|
"""
|
||||||
|
self.assertRaises(ValueError, Integer, Integer.MIN - 1)
|
||||||
|
|
||||||
def test_read_value(self):
|
def test_read_value(self):
|
||||||
encoding = (b'\x00\x00\x00\x01\x00\x00\x00\x00')
|
encoding = (b'\x00\x00\x00\x01\x00\x00\x00\x00')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user