diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Android.bp | 28 | ||||
-rw-r--r-- | tests/component/updater_test.cpp | 22 | ||||
-rw-r--r-- | tests/unit/commands_test.cpp | 172 |
3 files changed, 193 insertions, 29 deletions
diff --git a/tests/Android.bp b/tests/Android.bp index d305e2560..dc20f3326 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -108,15 +108,7 @@ cc_test { test_suites: ["device-tests"], srcs: [ - "unit/asn1_decoder_test.cpp", - "unit/applypatch_test.cpp", - "unit/commands_test.cpp", - "unit/dirutil_test.cpp", - "unit/locale_test.cpp", - "unit/rangeset_test.cpp", - "unit/screen_ui_test.cpp", - "unit/sysutil_test.cpp", - "unit/zip_test.cpp", + "unit/*.cpp", ], static_libs: libapplypatch_static_libs + [ @@ -161,16 +153,7 @@ cc_test { test_suites: ["device-tests"], srcs: [ - "component/bootloader_message_test.cpp", - "component/edify_test.cpp", - "component/imgdiff_test.cpp", - "component/install_test.cpp", - "component/resources_test.cpp", - "component/sideload_test.cpp", - "component/uncrypt_test.cpp", - "component/updater_test.cpp", - "component/update_verifier_test.cpp", - "component/verifier_test.cpp", + "component/*.cpp", ], static_libs: libapplypatch_static_libs + librecovery_static_libs + [ @@ -215,4 +198,11 @@ cc_test_host { ], data: ["testdata/*"], + + target: { + darwin: { + // libimgdiff is not available on the Mac. + enabled: false, + }, + }, } diff --git a/tests/component/updater_test.cpp b/tests/component/updater_test.cpp index 248b469b0..2ed797e62 100644 --- a/tests/component/updater_test.cpp +++ b/tests/component/updater_test.cpp @@ -56,8 +56,6 @@ using namespace std::string_literals; using PackageEntries = std::unordered_map<std::string, std::string>; -static constexpr size_t kTransferListHeaderLines = 4; - struct selabel_handle* sehandle = nullptr; static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code, @@ -846,7 +844,8 @@ TEST_F(UpdaterTest, last_command_update) { }; // "2\nstash " + block3_hash + " 2,2,3" - std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2]; + std::string last_command_content = + "2\n" + transfer_list_fail[TransferList::kTransferListHeaderLines + 2]; RunBlockImageUpdate(false, entries, image_file_, ""); @@ -895,7 +894,8 @@ TEST_F(UpdaterTest, last_command_update_unresumable) { ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_)); - std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines]; + std::string last_command_content = + "0\n" + transfer_list_unresumable[TransferList::kTransferListHeaderLines]; ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_)); RunBlockImageUpdate(false, entries, image_file_, ""); @@ -934,7 +934,8 @@ TEST_F(UpdaterTest, last_command_verify) { ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_)); // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1" - std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2]; + std::string last_command_content = + "2\n" + transfer_list_verify[TransferList::kTransferListHeaderLines + 2]; // First run: expect the verification to succeed and the last_command_file is intact. ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_)); @@ -1129,16 +1130,17 @@ static const std::vector<std::string> g_transfer_list = GenerateTransferList(); INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest, ::testing::Range(static_cast<size_t>(0), - g_transfer_list.size() - kTransferListHeaderLines)); + g_transfer_list.size() - + TransferList::kTransferListHeaderLines)); TEST_P(ResumableUpdaterTest, InterruptVerifyResume) { ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_)); LOG(INFO) << "Interrupting at line " << index_ << " (" - << g_transfer_list[kTransferListHeaderLines + index_] << ")"; + << g_transfer_list[TransferList::kTransferListHeaderLines + index_] << ")"; std::vector<std::string> transfer_list_copy{ g_transfer_list }; - transfer_list_copy[kTransferListHeaderLines + index_] = "abort"; + transfer_list_copy[TransferList::kTransferListHeaderLines + index_] = "abort"; g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n'); @@ -1151,8 +1153,8 @@ TEST_P(ResumableUpdaterTest, InterruptVerifyResume) { if (index_ == 0) { ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); } else { - last_command_expected = - std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1]; + last_command_expected = std::to_string(index_ - 1) + "\n" + + g_transfer_list[TransferList::kTransferListHeaderLines + index_ - 1]; std::string last_command_actual; ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual)); ASSERT_EQ(last_command_expected, last_command_actual); diff --git a/tests/unit/commands_test.cpp b/tests/unit/commands_test.cpp index 19841d676..8a54df703 100644 --- a/tests/unit/commands_test.cpp +++ b/tests/unit/commands_test.cpp @@ -14,10 +14,14 @@ * limitations under the License. */ +#include <algorithm> #include <string> +#include <android-base/strings.h> #include <gtest/gtest.h> +#include <openssl/sha.h> +#include "otautil/print_sha1.h" #include "otautil/rangeset.h" #include "private/commands.h" @@ -380,3 +384,171 @@ TEST(CommandsTest, Parse_InvalidNumberOfArgs) { ASSERT_FALSE(Command::Parse(input, 0, &err)); } } + +TEST(SourceInfoTest, Overlaps) { + ASSERT_TRUE(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } }), {}, {}) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } })))); + + ASSERT_TRUE(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } }), {}, {}) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 4, 7 }, { 16, 23 } })))); + + ASSERT_FALSE(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } }), {}, {}) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 9, 16 } })))); +} + +TEST(SourceInfoTest, Overlaps_EmptySourceOrTarget) { + ASSERT_FALSE(SourceInfo().Overlaps(TargetInfo())); + + ASSERT_FALSE(SourceInfo().Overlaps( + TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", RangeSet({ { 7, 9 }, { 16, 20 } })))); + + ASSERT_FALSE(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 7, 9 }, { 16, 20 } }), {}, {}) + .Overlaps(TargetInfo())); +} + +TEST(SourceInfoTest, Overlaps_WithStashes) { + ASSERT_FALSE(SourceInfo("a6cbdf3f416960f02189d3a814ec7e9e95c44a0d", + RangeSet({ { 81, 175 }, { 265, 266 } }), // source ranges + RangeSet({ { 0, 94 }, { 95, 96 } }), // source location + { StashInfo("9eedf00d11061549e32503cadf054ec6fbfa7a23", + RangeSet({ { 94, 95 } })) }) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 175, 265 } })))); + + ASSERT_TRUE(SourceInfo("a6cbdf3f416960f02189d3a814ec7e9e95c44a0d", + RangeSet({ { 81, 175 }, { 265, 266 } }), // source ranges + RangeSet({ { 0, 94 }, { 95, 96 } }), // source location + { StashInfo("9eedf00d11061549e32503cadf054ec6fbfa7a23", + RangeSet({ { 94, 95 } })) }) + .Overlaps(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb", + RangeSet({ { 265, 266 } })))); +} + +// The block size should be specified by the caller of ReadAll (i.e. from Command instance during +// normal run). +constexpr size_t kBlockSize = 4096; + +TEST(SourceInfoTest, ReadAll) { + // "2727756cfee3fbfe24bf5650123fd7743d7b3465" is the SHA-1 hex digest of 8192 * 'a'. + const SourceInfo source("2727756cfee3fbfe24bf5650123fd7743d7b3465", RangeSet({ { 0, 2 } }), {}, + {}); + auto block_reader = [](const RangeSet& src, std::vector<uint8_t>* block_buffer) -> int { + std::fill_n(block_buffer->begin(), src.blocks() * kBlockSize, 'a'); + return 0; + }; + auto stash_reader = [](const std::string&, std::vector<uint8_t>*) -> int { return 0; }; + std::vector<uint8_t> buffer(source.blocks() * kBlockSize); + ASSERT_TRUE(source.ReadAll(&buffer, kBlockSize, block_reader, stash_reader)); + ASSERT_EQ(source.blocks() * kBlockSize, buffer.size()); + + uint8_t digest[SHA_DIGEST_LENGTH]; + SHA1(buffer.data(), buffer.size(), digest); + ASSERT_EQ(source.hash(), print_sha1(digest)); +} + +TEST(SourceInfoTest, ReadAll_WithStashes) { + const SourceInfo source( + // SHA-1 hex digest of 8192 * 'a' + 4096 * 'b'. + "ee3ebea26130769c10ad13604712100346d48660", RangeSet({ { 0, 2 } }), RangeSet({ { 0, 2 } }), + { StashInfo("1e41f7a59e80c6eb4dc043caae80d273f130bed8", RangeSet({ { 2, 3 } })) }); + auto block_reader = [](const RangeSet& src, std::vector<uint8_t>* block_buffer) -> int { + std::fill_n(block_buffer->begin(), src.blocks() * kBlockSize, 'a'); + return 0; + }; + auto stash_reader = [](const std::string&, std::vector<uint8_t>* stash_buffer) -> int { + std::fill_n(stash_buffer->begin(), kBlockSize, 'b'); + return 0; + }; + std::vector<uint8_t> buffer(source.blocks() * kBlockSize); + ASSERT_TRUE(source.ReadAll(&buffer, kBlockSize, block_reader, stash_reader)); + ASSERT_EQ(source.blocks() * kBlockSize, buffer.size()); + + uint8_t digest[SHA_DIGEST_LENGTH]; + SHA1(buffer.data(), buffer.size(), digest); + ASSERT_EQ(source.hash(), print_sha1(digest)); +} + +TEST(SourceInfoTest, ReadAll_BufferTooSmall) { + const SourceInfo source("2727756cfee3fbfe24bf5650123fd7743d7b3465", RangeSet({ { 0, 2 } }), {}, + {}); + auto block_reader = [](const RangeSet&, std::vector<uint8_t>*) -> int { return 0; }; + auto stash_reader = [](const std::string&, std::vector<uint8_t>*) -> int { return 0; }; + std::vector<uint8_t> buffer(source.blocks() * kBlockSize - 1); + ASSERT_FALSE(source.ReadAll(&buffer, kBlockSize, block_reader, stash_reader)); +} + +TEST(SourceInfoTest, ReadAll_FailingReader) { + const SourceInfo source( + "ee3ebea26130769c10ad13604712100346d48660", RangeSet({ { 0, 2 } }), RangeSet({ { 0, 2 } }), + { StashInfo("1e41f7a59e80c6eb4dc043caae80d273f130bed8", RangeSet({ { 2, 3 } })) }); + std::vector<uint8_t> buffer(source.blocks() * kBlockSize); + auto failing_block_reader = [](const RangeSet&, std::vector<uint8_t>*) -> int { return -1; }; + auto stash_reader = [](const std::string&, std::vector<uint8_t>*) -> int { return 0; }; + ASSERT_FALSE(source.ReadAll(&buffer, kBlockSize, failing_block_reader, stash_reader)); + + auto block_reader = [](const RangeSet&, std::vector<uint8_t>*) -> int { return 0; }; + auto failing_stash_reader = [](const std::string&, std::vector<uint8_t>*) -> int { return -1; }; + ASSERT_FALSE(source.ReadAll(&buffer, kBlockSize, block_reader, failing_stash_reader)); +} + +TEST(TransferListTest, Parse) { + std::vector<std::string> input_lines{ + "4", // version + "2", // total blocks + "1", // max stashed entries + "1", // max stashed blocks + "stash 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1", + "move 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1 1 2,0,1", + }; + + std::string err; + TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err); + ASSERT_TRUE(static_cast<bool>(transfer_list)); + ASSERT_EQ(4, transfer_list.version()); + ASSERT_EQ(2, transfer_list.total_blocks()); + ASSERT_EQ(1, transfer_list.stash_max_entries()); + ASSERT_EQ(1, transfer_list.stash_max_blocks()); + ASSERT_EQ(2U, transfer_list.commands().size()); + ASSERT_EQ(Command::Type::STASH, transfer_list.commands()[0].type()); + ASSERT_EQ(Command::Type::MOVE, transfer_list.commands()[1].type()); +} + +TEST(TransferListTest, Parse_InvalidCommand) { + std::vector<std::string> input_lines{ + "4", // version + "2", // total blocks + "1", // max stashed entries + "1", // max stashed blocks + "stash 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1", + "move 1d74d1a60332fd38cf9405f1bae67917888da6cb 2,0,1 1", + }; + + std::string err; + TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err); + ASSERT_FALSE(static_cast<bool>(transfer_list)); +} + +TEST(TransferListTest, Parse_ZeroTotalBlocks) { + std::vector<std::string> input_lines{ + "4", // version + "0", // total blocks + "0", // max stashed entries + "0", // max stashed blocks + }; + + std::string err; + TransferList transfer_list = TransferList::Parse(android::base::Join(input_lines, '\n'), &err); + ASSERT_TRUE(static_cast<bool>(transfer_list)); + ASSERT_EQ(4, transfer_list.version()); + ASSERT_EQ(0, transfer_list.total_blocks()); + ASSERT_EQ(0, transfer_list.stash_max_entries()); + ASSERT_EQ(0, transfer_list.stash_max_blocks()); + ASSERT_TRUE(transfer_list.commands().empty()); +} |