|
20 | 20 | #include <aws/testing/AwsTestHelpers.h> |
21 | 21 | #include <aws/testing/MemoryTesting.h> |
22 | 22 | #include <aws/testing/mocks/http/MockHttpClient.h> |
| 23 | +#include <aws/testing/utils/UserAgentUtils.h> |
23 | 24 | #include <gtest/gtest.h> |
24 | 25 |
|
| 26 | +#include <functional> |
25 | 27 | #include <memory> |
26 | 28 |
|
27 | 29 | using namespace Aws; |
@@ -441,6 +443,71 @@ TEST_F(S3UnitTest, PutObjectShouldHaveCorrectUserAgent) { |
441 | 443 | EXPECT_TRUE(archMetadata < businessMetrics); |
442 | 444 | } |
443 | 445 |
|
| 446 | +namespace { |
| 447 | +struct ChecksumFeatureTestCase { |
| 448 | + ChecksumAlgorithm algorithm; |
| 449 | + std::function<void(PutObjectRequest&)> setChecksum; |
| 450 | + Aws::String metric; |
| 451 | +}; |
| 452 | +} // namespace |
| 453 | + |
| 454 | +TEST_F(S3UnitTest, PutObjectShouldTrackChecksumAlgorithmFeature) { |
| 455 | + // FLEXIBLE_CHECKSUMS_REQ_* metric values, from the AWS SDK business metrics registry. |
| 456 | + const Aws::Vector<ChecksumFeatureTestCase> testCases{ |
| 457 | + {ChecksumAlgorithm::CRC32, [](PutObjectRequest& r) { r.SetChecksumCRC32("Y2hlY2tzdW0="); }, "U"}, |
| 458 | + {ChecksumAlgorithm::CRC32C, [](PutObjectRequest& r) { r.SetChecksumCRC32C("Y2hlY2tzdW0="); }, "V"}, |
| 459 | + {ChecksumAlgorithm::CRC64NVME, [](PutObjectRequest& r) { r.SetChecksumCRC64NVME("Y2hlY2tzdW0="); }, "W"}, |
| 460 | + {ChecksumAlgorithm::SHA1, [](PutObjectRequest& r) { r.SetChecksumSHA1("Y2hlY2tzdW0="); }, "X"}, |
| 461 | + {ChecksumAlgorithm::SHA256, [](PutObjectRequest& r) { r.SetChecksumSHA256("Y2hlY2tzdW0="); }, "Y"}, |
| 462 | + {ChecksumAlgorithm::SHA512, [](PutObjectRequest& r) { r.SetChecksumSHA512("Y2hlY2tzdW0="); }, "AF"}, |
| 463 | + {ChecksumAlgorithm::XXHASH3, [](PutObjectRequest& r) { r.SetChecksumXXHASH3("Y2hlY2tzdW0="); }, "AG"}, |
| 464 | + {ChecksumAlgorithm::XXHASH64, [](PutObjectRequest& r) { r.SetChecksumXXHASH64("Y2hlY2tzdW0="); }, "AH"}, |
| 465 | + {ChecksumAlgorithm::XXHASH128, [](PutObjectRequest& r) { r.SetChecksumXXHASH128("Y2hlY2tzdW0="); }, "AI"}, |
| 466 | + }; |
| 467 | + Aws::Vector<Aws::String> allMetrics; |
| 468 | + for (const auto& testCase : testCases) { |
| 469 | + allMetrics.push_back(testCase.metric); |
| 470 | + } |
| 471 | + |
| 472 | + for (const auto& testCase : testCases) { |
| 473 | + // The SDK either calculates the checksum, or sends the one set on the request. |
| 474 | + for (const bool provided : {false, true}) { |
| 475 | + SCOPED_TRACE(Aws::String(ChecksumAlgorithmMapper::GetNameForChecksumAlgorithm(testCase.algorithm)) + |
| 476 | + (provided ? " provided" : " calculated")); |
| 477 | + // The mock client is shared by the suite, so start each case clean. |
| 478 | + _mockHttpClient->Reset(); |
| 479 | + auto request = PutObjectRequest() |
| 480 | + .WithBucket("big-boss") |
| 481 | + .WithKey("snake-eater") |
| 482 | + .WithChecksumAlgorithm(testCase.algorithm); |
| 483 | + if (provided) { |
| 484 | + testCase.setChecksum(request); |
| 485 | + } |
| 486 | + std::shared_ptr<IOStream> body = Aws::MakeShared<StringStream>(ALLOCATION_TAG, "phantom pain"); |
| 487 | + request.SetBody(body); |
| 488 | + |
| 489 | + auto mockRequest = Aws::MakeShared<Standard::StandardHttpRequest>(ALLOCATION_TAG, "outer.heaven/mother-base", HttpMethod::HTTP_PUT); |
| 490 | + mockRequest->SetResponseStreamFactory([]() -> IOStream* { |
| 491 | + return Aws::New<StringStream>(ALLOCATION_TAG, "", std::ios_base::in | std::ios_base::binary); |
| 492 | + }); |
| 493 | + auto mockResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, mockRequest); |
| 494 | + mockResponse->SetResponseCode(HttpResponseCode::OK); |
| 495 | + _mockHttpClient->AddResponseToReturn(mockResponse); |
| 496 | + |
| 497 | + const auto response = _s3Client->PutObject(request); |
| 498 | + EXPECT_TRUE(response.IsSuccess()); |
| 499 | + ASSERT_EQ(1u, _mockHttpClient->GetAllRequestsMade().size()); |
| 500 | + |
| 501 | + // Exactly one checksum algorithm metric is reported, and it's the right one. |
| 502 | + const auto metrics = GetFeaturesForRequest(_mockHttpClient->GetMostRecentHttpRequest()); |
| 503 | + for (const auto& metric : allMetrics) { |
| 504 | + const bool reported = std::find(metrics.begin(), metrics.end(), metric) != metrics.end(); |
| 505 | + EXPECT_EQ(metric == testCase.metric, reported) << "metric " << metric; |
| 506 | + } |
| 507 | + } |
| 508 | + } |
| 509 | +} |
| 510 | + |
444 | 511 | TEST_F(S3UnitTest, PutObjectShouldOverrideUserAgent) { |
445 | 512 | auto request = PutObjectRequest() |
446 | 513 | .WithBucket("ocelot") |
|
0 commit comments