| Kelvin Zhang | 893b3a1 | 2021-12-30 12:28:53 -0800 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2021 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
|  | 16 |  | 
|  | 17 | #include <unistd.h> | 
|  | 18 |  | 
|  | 19 | #include <algorithm> | 
|  | 20 | #include <mutex> | 
|  | 21 | #include <string> | 
|  | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <base/format_macros.h> | 
|  | 25 | #include <base/logging.h> | 
|  | 26 | #include <base/strings/string_number_conversions.h> | 
|  | 27 | #include <base/strings/string_util.h> | 
|  | 28 | #include <base/strings/stringprintf.h> | 
|  | 29 | #include <gtest/gtest.h> | 
|  | 30 | #include <erofs/internal.h> | 
|  | 31 | #include <erofs/io.h> | 
|  | 32 |  | 
|  | 33 | #include "lz4diff/lz4diff.h" | 
|  | 34 | #include "lz4diff/lz4patch.h" | 
|  | 35 | #include "update_engine/common/test_utils.h" | 
|  | 36 | #include "update_engine/common/utils.h" | 
|  | 37 | #include "update_engine/lz4diff/lz4diff_compress.h" | 
|  | 38 | #include "update_engine/payload_generator/delta_diff_generator.h" | 
|  | 39 | #include "update_engine/payload_generator/erofs_filesystem.h" | 
|  | 40 | #include "update_engine/payload_generator/extent_utils.h" | 
|  | 41 |  | 
|  | 42 | using std::string; | 
|  | 43 | using std::vector; | 
|  | 44 |  | 
|  | 45 | namespace chromeos_update_engine { | 
|  | 46 |  | 
|  | 47 | namespace { | 
|  | 48 | class Lz4diffTest : public ::testing::Test {}; | 
|  | 49 |  | 
|  | 50 | using test_utils::GetBuildArtifactsPath; | 
|  | 51 |  | 
|  | 52 | // This test parses the sample images generated during build time with the | 
|  | 53 | // "generate_image.sh" script. The expected conditions of each file in these | 
|  | 54 | // images is encoded in the file name, as defined in the mentioned script. | 
|  | 55 | TEST_F(Lz4diffTest, DiffElfBinary) { | 
|  | 56 | const auto old_img = GetBuildArtifactsPath("gen/erofs.img"); | 
|  | 57 | const auto new_img = GetBuildArtifactsPath("gen/erofs_new.img"); | 
|  | 58 | auto old_fs = ErofsFilesystem::CreateFromFile(old_img); | 
|  | 59 | ASSERT_NE(old_fs, nullptr); | 
|  | 60 | ASSERT_EQ(kBlockSize, old_fs->GetBlockSize()); | 
|  | 61 | auto new_fs = ErofsFilesystem::CreateFromFile(new_img); | 
|  | 62 | ASSERT_NE(new_fs, nullptr); | 
|  | 63 | ASSERT_EQ(kBlockSize, new_fs->GetBlockSize()); | 
|  | 64 |  | 
|  | 65 | vector<ErofsFilesystem::File> old_files; | 
|  | 66 | ASSERT_TRUE(old_fs->GetFiles(&old_files)); | 
|  | 67 | vector<ErofsFilesystem::File> new_files; | 
|  | 68 | ASSERT_TRUE(new_fs->GetFiles(&new_files)); | 
|  | 69 |  | 
|  | 70 | const auto it = | 
|  | 71 | std::find_if(old_files.begin(), old_files.end(), [](const auto& file) { | 
|  | 72 | return file.name == "/delta_generator"; | 
|  | 73 | }); | 
|  | 74 | ASSERT_NE(it, old_files.end()) | 
|  | 75 | << "There should be a delta_generator entry in gen/erofs.img. Is the " | 
|  | 76 | "generate_test_erofs_imgages.sh script implemented wrong?"; | 
|  | 77 | const auto new_it = | 
|  | 78 | std::find_if(new_files.begin(), new_files.end(), [](const auto& file) { | 
|  | 79 | return file.name == "/delta_generator"; | 
|  | 80 | }); | 
|  | 81 | ASSERT_NE(new_it, new_files.end()) | 
|  | 82 | << "There should be a delta_generator entry in gen/erofs_new.img. Is the " | 
|  | 83 | "generate_test_erofs_imgages.sh script implemented wrong?"; | 
|  | 84 |  | 
|  | 85 | const auto old_delta_generator = *it; | 
|  | 86 | auto new_delta_generator = *new_it; | 
|  | 87 | Blob old_data; | 
|  | 88 | ASSERT_TRUE(utils::ReadExtents( | 
|  | 89 | old_img, old_delta_generator.extents, &old_data, kBlockSize)); | 
|  | 90 | Blob new_data; | 
|  | 91 | ASSERT_TRUE(utils::ReadExtents( | 
|  | 92 | new_img, new_delta_generator.extents, &new_data, kBlockSize)); | 
|  | 93 | // New image is actually generated with compression level 7, we use a | 
|  | 94 | // different compression level so that recompressed blob is different. This | 
|  | 95 | // way we can test the postfix functionality. | 
|  | 96 | new_delta_generator.compressed_file_info.algo.set_level(5); | 
|  | 97 | Blob diff_blob; | 
|  | 98 | ASSERT_TRUE(Lz4Diff(old_data, | 
|  | 99 | new_data, | 
|  | 100 | old_delta_generator.compressed_file_info, | 
|  | 101 | new_delta_generator.compressed_file_info, | 
|  | 102 | &diff_blob)); | 
|  | 103 | Blob patched_new_data; | 
|  | 104 | ASSERT_TRUE(Lz4Patch(old_data, diff_blob, &patched_new_data)); | 
|  | 105 | ASSERT_EQ(patched_new_data, new_data); | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | }  // namespace | 
|  | 109 |  | 
|  | 110 | }  // namespace chromeos_update_engine |