Beispiel #1
0
    def test_not_equal_on_not_equal_unique_identifier(self):
        """
        Test that the inequality operator returns True when comparing two
        Encrypt response payloads with different unique identifiers.
        """
        a = encrypt.EncryptResponsePayload(unique_identifier='a')
        b = encrypt.EncryptResponsePayload(unique_identifier='b')

        self.assertTrue(a != b)
        self.assertTrue(b != a)
Beispiel #2
0
    def test_read_invalid(self):
        """
        Test that a ValueError gets raised when required Encrypt response
        payload attributes are missing from the payload encoding.
        """
        payload = encrypt.EncryptResponsePayload()
        args = (self.empty_encoding, )
        self.assertRaisesRegexp(
            ValueError,
            "invalid payload missing the unique identifier attribute",
            payload.read, *args)

        payload = encrypt.EncryptResponsePayload()
        args = (self.incomplete_encoding, )
        self.assertRaisesRegexp(ValueError,
                                "invalid payload missing the data attribute",
                                payload.read, *args)
Beispiel #3
0
 def test_invalid_data(self):
     """
     Test that a TypeError is raised when an invalid value is used to set
     the data of an Encrypt response payload.
     """
     payload = encrypt.EncryptResponsePayload()
     args = (payload, 'data', 0)
     self.assertRaisesRegexp(TypeError, "data must be bytes", setattr,
                             *args)
Beispiel #4
0
 def test_invalid_iv_counter_nonce(self):
     """
     Test that a TypeError is raised when an invalid value is used to set
     the IV/counter/nonce of an Encrypt response payload.
     """
     payload = encrypt.EncryptResponsePayload()
     args = (payload, 'iv_counter_nonce', 0)
     self.assertRaisesRegexp(TypeError, "IV/counter/nonce must be bytes",
                             setattr, *args)
Beispiel #5
0
    def test_encrypt(self, send_mock, build_mock):
        """
        Test that the client can encrypt data.
        """
        payload = encrypt.EncryptResponsePayload(
            unique_identifier='1',
            data=(
                b'\x6B\x77\xB4\xD6\x30\x06\xDE\xE6'
                b'\x05\xB1\x56\xE2\x74\x03\x97\x93'
                b'\x58\xDE\xB9\xE7\x15\x46\x16\xD9'
                b'\x74\x9D\xEC\xBE\xC0\x5D\x26\x4B'
            )
        )
        batch_item = ResponseBatchItem(
            operation=Operation(OperationEnum.ENCRYPT),
            result_status=ResultStatus(ResultStatusEnum.SUCCESS),
            response_payload=payload
        )
        response = ResponseMessage(batch_items=[batch_item])

        build_mock.return_value = None
        send_mock.return_value = response

        result = self.client.encrypt(
            (
                b'\x37\x36\x35\x34\x33\x32\x31\x20'
                b'\x4E\x6F\x77\x20\x69\x73\x20\x74'
                b'\x68\x65\x20\x74\x69\x6D\x65\x20'
                b'\x66\x6F\x72\x20\x00'
            ),
            unique_identifier='1',
            cryptographic_parameters=CryptographicParameters(
                block_cipher_mode=enums.BlockCipherMode.CBC,
                padding_method=enums.PaddingMethod.PKCS5,
                cryptographic_algorithm=enums.CryptographicAlgorithm.BLOWFISH
            ),
            iv_counter_nonce=b'\xFE\xDC\xBA\x98\x76\x54\x32\x10'
        )

        self.assertEqual('1', result.get('unique_identifier'))
        self.assertEqual(
            (
                b'\x6B\x77\xB4\xD6\x30\x06\xDE\xE6'
                b'\x05\xB1\x56\xE2\x74\x03\x97\x93'
                b'\x58\xDE\xB9\xE7\x15\x46\x16\xD9'
                b'\x74\x9D\xEC\xBE\xC0\x5D\x26\x4B'
            ),
            result.get('data')
        )
        self.assertEqual(None, result.get('iv_counter_nonce'))
        self.assertEqual(
            ResultStatusEnum.SUCCESS,
            result.get('result_status')
        )
        self.assertEqual(None, result.get('result_reason'))
        self.assertEqual(None, result.get('result_message'))
Beispiel #6
0
 def test_invalid_unique_identifier(self):
     """
     Test that a TypeError is raised when an invalid value is used to set
     the unique identifier of an Encrypt response payload.
     """
     payload = encrypt.EncryptResponsePayload()
     args = (payload, 'unique_identifier', 0)
     self.assertRaisesRegexp(TypeError,
                             "unique identifier must be a string", setattr,
                             *args)
Beispiel #7
0
    def test_init(self):
        """
        Test that an Encrypt response payload can be constructed with no
        arguments.
        """
        payload = encrypt.EncryptResponsePayload()

        self.assertEqual(None, payload.unique_identifier)
        self.assertEqual(None, payload.data)
        self.assertEqual(None, payload.iv_counter_nonce)
Beispiel #8
0
    def test_write_invalid(self):
        """
        Test that a ValueError gets raised when required Encrypt response
        payload attributes are missing when encoding the payload.
        """
        payload = encrypt.EncryptResponsePayload()
        self.assertIsNone(payload.unique_identifier)
        stream = utils.BytearrayStream()
        args = (stream, )
        self.assertRaisesRegexp(
            ValueError,
            "invalid payload missing the unique identifier attribute",
            payload.write, *args)

        payload = encrypt.EncryptResponsePayload(
            unique_identifier='b4faee10-aa2a-4446-8ad4-0881f3422959')
        stream = utils.BytearrayStream()
        args = (stream, )
        self.assertRaisesRegexp(ValueError,
                                "invalid payload missing the data attribute",
                                payload.write, *args)
Beispiel #9
0
    def test_not_equal_on_equal(self):
        """
        Test that the inequality operator returns False when comparing two
        Encrypt response payloads with the same data.
        """
        a = encrypt.EncryptResponsePayload()
        b = encrypt.EncryptResponsePayload()

        self.assertFalse(a != b)
        self.assertFalse(b != a)

        a = encrypt.EncryptResponsePayload(
            unique_identifier='b4faee10-aa2a-4446-8ad4-0881f3422959',
            data=b'\x01\x23\x45\x67\x89\xAB\xCD\xEF',
            iv_counter_nonce=b'\x01')
        b = encrypt.EncryptResponsePayload(
            unique_identifier='b4faee10-aa2a-4446-8ad4-0881f3422959',
            data=b'\x01\x23\x45\x67\x89\xAB\xCD\xEF',
            iv_counter_nonce=b'\x01')

        self.assertFalse(a != b)
        self.assertFalse(b != a)
Beispiel #10
0
    def test_write_partial(self):
        """
        Test that a partially defined Encrypt response payload can be written
        to a data stream.
        """
        payload = encrypt.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))
Beispiel #11
0
    def test_write(self):
        """
        Test that an Encrypt response payload can be written to a data stream.
        """
        payload = encrypt.EncryptResponsePayload(
            unique_identifier='b4faee10-aa2a-4446-8ad4-0881f3422959',
            data=b'\x01\x23\x45\x67\x89\xAB\xCD\xEF',
            iv_counter_nonce=b'\x01')
        stream = utils.BytearrayStream()
        payload.write(stream)

        self.assertEqual(len(self.full_encoding), len(stream))
        self.assertEqual(str(self.full_encoding), str(stream))
Beispiel #12
0
    def test_init_with_args(self):
        """
        Test that an Encrypt response payload can be constructed with valid
        values
        """
        payload = encrypt.EncryptResponsePayload(
            unique_identifier='00000000-1111-2222-3333-444444444444',
            data=b'\x01\x02\x03',
            iv_counter_nonce=b'\x01')

        self.assertEqual('00000000-1111-2222-3333-444444444444',
                         payload.unique_identifier)
        self.assertEqual(b'\x01\x02\x03', payload.data)
        self.assertEqual(b'\x01', payload.iv_counter_nonce)
Beispiel #13
0
    def test_read(self):
        """
        Test that an Encrypt response payload can be read from a data stream.
        """
        payload = encrypt.EncryptResponsePayload()

        self.assertEqual(None, payload.unique_identifier)
        self.assertEqual(None, payload.data)
        self.assertEqual(None, payload.iv_counter_nonce)

        payload.read(self.full_encoding)

        self.assertEqual('b4faee10-aa2a-4446-8ad4-0881f3422959',
                         payload.unique_identifier)
        self.assertEqual(b'\x01\x23\x45\x67\x89\xAB\xCD\xEF', payload.data)
        self.assertEqual(b'\x01', payload.iv_counter_nonce)
Beispiel #14
0
    def test_read_partial(self):
        """
        Test that an Encrypt response payload can be read from a partial data
        stream containing the minimum required attributes.
        """
        payload = encrypt.EncryptResponsePayload()

        self.assertEqual(None, payload.unique_identifier)
        self.assertEqual(None, payload.data)
        self.assertEqual(None, payload.iv_counter_nonce)

        payload.read(self.minimum_encoding)

        self.assertEqual('b4faee10-aa2a-4446-8ad4-0881f3422959',
                         payload.unique_identifier)
        self.assertEqual(b'\x01\x02\x03\x04\x05\x06\x07\x08', payload.data)
        self.assertEqual(None, payload.iv_counter_nonce)
Beispiel #15
0
 def _create_encrypt_payload(self):
     return encrypt.EncryptResponsePayload()