Skip to content

Commit c68f3a6

Browse files
committed
refactor the test
1 parent e64cbf0 commit c68f3a6

File tree

1 file changed

+70
-200
lines changed

1 file changed

+70
-200
lines changed

tests/aws-cpp-sdk-core-tests/aws/auth/CredentialTrackingTest.cpp

Lines changed: 70 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ using namespace Aws::Client;
2323
using namespace Aws::Auth;
2424
using namespace Aws::Http;
2525

26-
static const char ALLOCATION_TAG[] = "CredentialTrackingTest";
26+
namespace {
27+
const char* TEST_LOG_TAG = "CredentialTrackingTest";
28+
}
2729

2830
static Aws::String WrapEchoStringWithSingleQuoteForUnixShell(Aws::String str)
2931
{
@@ -40,21 +42,21 @@ class CredentialTestingClient : public Aws::Client::AWSClient
4042
public:
4143
explicit CredentialTestingClient(const Aws::Client::ClientConfiguration& configuration)
4244
: AWSClient(configuration,
43-
Aws::MakeShared<Aws::Client::AWSAuthV4Signer>(ALLOCATION_TAG,
44-
Aws::MakeShared<DefaultAWSCredentialsProviderChain>(ALLOCATION_TAG),
45+
Aws::MakeShared<Aws::Client::AWSAuthV4Signer>(TEST_LOG_TAG,
46+
Aws::MakeShared<DefaultAWSCredentialsProviderChain>(TEST_LOG_TAG),
4547
"service", configuration.region),
46-
Aws::MakeShared<MockAWSErrorMarshaller>(ALLOCATION_TAG))
48+
Aws::MakeShared<MockAWSErrorMarshaller>(TEST_LOG_TAG))
4749
{
4850
}
4951

5052
// Constructor with custom credential provider for IMDS test
5153
explicit CredentialTestingClient(const Aws::Client::ClientConfiguration& configuration,
5254
std::shared_ptr<AWSCredentialsProvider> credentialsProvider)
5355
: AWSClient(configuration,
54-
Aws::MakeShared<Aws::Client::AWSAuthV4Signer>(ALLOCATION_TAG,
56+
Aws::MakeShared<Aws::Client::AWSAuthV4Signer>(TEST_LOG_TAG,
5557
credentialsProvider,
5658
"service", configuration.region),
57-
Aws::MakeShared<MockAWSErrorMarshaller>(ALLOCATION_TAG))
59+
Aws::MakeShared<MockAWSErrorMarshaller>(TEST_LOG_TAG))
5860
{
5961
}
6062

@@ -82,8 +84,8 @@ class CredentialTrackingTest : public Aws::Testing::AwsCppSdkGTestSuite
8284

8385
void SetUp() override
8486
{
85-
mockHttpClient = Aws::MakeShared<MockHttpClient>(ALLOCATION_TAG);
86-
mockHttpClientFactory = Aws::MakeShared<MockHttpClientFactory>(ALLOCATION_TAG);
87+
mockHttpClient = Aws::MakeShared<MockHttpClient>(TEST_LOG_TAG);
88+
mockHttpClientFactory = Aws::MakeShared<MockHttpClientFactory>(TEST_LOG_TAG);
8789
mockHttpClientFactory->SetClient(mockHttpClient);
8890
SetHttpClientFactory(mockHttpClientFactory);
8991
}
@@ -96,6 +98,56 @@ class CredentialTrackingTest : public Aws::Testing::AwsCppSdkGTestSuite
9698
Aws::Http::CleanupHttp();
9799
Aws::Http::InitHttp();
98100
}
101+
102+
void RunTestWithCredentialsProvider(const std::shared_ptr<AWSCredentialsProvider>& credentialsProvider, std::string id) {
103+
// Setup mock response
104+
std::shared_ptr<HttpRequest> requestTmp =
105+
CreateHttpRequest(Aws::Http::URI("dummy"), Aws::Http::HttpMethod::HTTP_POST,
106+
Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
107+
auto successResponse = Aws::MakeShared<Standard::StandardHttpResponse>(TEST_LOG_TAG, requestTmp);
108+
successResponse->SetResponseCode(HttpResponseCode::OK);
109+
successResponse->GetResponseBody() << "{}";
110+
mockHttpClient->AddResponseToReturn(successResponse);
111+
112+
// Create client configuration
113+
Aws::Client::ClientConfigurationInitValues cfgInit;
114+
cfgInit.shouldDisableIMDS = true;
115+
Aws::Client::ClientConfiguration clientConfig(cfgInit);
116+
clientConfig.region = Aws::Region::US_EAST_1;
117+
118+
// Create credential testing client that uses default provider chain
119+
CredentialTestingClient client(clientConfig, credentialsProvider);
120+
121+
// Create mock request
122+
AmazonWebServiceRequestMock mockRequest;
123+
124+
// Make request
125+
auto outcome = client.MakeRequest(mockRequest);
126+
ASSERT_TRUE(outcome.IsSuccess());
127+
128+
// Verify User-Agent contains environment credentials tracking
129+
auto lastRequest = mockHttpClient->GetMostRecentHttpRequest();
130+
EXPECT_TRUE(lastRequest.HasHeader(Aws::Http::USER_AGENT_HEADER));
131+
const auto& userAgent = lastRequest.GetHeaderValue(Aws::Http::USER_AGENT_HEADER);
132+
EXPECT_FALSE(userAgent.empty());
133+
134+
const auto userAgentParsed = Aws::Utils::StringUtils::Split(userAgent, ' ');
135+
136+
// Verify there's only one m/ section (no duplicate m/ sections)
137+
int mSectionCount = 0;
138+
for (const auto& part : userAgentParsed) {
139+
if (part.find("m/") != Aws::String::npos) {
140+
mSectionCount++;
141+
}
142+
}
143+
EXPECT_EQ(1, mSectionCount);
144+
145+
// Check for environment credentials business metric (g) in user agent
146+
auto businessMetrics = std::find_if(userAgentParsed.begin(), userAgentParsed.end(),
147+
[&id](const Aws::String& value) { return value.find("m/") != Aws::String::npos && value.find(id) != Aws::String::npos; });
148+
149+
EXPECT_TRUE(businessMetrics != userAgentParsed.end());
150+
}
99151
};
100152

101153
TEST_F(CredentialTrackingTest, TestEnvironmentCredentialsTracking)
@@ -104,54 +156,8 @@ TEST_F(CredentialTrackingTest, TestEnvironmentCredentialsTracking)
104156
{"AWS_ACCESS_KEY_ID", "test-access-key"},
105157
{"AWS_SECRET_ACCESS_KEY", "test-secret-key"},
106158
}};
107-
108-
// Setup mock response
109-
std::shared_ptr<HttpRequest> requestTmp =
110-
CreateHttpRequest(Aws::Http::URI("dummy"), Aws::Http::HttpMethod::HTTP_POST,
111-
Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
112-
auto successResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, requestTmp);
113-
successResponse->SetResponseCode(HttpResponseCode::OK);
114-
successResponse->GetResponseBody() << "{}";
115-
mockHttpClient->AddResponseToReturn(successResponse);
116-
117-
// Create client configuration
118-
Aws::Client::ClientConfigurationInitValues cfgInit;
119-
cfgInit.shouldDisableIMDS = true;
120-
Aws::Client::ClientConfiguration clientConfig(cfgInit);
121-
clientConfig.region = Aws::Region::US_EAST_1;
122-
123-
// Create credential testing client that uses default provider chain
124-
CredentialTestingClient client(clientConfig);
125-
126-
// Create mock request
127-
AmazonWebServiceRequestMock mockRequest;
128-
129-
// Make request
130-
auto outcome = client.MakeRequest(mockRequest);
131-
ASSERT_TRUE(outcome.IsSuccess());
132-
133-
// Verify User-Agent contains environment credentials tracking
134-
auto lastRequest = mockHttpClient->GetMostRecentHttpRequest();
135-
EXPECT_TRUE(lastRequest.HasHeader(Aws::Http::USER_AGENT_HEADER));
136-
const auto& userAgent = lastRequest.GetHeaderValue(Aws::Http::USER_AGENT_HEADER);
137-
EXPECT_FALSE(userAgent.empty());
138-
139-
const auto userAgentParsed = Aws::Utils::StringUtils::Split(userAgent, ' ');
140-
141-
// Verify there's only one m/ section (no duplicate m/ sections)
142-
int mSectionCount = 0;
143-
for (const auto& part : userAgentParsed) {
144-
if (part.find("m/") != Aws::String::npos) {
145-
mSectionCount++;
146-
}
147-
}
148-
EXPECT_EQ(1, mSectionCount);
149-
150-
// Check for environment credentials business metric (g) in user agent
151-
auto businessMetrics = std::find_if(userAgentParsed.begin(), userAgentParsed.end(),
152-
[](const Aws::String& value) { return value.find("m/") != Aws::String::npos && value.find("g") != Aws::String::npos; });
153-
154-
EXPECT_TRUE(businessMetrics != userAgentParsed.end());
159+
auto credsProvider = Aws::MakeShared<Aws::Auth::EnvironmentAWSCredentialsProvider>(TEST_LOG_TAG);
160+
RunTestWithCredentialsProvider(std::move(credsProvider), "g");
155161
}
156162

157163
TEST_F(CredentialTrackingTest, TestProfileCredentialsTracking)
@@ -170,53 +176,8 @@ TEST_F(CredentialTrackingTest, TestProfileCredentialsTracking)
170176
}};
171177
Aws::Config::ReloadCachedCredentialsFile();
172178

173-
// Setup mock response
174-
std::shared_ptr<HttpRequest> requestTmp =
175-
CreateHttpRequest(Aws::Http::URI("dummy"), Aws::Http::HttpMethod::HTTP_POST,
176-
Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
177-
auto successResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, requestTmp);
178-
successResponse->SetResponseCode(HttpResponseCode::OK);
179-
successResponse->GetResponseBody() << "{}";
180-
mockHttpClient->AddResponseToReturn(successResponse);
181-
182-
// Create client configuration
183-
Aws::Client::ClientConfigurationInitValues cfgInit;
184-
cfgInit.shouldDisableIMDS = true;
185-
Aws::Client::ClientConfiguration clientConfig(cfgInit);
186-
clientConfig.region = Aws::Region::US_EAST_1;
187-
188-
// Create credential testing client that uses default provider chain
189-
CredentialTestingClient client(clientConfig);
190-
191-
// Create mock request
192-
AmazonWebServiceRequestMock mockRequest;
193-
194-
// Make request
195-
auto outcome = client.MakeRequest(mockRequest);
196-
ASSERT_TRUE(outcome.IsSuccess());
197-
198-
// Verify User-Agent contains profile credentials tracking
199-
auto lastRequest = mockHttpClient->GetMostRecentHttpRequest();
200-
EXPECT_TRUE(lastRequest.HasHeader(Aws::Http::USER_AGENT_HEADER));
201-
const auto& userAgent = lastRequest.GetHeaderValue(Aws::Http::USER_AGENT_HEADER);
202-
EXPECT_FALSE(userAgent.empty());
203-
204-
const auto userAgentParsed = Aws::Utils::StringUtils::Split(userAgent, ' ');
205-
206-
// Verify there's only one m/ section (no duplicate m/ sections)
207-
int mSectionCount = 0;
208-
for (const auto& part : userAgentParsed) {
209-
if (part.find("m/") != Aws::String::npos) {
210-
mSectionCount++;
211-
}
212-
}
213-
EXPECT_EQ(1, mSectionCount);
214-
215-
// Check for profile credentials business metric (n) in user agent
216-
auto businessMetrics = std::find_if(userAgentParsed.begin(), userAgentParsed.end(),
217-
[](const Aws::String& value) { return value.find("m/") != Aws::String::npos && value.find("n") != Aws::String::npos; });
218-
219-
EXPECT_TRUE(businessMetrics != userAgentParsed.end());
179+
auto credsProvider = Aws::MakeShared<Aws::Auth::ProfileConfigFileAWSCredentialsProvider>(TEST_LOG_TAG);
180+
RunTestWithCredentialsProvider(std::move(credsProvider), "n");
220181
}
221182

222183
TEST_F(CredentialTrackingTest, TestProcessCredentialsTracking)
@@ -236,111 +197,20 @@ TEST_F(CredentialTrackingTest, TestProcessCredentialsTracking)
236197
// Force reload config file after setting environment variable
237198
Aws::Config::ReloadCachedConfigFile();
238199

239-
// Setup mock response
240-
std::shared_ptr<HttpRequest> requestTmp =
241-
CreateHttpRequest(Aws::Http::URI("dummy"), Aws::Http::HttpMethod::HTTP_POST,
242-
Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
243-
auto successResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, requestTmp);
244-
successResponse->SetResponseCode(HttpResponseCode::OK);
245-
successResponse->GetResponseBody() << "{}";
246-
mockHttpClient->AddResponseToReturn(successResponse);
247-
248-
// Create client configuration
249-
Aws::Client::ClientConfigurationInitValues cfgInit;
250-
cfgInit.shouldDisableIMDS = true;
251-
Aws::Client::ClientConfiguration clientConfig(cfgInit);
252-
clientConfig.region = Aws::Region::US_EAST_1;
253-
254-
// Create credential testing client that uses default provider chain
255-
CredentialTestingClient client(clientConfig);
256-
257-
// Create mock request
258-
AmazonWebServiceRequestMock mockRequest;
259-
260-
// Make request
261-
auto outcome = client.MakeRequest(mockRequest);
262-
ASSERT_TRUE(outcome.IsSuccess());
263-
264-
// Verify User-Agent contains process credentials tracking
265-
auto lastRequest = mockHttpClient->GetMostRecentHttpRequest();
266-
EXPECT_TRUE(lastRequest.HasHeader(Aws::Http::USER_AGENT_HEADER));
267-
const auto& userAgent = lastRequest.GetHeaderValue(Aws::Http::USER_AGENT_HEADER);
268-
EXPECT_FALSE(userAgent.empty());
269-
270-
const auto userAgentParsed = Aws::Utils::StringUtils::Split(userAgent, ' ');
271-
272-
// Verify there's only one m/ section (no duplicate m/ sections)
273-
int mSectionCount = 0;
274-
for (const auto& part : userAgentParsed) {
275-
if (part.find("m/") != Aws::String::npos) {
276-
mSectionCount++;
277-
}
278-
}
279-
EXPECT_EQ(1, mSectionCount);
280-
281-
// Check for process credentials business metric (w) in user agent
282-
auto businessMetrics = std::find_if(userAgentParsed.begin(), userAgentParsed.end(),
283-
[](const Aws::String& value) { return value.find("m/") != Aws::String::npos && value.find("w") != Aws::String::npos; });
284-
285-
EXPECT_TRUE(businessMetrics != userAgentParsed.end());
200+
auto credsProvider = Aws::MakeShared<Aws::Auth::ProcessCredentialsProvider>(TEST_LOG_TAG);
201+
RunTestWithCredentialsProvider(std::move(credsProvider), "w");
286202
}
287203

288204
TEST_F(CredentialTrackingTest, TestInstanceProfileCredentialsTracking)
289205
{
290206
// Create mock EC2 metadata client with valid credentials
291-
auto mockClient = Aws::MakeShared<MockEC2MetadataClient>(ALLOCATION_TAG);
207+
auto mockClient = Aws::MakeShared<MockEC2MetadataClient>(TEST_LOG_TAG);
292208
const char* validCredentials = R"({ "AccessKeyId": "test-imds-access-key", "SecretAccessKey": "test-imds-secret-key", "Token": "test-imds-token", "Code": "Success", "Expiration": "2037-04-19T00:00:00Z" })";
293209
mockClient->SetMockedCredentialsValue(validCredentials);
294210

295211
// Create IMDS credential provider with mock client
296-
auto imdsProvider = Aws::MakeShared<InstanceProfileCredentialsProvider>(ALLOCATION_TAG,
297-
Aws::MakeShared<Aws::Config::EC2InstanceProfileConfigLoader>(ALLOCATION_TAG, mockClient), 1000 * 60 * 15);
298-
299-
// Setup mock response for service call
300-
std::shared_ptr<HttpRequest> requestTmp =
301-
CreateHttpRequest(Aws::Http::URI("dummy"), Aws::Http::HttpMethod::HTTP_POST,
302-
Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
303-
auto successResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, requestTmp);
304-
successResponse->SetResponseCode(HttpResponseCode::OK);
305-
successResponse->GetResponseBody() << "{}";
306-
mockHttpClient->AddResponseToReturn(successResponse);
307-
308-
// Create client configuration
309-
Aws::Client::ClientConfigurationInitValues cfgInit;
310-
cfgInit.shouldDisableIMDS = false;
311-
Aws::Client::ClientConfiguration clientConfig(cfgInit);
312-
clientConfig.region = Aws::Region::US_EAST_1;
313-
314-
// Create credential testing client with IMDS provider
315-
CredentialTestingClient client(clientConfig, imdsProvider);
316-
317-
// Create mock request
318-
AmazonWebServiceRequestMock mockRequest;
319-
320-
// Make request
321-
auto outcome = client.MakeRequest(mockRequest);
322-
ASSERT_TRUE(outcome.IsSuccess());
323-
324-
// Verify User-Agent contains IMDS credentials tracking
325-
auto lastRequest = mockHttpClient->GetMostRecentHttpRequest();
326-
EXPECT_TRUE(lastRequest.HasHeader(Aws::Http::USER_AGENT_HEADER));
327-
const auto& userAgent = lastRequest.GetHeaderValue(Aws::Http::USER_AGENT_HEADER);
328-
EXPECT_FALSE(userAgent.empty());
329-
330-
const auto userAgentParsed = Aws::Utils::StringUtils::Split(userAgent, ' ');
331-
332-
// Verify there's only one m/ section (no duplicate m/ sections)
333-
int mSectionCount = 0;
334-
for (const auto& part : userAgentParsed) {
335-
if (part.find("m/") != Aws::String::npos) {
336-
mSectionCount++;
337-
}
338-
}
339-
EXPECT_EQ(1, mSectionCount);
340-
341-
// Check for IMDS credentials business metric (0) in user agent
342-
auto businessMetrics = std::find_if(userAgentParsed.begin(), userAgentParsed.end(),
343-
[](const Aws::String& value) { return value.find("m/") != Aws::String::npos && value.find("0") != Aws::String::npos; });
212+
auto imdsProvider = Aws::MakeShared<InstanceProfileCredentialsProvider>(TEST_LOG_TAG,
213+
Aws::MakeShared<Aws::Config::EC2InstanceProfileConfigLoader>(TEST_LOG_TAG, mockClient), 1000);
344214

345-
EXPECT_TRUE(businessMetrics != userAgentParsed.end());
215+
RunTestWithCredentialsProvider(std::move(imdsProvider), "0");
346216
}

0 commit comments

Comments
 (0)