blob: f5ae7965a33cd926e01dcb2e3348e63e706e2cfa [file] [log] [blame]
Kelvin Zhang893b3a12021-12-30 12:28:53 -08001//
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>
Kelvin Zhangb9a9aa22024-10-15 10:38:35 -070027#include <android-base/stringprintf.h>
Kelvin Zhang893b3a12021-12-30 12:28:53 -080028#include <gtest/gtest.h>
29#include <erofs/internal.h>
30#include <erofs/io.h>
31
32#include "lz4diff/lz4diff.h"
33#include "lz4diff/lz4patch.h"
34#include "update_engine/common/test_utils.h"
35#include "update_engine/common/utils.h"
36#include "update_engine/lz4diff/lz4diff_compress.h"
37#include "update_engine/payload_generator/delta_diff_generator.h"
38#include "update_engine/payload_generator/erofs_filesystem.h"
39#include "update_engine/payload_generator/extent_utils.h"
40
41using std::string;
42using std::vector;
43
44namespace chromeos_update_engine {
45
46namespace {
47class Lz4diffTest : public ::testing::Test {};
48
49using test_utils::GetBuildArtifactsPath;
50
51// This test parses the sample images generated during build time with the
52// "generate_image.sh" script. The expected conditions of each file in these
53// images is encoded in the file name, as defined in the mentioned script.
54TEST_F(Lz4diffTest, DiffElfBinary) {
55 const auto old_img = GetBuildArtifactsPath("gen/erofs.img");
56 const auto new_img = GetBuildArtifactsPath("gen/erofs_new.img");
57 auto old_fs = ErofsFilesystem::CreateFromFile(old_img);
58 ASSERT_NE(old_fs, nullptr);
59 ASSERT_EQ(kBlockSize, old_fs->GetBlockSize());
60 auto new_fs = ErofsFilesystem::CreateFromFile(new_img);
61 ASSERT_NE(new_fs, nullptr);
62 ASSERT_EQ(kBlockSize, new_fs->GetBlockSize());
63
64 vector<ErofsFilesystem::File> old_files;
65 ASSERT_TRUE(old_fs->GetFiles(&old_files));
66 vector<ErofsFilesystem::File> new_files;
67 ASSERT_TRUE(new_fs->GetFiles(&new_files));
68
69 const auto it =
70 std::find_if(old_files.begin(), old_files.end(), [](const auto& file) {
71 return file.name == "/delta_generator";
72 });
73 ASSERT_NE(it, old_files.end())
74 << "There should be a delta_generator entry in gen/erofs.img. Is the "
75 "generate_test_erofs_imgages.sh script implemented wrong?";
76 const auto new_it =
77 std::find_if(new_files.begin(), new_files.end(), [](const auto& file) {
78 return file.name == "/delta_generator";
79 });
80 ASSERT_NE(new_it, new_files.end())
81 << "There should be a delta_generator entry in gen/erofs_new.img. Is the "
82 "generate_test_erofs_imgages.sh script implemented wrong?";
83
84 const auto old_delta_generator = *it;
85 auto new_delta_generator = *new_it;
86 Blob old_data;
87 ASSERT_TRUE(utils::ReadExtents(
88 old_img, old_delta_generator.extents, &old_data, kBlockSize));
89 Blob new_data;
90 ASSERT_TRUE(utils::ReadExtents(
91 new_img, new_delta_generator.extents, &new_data, kBlockSize));
92 // New image is actually generated with compression level 7, we use a
93 // different compression level so that recompressed blob is different. This
94 // way we can test the postfix functionality.
95 new_delta_generator.compressed_file_info.algo.set_level(5);
96 Blob diff_blob;
97 ASSERT_TRUE(Lz4Diff(old_data,
98 new_data,
99 old_delta_generator.compressed_file_info,
100 new_delta_generator.compressed_file_info,
101 &diff_blob));
102 Blob patched_new_data;
103 ASSERT_TRUE(Lz4Patch(old_data, diff_blob, &patched_new_data));
104 ASSERT_EQ(patched_new_data, new_data);
105}
106
107} // namespace
108
109} // namespace chromeos_update_engine