我正在尝试用C ++模拟部分AWS SDK进行测试。具体来说,我嘲笑KMSClient和DecryptOutcome对象。 MockKMSClient设置为将MockDecryptOutcome传入DecryptResponseReceivedHandler。
以下是我的模拟课程:
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);
}
};
此处理程序在AWS SDK中定义:http://sdk.amazonaws.com/cpp/api/LATEST/namespace_aws_1_1_k_m_s.html#a6bb4999b2fbc6cd499913779e42421b3
这是回调函数:
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;
}
最后,这里是调用异步函数的地方:
kms_client->DecryptAsync(
decrypt_request,
std::bind(
&KmsCallback::on_decrypt_callback,
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3,
std::placeholders::_4
)
);
在测试中运行它会为DecryptOutcome变量输出两个不同的内存地址:
0x7f03b6064dc0
0x7f03b6064dc8
我尝试过使用&#34; new&#34;运算符,删除&#34; const&#34;以及许多其他组合,使其无法成功运行。任何建议将不胜感激。