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