Use ScopedTempFile in unit test.
Replace these 3 lines of code:
string path;
ASSERT_TRUE(utils::MakeTempFile("name-XXXXXX", &path, nullptr));
ScopedPathUnlinker path_unlinker(path);
with one liner:
test_utils::ScopedTempFile file("name-XXXXXX");
Bug: None
Test: unit test
Change-Id: Ic5be7dc8339842270023055bcc3a97e526953f04
diff --git a/common/hash_calculator_unittest.cc b/common/hash_calculator_unittest.cc
index 233237b..79f22ad 100644
--- a/common/hash_calculator_unittest.cc
+++ b/common/hash_calculator_unittest.cc
@@ -26,6 +26,7 @@
#include <brillo/secure_blob.h>
#include <gtest/gtest.h>
+#include "update_engine/common/test_utils.h"
#include "update_engine/common/utils.h"
using std::string;
@@ -43,10 +44,7 @@
0xc8, 0x8b, 0x59, 0xb2, 0xdc, 0x32, 0x7a, 0xa4
};
-class HashCalculatorTest : public ::testing::Test {
- public:
- HashCalculatorTest() {}
-};
+class HashCalculatorTest : public ::testing::Test {};
TEST_F(HashCalculatorTest, SimpleTest) {
HashCalculator calc;
@@ -54,7 +52,7 @@
calc.Finalize();
brillo::Blob raw_hash(std::begin(kExpectedRawHash),
std::end(kExpectedRawHash));
- EXPECT_TRUE(raw_hash == calc.raw_hash());
+ EXPECT_EQ(raw_hash, calc.raw_hash());
}
TEST_F(HashCalculatorTest, MultiUpdateTest) {
@@ -64,7 +62,7 @@
calc.Finalize();
brillo::Blob raw_hash(std::begin(kExpectedRawHash),
std::end(kExpectedRawHash));
- EXPECT_TRUE(raw_hash == calc.raw_hash());
+ EXPECT_EQ(raw_hash, calc.raw_hash());
}
TEST_F(HashCalculatorTest, ContextTest) {
@@ -78,7 +76,7 @@
calc_next.Finalize();
brillo::Blob raw_hash(std::begin(kExpectedRawHash),
std::end(kExpectedRawHash));
- EXPECT_TRUE(raw_hash == calc_next.raw_hash());
+ EXPECT_EQ(raw_hash, calc_next.raw_hash());
}
TEST_F(HashCalculatorTest, BigTest) {
@@ -108,25 +106,21 @@
}
TEST_F(HashCalculatorTest, UpdateFileSimpleTest) {
- string data_path;
- ASSERT_TRUE(
- utils::MakeTempFile("data.XXXXXX", &data_path, nullptr));
- ScopedPathUnlinker data_path_unlinker(data_path);
- ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2));
+ test_utils::ScopedTempFile data_file("data.XXXXXX");
+ ASSERT_TRUE(test_utils::WriteFileString(data_file.path(), "hi"));
- static const int kLengths[] = { -1, 2, 10 };
- for (size_t i = 0; i < arraysize(kLengths); i++) {
+ for (const int length : {-1, 2, 10}) {
HashCalculator calc;
- EXPECT_EQ(2, calc.UpdateFile(data_path, kLengths[i]));
+ EXPECT_EQ(2, calc.UpdateFile(data_file.path(), length));
EXPECT_TRUE(calc.Finalize());
brillo::Blob raw_hash(std::begin(kExpectedRawHash),
std::end(kExpectedRawHash));
- EXPECT_TRUE(raw_hash == calc.raw_hash());
+ EXPECT_EQ(raw_hash, calc.raw_hash());
}
HashCalculator calc;
- EXPECT_EQ(0, calc.UpdateFile(data_path, 0));
- EXPECT_EQ(1, calc.UpdateFile(data_path, 1));
+ EXPECT_EQ(0, calc.UpdateFile(data_file.path(), 0));
+ EXPECT_EQ(1, calc.UpdateFile(data_file.path(), 1));
EXPECT_TRUE(calc.Finalize());
// echo -n h | openssl dgst -sha256 -binary | openssl base64
EXPECT_EQ("qqlAJmTxpB9A67xSyZk+tmrrNmYClY/fqig7ceZNsSM=",
@@ -134,21 +128,16 @@
}
TEST_F(HashCalculatorTest, RawHashOfFileSimpleTest) {
- string data_path;
- ASSERT_TRUE(
- utils::MakeTempFile("data.XXXXXX", &data_path, nullptr));
- ScopedPathUnlinker data_path_unlinker(data_path);
- ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2));
+ test_utils::ScopedTempFile data_file("data.XXXXXX");
+ ASSERT_TRUE(test_utils::WriteFileString(data_file.path(), "hi"));
- static const int kLengths[] = { -1, 2, 10 };
- for (size_t i = 0; i < arraysize(kLengths); i++) {
+ for (const int length : {-1, 2, 10}) {
brillo::Blob exp_raw_hash(std::begin(kExpectedRawHash),
std::end(kExpectedRawHash));
brillo::Blob raw_hash;
- EXPECT_EQ(2, HashCalculator::RawHashOfFile(data_path,
- kLengths[i],
- &raw_hash));
- EXPECT_TRUE(exp_raw_hash == raw_hash);
+ EXPECT_EQ(
+ 2, HashCalculator::RawHashOfFile(data_file.path(), length, &raw_hash));
+ EXPECT_EQ(exp_raw_hash, raw_hash);
}
}
diff --git a/common/utils_unittest.cc b/common/utils_unittest.cc
index 62f9f6c..dff0569 100644
--- a/common/utils_unittest.cc
+++ b/common/utils_unittest.cc
@@ -57,13 +57,11 @@
}
TEST(UtilsTest, WriteFileReadFile) {
- base::FilePath file;
- EXPECT_TRUE(base::CreateTemporaryFile(&file));
- ScopedPathUnlinker unlinker(file.value());
- EXPECT_TRUE(utils::WriteFile(file.value().c_str(), "hello", 5));
+ test_utils::ScopedTempFile file;
+ EXPECT_TRUE(utils::WriteFile(file.path().c_str(), "hello", 5));
brillo::Blob readback;
- EXPECT_TRUE(utils::ReadFile(file.value().c_str(), &readback));
+ EXPECT_TRUE(utils::ReadFile(file.path().c_str(), &readback));
EXPECT_EQ("hello", string(readback.begin(), readback.end()));
}
@@ -73,24 +71,21 @@
}
TEST(UtilsTest, ReadFileChunk) {
- base::FilePath file;
- EXPECT_TRUE(base::CreateTemporaryFile(&file));
- ScopedPathUnlinker unlinker(file.value());
+ test_utils::ScopedTempFile file;
brillo::Blob data;
const size_t kSize = 1024 * 1024;
for (size_t i = 0; i < kSize; i++) {
data.push_back(i % 255);
}
- EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size()));
+ EXPECT_TRUE(test_utils::WriteFileVector(file.path(), data));
brillo::Blob in_data;
- EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
+ EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), kSize, 10, &in_data));
EXPECT_TRUE(in_data.empty());
- EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
- EXPECT_TRUE(data == in_data);
+ EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 0, -1, &in_data));
+ EXPECT_EQ(data, in_data);
in_data.clear();
- EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
- EXPECT_TRUE(brillo::Blob(data.begin() + 10, data.begin() + 10 + 20) ==
- in_data);
+ EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 10, 20, &in_data));
+ EXPECT_EQ(brillo::Blob(data.begin() + 10, data.begin() + 10 + 20), in_data);
}
TEST(UtilsTest, ErrnoNumberAsStringTest) {
@@ -464,20 +459,18 @@
}
TEST(UtilsTest, RunAsRootUnmountFilesystemBusyFailureTest) {
- string tmp_image;
- EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &tmp_image, nullptr));
- ScopedPathUnlinker tmp_image_unlinker(tmp_image);
+ test_utils::ScopedTempFile tmp_image("img.XXXXXX");
EXPECT_TRUE(base::CopyFile(
test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"),
- base::FilePath(tmp_image)));
+ base::FilePath(tmp_image.path())));
base::ScopedTempDir mnt_dir;
EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
string loop_dev;
test_utils::ScopedLoopbackDeviceBinder loop_binder(
- tmp_image, true, &loop_dev);
+ tmp_image.path(), true, &loop_dev);
EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
// This is the actual test part. While we hold a file descriptor open for the
@@ -506,10 +499,8 @@
EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
- base::FilePath file;
- EXPECT_TRUE(base::CreateTemporaryFile(&file));
- ScopedPathUnlinker unlinker(file.value());
- EXPECT_FALSE(utils::IsMountpoint(file.value()));
+ test_utils::ScopedTempFile file;
+ EXPECT_FALSE(utils::IsMountpoint(file.path()));
}
} // namespace chromeos_update_engine
diff --git a/omaha_response_handler_action_unittest.cc b/omaha_response_handler_action_unittest.cc
index 9e2cdd1..909452a 100644
--- a/omaha_response_handler_action_unittest.cc
+++ b/omaha_response_handler_action_unittest.cc
@@ -159,11 +159,8 @@
}
TEST_F(OmahaResponseHandlerActionTest, SimpleTest) {
- string test_deadline_file;
- CHECK(utils::MakeTempFile("omaha_response_handler_action_unittest-XXXXXX",
- &test_deadline_file,
- nullptr));
- ScopedPathUnlinker deadline_unlinker(test_deadline_file);
+ test_utils::ScopedTempFile test_deadline_file(
+ "omaha_response_handler_action_unittest-XXXXXX");
{
OmahaResponse in;
in.update_exists = true;
@@ -176,15 +173,15 @@
in.prompt = false;
in.deadline = "20101020";
InstallPlan install_plan;
- EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
+ EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
EXPECT_EQ(1U, install_plan.target_slot);
string deadline;
- EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
+ EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline));
EXPECT_EQ("20101020", deadline);
struct stat deadline_stat;
- EXPECT_EQ(0, stat(test_deadline_file.c_str(), &deadline_stat));
+ EXPECT_EQ(0, stat(test_deadline_file.path().c_str(), &deadline_stat));
EXPECT_EQ(
static_cast<mode_t>(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
deadline_stat.st_mode);
@@ -203,12 +200,12 @@
InstallPlan install_plan;
// Set the other slot as current.
fake_system_state_.fake_boot_control()->SetCurrentSlot(1);
- EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
+ EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
EXPECT_EQ(0U, install_plan.target_slot);
string deadline;
- EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline) &&
+ EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline) &&
deadline.empty());
EXPECT_EQ(in.version, install_plan.version);
}
@@ -223,12 +220,12 @@
in.deadline = "some-deadline";
InstallPlan install_plan;
fake_system_state_.fake_boot_control()->SetCurrentSlot(0);
- EXPECT_TRUE(DoTest(in, test_deadline_file, &install_plan));
+ EXPECT_TRUE(DoTest(in, test_deadline_file.path(), &install_plan));
EXPECT_EQ(in.packages[0].payload_urls[0], install_plan.download_url);
EXPECT_EQ(expected_hash_, install_plan.payloads[0].hash);
EXPECT_EQ(1U, install_plan.target_slot);
string deadline;
- EXPECT_TRUE(utils::ReadFile(test_deadline_file, &deadline));
+ EXPECT_TRUE(utils::ReadFile(test_deadline_file.path(), &deadline));
EXPECT_EQ("some-deadline", deadline);
EXPECT_EQ(in.version, install_plan.version);
}
diff --git a/payload_consumer/delta_performer_integration_test.cc b/payload_consumer/delta_performer_integration_test.cc
index ef99cc9..52cc1df 100644
--- a/payload_consumer/delta_performer_integration_test.cc
+++ b/payload_consumer/delta_performer_integration_test.cc
@@ -234,9 +234,7 @@
RSA_free(rsa);
}
int signature_size = GetSignatureSize(private_key_path);
- string hash_file;
- ASSERT_TRUE(utils::MakeTempFile("hash.XXXXXX", &hash_file, nullptr));
- ScopedPathUnlinker hash_unlinker(hash_file);
+ test_utils::ScopedTempFile hash_file("hash.XXXXXX");
string signature_size_string;
if (signature_test == kSignatureGeneratedShellRotateCl1 ||
signature_test == kSignatureGeneratedShellRotateCl2)
@@ -251,28 +249,25 @@
delta_generator_path.c_str(),
payload_path.c_str(),
signature_size_string.c_str(),
- hash_file.c_str())));
+ hash_file.path().c_str())));
// Sign the hash
brillo::Blob hash, signature;
- ASSERT_TRUE(utils::ReadFile(hash_file, &hash));
+ ASSERT_TRUE(utils::ReadFile(hash_file.path(), &hash));
ASSERT_TRUE(PayloadSigner::SignHash(hash, private_key_path, &signature));
- string sig_file;
- ASSERT_TRUE(utils::MakeTempFile("signature.XXXXXX", &sig_file, nullptr));
- ScopedPathUnlinker sig_unlinker(sig_file);
- ASSERT_TRUE(test_utils::WriteFileVector(sig_file, signature));
+ test_utils::ScopedTempFile sig_file("signature.XXXXXX");
+ ASSERT_TRUE(test_utils::WriteFileVector(sig_file.path(), signature));
+ string sig_files = sig_file.path();
- string sig_file2;
- ASSERT_TRUE(utils::MakeTempFile("signature.XXXXXX", &sig_file2, nullptr));
- ScopedPathUnlinker sig2_unlinker(sig_file2);
+ test_utils::ScopedTempFile sig_file2("signature.XXXXXX");
if (signature_test == kSignatureGeneratedShellRotateCl1 ||
signature_test == kSignatureGeneratedShellRotateCl2) {
ASSERT_TRUE(PayloadSigner::SignHash(
hash, GetBuildArtifactsPath(kUnittestPrivateKey2Path), &signature));
- ASSERT_TRUE(test_utils::WriteFileVector(sig_file2, signature));
+ ASSERT_TRUE(test_utils::WriteFileVector(sig_file2.path(), signature));
// Append second sig file to first path
- sig_file += ":" + sig_file2;
+ sig_files += ":" + sig_file2.path();
}
ASSERT_EQ(0,
@@ -280,7 +275,7 @@
"%s -in_file=%s -signature_file=%s -out_file=%s",
delta_generator_path.c_str(),
payload_path.c_str(),
- sig_file.c_str(),
+ sig_files.c_str(),
payload_path.c_str())));
int verify_result = System(base::StringPrintf(
"%s -in_file=%s -public_key=%s -public_key_version=%d",
diff --git a/payload_consumer/delta_performer_unittest.cc b/payload_consumer/delta_performer_unittest.cc
index c3e4fdb..c2e11e5 100644
--- a/payload_consumer/delta_performer_unittest.cc
+++ b/payload_consumer/delta_performer_unittest.cc
@@ -185,12 +185,8 @@
bool sign_payload,
uint64_t major_version,
uint32_t minor_version) {
- string blob_path;
- EXPECT_TRUE(utils::MakeTempFile("Blob-XXXXXX", &blob_path, nullptr));
- ScopedPathUnlinker blob_unlinker(blob_path);
- EXPECT_TRUE(utils::WriteFile(blob_path.c_str(),
- blob_data.data(),
- blob_data.size()));
+ test_utils::ScopedTempFile blob_file("Blob-XXXXXX");
+ EXPECT_TRUE(test_utils::WriteFileVector(blob_file.path(), blob_data));
PayloadGenerationConfig config;
config.version.major = major_version;
@@ -218,16 +214,16 @@
new_part.size = 0;
payload.AddPartition(old_part, new_part, {});
- string payload_path;
- EXPECT_TRUE(utils::MakeTempFile("Payload-XXXXXX", &payload_path, nullptr));
- ScopedPathUnlinker payload_unlinker(payload_path);
+ test_utils::ScopedTempFile payload_file("Payload-XXXXXX");
string private_key =
sign_payload ? GetBuildArtifactsPath(kUnittestPrivateKeyPath) : "";
- EXPECT_TRUE(payload.WritePayload(
- payload_path, blob_path, private_key, &payload_.metadata_size));
+ EXPECT_TRUE(payload.WritePayload(payload_file.path(),
+ blob_file.path(),
+ private_key,
+ &payload_.metadata_size));
brillo::Blob payload_data;
- EXPECT_TRUE(utils::ReadFile(payload_path, &payload_data));
+ EXPECT_TRUE(utils::ReadFile(payload_file.path(), &payload_data));
return payload_data;
}
@@ -268,16 +264,13 @@
const string& source_path,
const brillo::Blob& target_data,
bool expect_success) {
- string new_part;
- EXPECT_TRUE(utils::MakeTempFile("Partition-XXXXXX", &new_part, nullptr));
- ScopedPathUnlinker partition_unlinker(new_part);
- EXPECT_TRUE(utils::WriteFile(new_part.c_str(), target_data.data(),
- target_data.size()));
+ test_utils::ScopedTempFile new_part("Partition-XXXXXX");
+ EXPECT_TRUE(test_utils::WriteFileVector(new_part.path(), target_data));
// We installed the operations only in the rootfs partition, but the
// delta performer needs to access all the partitions.
fake_boot_control_.SetPartitionDevice(
- kLegacyPartitionNameRoot, install_plan_.target_slot, new_part);
+ kLegacyPartitionNameRoot, install_plan_.target_slot, new_part.path());
fake_boot_control_.SetPartitionDevice(
kLegacyPartitionNameRoot, install_plan_.source_slot, source_path);
fake_boot_control_.SetPartitionDevice(
@@ -290,7 +283,7 @@
EXPECT_EQ(0, performer_.Close());
brillo::Blob partition_data;
- EXPECT_TRUE(utils::ReadFile(new_part, &partition_data));
+ EXPECT_TRUE(utils::ReadFile(new_part.path(), &partition_data));
return partition_data;
}
@@ -568,15 +561,10 @@
brillo::Blob payload_data = GeneratePayload(brillo::Blob(), {aop}, false);
- string source_path;
- EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX",
- &source_path, nullptr));
- ScopedPathUnlinker path_unlinker(source_path);
- EXPECT_TRUE(utils::WriteFile(source_path.c_str(),
- expected_data.data(),
- expected_data.size()));
+ test_utils::ScopedTempFile source("Source-XXXXXX");
+ EXPECT_TRUE(test_utils::WriteFileVector(source.path(), expected_data));
- EXPECT_EQ(expected_data, ApplyPayload(payload_data, source_path, true));
+ EXPECT_EQ(expected_data, ApplyPayload(payload_data, source.path(), true));
}
TEST_F(DeltaPerformerTest, PuffdiffOperationTest) {
@@ -596,13 +584,11 @@
brillo::Blob payload_data = GeneratePayload(puffdiff_payload, {aop}, false);
- string source_path;
- EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX", &source_path, nullptr));
- ScopedPathUnlinker path_unlinker(source_path);
- EXPECT_TRUE(utils::WriteFile(source_path.c_str(), src.data(), src.size()));
+ test_utils::ScopedTempFile source("Source-XXXXXX");
+ EXPECT_TRUE(test_utils::WriteFileVector(source.path(), src));
brillo::Blob dst(std::begin(dst_deflates), std::end(dst_deflates));
- EXPECT_EQ(dst, ApplyPayload(payload_data, source_path, true));
+ EXPECT_EQ(dst, ApplyPayload(payload_data, source.path(), true));
}
TEST_F(DeltaPerformerTest, SourceHashMismatchTest) {
@@ -621,27 +607,21 @@
brillo::Blob payload_data = GeneratePayload(brillo::Blob(), {aop}, false);
- string source_path;
- EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX", &source_path, nullptr));
- ScopedPathUnlinker path_unlinker(source_path);
- EXPECT_TRUE(utils::WriteFile(source_path.c_str(), actual_data.data(),
- actual_data.size()));
+ test_utils::ScopedTempFile source("Source-XXXXXX");
+ EXPECT_TRUE(test_utils::WriteFileVector(source.path(), actual_data));
- EXPECT_EQ(actual_data, ApplyPayload(payload_data, source_path, false));
+ EXPECT_EQ(actual_data, ApplyPayload(payload_data, source.path(), false));
}
// Test that the error-corrected file descriptor is used to read the partition
// since the source partition doesn't match the operation hash.
TEST_F(DeltaPerformerTest, ErrorCorrectionSourceCopyFallbackTest) {
- const size_t kCopyOperationSize = 4 * 4096;
- string source_path;
- EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX", &source_path, nullptr));
- ScopedPathUnlinker path_unlinker(source_path);
+ constexpr size_t kCopyOperationSize = 4 * 4096;
+ test_utils::ScopedTempFile source("Source-XXXXXX");
// Write invalid data to the source image, which doesn't match the expected
// hash.
brillo::Blob invalid_data(kCopyOperationSize, 0x55);
- EXPECT_TRUE(utils::WriteFile(
- source_path.c_str(), invalid_data.data(), invalid_data.size()));
+ EXPECT_TRUE(test_utils::WriteFileVector(source.path(), invalid_data));
// Setup the fec file descriptor as the fake stream, which matches
// |expected_data|.
@@ -649,7 +629,7 @@
brillo::Blob expected_data = FakeFileDescriptorData(kCopyOperationSize);
brillo::Blob payload_data = GenerateSourceCopyPayload(expected_data, true);
- EXPECT_EQ(expected_data, ApplyPayload(payload_data, source_path, true));
+ EXPECT_EQ(expected_data, ApplyPayload(payload_data, source.path(), true));
// Verify that the fake_fec was actually used.
EXPECT_EQ(1U, fake_fec->GetReadOps().size());
EXPECT_EQ(1U, GetSourceEccRecoveredFailures());
@@ -659,14 +639,11 @@
// when no hash is available for SOURCE_COPY but it falls back to the normal
// file descriptor when the size of the error corrected one is too small.
TEST_F(DeltaPerformerTest, ErrorCorrectionSourceCopyWhenNoHashFallbackTest) {
- const size_t kCopyOperationSize = 4 * 4096;
- string source_path;
- EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX", &source_path, nullptr));
- ScopedPathUnlinker path_unlinker(source_path);
+ constexpr size_t kCopyOperationSize = 4 * 4096;
+ test_utils::ScopedTempFile source("Source-XXXXXX");
// Setup the source path with the right expected data.
brillo::Blob expected_data = FakeFileDescriptorData(kCopyOperationSize);
- EXPECT_TRUE(utils::WriteFile(
- source_path.c_str(), expected_data.data(), expected_data.size()));
+ EXPECT_TRUE(test_utils::WriteFileVector(source.path(), expected_data));
// Setup the fec file descriptor as the fake stream, with smaller data than
// the expected.
@@ -674,7 +651,7 @@
// The payload operation doesn't include an operation hash.
brillo::Blob payload_data = GenerateSourceCopyPayload(expected_data, false);
- EXPECT_EQ(expected_data, ApplyPayload(payload_data, source_path, true));
+ EXPECT_EQ(expected_data, ApplyPayload(payload_data, source.path(), true));
// Verify that the fake_fec was attempted to be used. Since the file
// descriptor is shorter it can actually do more than one read to realize it
// reached the EOF.
@@ -685,18 +662,15 @@
}
TEST_F(DeltaPerformerTest, ChooseSourceFDTest) {
- const size_t kSourceSize = 4 * 4096;
- string source_path;
- EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX", &source_path, nullptr));
- ScopedPathUnlinker path_unlinker(source_path);
+ constexpr size_t kSourceSize = 4 * 4096;
+ test_utils::ScopedTempFile source("Source-XXXXXX");
// Write invalid data to the source image, which doesn't match the expected
// hash.
brillo::Blob invalid_data(kSourceSize, 0x55);
- EXPECT_TRUE(utils::WriteFile(
- source_path.c_str(), invalid_data.data(), invalid_data.size()));
+ EXPECT_TRUE(test_utils::WriteFileVector(source.path(), invalid_data));
performer_.source_fd_ = std::make_shared<EintrSafeFileDescriptor>();
- performer_.source_fd_->Open(source_path.c_str(), O_RDONLY);
+ performer_.source_fd_->Open(source.path().c_str(), O_RDONLY);
performer_.block_size_ = 4096;
// Setup the fec file descriptor as the fake stream, which matches
diff --git a/payload_consumer/file_writer_unittest.cc b/payload_consumer/file_writer_unittest.cc
index 92837c8..05df307 100644
--- a/payload_consumer/file_writer_unittest.cc
+++ b/payload_consumer/file_writer_unittest.cc
@@ -36,19 +36,17 @@
TEST(FileWriterTest, SimpleTest) {
// Create a uniquely named file for testing.
- string path;
- ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr));
- ScopedPathUnlinker path_unlinker(path);
-
+ test_utils::ScopedTempFile file("FileWriterTest-XXXXXX");
DirectFileWriter file_writer;
- EXPECT_EQ(0, file_writer.Open(path.c_str(),
- O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
- 0644));
+ EXPECT_EQ(0,
+ file_writer.Open(file.path().c_str(),
+ O_CREAT | O_LARGEFILE | O_TRUNC | O_WRONLY,
+ 0644));
EXPECT_TRUE(file_writer.Write("test", 4));
brillo::Blob actual_data;
- EXPECT_TRUE(utils::ReadFile(path, &actual_data));
+ EXPECT_TRUE(utils::ReadFile(file.path(), &actual_data));
- EXPECT_FALSE(memcmp("test", actual_data.data(), actual_data.size()));
+ EXPECT_EQ("test", string(actual_data.begin(), actual_data.end()));
EXPECT_EQ(0, file_writer.Close());
}
@@ -61,14 +59,12 @@
TEST(FileWriterTest, WriteErrorTest) {
// Create a uniquely named file for testing.
- string path;
- ASSERT_TRUE(utils::MakeTempFile("FileWriterTest-XXXXXX", &path, nullptr));
- ScopedPathUnlinker path_unlinker(path);
-
+ test_utils::ScopedTempFile file("FileWriterTest-XXXXXX");
DirectFileWriter file_writer;
- EXPECT_EQ(0, file_writer.Open(path.c_str(),
- O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
- 0644));
+ EXPECT_EQ(0,
+ file_writer.Open(file.path().c_str(),
+ O_CREAT | O_LARGEFILE | O_TRUNC | O_RDONLY,
+ 0644));
EXPECT_FALSE(file_writer.Write("x", 1));
EXPECT_EQ(0, file_writer.Close());
}
diff --git a/payload_consumer/filesystem_verifier_action_unittest.cc b/payload_consumer/filesystem_verifier_action_unittest.cc
index b4f7f7f..fd1d084 100644
--- a/payload_consumer/filesystem_verifier_action_unittest.cc
+++ b/payload_consumer/filesystem_verifier_action_unittest.cc
@@ -112,13 +112,7 @@
bool FilesystemVerifierActionTest::DoTest(bool terminate_early,
bool hash_fail) {
- string a_loop_file;
-
- if (!(utils::MakeTempFile("a_loop_file.XXXXXX", &a_loop_file, nullptr))) {
- ADD_FAILURE();
- return false;
- }
- ScopedPathUnlinker a_loop_file_unlinker(a_loop_file);
+ test_utils::ScopedTempFile a_loop_file("a_loop_file.XXXXXX");
// Make random data for a.
const size_t kLoopFileSize = 10 * 1024 * 1024 + 512;
@@ -126,7 +120,7 @@
test_utils::FillWithData(&a_loop_data);
// Write data to disk
- if (!(test_utils::WriteFileVector(a_loop_file, a_loop_data))) {
+ if (!(test_utils::WriteFileVector(a_loop_file.path(), a_loop_data))) {
ADD_FAILURE();
return false;
}
@@ -134,13 +128,13 @@
// Attach loop devices to the files
string a_dev;
test_utils::ScopedLoopbackDeviceBinder a_dev_releaser(
- a_loop_file, false, &a_dev);
+ a_loop_file.path(), false, &a_dev);
if (!(a_dev_releaser.is_bound())) {
ADD_FAILURE();
return false;
}
- LOG(INFO) << "verifying: " << a_loop_file << " (" << a_dev << ")";
+ LOG(INFO) << "verifying: " << a_loop_file.path() << " (" << a_dev << ")";
bool success = true;
diff --git a/payload_generator/ab_generator_unittest.cc b/payload_generator/ab_generator_unittest.cc
index 25609c7..d083d8a 100644
--- a/payload_generator/ab_generator_unittest.cc
+++ b/payload_generator/ab_generator_unittest.cc
@@ -58,10 +58,6 @@
const size_t part_num_blocks = 7;
// Create the target partition data.
- string part_path;
- EXPECT_TRUE(utils::MakeTempFile(
- "SplitReplaceOrReplaceBzTest_part.XXXXXX", &part_path, nullptr));
- ScopedPathUnlinker part_path_unlinker(part_path);
const size_t part_size = part_num_blocks * kBlockSize;
brillo::Blob part_data;
if (compressible) {
@@ -74,7 +70,9 @@
part_data.push_back(dis(gen));
}
ASSERT_EQ(part_size, part_data.size());
- ASSERT_TRUE(utils::WriteFile(part_path.c_str(), part_data.data(), part_size));
+ test_utils::ScopedTempFile part_file(
+ "SplitReplaceOrReplaceBzTest_part.XXXXXX");
+ ASSERT_TRUE(test_utils::WriteFileVector(part_file.path(), part_data));
// Create original operation and blob data.
const size_t op_ex1_offset = op_ex1_start_block * kBlockSize;
@@ -109,15 +107,12 @@
aop.name = "SplitTestOp";
// Create the data file.
- string data_path;
- EXPECT_TRUE(utils::MakeTempFile(
- "SplitReplaceOrReplaceBzTest_data.XXXXXX", &data_path, nullptr));
- ScopedPathUnlinker data_path_unlinker(data_path);
- int data_fd = open(data_path.c_str(), O_RDWR, 000);
+ test_utils::ScopedTempFile data_file(
+ "SplitReplaceOrReplaceBzTest_data.XXXXXX");
+ EXPECT_TRUE(test_utils::WriteFileVector(data_file.path(), op_blob));
+ int data_fd = open(data_file.path().c_str(), O_RDWR, 000);
EXPECT_GE(data_fd, 0);
ScopedFdCloser data_fd_closer(&data_fd);
- EXPECT_TRUE(utils::WriteFile(data_path.c_str(), op_blob.data(),
- op_blob.size()));
off_t data_file_size = op_blob.size();
BlobFileWriter blob_file(data_fd, &data_file_size);
@@ -126,7 +121,7 @@
PayloadVersion version(kChromeOSMajorPayloadVersion,
kSourceMinorPayloadVersion);
ASSERT_TRUE(ABGenerator::SplitAReplaceOp(
- version, aop, part_path, &result_ops, &blob_file));
+ version, aop, part_file.path(), &result_ops, &blob_file));
// Check the result.
InstallOperation_Type expected_type =
@@ -212,10 +207,6 @@
const size_t part_num_blocks = total_op_num_blocks + 2;
// Create the target partition data.
- string part_path;
- EXPECT_TRUE(utils::MakeTempFile(
- "MergeReplaceOrReplaceBzTest_part.XXXXXX", &part_path, nullptr));
- ScopedPathUnlinker part_path_unlinker(part_path);
const size_t part_size = part_num_blocks * kBlockSize;
brillo::Blob part_data;
if (compressible) {
@@ -228,7 +219,9 @@
part_data.push_back(dis(gen));
}
ASSERT_EQ(part_size, part_data.size());
- ASSERT_TRUE(utils::WriteFile(part_path.c_str(), part_data.data(), part_size));
+ test_utils::ScopedTempFile part_file(
+ "MergeReplaceOrReplaceBzTest_part.XXXXXX");
+ ASSERT_TRUE(test_utils::WriteFileVector(part_file.path(), part_data));
// Create original operations and blob data.
vector<AnnotatedOperation> aops;
@@ -277,23 +270,20 @@
aops.push_back(second_aop);
// Create the data file.
- string data_path;
- EXPECT_TRUE(utils::MakeTempFile(
- "MergeReplaceOrReplaceBzTest_data.XXXXXX", &data_path, nullptr));
- ScopedPathUnlinker data_path_unlinker(data_path);
- int data_fd = open(data_path.c_str(), O_RDWR, 000);
+ test_utils::ScopedTempFile data_file(
+ "MergeReplaceOrReplaceBzTest_data.XXXXXX");
+ EXPECT_TRUE(test_utils::WriteFileVector(data_file.path(), blob_data));
+ int data_fd = open(data_file.path().c_str(), O_RDWR, 000);
EXPECT_GE(data_fd, 0);
ScopedFdCloser data_fd_closer(&data_fd);
- EXPECT_TRUE(utils::WriteFile(data_path.c_str(), blob_data.data(),
- blob_data.size()));
off_t data_file_size = blob_data.size();
BlobFileWriter blob_file(data_fd, &data_file_size);
// Merge the operations.
PayloadVersion version(kChromeOSMajorPayloadVersion,
kSourceMinorPayloadVersion);
- EXPECT_TRUE(
- ABGenerator::MergeOperations(&aops, version, 5, part_path, &blob_file));
+ EXPECT_TRUE(ABGenerator::MergeOperations(
+ &aops, version, 5, part_file.path(), &blob_file));
// Check the result.
InstallOperation_Type expected_op_type =
@@ -570,16 +560,12 @@
second_aop.op = second_op;
aops.push_back(second_aop);
- string src_part_path;
- EXPECT_TRUE(utils::MakeTempFile("AddSourceHashTest_src_part.XXXXXX",
- &src_part_path, nullptr));
- ScopedPathUnlinker src_part_path_unlinker(src_part_path);
+ test_utils::ScopedTempFile src_part_file("AddSourceHashTest_src_part.XXXXXX");
brillo::Blob src_data(kBlockSize);
test_utils::FillWithData(&src_data);
- ASSERT_TRUE(utils::WriteFile(src_part_path.c_str(), src_data.data(),
- src_data.size()));
+ ASSERT_TRUE(test_utils::WriteFileVector(src_part_file.path(), src_data));
- EXPECT_TRUE(ABGenerator::AddSourceHash(&aops, src_part_path));
+ EXPECT_TRUE(ABGenerator::AddSourceHash(&aops, src_part_file.path()));
EXPECT_TRUE(aops[0].op.has_src_sha256_hash());
EXPECT_FALSE(aops[1].op.has_src_sha256_hash());
diff --git a/payload_generator/block_mapping_unittest.cc b/payload_generator/block_mapping_unittest.cc
index 4d09710..e1870ec 100644
--- a/payload_generator/block_mapping_unittest.cc
+++ b/payload_generator/block_mapping_unittest.cc
@@ -39,23 +39,9 @@
class BlockMappingTest : public ::testing::Test {
protected:
- void SetUp() override {
- EXPECT_TRUE(utils::MakeTempFile("BlockMappingTest_old.XXXXXX",
- &old_part_path_,
- nullptr));
- EXPECT_TRUE(utils::MakeTempFile("BlockMappingTest_new.XXXXXX",
- &new_part_path_,
- nullptr));
-
- old_part_unlinker_.reset(new ScopedPathUnlinker(old_part_path_));
- new_part_unlinker_.reset(new ScopedPathUnlinker(new_part_path_));
- }
-
// Old new partition files used in testing.
- string old_part_path_;
- string new_part_path_;
- std::unique_ptr<ScopedPathUnlinker> old_part_unlinker_;
- std::unique_ptr<ScopedPathUnlinker> new_part_unlinker_;
+ test_utils::ScopedTempFile old_part_{"BlockMappingTest_old.XXXXXX"};
+ test_utils::ScopedTempFile new_part_{"BlockMappingTest_new.XXXXXX"};
size_t block_size_{1024};
BlockMapping bm_{block_size_}; // BlockMapping under test.
@@ -72,8 +58,8 @@
}
TEST_F(BlockMappingTest, BlocksAreNotKeptInMemory) {
- test_utils::WriteFileString(old_part_path_, string(block_size_, 'a'));
- int old_fd = HANDLE_EINTR(open(old_part_path_.c_str(), O_RDONLY));
+ test_utils::WriteFileString(old_part_.path(), string(block_size_, 'a'));
+ int old_fd = HANDLE_EINTR(open(old_part_.path().c_str(), O_RDONLY));
ScopedFdCloser old_fd_closer(&old_fd);
EXPECT_EQ(0, bm_.AddDiskBlock(old_fd, 0));
@@ -107,18 +93,18 @@
string old_contents(10 * block_size_, '\0');
for (size_t i = 0; i < old_contents.size(); ++i)
old_contents[i] = 4 + i / block_size_;
- test_utils::WriteFileString(old_part_path_, old_contents);
+ test_utils::WriteFileString(old_part_.path(), old_contents);
// A string including the block with all zeros and overlapping some of the
// other blocks in old_contents.
string new_contents(6 * block_size_, '\0');
for (size_t i = 0; i < new_contents.size(); ++i)
new_contents[i] = i / block_size_;
- test_utils::WriteFileString(new_part_path_, new_contents);
+ test_utils::WriteFileString(new_part_.path(), new_contents);
vector<BlockMapping::BlockId> old_ids, new_ids;
- EXPECT_TRUE(MapPartitionBlocks(old_part_path_,
- new_part_path_,
+ EXPECT_TRUE(MapPartitionBlocks(old_part_.path(),
+ new_part_.path(),
old_contents.size(),
new_contents.size(),
block_size_,
diff --git a/payload_generator/full_update_generator_unittest.cc b/payload_generator/full_update_generator_unittest.cc
index 6da4d10..e398125 100644
--- a/payload_generator/full_update_generator_unittest.cc
+++ b/payload_generator/full_update_generator_unittest.cc
@@ -40,15 +40,11 @@
config_.hard_chunk_size = 128 * 1024;
config_.block_size = 4096;
- EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_partition.XXXXXX",
- &new_part_conf.path,
- nullptr));
- EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_blobs.XXXXXX",
- &out_blobs_path_,
- &out_blobs_fd_));
+ new_part_conf.path = part_file_.path();
+ EXPECT_TRUE(utils::MakeTempFile(
+ "FullUpdateTest_blobs.XXXXXX", &out_blobs_path_, &out_blobs_fd_));
blob_file_.reset(new BlobFileWriter(out_blobs_fd_, &out_blobs_length_));
- part_path_unlinker_.reset(new ScopedPathUnlinker(new_part_conf.path));
out_blobs_unlinker_.reset(new ScopedPathUnlinker(out_blobs_path_));
}
@@ -62,9 +58,9 @@
int out_blobs_fd_{-1};
off_t out_blobs_length_{0};
ScopedFdCloser out_blobs_fd_closer_{&out_blobs_fd_};
+ test_utils::ScopedTempFile part_file_{"FullUpdateTest_partition.XXXXXX"};
std::unique_ptr<BlobFileWriter> blob_file_;
- std::unique_ptr<ScopedPathUnlinker> part_path_unlinker_;
std::unique_ptr<ScopedPathUnlinker> out_blobs_unlinker_;
// FullUpdateGenerator under test.
diff --git a/payload_generator/payload_file_unittest.cc b/payload_generator/payload_file_unittest.cc
index e8e7e14..45faebb9 100644
--- a/payload_generator/payload_file_unittest.cc
+++ b/payload_generator/payload_file_unittest.cc
@@ -36,23 +36,16 @@
};
TEST_F(PayloadFileTest, ReorderBlobsTest) {
- string orig_blobs;
- EXPECT_TRUE(utils::MakeTempFile("ReorderBlobsTest.orig.XXXXXX", &orig_blobs,
- nullptr));
- ScopedPathUnlinker orig_blobs_unlinker(orig_blobs);
+ test_utils::ScopedTempFile orig_blobs("ReorderBlobsTest.orig.XXXXXX");
// The operations have three blob and one gap (the whitespace):
// Rootfs operation 1: [8, 3] bcd
// Rootfs operation 2: [7, 1] a
// Kernel operation 1: [0, 6] kernel
string orig_data = "kernel abcd";
- EXPECT_TRUE(
- utils::WriteFile(orig_blobs.c_str(), orig_data.data(), orig_data.size()));
+ EXPECT_TRUE(test_utils::WriteFileString(orig_blobs.path(), orig_data));
- string new_blobs;
- EXPECT_TRUE(
- utils::MakeTempFile("ReorderBlobsTest.new.XXXXXX", &new_blobs, nullptr));
- ScopedPathUnlinker new_blobs_unlinker(new_blobs);
+ test_utils::ScopedTempFile new_blobs("ReorderBlobsTest.new.XXXXXX");
payload_.part_vec_.resize(2);
@@ -71,12 +64,12 @@
aop.op.set_data_length(6);
payload_.part_vec_[1].aops = {aop};
- EXPECT_TRUE(payload_.ReorderDataBlobs(orig_blobs, new_blobs));
+ EXPECT_TRUE(payload_.ReorderDataBlobs(orig_blobs.path(), new_blobs.path()));
const vector<AnnotatedOperation>& part0_aops = payload_.part_vec_[0].aops;
const vector<AnnotatedOperation>& part1_aops = payload_.part_vec_[1].aops;
string new_data;
- EXPECT_TRUE(utils::ReadFile(new_blobs, &new_data));
+ EXPECT_TRUE(utils::ReadFile(new_blobs.path(), &new_data));
// Kernel blobs should appear at the end.
EXPECT_EQ("bcdakernel", new_data);
diff --git a/payload_generator/payload_signer_unittest.cc b/payload_generator/payload_signer_unittest.cc
index 62b6e7a..fc0925a 100644
--- a/payload_generator/payload_signer_unittest.cc
+++ b/payload_generator/payload_signer_unittest.cc
@@ -127,16 +127,14 @@
void DoWriteAndLoadPayloadTest(const PayloadGenerationConfig& config) {
PayloadFile payload;
payload.Init(config);
- string payload_path;
- EXPECT_TRUE(utils::MakeTempFile("payload.XXXXXX", &payload_path, nullptr));
- ScopedPathUnlinker payload_path_unlinker(payload_path);
+ test_utils::ScopedTempFile payload_file("payload.XXXXXX");
uint64_t metadata_size;
- EXPECT_TRUE(
- payload.WritePayload(payload_path, "/dev/null", "", &metadata_size));
+ EXPECT_TRUE(payload.WritePayload(
+ payload_file.path(), "/dev/null", "", &metadata_size));
brillo::Blob payload_metadata_blob;
DeltaArchiveManifest manifest;
uint64_t load_metadata_size, load_major_version;
- EXPECT_TRUE(PayloadSigner::LoadPayloadMetadata(payload_path,
+ EXPECT_TRUE(PayloadSigner::LoadPayloadMetadata(payload_file.path(),
&payload_metadata_blob,
&manifest,
&load_major_version,
@@ -215,50 +213,46 @@
}
TEST_F(PayloadSignerTest, SkipMetadataSignatureTest) {
- string payload_path;
- EXPECT_TRUE(utils::MakeTempFile("payload.XXXXXX", &payload_path, nullptr));
- ScopedPathUnlinker payload_path_unlinker(payload_path);
-
+ test_utils::ScopedTempFile payload_file("payload.XXXXXX");
PayloadGenerationConfig config;
config.version.major = kBrilloMajorPayloadVersion;
PayloadFile payload;
EXPECT_TRUE(payload.Init(config));
uint64_t metadata_size;
- EXPECT_TRUE(
- payload.WritePayload(payload_path, "/dev/null", "", &metadata_size));
+ EXPECT_TRUE(payload.WritePayload(
+ payload_file.path(), "/dev/null", "", &metadata_size));
const vector<int> sizes = {256};
brillo::Blob unsigned_payload_hash, unsigned_metadata_hash;
- EXPECT_TRUE(PayloadSigner::HashPayloadForSigning(
- payload_path, sizes, &unsigned_payload_hash, &unsigned_metadata_hash));
+ EXPECT_TRUE(PayloadSigner::HashPayloadForSigning(payload_file.path(),
+ sizes,
+ &unsigned_payload_hash,
+ &unsigned_metadata_hash));
EXPECT_TRUE(
- payload.WritePayload(payload_path,
+ payload.WritePayload(payload_file.path(),
"/dev/null",
GetBuildArtifactsPath(kUnittestPrivateKeyPath),
&metadata_size));
brillo::Blob signed_payload_hash, signed_metadata_hash;
EXPECT_TRUE(PayloadSigner::HashPayloadForSigning(
- payload_path, sizes, &signed_payload_hash, &signed_metadata_hash));
+ payload_file.path(), sizes, &signed_payload_hash, &signed_metadata_hash));
EXPECT_EQ(unsigned_payload_hash, signed_payload_hash);
EXPECT_EQ(unsigned_metadata_hash, signed_metadata_hash);
}
TEST_F(PayloadSignerTest, VerifySignedPayloadTest) {
- string payload_path;
- EXPECT_TRUE(utils::MakeTempFile("payload.XXXXXX", &payload_path, nullptr));
- ScopedPathUnlinker payload_path_unlinker(payload_path);
-
+ test_utils::ScopedTempFile payload_file("payload.XXXXXX");
PayloadGenerationConfig config;
config.version.major = kBrilloMajorPayloadVersion;
PayloadFile payload;
EXPECT_TRUE(payload.Init(config));
uint64_t metadata_size;
EXPECT_TRUE(
- payload.WritePayload(payload_path,
+ payload.WritePayload(payload_file.path(),
"/dev/null",
GetBuildArtifactsPath(kUnittestPrivateKeyPath),
&metadata_size));
EXPECT_TRUE(PayloadSigner::VerifySignedPayload(
- payload_path, GetBuildArtifactsPath(kUnittestPublicKeyPath)));
+ payload_file.path(), GetBuildArtifactsPath(kUnittestPublicKeyPath)));
}
} // namespace chromeos_update_engine