Skip to content

Commit 1699cc9

Browse files
committed
using existing tempFile object
1 parent 5fd263f commit 1699cc9

File tree

1 file changed

+10
-57
lines changed

1 file changed

+10
-57
lines changed

tests/aws-cpp-sdk-transfer-unit-tests/TransferUnitTests.cpp

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
#ifdef _WIN32
2-
#ifndef NOMINMAX
3-
#define NOMINMAX
4-
#endif
5-
#include <windows.h>
6-
#endif
7-
81
#include <gtest/gtest.h>
92
#include <aws/core/Aws.h>
103
#include <aws/core/utils/threading/PooledThreadExecutor.h>
@@ -18,6 +11,7 @@
1811
#include <aws/testing/MemoryTesting.h>
1912
#include <sstream>
2013
#include <fstream>
14+
#include <aws/core/utils/FileSystemUtils.h>
2115

2216
using namespace Aws;
2317
using namespace Aws::S3;
@@ -35,7 +29,6 @@ class MockS3Client : public S3Client {
3529
GetObjectResult result;
3630

3731
if (request.RangeHasBeenSet()) {
38-
// Always return mismatched range to trigger validation failure
3932
result.SetContentRange("bytes 1024-2047/2048");
4033
}
4134

@@ -48,7 +41,7 @@ class MockS3Client : public S3Client {
4841

4942
class MockMultipartS3Client : public S3Client {
5043
public:
51-
Aws::String FULL_OBJECT_CHECKSUM; //"SBi/K+1ooBg="
44+
Aws::String FULL_OBJECT_CHECKSUM;
5245
MockMultipartS3Client(Aws::String expected_checksum) : S3Client() {
5346
FULL_OBJECT_CHECKSUM = expected_checksum;
5447
};
@@ -57,8 +50,8 @@ class MockMultipartS3Client : public S3Client {
5750
HeadObjectResult result;
5851
result.SetContentLength(78643200);
5952
result.SetChecksumCRC64NVME(FULL_OBJECT_CHECKSUM);
60-
result.SetChecksumType(Aws::S3::Model::ChecksumType::FULL_OBJECT); // This is key!
61-
result.SetETag("\"test-etag-12345\""); // Add ETag
53+
result.SetChecksumType(Aws::S3::Model::ChecksumType::FULL_OBJECT);
54+
result.SetETag("\"test-etag-12345\"");
6255
return HeadObjectOutcome(std::move(result));
6356
}
6457

@@ -87,25 +80,21 @@ class MockMultipartS3Client : public S3Client {
8780
result.SetContentLength(size);
8881
result.SetETag(Aws::String("\"part-etag-") + Aws::String(std::to_string(partNum).c_str()) + "\"");
8982

90-
// Call the response stream factory if provided
9183
if (request.GetResponseStreamFactory()) {
9284
auto responseStream = request.GetResponseStreamFactory()();
9385

94-
// Write part-specific data to the response stream
9586
char partChar = 'A' + (partNum % 3);
9687
for (uint64_t i = 0; i < size; ++i) {
9788
responseStream->put(partChar);
9889
}
9990
responseStream->flush();
10091

101-
// Simulate data received callback to track bytes transferred
10292
if (request.GetDataReceivedEventHandler()) {
10393
request.GetDataReceivedEventHandler()(nullptr, nullptr, size);
10494
}
10595

10696
result.ReplaceBody(responseStream);
10797
} else {
108-
// Fallback for non-factory requests
10998
auto stream = Aws::New<std::stringstream>(ALLOCATION_TAG);
11099
char partChar = 'A' + (partNum % 3);
111100
for (uint64_t i = 0; i < size; ++i) {
@@ -167,35 +156,17 @@ TEST_F(TransferUnitTest, MultipartDownloadTest) {
167156
config.bufferSize = 5242880; // 5MB to ensure multipart
168157
auto transferManager = TransferManager::Create(config);
169158

170-
// Create a temporary file for download since multipart needs seekable stream
171-
std::string tempFile;
172-
#ifdef _WIN32
173-
char tempPath[MAX_PATH];
174-
GetTempPathA(MAX_PATH, tempPath);
175-
tempFile = std::string(tempPath) + "test_download_" + std::to_string(rand());
176-
#else
177-
tempFile = "/tmp/test_download_" + std::to_string(rand());
178-
#endif
179-
auto createStreamFn = [tempFile]() -> Aws::IOStream* {
180-
return Aws::New<Aws::FStream>(ALLOCATION_TAG, tempFile.c_str(),
181-
std::ios_base::out | std::ios_base::in |
182-
std::ios_base::binary | std::ios_base::trunc);
183-
};
159+
Utils::TempFile tempFile(std::ios_base::out | std::ios_base::in | std::ios_base::binary | std::ios_base::trunc);
184160

185-
// Download the full 78MB file
186-
auto handle = transferManager->DownloadFile("test-bucket", "test-key", createStreamFn);
161+
auto handle = transferManager->DownloadFile("test-bucket", "test-key", tempFile.GetFileName());
187162
handle->WaitUntilFinished();
188163

189-
// Test multipart download functionality - should PASS with correct checksum
190164
EXPECT_TRUE(handle->IsMultipart());
191165
EXPECT_EQ(78643200u, handle->GetBytesTotalSize());
192166
EXPECT_EQ(15u, handle->GetCompletedParts().size());
193167
EXPECT_EQ(0u, handle->GetFailedParts().size());
194168
EXPECT_EQ(0u, handle->GetPendingParts().size());
195-
EXPECT_EQ(TransferStatus::COMPLETED, handle->GetStatus()); // Should PASS
196-
197-
// Clean up
198-
std::remove(tempFile.c_str());
169+
EXPECT_EQ(TransferStatus::COMPLETED, handle->GetStatus());
199170
}
200171

201172
TEST_F(TransferUnitTest, MultipartDownloadTest_Fail) {
@@ -205,34 +176,16 @@ TEST_F(TransferUnitTest, MultipartDownloadTest_Fail) {
205176
config.bufferSize = 5242880; // 5MB to ensure multipart
206177
auto transferManager = TransferManager::Create(config);
207178

208-
// Create a temporary file for download since multipart needs seekable stream
209-
std::string tempFile;
210-
#ifdef _WIN32
211-
char tempPath[MAX_PATH];
212-
GetTempPathA(MAX_PATH, tempPath);
213-
tempFile = std::string(tempPath) + "test_download_" + std::to_string(rand());
214-
#else
215-
tempFile = "/tmp/test_download_" + std::to_string(rand());
216-
#endif
217-
auto createStreamFn = [tempFile]() -> Aws::IOStream* {
218-
return Aws::New<Aws::FStream>(ALLOCATION_TAG, tempFile.c_str(),
219-
std::ios_base::out | std::ios_base::in |
220-
std::ios_base::binary | std::ios_base::trunc);
221-
};
179+
Utils::TempFile tempFile{std::ios_base::out | std::ios_base::in | std::ios_base::binary | std::ios_base::trunc};
222180

223-
// Download the full 78MB file
224-
auto handle = transferManager->DownloadFile("test-bucket", "test-key", createStreamFn);
181+
auto handle = transferManager->DownloadFile("test-bucket", "test-key", tempFile.GetFileName());
225182
handle->WaitUntilFinished();
226183

227-
// Test multipart download functionality - should FAIL with wrong checksum
228184
EXPECT_TRUE(handle->IsMultipart());
229185
EXPECT_EQ(78643200u, handle->GetBytesTotalSize());
230186
EXPECT_EQ(15u, handle->GetCompletedParts().size());
231187
EXPECT_EQ(0u, handle->GetFailedParts().size());
232188
EXPECT_EQ(0u, handle->GetPendingParts().size());
233-
EXPECT_EQ(TransferStatus::FAILED, handle->GetStatus()); // Should FAIL due to wrong checksum
189+
EXPECT_EQ(TransferStatus::FAILED, handle->GetStatus());
234190
EXPECT_EQ("Full-object checksum validation failed", handle->GetLastError().GetMessage());
235-
236-
// Clean up
237-
std::remove(tempFile.c_str());
238191
}

0 commit comments

Comments
 (0)