From e00a04b07176a1bc769aacdfd43761ec17eacaf0 Mon Sep 17 00:00:00 2001 From: zhangzifa Date: Fri, 18 Jan 2019 18:15:04 +0800 Subject: [PATCH] unit test for InstanceProfileCredentialsProvider --- .../core/InstanceProfileCredentialsProvider.h | 15 ++--- core/src/EcsMetadataFetcher.cc | 4 ++ core/src/EcsMetadataFetcher.h | 3 +- .../src/InstanceProfileCredentialsProvider.cc | 30 ++++------ test/core/CMakeLists.txt | 2 + .../instanceprofilecredentialsprovider_ut.cc | 56 +++++++++++++++++++ 6 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 test/core/instanceprofilecredentialsprovider_ut.cc diff --git a/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h b/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h index 5757b161f..63c10e03c 100644 --- a/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h +++ b/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h @@ -1,12 +1,12 @@ /* * Copyright 2009-2017 Alibaba Cloud All rights reserved. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,11 +23,11 @@ #include #include "CredentialsProvider.h" #include "Credentials.h" +#include "../src/EcsMetadataFetcher.h" namespace AlibabaCloud { - class EcsMetadataFetcher; - class ALIBABACLOUD_CORE_EXPORT InstanceProfileCredentialsProvider : public CredentialsProvider + class ALIBABACLOUD_CORE_EXPORT InstanceProfileCredentialsProvider : public CredentialsProvider, public EcsMetadataFetcher { public: InstanceProfileCredentialsProvider(const std::string &roleName, int durationSeconds = 3600); @@ -35,7 +35,9 @@ namespace AlibabaCloud std::string roleName()const; virtual Credentials getCredentials() override; - + using EcsMetadataFetcher::roleName; + using EcsMetadataFetcher::setRoleName; + using EcsMetadataFetcher::getMetadata; private: void loadCredentials(); bool checkExpiry()const; @@ -44,7 +46,6 @@ namespace AlibabaCloud Credentials cachedCredentials_; int durationSeconds_; std::chrono::system_clock::time_point expiry_; - EcsMetadataFetcher *fetcher_; }; } #endif \ No newline at end of file diff --git a/core/src/EcsMetadataFetcher.cc b/core/src/EcsMetadataFetcher.cc index 3effb0275..5084310d2 100644 --- a/core/src/EcsMetadataFetcher.cc +++ b/core/src/EcsMetadataFetcher.cc @@ -40,6 +40,10 @@ void EcsMetadataFetcher::setRoleName(const std::string & roleName) roleName_ = roleName; } +std::string EcsMetadataFetcher::getMetadata() { + return getMetadata(METADATA_SERVICE_HOST, URL_IN_ECS_METADATA); +} + std::string EcsMetadataFetcher::getMetadata(const std::string host, const std::string in_path) { std::stringstream path; diff --git a/core/src/EcsMetadataFetcher.h b/core/src/EcsMetadataFetcher.h index c896abf23..e3da75b67 100644 --- a/core/src/EcsMetadataFetcher.h +++ b/core/src/EcsMetadataFetcher.h @@ -35,7 +35,8 @@ namespace AlibabaCloud std::string roleName()const; void setRoleName(const std::string & roleName); - std::string getMetadata(const std::string host = METADATA_SERVICE_HOST, const std::string path = URL_IN_ECS_METADATA); + std::string getMetadata(const std::string host, const std::string path); + virtual std::string getMetadata(); private: std::string roleName_; }; diff --git a/core/src/InstanceProfileCredentialsProvider.cc b/core/src/InstanceProfileCredentialsProvider.cc index c104055bc..19c2bed70 100644 --- a/core/src/InstanceProfileCredentialsProvider.cc +++ b/core/src/InstanceProfileCredentialsProvider.cc @@ -1,12 +1,12 @@ /* * Copyright 2009-2017 Alibaba Cloud All rights reserved. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -27,23 +27,17 @@ using namespace AlibabaCloud; InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::string & roleName, int durationSeconds): CredentialsProvider(), + EcsMetadataFetcher(), durationSeconds_(durationSeconds), cachedMutex_(), cachedCredentials_("", ""), - fetcher_(new EcsMetadataFetcher), expiry_() { - fetcher_->setRoleName(roleName); + setRoleName(roleName); } InstanceProfileCredentialsProvider::~InstanceProfileCredentialsProvider() { - delete fetcher_; -} - -std::string InstanceProfileCredentialsProvider::roleName()const -{ - return fetcher_->roleName(); } Credentials InstanceProfileCredentialsProvider::getCredentials() @@ -68,16 +62,16 @@ void InstanceProfileCredentialsProvider::loadCredentials() std::lock_guard locker(cachedMutex_); if (checkExpiry()) { - auto outcome = fetcher_->getMetadata(); + auto outcome = getMetadata(); Json::Value value; Json::Reader reader; if (reader.parse(outcome, value)) { - if (value["Code"] == nullptr - &&value["AccessKeyId"] == nullptr - &&value["AccessKeySecret"] == nullptr - &&value["SecurityToken"] == nullptr - &&value["Expiration"] == nullptr) + if (value["Code"].empty() + &&value["AccessKeyId"].empty() + &&value["AccessKeySecret"].empty() + &&value["SecurityToken"].empty() + &&value["Expiration"].empty()) { cachedCredentials_ = Credentials("",""); return; @@ -96,7 +90,7 @@ void InstanceProfileCredentialsProvider::loadCredentials() std::tm tm = {}; #if defined(__GNUG__) && __GNUC__ < 5 strptime(expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm); -#else +#else std::stringstream ss(expiration); ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); #endif diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index ee5e112a2..09f867667 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -64,6 +64,8 @@ add_executable(core_ut url_ut.cc utils_ut.cc + instanceprofilecredentialsprovider_ut.cc + locationclient_ut.cc location_model_describeendpoints_request_ut.cc location_model_describeendpoints_result_ut.cc diff --git a/test/core/instanceprofilecredentialsprovider_ut.cc b/test/core/instanceprofilecredentialsprovider_ut.cc new file mode 100644 index 000000000..1a64cdcd5 --- /dev/null +++ b/test/core/instanceprofilecredentialsprovider_ut.cc @@ -0,0 +1,56 @@ +#include +#include +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "alibabacloud/core/InstanceProfileCredentialsProvider.h" + +using namespace std; +using namespace AlibabaCloud; + +using ::testing::_; +using ::testing::DefaultValue; + + +class mockInstanceProfileCredentialsProvider: public InstanceProfileCredentialsProvider { + public: + mockInstanceProfileCredentialsProvider(const std::string &roleName, int durationSeconds): + + InstanceProfileCredentialsProvider(roleName, durationSeconds) {} + MOCK_METHOD0(getMetadata, std::string()); +}; + +TEST(InstanceProfileCredentialsProvider, basic) { + mockInstanceProfileCredentialsProvider p("test-role-name", 1800); + + DefaultValue::Set("{\"Code\":\"test-code\",\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"SecurityToken\":\"any-token\",\"Expiration\":3233}"); + EXPECT_CALL(p, getMetadata()); + + Credentials credentials = p.getCredentials(); + EXPECT_TRUE(credentials.accessKeyId() == "any-key"); + EXPECT_TRUE(credentials.accessKeySecret() == "any-secret"); + EXPECT_TRUE(credentials.sessionToken() == "any-token"); +} + +TEST(InstanceProfileCredentialsProvider, empty) { + mockInstanceProfileCredentialsProvider p("test-role-name", 1800); + + DefaultValue::Set("{}"); + EXPECT_CALL(p, getMetadata()); + + Credentials credentials = p.getCredentials(); + EXPECT_TRUE(credentials.accessKeyId().empty()); + EXPECT_TRUE(credentials.accessKeySecret().empty()); + EXPECT_TRUE(credentials.sessionToken().empty()); +} + +TEST(InstanceProfileCredentialsProvider, empty_1) { + mockInstanceProfileCredentialsProvider p("test-role-name", 1800); + + DefaultValue::Set(""); + EXPECT_CALL(p, getMetadata()); + + Credentials credentials = p.getCredentials(); + EXPECT_TRUE(credentials.accessKeyId().empty()); + EXPECT_TRUE(credentials.accessKeySecret().empty()); + EXPECT_TRUE(credentials.sessionToken().empty()); +} \ No newline at end of file