Skip to content

Commit e451630

Browse files
committed
adding tranfermanager unit tests and updating error case for contentRange
1 parent feb6137 commit e451630

File tree

4 files changed

+162
-23
lines changed

4 files changed

+162
-23
lines changed

cmake/sdksCommon.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ list(APPEND SDK_TEST_PROJECT_LIST "s3control:tests/aws-cpp-sdk-s3control-integra
112112
list(APPEND SDK_TEST_PROJECT_LIST "sns:tests/aws-cpp-sdk-sns-integration-tests")
113113
list(APPEND SDK_TEST_PROJECT_LIST "sqs:tests/aws-cpp-sdk-sqs-integration-tests")
114114
list(APPEND SDK_TEST_PROJECT_LIST "sqs:tests/aws-cpp-sdk-sqs-unit-tests")
115-
list(APPEND SDK_TEST_PROJECT_LIST "transfer:tests/aws-cpp-sdk-transfer-tests")
115+
list(APPEND SDK_TEST_PROJECT_LIST "transfer:tests/aws-cpp-sdk-transfer-tests,tests/aws-cpp-sdk-transfer-unit-tests")
116116
list(APPEND SDK_TEST_PROJECT_LIST "text-to-speech:tests/aws-cpp-sdk-text-to-speech-tests,tests/aws-cpp-sdk-polly-sample")
117117
list(APPEND SDK_TEST_PROJECT_LIST "timestream-query:tests/aws-cpp-sdk-timestream-query-unit-tests")
118118
list(APPEND SDK_TEST_PROJECT_LIST "transcribestreaming:tests/aws-cpp-sdk-transcribestreaming-integ-tests")

src/aws-cpp-sdk-transfer/source/transfer/TransferManager.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,30 +1140,26 @@ namespace Aws
11401140
{
11411141
const auto& requestedRange = request.GetRange();
11421142
const auto& responseContentRange = outcome.GetResult().GetContentRange();
1143-
1144-
if (!responseContentRange.empty())
1145-
{
1146-
if (!VerifyContentRange(requestedRange, responseContentRange))
1143+
1144+
if (responseContentRange.empty() or !VerifyContentRange(requestedRange, responseContentRange)) {
1145+
Aws::Client::AWSError<Aws::S3::S3Errors> error(Aws::S3::S3Errors::INTERNAL_FAILURE,
1146+
"ContentRangeMismatch",
1147+
"ContentRange in response does not match requested range",
1148+
false);
1149+
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Transfer handle [" << handle->GetId()
1150+
<< "] ContentRange mismatch. Requested: [" << requestedRange
1151+
<< "] Received: [" << responseContentRange << "]");
1152+
handle->ChangePartToFailed(partState);
1153+
handle->SetError(error);
1154+
TriggerErrorCallback(handle, error);
1155+
handle->Cancel();
1156+
1157+
if(partState->GetDownloadBuffer())
11471158
{
1148-
Aws::Client::AWSError<Aws::S3::S3Errors> error(Aws::S3::S3Errors::INTERNAL_FAILURE,
1149-
"ContentRangeMismatch",
1150-
"ContentRange in response does not match requested range",
1151-
false);
1152-
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Transfer handle [" << handle->GetId()
1153-
<< "] ContentRange mismatch. Requested: [" << requestedRange
1154-
<< "] Received: [" << responseContentRange << "]");
1155-
handle->ChangePartToFailed(partState);
1156-
handle->SetError(error);
1157-
TriggerErrorCallback(handle, error);
1158-
handle->Cancel();
1159-
1160-
if(partState->GetDownloadBuffer())
1161-
{
1162-
m_bufferManager.Release(partState->GetDownloadBuffer());
1163-
partState->SetDownloadBuffer(nullptr);
1164-
}
1165-
return;
1159+
m_bufferManager.Release(partState->GetDownloadBuffer());
1160+
partState->SetDownloadBuffer(nullptr);
11661161
}
1162+
return;
11671163
}
11681164
}
11691165

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
add_project(aws-cpp-sdk-transfer-unit-tests
2+
"Unit Tests for the Transfer Manager"
3+
aws-cpp-sdk-transfer
4+
testing-resources
5+
aws_test_main
6+
aws-cpp-sdk-core)
7+
8+
add_definitions(-DRESOURCES_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources")
9+
10+
if(MSVC AND BUILD_SHARED_LIBS)
11+
add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1)
12+
endif()
13+
14+
enable_testing()
15+
16+
if(PLATFORM_ANDROID AND BUILD_SHARED_LIBS)
17+
add_library(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/TransferUnitTests.cpp)
18+
else()
19+
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/TransferUnitTests.cpp)
20+
endif()
21+
22+
set_compiler_flags(${PROJECT_NAME})
23+
set_compiler_warnings(${PROJECT_NAME})
24+
25+
target_link_libraries(${PROJECT_NAME} ${PROJECT_LIBS})
26+
27+
if(MSVC AND BUILD_SHARED_LIBS)
28+
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/DELAYLOAD:aws-cpp-sdk-transfer.dll /DELAYLOAD:aws-cpp-sdk-core.dll")
29+
target_link_libraries(${PROJECT_NAME} delayimp.lib)
30+
endif()
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <gtest/gtest.h>
2+
#include <aws/core/Aws.h>
3+
#include <aws/testing/AwsTestHelpers.h>
4+
#include <aws/testing/MemoryTesting.h>
5+
6+
using namespace Aws;
7+
8+
const char* ALLOCATION_TAG = "TransferUnitTest";
9+
10+
// Copy the VerifyContentRange function for testing
11+
// In production, this would be exposed in a header or made testable
12+
static bool VerifyContentRange(const Aws::String& requestedRange, const Aws::String& responseContentRange)
13+
{
14+
if (requestedRange.empty() || responseContentRange.empty())
15+
{
16+
return false;
17+
}
18+
19+
if (requestedRange.find("bytes=") != 0)
20+
{
21+
return false;
22+
}
23+
Aws::String requestRange = requestedRange.substr(6);
24+
25+
if (responseContentRange.find("bytes ") != 0)
26+
{
27+
return false;
28+
}
29+
Aws::String responseRange = responseContentRange.substr(6);
30+
size_t slashPos = responseRange.find('/');
31+
if (slashPos != Aws::String::npos)
32+
{
33+
responseRange = responseRange.substr(0, slashPos);
34+
}
35+
36+
return requestRange == responseRange;
37+
}
38+
39+
class TransferUnitTest : public testing::Test {
40+
protected:
41+
static void SetUpTestSuite() {
42+
#ifdef USE_AWS_MEMORY_MANAGEMENT
43+
_testMemorySystem.reset(new ExactTestMemorySystem(1024, 128));
44+
_options.memoryManagementOptions.memoryManager = _testMemorySystem.get();
45+
#endif
46+
InitAPI(_options);
47+
}
48+
49+
static void TearDownTestSuite() {
50+
ShutdownAPI(_options);
51+
#ifdef USE_AWS_MEMORY_MANAGEMENT
52+
EXPECT_EQ(_testMemorySystem->GetCurrentOutstandingAllocations(), 0ULL);
53+
EXPECT_EQ(_testMemorySystem->GetCurrentBytesAllocated(), 0ULL);
54+
EXPECT_TRUE(_testMemorySystem->IsClean());
55+
if (_testMemorySystem->GetCurrentOutstandingAllocations() != 0ULL)
56+
FAIL();
57+
if (_testMemorySystem->GetCurrentBytesAllocated() != 0ULL)
58+
FAIL();
59+
if (!_testMemorySystem->IsClean())
60+
FAIL();
61+
_testMemorySystem.reset();
62+
#endif
63+
}
64+
65+
static SDKOptions _options;
66+
#ifdef USE_AWS_MEMORY_MANAGEMENT
67+
static std::unique_ptr<ExactTestMemorySystem> _testMemorySystem;
68+
#endif
69+
};
70+
71+
SDKOptions TransferUnitTest::_options;
72+
#ifdef USE_AWS_MEMORY_MANAGEMENT
73+
std::unique_ptr<ExactTestMemorySystem> TransferUnitTest::_testMemorySystem = nullptr;
74+
#endif
75+
76+
TEST_F(TransferUnitTest, VerifyContentRange_ValidRanges) {
77+
// Test matching ranges
78+
EXPECT_TRUE(VerifyContentRange("bytes=0-1023", "bytes 0-1023/2048"));
79+
EXPECT_TRUE(VerifyContentRange("bytes=1024-2047", "bytes 1024-2047/2048"));
80+
EXPECT_TRUE(VerifyContentRange("bytes=0-499", "bytes 0-499/500"));
81+
82+
// Test without total size in response
83+
EXPECT_TRUE(VerifyContentRange("bytes=0-1023", "bytes 0-1023"));
84+
}
85+
86+
TEST_F(TransferUnitTest, VerifyContentRange_InvalidRanges) {
87+
// Test mismatched ranges - this is what @kai-ion wanted to test!
88+
EXPECT_FALSE(VerifyContentRange("bytes=0-1023", "bytes 0-1022/2048"));
89+
EXPECT_FALSE(VerifyContentRange("bytes=0-1023", "bytes 1024-2047/2048"));
90+
EXPECT_FALSE(VerifyContentRange("bytes=1024-2047", "bytes 0-1023/2048"));
91+
92+
// Test empty inputs
93+
EXPECT_FALSE(VerifyContentRange("", "bytes 0-1023/2048"));
94+
EXPECT_FALSE(VerifyContentRange("bytes=0-1023", ""));
95+
EXPECT_FALSE(VerifyContentRange("", ""));
96+
97+
// Test invalid format
98+
EXPECT_FALSE(VerifyContentRange("0-1023", "bytes 0-1023/2048"));
99+
EXPECT_FALSE(VerifyContentRange("bytes=0-1023", "0-1023/2048"));
100+
EXPECT_FALSE(VerifyContentRange("range=0-1023", "bytes 0-1023/2048"));
101+
}
102+
103+
TEST_F(TransferUnitTest, VerifyContentRange_EdgeCases) {
104+
// Test single byte range
105+
EXPECT_TRUE(VerifyContentRange("bytes=0-0", "bytes 0-0/1"));
106+
107+
// Test large ranges
108+
EXPECT_TRUE(VerifyContentRange("bytes=0-1073741823", "bytes 0-1073741823/1073741824"));
109+
110+
// Test ranges with different total sizes (should still match the range part)
111+
EXPECT_TRUE(VerifyContentRange("bytes=0-1023", "bytes 0-1023/5000"));
112+
EXPECT_TRUE(VerifyContentRange("bytes=0-1023", "bytes 0-1023/1024"));
113+
}

0 commit comments

Comments
 (0)