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 "update_engine/common/test_utils.h"
34 #include "update_engine/common/utils.h"
35 #include "update_engine/lz4diff/lz4diff_compress.h"
36 #include "update_engine/payload_generator/delta_diff_generator.h"
37 #include "update_engine/payload_generator/erofs_filesystem.h"
38 #include "update_engine/payload_generator/extent_utils.h"
39
40 using std::string;
41 using std::vector;
42
43 namespace chromeos_update_engine {
44
45 namespace {
46
ExtractErofsImage(const char * erofs_image,const char * inode_path,Blob * output)47 static void ExtractErofsImage(const char* erofs_image,
48 const char* inode_path,
49 Blob* output) {
50 struct erofs_sb_info sbi {};
51 auto err = dev_open_ro(&sbi, erofs_image);
52 ASSERT_EQ(err, 0);
53 DEFER {
54 dev_close(&sbi);
55 };
56
57 err = erofs_read_superblock(&sbi);
58 ASSERT_EQ(err, 0);
59 struct erofs_inode inode {
60 .sbi = &sbi
61 };
62 err = erofs_ilookup(inode_path, &inode);
63 ASSERT_EQ(err, 0) << strerror(-err);
64 output->resize(inode.i_size);
65 err = erofs_pread(&inode,
66 reinterpret_cast<char*>(output->data()),
67 output->size(),
68 0 /* offset */);
69 ASSERT_EQ(err, 0);
70 }
71
72 class Lz4diffCompressTest : public ::testing::Test {};
73
74 using test_utils::GetBuildArtifactsPath;
75
76 // This test parses the sample images generated during build time with the
77 // "generate_image.sh" script. The expected conditions of each file in these
78 // images is encoded in the file name, as defined in the mentioned script.
TEST_F(Lz4diffCompressTest,ExtractElfBinary)79 TEST_F(Lz4diffCompressTest, ExtractElfBinary) {
80 const auto build_path = GetBuildArtifactsPath("gen/erofs.img");
81 auto fs = ErofsFilesystem::CreateFromFile(build_path);
82 ASSERT_NE(fs, nullptr);
83 ASSERT_EQ(kBlockSize, fs->GetBlockSize());
84
85 vector<ErofsFilesystem::File> files;
86 ASSERT_TRUE(fs->GetFiles(&files));
87
88 const auto it =
89 std::find_if(files.begin(), files.end(), [](const auto& file) {
90 return file.name == "/delta_generator";
91 });
92 ASSERT_NE(it, files.end())
93 << "There should be a delta_generator entry in gen/erofs.img. Is the "
94 "generate_test_erofs_imgages.sh script implemented wrong?";
95
96 const auto delta_generator = *it;
97 Blob expected_blob;
98 ASSERT_NO_FATAL_FAILURE(ExtractErofsImage(
99 build_path.c_str(), "/delta_generator", &expected_blob));
100 Blob compressed_blob;
101 ASSERT_TRUE(utils::ReadExtents(
102 build_path, delta_generator.extents, &compressed_blob, kBlockSize));
103 auto decompressed_blob = TryDecompressBlob(
104 compressed_blob,
105 delta_generator.compressed_file_info.blocks,
106 delta_generator.compressed_file_info.zero_padding_enabled);
107 ASSERT_GT(decompressed_blob.size(), 0UL);
108 ASSERT_GE(decompressed_blob.size(),
109 static_cast<size_t>(delta_generator.file_stat.st_size));
110 decompressed_blob.resize(delta_generator.file_stat.st_size);
111 ASSERT_EQ(decompressed_blob, expected_blob);
112 }
113
114 } // namespace
115
116 } // namespace chromeos_update_engine
117