Fix *ExtentWriterTest unittest.
am: bffa06080e
* commit 'bffa06080ec8bd5d196cbfadf2023f78b6e89169':
Fix *ExtentWriterTest unittest.
diff --git a/common/test_utils.h b/common/test_utils.h
index b5151f2..7be027a 100644
--- a/common/test_utils.h
+++ b/common/test_utils.h
@@ -162,13 +162,15 @@
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 24468b9..f840a75 100644
--- a/common/utils_unittest.cc
+++ b/common/utils_unittest.cc
@@ -280,10 +280,10 @@
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 a52a286..0abf04e 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 @@
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 @@
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_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 d12b4ae..4ffd35c 100644
--- a/payload_consumer/download_action_unittest.cc
+++ b/payload_consumer/download_action_unittest.cc
@@ -135,9 +135,8 @@
// 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 @@
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 @@
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 @@
}
// 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 @@
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 @@
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 6884c0b..4d6b4d6 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 @@
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 @@
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 @@
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 @@
}
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 @@
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 @@
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);