From c3a161e2b8232a887efe73abe51ea9071052555d Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Thu, 20 Jun 2019 18:16:49 -0700 Subject: Add unit tests for simulator Make sure the simulator succeeds executing common non-A/B update functions. Bug: 131911365 Test: run unit tests Change-Id: I520ce6a8827539b88a9e36f9e67eec30d8b586d4 --- tests/Android.bp | 16 +- tests/unit/host/imgdiff_test.cpp | 1114 +++++++++++++++++++++++++++++ tests/unit/host/update_simulator_test.cpp | 403 +++++++++++ tests/unit/imgdiff_test.cpp | 1114 ----------------------------- updater/target_files.cpp | 5 + 5 files changed, 1528 insertions(+), 1124 deletions(-) create mode 100644 tests/unit/host/imgdiff_test.cpp create mode 100644 tests/unit/host/update_simulator_test.cpp delete mode 100644 tests/unit/imgdiff_test.cpp diff --git a/tests/Android.bp b/tests/Android.bp index 4969c087b..fec2f07de 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -157,26 +157,22 @@ cc_test_host { defaults: [ "recovery_test_defaults", + "libupdater_defaults", ], srcs: [ - "unit/imgdiff_test.cpp", + "unit/host/*", ], static_libs: [ + "libupdater_host", + "libupdater_core", "libimgdiff", - "libimgpatch", - "libotautil", "libbsdiff", - "libbspatch", - "libziparchive", - "libutils", - "libcrypto", - "libbrotli", - "libbz", "libdivsufsort64", "libdivsufsort", - "libz", + "libfstab", + "libc++fs", ], test_suites: ["general-tests"], diff --git a/tests/unit/host/imgdiff_test.cpp b/tests/unit/host/imgdiff_test.cpp new file mode 100644 index 000000000..e76ccbdfb --- /dev/null +++ b/tests/unit/host/imgdiff_test.cpp @@ -0,0 +1,1114 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common/test_constants.h" + +using android::base::get_unaligned; + +// Sanity check for the given imgdiff patch header. +static void verify_patch_header(const std::string& patch, size_t* num_normal, size_t* num_raw, + size_t* num_deflate) { + const size_t size = patch.size(); + const char* data = patch.data(); + + ASSERT_GE(size, 12U); + ASSERT_EQ("IMGDIFF2", std::string(data, 8)); + + const int num_chunks = get_unaligned(data + 8); + ASSERT_GE(num_chunks, 0); + + size_t normal = 0; + size_t raw = 0; + size_t deflate = 0; + + size_t pos = 12; + for (int i = 0; i < num_chunks; ++i) { + ASSERT_LE(pos + 4, size); + int type = get_unaligned(data + pos); + pos += 4; + if (type == CHUNK_NORMAL) { + pos += 24; + ASSERT_LE(pos, size); + normal++; + } else if (type == CHUNK_RAW) { + ASSERT_LE(pos + 4, size); + ssize_t data_len = get_unaligned(data + pos); + ASSERT_GT(data_len, 0); + pos += 4 + data_len; + ASSERT_LE(pos, size); + raw++; + } else if (type == CHUNK_DEFLATE) { + pos += 60; + ASSERT_LE(pos, size); + deflate++; + } else { + FAIL() << "Invalid patch type: " << type; + } + } + + if (num_normal != nullptr) *num_normal = normal; + if (num_raw != nullptr) *num_raw = raw; + if (num_deflate != nullptr) *num_deflate = deflate; +} + +static void GenerateTarget(const std::string& src, const std::string& patch, std::string* patched) { + patched->clear(); + ASSERT_EQ(0, ApplyImagePatch(reinterpret_cast(src.data()), src.size(), + reinterpret_cast(patch.data()), patch.size(), + [&](const unsigned char* data, size_t len) { + patched->append(reinterpret_cast(data), len); + return len; + })); +} + +static void verify_patched_image(const std::string& src, const std::string& patch, + const std::string& tgt) { + std::string patched; + GenerateTarget(src, patch, &patched); + ASSERT_EQ(tgt, patched); +} + +TEST(ImgdiffTest, invalid_args) { + // Insufficient inputs. + ASSERT_EQ(2, imgdiff(1, (const char* []){ "imgdiff" })); + ASSERT_EQ(2, imgdiff(2, (const char* []){ "imgdiff", "-z" })); + ASSERT_EQ(2, imgdiff(2, (const char* []){ "imgdiff", "-b" })); + ASSERT_EQ(2, imgdiff(3, (const char* []){ "imgdiff", "-z", "-b" })); + + // Failed to read bonus file. + ASSERT_EQ(1, imgdiff(3, (const char* []){ "imgdiff", "-b", "doesntexist" })); + + // Failed to read input files. + ASSERT_EQ(1, imgdiff(4, (const char* []){ "imgdiff", "doesntexist", "doesntexist", "output" })); + ASSERT_EQ( + 1, imgdiff(5, (const char* []){ "imgdiff", "-z", "doesntexist", "doesntexist", "output" })); +} + +TEST(ImgdiffTest, image_mode_smoke) { + // Random bytes. + const std::string src("abcdefg"); + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + const std::string tgt("abcdefgxyz"); + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect one CHUNK_RAW entry. + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(0U, num_deflate); + ASSERT_EQ(1U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, zip_mode_smoke_store) { + // Construct src and tgt zip files. + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + ASSERT_EQ(0, src_writer.StartEntry("file1.txt", 0)); // Store mode. + const std::string src_content("abcdefg"); + ASSERT_EQ(0, src_writer.WriteBytes(src_content.data(), src_content.size())); + ASSERT_EQ(0, src_writer.FinishEntry()); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + ASSERT_EQ(0, tgt_writer.StartEntry("file1.txt", 0)); // Store mode. + const std::string tgt_content("abcdefgxyz"); + ASSERT_EQ(0, tgt_writer.WriteBytes(tgt_content.data(), tgt_content.size())); + ASSERT_EQ(0, tgt_writer.FinishEntry()); + ASSERT_EQ(0, tgt_writer.Finish()); + ASSERT_EQ(0, fclose(tgt_file_ptr)); + + // Compute patch. + TemporaryFile patch_file; + std::vector args = { + "imgdiff", "-z", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + std::string src; + ASSERT_TRUE(android::base::ReadFileToString(src_file.path, &src)); + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect one CHUNK_RAW entry. + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(0U, num_deflate); + ASSERT_EQ(1U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, zip_mode_smoke_compressed) { + // Generate 1 block of random data. + std::string random_data; + random_data.reserve(4096); + generate_n(back_inserter(random_data), 4096, []() { return rand() % 256; }); + + // Construct src and tgt zip files. + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + ASSERT_EQ(0, src_writer.StartEntry("file1.txt", ZipWriter::kCompress)); + const std::string src_content = random_data; + ASSERT_EQ(0, src_writer.WriteBytes(src_content.data(), src_content.size())); + ASSERT_EQ(0, src_writer.FinishEntry()); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + ASSERT_EQ(0, tgt_writer.StartEntry("file1.txt", ZipWriter::kCompress)); + const std::string tgt_content = random_data + "extra contents"; + ASSERT_EQ(0, tgt_writer.WriteBytes(tgt_content.data(), tgt_content.size())); + ASSERT_EQ(0, tgt_writer.FinishEntry()); + ASSERT_EQ(0, tgt_writer.Finish()); + ASSERT_EQ(0, fclose(tgt_file_ptr)); + + // Compute patch. + TemporaryFile patch_file; + std::vector args = { + "imgdiff", "-z", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + std::string src; + ASSERT_TRUE(android::base::ReadFileToString(src_file.path, &src)); + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect three entries: CHUNK_RAW (header) + CHUNK_DEFLATE (data) + CHUNK_RAW (footer). + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(1U, num_deflate); + ASSERT_EQ(2U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, zip_mode_empty_target) { + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + ASSERT_EQ(0, src_writer.StartEntry("file1.txt", ZipWriter::kCompress)); + const std::string src_content = "abcdefg"; + ASSERT_EQ(0, src_writer.WriteBytes(src_content.data(), src_content.size())); + ASSERT_EQ(0, src_writer.FinishEntry()); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + // Construct a empty entry in the target zip. + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + ASSERT_EQ(0, tgt_writer.StartEntry("file1.txt", ZipWriter::kCompress)); + const std::string tgt_content; + ASSERT_EQ(0, tgt_writer.WriteBytes(tgt_content.data(), tgt_content.size())); + ASSERT_EQ(0, tgt_writer.FinishEntry()); + ASSERT_EQ(0, tgt_writer.Finish()); + + // Compute patch. + TemporaryFile patch_file; + std::vector args = { + "imgdiff", "-z", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + std::string src; + ASSERT_TRUE(android::base::ReadFileToString(src_file.path, &src)); + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, zip_mode_smoke_trailer_zeros) { + // Generate 1 block of random data. + std::string random_data; + random_data.reserve(4096); + generate_n(back_inserter(random_data), 4096, []() { return rand() % 256; }); + + // Construct src and tgt zip files. + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + ASSERT_EQ(0, src_writer.StartEntry("file1.txt", ZipWriter::kCompress)); + const std::string src_content = random_data; + ASSERT_EQ(0, src_writer.WriteBytes(src_content.data(), src_content.size())); + ASSERT_EQ(0, src_writer.FinishEntry()); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + ASSERT_EQ(0, tgt_writer.StartEntry("file1.txt", ZipWriter::kCompress)); + const std::string tgt_content = random_data + "abcdefg"; + ASSERT_EQ(0, tgt_writer.WriteBytes(tgt_content.data(), tgt_content.size())); + ASSERT_EQ(0, tgt_writer.FinishEntry()); + ASSERT_EQ(0, tgt_writer.Finish()); + // Add trailing zeros to the target zip file. + std::vector zeros(10); + ASSERT_EQ(zeros.size(), fwrite(zeros.data(), sizeof(uint8_t), zeros.size(), tgt_file_ptr)); + ASSERT_EQ(0, fclose(tgt_file_ptr)); + + // Compute patch. + TemporaryFile patch_file; + std::vector args = { + "imgdiff", "-z", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + std::string src; + ASSERT_TRUE(android::base::ReadFileToString(src_file.path, &src)); + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect three entries: CHUNK_RAW (header) + CHUNK_DEFLATE (data) + CHUNK_RAW (footer). + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(1U, num_deflate); + ASSERT_EQ(2U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, image_mode_simple) { + std::string gzipped_source_path = from_testdata_base("gzipped_source"); + std::string gzipped_source; + ASSERT_TRUE(android::base::ReadFileToString(gzipped_source_path, &gzipped_source)); + + const std::string src = "abcdefg" + gzipped_source; + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + std::string gzipped_target_path = from_testdata_base("gzipped_target"); + std::string gzipped_target; + ASSERT_TRUE(android::base::ReadFileToString(gzipped_target_path, &gzipped_target)); + const std::string tgt = "abcdefgxyz" + gzipped_target; + + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect three entries: CHUNK_RAW (header) + CHUNK_DEFLATE (data) + CHUNK_RAW (footer). + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(1U, num_deflate); + ASSERT_EQ(2U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, image_mode_bad_gzip) { + // Modify the uncompressed length in the gzip footer. + const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', '\x1f', '\x8b', '\x08', '\x00', '\xc4', '\x1e', + '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xac', + '\x02', '\x00', '\x67', '\xba', '\x8e', '\xeb', '\x03', + '\xff', '\xff', '\xff' }; + const std::string src(src_data.cbegin(), src_data.cend()); + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + // Modify the uncompressed length in the gzip footer. + const std::vector tgt_data = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z', '\x1f', '\x8b', + '\x08', '\x00', '\x62', '\x1f', '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xa8', '\xac', + '\xac', '\xaa', '\x02', '\x00', '\x96', '\x30', '\x06', '\xb7', '\x06', '\xff', '\xff', '\xff' + }; + const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, image_mode_different_num_chunks) { + // src: "abcdefgh" + gzipped "xyz" (echo -n "xyz" | gzip -f | hd) + gzipped "test". + const std::vector src_data = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\x1f', '\x8b', '\x08', + '\x00', '\xc4', '\x1e', '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xac', '\x02', + '\x00', '\x67', '\xba', '\x8e', '\xeb', '\x03', '\x00', '\x00', '\x00', '\x1f', '\x8b', + '\x08', '\x00', '\xb2', '\x3a', '\x53', '\x58', '\x00', '\x03', '\x2b', '\x49', '\x2d', + '\x2e', '\x01', '\x00', '\x0c', '\x7e', '\x7f', '\xd8', '\x04', '\x00', '\x00', '\x00' + }; + const std::string src(src_data.cbegin(), src_data.cend()); + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + // tgt: "abcdefgxyz" + gzipped "xxyyzz". + const std::vector tgt_data = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z', '\x1f', '\x8b', + '\x08', '\x00', '\x62', '\x1f', '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xa8', '\xac', + '\xac', '\xaa', '\x02', '\x00', '\x96', '\x30', '\x06', '\xb7', '\x06', '\x00', '\x00', '\x00' + }; + const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(1, imgdiff(args.size(), args.data())); +} + +TEST(ImgdiffTest, image_mode_merge_chunks) { + // src: "abcdefg" + gzipped_source. + std::string gzipped_source_path = from_testdata_base("gzipped_source"); + std::string gzipped_source; + ASSERT_TRUE(android::base::ReadFileToString(gzipped_source_path, &gzipped_source)); + + const std::string src = "abcdefg" + gzipped_source; + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + // tgt: gzipped_target + "abcdefgxyz". + std::string gzipped_target_path = from_testdata_base("gzipped_target"); + std::string gzipped_target; + ASSERT_TRUE(android::base::ReadFileToString(gzipped_target_path, &gzipped_target)); + + const std::string tgt = gzipped_target + "abcdefgxyz"; + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + // Since a gzipped entry will become CHUNK_RAW (header) + CHUNK_DEFLATE (data) + + // CHUNK_RAW (footer), they both should contain the same chunk types after merging. + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect three entries: CHUNK_RAW (header) + CHUNK_DEFLATE (data) + CHUNK_RAW (footer). + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(1U, num_deflate); + ASSERT_EQ(2U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, image_mode_spurious_magic) { + // src: "abcdefgh" + '0x1f8b0b00' + some bytes. + const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', '\x1f', '\x8b', '\x08', '\x00', '\xc4', '\x1e', + '\x53', '\x58', 't', 'e', 's', 't' }; + const std::string src(src_data.cbegin(), src_data.cend()); + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + // tgt: "abcdefgxyz". + const std::vector tgt_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z' }; + const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect one CHUNK_RAW (header) entry. + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(0U, num_deflate); + ASSERT_EQ(1U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, image_mode_short_input1) { + // src: "abcdefgh" + '0x1f8b0b'. + const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', '\x1f', '\x8b', '\x08' }; + const std::string src(src_data.cbegin(), src_data.cend()); + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + // tgt: "abcdefgxyz". + const std::vector tgt_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z' }; + const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect one CHUNK_RAW (header) entry. + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(0U, num_deflate); + ASSERT_EQ(1U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, image_mode_short_input2) { + // src: "abcdefgh" + '0x1f8b0b00'. + const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', '\x1f', '\x8b', '\x08', '\x00' }; + const std::string src(src_data.cbegin(), src_data.cend()); + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + // tgt: "abcdefgxyz". + const std::vector tgt_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z' }; + const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect one CHUNK_RAW (header) entry. + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(0U, num_normal); + ASSERT_EQ(0U, num_deflate); + ASSERT_EQ(1U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgdiffTest, image_mode_single_entry_long) { + // src: "abcdefgh" + '0x1f8b0b00' + some bytes. + const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', '\x1f', '\x8b', '\x08', '\x00', '\xc4', '\x1e', + '\x53', '\x58', 't', 'e', 's', 't' }; + const std::string src(src_data.cbegin(), src_data.cend()); + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + // tgt: "abcdefgxyz" + 200 bytes. + std::vector tgt_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z' }; + tgt_data.resize(tgt_data.size() + 200); + + const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + + // Expect one CHUNK_NORMAL entry, since it's exceeding the 160-byte limit for RAW. + size_t num_normal; + size_t num_raw; + size_t num_deflate; + verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); + ASSERT_EQ(1U, num_normal); + ASSERT_EQ(0U, num_deflate); + ASSERT_EQ(0U, num_raw); + + verify_patched_image(src, patch, tgt); +} + +TEST(ImgpatchTest, image_mode_patch_corruption) { + // src: "abcdefgh" + gzipped "xyz" (echo -n "xyz" | gzip -f | hd). + const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', '\x1f', '\x8b', '\x08', '\x00', '\xc4', '\x1e', + '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xac', + '\x02', '\x00', '\x67', '\xba', '\x8e', '\xeb', '\x03', + '\x00', '\x00', '\x00' }; + const std::string src(src_data.cbegin(), src_data.cend()); + TemporaryFile src_file; + ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); + + // tgt: "abcdefgxyz" + gzipped "xxyyzz". + const std::vector tgt_data = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z', '\x1f', '\x8b', + '\x08', '\x00', '\x62', '\x1f', '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xa8', '\xac', + '\xac', '\xaa', '\x02', '\x00', '\x96', '\x30', '\x06', '\xb7', '\x06', '\x00', '\x00', '\x00' + }; + const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); + TemporaryFile tgt_file; + ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); + + TemporaryFile patch_file; + std::vector args = { + "imgdiff", src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + // Verify. + std::string patch; + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); + verify_patched_image(src, patch, tgt); + + // Corrupt the end of the patch and expect the ApplyImagePatch to fail. + patch.insert(patch.end() - 10, 10, '0'); + ASSERT_EQ(-1, ApplyImagePatch(reinterpret_cast(src.data()), src.size(), + reinterpret_cast(patch.data()), patch.size(), + [](const unsigned char* /*data*/, size_t len) { return len; })); +} + +static void construct_store_entry(const std::vector>& info, + ZipWriter* writer) { + for (auto& t : info) { + // Create t(1) blocks of t(2), and write the data to t(0) + ASSERT_EQ(0, writer->StartEntry(std::get<0>(t).c_str(), 0)); + const std::string content(std::get<1>(t) * 4096, std::get<2>(t)); + ASSERT_EQ(0, writer->WriteBytes(content.data(), content.size())); + ASSERT_EQ(0, writer->FinishEntry()); + } +} + +static void construct_deflate_entry(const std::vector>& info, + ZipWriter* writer, const std::string& data) { + for (auto& t : info) { + // t(0): entry_name; t(1): block offset; t(2) length in blocks. + ASSERT_EQ(0, writer->StartEntry(std::get<0>(t).c_str(), ZipWriter::kCompress)); + ASSERT_EQ(0, writer->WriteBytes(data.data() + std::get<1>(t) * 4096, std::get<2>(t) * 4096)); + ASSERT_EQ(0, writer->FinishEntry()); + } +} + +// Look for the source and patch pieces in debug_dir. Generate a target piece from each pair. +// Concatenate all the target pieces and match against the orignal one. Used pieces in debug_dir +// will be cleaned up. +static void GenerateAndCheckSplitTarget(const std::string& debug_dir, size_t count, + const std::string& tgt) { + std::string patched; + for (size_t i = 0; i < count; i++) { + std::string split_src_path = android::base::StringPrintf("%s/src-%zu", debug_dir.c_str(), i); + std::string split_src; + ASSERT_TRUE(android::base::ReadFileToString(split_src_path, &split_src)); + ASSERT_EQ(0, unlink(split_src_path.c_str())); + + std::string split_patch_path = + android::base::StringPrintf("%s/patch-%zu", debug_dir.c_str(), i); + std::string split_patch; + ASSERT_TRUE(android::base::ReadFileToString(split_patch_path, &split_patch)); + ASSERT_EQ(0, unlink(split_patch_path.c_str())); + + std::string split_tgt; + GenerateTarget(split_src, split_patch, &split_tgt); + patched += split_tgt; + } + + // Verify we can get back the original target image. + ASSERT_EQ(tgt, patched); +} + +std::vector ConstructImageChunks( + const std::vector& content, const std::vector>& info) { + std::vector chunks; + size_t start = 0; + for (const auto& t : info) { + size_t length = std::get<1>(t); + chunks.emplace_back(CHUNK_NORMAL, start, &content, length, std::get<0>(t)); + start += length; + } + + return chunks; +} + +TEST(ImgdiffTest, zip_mode_split_image_smoke) { + std::vector content; + content.reserve(4096 * 50); + uint8_t n = 0; + generate_n(back_inserter(content), 4096 * 50, [&n]() { return n++ / 4096; }); + + ZipModeImage tgt_image(false, 4096 * 10); + std::vector tgt_chunks = ConstructImageChunks(content, { { "a", 100 }, + { "b", 4096 * 2 }, + { "c", 4096 * 3 }, + { "d", 300 }, + { "e-0", 4096 * 10 }, + { "e-1", 4096 * 5 }, + { "CD", 200 } }); + tgt_image.Initialize(std::move(tgt_chunks), + std::vector(content.begin(), content.begin() + 82520)); + + tgt_image.DumpChunks(); + + ZipModeImage src_image(true, 4096 * 10); + std::vector src_chunks = ConstructImageChunks(content, { { "b", 4096 * 3 }, + { "c-0", 4096 * 10 }, + { "c-1", 4096 * 2 }, + { "a", 4096 * 5 }, + { "e-0", 4096 * 10 }, + { "e-1", 10000 }, + { "CD", 5000 } }); + src_image.Initialize(std::move(src_chunks), + std::vector(content.begin(), content.begin() + 137880)); + + std::vector split_tgt_images; + std::vector split_src_images; + std::vector split_src_ranges; + + ZipModeImage::SplitZipModeImageWithLimit(tgt_image, src_image, &split_tgt_images, + &split_src_images, &split_src_ranges); + + // src_piece 1: a 5 blocks, b 3 blocks + // src_piece 2: c-0 10 blocks + // src_piece 3: d 0 block, e-0 10 blocks + // src_piece 4: e-1 2 blocks; CD 2 blocks + ASSERT_EQ(split_tgt_images.size(), split_src_images.size()); + ASSERT_EQ(static_cast(4), split_tgt_images.size()); + + ASSERT_EQ(static_cast(1), split_tgt_images[0].NumOfChunks()); + ASSERT_EQ(static_cast(12288), split_tgt_images[0][0].DataLengthForPatch()); + ASSERT_EQ("4,0,3,15,20", split_src_ranges[0].ToString()); + + ASSERT_EQ(static_cast(1), split_tgt_images[1].NumOfChunks()); + ASSERT_EQ(static_cast(12288), split_tgt_images[1][0].DataLengthForPatch()); + ASSERT_EQ("2,3,13", split_src_ranges[1].ToString()); + + ASSERT_EQ(static_cast(1), split_tgt_images[2].NumOfChunks()); + ASSERT_EQ(static_cast(40960), split_tgt_images[2][0].DataLengthForPatch()); + ASSERT_EQ("2,20,30", split_src_ranges[2].ToString()); + + ASSERT_EQ(static_cast(1), split_tgt_images[3].NumOfChunks()); + ASSERT_EQ(static_cast(16984), split_tgt_images[3][0].DataLengthForPatch()); + ASSERT_EQ("2,30,34", split_src_ranges[3].ToString()); +} + +TEST(ImgdiffTest, zip_mode_store_large_apk) { + // Construct src and tgt zip files with limit = 10 blocks. + // src tgt + // 12 blocks 'd' 3 blocks 'a' + // 8 blocks 'c' 3 blocks 'b' + // 3 blocks 'b' 8 blocks 'c' (exceeds limit) + // 3 blocks 'a' 12 blocks 'd' (exceeds limit) + // 3 blocks 'e' + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + construct_store_entry( + { { "a", 3, 'a' }, { "b", 3, 'b' }, { "c", 8, 'c' }, { "d", 12, 'd' }, { "e", 3, 'e' } }, + &tgt_writer); + ASSERT_EQ(0, tgt_writer.Finish()); + ASSERT_EQ(0, fclose(tgt_file_ptr)); + + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + construct_store_entry({ { "d", 12, 'd' }, { "c", 8, 'c' }, { "b", 3, 'b' }, { "a", 3, 'a' } }, + &src_writer); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + // Compute patch. + TemporaryFile patch_file; + TemporaryFile split_info_file; + TemporaryDir debug_dir; + std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); + std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); + std::vector args = { + "imgdiff", "-z", "--block-limit=10", split_info_arg.c_str(), debug_dir_arg.c_str(), + src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + + // Expect 4 pieces of patch. (Roughly 3'a',3'b'; 8'c'; 10'd'; 2'd'3'e') + GenerateAndCheckSplitTarget(debug_dir.path, 4, tgt); +} + +TEST(ImgdiffTest, zip_mode_deflate_large_apk) { + // Src and tgt zip files are constructed as follows. + // src tgt + // 22 blocks, "d" 4 blocks, "a" + // 5 blocks, "b" 4 blocks, "b" + // 3 blocks, "a" 8 blocks, "c" (exceeds limit) + // 1 block, "g" 20 blocks, "d" (exceeds limit) + // 8 blocks, "c" 2 blocks, "e" + // 1 block, "f" 1 block , "f" + std::string tgt_path = from_testdata_base("deflate_tgt.zip"); + std::string src_path = from_testdata_base("deflate_src.zip"); + + ZipModeImage src_image(true, 10 * 4096); + ZipModeImage tgt_image(false, 10 * 4096); + ASSERT_TRUE(src_image.Initialize(src_path)); + ASSERT_TRUE(tgt_image.Initialize(tgt_path)); + ASSERT_TRUE(ZipModeImage::CheckAndProcessChunks(&tgt_image, &src_image)); + + src_image.DumpChunks(); + tgt_image.DumpChunks(); + + std::vector split_tgt_images; + std::vector split_src_images; + std::vector split_src_ranges; + ZipModeImage::SplitZipModeImageWithLimit(tgt_image, src_image, &split_tgt_images, + &split_src_images, &split_src_ranges); + + // Expected split images with limit = 10 blocks. + // src_piece 0: a 3 blocks, b 5 blocks + // src_piece 1: c 8 blocks + // src_piece 2: d-0 10 block + // src_piece 3: d-1 10 blocks + // src_piece 4: e 1 block, CD + ASSERT_EQ(split_tgt_images.size(), split_src_images.size()); + ASSERT_EQ(static_cast(5), split_tgt_images.size()); + + ASSERT_EQ(static_cast(2), split_src_images[0].NumOfChunks()); + ASSERT_EQ("a", split_src_images[0][0].GetEntryName()); + ASSERT_EQ("b", split_src_images[0][1].GetEntryName()); + + ASSERT_EQ(static_cast(1), split_src_images[1].NumOfChunks()); + ASSERT_EQ("c", split_src_images[1][0].GetEntryName()); + + ASSERT_EQ(static_cast(0), split_src_images[2].NumOfChunks()); + ASSERT_EQ(static_cast(0), split_src_images[3].NumOfChunks()); + ASSERT_EQ(static_cast(0), split_src_images[4].NumOfChunks()); + + // Compute patch. + TemporaryFile patch_file; + TemporaryFile split_info_file; + TemporaryDir debug_dir; + ASSERT_TRUE(ZipModeImage::GeneratePatches(split_tgt_images, split_src_images, split_src_ranges, + patch_file.path, split_info_file.path, debug_dir.path)); + + // Verify the content of split info. + // Expect 5 pieces of patch. ["a","b"; "c"; "d-0"; "d-1"; "e"] + std::string split_info_string; + android::base::ReadFileToString(split_info_file.path, &split_info_string); + std::vector info_list = + android::base::Split(android::base::Trim(split_info_string), "\n"); + + ASSERT_EQ(static_cast(7), info_list.size()); + ASSERT_EQ("2", android::base::Trim(info_list[0])); + ASSERT_EQ("5", android::base::Trim(info_list[1])); + + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_path, &tgt)); + ASSERT_EQ(static_cast(160385), tgt.size()); + std::vector tgt_file_ranges = { + "36864 2,22,31", "32768 2,31,40", "40960 2,0,11", "40960 2,11,21", "8833 4,21,22,40,41", + }; + + for (size_t i = 0; i < 5; i++) { + struct stat st; + std::string path = android::base::StringPrintf("%s/patch-%zu", debug_dir.path, i); + ASSERT_EQ(0, stat(path.c_str(), &st)); + ASSERT_EQ(std::to_string(st.st_size) + " " + tgt_file_ranges[i], + android::base::Trim(info_list[i + 2])); + } + + GenerateAndCheckSplitTarget(debug_dir.path, 5, tgt); +} + +TEST(ImgdiffTest, zip_mode_no_match_source) { + // Generate 20 blocks of random data. + std::string random_data; + random_data.reserve(4096 * 20); + generate_n(back_inserter(random_data), 4096 * 20, []() { return rand() % 256; }); + + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + + construct_deflate_entry({ { "a", 0, 4 }, { "b", 5, 5 }, { "c", 11, 5 } }, &tgt_writer, + random_data); + + ASSERT_EQ(0, tgt_writer.Finish()); + ASSERT_EQ(0, fclose(tgt_file_ptr)); + + // We don't have a matching source entry. + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + construct_store_entry({ { "d", 1, 'd' } }, &src_writer); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + // Compute patch. + TemporaryFile patch_file; + TemporaryFile split_info_file; + TemporaryDir debug_dir; + std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); + std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); + std::vector args = { + "imgdiff", "-z", "--block-limit=10", debug_dir_arg.c_str(), split_info_arg.c_str(), + src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + + // Expect 1 pieces of patch due to no matching source entry. + GenerateAndCheckSplitTarget(debug_dir.path, 1, tgt); +} + +TEST(ImgdiffTest, zip_mode_large_enough_limit) { + // Generate 20 blocks of random data. + std::string random_data; + random_data.reserve(4096 * 20); + generate_n(back_inserter(random_data), 4096 * 20, []() { return rand() % 256; }); + + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + + construct_deflate_entry({ { "a", 0, 10 }, { "b", 10, 5 } }, &tgt_writer, random_data); + + ASSERT_EQ(0, tgt_writer.Finish()); + ASSERT_EQ(0, fclose(tgt_file_ptr)); + + // Construct 10 blocks of source. + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + construct_deflate_entry({ { "a", 1, 10 } }, &src_writer, random_data); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + // Compute patch with a limit of 20 blocks. + TemporaryFile patch_file; + TemporaryFile split_info_file; + TemporaryDir debug_dir; + std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); + std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); + std::vector args = { + "imgdiff", "-z", "--block-limit=20", split_info_arg.c_str(), debug_dir_arg.c_str(), + src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + + // Expect 1 piece of patch since limit is larger than the zip file size. + GenerateAndCheckSplitTarget(debug_dir.path, 1, tgt); +} + +TEST(ImgdiffTest, zip_mode_large_apk_small_target_chunk) { + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + + // The first entry is less than 4096 bytes, followed immediately by an entry that has a very + // large counterpart in the source file. Therefore the first entry will be patched separately. + std::string small_chunk("a", 2000); + ASSERT_EQ(0, tgt_writer.StartEntry("a", 0)); + ASSERT_EQ(0, tgt_writer.WriteBytes(small_chunk.data(), small_chunk.size())); + ASSERT_EQ(0, tgt_writer.FinishEntry()); + construct_store_entry( + { + { "b", 12, 'b' }, { "c", 3, 'c' }, + }, + &tgt_writer); + ASSERT_EQ(0, tgt_writer.Finish()); + ASSERT_EQ(0, fclose(tgt_file_ptr)); + + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + construct_store_entry({ { "a", 1, 'a' }, { "b", 13, 'b' }, { "c", 1, 'c' } }, &src_writer); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + // Compute patch. + TemporaryFile patch_file; + TemporaryFile split_info_file; + TemporaryDir debug_dir; + std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); + std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); + std::vector args = { + "imgdiff", "-z", "--block-limit=10", split_info_arg.c_str(), debug_dir_arg.c_str(), + src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + + // Expect three split src images: + // src_piece 0: a 1 blocks + // src_piece 1: b-0 10 blocks + // src_piece 2: b-1 3 blocks, c 1 blocks, CD + GenerateAndCheckSplitTarget(debug_dir.path, 3, tgt); +} + +TEST(ImgdiffTest, zip_mode_large_apk_skipped_small_target_chunk) { + TemporaryFile tgt_file; + FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); + ZipWriter tgt_writer(tgt_file_ptr); + + construct_store_entry( + { + { "a", 11, 'a' }, + }, + &tgt_writer); + + // Construct a tiny target entry of 1 byte, which will be skipped due to the tail alignment of + // the previous entry. + std::string small_chunk("b", 1); + ASSERT_EQ(0, tgt_writer.StartEntry("b", 0)); + ASSERT_EQ(0, tgt_writer.WriteBytes(small_chunk.data(), small_chunk.size())); + ASSERT_EQ(0, tgt_writer.FinishEntry()); + + ASSERT_EQ(0, tgt_writer.Finish()); + ASSERT_EQ(0, fclose(tgt_file_ptr)); + + TemporaryFile src_file; + FILE* src_file_ptr = fdopen(src_file.release(), "wb"); + ZipWriter src_writer(src_file_ptr); + construct_store_entry( + { + { "a", 11, 'a' }, { "b", 11, 'b' }, + }, + &src_writer); + ASSERT_EQ(0, src_writer.Finish()); + ASSERT_EQ(0, fclose(src_file_ptr)); + + // Compute patch. + TemporaryFile patch_file; + TemporaryFile split_info_file; + TemporaryDir debug_dir; + std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); + std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); + std::vector args = { + "imgdiff", "-z", "--block-limit=10", split_info_arg.c_str(), debug_dir_arg.c_str(), + src_file.path, tgt_file.path, patch_file.path, + }; + ASSERT_EQ(0, imgdiff(args.size(), args.data())); + + std::string tgt; + ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); + + // Expect two split src images: + // src_piece 0: a-0 10 blocks + // src_piece 1: a-0 1 block, CD + GenerateAndCheckSplitTarget(debug_dir.path, 2, tgt); +} diff --git a/tests/unit/host/update_simulator_test.cpp b/tests/unit/host/update_simulator_test.cpp new file mode 100644 index 000000000..bf89b7859 --- /dev/null +++ b/tests/unit/host/update_simulator_test.cpp @@ -0,0 +1,403 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "otautil/paths.h" +#include "otautil/print_sha1.h" +#include "updater/blockimg.h" +#include "updater/build_info.h" +#include "updater/install.h" +#include "updater/simulator_runtime.h" +#include "updater/target_files.h" +#include "updater/updater.h" + +using std::string; + +// echo -n "system.img" > system.img && img2simg system.img sparse_system_string_.img 4096 && +// hexdump -v -e '" " 12/1 "0x%02x, " "\n"' sparse_system_string_.img +// The total size of the result sparse image is 4136 bytes; and we can append 0s in the end to get +// the full image. +constexpr uint8_t SPARSE_SYSTEM_HEADER[] = { + 0x3a, 0xff, 0x26, 0xed, 0x01, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x0c, 0x00, 0x00, 0x10, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0xca, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x10, 0x00, 0x00, 0x73, 0x79, 0x73, 0x74, 0x65, + 0x6d, 0x2e, 0x69, 0x6d, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + +static void AddZipEntries(int fd, const std::map& entries) { + FILE* zip_file = fdopen(fd, "w"); + ZipWriter writer(zip_file); + for (const auto& pair : entries) { + ASSERT_EQ(0, writer.StartEntry(pair.first.c_str(), 0)); + ASSERT_EQ(0, writer.WriteBytes(pair.second.data(), pair.second.size())); + ASSERT_EQ(0, writer.FinishEntry()); + } + ASSERT_EQ(0, writer.Finish()); + ASSERT_EQ(0, fclose(zip_file)); +} + +static string CalculateSha1(const string& data) { + uint8_t digest[SHA_DIGEST_LENGTH]; + SHA1(reinterpret_cast(data.c_str()), data.size(), digest); + return print_sha1(digest); +} + +static void CreateBsdiffPatch(const string& src, const string& tgt, string* patch) { + TemporaryFile patch_file; + ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast(src.data()), src.size(), + reinterpret_cast(tgt.data()), tgt.size(), + patch_file.path, nullptr)); + ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, patch)); +} + +static void RunSimulation(std::string_view src_tf, std::string_view ota_package, bool expected) { + TemporaryFile cmd_pipe; + TemporaryFile temp_saved_source; + TemporaryFile temp_last_command; + TemporaryDir temp_stash_base; + + Paths::Get().set_cache_temp_source(temp_saved_source.path); + Paths::Get().set_last_command_file(temp_last_command.path); + Paths::Get().set_stash_directory_base(temp_stash_base.path); + + // Configure edify's functions. + RegisterBuiltins(); + RegisterInstallFunctions(); + RegisterBlockImageFunctions(); + + // Run the update simulation and check the result. + TemporaryDir work_dir; + BuildInfo build_info(work_dir.path); + ASSERT_TRUE(build_info.ParseTargetFile(src_tf, false)); + Updater updater(std::make_unique(&build_info)); + ASSERT_TRUE(updater.Init(cmd_pipe.release(), ota_package, false)); + ASSERT_EQ(expected, updater.RunUpdate()); + // TODO(xunchang) check the recovery&system has the expected contents. +} + +class UpdateSimulatorTest : public ::testing::Test { + protected: + void SetUp() override { + std::vector props = { + "import /oem/oem.prop oem*", + "# begin build properties", + "# autogenerated by buildinfo.sh", + "ro.build.id=OPR1.170510.001", + "ro.build.display.id=OPR1.170510.001 dev-keys", + "ro.build.version.incremental=3993052", + "ro.build.version.release=O", + "ro.build.date=Wed May 10 11:10:29 UTC 2017", + "ro.build.date.utc=1494414629", + "ro.build.type=user", + "ro.build.tags=dev-keys", + "ro.build.flavor=angler-user", + "ro.product.system.brand=google", + "ro.product.system.name=angler", + "ro.product.system.device=angler", + }; + build_prop_string_ = android::base::Join(props, "\n"); + + fstab_content_ = R"( +# +# More comments..... + +/dev/block/by-name/system /system ext4 ro,barrier=1 wait +/dev/block/by-name/vendor /vendor ext4 ro wait,verify=/dev/metadata +/dev/block/by-name/cache /cache ext4 noatime,errors=panic wait,check +/dev/block/by-name/modem /firmware vfat ro,uid=1000,gid=1000, wait +/dev/block/by-name/boot /boot emmc defaults defaults +/dev/block/by-name/recovery /recovery emmc defaults defaults +/dev/block/by-name/misc /misc emmc defaults +/dev/block/by-name/modem /modem emmc defaults defaults)"; + + raw_system_string_ = "system.img" + string(4086, '\0'); // raw image is 4096 bytes in total + sparse_system_string_ = string(SPARSE_SYSTEM_HEADER, std::end(SPARSE_SYSTEM_HEADER)) + + string(4136 - sizeof(SPARSE_SYSTEM_HEADER), '\0'); + } + + string build_prop_string_; + string fstab_content_; + string raw_system_string_; + string sparse_system_string_; +}; + +TEST_F(UpdateSimulatorTest, TargetFile_ExtractImage) { + TemporaryFile zip_file; + AddZipEntries(zip_file.release(), { { "META/misc_info.txt", "extfs_sparse_flag=-s" }, + { "IMAGES/system.img", sparse_system_string_ } }); + TargetFile target_file(zip_file.path, false); + ASSERT_TRUE(target_file.Open()); + + TemporaryDir temp_dir; + TemporaryFile raw_image; + ASSERT_TRUE(target_file.ExtractImage( + "IMAGES/system.img", FstabInfo("/dev/system", "system", "ext4"), temp_dir.path, &raw_image)); + + // Check the raw image has expected contents. + string content; + ASSERT_TRUE(android::base::ReadFileToString(raw_image.path, &content)); + string expected_content = "system.img" + string(4086, '\0'); + ASSERT_EQ(expected_content, content); +} + +TEST_F(UpdateSimulatorTest, TargetFile_ParseFstabInfo) { + TemporaryFile zip_file; + AddZipEntries(zip_file.release(), + { { "META/misc_info.txt", "" }, + { "RECOVERY/RAMDISK/system/etc/recovery.fstab", fstab_content_ } }); + TargetFile target_file(zip_file.path, false); + ASSERT_TRUE(target_file.Open()); + + std::vector fstab_info; + EXPECT_TRUE(target_file.ParseFstabInfo(&fstab_info)); + + std::vector> transformed; + std::transform(fstab_info.begin(), fstab_info.end(), std::back_inserter(transformed), + [](const FstabInfo& info) { + return std::vector{ info.blockdev_name, info.mount_point, info.fs_type }; + }); + + std::vector> expected = { + { "/dev/block/by-name/system", "/system", "ext4" }, + { "/dev/block/by-name/vendor", "/vendor", "ext4" }, + { "/dev/block/by-name/cache", "/cache", "ext4" }, + { "/dev/block/by-name/boot", "/boot", "emmc" }, + { "/dev/block/by-name/recovery", "/recovery", "emmc" }, + { "/dev/block/by-name/misc", "/misc", "emmc" }, + { "/dev/block/by-name/modem", "/modem", "emmc" }, + }; + EXPECT_EQ(expected, transformed); +} + +TEST_F(UpdateSimulatorTest, BuildInfo_ParseTargetFile) { + std::map entries = { + { "META/misc_info.txt", "" }, + { "SYSTEM/build.prop", build_prop_string_ }, + { "RECOVERY/RAMDISK/system/etc/recovery.fstab", fstab_content_ }, + { "IMAGES/recovery.img", "" }, + { "IMAGES/boot.img", "" }, + { "IMAGES/misc.img", "" }, + { "IMAGES/system.map", "" }, + { "IMAGES/system.img", sparse_system_string_ }, + }; + + TemporaryFile zip_file; + AddZipEntries(zip_file.release(), entries); + + TemporaryDir temp_dir; + BuildInfo build_info(temp_dir.path); + ASSERT_TRUE(build_info.ParseTargetFile(zip_file.path, false)); + + std::map expected_result = { + { "ro.build.id", "OPR1.170510.001" }, + { "ro.build.display.id", "OPR1.170510.001 dev-keys" }, + { "ro.build.version.incremental", "3993052" }, + { "ro.build.version.release", "O" }, + { "ro.build.date", "Wed May 10 11:10:29 UTC 2017" }, + { "ro.build.date.utc", "1494414629" }, + { "ro.build.type", "user" }, + { "ro.build.tags", "dev-keys" }, + { "ro.build.flavor", "angler-user" }, + { "ro.product.brand", "google" }, + { "ro.product.name", "angler" }, + { "ro.product.device", "angler" }, + }; + + for (const auto& [key, value] : expected_result) { + ASSERT_EQ(value, build_info.GetProperty(key, "")); + } + + // Check that the temp files for each block device are created successfully. + for (auto name : { "/dev/block/by-name/system", "/dev/block/by-name/recovery", + "/dev/block/by-name/boot", "/dev/block/by-name/misc" }) { + ASSERT_EQ(0, access(build_info.FindBlockDeviceName(name).c_str(), R_OK)); + } +} + +TEST_F(UpdateSimulatorTest, RunUpdateSmoke) { + string recovery_img_string = "recovery.img"; + string boot_img_string = "boot.img"; + + std::map src_entries{ + { "META/misc_info.txt", "extfs_sparse_flag=-s" }, + { "RECOVERY/RAMDISK/etc/recovery.fstab", fstab_content_ }, + { "SYSTEM/build.prop", build_prop_string_ }, + { "IMAGES/recovery.img", "" }, + { "IMAGES/boot.img", boot_img_string }, + { "IMAGES/system.img", sparse_system_string_ }, + }; + + // Construct the source target-files. + TemporaryFile src_tf; + AddZipEntries(src_tf.release(), src_entries); + + string recovery_from_boot; + CreateBsdiffPatch(boot_img_string, recovery_img_string, &recovery_from_boot); + + // Set up the apply patch commands to patch the recovery image. + string recovery_sha1 = CalculateSha1(recovery_img_string); + string boot_sha1 = CalculateSha1(boot_img_string); + string apply_patch_source_string = android::base::StringPrintf( + "EMMC:/dev/block/by-name/boot:%zu:%s", boot_img_string.size(), boot_sha1.c_str()); + string apply_patch_target_string = android::base::StringPrintf( + "EMMC:/dev/block/by-name/recovery:%zu:%s", recovery_img_string.size(), recovery_sha1.c_str()); + string check_command = android::base::StringPrintf( + R"(patch_partition_check("%s", "%s") || abort("check failed");)", + apply_patch_target_string.c_str(), apply_patch_source_string.c_str()); + string patch_command = android::base::StringPrintf( + R"(patch_partition("%s", "%s", package_extract_file("patch.p")) || abort("patch failed");)", + apply_patch_target_string.c_str(), apply_patch_source_string.c_str()); + + // Add the commands to update the system image. Test common commands: + // * getprop + // * ui_print + // * patch_partition + // * package_extract_file (single argument) + // * block_image_verify, block_image_update + string tgt_system_string = string(4096, 'a'); + string system_patch; + CreateBsdiffPatch(raw_system_string_, tgt_system_string, &system_patch); + + string tgt_system_hash = CalculateSha1(tgt_system_string); + string src_system_hash = CalculateSha1(raw_system_string_); + + std::vector transfer_list = { + "4", + "1", + "0", + "0", + android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,1 1 2,0,1", system_patch.size(), + src_system_hash.c_str(), tgt_system_hash.c_str()), + }; + + // Construct the updater_script. + std::vector updater_commands = { + R"(getprop("ro.product.device") == "angler" || abort("This package is for \"angler\"");)", + R"(ui_print("Source: angler/OPR1.170510.001");)", + check_command, + patch_command, + R"(block_image_verify("/dev/block/by-name/system", )" + R"(package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat") || )" + R"(abort("Failed to verify system.");)", + R"(block_image_update("/dev/block/by-name/system", )" + R"(package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat") || )" + R"(abort("Failed to verify system.");)", + }; + string updater_script = android::base::Join(updater_commands, '\n'); + + // Construct the ota update package. + std::map ota_entries{ + { "system.new.dat", "" }, + { "system.patch.dat", system_patch }, + { "system.transfer.list", android::base::Join(transfer_list, '\n') }, + { "META-INF/com/google/android/updater-script", updater_script }, + { "patch.p", recovery_from_boot }, + }; + + TemporaryFile ota_package; + AddZipEntries(ota_package.release(), ota_entries); + + RunSimulation(src_tf.path, ota_package.path, true); +} + +TEST_F(UpdateSimulatorTest, RunUpdateUnrecognizedFunction) { + std::map src_entries{ + { "META/misc_info.txt", "extfs_sparse_flag=-s" }, + { "IMAGES/system.img", sparse_system_string_ }, + { "RECOVERY/RAMDISK/etc/recovery.fstab", fstab_content_ }, + { "SYSTEM/build.prop", build_prop_string_ }, + }; + + TemporaryFile src_tf; + AddZipEntries(src_tf.release(), src_entries); + + std::map ota_entries{ + { "system.new.dat", "" }, + { "system.patch.dat", "" }, + { "system.transfer.list", "" }, + { "META-INF/com/google/android/updater-script", R"(bad_function("");)" }, + }; + + TemporaryFile ota_package; + AddZipEntries(ota_package.release(), ota_entries); + + RunSimulation(src_tf.path, ota_package.path, false); +} + +TEST_F(UpdateSimulatorTest, RunUpdateApplyPatchFailed) { + string recovery_img_string = "recovery.img"; + string boot_img_string = "boot.img"; + + std::map src_entries{ + { "META/misc_info.txt", "extfs_sparse_flag=-s" }, + { "IMAGES/recovery.img", "" }, + { "IMAGES/boot.img", boot_img_string }, + { "IMAGES/system.img", sparse_system_string_ }, + { "RECOVERY/RAMDISK/etc/recovery.fstab", fstab_content_ }, + { "SYSTEM/build.prop", build_prop_string_ }, + }; + + TemporaryFile src_tf; + AddZipEntries(src_tf.release(), src_entries); + + string recovery_sha1 = CalculateSha1(recovery_img_string); + string boot_sha1 = CalculateSha1(boot_img_string); + string apply_patch_source_string = android::base::StringPrintf( + "EMMC:/dev/block/by-name/boot:%zu:%s", boot_img_string.size(), boot_sha1.c_str()); + string apply_patch_target_string = android::base::StringPrintf( + "EMMC:/dev/block/by-name/recovery:%zu:%s", recovery_img_string.size(), recovery_sha1.c_str()); + string check_command = android::base::StringPrintf( + R"(patch_partition_check("%s", "%s") || abort("check failed");)", + apply_patch_target_string.c_str(), apply_patch_source_string.c_str()); + string patch_command = android::base::StringPrintf( + R"(patch_partition("%s", "%s", package_extract_file("patch.p")) || abort("failed");)", + apply_patch_target_string.c_str(), apply_patch_source_string.c_str()); + + // Give an invalid recovery patch and expect the apply patch to fail. + // TODO(xunchang) check the cause code. + std::vector updater_commands = { + R"(ui_print("Source: angler/OPR1.170510.001");)", + check_command, + patch_command, + }; + + string updater_script = android::base::Join(updater_commands, '\n'); + std::map ota_entries{ + { "system.new.dat", "" }, + { "system.patch.dat", "" }, + { "system.transfer.list", "" }, + { "META-INF/com/google/android/updater-script", updater_script }, + { "patch.p", "random string" }, + }; + + TemporaryFile ota_package; + AddZipEntries(ota_package.release(), ota_entries); + + RunSimulation(src_tf.path, ota_package.path, false); +} diff --git a/tests/unit/imgdiff_test.cpp b/tests/unit/imgdiff_test.cpp deleted file mode 100644 index e76ccbdfb..000000000 --- a/tests/unit/imgdiff_test.cpp +++ /dev/null @@ -1,1114 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common/test_constants.h" - -using android::base::get_unaligned; - -// Sanity check for the given imgdiff patch header. -static void verify_patch_header(const std::string& patch, size_t* num_normal, size_t* num_raw, - size_t* num_deflate) { - const size_t size = patch.size(); - const char* data = patch.data(); - - ASSERT_GE(size, 12U); - ASSERT_EQ("IMGDIFF2", std::string(data, 8)); - - const int num_chunks = get_unaligned(data + 8); - ASSERT_GE(num_chunks, 0); - - size_t normal = 0; - size_t raw = 0; - size_t deflate = 0; - - size_t pos = 12; - for (int i = 0; i < num_chunks; ++i) { - ASSERT_LE(pos + 4, size); - int type = get_unaligned(data + pos); - pos += 4; - if (type == CHUNK_NORMAL) { - pos += 24; - ASSERT_LE(pos, size); - normal++; - } else if (type == CHUNK_RAW) { - ASSERT_LE(pos + 4, size); - ssize_t data_len = get_unaligned(data + pos); - ASSERT_GT(data_len, 0); - pos += 4 + data_len; - ASSERT_LE(pos, size); - raw++; - } else if (type == CHUNK_DEFLATE) { - pos += 60; - ASSERT_LE(pos, size); - deflate++; - } else { - FAIL() << "Invalid patch type: " << type; - } - } - - if (num_normal != nullptr) *num_normal = normal; - if (num_raw != nullptr) *num_raw = raw; - if (num_deflate != nullptr) *num_deflate = deflate; -} - -static void GenerateTarget(const std::string& src, const std::string& patch, std::string* patched) { - patched->clear(); - ASSERT_EQ(0, ApplyImagePatch(reinterpret_cast(src.data()), src.size(), - reinterpret_cast(patch.data()), patch.size(), - [&](const unsigned char* data, size_t len) { - patched->append(reinterpret_cast(data), len); - return len; - })); -} - -static void verify_patched_image(const std::string& src, const std::string& patch, - const std::string& tgt) { - std::string patched; - GenerateTarget(src, patch, &patched); - ASSERT_EQ(tgt, patched); -} - -TEST(ImgdiffTest, invalid_args) { - // Insufficient inputs. - ASSERT_EQ(2, imgdiff(1, (const char* []){ "imgdiff" })); - ASSERT_EQ(2, imgdiff(2, (const char* []){ "imgdiff", "-z" })); - ASSERT_EQ(2, imgdiff(2, (const char* []){ "imgdiff", "-b" })); - ASSERT_EQ(2, imgdiff(3, (const char* []){ "imgdiff", "-z", "-b" })); - - // Failed to read bonus file. - ASSERT_EQ(1, imgdiff(3, (const char* []){ "imgdiff", "-b", "doesntexist" })); - - // Failed to read input files. - ASSERT_EQ(1, imgdiff(4, (const char* []){ "imgdiff", "doesntexist", "doesntexist", "output" })); - ASSERT_EQ( - 1, imgdiff(5, (const char* []){ "imgdiff", "-z", "doesntexist", "doesntexist", "output" })); -} - -TEST(ImgdiffTest, image_mode_smoke) { - // Random bytes. - const std::string src("abcdefg"); - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - const std::string tgt("abcdefgxyz"); - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect one CHUNK_RAW entry. - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(0U, num_deflate); - ASSERT_EQ(1U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, zip_mode_smoke_store) { - // Construct src and tgt zip files. - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - ASSERT_EQ(0, src_writer.StartEntry("file1.txt", 0)); // Store mode. - const std::string src_content("abcdefg"); - ASSERT_EQ(0, src_writer.WriteBytes(src_content.data(), src_content.size())); - ASSERT_EQ(0, src_writer.FinishEntry()); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - ASSERT_EQ(0, tgt_writer.StartEntry("file1.txt", 0)); // Store mode. - const std::string tgt_content("abcdefgxyz"); - ASSERT_EQ(0, tgt_writer.WriteBytes(tgt_content.data(), tgt_content.size())); - ASSERT_EQ(0, tgt_writer.FinishEntry()); - ASSERT_EQ(0, tgt_writer.Finish()); - ASSERT_EQ(0, fclose(tgt_file_ptr)); - - // Compute patch. - TemporaryFile patch_file; - std::vector args = { - "imgdiff", "-z", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - std::string src; - ASSERT_TRUE(android::base::ReadFileToString(src_file.path, &src)); - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect one CHUNK_RAW entry. - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(0U, num_deflate); - ASSERT_EQ(1U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, zip_mode_smoke_compressed) { - // Generate 1 block of random data. - std::string random_data; - random_data.reserve(4096); - generate_n(back_inserter(random_data), 4096, []() { return rand() % 256; }); - - // Construct src and tgt zip files. - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - ASSERT_EQ(0, src_writer.StartEntry("file1.txt", ZipWriter::kCompress)); - const std::string src_content = random_data; - ASSERT_EQ(0, src_writer.WriteBytes(src_content.data(), src_content.size())); - ASSERT_EQ(0, src_writer.FinishEntry()); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - ASSERT_EQ(0, tgt_writer.StartEntry("file1.txt", ZipWriter::kCompress)); - const std::string tgt_content = random_data + "extra contents"; - ASSERT_EQ(0, tgt_writer.WriteBytes(tgt_content.data(), tgt_content.size())); - ASSERT_EQ(0, tgt_writer.FinishEntry()); - ASSERT_EQ(0, tgt_writer.Finish()); - ASSERT_EQ(0, fclose(tgt_file_ptr)); - - // Compute patch. - TemporaryFile patch_file; - std::vector args = { - "imgdiff", "-z", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - std::string src; - ASSERT_TRUE(android::base::ReadFileToString(src_file.path, &src)); - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect three entries: CHUNK_RAW (header) + CHUNK_DEFLATE (data) + CHUNK_RAW (footer). - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(1U, num_deflate); - ASSERT_EQ(2U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, zip_mode_empty_target) { - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - ASSERT_EQ(0, src_writer.StartEntry("file1.txt", ZipWriter::kCompress)); - const std::string src_content = "abcdefg"; - ASSERT_EQ(0, src_writer.WriteBytes(src_content.data(), src_content.size())); - ASSERT_EQ(0, src_writer.FinishEntry()); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - // Construct a empty entry in the target zip. - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - ASSERT_EQ(0, tgt_writer.StartEntry("file1.txt", ZipWriter::kCompress)); - const std::string tgt_content; - ASSERT_EQ(0, tgt_writer.WriteBytes(tgt_content.data(), tgt_content.size())); - ASSERT_EQ(0, tgt_writer.FinishEntry()); - ASSERT_EQ(0, tgt_writer.Finish()); - - // Compute patch. - TemporaryFile patch_file; - std::vector args = { - "imgdiff", "-z", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - std::string src; - ASSERT_TRUE(android::base::ReadFileToString(src_file.path, &src)); - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, zip_mode_smoke_trailer_zeros) { - // Generate 1 block of random data. - std::string random_data; - random_data.reserve(4096); - generate_n(back_inserter(random_data), 4096, []() { return rand() % 256; }); - - // Construct src and tgt zip files. - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - ASSERT_EQ(0, src_writer.StartEntry("file1.txt", ZipWriter::kCompress)); - const std::string src_content = random_data; - ASSERT_EQ(0, src_writer.WriteBytes(src_content.data(), src_content.size())); - ASSERT_EQ(0, src_writer.FinishEntry()); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - ASSERT_EQ(0, tgt_writer.StartEntry("file1.txt", ZipWriter::kCompress)); - const std::string tgt_content = random_data + "abcdefg"; - ASSERT_EQ(0, tgt_writer.WriteBytes(tgt_content.data(), tgt_content.size())); - ASSERT_EQ(0, tgt_writer.FinishEntry()); - ASSERT_EQ(0, tgt_writer.Finish()); - // Add trailing zeros to the target zip file. - std::vector zeros(10); - ASSERT_EQ(zeros.size(), fwrite(zeros.data(), sizeof(uint8_t), zeros.size(), tgt_file_ptr)); - ASSERT_EQ(0, fclose(tgt_file_ptr)); - - // Compute patch. - TemporaryFile patch_file; - std::vector args = { - "imgdiff", "-z", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - std::string src; - ASSERT_TRUE(android::base::ReadFileToString(src_file.path, &src)); - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect three entries: CHUNK_RAW (header) + CHUNK_DEFLATE (data) + CHUNK_RAW (footer). - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(1U, num_deflate); - ASSERT_EQ(2U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, image_mode_simple) { - std::string gzipped_source_path = from_testdata_base("gzipped_source"); - std::string gzipped_source; - ASSERT_TRUE(android::base::ReadFileToString(gzipped_source_path, &gzipped_source)); - - const std::string src = "abcdefg" + gzipped_source; - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - std::string gzipped_target_path = from_testdata_base("gzipped_target"); - std::string gzipped_target; - ASSERT_TRUE(android::base::ReadFileToString(gzipped_target_path, &gzipped_target)); - const std::string tgt = "abcdefgxyz" + gzipped_target; - - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect three entries: CHUNK_RAW (header) + CHUNK_DEFLATE (data) + CHUNK_RAW (footer). - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(1U, num_deflate); - ASSERT_EQ(2U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, image_mode_bad_gzip) { - // Modify the uncompressed length in the gzip footer. - const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', - 'h', '\x1f', '\x8b', '\x08', '\x00', '\xc4', '\x1e', - '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xac', - '\x02', '\x00', '\x67', '\xba', '\x8e', '\xeb', '\x03', - '\xff', '\xff', '\xff' }; - const std::string src(src_data.cbegin(), src_data.cend()); - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - // Modify the uncompressed length in the gzip footer. - const std::vector tgt_data = { - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z', '\x1f', '\x8b', - '\x08', '\x00', '\x62', '\x1f', '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xa8', '\xac', - '\xac', '\xaa', '\x02', '\x00', '\x96', '\x30', '\x06', '\xb7', '\x06', '\xff', '\xff', '\xff' - }; - const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, image_mode_different_num_chunks) { - // src: "abcdefgh" + gzipped "xyz" (echo -n "xyz" | gzip -f | hd) + gzipped "test". - const std::vector src_data = { - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\x1f', '\x8b', '\x08', - '\x00', '\xc4', '\x1e', '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xac', '\x02', - '\x00', '\x67', '\xba', '\x8e', '\xeb', '\x03', '\x00', '\x00', '\x00', '\x1f', '\x8b', - '\x08', '\x00', '\xb2', '\x3a', '\x53', '\x58', '\x00', '\x03', '\x2b', '\x49', '\x2d', - '\x2e', '\x01', '\x00', '\x0c', '\x7e', '\x7f', '\xd8', '\x04', '\x00', '\x00', '\x00' - }; - const std::string src(src_data.cbegin(), src_data.cend()); - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - // tgt: "abcdefgxyz" + gzipped "xxyyzz". - const std::vector tgt_data = { - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z', '\x1f', '\x8b', - '\x08', '\x00', '\x62', '\x1f', '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xa8', '\xac', - '\xac', '\xaa', '\x02', '\x00', '\x96', '\x30', '\x06', '\xb7', '\x06', '\x00', '\x00', '\x00' - }; - const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(1, imgdiff(args.size(), args.data())); -} - -TEST(ImgdiffTest, image_mode_merge_chunks) { - // src: "abcdefg" + gzipped_source. - std::string gzipped_source_path = from_testdata_base("gzipped_source"); - std::string gzipped_source; - ASSERT_TRUE(android::base::ReadFileToString(gzipped_source_path, &gzipped_source)); - - const std::string src = "abcdefg" + gzipped_source; - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - // tgt: gzipped_target + "abcdefgxyz". - std::string gzipped_target_path = from_testdata_base("gzipped_target"); - std::string gzipped_target; - ASSERT_TRUE(android::base::ReadFileToString(gzipped_target_path, &gzipped_target)); - - const std::string tgt = gzipped_target + "abcdefgxyz"; - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - // Since a gzipped entry will become CHUNK_RAW (header) + CHUNK_DEFLATE (data) + - // CHUNK_RAW (footer), they both should contain the same chunk types after merging. - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect three entries: CHUNK_RAW (header) + CHUNK_DEFLATE (data) + CHUNK_RAW (footer). - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(1U, num_deflate); - ASSERT_EQ(2U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, image_mode_spurious_magic) { - // src: "abcdefgh" + '0x1f8b0b00' + some bytes. - const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', - 'h', '\x1f', '\x8b', '\x08', '\x00', '\xc4', '\x1e', - '\x53', '\x58', 't', 'e', 's', 't' }; - const std::string src(src_data.cbegin(), src_data.cend()); - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - // tgt: "abcdefgxyz". - const std::vector tgt_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z' }; - const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect one CHUNK_RAW (header) entry. - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(0U, num_deflate); - ASSERT_EQ(1U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, image_mode_short_input1) { - // src: "abcdefgh" + '0x1f8b0b'. - const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', '\x1f', '\x8b', '\x08' }; - const std::string src(src_data.cbegin(), src_data.cend()); - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - // tgt: "abcdefgxyz". - const std::vector tgt_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z' }; - const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect one CHUNK_RAW (header) entry. - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(0U, num_deflate); - ASSERT_EQ(1U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, image_mode_short_input2) { - // src: "abcdefgh" + '0x1f8b0b00'. - const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', '\x1f', '\x8b', '\x08', '\x00' }; - const std::string src(src_data.cbegin(), src_data.cend()); - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - // tgt: "abcdefgxyz". - const std::vector tgt_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z' }; - const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect one CHUNK_RAW (header) entry. - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(0U, num_normal); - ASSERT_EQ(0U, num_deflate); - ASSERT_EQ(1U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgdiffTest, image_mode_single_entry_long) { - // src: "abcdefgh" + '0x1f8b0b00' + some bytes. - const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', - 'h', '\x1f', '\x8b', '\x08', '\x00', '\xc4', '\x1e', - '\x53', '\x58', 't', 'e', 's', 't' }; - const std::string src(src_data.cbegin(), src_data.cend()); - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - // tgt: "abcdefgxyz" + 200 bytes. - std::vector tgt_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z' }; - tgt_data.resize(tgt_data.size() + 200); - - const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - - // Expect one CHUNK_NORMAL entry, since it's exceeding the 160-byte limit for RAW. - size_t num_normal; - size_t num_raw; - size_t num_deflate; - verify_patch_header(patch, &num_normal, &num_raw, &num_deflate); - ASSERT_EQ(1U, num_normal); - ASSERT_EQ(0U, num_deflate); - ASSERT_EQ(0U, num_raw); - - verify_patched_image(src, patch, tgt); -} - -TEST(ImgpatchTest, image_mode_patch_corruption) { - // src: "abcdefgh" + gzipped "xyz" (echo -n "xyz" | gzip -f | hd). - const std::vector src_data = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', - 'h', '\x1f', '\x8b', '\x08', '\x00', '\xc4', '\x1e', - '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xac', - '\x02', '\x00', '\x67', '\xba', '\x8e', '\xeb', '\x03', - '\x00', '\x00', '\x00' }; - const std::string src(src_data.cbegin(), src_data.cend()); - TemporaryFile src_file; - ASSERT_TRUE(android::base::WriteStringToFile(src, src_file.path)); - - // tgt: "abcdefgxyz" + gzipped "xxyyzz". - const std::vector tgt_data = { - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z', '\x1f', '\x8b', - '\x08', '\x00', '\x62', '\x1f', '\x53', '\x58', '\x00', '\x03', '\xab', '\xa8', '\xa8', '\xac', - '\xac', '\xaa', '\x02', '\x00', '\x96', '\x30', '\x06', '\xb7', '\x06', '\x00', '\x00', '\x00' - }; - const std::string tgt(tgt_data.cbegin(), tgt_data.cend()); - TemporaryFile tgt_file; - ASSERT_TRUE(android::base::WriteStringToFile(tgt, tgt_file.path)); - - TemporaryFile patch_file; - std::vector args = { - "imgdiff", src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - // Verify. - std::string patch; - ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch)); - verify_patched_image(src, patch, tgt); - - // Corrupt the end of the patch and expect the ApplyImagePatch to fail. - patch.insert(patch.end() - 10, 10, '0'); - ASSERT_EQ(-1, ApplyImagePatch(reinterpret_cast(src.data()), src.size(), - reinterpret_cast(patch.data()), patch.size(), - [](const unsigned char* /*data*/, size_t len) { return len; })); -} - -static void construct_store_entry(const std::vector>& info, - ZipWriter* writer) { - for (auto& t : info) { - // Create t(1) blocks of t(2), and write the data to t(0) - ASSERT_EQ(0, writer->StartEntry(std::get<0>(t).c_str(), 0)); - const std::string content(std::get<1>(t) * 4096, std::get<2>(t)); - ASSERT_EQ(0, writer->WriteBytes(content.data(), content.size())); - ASSERT_EQ(0, writer->FinishEntry()); - } -} - -static void construct_deflate_entry(const std::vector>& info, - ZipWriter* writer, const std::string& data) { - for (auto& t : info) { - // t(0): entry_name; t(1): block offset; t(2) length in blocks. - ASSERT_EQ(0, writer->StartEntry(std::get<0>(t).c_str(), ZipWriter::kCompress)); - ASSERT_EQ(0, writer->WriteBytes(data.data() + std::get<1>(t) * 4096, std::get<2>(t) * 4096)); - ASSERT_EQ(0, writer->FinishEntry()); - } -} - -// Look for the source and patch pieces in debug_dir. Generate a target piece from each pair. -// Concatenate all the target pieces and match against the orignal one. Used pieces in debug_dir -// will be cleaned up. -static void GenerateAndCheckSplitTarget(const std::string& debug_dir, size_t count, - const std::string& tgt) { - std::string patched; - for (size_t i = 0; i < count; i++) { - std::string split_src_path = android::base::StringPrintf("%s/src-%zu", debug_dir.c_str(), i); - std::string split_src; - ASSERT_TRUE(android::base::ReadFileToString(split_src_path, &split_src)); - ASSERT_EQ(0, unlink(split_src_path.c_str())); - - std::string split_patch_path = - android::base::StringPrintf("%s/patch-%zu", debug_dir.c_str(), i); - std::string split_patch; - ASSERT_TRUE(android::base::ReadFileToString(split_patch_path, &split_patch)); - ASSERT_EQ(0, unlink(split_patch_path.c_str())); - - std::string split_tgt; - GenerateTarget(split_src, split_patch, &split_tgt); - patched += split_tgt; - } - - // Verify we can get back the original target image. - ASSERT_EQ(tgt, patched); -} - -std::vector ConstructImageChunks( - const std::vector& content, const std::vector>& info) { - std::vector chunks; - size_t start = 0; - for (const auto& t : info) { - size_t length = std::get<1>(t); - chunks.emplace_back(CHUNK_NORMAL, start, &content, length, std::get<0>(t)); - start += length; - } - - return chunks; -} - -TEST(ImgdiffTest, zip_mode_split_image_smoke) { - std::vector content; - content.reserve(4096 * 50); - uint8_t n = 0; - generate_n(back_inserter(content), 4096 * 50, [&n]() { return n++ / 4096; }); - - ZipModeImage tgt_image(false, 4096 * 10); - std::vector tgt_chunks = ConstructImageChunks(content, { { "a", 100 }, - { "b", 4096 * 2 }, - { "c", 4096 * 3 }, - { "d", 300 }, - { "e-0", 4096 * 10 }, - { "e-1", 4096 * 5 }, - { "CD", 200 } }); - tgt_image.Initialize(std::move(tgt_chunks), - std::vector(content.begin(), content.begin() + 82520)); - - tgt_image.DumpChunks(); - - ZipModeImage src_image(true, 4096 * 10); - std::vector src_chunks = ConstructImageChunks(content, { { "b", 4096 * 3 }, - { "c-0", 4096 * 10 }, - { "c-1", 4096 * 2 }, - { "a", 4096 * 5 }, - { "e-0", 4096 * 10 }, - { "e-1", 10000 }, - { "CD", 5000 } }); - src_image.Initialize(std::move(src_chunks), - std::vector(content.begin(), content.begin() + 137880)); - - std::vector split_tgt_images; - std::vector split_src_images; - std::vector split_src_ranges; - - ZipModeImage::SplitZipModeImageWithLimit(tgt_image, src_image, &split_tgt_images, - &split_src_images, &split_src_ranges); - - // src_piece 1: a 5 blocks, b 3 blocks - // src_piece 2: c-0 10 blocks - // src_piece 3: d 0 block, e-0 10 blocks - // src_piece 4: e-1 2 blocks; CD 2 blocks - ASSERT_EQ(split_tgt_images.size(), split_src_images.size()); - ASSERT_EQ(static_cast(4), split_tgt_images.size()); - - ASSERT_EQ(static_cast(1), split_tgt_images[0].NumOfChunks()); - ASSERT_EQ(static_cast(12288), split_tgt_images[0][0].DataLengthForPatch()); - ASSERT_EQ("4,0,3,15,20", split_src_ranges[0].ToString()); - - ASSERT_EQ(static_cast(1), split_tgt_images[1].NumOfChunks()); - ASSERT_EQ(static_cast(12288), split_tgt_images[1][0].DataLengthForPatch()); - ASSERT_EQ("2,3,13", split_src_ranges[1].ToString()); - - ASSERT_EQ(static_cast(1), split_tgt_images[2].NumOfChunks()); - ASSERT_EQ(static_cast(40960), split_tgt_images[2][0].DataLengthForPatch()); - ASSERT_EQ("2,20,30", split_src_ranges[2].ToString()); - - ASSERT_EQ(static_cast(1), split_tgt_images[3].NumOfChunks()); - ASSERT_EQ(static_cast(16984), split_tgt_images[3][0].DataLengthForPatch()); - ASSERT_EQ("2,30,34", split_src_ranges[3].ToString()); -} - -TEST(ImgdiffTest, zip_mode_store_large_apk) { - // Construct src and tgt zip files with limit = 10 blocks. - // src tgt - // 12 blocks 'd' 3 blocks 'a' - // 8 blocks 'c' 3 blocks 'b' - // 3 blocks 'b' 8 blocks 'c' (exceeds limit) - // 3 blocks 'a' 12 blocks 'd' (exceeds limit) - // 3 blocks 'e' - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - construct_store_entry( - { { "a", 3, 'a' }, { "b", 3, 'b' }, { "c", 8, 'c' }, { "d", 12, 'd' }, { "e", 3, 'e' } }, - &tgt_writer); - ASSERT_EQ(0, tgt_writer.Finish()); - ASSERT_EQ(0, fclose(tgt_file_ptr)); - - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - construct_store_entry({ { "d", 12, 'd' }, { "c", 8, 'c' }, { "b", 3, 'b' }, { "a", 3, 'a' } }, - &src_writer); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - // Compute patch. - TemporaryFile patch_file; - TemporaryFile split_info_file; - TemporaryDir debug_dir; - std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); - std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); - std::vector args = { - "imgdiff", "-z", "--block-limit=10", split_info_arg.c_str(), debug_dir_arg.c_str(), - src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - - // Expect 4 pieces of patch. (Roughly 3'a',3'b'; 8'c'; 10'd'; 2'd'3'e') - GenerateAndCheckSplitTarget(debug_dir.path, 4, tgt); -} - -TEST(ImgdiffTest, zip_mode_deflate_large_apk) { - // Src and tgt zip files are constructed as follows. - // src tgt - // 22 blocks, "d" 4 blocks, "a" - // 5 blocks, "b" 4 blocks, "b" - // 3 blocks, "a" 8 blocks, "c" (exceeds limit) - // 1 block, "g" 20 blocks, "d" (exceeds limit) - // 8 blocks, "c" 2 blocks, "e" - // 1 block, "f" 1 block , "f" - std::string tgt_path = from_testdata_base("deflate_tgt.zip"); - std::string src_path = from_testdata_base("deflate_src.zip"); - - ZipModeImage src_image(true, 10 * 4096); - ZipModeImage tgt_image(false, 10 * 4096); - ASSERT_TRUE(src_image.Initialize(src_path)); - ASSERT_TRUE(tgt_image.Initialize(tgt_path)); - ASSERT_TRUE(ZipModeImage::CheckAndProcessChunks(&tgt_image, &src_image)); - - src_image.DumpChunks(); - tgt_image.DumpChunks(); - - std::vector split_tgt_images; - std::vector split_src_images; - std::vector split_src_ranges; - ZipModeImage::SplitZipModeImageWithLimit(tgt_image, src_image, &split_tgt_images, - &split_src_images, &split_src_ranges); - - // Expected split images with limit = 10 blocks. - // src_piece 0: a 3 blocks, b 5 blocks - // src_piece 1: c 8 blocks - // src_piece 2: d-0 10 block - // src_piece 3: d-1 10 blocks - // src_piece 4: e 1 block, CD - ASSERT_EQ(split_tgt_images.size(), split_src_images.size()); - ASSERT_EQ(static_cast(5), split_tgt_images.size()); - - ASSERT_EQ(static_cast(2), split_src_images[0].NumOfChunks()); - ASSERT_EQ("a", split_src_images[0][0].GetEntryName()); - ASSERT_EQ("b", split_src_images[0][1].GetEntryName()); - - ASSERT_EQ(static_cast(1), split_src_images[1].NumOfChunks()); - ASSERT_EQ("c", split_src_images[1][0].GetEntryName()); - - ASSERT_EQ(static_cast(0), split_src_images[2].NumOfChunks()); - ASSERT_EQ(static_cast(0), split_src_images[3].NumOfChunks()); - ASSERT_EQ(static_cast(0), split_src_images[4].NumOfChunks()); - - // Compute patch. - TemporaryFile patch_file; - TemporaryFile split_info_file; - TemporaryDir debug_dir; - ASSERT_TRUE(ZipModeImage::GeneratePatches(split_tgt_images, split_src_images, split_src_ranges, - patch_file.path, split_info_file.path, debug_dir.path)); - - // Verify the content of split info. - // Expect 5 pieces of patch. ["a","b"; "c"; "d-0"; "d-1"; "e"] - std::string split_info_string; - android::base::ReadFileToString(split_info_file.path, &split_info_string); - std::vector info_list = - android::base::Split(android::base::Trim(split_info_string), "\n"); - - ASSERT_EQ(static_cast(7), info_list.size()); - ASSERT_EQ("2", android::base::Trim(info_list[0])); - ASSERT_EQ("5", android::base::Trim(info_list[1])); - - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_path, &tgt)); - ASSERT_EQ(static_cast(160385), tgt.size()); - std::vector tgt_file_ranges = { - "36864 2,22,31", "32768 2,31,40", "40960 2,0,11", "40960 2,11,21", "8833 4,21,22,40,41", - }; - - for (size_t i = 0; i < 5; i++) { - struct stat st; - std::string path = android::base::StringPrintf("%s/patch-%zu", debug_dir.path, i); - ASSERT_EQ(0, stat(path.c_str(), &st)); - ASSERT_EQ(std::to_string(st.st_size) + " " + tgt_file_ranges[i], - android::base::Trim(info_list[i + 2])); - } - - GenerateAndCheckSplitTarget(debug_dir.path, 5, tgt); -} - -TEST(ImgdiffTest, zip_mode_no_match_source) { - // Generate 20 blocks of random data. - std::string random_data; - random_data.reserve(4096 * 20); - generate_n(back_inserter(random_data), 4096 * 20, []() { return rand() % 256; }); - - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - - construct_deflate_entry({ { "a", 0, 4 }, { "b", 5, 5 }, { "c", 11, 5 } }, &tgt_writer, - random_data); - - ASSERT_EQ(0, tgt_writer.Finish()); - ASSERT_EQ(0, fclose(tgt_file_ptr)); - - // We don't have a matching source entry. - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - construct_store_entry({ { "d", 1, 'd' } }, &src_writer); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - // Compute patch. - TemporaryFile patch_file; - TemporaryFile split_info_file; - TemporaryDir debug_dir; - std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); - std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); - std::vector args = { - "imgdiff", "-z", "--block-limit=10", debug_dir_arg.c_str(), split_info_arg.c_str(), - src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - - // Expect 1 pieces of patch due to no matching source entry. - GenerateAndCheckSplitTarget(debug_dir.path, 1, tgt); -} - -TEST(ImgdiffTest, zip_mode_large_enough_limit) { - // Generate 20 blocks of random data. - std::string random_data; - random_data.reserve(4096 * 20); - generate_n(back_inserter(random_data), 4096 * 20, []() { return rand() % 256; }); - - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - - construct_deflate_entry({ { "a", 0, 10 }, { "b", 10, 5 } }, &tgt_writer, random_data); - - ASSERT_EQ(0, tgt_writer.Finish()); - ASSERT_EQ(0, fclose(tgt_file_ptr)); - - // Construct 10 blocks of source. - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - construct_deflate_entry({ { "a", 1, 10 } }, &src_writer, random_data); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - // Compute patch with a limit of 20 blocks. - TemporaryFile patch_file; - TemporaryFile split_info_file; - TemporaryDir debug_dir; - std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); - std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); - std::vector args = { - "imgdiff", "-z", "--block-limit=20", split_info_arg.c_str(), debug_dir_arg.c_str(), - src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - - // Expect 1 piece of patch since limit is larger than the zip file size. - GenerateAndCheckSplitTarget(debug_dir.path, 1, tgt); -} - -TEST(ImgdiffTest, zip_mode_large_apk_small_target_chunk) { - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - - // The first entry is less than 4096 bytes, followed immediately by an entry that has a very - // large counterpart in the source file. Therefore the first entry will be patched separately. - std::string small_chunk("a", 2000); - ASSERT_EQ(0, tgt_writer.StartEntry("a", 0)); - ASSERT_EQ(0, tgt_writer.WriteBytes(small_chunk.data(), small_chunk.size())); - ASSERT_EQ(0, tgt_writer.FinishEntry()); - construct_store_entry( - { - { "b", 12, 'b' }, { "c", 3, 'c' }, - }, - &tgt_writer); - ASSERT_EQ(0, tgt_writer.Finish()); - ASSERT_EQ(0, fclose(tgt_file_ptr)); - - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - construct_store_entry({ { "a", 1, 'a' }, { "b", 13, 'b' }, { "c", 1, 'c' } }, &src_writer); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - // Compute patch. - TemporaryFile patch_file; - TemporaryFile split_info_file; - TemporaryDir debug_dir; - std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); - std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); - std::vector args = { - "imgdiff", "-z", "--block-limit=10", split_info_arg.c_str(), debug_dir_arg.c_str(), - src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - - // Expect three split src images: - // src_piece 0: a 1 blocks - // src_piece 1: b-0 10 blocks - // src_piece 2: b-1 3 blocks, c 1 blocks, CD - GenerateAndCheckSplitTarget(debug_dir.path, 3, tgt); -} - -TEST(ImgdiffTest, zip_mode_large_apk_skipped_small_target_chunk) { - TemporaryFile tgt_file; - FILE* tgt_file_ptr = fdopen(tgt_file.release(), "wb"); - ZipWriter tgt_writer(tgt_file_ptr); - - construct_store_entry( - { - { "a", 11, 'a' }, - }, - &tgt_writer); - - // Construct a tiny target entry of 1 byte, which will be skipped due to the tail alignment of - // the previous entry. - std::string small_chunk("b", 1); - ASSERT_EQ(0, tgt_writer.StartEntry("b", 0)); - ASSERT_EQ(0, tgt_writer.WriteBytes(small_chunk.data(), small_chunk.size())); - ASSERT_EQ(0, tgt_writer.FinishEntry()); - - ASSERT_EQ(0, tgt_writer.Finish()); - ASSERT_EQ(0, fclose(tgt_file_ptr)); - - TemporaryFile src_file; - FILE* src_file_ptr = fdopen(src_file.release(), "wb"); - ZipWriter src_writer(src_file_ptr); - construct_store_entry( - { - { "a", 11, 'a' }, { "b", 11, 'b' }, - }, - &src_writer); - ASSERT_EQ(0, src_writer.Finish()); - ASSERT_EQ(0, fclose(src_file_ptr)); - - // Compute patch. - TemporaryFile patch_file; - TemporaryFile split_info_file; - TemporaryDir debug_dir; - std::string split_info_arg = android::base::StringPrintf("--split-info=%s", split_info_file.path); - std::string debug_dir_arg = android::base::StringPrintf("--debug-dir=%s", debug_dir.path); - std::vector args = { - "imgdiff", "-z", "--block-limit=10", split_info_arg.c_str(), debug_dir_arg.c_str(), - src_file.path, tgt_file.path, patch_file.path, - }; - ASSERT_EQ(0, imgdiff(args.size(), args.data())); - - std::string tgt; - ASSERT_TRUE(android::base::ReadFileToString(tgt_file.path, &tgt)); - - // Expect two split src images: - // src_piece 0: a-0 10 blocks - // src_piece 1: a-0 1 block, CD - GenerateAndCheckSplitTarget(debug_dir.path, 2, tgt); -} diff --git a/updater/target_files.cpp b/updater/target_files.cpp index 93540b2e5..092edb9a8 100644 --- a/updater/target_files.cpp +++ b/updater/target_files.cpp @@ -132,6 +132,11 @@ bool TargetFile::ReadEntryToString(const std::string_view name, std::string* con return false; } + if (entry.uncompressed_length == 0) { + content->clear(); + return true; + } + content->resize(entry.uncompressed_length); if (auto extract_err = ExtractToMemory( handle_, &entry, reinterpret_cast(&content->at(0)), entry.uncompressed_length); -- cgit v1.2.3