0

我正在尝试在 C++ 中模拟 AWS 开发工具包的部分以进行测试。具体来说,我在模拟 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 开发工具包中定义: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

我尝试使用“new”运算符、删除“const”和许多其他组合来使其正常工作但没有成功。任何建议,将不胜感激。

4

0 回答 0