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 "update_engine/payload_generator/erofs_filesystem.h"
18
19 #include <unistd.h>
20
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 <android-base/stringprintf.h>
28 #include <gtest/gtest.h>
29
30 #include "payload_generator/delta_diff_generator.h"
31 #include "update_engine/common/test_utils.h"
32 #include "update_engine/common/utils.h"
33 #include "update_engine/payload_generator/extent_utils.h"
34
35 using std::string;
36 using std::unique_ptr;
37 using std::vector;
38
39 namespace {
40
41 class ErofsFilesystemTest : public ::testing::Test {};
42
43 } // namespace
44
45 namespace chromeos_update_engine {
46
47 using test_utils::GetBuildArtifactsPath;
48
TEST_F(ErofsFilesystemTest,InvalidFilesystem)49 TEST_F(ErofsFilesystemTest, InvalidFilesystem) {
50 ScopedTempFile fs_filename_{"ErofsFilesystemTest-XXXXXX"};
51 ASSERT_EQ(0, truncate(fs_filename_.path().c_str(), kBlockSize));
52 unique_ptr<ErofsFilesystem> fs =
53 ErofsFilesystem::CreateFromFile(fs_filename_.path());
54 ASSERT_EQ(nullptr, fs.get());
55
56 fs = ErofsFilesystem::CreateFromFile("/path/to/invalid/file");
57 ASSERT_EQ(nullptr, fs.get());
58 }
59
TEST_F(ErofsFilesystemTest,EmptyFilesystem)60 TEST_F(ErofsFilesystemTest, EmptyFilesystem) {
61 unique_ptr<ErofsFilesystem> fs = ErofsFilesystem::CreateFromFile(
62 GetBuildArtifactsPath("gen/erofs_empty.img"));
63
64 ASSERT_NE(nullptr, fs);
65 ASSERT_EQ(kBlockSize, fs->GetBlockSize());
66
67 vector<FilesystemInterface::File> files;
68 ASSERT_TRUE(fs->GetFiles(&files));
69 ASSERT_EQ(files.size(), 0UL);
70 }
71
72 // This test parses the sample images generated during build time with the
73 // "generate_image.sh" script. The expected conditions of each file in these
74 // images is encoded in the file name, as defined in the mentioned script.
TEST_F(ErofsFilesystemTest,ParseGeneratedImages)75 TEST_F(ErofsFilesystemTest, ParseGeneratedImages) {
76 const auto build_path = GetBuildArtifactsPath("gen/erofs.img");
77 auto fs = ErofsFilesystem::CreateFromFile(build_path);
78 ASSERT_NE(fs, nullptr);
79 ASSERT_EQ(kBlockSize, fs->GetBlockSize());
80
81 vector<ErofsFilesystem::File> files;
82 ASSERT_TRUE(fs->GetFiles(&files));
83
84 std::sort(files.begin(), files.end(), [](const auto& a, const auto& b) {
85 return a.name < b.name;
86 });
87 vector<string> filenames;
88 filenames.resize(files.size());
89 std::transform(
90 files.begin(), files.end(), filenames.begin(), [](const auto& file) {
91 return file.name;
92 });
93 const std::vector<std::string> expected_filenames = {
94 "/delta_generator",
95 "/dir1/dir2/dir123/chunks_of_zero",
96 // Empty files are ignored
97 // "/dir1/dir2/dir123/empty",
98 "/dir1/dir2/file0",
99 "/dir1/dir2/file1",
100 "/dir1/dir2/file2",
101 "/dir1/dir2/file4",
102 "/dir1/file0",
103 "/dir1/file2",
104 "/etc/update_engine.conf",
105 "/file1",
106 // Files < 4K are stored inline, and therefore ignored, as they are often
107 // stored not on block boundary.
108 "/generate_test_erofs_images.sh"};
109 ASSERT_EQ(filenames, expected_filenames);
110 const auto delta_generator = files[0];
111 ASSERT_GT(delta_generator.compressed_file_info.blocks.size(), 0UL);
112 size_t compressed_size = 0;
113 size_t uncompressed_size = 0;
114 for (const auto& block : delta_generator.compressed_file_info.blocks) {
115 compressed_size += block.compressed_length;
116 uncompressed_size += block.uncompressed_length;
117 }
118 ASSERT_GE(uncompressed_size,
119 static_cast<size_t>(delta_generator.file_stat.st_size))
120 << "Uncompressed data should be at least as big as original file, plus "
121 "possible trailing data.";
122 const auto total_blocks = utils::BlocksInExtents(delta_generator.extents);
123 ASSERT_EQ(compressed_size, total_blocks * kBlockSize);
124 }
125
126 } // namespace chromeos_update_engine
127