From ba9b73ef8f5f32c19cd77feddb2fd5858dfa76b2 Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Mon, 10 Feb 2020 08:15:14 +0900 Subject: [PATCH] Convert system/core/fs_mgr to Result::ok() No functionality changes. Test: m checkbuild Change-Id: I96b2adfe7aaea2b2e67307a3ce308455bc1d7a0a --- fs_mgr/libfs_avb/tests/util_test.cpp | 11 ++++------- fs_mgr/libvbmeta/builder.cpp | 20 ++++++++++---------- fs_mgr/libvbmeta/builder_test.cpp | 26 +++++++++++--------------- fs_mgr/libvbmeta/reader.cpp | 4 ++-- fs_mgr/libvbmeta/super_vbmeta_test.cpp | 16 ++++++++-------- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/fs_mgr/libfs_avb/tests/util_test.cpp b/fs_mgr/libfs_avb/tests/util_test.cpp index e64282bdc..5c388aaeb 100644 --- a/fs_mgr/libfs_avb/tests/util_test.cpp +++ b/fs_mgr/libfs_avb/tests/util_test.cpp @@ -232,8 +232,7 @@ TEST(BasicUtilTest, ListFiles) { // List files for comparison. auto result = ListFiles(test_dir.value()); - ASSERT_TRUE(result); - ASSERT_TRUE(result.has_value()); + ASSERT_RESULT_OK(result); auto files = result.value(); EXPECT_EQ(3UL, files.size()); // Sort them offline for comparison. @@ -266,8 +265,7 @@ TEST(BasicUtilTest, ListFilesShouldDiscardSymlink) { // List files for comparison. auto result = ListFiles(test_dir.value()); - ASSERT_TRUE(result); - ASSERT_TRUE(result.has_value()); + ASSERT_RESULT_OK(result); auto files = result.value(); EXPECT_EQ(2UL, files.size()); // Should not include the symlink file. // Sort them offline for comparison. @@ -287,7 +285,7 @@ TEST(BasicUtilTest, ListFilesOpenDirFailure) { base::FilePath no_such_dir = tmp_dir.Append("not_such_dir"); auto fail = ListFiles(no_such_dir.value()); - ASSERT_FALSE(fail); + ASSERT_FALSE(fail.ok()); EXPECT_EQ(ENOENT, fail.error().code()); EXPECT_TRUE(android::base::StartsWith(fail.error().message(), "Failed to opendir: ")); } @@ -303,8 +301,7 @@ TEST(BasicUtilTest, ListFilesEmptyDir) { // List files without sorting. auto result = ListFiles(test_dir.value()); - ASSERT_TRUE(result); - ASSERT_TRUE(result.has_value()); + ASSERT_RESULT_OK(result); auto files = result.value(); EXPECT_EQ(0UL, files.size()); diff --git a/fs_mgr/libvbmeta/builder.cpp b/fs_mgr/libvbmeta/builder.cpp index a901a4f8e..e6576cec0 100644 --- a/fs_mgr/libvbmeta/builder.cpp +++ b/fs_mgr/libvbmeta/builder.cpp @@ -40,18 +40,18 @@ SuperVBMetaBuilder::SuperVBMetaBuilder(const int super_vbmeta_fd, Result SuperVBMetaBuilder::Build() { for (const auto& [vbmeta_name, file_path] : images_path_) { Result content = ReadVBMetaImageFromFile(file_path); - if (!content) { + if (!content.ok()) { return content.error(); } Result vbmeta_index = AddVBMetaImage(vbmeta_name); - if (!vbmeta_index) { + if (!vbmeta_index.ok()) { return vbmeta_index.error(); } Result rv_export_vbmeta_image = ExportVBMetaImageToFile(vbmeta_index.value(), content.value()); - if (!rv_export_vbmeta_image) { + if (!rv_export_vbmeta_image.ok()) { return rv_export_vbmeta_image; } } @@ -65,7 +65,7 @@ Result SuperVBMetaBuilder::ReadVBMetaImageFromFile(const std::strin } Result file_size = GetFileSize(source_fd); - if (!file_size) { + if (!file_size.ok()) { return file_size.error(); } @@ -98,7 +98,7 @@ Result SuperVBMetaBuilder::AddVBMetaImage(const std::string& vbmeta_nam slot_number = desc->vbmeta_index; } else { Result new_slot = GetEmptySlot(); - if (!new_slot) { + if (!new_slot.ok()) { return new_slot; } slot_number = new_slot.value(); @@ -162,7 +162,7 @@ Result SuperVBMetaBuilder::ExportVBMetaTableToFile() { android::base::Result rv_write_primary_vbmeta_table = WritePrimaryVBMetaTable(super_vbmeta_fd_, serialized_table); - if (!rv_write_primary_vbmeta_table) { + if (!rv_write_primary_vbmeta_table.ok()) { return rv_write_primary_vbmeta_table; } @@ -175,7 +175,7 @@ Result SuperVBMetaBuilder::ExportVBMetaImageToFile(const uint8_t vbmeta_in const std::string& vbmeta_image) { Result rv_write_vbmeta_image = WriteVBMetaImage(super_vbmeta_fd_, vbmeta_index, vbmeta_image); - if (!rv_write_vbmeta_image) { + if (!rv_write_vbmeta_image.ok()) { return rv_write_vbmeta_image; } @@ -196,13 +196,13 @@ bool WriteToSuperVBMetaFile(const std::string& super_vbmeta_file, SuperVBMetaBuilder builder(super_vbmeta_fd, images_path); Result rv_build = builder.Build(); - if (!rv_build) { + if (!rv_build.ok()) { LERROR << rv_build.error(); return false; } Result rv_export = builder.ExportVBMetaTableToFile(); - if (!rv_export) { + if (!rv_export.ok()) { LERROR << rv_export.error(); return false; } @@ -211,4 +211,4 @@ bool WriteToSuperVBMetaFile(const std::string& super_vbmeta_file, } } // namespace fs_mgr -} // namespace android \ No newline at end of file +} // namespace android diff --git a/fs_mgr/libvbmeta/builder_test.cpp b/fs_mgr/libvbmeta/builder_test.cpp index 9a015fd36..487beceed 100644 --- a/fs_mgr/libvbmeta/builder_test.cpp +++ b/fs_mgr/libvbmeta/builder_test.cpp @@ -26,24 +26,20 @@ TEST(BuilderTest, VBMetaTableBasic) { std::unique_ptr builder = std::make_unique(); ASSERT_NE(builder, nullptr); - Result vbmeta_index = builder->AddVBMetaImage("vbmeta" /* vbmeta_name */ - ); - EXPECT_TRUE(vbmeta_index); + Result vbmeta_index = builder->AddVBMetaImage("vbmeta" /* vbmeta_name */); + EXPECT_RESULT_OK(vbmeta_index); - Result vbmeta_system_slot = builder->AddVBMetaImage("vbmeta_system" /* vbmeta_name */ - ); - EXPECT_TRUE(vbmeta_system_slot); + Result vbmeta_system_slot = builder->AddVBMetaImage("vbmeta_system" /* vbmeta_name */); + EXPECT_RESULT_OK(vbmeta_system_slot); - Result vbmeta_vendor_slot = builder->AddVBMetaImage("vbmeta_vendor" /* vbmeta_name */ - ); - EXPECT_TRUE(vbmeta_vendor_slot); + Result vbmeta_vendor_slot = builder->AddVBMetaImage("vbmeta_vendor" /* vbmeta_name */); + EXPECT_RESULT_OK(vbmeta_vendor_slot); - builder->DeleteVBMetaImage("vbmeta_system" /* vbmeta_name */ - ); + builder->DeleteVBMetaImage("vbmeta_system" /* vbmeta_name */); - Result vbmeta_product_slot = builder->AddVBMetaImage("vbmeta_product" /* vbmeta_name */ - ); - EXPECT_TRUE(vbmeta_product_slot); + Result vbmeta_product_slot = + builder->AddVBMetaImage("vbmeta_product" /* vbmeta_name */); + EXPECT_RESULT_OK(vbmeta_product_slot); std::unique_ptr table = builder->ExportVBMetaTable(); ASSERT_NE(table, nullptr); @@ -77,4 +73,4 @@ TEST(BuilderTest, VBMetaTableBasic) { for (int i = 0; i < sizeof(table->descriptors[2].reserved); i++) EXPECT_EQ(table->descriptors[2].reserved[i], 0); EXPECT_EQ(table->descriptors[2].vbmeta_name, "vbmeta_product"); -} \ No newline at end of file +} diff --git a/fs_mgr/libvbmeta/reader.cpp b/fs_mgr/libvbmeta/reader.cpp index 212d186ea..7b5ed9302 100644 --- a/fs_mgr/libvbmeta/reader.cpp +++ b/fs_mgr/libvbmeta/reader.cpp @@ -64,7 +64,7 @@ Result ReadVBMetaTable(int fd, uint64_t offset, VBMetaTable* table) { } Result rv_header = LoadAndVerifySuperVBMetaHeader(header_buffer.get(), &table->header); - if (!rv_header) { + if (!rv_header.ok()) { return rv_header; } @@ -104,7 +104,7 @@ Result ReadVBMetaImage(int fd, int slot) { Result ValidateVBMetaImage(int super_vbmeta_fd, int vbmeta_index, const std::string& vbmeta_image) { Result content = ReadVBMetaImage(super_vbmeta_fd, vbmeta_index); - if (!content) { + if (!content.ok()) { return content.error(); } diff --git a/fs_mgr/libvbmeta/super_vbmeta_test.cpp b/fs_mgr/libvbmeta/super_vbmeta_test.cpp index 6b4fc5d33..daed0d1b3 100644 --- a/fs_mgr/libvbmeta/super_vbmeta_test.cpp +++ b/fs_mgr/libvbmeta/super_vbmeta_test.cpp @@ -77,7 +77,7 @@ std::string ReadVBMetaImageFromFile(const std::string& file) { android::base::unique_fd fd(open(file.c_str(), O_RDONLY | O_CLOEXEC)); EXPECT_GT(fd, 0); Result file_size = GetFileSize(fd); - EXPECT_TRUE(file_size); + EXPECT_RESULT_OK(file_size); std::unique_ptr buffer = std::make_unique(VBMETA_IMAGE_MAX_SIZE); EXPECT_TRUE(android::base::ReadFully(fd, buffer.get(), file_size.value())); return std::string(reinterpret_cast(buffer.get()), VBMETA_IMAGE_MAX_SIZE); @@ -138,15 +138,15 @@ TEST(VBMetaTableTest, VBMetaTableBasic) { // Check the size of vbmeta table Result super_vbmeta_size = GetFileSize(fd); - EXPECT_TRUE(super_vbmeta_size); + EXPECT_RESULT_OK(super_vbmeta_size); EXPECT_EQ(super_vbmeta_size.value(), SUPER_VBMETA_TABLE_MAX_SIZE * 2 + VBMETA_IMAGE_MAX_SIZE * 3); // Check Primary vbmeta table is equal to Backup one VBMetaTable table; - EXPECT_TRUE(android::fs_mgr::ReadPrimaryVBMetaTable(fd, &table)); + EXPECT_RESULT_OK(android::fs_mgr::ReadPrimaryVBMetaTable(fd, &table)); VBMetaTable table_backup; - EXPECT_TRUE(android::fs_mgr::ReadBackupVBMetaTable(fd, &table_backup)); + EXPECT_RESULT_OK(android::fs_mgr::ReadBackupVBMetaTable(fd, &table_backup)); EXPECT_EQ(android::fs_mgr::SerializeVBMetaTable(table), android::fs_mgr::SerializeVBMetaTable(table_backup)); @@ -167,25 +167,25 @@ TEST(VBMetaTableTest, VBMetaTableBasic) { EXPECT_EQ(table.descriptors[0].vbmeta_name_length, 14); EXPECT_EQ(table.descriptors[0].vbmeta_name, "vbmeta_product"); Result vbmeta_product_content = ReadVBMetaImage(fd, 0); - EXPECT_TRUE(vbmeta_product_content); + EXPECT_RESULT_OK(vbmeta_product_content); EXPECT_EQ(ReadVBMetaImageFromFile(vbmeta_product_path), vbmeta_product_content.value()); EXPECT_EQ(table.descriptors[1].vbmeta_index, 1); EXPECT_EQ(table.descriptors[1].vbmeta_name_length, 13); EXPECT_EQ(table.descriptors[1].vbmeta_name, "vbmeta_system"); Result vbmeta_system_content = ReadVBMetaImage(fd, 1); - EXPECT_TRUE(vbmeta_system_content); + EXPECT_RESULT_OK(vbmeta_system_content); EXPECT_EQ(ReadVBMetaImageFromFile(vbmeta_system_path), vbmeta_system_content.value()); EXPECT_EQ(table.descriptors[2].vbmeta_index, 2); EXPECT_EQ(table.descriptors[2].vbmeta_name_length, 13); EXPECT_EQ(table.descriptors[2].vbmeta_name, "vbmeta_vendor"); Result vbmeta_vendor_content = ReadVBMetaImage(fd, 2); - EXPECT_TRUE(vbmeta_vendor_content); + EXPECT_RESULT_OK(vbmeta_vendor_content); EXPECT_EQ(ReadVBMetaImageFromFile(vbmeta_vendor_path), vbmeta_vendor_content.value()); } int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} \ No newline at end of file +}