def test_count_bytes_overflow(self): num = 65536 bytes_exp = 3 bytes_obs = utils.count_bytes(num) self.assertEqual( bytes_exp, bytes_obs, 'Value {0} requires {1} bytes to encode, ' 'received {2} bytes'.format(num, bytes_exp, bytes_obs))
def test_count_bytes_zero(self): num = 0 bytes_exp = 1 bytes_obs = utils.count_bytes(num) self.assertEqual( bytes_exp, bytes_obs, 'Value {0} requires {1} bytes to encode, ' 'received {2} byte(s)'.format(num, bytes_exp, bytes_obs))
def write_length(self, ostream): if type(self.length) is not int: msg = ErrorStrings.BAD_EXP_RECV raise TypeError(msg.format(Base.__name__, "length", int, type(self.length))) num_bytes = utils.count_bytes(self.length) if num_bytes > self.LENGTH_SIZE: raise errors.WriteOverflowError(Base.__name__, "length", self.LENGTH_SIZE, num_bytes) ostream.write(pack("!I", self.length))
def __validate(self): if self.value is not None: data_type = type(self.value) if data_type not in six.integer_types: raise errors.StateTypeError(BigInteger.__name__, "{0}".format(six.integer_types), data_type) num_bytes = utils.count_bytes(self.length) if num_bytes > self.LENGTH_SIZE: raise errors.StateOverflowError(BigInteger.__name__, "length", self.LENGTH_SIZE, num_bytes)
def test_count_bytes_zero(self): num = 0 bytes_exp = 1 bytes_obs = utils.count_bytes(num) self.assertEqual(bytes_exp, bytes_obs, 'Value {0} requires {1} bytes to encode, ' 'received {2} byte(s)'.format(num, bytes_exp, bytes_obs))
def test_count_bytes_overflow(self): num = 65536 bytes_exp = 3 bytes_obs = utils.count_bytes(num) self.assertEqual(bytes_exp, bytes_obs, 'Value {0} requires {1} bytes to encode, ' 'received {2} bytes'.format(num, bytes_exp, bytes_obs))
def write_length(self, ostream): if type(self.length) is not int: msg = ErrorStrings.BAD_EXP_RECV raise TypeError(msg.format(Base.__name__, 'length', int, type(self.length))) num_bytes = utils.count_bytes(self.length) if num_bytes > self.LENGTH_SIZE: raise errors.WriteOverflowError(Base.__name__, 'length', self.LENGTH_SIZE, num_bytes) ostream.write(pack('!I', self.length))
def __validate(self): if self.value is not None: data_type = type(self.value) if data_type is not int: raise errors.StateTypeError(Integer.__name__, int, data_type) num_bytes = utils.count_bytes(self.value) if num_bytes > self.length: raise errors.StateOverflowError(Integer.__name__, 'value', self.length, num_bytes)
def __validate(self): if self.value is not None: data_type = type(self.value) if data_type not in (int, long): raise errors.StateTypeError(BigInteger.__name__, '{0} or {1}'.format(int, long), data_type) num_bytes = utils.count_bytes(self.length) if num_bytes > self.LENGTH_SIZE: raise errors.StateOverflowError(BigInteger.__name__, 'length', self.LENGTH_SIZE, num_bytes)
def __init__(self, value=None, tag=Tags.DEFAULT): super(BigInteger, self).__init__(tag, type=Types.BIG_INTEGER) self.value = value if self.value is not None: self.real_length = utils.count_bytes(self.value) self.padding_length = self.BLOCK_SIZE - (self.length % self.BLOCK_SIZE) if self.padding_length == self.BLOCK_SIZE: self.padding_length = 0 else: self.length = None self.padding_length = None self.validate()
def __validate(self): if self.value is not None: data_type = type(self.value) if sys.version < '3': valid_data_types = (int, long) error_msg = '{0} or {1}'.format(int, long) else: valid_data_types = (int,) error_msg = '{0}'.format(int) if data_type not in valid_data_types: raise errors.StateTypeError(LongInteger.__name__, error_msg, data_type) num_bytes = utils.count_bytes(self.value) if num_bytes > self.length: raise errors.StateOverflowError(LongInteger.__name__, 'value', self.length, num_bytes)