16 #include <gtest/gtest.h> 20 #include <aws/core/Aws.h> 21 #include <aws/core/utils/json/JsonSerializer.h> 28 using ::testing::Matcher;
29 using ::testing::DoAll;
30 using ::testing::SetArgReferee;
31 using ::testing::Return;
37 bool operator==(
const AWSCredentials & left,
const AWSCredentials & right)
40 (left.GetAWSAccessKeyId() == right.GetAWSAccessKeyId())
41 && (left.GetAWSSecretKey() == right.GetAWSSecretKey())
42 && (left.GetSessionToken() == right.GetSessionToken())
59 std::shared_ptr<ParameterReaderMock> param_reader_ =
60 std::make_shared<ParameterReaderMock>();
64 {
"cafile",
"M2M1NTA0NTQxMDg4YTUxMzcyMzY4MTNh"}, {
"certfile",
"MmQ2NWEyZmFmNThlOWM1"},
65 {
"keyfile",
"MDAxZmZiY2VjYmIwMGM"}, {
"endpoint",
"xNDhkYzU5MjNm"},
66 {
"role",
"YmIwNTMx"}, {
"thing_name",
"MzExMTdlMGI2YzY5ZjJmYTli"},
67 {
"connect_timeout_ms",
"42"}, {
"total_timeout_ms",
"27"}
71 "cafile",
"certfile",
"keyfile",
"endpoint",
"role",
"thing_name" 75 kFullIotConfigMap.at(
"cafile").c_str(),
76 kFullIotConfigMap.at(
"certfile").c_str(),
77 kFullIotConfigMap.at(
"keyfile").c_str(),
78 kFullIotConfigMap.at(
"endpoint").c_str(),
79 kFullIotConfigMap.at(
"role").c_str(),
80 kFullIotConfigMap.at(
"thing_name").c_str(),
81 StringUtils::ConvertToInt32(kFullIotConfigMap.at(
"connect_timeout_ms").c_str()),
82 StringUtils::ConvertToInt32(kFullIotConfigMap.at(
"total_timeout_ms").c_str())
86 {
"expiration",
"2019-01-10T21:57:06Z"},
87 {
"accessKeyId",
"ZWM2ODYzNDEwZWJhNGM0NjZiYzk4ZDI4"},
88 {
"secretAccessKey",
"YWYyNWM0NmEzZWE1NWQy"},
89 {
"sessionToken",
"YTFhM2NhNjM5OGZlMDlmYmRmMTY3Mzk5WQyNDVkMTJjYThi"}
93 "expiration",
"accessKeyId",
"secretAccessKey",
"sessionToken" 98 EXPECT_CALL(*param_reader_, ReadParam(_, Matcher<std::map<std::string, std::string> &>(_)))
104 EXPECT_FALSE(success);
109 public ::testing::WithParamInterface<std::string> {};
113 auto missing_config_key = GetParam();
114 auto partial_iot_config = std::map<std::string, std::string>(kFullIotConfigMap);
115 partial_iot_config.erase(missing_config_key);
116 EXPECT_CALL(*param_reader_, ReadParam(_, Matcher<std::map<std::string, std::string> &>(_)))
122 EXPECT_FALSE(success);
126 TestGetServiceAuthConfigPartialIotConfig,
133 EXPECT_CALL(*param_reader_, ReadParam(_, Matcher<std::map<std::string, std::string> &>(_)))
139 EXPECT_TRUE(success);
140 EXPECT_STREQ(kFullIotConfigMap.at(
"cafile").c_str(), config.
iot.
cafile.c_str());
141 EXPECT_STREQ(kFullIotConfigMap.at(
"certfile").c_str(), config.
iot.
certfile.c_str());
142 EXPECT_STREQ(kFullIotConfigMap.at(
"keyfile").c_str(), config.
iot.
keyfile.c_str());
143 EXPECT_STREQ(kFullIotConfigMap.at(
"endpoint").c_str(), config.
iot.
host.c_str());
144 EXPECT_STREQ(kFullIotConfigMap.at(
"role").c_str(), config.
iot.
role.c_str());
145 EXPECT_STREQ(kFullIotConfigMap.at(
"thing_name").c_str(), config.
iot.
name.c_str());
146 EXPECT_EQ(StringUtils::ConvertToInt32(kFullIotConfigMap.at(
"connect_timeout_ms").c_str()),
148 EXPECT_EQ(StringUtils::ConvertToInt32(kFullIotConfigMap.at(
"total_timeout_ms").c_str()),
158 EXPECT_EQ(default_conf_chain.GetProviders().size() + 1, configured_chain.GetProviders().size());
170 EXPECT_EQ(default_conf_chain.GetProviders().size(), configured_chain.GetProviders().size());
181 return IotRoleCredentialsProvider::ValidateResponse(value);
189 auto provider = std::make_shared<OpenIotRoleCredentialsProvider>(kFullIotConfig);
190 AWSCredentials initial_credentials(provider->GetAWSCredentials());
192 provider->PublicRefresh();
195 EXPECT_EQ(initial_credentials, provider->GetAWSCredentials());
200 auto provider = std::make_shared<OpenIotRoleCredentialsProvider>(kFullIotConfig);
201 AWSCredentials aws_credentials{
"ZWM2ODYzNDEwZWJhNGM0NjZiYzk4ZDI4",
202 "YWYyNWM0NmEzZWE1NWQy",
"YTFhM2NhNjM5OGZlMDlmYmRmMTY3Mzk5WQyNDVkMTJjYThi"};
204 provider->PublicSetCredentials(aws_credentials);
206 EXPECT_EQ(aws_credentials, provider->GetAWSCredentials());
211 auto provider = std::make_shared<OpenIotRoleCredentialsProvider>(kFullIotConfig);
213 Json::JsonValue malformed_json(Aws::String(
"malformed"));
214 EXPECT_FALSE(provider->PublicValidateResponse(malformed_json));
216 auto response = Json::JsonValue();
217 EXPECT_FALSE(provider->PublicValidateResponse(response));
219 EXPECT_FALSE(provider->PublicValidateResponse(response.WithString(
"credentials", Aws::String(
"foo"))));
221 auto credentials = Json::JsonValue();
222 EXPECT_FALSE(provider->PublicValidateResponse(response.WithObject(
"credentials", credentials)));
224 credentials = credentials.WithString(
"expiration", Aws::String(
"2019-01-10T21:57:06Z"));
225 EXPECT_FALSE(provider->PublicValidateResponse(response.WithObject(
"credentials", credentials)));
227 credentials = credentials.WithString(
"accessKeyId", Aws::String(
"ZWM2ODYzNDEwZWJhNGM0NjZiYzk4ZDI4"));
228 EXPECT_FALSE(provider->PublicValidateResponse(response.WithObject(
"credentials", credentials)));
230 credentials = credentials.WithString(
"secretAccessKey", Aws::String(
"YWYyNWM0NmEzZWE1NWQy"));
231 EXPECT_FALSE(provider->PublicValidateResponse(response.WithObject(
"credentials", credentials)));
233 credentials = credentials.WithString(
"sessionToken", Aws::String(
"YTFhM2NhNjM5OGZlMDlmYmRmMTY3Mzk5WQyNDVkMTJjYThi"));
234 EXPECT_TRUE(provider->PublicValidateResponse(response.WithObject(
"credentials", credentials)));
237 int main(
int argc,
char ** argv)
239 Aws::SDKOptions options;
240 Aws::InitAPI(options);
242 testing::InitGoogleTest(&argc, argv);
243 auto test_result = RUN_ALL_TESTS();
245 Aws::ShutdownAPI(options);
static const std::list< std::string > kFullCredentialsInfoKeys
OpenIotRoleCredentialsProvider(const IotRoleConfig &config)
void PublicSetCredentials(AWSCredentials &creds)
Credentials provider chain for ROS AWS service integrations.
Aws::Auth::AWSCredentials GetCachedCredentials()
Aws::String host
Host name of the iot:CredentialProvider endpoint.
bool operator==(const ClientConfiguration &left, const ClientConfiguration &right)
IotRoleConfig iot
IoT-specific configuration.
static const std::list< std::string > kFullIotConfigMandatoryKeys
static const std::map< std::string, std::string > kFullIotConfigMap
long connect_timeout_ms
Number of ms to wait before timing out when connecting to the endpoint.
TEST_P(TestGetServiceAuthConfigFixture, TestGetServiceAuthConfigPartialIotConfig)
AwsError
Defines error return codes for functions This enum defines standard error codes that will be returned...
Aws::String cafile
Path to the Root CA for the endpoint.
bool PublicValidateResponse(Aws::Utils::Json::JsonValue &value)
static const IotRoleConfig kFullIotConfig
Auth configuration needed to retrieve AWS credentials via the IoT service.
Aws::String name
Thing name for the device.
Aws::String certfile
Path to the certificate which identifies the device.
Aws::String role
Name of the AWS IoT Role Alias for the device.
int main(int argc, char **argv)
TEST_F(ServiceCredentialsProviderFixture, TestGetServiceAuthConfigNoIotConfig)
static const std::map< std::string, std::string > kFullCredentialsInfo
INSTANTIATE_TEST_CASE_P(TestGetServiceAuthConfigPartialIotConfig, TestGetServiceAuthConfigFixture,::testing::ValuesIn(ServiceCredentialsProviderFixture::kFullIotConfigMandatoryKeys))
long total_timeout_ms
Total number of ms to wait for the entire connect/request/response transaction.
bool GetServiceAuthConfig(ServiceAuthConfig &config, const std::shared_ptr< Aws::Client::ParameterReaderInterface > ¶meters)
Retrieves service authorization data from a ParameterReaderInterface and populates the ServiceAuthCon...
Aws::String keyfile
Path to the related private key for the certificate.
AWSCredentialsProvider that obtains credentials using the AWS IoT Core service.
Auth configuration for ROS AWS service integration.