-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
add_project(aws-cpp-sdk-s3-unit-tests | ||
"Unit Tests for the S3 SDK Client" | ||
aws-cpp-sdk-s3 | ||
testing-resources | ||
aws_test_main | ||
aws-cpp-sdk-core) | ||
|
||
add_definitions(-DRESOURCES_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources") | ||
|
||
if(MSVC AND BUILD_SHARED_LIBS) | ||
add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1) | ||
endif() | ||
|
||
enable_testing() | ||
|
||
if(PLATFORM_ANDROID AND BUILD_SHARED_LIBS) | ||
add_library(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/S3UnitTests.cpp) | ||
else() | ||
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/S3UnitTests.cpp) | ||
endif() | ||
|
||
set_compiler_flags(${PROJECT_NAME}) | ||
set_compiler_warnings(${PROJECT_NAME}) | ||
|
||
target_link_libraries(${PROJECT_NAME} ${PROJECT_LIBS}) | ||
|
||
if(MSVC AND BUILD_SHARED_LIBS) | ||
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/DELAYLOAD:aws-cpp-sdk-s3.dll /DELAYLOAD:aws-cpp-sdk-core.dll") | ||
endif() | ||
|
||
include(GoogleTest) | ||
gtest_discover_tests(${PROJECT_NAME}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <gtest/gtest.h> | ||
#include <aws/core/Aws.h> | ||
#include <aws/core/auth/AWSCredentials.h> | ||
#include <aws/s3/S3Client.h> | ||
#include <aws/s3/model/PutObjectRequest.h> | ||
#include <aws/testing/mocks/http/MockHttpClient.h> | ||
#include <aws/testing/AwsTestHelpers.h> | ||
|
||
using namespace Aws; | ||
using namespace Aws::Auth; | ||
using namespace Aws::Http; | ||
using namespace Aws::S3; | ||
using namespace Aws::S3::Model; | ||
|
||
const char* ALLOCATION_TAG = "S3UnitTest"; | ||
|
||
class S3UnitTest : public testing::Test { | ||
protected: | ||
void SetUp() override { | ||
_mockClientFactory = Aws::MakeShared<MockHttpClientFactory>(ALLOCATION_TAG); | ||
_mockHttpClient = Aws::MakeShared<MockHttpClient>(ALLOCATION_TAG); | ||
_mockClientFactory->SetClient(_mockHttpClient); | ||
HttpOptions httpOptions; | ||
httpOptions.httpClientFactory_create_fn = [this]() -> std::shared_ptr<HttpClientFactory> { | ||
return _mockClientFactory; | ||
}; | ||
_options.httpOptions = httpOptions; | ||
InitAPI(_options); | ||
AWSCredentials credentials{"mock", "credentials"}; | ||
_s3Client = Aws::MakeUnique<S3Client>("ALLOCATION_TAG", credentials); | ||
} | ||
|
||
void TearDown() override { | ||
ShutdownAPI(_options); | ||
} | ||
|
||
SDKOptions _options; | ||
std::shared_ptr<MockHttpClient> _mockHttpClient; | ||
std::shared_ptr<MockHttpClientFactory> _mockClientFactory; | ||
UniquePtr<S3Client> _s3Client; | ||
}; | ||
|
||
|
||
TEST_F(S3UnitTest, S3UriLeadingDots) { | ||
auto putObjectRequest = PutObjectRequest() | ||
.WithBucket("bluerev") | ||
.WithKey("../BoredInBristol"); | ||
|
||
std::shared_ptr<IOStream> body = Aws::MakeShared<StringStream>(ALLOCATION_TAG, | ||
"Bored in Bristol, always waiting", | ||
std::ios_base::in | std::ios_base::binary); | ||
|
||
putObjectRequest.SetBody(body); | ||
|
||
//We have to mock requset because it is used to create the return body, it actually isnt used. | ||
auto mockRequest = Aws::MakeShared<Standard::StandardHttpRequest>(ALLOCATION_TAG, "mockuri", HttpMethod::HTTP_GET); | ||
mockRequest->SetResponseStreamFactory([]() -> IOStream* { | ||
IOStream* stream = Aws::New<StringStream>(ALLOCATION_TAG, "response-string", std::ios_base::in | std::ios_base::binary); | ||
return stream; | ||
}); | ||
auto mockResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, mockRequest); | ||
mockResponse->SetResponseCode(HttpResponseCode::OK); | ||
_mockHttpClient->AddResponseToReturn(mockResponse); | ||
const auto response = _s3Client->PutObject(putObjectRequest); | ||
AWS_EXPECT_SUCCESS(response); | ||
const auto seenRequest = _mockHttpClient->GetMostRecentHttpRequest(); | ||
EXPECT_EQ("https://bluerev.s3.us-east-1.amazonaws.com/../BoredInBristol", seenRequest.GetUri().GetURIString()); | ||
} | ||
|
||
TEST_F(S3UnitTest, S3UriMiddleDots) { | ||
auto putObjectRequest = PutObjectRequest() | ||
.WithBucket("bluerev") | ||
.WithKey("belinda/../says"); | ||
|
||
std::shared_ptr<IOStream> body = Aws::MakeShared<StringStream>(ALLOCATION_TAG, | ||
"Blue Rev behind the rink I didn't really need it", | ||
std::ios_base::in | std::ios_base::binary); | ||
|
||
putObjectRequest.SetBody(body); | ||
|
||
//We have to mock requset because it is used to create the return body, it actually isnt used. | ||
auto mockRequest = Aws::MakeShared<Standard::StandardHttpRequest>(ALLOCATION_TAG, "mockuri", HttpMethod::HTTP_GET); | ||
mockRequest->SetResponseStreamFactory([]() -> IOStream* { | ||
return Aws::New<StringStream>(ALLOCATION_TAG, "response-string", std::ios_base::in | std::ios_base::binary); | ||
}); | ||
auto mockResponse = Aws::MakeShared<Standard::StandardHttpResponse>(ALLOCATION_TAG, mockRequest); | ||
mockResponse->SetResponseCode(HttpResponseCode::OK); | ||
_mockHttpClient->AddResponseToReturn(mockResponse); | ||
const auto response = _s3Client->PutObject(putObjectRequest); | ||
AWS_EXPECT_SUCCESS(response); | ||
const auto seenRequest = _mockHttpClient->GetMostRecentHttpRequest(); | ||
EXPECT_EQ("https://bluerev.s3.us-east-1.amazonaws.com/belinda/../says", seenRequest.GetUri().GetURIString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters