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