From 5609bc8b34745b70d18916aa3b74f3272648e490 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 20 Jun 2018 00:30:48 -0700 Subject: applypatch: Refactor applypatch(). applypatch() was initially designed for file-based OTA, operating on individual files. It was later extended to allow patching eMMC targets as a whole, in favor of block-based updates. As we have deprecated file-based OTA since Oreo, part of the code in applypatch() has become obsolete. This CL refactors the related functions, by removing the obsolete logic and focusing on eMMC targets. Since this CL substantially changes applypatch APIs, it adds new functions to avoid unintentionally mixing them together. In particular, it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`, and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()` and `CheckPartition()`. It also replaces the old Edify functions `apply_patch()` and `apply_patch_check()` with `patch_partition()` and `patch_partition_check()` respectively. This CL requires matching changes to OTA generation script (in the same topic). Bug: 110106408 Test: Run recovery_unit_test and recovery_component_test on marlin. Test: `m dist` with non-A/B target. Verify /system/bin/install-recovery.sh on device. Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE. Verify /system/bin/install-recovery.sh on device. Test: Install an incremental OTA with the new updater and scripts. Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f --- tests/unit/applypatch_test.cpp | 121 ++++++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 56 deletions(-) (limited to 'tests/unit/applypatch_test.cpp') diff --git a/tests/unit/applypatch_test.cpp b/tests/unit/applypatch_test.cpp index fe00e1e66..4204074e7 100644 --- a/tests/unit/applypatch_test.cpp +++ b/tests/unit/applypatch_test.cpp @@ -38,6 +38,7 @@ #include "applypatch/applypatch.h" #include "common/test_constants.h" +#include "edify/expr.h" #include "otautil/paths.h" #include "otautil/print_sha1.h" @@ -58,12 +59,19 @@ class ApplyPatchTest : public ::testing::Test { target_size = recovery_fc.data.size(); target_sha1 = print_sha1(recovery_fc.sha1); + source_partition = Partition(source_file, source_size, source_sha1); + target_partition = Partition(partition_file.path, target_size, target_sha1); + srand(time(nullptr)); bad_sha1_a = android::base::StringPrintf("%040x", rand()); bad_sha1_b = android::base::StringPrintf("%040x", rand()); // Reset the cache backup file. - Paths::Get().set_cache_temp_source("/cache/saved.file"); + Paths::Get().set_cache_temp_source(cache_temp_source.path); + } + + void TearDown() override { + ASSERT_TRUE(android::base::RemoveFileIfExists(cache_temp_source.path)); } std::string source_file; @@ -76,74 +84,75 @@ class ApplyPatchTest : public ::testing::Test { std::string bad_sha1_a; std::string bad_sha1_b; + + Partition source_partition; + Partition target_partition; + + private: + TemporaryFile partition_file; + TemporaryFile cache_temp_source; }; -TEST_F(ApplyPatchTest, CheckMode) { - std::string partition = - "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + source_sha1; - ASSERT_EQ(0, applypatch_check(partition, {})); - ASSERT_EQ(0, applypatch_check(partition, { source_sha1 })); - ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, bad_sha1_b })); - ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, source_sha1, bad_sha1_b })); +TEST_F(ApplyPatchTest, CheckPartition) { + ASSERT_TRUE(CheckPartition(source_partition)); } -TEST_F(ApplyPatchTest, CheckMode_NonEmmcTarget) { - ASSERT_NE(0, applypatch_check(source_file, {})); - ASSERT_NE(0, applypatch_check(source_file, { source_sha1 })); - ASSERT_NE(0, applypatch_check(source_file, { bad_sha1_a, bad_sha1_b })); - ASSERT_NE(0, applypatch_check(source_file, { bad_sha1_a, source_sha1, bad_sha1_b })); +TEST_F(ApplyPatchTest, CheckPartition_Mismatching) { + ASSERT_FALSE(CheckPartition(Partition(source_file, target_size, target_sha1))); + ASSERT_FALSE(CheckPartition(Partition(source_file, source_size, bad_sha1_a))); + + ASSERT_FALSE(CheckPartition(Partition(source_file, source_size - 1, source_sha1))); + ASSERT_FALSE(CheckPartition(Partition(source_file, source_size + 1, source_sha1))); } -TEST_F(ApplyPatchTest, CheckMode_EmmcTarget) { - // EMMC:source_file:size:sha1 should pass the check. - std::string src_file = - "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + source_sha1; - ASSERT_EQ(0, applypatch_check(src_file, {})); - - // EMMC:source_file:(size-1):sha1:(size+1):sha1 should fail the check. - src_file = "EMMC:" + source_file + ":" + std::to_string(source_size - 1) + ":" + source_sha1 + - ":" + std::to_string(source_size + 1) + ":" + source_sha1; - ASSERT_NE(0, applypatch_check(src_file, {})); - - // EMMC:source_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. - src_file = "EMMC:" + source_file + ":" + std::to_string(source_size - 1) + ":" + source_sha1 + - ":" + std::to_string(source_size) + ":" + source_sha1 + ":" + - std::to_string(source_size + 1) + ":" + source_sha1; - ASSERT_EQ(0, applypatch_check(src_file, {})); - - // EMMC:source_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check. - src_file = "EMMC:" + source_file + ":" + std::to_string(source_size + 1) + ":" + source_sha1 + - ":" + std::to_string(source_size - 1) + ":" + source_sha1 + ":" + - std::to_string(source_size) + ":" + source_sha1; - ASSERT_EQ(0, applypatch_check(src_file, {})); - - // EMMC:target_file:(size+1):source_sha1:(size-1):source_sha1:size:source_sha1:size:target_sha1 - // should pass the check. - src_file = "EMMC:" + target_file + ":" + std::to_string(source_size + 1) + ":" + source_sha1 + - ":" + std::to_string(source_size - 1) + ":" + source_sha1 + ":" + - std::to_string(source_size) + ":" + source_sha1 + ":" + std::to_string(target_size) + - ":" + target_sha1; - ASSERT_EQ(0, applypatch_check(src_file, {})); +TEST_F(ApplyPatchTest, PatchPartitionCheck) { + ASSERT_TRUE(PatchPartitionCheck(target_partition, source_partition)); + + ASSERT_TRUE( + PatchPartitionCheck(Partition(source_file, source_size - 1, source_sha1), source_partition)); + + ASSERT_TRUE( + PatchPartitionCheck(Partition(source_file, source_size + 1, source_sha1), source_partition)); } -TEST_F(ApplyPatchTest, CheckMode_UseBackup) { - std::string corrupted = - "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + bad_sha1_a; - ASSERT_NE(0, applypatch_check(corrupted, { source_sha1 })); +TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup) { + ASSERT_FALSE( + PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1))); Paths::Get().set_cache_temp_source(source_file); - ASSERT_EQ(0, applypatch_check(corrupted, { source_sha1 })); - ASSERT_EQ(0, applypatch_check(corrupted, { bad_sha1_a, source_sha1, bad_sha1_b })); + ASSERT_TRUE( + PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1))); } -TEST_F(ApplyPatchTest, CheckMode_UseBackup_BothCorrupted) { - std::string corrupted = - "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + bad_sha1_a; - ASSERT_NE(0, applypatch_check(corrupted, {})); - ASSERT_NE(0, applypatch_check(corrupted, { source_sha1 })); +TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup_BothCorrupted) { + ASSERT_FALSE( + PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1))); - Paths::Get().set_cache_temp_source(source_file); - ASSERT_NE(0, applypatch_check(corrupted, { bad_sha1_a, bad_sha1_b })); + Paths::Get().set_cache_temp_source(target_file); + ASSERT_FALSE( + PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1))); +} + +TEST_F(ApplyPatchTest, PatchPartition) { + FileContents patch_fc; + ASSERT_EQ(0, LoadFileContents(from_testdata_base("recovery-from-boot.p"), &patch_fc)); + Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend())); + + FileContents bonus_fc; + ASSERT_EQ(0, LoadFileContents(from_testdata_base("bonus.file"), &bonus_fc)); + Value bonus(Value::Type::BLOB, std::string(bonus_fc.data.cbegin(), bonus_fc.data.cend())); + + ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, &bonus)); +} + +// Tests patching an eMMC target without a separate bonus file (i.e. recovery-from-boot patch has +// everything). +TEST_F(ApplyPatchTest, PatchPartitionWithoutBonusFile) { + FileContents patch_fc; + ASSERT_EQ(0, LoadFileContents(from_testdata_base("recovery-from-boot-with-bonus.p"), &patch_fc)); + Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend())); + + ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, nullptr)); } class FreeCacheTest : public ::testing::Test { -- cgit v1.2.3