|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <aws/core/Aws.h> |
| 3 | +#include <aws/core/auth/AWSCredentials.h> |
| 4 | +#include <aws/s3/S3Client.h> |
| 5 | +#include <aws/s3/model/PutObjectRequest.h> |
| 6 | +#include <aws/testing/mocks/http/MockHttpClient.h> |
| 7 | +#include <aws/testing/AwsTestHelpers.h> |
| 8 | +#include <aws/testing/MemoryTesting.h> |
| 9 | + |
| 10 | +using namespace Aws; |
| 11 | +using namespace Aws::Auth; |
| 12 | +using namespace Aws::Http; |
| 13 | +using namespace Aws::S3; |
| 14 | +using namespace Aws::S3::Model; |
| 15 | + |
| 16 | +const char* ALLOCATION_TAG = "S3UnitTest"; |
| 17 | + |
| 18 | +class S3UnitTest : public testing::Test { |
| 19 | +protected: |
| 20 | + static void SetUpTestSuite() { |
| 21 | +#ifdef USE_AWS_MEMORY_MANAGEMENT |
| 22 | + _testMemorySystem = Aws::MakeShared<ExactTestMemorySystem>(ALLOCATION_TAG, 1024, 128); |
| 23 | + _options.memoryManagementOptions.memoryManager = _testMemorySystem.get(); |
| 24 | +#endif |
| 25 | + _mockClientFactory = Aws::MakeShared<MockHttpClientFactory>(ALLOCATION_TAG); |
| 26 | + _mockHttpClient = Aws::MakeShared<MockHttpClient>(ALLOCATION_TAG); |
| 27 | + _mockClientFactory->SetClient(_mockHttpClient); |
| 28 | + HttpOptions httpOptions; |
| 29 | + httpOptions.httpClientFactory_create_fn = []() -> std::shared_ptr<HttpClientFactory> { |
| 30 | + return _mockClientFactory; |
| 31 | + }; |
| 32 | + _options.httpOptions = httpOptions; |
| 33 | + InitAPI(_options); |
| 34 | + AWSCredentials credentials{"mock", "credentials"}; |
| 35 | + const auto epProvider = Aws::MakeShared<S3EndpointProvider>(ALLOCATION_TAG); |
| 36 | + S3ClientConfiguration s3Config; |
| 37 | + s3Config.region = "us-east-1"; |
| 38 | + _s3Client = Aws::MakeShared<S3Client>("ALLOCATION_TAG", credentials, epProvider, s3Config); |
| 39 | + } |
| 40 | + |
| 41 | + static void TearDownTestSuite() { |
| 42 | + _mockClientFactory.reset(); |
| 43 | + _mockHttpClient.reset(); |
| 44 | + _s3Client.reset(); |
| 45 | + ShutdownAPI(_options); |
| 46 | +#ifdef USE_AWS_MEMORY_MANAGEMENT |
| 47 | + EXPECT_EQ(_testMemorySystem->GetCurrentOutstandingAllocations(), 0ULL); |
| 48 | + EXPECT_EQ(_testMemorySystem->GetCurrentBytesAllocated(), 0ULL); |
| 49 | + EXPECT_TRUE(_testMemorySystem->IsClean()); |
| 50 | + if (_testMemorySystem->GetCurrentOutstandingAllocations() != 0ULL) |
| 51 | + FAIL(); |
| 52 | + if (_testMemorySystem->GetCurrentBytesAllocated() != 0ULL) |
| 53 | + FAIL(); |
| 54 | + if (!_testMemorySystem->IsClean()) |
| 55 | + FAIL(); |
| 56 | + _testMemorySystem.reset(); |
| 57 | +#endif |
| 58 | + } |
| 59 | + |
| 60 | + static SDKOptions _options; |
| 61 | + static std::shared_ptr<MockHttpClient> _mockHttpClient; |
| 62 | + static std::shared_ptr<MockHttpClientFactory> _mockClientFactory; |
| 63 | + static std::shared_ptr<S3Client> _s3Client; |
| 64 | +#ifdef USE_AWS_MEMORY_MANAGEMENT |
| 65 | + static std::shared_ptr<ExactTestMemorySystem> _testMemorySystem; |
| 66 | +#endif |
| 67 | +}; |
| 68 | + |
| 69 | +SDKOptions S3UnitTest::_options; |
| 70 | +std::shared_ptr<MockHttpClient> S3UnitTest::_mockHttpClient = nullptr; |
| 71 | +std::shared_ptr<MockHttpClientFactory> S3UnitTest::_mockClientFactory = nullptr; |
| 72 | +std::shared_ptr<S3Client> S3UnitTest::_s3Client = nullptr; |
| 73 | +#ifdef USE_AWS_MEMORY_MANAGEMENT |
| 74 | +std::shared_ptr<ExactTestMemorySystem> S3UnitTest::_testMemorySystem = nullptr; |
| 75 | +#endif |
| 76 | + |
| 77 | + |
| 78 | +TEST_F(S3UnitTest, S3UriLeadingDots) { |
| 79 | + auto putObjectRequest = PutObjectRequest() |
| 80 | + .WithBucket("bluerev") |
| 81 | + .WithKey("../BoredInBristol"); |
| 82 | + |
| 83 | + std::shared_ptr<IOStream> body = Aws::MakeShared<StringStream>(ALLOCATION_TAG, |
| 84 | + "Bored in Bristol, always waiting", |
| 85 | + std::ios_base::in | std::ios_base::binary); |
| 86 | + |
| 87 | + putObjectRequest.SetBody(body); |
| 88 | + |
| 89 | + //We have to mock requset because it is used to create the return body, it actually isnt used. |
| 90 | + auto mockRequest = Aws::MakeShared<Standard::StandardHttpRequest>(ALLOCATION_TAG, "mockuri", HttpMethod::HTTP_GET); |
| 91 | + mockRequest->SetResponseStreamFactory([]() -> IOStream* { |
| 92 | + return Aws::New<StringStream>(ALLOCATION_TAG, "response-string", std::ios_base::in | std::ios_base::binary); |
| 93 | + }); |
| 94 | + auto mockResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, mockRequest); |
| 95 | + mockResponse->SetResponseCode(HttpResponseCode::OK); |
| 96 | + _mockHttpClient->AddResponseToReturn(mockResponse); |
| 97 | + const auto response = _s3Client->PutObject(putObjectRequest); |
| 98 | + AWS_EXPECT_SUCCESS(response); |
| 99 | + const auto seenRequest = _mockHttpClient->GetMostRecentHttpRequest(); |
| 100 | + EXPECT_EQ("https://bluerev.s3.us-east-1.amazonaws.com/../BoredInBristol", seenRequest.GetUri().GetURIString()); |
| 101 | +} |
| 102 | + |
| 103 | +TEST_F(S3UnitTest, S3UriMiddleDots) { |
| 104 | + auto putObjectRequest = PutObjectRequest() |
| 105 | + .WithBucket("bluerev") |
| 106 | + .WithKey("belinda/../says"); |
| 107 | + |
| 108 | + std::shared_ptr<IOStream> body = Aws::MakeShared<StringStream>(ALLOCATION_TAG, |
| 109 | + "Blue Rev behind the rink I didn't really need it", |
| 110 | + std::ios_base::in | std::ios_base::binary); |
| 111 | + |
| 112 | + putObjectRequest.SetBody(body); |
| 113 | + |
| 114 | + //We have to mock requset because it is used to create the return body, it actually isnt used. |
| 115 | + auto mockRequest = Aws::MakeShared<Standard::StandardHttpRequest>(ALLOCATION_TAG, "mockuri", HttpMethod::HTTP_GET); |
| 116 | + mockRequest->SetResponseStreamFactory([]() -> IOStream* { |
| 117 | + return Aws::New<StringStream>(ALLOCATION_TAG, "response-string", std::ios_base::in | std::ios_base::binary); |
| 118 | + }); |
| 119 | + auto mockResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, mockRequest); |
| 120 | + mockResponse->SetResponseCode(HttpResponseCode::OK); |
| 121 | + _mockHttpClient->AddResponseToReturn(mockResponse); |
| 122 | + const auto response = _s3Client->PutObject(putObjectRequest); |
| 123 | + AWS_EXPECT_SUCCESS(response); |
| 124 | + const auto seenRequest = _mockHttpClient->GetMostRecentHttpRequest(); |
| 125 | + EXPECT_EQ("https://bluerev.s3.us-east-1.amazonaws.com/belinda/../says", seenRequest.GetUri().GetURIString()); |
| 126 | +} |
0 commit comments