From 6cfcc1165744aa7faec28aad2291da0ae4df84c1 Mon Sep 17 00:00:00 2001 From: zhangzifa Date: Thu, 17 Jan 2019 08:54:52 +0800 Subject: [PATCH] unit test for sts and location --- .../alibabacloud/core/RpcServiceClient.h | 2 +- .../core/location/LocationClient.h | 1 + .../include/alibabacloud/core/sts/StsClient.h | 1 + test/core/CMakeLists.txt | 2 + test/core/locationclient_ut.cc | 174 ++++++++++++++++++ test/core/stsclient_ut.cc | 154 ++++++++++++++++ 6 files changed, 333 insertions(+), 1 deletion(-) create mode 100644 test/core/locationclient_ut.cc create mode 100644 test/core/stsclient_ut.cc diff --git a/core/include/alibabacloud/core/RpcServiceClient.h b/core/include/alibabacloud/core/RpcServiceClient.h index fc8b8ccc7..e5a559c8b 100644 --- a/core/include/alibabacloud/core/RpcServiceClient.h +++ b/core/include/alibabacloud/core/RpcServiceClient.h @@ -42,7 +42,7 @@ namespace AlibabaCloud virtual ~RpcServiceClient(); protected: - JsonOutcome makeRequest(const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const; + virtual JsonOutcome makeRequest(const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const; virtual HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const override; HttpRequest buildHttpRequest(const std::string & endpoint, const RpcServiceRequest &msg, HttpRequest::Method method)const; private: diff --git a/core/include/alibabacloud/core/location/LocationClient.h b/core/include/alibabacloud/core/location/LocationClient.h index e0c930026..f492ec385 100644 --- a/core/include/alibabacloud/core/location/LocationClient.h +++ b/core/include/alibabacloud/core/location/LocationClient.h @@ -42,6 +42,7 @@ namespace AlibabaCloud DescribeEndpointsOutcome describeEndpoints(const Model::DescribeEndpointsRequest &request)const; void describeEndpointsAsync(const Model::DescribeEndpointsRequest& request, const DescribeEndpointsAsyncHandler& handler, const std::shared_ptr& context = nullptr) const; DescribeEndpointsOutcomeCallable describeEndpointsCallable(const Model::DescribeEndpointsRequest& request) const; + using RpcServiceClient::makeRequest; }; } } diff --git a/core/include/alibabacloud/core/sts/StsClient.h b/core/include/alibabacloud/core/sts/StsClient.h index b5021c24f..57caff9ca 100644 --- a/core/include/alibabacloud/core/sts/StsClient.h +++ b/core/include/alibabacloud/core/sts/StsClient.h @@ -51,6 +51,7 @@ namespace AlibabaCloud GetCallerIdentityOutcome getCallerIdentity(const Model::GetCallerIdentityRequest &request)const; void getCallerIdentityAsync(const Model::GetCallerIdentityRequest& request, const GetCallerIdentityAsyncHandler& handler, const std::shared_ptr& context = nullptr) const; GetCallerIdentityOutcomeCallable getCallerIdentityCallable(const Model::GetCallerIdentityRequest& request) const; + using RpcServiceClient::makeRequest; }; } } diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index 8ff32ea9f..70dba06c6 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -63,9 +63,11 @@ add_executable(core_ut url_ut.cc utils_ut.cc + locationclient_ut.cc location_model_describeendpoints_request_ut.cc location_model_describeendpoints_result_ut.cc + stsclient_ut.cc sts_model_assumerole_request_ut.cc sts_model_assumerole_result_ut.cc sts_model_getcalleridentity_request_ut.cc diff --git a/test/core/locationclient_ut.cc b/test/core/locationclient_ut.cc new file mode 100644 index 000000000..a89dbfc3e --- /dev/null +++ b/test/core/locationclient_ut.cc @@ -0,0 +1,174 @@ +#include +#include +#include "utils.h" +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "alibabacloud/core/AlibabaCloud.h" +#include "alibabacloud/core/SimpleCredentialsProvider.h" +#include "alibabacloud/core/location/LocationClient.h" + +using namespace std; +using namespace AlibabaCloud; +using namespace AlibabaCloud::Location; + +using ::testing::_; +using ::testing::DefaultValue; + +namespace { + + class mockLocationClient : public LocationClient { + public: + mockLocationClient(const string& key, const string& secret, const ClientConfiguration& cfg): + LocationClient(key, secret, cfg) {} + mockLocationClient(const Credentials& credentials, const ClientConfiguration& cfg): + LocationClient(credentials, cfg) {} + mockLocationClient(const std::shared_ptr &credentialsProvider, const ClientConfiguration &configuration): + LocationClient(credentialsProvider, configuration) {} + MOCK_CONST_METHOD3(makeRequest, RpcServiceClient::JsonOutcome (const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method)); + }; + + TEST(LocationClient, sync_ak) { + ClientConfiguration configuration("cn-hangzhou"); + Model::DescribeEndpointsRequest req; + req.setId("cn-hangzhou"); + req.setServiceCode("ecs"); + req.setType("openAPI"); + + mockLocationClient client("key", "secret", configuration); + RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}"); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + + LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req); + + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().success()); + EXPECT_TRUE(out.result().requestId() == "test-request-id"); + EXPECT_TRUE(out.result().endpoints().size() == 1); + + Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0]; + + EXPECT_TRUE(ep.endpoint == "test-endpoint"); + EXPECT_TRUE(ep.id == "cn-hangzhou"); + EXPECT_TRUE(ep.namespace_ == "test-namespace"); + EXPECT_TRUE(ep.type == "test-type"); + EXPECT_TRUE(ep.protocols.size() == 2); + std::vector::iterator it; + std::vector protocols = ep.protocols; + it = std::find(protocols.begin(), protocols.end(), string("HTTP")); + EXPECT_TRUE(it != protocols.end()); + it = std::find(protocols.begin(), protocols.end(), string("HTTPS")); + EXPECT_TRUE(it != protocols.end()); + } + + //{"Endpoints":{"Endpoint":[{"Protocols":{"Protocols":["HTTP","HTTPS"]},"Type":"openAPI","Namespace":"","Id":"cn-hangzhou","SerivceCode":"ecs","Endpoint":"ecs-cn-hangzhou.aliyuncs.com"}]},"RequestId":"test-request-id","Success":true} + + TEST(LocationClient, sync_credentials) { + Credentials credentials("key", "secret"); + ClientConfiguration configuration("cn-hangzhou"); + + Model::DescribeEndpointsRequest req; + req.setId("cn-hangzhou"); + req.setServiceCode("ecs"); + req.setType("openAPI"); + + mockLocationClient client(credentials, configuration); + RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}"); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + + LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req); + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().success()); + EXPECT_TRUE(out.result().requestId() == "test-request-id"); + EXPECT_TRUE(out.result().endpoints().size() == 1); + + Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0]; + + EXPECT_TRUE(ep.endpoint == "test-endpoint"); + EXPECT_TRUE(ep.id == "cn-hangzhou"); + EXPECT_TRUE(ep.namespace_ == "test-namespace"); + EXPECT_TRUE(ep.type == "test-type"); + EXPECT_TRUE(ep.protocols.size() == 2); + std::vector::iterator it; + std::vector protocols = ep.protocols; + it = std::find(protocols.begin(), protocols.end(), string("HTTP")); + EXPECT_TRUE(it != protocols.end()); + it = std::find(protocols.begin(), protocols.end(), string("HTTPS")); + EXPECT_TRUE(it != protocols.end()); + } + + TEST(LocationClient, sync_credentials_ptr) { + Credentials credentials("key", "secret"); + ClientConfiguration configuration("cn-hangzhou"); + + Model::DescribeEndpointsRequest req; + req.setId("cn-hangzhou"); + req.setServiceCode("ecs"); + req.setType("openAPI"); + + mockLocationClient client(std::make_shared(credentials), configuration); + RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}"); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req); + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().success()); + EXPECT_TRUE(out.result().requestId() == "test-request-id"); + EXPECT_TRUE(out.result().endpoints().size() == 1); + + Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0]; + + EXPECT_TRUE(ep.endpoint == "test-endpoint"); + EXPECT_TRUE(ep.id == "cn-hangzhou"); + EXPECT_TRUE(ep.namespace_ == "test-namespace"); + EXPECT_TRUE(ep.type == "test-type"); + EXPECT_TRUE(ep.protocols.size() == 2); + std::vector::iterator it; + std::vector protocols = ep.protocols; + it = std::find(protocols.begin(), protocols.end(), string("HTTP")); + EXPECT_TRUE(it != protocols.end()); + it = std::find(protocols.begin(), protocols.end(), string("HTTPS")); + EXPECT_TRUE(it != protocols.end()); + } + + + + + TEST(LocationClient, callable) { + InitializeSdk(); + ClientConfiguration configuration("cn-hangzhou"); + Model::DescribeEndpointsRequest req; + req.setId("cn-hangzhou"); + req.setServiceCode("ecs"); + req.setType("openAPI"); + + mockLocationClient client("key", "secret", configuration); + RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}"); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + + LocationClient::DescribeEndpointsOutcomeCallable cb = client.describeEndpointsCallable(req); + LocationClient::DescribeEndpointsOutcome out = cb.get(); + + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().success()); + EXPECT_TRUE(out.result().requestId() == "test-request-id"); + EXPECT_TRUE(out.result().endpoints().size() == 1); + + Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0]; + + EXPECT_TRUE(ep.endpoint == "test-endpoint"); + EXPECT_TRUE(ep.id == "cn-hangzhou"); + EXPECT_TRUE(ep.namespace_ == "test-namespace"); + EXPECT_TRUE(ep.type == "test-type"); + EXPECT_TRUE(ep.protocols.size() == 2); + std::vector::iterator it; + std::vector protocols = ep.protocols; + it = std::find(protocols.begin(), protocols.end(), string("HTTP")); + EXPECT_TRUE(it != protocols.end()); + it = std::find(protocols.begin(), protocols.end(), string("HTTPS")); + EXPECT_TRUE(it != protocols.end()); + ShutdownSdk(); + } +} diff --git a/test/core/stsclient_ut.cc b/test/core/stsclient_ut.cc new file mode 100644 index 000000000..378bb1788 --- /dev/null +++ b/test/core/stsclient_ut.cc @@ -0,0 +1,154 @@ +#include +#include +#include "utils.h" +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "alibabacloud/core/AlibabaCloud.h" +#include "alibabacloud/core/SimpleCredentialsProvider.h" +#include "alibabacloud/core/sts/StsClient.h" + +using ::testing::_; +using ::testing::DefaultValue; + +using namespace std; +using namespace AlibabaCloud; +using namespace AlibabaCloud::Sts; + +namespace { + class mockStsClient : public StsClient { + public: + mockStsClient(const string& key, const string& secret, const ClientConfiguration& cfg): + StsClient(key, secret, cfg) {} + mockStsClient(const Credentials& credentials, const ClientConfiguration& cfg): + StsClient(credentials, cfg) {} + mockStsClient(const std::shared_ptr &credentialsProvider, const ClientConfiguration &configuration): + StsClient(credentialsProvider, configuration) {} + MOCK_CONST_METHOD3(makeRequest, RpcServiceClient::JsonOutcome (const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method)); + }; + + + TEST(StsClient, getCallerIdentity_ak) { + ClientConfiguration configuration; + RpcServiceClient::JsonOutcome xout("{\"Request\":\"test-req-id\",\"AccountId\":\"123456789\",\"Arn\":\"any-arn\",\"UserId\":\"any-id\"}"); + mockStsClient client("mock-key", "mock-secret", configuration); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + Model::GetCallerIdentityRequest req; + StsClient::GetCallerIdentityOutcome out = client.getCallerIdentity(req); + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().accountId() == "123456789"); + EXPECT_TRUE(out.result().userId() == "any-id"); + EXPECT_TRUE(out.result().arn() == "any-arn"); + } + + TEST(StsClient, getCallerIdentity_credentials) { + ClientConfiguration configuration; + RpcServiceClient::JsonOutcome xout("{\"Request\":\"test-req-id\",\"AccountId\":\"123456789\",\"Arn\":\"any-arn\",\"UserId\":\"any-id\"}"); + Credentials credentials("key", "secret"); + mockStsClient client(credentials, configuration); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + Model::GetCallerIdentityRequest req; + StsClient::GetCallerIdentityOutcome out = client.getCallerIdentity(req); + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().accountId() == "123456789"); + EXPECT_TRUE(out.result().userId() == "any-id"); + EXPECT_TRUE(out.result().arn() == "any-arn"); + } + + TEST(StsClient, getCallerIdentity_credentials_ptr) { + + ClientConfiguration configuration; + RpcServiceClient::JsonOutcome xout("{\"Request\":\"test-req-id\",\"AccountId\":\"123456789\",\"Arn\":\"any-arn\",\"UserId\":\"any-id\"}"); + Credentials credentials("key", "secret"); + mockStsClient client(std::make_shared(credentials), configuration); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + Model::GetCallerIdentityRequest req; + StsClient::GetCallerIdentityOutcome out = client.getCallerIdentity(req); + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().accountId() == "123456789"); + EXPECT_TRUE(out.result().userId() == "any-id"); + EXPECT_TRUE(out.result().arn() == "any-arn"); + } + + TEST(StsClient, getCallerIdentity_ak_error) { + InitializeSdk(); + ClientConfiguration configuration; + RpcServiceClient::JsonOutcome xout(Error("any-error-code", "any-error-message")); + mockStsClient client("mock-key", "mock-secret", configuration); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + Model::GetCallerIdentityRequest req; + StsClient::GetCallerIdentityOutcome out = client.getCallerIdentity(req); + EXPECT_TRUE(out.error().errorCode() == "any-error-code"); + ShutdownSdk(); + } + + TEST(StsClient, getCallerIdentity_callable) { + InitializeSdk(); + ClientConfiguration configuration; + RpcServiceClient::JsonOutcome xout("{\"Request\":\"test-req-id\",\"AccountId\":\"123456789\",\"Arn\":\"any-arn\",\"UserId\":\"any-id\"}"); + mockStsClient client("mock-key", "mock-secret", configuration); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + Model::GetCallerIdentityRequest req; + StsClient::GetCallerIdentityOutcomeCallable cb = client.getCallerIdentityCallable(req); + StsClient::GetCallerIdentityOutcome out = cb.get(); + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().accountId() == "123456789"); + EXPECT_TRUE(out.result().userId() == "any-id"); + EXPECT_TRUE(out.result().arn() == "any-arn"); + ShutdownSdk(); + } + + TEST(StsClient, assumeRole) { + ClientConfiguration configuration; + RpcServiceClient::JsonOutcome xout("{\"RequestId\":\"any-request-id\",\"success\":true,\"AssumedRoleUser\":{\"AssumedRoleId\":\"123456789\",\"Arn\":\"any-arn\"},\"Credentials\":{\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"Expiration\":\"2019-01-15T16:31:10Z\",\"SecurityToken\":\"any-token\"}}"); + mockStsClient client("mock-key", "mock-secret", configuration); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + Model::AssumeRoleRequest req; + StsClient::AssumeRoleOutcome out = client.assumeRole(req); + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().assumedRoleUser().arn == "any-arn"); + EXPECT_TRUE(out.result().assumedRoleUser().assumedRoleId == "123456789"); + EXPECT_TRUE(out.result().credentials().accessKeyId == "any-key"); + EXPECT_TRUE(out.result().credentials().accessKeySecret == "any-secret"); + EXPECT_TRUE(out.result().credentials().expiration == "2019-01-15T16:31:10Z"); + EXPECT_TRUE(out.result().credentials().securityToken == "any-token"); + } + + TEST(StsClient, assumeRole_callable) { + InitializeSdk(); + ClientConfiguration configuration; + RpcServiceClient::JsonOutcome xout("{\"RequestId\":\"any-request-id\",\"success\":true,\"AssumedRoleUser\":{\"AssumedRoleId\":\"123456789\",\"Arn\":\"any-arn\"},\"Credentials\":{\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"Expiration\":\"2019-01-15T16:31:10Z\",\"SecurityToken\":\"any-token\"}}"); + mockStsClient client("mock-key", "mock-secret", configuration); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + Model::AssumeRoleRequest req; + StsClient::AssumeRoleOutcomeCallable cb = client.assumeRoleCallable(req); + StsClient::AssumeRoleOutcome out = cb.get(); + + EXPECT_TRUE(out.error().errorCode().empty()); + EXPECT_TRUE(out.result().assumedRoleUser().arn == "any-arn"); + EXPECT_TRUE(out.result().assumedRoleUser().assumedRoleId == "123456789"); + EXPECT_TRUE(out.result().credentials().accessKeyId == "any-key"); + EXPECT_TRUE(out.result().credentials().accessKeySecret == "any-secret"); + EXPECT_TRUE(out.result().credentials().expiration == "2019-01-15T16:31:10Z"); + EXPECT_TRUE(out.result().credentials().securityToken == "any-token"); + ShutdownSdk(); + } + + TEST(StsClient, assumeRole_error) { + ClientConfiguration configuration; + RpcServiceClient::JsonOutcome xout(Error("any-error-code", "any-error-message")); + mockStsClient client("mock-key", "mock-secret", configuration); + DefaultValue::Set(xout); + EXPECT_CALL(client, makeRequest(_, _, _)); + Model::AssumeRoleRequest req; + StsClient::AssumeRoleOutcome out = client.assumeRole(req); + EXPECT_TRUE(out.error().errorCode() == "any-error-code"); + } + +}