def test_equal_on_equal_and_empty(self): """ Test that the equality operator returns True when comparing two BigIntegers. """ a = primitives.BigInteger() b = primitives.BigInteger() 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 BigIntegers with different values. """ a = primitives.BigInteger(1) b = primitives.BigInteger(2) 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 BigIntegers with different values. """ a = primitives.BigInteger(1) b = primitives.BigInteger(2) self.assertTrue(a != b) self.assertTrue(b != a)
def test_not_equal_on_equal(self): """ Test that the inequality operator returns False when comparing two BigIntegers with the same values. """ a = primitives.BigInteger(1) b = primitives.BigInteger(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 BigIntegers. """ a = primitives.BigInteger() b = primitives.BigInteger() self.assertFalse(a != b) self.assertFalse(b != a)
def test_read_zero(self): """ Test that a BigInteger representing the value 0 can be read from a byte stream. """ big_int = primitives.BigInteger() big_int.read(self.encoding_zero) self.assertEqual(0, big_int.value)
def test_read_on_invalid_length(self): """ Test that an InvalidPrimitiveLength exception is thrown when attempting to decode a BigInteger with an invalid length. """ big_int = primitives.BigInteger() self.assertRaises(exceptions.InvalidPrimitiveLength, big_int.read, self.encoding_bad_length)
def test_read_negative(self): """ Test that a BigInteger representing a negative value can be read from a byte stream. """ big_int = primitives.BigInteger() big_int.read(self.encoding_negative) self.assertEqual(self.value_negative, big_int.value)
def test_repr(self): """ Test that the representation of a BigInteger is formatted properly. """ long_int = primitives.BigInteger() value = "value={0}".format(long_int.value) tag = "tag={0}".format(long_int.tag) self.assertEqual("BigInteger({0}, {1})".format(value, tag), repr(long_int))
def test_write_negative(self): """ Test that a BigInteger representing a negative value can be written to a byte stream. """ stream = utils.BytearrayStream() big_int = primitives.BigInteger(self.value_negative) big_int.write(stream) self.assertEqual(self.encoding_negative, stream)
def test_write_zero(self): """ Test that a BigInteger representing the value 0 can be read written to a byte stream. """ stream = utils.BytearrayStream() big_int = primitives.BigInteger() big_int.write(stream) self.assertEqual(self.encoding_zero, stream)
def test_not_equal_on_type_mismatch(self): """ Test that the inequality operator returns True when comparing a BigInteger to a non-BigInteger object. """ a = primitives.BigInteger() b = 'invalid' self.assertTrue(a != b) self.assertTrue(b != a)
def test_equal_on_type_mismatch(self): """ Test that the equality operator returns False when comparing a BigInteger to a non-BigInteger object. """ a = primitives.BigInteger() b = 'invalid' self.assertFalse(a == b) self.assertFalse(b == a)
def prime_field_size(self, value): if value is None: self._prime_field_size = None elif isinstance(value, six.integer_types): self._prime_field_size = primitives.BigInteger( value=value, tag=enums.Tags.PRIME_FIELD_SIZE ) else: raise TypeError("The prime field size must be an integer.")
def test_init_unset(self): """ Test that a BigInteger can be instantiated with no input. """ big_int = primitives.BigInteger() self.assertEqual(0, big_int.value)
def test_init(self): """ Test that a BigInteger can be instantiated. """ big_int = primitives.BigInteger(1) self.assertEqual(1, big_int.value)
def test_str(self): """ Test that the string representation of a BigInteger is formatted properly. """ self.assertEqual("0", str(primitives.BigInteger()))
def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the SplitKey object and decode it. Args: input_buffer (stream): A data stream containing the encoded object data, supporting a read method; usually a BytearrayStream object. kmip_version (KMIPVersion): An enumeration defining the KMIP version with which the object will be decoded. Optional, defaults to KMIP 1.0. """ super(SplitKey, self).read(input_buffer, kmip_version=kmip_version) local_buffer = utils.BytearrayStream(input_buffer.read(self.length)) if self.is_tag_next(enums.Tags.SPLIT_KEY_PARTS, local_buffer): self._split_key_parts = primitives.Integer( tag=enums.Tags.SPLIT_KEY_PARTS ) self._split_key_parts.read(local_buffer, kmip_version=kmip_version) else: raise exceptions.InvalidKmipEncoding( "The SplitKey encoding is missing the SplitKeyParts field." ) if self.is_tag_next(enums.Tags.KEY_PART_IDENTIFIER, local_buffer): self._key_part_identifier = primitives.Integer( tag=enums.Tags.KEY_PART_IDENTIFIER ) self._key_part_identifier.read( local_buffer, kmip_version=kmip_version ) else: raise exceptions.InvalidKmipEncoding( "The SplitKey encoding is missing the KeyPartIdentifier field." ) if self.is_tag_next(enums.Tags.SPLIT_KEY_THRESHOLD, local_buffer): self._split_key_threshold = primitives.Integer( tag=enums.Tags.SPLIT_KEY_THRESHOLD ) self._split_key_threshold.read( local_buffer, kmip_version=kmip_version ) else: raise exceptions.InvalidKmipEncoding( "The SplitKey encoding is missing the SplitKeyThreshold field." ) if self.is_tag_next(enums.Tags.SPLIT_KEY_METHOD, local_buffer): self._split_key_method = primitives.Enumeration( enums.SplitKeyMethod, tag=enums.Tags.SPLIT_KEY_METHOD ) self._split_key_method.read( local_buffer, kmip_version=kmip_version ) else: raise exceptions.InvalidKmipEncoding( "The SplitKey encoding is missing the SplitKeyMethod field." ) if self.is_tag_next(enums.Tags.PRIME_FIELD_SIZE, local_buffer): self._prime_field_size = primitives.BigInteger( tag=enums.Tags.PRIME_FIELD_SIZE ) self._prime_field_size.read( local_buffer, kmip_version=kmip_version ) else: corner_case = enums.SplitKeyMethod.POLYNOMIAL_SHARING_PRIME_FIELD if self.split_key_method == corner_case: raise exceptions.InvalidKmipEncoding( "The SplitKey encoding is missing the PrimeFieldSize " "field. This field is required when the SplitKeyMethod is " "PolynomialSharingPrimeField." ) if self.is_tag_next(enums.Tags.KEY_BLOCK, local_buffer): self._key_block = objects.KeyBlock() self._key_block.read(local_buffer, kmip_version=kmip_version) else: raise exceptions.InvalidKmipEncoding( "The SplitKey encoding is missing the KeyBlock field." ) self.is_oversized(local_buffer)
def test_init_negative(self): """ Test that a BigInteger can be instantiated with negative input. """ big_int = primitives.BigInteger(self.value_negative) self.assertEqual(self.value_negative, big_int.value)