I am trying to mock parts of the AWS SDK in C++ for testing. Specifically, I'm mocking a KMSClient and DecryptOutcome object. The MockKMSClient is set to pass in a MockDecryptOutcome into the DecryptResponseReceivedHandler.
Here are my mocked classes:
class MockDecryptOutcome : public Aws::KMS::Model::DecryptOutcome {
public:
Aws::Utils::Base64::Base64 _Base64;
MockDecryptOutcome(string request) {
if (request == SUCCESS) {
EXPECT_CALL(*this, IsSuccess()).WillRepeatedly(Return(true));
Aws::KMS::Model::DecryptResult result;
result.SetPlaintext(_Base64.Decode(SUCCESS));
EXPECT_CALL(*this, GetResult()).WillRepeatedly(Return(result));
} else {
EXPECT_CALL(*this, IsSuccess()).WillRepeatedly(Return(false));
if (request == GRANT_TOKEN_NEEDED) {
EXPECT_CALL(*this, GetError()).WillRepeatedly(Return(Aws::KMS::KMSErrors::ACCESS_DENIED));
} else if (request == ENDPOINT_ERROR) {
EXPECT_CALL(*this, GetError()).WillRepeatedly(Return(Aws::KMS::KMSErrors::NETWORK_CONNECTION));
}
}
}
virtual ~MockDecryptOutcome() {};
MOCK_METHOD0(IsSuccess, bool());
MOCK_METHOD0(GetResult, Aws::KMS::Model::DecryptResult());
MOCK_CONST_METHOD0(GetError, Aws::KMS::KMSErrors());
};
class MockKMSClient : public Aws::KMS::KMSClient {
public:
MockKMSClient() {
EXPECT_CALL(*this, DecryptAsync_impl(_, _)).WillRepeatedly(Invoke(this, &MockKMSClient::do_DecryptAsync));
}
virtual ~MockKMSClient() {};
Aws::Utils::Base64::Base64 _Base64;
// Have to invoke Mocked method manually to discard optional parameter
void DecryptAsync(
const Aws::KMS::Model::DecryptRequest& request,
const Aws::KMS::DecryptResponseReceivedHandler& handler,
const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr
) const
{
DecryptAsync_impl(request, handler);
}
MOCK_CONST_METHOD2(
DecryptAsync_impl,
void(
const Aws::KMS::Model::DecryptRequest& request,
const Aws::KMS::DecryptResponseReceivedHandler& handler
)
);
void do_DecryptAsync(
const Aws::KMS::Model::DecryptRequest& request,
const Aws::KMS::DecryptResponseReceivedHandler& handler
)
{
const MockDecryptOutcome& outcome(_Base64.Encode(request.GetCiphertextBlob()));
cout << &outcome << endl;
handler(this, request, outcome, nullptr);
}
};
The handler is defined in the AWS SDK here: http://sdk.amazonaws.com/cpp/api/LATEST/namespace_aws_1_1_k_m_s.html#a6bb4999b2fbc6cd499913779e42421b3
Here is the callback function:
void KmsCallback::on_decrypt_callback(
const Aws::KMS::KMSClient* client,
const Aws::KMS::Model::DecryptRequest&,
const Aws::KMS::Model::DecryptOutcome& outcome,
const std::shared_ptr<const Aws::Client::AsyncCallerContext>&
)
{
cout << &outcome << endl;
}
Finally, here is where the async function is called:
kms_client->DecryptAsync(
decrypt_request,
std::bind(
&KmsCallback::on_decrypt_callback,
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3,
std::placeholders::_4
)
);
Running this in a test prints out two different memory addresses for the DecryptOutcome variables:
0x7f03b6064dc0
0x7f03b6064dc8
I've tried using the "new" operator, removing "const", and many other combinations to get this to work without success. Any suggestions would be appreciated.