def test_write(self): """ Test that a Sign response payload can be written to a data stream. """ payload = sign.SignResponsePayload( unique_identifier='b4faee10-aa2a-4446-8ad4-0881f3422959', signature_data=b'\x01\x02\x03\x04\x05\x06\x07\x08' b'\x09\x10\x11\x12\x13\x14\x15\x16' ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def test_write(self): """ Test that a CurrentAttribute object can be written to a data stream. """ current_attribute = objects.CurrentAttribute( attribute=primitives.Enumeration( enums.CryptographicAlgorithm, enums.CryptographicAlgorithm.AES, enums.Tags.CRYPTOGRAPHIC_ALGORITHM)) stream = utils.BytearrayStream() current_attribute.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def test_write_missing_split_key_threshold(self): """ Test that an InvalidField error is raised during the encoding of a SplitKey object when the object is missing the split key threshold field. """ split_key = secrets.SplitKey(split_key_parts=4, key_part_identifier=1) stream = utils.BytearrayStream() args = (stream, ) self.assertRaisesRegex( exceptions.InvalidField, "The SplitKey object is missing the SplitKeyThreshold field.", split_key.write, *args)
def test_write_partial(self): """ Test that a partially defined Sign request payload can be written to a data stream. """ payload = sign.SignRequestPayload( data=b'\x01\x02\x03\x04\x05\x06\x07\x08' b'\x09\x10\x11\x12\x13\x14\x15\x16' ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.minimum_encoding), len(stream)) self.assertEqual(str(self.minimum_encoding), str(stream))
def test_write_missing_minor_number(self): """ Test that a ValueError gets raised when a required ProtocolVersion struct attribute is missing when encoding the struct. """ struct = contents.ProtocolVersion(1, None) stream = utils.BytearrayStream() args = (stream, ) self.assertRaisesRegexp( ValueError, "Invalid struct missing the minor protocol version number.", struct.write, *args )
def test_write_partial(self): """ Test that a partial Check response payload can be written to a data stream. """ payload = payloads.CheckResponsePayload( unique_identifier='2c23217e-f53c-4bdf-ad0a-58a31fd3d4b6', usage_limits_count=500 ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.partial_encoding), len(stream)) self.assertEqual(str(self.partial_encoding), str(stream))
def test_write_with_no_attribute_names(self): """ Test that a GetAttributes response payload with no attribute name data can be written to a data stream. """ payload = get_attributes.GetAttributesResponsePayload( self.unique_identifier, None ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.encoding_sans_attributes), len(stream)) self.assertEqual(str(self.encoding_sans_attributes), str(stream))
def test_write(self): """ Test that a GetAttributes response payload can be written to a data stream. """ payload = get_attributes.GetAttributesResponsePayload( self.unique_identifier, self.attributes ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def test_write(self): """ Test that an Encrypt response payload can be written to a data stream. """ payload = payloads.EncryptResponsePayload( unique_identifier='b4faee10-aa2a-4446-8ad4-0881f3422959', data=b'\x01\x23\x45\x67\x89\xAB\xCD\xEF', iv_counter_nonce=b'\x01', auth_tag=b'\x01\x90\xFE') stream = utils.BytearrayStream() payload.write(stream, enums.KMIPVersion.KMIP_1_4) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def test_write_version_not_supported(self): """ Test that a VersionNotSupported error is raised when an unsupported KMIP version is provided while writing a CurrentAttribute object to a data stream. The CurrentAttribute structure is only supported in KMIP 2.0+. """ current_attribute = objects.CurrentAttribute() args = (utils.BytearrayStream(), ) kwargs = {"kmip_version": enums.KMIPVersion.KMIP_1_4} self.assertRaisesRegex( exceptions.VersionNotSupported, "KMIP 1.4 does not support the CurrentAttribute object.", current_attribute.write, *args, **kwargs)
def setUp(self): super(TestRevokeRequestPayload, self).setUp() self.uuid = attributes.UniqueIdentifier( '668eff89-3010-4258-bc0e-8c402309c746') self.encoding_a = utils.BytearrayStream(( b'\x42\x00\x79\x01\x00\x00\x00\x58\x42\x00\x94\x07\x00\x00\x00\x24' b'\x36\x36\x38\x65\x66\x66\x38\x39\x2D\x33\x30\x31\x30\x2D\x34\x32' b'\x35\x38\x2D\x62\x63\x30\x65\x2D\x38\x63\x34\x30\x32\x33\x30\x39' b'\x63\x37\x34\x36\x00\x00\x00\x00\x42\x00\x81\x01\x00\x00\x00\x10' b'\x42\x00\x82\x05\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x00' b'\x42\x00\x21\x09\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x06' ))
def test_write_partial(self): """ Test that a partially defined Encrypt response payload can be written to a data stream. """ payload = payloads.EncryptResponsePayload( unique_identifier='b4faee10-aa2a-4446-8ad4-0881f3422959', data=b'\x01\x02\x03\x04\x05\x06\x07\x08' ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.minimum_encoding), len(stream)) self.assertEqual(str(self.minimum_encoding), str(stream))
def setUp(self): super(TestRekeyKeyPairRequestPayload, self).setUp() self.uuid = '00000000-0000-0000-0000-000000000000' self.private_key_uuid = attributes.PrivateKeyUniqueIdentifier( self.uuid) self.offset = misc.Offset(0) self.common_template_attribute = objects.CommonTemplateAttribute() self.private_key_template_attribute = \ objects.PrivateKeyTemplateAttribute() self.public_key_template_attribute = \ objects.PublicKeyTemplateAttribute() self.encoding_empty = utils.BytearrayStream(( b'\x42\x00\x79\x01\x00\x00\x00\x00')) self.encoding_full = utils.BytearrayStream(( b'\x42\x00\x79\x01\x00\x00\x00\x58\x42\x00\x66\x07\x00\x00\x00\x24' b'\x30\x30\x30\x30\x30\x30\x30\x30\x2d\x30\x30\x30\x30\x2d\x30\x30' b'\x30\x30\x2d\x30\x30\x30\x30\x2d\x30\x30\x30\x30\x30\x30\x30\x30' b'\x30\x30\x30\x30\x00\x00\x00\x00\x42\x00\x58\x0A\x00\x00\x00\x04' b'\x00\x00\x00\x00\x00\x00\x00\x00\x42\x00\x1F\x01\x00\x00\x00\x00' b'\x42\x00\x65\x01\x00\x00\x00\x00\x42\x00\x6E\x01\x00\x00\x00\x00' ))
def test_write(self): """ Test that an ObtainLease response payload can be written to a data stream. """ payload = payloads.ObtainLeaseResponsePayload( unique_identifier='f4152f17-9312-431a-b3fb-4fe86a86a7a1', lease_time=0, last_change_date=1335514468) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
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_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_with_no_unique_identifier(self): """ Test that a GetAttributes request payload with no ID can be written to a data stream. """ payload = payloads.GetAttributesRequestPayload(None, self.attribute_names) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.encoding_sans_unique_identifier), len(stream)) self.assertEqual(str(self.encoding_sans_unique_identifier), str(stream))
def read(self, input_stream): """ Read the data encoding the SignatureVerify request payload and decode it into its constituent parts. Args: input_stream (stream): A data stream containing encoded object data, supporting a read method; usually a BytearrayStream object. Raises: ValueError: Raised if the data attribute is missing from the encoded payload. """ super(SignatureVerifyRequestPayload, self).read(input_stream) local_stream = utils.BytearrayStream(input_stream.read(self.length)) if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_stream): self._unique_identifier = primitives.TextString( tag=enums.Tags.UNIQUE_IDENTIFIER) self._unique_identifier.read(local_stream) if self.is_tag_next(enums.Tags.CRYPTOGRAPHIC_PARAMETERS, local_stream): self._cryptographic_parameters = \ attributes.CryptographicParameters() self._cryptographic_parameters.read(local_stream) if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream) if self.is_tag_next(enums.Tags.DIGESTED_DATA, local_stream): self._digested_data = primitives.ByteString( tag=enums.Tags.DIGESTED_DATA) self._digested_data.read(local_stream) if self.is_tag_next(enums.Tags.SIGNATURE_DATA, local_stream): self._signature_data = primitives.ByteString( tag=enums.Tags.SIGNATURE_DATA) self._signature_data.read(local_stream) if self.is_tag_next(enums.Tags.CORRELATION_VALUE, local_stream): self._correlation_value = primitives.ByteString( tag=enums.Tags.CORRELATION_VALUE) self._correlation_value.read(local_stream) if self.is_tag_next(enums.Tags.INIT_INDICATOR, local_stream): self._init_indicator = primitives.Boolean( tag=enums.Tags.INIT_INDICATOR) self._init_indicator.read(local_stream) if self.is_tag_next(enums.Tags.FINAL_INDICATOR, local_stream): self._final_indicator = primitives.Boolean( tag=enums.Tags.FINAL_INDICATOR) self._final_indicator.read(local_stream) self.is_oversized(local_stream)
def test_write_invalid(self): """ Test that a ValueError gets raised when a required Rekey response payload attribute is missing when encoding the payload. """ payload = payloads.RekeyResponsePayload() stream = utils.BytearrayStream() args = (stream, ) self.assertRaisesRegexp( ValueError, "The Rekey response payload is missing the unique identifier.", payload.write, *args )
def write(self, output_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Write the data encoding the SignatureVerify response payload to a stream. Args: output_stream (stream): A data stream in which to encode object data, supporting a write method; usually a BytearrayStream object. kmip_version (KMIPVersion): An enumeration defining the KMIP version with which the object will be encoded. Optional, defaults to KMIP 1.0. Raises: ValueError: Raised if the data attribute is not defined. """ local_stream = utils.BytearrayStream() if self._unique_identifier: self._unique_identifier.write( local_stream, kmip_version=kmip_version ) else: raise ValueError( "Payload is missing the unique identifier field." ) if self._validity_indicator: self._validity_indicator.write( local_stream, kmip_version=kmip_version ) else: raise ValueError( "Payload is missing the validity indicator field." ) if self._data: self._data.write(local_stream, kmip_version=kmip_version) if self._correlation_value: self._correlation_value.write( local_stream, kmip_version=kmip_version ) self.length = local_stream.length() super(SignatureVerifyResponsePayload, self).write( output_stream, kmip_version=kmip_version ) output_stream.write(local_stream.buffer)
def test_write_invalid(self): """ Test that a ValueError gets raised when a required Decrypt request payload attribute is missing when encoding the payload. """ payload = payloads.DecryptRequestPayload() stream = utils.BytearrayStream() args = (stream, ) self.assertRaisesRegex( ValueError, "invalid payload missing the data attribute", payload.write, *args )
def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0): """ Read the data encoding the SignatureVerify response payload and decode it into its constituent parts. Args: input_stream (stream): A data stream containing 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. Raises: ValueError: Raised if the data attribute is missing from the encoded payload. """ super(SignatureVerifyResponsePayload, self).read(input_stream, kmip_version=kmip_version) local_stream = utils.BytearrayStream(input_stream.read(self.length)) if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_stream): self._unique_identifier = primitives.TextString( tag=enums.Tags.UNIQUE_IDENTIFIER) self._unique_identifier.read(local_stream, kmip_version=kmip_version) else: raise ValueError( "Parsed payload encoding is missing the unique identifier " "field.") if self.is_tag_next(enums.Tags.VALIDITY_INDICATOR, local_stream): self._validity_indicator = primitives.Enumeration( enums.ValidityIndicator, tag=enums.Tags.VALIDITY_INDICATOR) self._validity_indicator.read(local_stream, kmip_version=kmip_version) else: raise ValueError( "Parsed payload encoding is missing the validity indicator " "field.") if self.is_tag_next(enums.Tags.DATA, local_stream): self._data = primitives.ByteString(tag=enums.Tags.DATA) self._data.read(local_stream, kmip_version=kmip_version) if self.is_tag_next(enums.Tags.CORRELATION_VALUE, local_stream): self._correlation_value = primitives.ByteString( tag=enums.Tags.CORRELATION_VALUE) self._correlation_value.read(local_stream, kmip_version=kmip_version) self.is_oversized(local_stream)
def test_write(self): """ Test that a Check response payload can be written to a data stream. """ payload = payloads.CheckResponsePayload( unique_identifier='2c23217e-f53c-4bdf-ad0a-58a31fd3d4b6', usage_limits_count=500, cryptographic_usage_mask=12, lease_time=0 ) stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def test_write(self): """ Test that a Sign request payload can be written to a data stream. """ payload = sign.SignRequestPayload( unique_identifier='b4faee10-aa2a-4446-8ad4-0881f3422959', cryptographic_parameters=attributes.CryptographicParameters( cryptographic_algorithm=enums.CryptographicAlgorithm.ECDSA), data=b'\x01\x02\x03\x04\x05\x06\x07\x08' b'\x09\x10\x11\x12\x13\x14\x15\x16') stream = utils.BytearrayStream() payload.write(stream) self.assertEqual(len(self.full_encoding), len(stream)) self.assertEqual(str(self.full_encoding), str(stream))
def setUp(self): super(TestDiscoverVersionsRequestPayload, self).setUp() self.protocol_versions_empty = list() self.protocol_versions_one = list() self.protocol_versions_one.append(ProtocolVersion.create(1, 0)) self.protocol_versions_two = list() self.protocol_versions_two.append(ProtocolVersion.create(1, 1)) self.protocol_versions_two.append(ProtocolVersion.create(1, 0)) self.encoding_empty = utils.BytearrayStream( (b'\x42\x00\x79\x01\x00\x00\x00\x00')) self.encoding_one = utils.BytearrayStream(( b'\x42\x00\x79\x01\x00\x00\x00\x28\x42\x00\x69\x01\x00\x00\x00\x20' b'\x42\x00\x6A\x02\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\x6B\x02\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00' b'\x00')) self.encoding_two = utils.BytearrayStream(( b'\x42\x00\x79\x01\x00\x00\x00\x50\x42\x00\x69\x01\x00\x00\x00\x20' b'\x42\x00\x6A\x02\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\x6B\x02\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00' b'\x42\x00\x69\x01\x00\x00\x00\x20\x42\x00\x6A\x02\x00\x00\x00\x04' b'\x00\x00\x00\x01\x00\x00\x00\x00\x42\x00\x6B\x02\x00\x00\x00\x04' b'\x00\x00\x00\x00\x00\x00\x00\x00'))
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 setUp(self): super(TestMACRequestPayload, self).setUp() self.unique_identifier = attributes.UniqueIdentifier(value='1') self.cryptographic_parameters = \ attributes.CryptographicParameters( cryptographic_algorithm=enums.CryptographicAlgorithm. HMAC_SHA512 ) self.data = objects.Data( value=(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B' b'\x0C\x0D\x0E\x0F')) self.encoding_full = utils.BytearrayStream(( b'\x42\x00\x79\x01\x00\x00\x00\x40\x42\x00\x94\x07\x00\x00\x00\x01' b'\x31\x00\x00\x00\x00\x00\x00\x00\x42\x00\x2b\x01\x00\x00\x00\x10' b'\x42\x00\x28\x05\x00\x00\x00\x04\x00\x00\x00\x0b\x00\x00\x00\x00' b'\x42\x00\xc2\x08\x00\x00\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07' b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')) self.encoding_no_data = utils.BytearrayStream(( b'\x42\x00\x79\x01\x00\x00\x00\x28\x42\x00\x94\x07\x00\x00\x00\x01' b'\x31\x00\x00\x00\x00\x00\x00\x00\x42\x00\x2b\x01\x00\x00\x00\x10' b'\x42\x00\x28\x05\x00\x00\x00\x04\x00\x00\x00\x0b\x00\x00\x00\x00' ))
def test_write_missing_credentials(self): """ Test that a ValueError gets raised when attempting to write a Authentication struct missing credentials data to a data stream. """ authentication = contents.Authentication() stream = utils.BytearrayStream() args = (stream, ) self.assertRaisesRegexp( ValueError, "Authentication struct missing credentials.", authentication.write, *args )
def test_write_min_min(self): """ Test that a LongInteger representing the minimum negative value can be written to a byte stream. """ encoding = ( b'\x42\x00\x00\x03\x00\x00\x00\x08\x80\x00\x00\x00\x00\x00\x00' b'\x00') stream = utils.BytearrayStream() long_int = primitives.LongInteger(primitives.LongInteger.MIN) long_int.write(stream) result = stream.read() self.assertEqual(len(encoding), len(result)) self.assertEqual(encoding, result)
def test_write_missing_application_data(self): """ Test that an InvalidField error is raised during the encoding of an ApplicationSpecificInformation object when the object is missing the application data field. """ app_specific_info = attributes.ApplicationSpecificInformation( application_namespace="ssl") buff = utils.BytearrayStream() args = (buff, ) self.assertRaisesRegex( exceptions.InvalidField, "The ApplicationSpecificInformation object is missing the " "ApplicationData field.", app_specific_info.write, *args)