client_configuration_provider_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #include <aws/core/client/ClientConfiguration.h>
18 #include <gtest/gtest.h>
19 #include <ros/ros.h>
20 
21 #define CLIENT_CONFIG_PREFIX "aws_client_configuration"
22 #define USER_AGENT_SUFFIX "/user_agent"
23 
29 void InitializeNodeAndConfig(ros::NodeHandle & node, Aws::Client::ClientConfiguration & config)
30 {
31  node.setParam(CLIENT_CONFIG_PREFIX "/region", "uk-north-20");
32  config.region = "uk-north-20";
33  node.setParam(CLIENT_CONFIG_PREFIX "/proxy_port", 787);
34  config.proxyPort = 787;
35  node.setParam(CLIENT_CONFIG_PREFIX "/connect_timeout_ms", 511111);
36  config.connectTimeoutMs = 511111;
37  node.setParam(CLIENT_CONFIG_PREFIX "/verify_SSL", true);
38  config.verifySSL = true;
39  node.setParam(CLIENT_CONFIG_PREFIX "/follow_redirects", true);
40  config.followRedirects = true;
41 }
42 
46 TEST(DefaultClientConfigurationProvider, getClientConfiguration)
47 {
48  Aws::Client::ClientConfiguration prepared_config;
49  ros::NodeHandle dummy_node;
50 
51  InitializeNodeAndConfig(dummy_node, prepared_config);
52  auto parameter_reader = std::make_shared<Aws::Client::Ros1NodeParameterReader>();
53  Aws::Client::ClientConfigurationProvider config_provider(parameter_reader);
54  Aws::Client::ClientConfiguration generated_config = config_provider.GetClientConfiguration();
55  prepared_config.userAgent =
56  generated_config
57  .userAgent; /* Set the user agent to w/e was generated. Will be tested separately */
58 
59  prepared_config.followRedirects = false;
60  ASSERT_NE(prepared_config, generated_config);
61 
62  prepared_config.followRedirects = true;
63  ASSERT_EQ(prepared_config, generated_config);
64 }
65 
70 TEST(DefaultClientConfigurationProvider, userAgentTest)
71 {
72  Aws::Client::ClientConfiguration prepared_config;
73  ros::NodeHandle dummy_node;
74  InitializeNodeAndConfig(dummy_node, prepared_config);
75  auto parameter_reader = std::make_shared<Aws::Client::Ros1NodeParameterReader>();
76  Aws::Client::ClientConfigurationProvider config_provider(parameter_reader);
77 
78  /* Verify userAgent starts with "ros-<distro>" */
79  std::vector<std::string> ros1_distros = {"indigo", "jade", "kinetic", "lunar", "melodic"};
80  Aws::Client::ClientConfiguration generated_config = config_provider.GetClientConfiguration();
81  size_t ros_user_agent_index = std::string::npos;
82  for (const auto & distro : ros1_distros) {
83  ros_user_agent_index =
84  generated_config.userAgent.find(std::string("exec-env/AWS_RoboMaker ros-" + distro).c_str());
85  if (std::string::npos != ros_user_agent_index) {
86  break;
87  }
88  }
89  ASSERT_NE(std::string::npos, ros_user_agent_index);
90 
91  /* Test version override */
92  const char version_override[] = "1.12.16";
93  generated_config = config_provider.GetClientConfiguration(version_override);
94  ASSERT_EQ(0, generated_config.userAgent
95  .substr(generated_config.userAgent.size() - (sizeof(version_override) - 1),
96  sizeof(version_override) - 1)
97  .compare(version_override));
98 
99  /* Verify that we only add information on top of the SDK's userAgent */
100  Aws::Client::ClientConfiguration stock_config;
101  ASSERT_EQ(
102  0,
103  generated_config.userAgent.substr(0, ros_user_agent_index - 1).compare(stock_config.userAgent));
104 
105  /* UserAgent override using ROS parameter */
106  prepared_config.userAgent = "my-user-agent";
107  std::string stored_user_agent;
109  dummy_node.getParam(CLIENT_CONFIG_PREFIX USER_AGENT_SUFFIX, stored_user_agent);
110  }
111  dummy_node.setParam(CLIENT_CONFIG_PREFIX USER_AGENT_SUFFIX, prepared_config.userAgent.c_str());
112  generated_config = config_provider.GetClientConfiguration();
113  ASSERT_EQ(prepared_config, generated_config);
114  if (!stored_user_agent.empty()) {
115  dummy_node.setParam(CLIENT_CONFIG_PREFIX USER_AGENT_SUFFIX, stored_user_agent);
116  } else {
117  dummy_node.deleteParam(CLIENT_CONFIG_PREFIX USER_AGENT_SUFFIX);
118  }
119 }
120 
121 int main(int argc, char ** argv)
122 {
123  testing::InitGoogleTest(&argc, argv);
124  ros::init(argc, argv, "test_configuration_provider");
125  return RUN_ALL_TESTS();
126 }
bool deleteParam(const std::string &key) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
void InitializeNodeAndConfig(ros::NodeHandle &node, Aws::Client::ClientConfiguration &config)
bool getParam(const std::string &key, std::string &s) const
bool hasParam(const std::string &key) const
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
TEST(DefaultClientConfigurationProvider, getClientConfiguration)


aws_ros1_common
Author(s): AWS RoboMaker
autogenerated on Thu Mar 4 2021 03:25:44