|
| 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