diff options
-rw-r--r-- | common/test_utils.h | 12 | ||||
-rw-r--r-- | common/utils_unittest.cc | 4 | ||||
-rw-r--r-- | payload_consumer/bzip_extent_writer_unittest.cc | 59 | ||||
-rw-r--r-- | payload_consumer/download_action_unittest.cc | 20 | ||||
-rw-r--r-- | payload_consumer/extent_writer_unittest.cc | 33 |
5 files changed, 52 insertions, 76 deletions
diff --git a/common/test_utils.h b/common/test_utils.h index b5151f26..7be027a0 100644 --- a/common/test_utils.h +++ b/common/test_utils.h @@ -162,13 +162,15 @@ class ScopedLoopbackDeviceBinder { class ScopedTempFile { public: - ScopedTempFile() { - EXPECT_TRUE(utils::MakeTempFile("update_engine_test_temp_file.XXXXXX", - &path_, - nullptr)); + ScopedTempFile() : ScopedTempFile("update_engine_test_temp_file.XXXXXX") {} + + explicit ScopedTempFile(const std::string& pattern) { + EXPECT_TRUE(utils::MakeTempFile(pattern, &path_, nullptr)); unlinker_.reset(new ScopedPathUnlinker(path_)); } - const std::string& GetPath() { return path_; } + + const std::string& path() { return path_; } + private: std::string path_; std::unique_ptr<ScopedPathUnlinker> unlinker_; diff --git a/common/utils_unittest.cc b/common/utils_unittest.cc index 24468b91..f840a75b 100644 --- a/common/utils_unittest.cc +++ b/common/utils_unittest.cc @@ -280,10 +280,10 @@ namespace { void GetFileFormatTester(const string& expected, const vector<uint8_t>& contents) { test_utils::ScopedTempFile file; - ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(), + ASSERT_TRUE(utils::WriteFile(file.path().c_str(), reinterpret_cast<const char*>(contents.data()), contents.size())); - EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath())); + EXPECT_EQ(expected, utils::GetFileFormat(file.path())); } } // namespace diff --git a/payload_consumer/bzip_extent_writer_unittest.cc b/payload_consumer/bzip_extent_writer_unittest.cc index a52a2865..0abf04e8 100644 --- a/payload_consumer/bzip_extent_writer_unittest.cc +++ b/payload_consumer/bzip_extent_writer_unittest.cc @@ -16,11 +16,6 @@ #include "update_engine/payload_consumer/bzip_extent_writer.h" -#include <fcntl.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> - #include <algorithm> #include <string> #include <vector> @@ -38,28 +33,23 @@ using std::vector; namespace chromeos_update_engine { namespace { -const char kPathTemplate[] = "./BzipExtentWriterTest-file.XXXXXX"; const uint32_t kBlockSize = 4096; } class BzipExtentWriterTest : public ::testing::Test { protected: void SetUp() override { - memcpy(path_, kPathTemplate, sizeof(kPathTemplate)); fd_.reset(new EintrSafeFileDescriptor); - int fd = mkstemp(path_); - ASSERT_TRUE(fd_->Open(path_, O_RDWR, 0600)); - close(fd); + ASSERT_TRUE(fd_->Open(temp_file_.path().c_str(), O_RDWR, 0600)); } void TearDown() override { fd_->Close(); - unlink(path_); } void WriteAlignedExtents(size_t chunk_size, size_t first_chunk_size); void TestZeroPad(bool aligned_size); FileDescriptorPtr fd_; - char path_[sizeof(kPathTemplate)]; + test_utils::ScopedTempFile temp_file_{"BzipExtentWriterTest-file.XXXXXX"}; }; TEST_F(BzipExtentWriterTest, SimpleTest) { @@ -85,39 +75,37 @@ TEST_F(BzipExtentWriterTest, SimpleTest) { EXPECT_TRUE(bzip_writer.End()); brillo::Blob buf; - EXPECT_TRUE(utils::ReadFile(path_, &buf)); + EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &buf)); EXPECT_EQ(strlen(test_uncompressed), buf.size()); EXPECT_EQ(string(buf.begin(), buf.end()), string(test_uncompressed)); } TEST_F(BzipExtentWriterTest, ChunkedTest) { - const brillo::Blob::size_type kDecompressedLength = 2048 * 1024; // 2 MiB - string decompressed_path; - ASSERT_TRUE(utils::MakeTempFile("BzipExtentWriterTest-decompressed-XXXXXX", - &decompressed_path, nullptr)); - string compressed_path; - ASSERT_TRUE(utils::MakeTempFile("BzipExtentWriterTest-compressed-XXXXXX", - &compressed_path, nullptr)); + // Generated with: + // yes "ABC" | head -c 819200 | bzip2 -9 | \ + // hexdump -v -e '" " 11/1 "0x%02x, " "\n"' + static const uint8_t kCompressedData[] = { + 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xbe, + 0x1c, 0xda, 0xee, 0x03, 0x1f, 0xff, 0xc4, 0x00, 0x00, 0x10, 0x38, + 0x00, 0x20, 0x00, 0x50, 0x66, 0x9a, 0x05, 0x28, 0x38, 0x00, 0x11, + 0x60, 0x00, 0x22, 0xd0, 0x00, 0x45, 0xc0, 0x00, 0x8b, 0xc5, 0xdc, + 0x91, 0x4e, 0x14, 0x24, 0x2f, 0x87, 0x36, 0xbb, 0x80}; + brillo::Blob compressed_data(std::begin(kCompressedData), + std::end(kCompressedData)); + + const brillo::Blob::size_type kDecompressedLength = 800 * 1024; // 800 KiB const size_t kChunkSize = 3; + brillo::Blob decompressed_data(kDecompressedLength); + for (size_t i = 0; i < decompressed_data.size(); ++i) + decompressed_data[i] = static_cast<uint8_t>("ABC\n"[i % 4]); + vector<Extent> extents; Extent extent; extent.set_start_block(0); - extent.set_num_blocks(kDecompressedLength / kBlockSize + 1); + extent.set_num_blocks((kDecompressedLength + kBlockSize - 1) / kBlockSize); extents.push_back(extent); - brillo::Blob decompressed_data(kDecompressedLength); - test_utils::FillWithData(&decompressed_data); - - EXPECT_TRUE(test_utils::WriteFileVector( - decompressed_path, decompressed_data)); - - EXPECT_EQ(0, test_utils::System( - string("cat ") + decompressed_path + "|bzip2>" + compressed_path)); - - brillo::Blob compressed_data; - EXPECT_TRUE(utils::ReadFile(compressed_path, &compressed_data)); - BzipExtentWriter bzip_writer( brillo::make_unique_ptr(new DirectExtentWriter())); EXPECT_TRUE(bzip_writer.Init(fd_, extents, kBlockSize)); @@ -134,12 +122,9 @@ TEST_F(BzipExtentWriterTest, ChunkedTest) { test_utils::ExpectVectorsEq(original_compressed_data, compressed_data); brillo::Blob output; - EXPECT_TRUE(utils::ReadFile(path_, &output)); + EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &output)); EXPECT_EQ(kDecompressedLength, output.size()); test_utils::ExpectVectorsEq(decompressed_data, output); - - unlink(decompressed_path.c_str()); - unlink(compressed_path.c_str()); } } // namespace chromeos_update_engine diff --git a/payload_consumer/download_action_unittest.cc b/payload_consumer/download_action_unittest.cc index d12b4ae5..4ffd35cb 100644 --- a/payload_consumer/download_action_unittest.cc +++ b/payload_consumer/download_action_unittest.cc @@ -135,9 +135,8 @@ void TestWithData(const brillo::Blob& data, // TODO(adlr): see if we need a different file for build bots ScopedTempFile output_temp_file; TestDirectFileWriter writer; - EXPECT_EQ(0, writer.Open(output_temp_file.GetPath().c_str(), - O_WRONLY | O_CREAT, - 0)); + EXPECT_EQ( + 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0)); writer.set_fail_write(fail_write); // We pull off the first byte from data and seek past it. @@ -183,7 +182,7 @@ void TestWithData(const brillo::Blob& data, expected_code = ErrorCode::kDownloadWriteError; DownloadActionTestProcessorDelegate delegate(expected_code); delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end()); - delegate.path_ = output_temp_file.GetPath(); + delegate.path_ = output_temp_file.path(); ActionProcessor processor; processor.set_delegate(&delegate); processor.EnqueueAction(&feeder_action); @@ -268,9 +267,7 @@ void TestTerminateEarly(bool use_download_delegate) { ScopedTempFile temp_file; { DirectFileWriter writer; - EXPECT_EQ(0, writer.Open(temp_file.GetPath().c_str(), - O_WRONLY | O_CREAT, - 0)); + EXPECT_EQ(0, writer.Open(temp_file.path().c_str(), O_WRONLY | O_CREAT, 0)); // takes ownership of passed in HttpFetcher ObjectFeederAction<InstallPlan> feeder_action; @@ -304,7 +301,7 @@ void TestTerminateEarly(bool use_download_delegate) { } // 1 or 0 chunks should have come through - const off_t resulting_file_size(utils::FileSize(temp_file.GetPath())); + const off_t resulting_file_size(utils::FileSize(temp_file.path())); EXPECT_GE(resulting_file_size, 0); if (resulting_file_size != 0) EXPECT_EQ(kMockHttpFetcherChunkSize, @@ -452,9 +449,8 @@ class P2PDownloadActionTest : public testing::Test { ScopedTempFile output_temp_file; TestDirectFileWriter writer; - EXPECT_EQ(0, writer.Open(output_temp_file.GetPath().c_str(), - O_WRONLY | O_CREAT, - 0)); + EXPECT_EQ( + 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0)); InstallPlan install_plan; install_plan.payload_size = data_.length(); install_plan.payload_hash = "1234hash"; @@ -475,7 +471,7 @@ class P2PDownloadActionTest : public testing::Test { DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess); delegate.expected_data_ = brillo::Blob(data_.begin() + start_at_offset_, data_.end()); - delegate.path_ = output_temp_file.GetPath(); + delegate.path_ = output_temp_file.path(); processor_.set_delegate(&delegate); processor_.EnqueueAction(&feeder_action); processor_.EnqueueAction(download_action_.get()); diff --git a/payload_consumer/extent_writer_unittest.cc b/payload_consumer/extent_writer_unittest.cc index 6884c0bb..4d6b4d6f 100644 --- a/payload_consumer/extent_writer_unittest.cc +++ b/payload_consumer/extent_writer_unittest.cc @@ -16,11 +16,6 @@ #include "update_engine/payload_consumer/extent_writer.h" -#include <fcntl.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> - #include <algorithm> #include <string> #include <vector> @@ -43,22 +38,17 @@ namespace chromeos_update_engine { static_assert(sizeof(off_t) == 8, "off_t not 64 bit"); namespace { -const char kPathTemplate[] = "./ExtentWriterTest-file.XXXXXX"; const size_t kBlockSize = 4096; } class ExtentWriterTest : public ::testing::Test { protected: void SetUp() override { - memcpy(path_, kPathTemplate, sizeof(kPathTemplate)); fd_.reset(new EintrSafeFileDescriptor); - int fd = mkstemp(path_); - ASSERT_TRUE(fd_->Open(path_, O_RDWR, 0600)); - close(fd); + ASSERT_TRUE(fd_->Open(temp_file_.path().c_str(), O_RDWR, 0600)); } void TearDown() override { fd_->Close(); - unlink(path_); } // Writes data to an extent writer in 'chunk_size' chunks with @@ -69,7 +59,7 @@ class ExtentWriterTest : public ::testing::Test { void TestZeroPad(bool aligned_size); FileDescriptorPtr fd_; - char path_[sizeof(kPathTemplate)]; + test_utils::ScopedTempFile temp_file_{"ExtentWriterTest-file.XXXXXX"}; }; TEST_F(ExtentWriterTest, SimpleTest) { @@ -87,10 +77,10 @@ TEST_F(ExtentWriterTest, SimpleTest) { EXPECT_TRUE(direct_writer.End()); EXPECT_EQ(static_cast<off_t>(kBlockSize + bytes.size()), - utils::FileSize(path_)); + utils::FileSize(temp_file_.path())); brillo::Blob result_file; - EXPECT_TRUE(utils::ReadFile(path_, &result_file)); + EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &result_file)); brillo::Blob expected_file(kBlockSize); expected_file.insert(expected_file.end(), @@ -154,10 +144,11 @@ void ExtentWriterTest::WriteAlignedExtents(size_t chunk_size, } EXPECT_TRUE(direct_writer.End()); - EXPECT_EQ(static_cast<off_t>(data.size()), utils::FileSize(path_)); + EXPECT_EQ(static_cast<off_t>(data.size()), + utils::FileSize(temp_file_.path())); brillo::Blob result_file; - EXPECT_TRUE(utils::ReadFile(path_, &result_file)); + EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &result_file)); brillo::Blob expected_file; expected_file.insert(expected_file.end(), @@ -203,10 +194,11 @@ void ExtentWriterTest::TestZeroPad(bool aligned_size) { ASSERT_TRUE(zero_pad_writer.Write(data.data(), bytes_to_write)); EXPECT_TRUE(zero_pad_writer.End()); - EXPECT_EQ(static_cast<off_t>(data.size()), utils::FileSize(path_)); + EXPECT_EQ(static_cast<off_t>(data.size()), + utils::FileSize(temp_file_.path())); brillo::Blob result_file; - EXPECT_TRUE(utils::ReadFile(path_, &result_file)); + EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &result_file)); brillo::Blob expected_file; expected_file.insert(expected_file.end(), @@ -252,10 +244,11 @@ TEST_F(ExtentWriterTest, SparseFileTest) { EXPECT_TRUE(direct_writer.End()); // check file size, then data inside - ASSERT_EQ(static_cast<off_t>(2 * kBlockSize), utils::FileSize(path_)); + ASSERT_EQ(static_cast<off_t>(2 * kBlockSize), + utils::FileSize(temp_file_.path())); brillo::Blob resultant_data; - EXPECT_TRUE(utils::ReadFile(path_, &resultant_data)); + EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &resultant_data)); // Create expected data brillo::Blob expected_data(on_disk_count * kBlockSize); |