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 <base/strings/string_util.h>
28 #include <base/strings/stringprintf.h>
29 #include <gtest/gtest.h>
30
31 #include "payload_generator/delta_diff_generator.h"
32 #include "update_engine/common/test_utils.h"
33 #include "update_engine/common/utils.h"
34 #include "update_engine/payload_generator/extent_utils.h"
35
36 using std::string;
37 using std::unique_ptr;
38 using std::vector;
39
40 namespace {
41
42 class ErofsFilesystemTest : public ::testing::Test {};
43
44 } // namespace
45
46 namespace chromeos_update_engine {
47
48 using test_utils::GetBuildArtifactsPath;
49
TEST_F(ErofsFilesystemTest,InvalidFilesystem)50 TEST_F(ErofsFilesystemTest, InvalidFilesystem) {
51 ScopedTempFile fs_filename_{"ErofsFilesystemTest-XXXXXX"};
52 ASSERT_EQ(0, truncate(fs_filename_.path().c_str(), kBlockSize));
53 unique_ptr<ErofsFilesystem> fs =
54 ErofsFilesystem::CreateFromFile(fs_filename_.path());
55 ASSERT_EQ(nullptr, fs.get());
56
57 fs = ErofsFilesystem::CreateFromFile("/path/to/invalid/file");
58 ASSERT_EQ(nullptr, fs.get());
59 }
60
TEST_F(ErofsFilesystemTest,EmptyFilesystem)61 TEST_F(ErofsFilesystemTest, EmptyFilesystem) {
62 unique_ptr<ErofsFilesystem> fs = ErofsFilesystem::CreateFromFile(
63 GetBuildArtifactsPath("gen/erofs_empty.img"));
64
65 ASSERT_NE(nullptr, fs);
66 ASSERT_EQ(kBlockSize, fs->GetBlockSize());
67
68 vector<FilesystemInterface::File> files;
69 ASSERT_TRUE(fs->GetFiles(&files));
70 ASSERT_EQ(files.size(), 0UL);
71 }
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(ErofsFilesystemTest,ParseGeneratedImages)76 TEST_F(ErofsFilesystemTest, ParseGeneratedImages) {
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 std::sort(files.begin(), files.end(), [](const auto& a, const auto& b) {
86 return a.name < b.name;
87 });
88 vector<string> filenames;
89 filenames.resize(files.size());
90 std::transform(
91 files.begin(), files.end(), filenames.begin(), [](const auto& file) {
92 return file.name;
93 });
94 const std::vector<std::string> expected_filenames = {
95 "/delta_generator",
96 "/dir1/dir2/dir123/chunks_of_zero",
97 // Empty files are ignored
98 // "/dir1/dir2/dir123/empty",
99 "/dir1/dir2/file0",
100 "/dir1/dir2/file1",
101 "/dir1/dir2/file2",
102 "/dir1/dir2/file4",
103 "/dir1/file0",
104 "/dir1/file2",
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 };
110 ASSERT_EQ(filenames, expected_filenames);
111 const auto delta_generator = files[0];
112 ASSERT_GT(delta_generator.compressed_file_info.blocks.size(), 0UL);
113 size_t compressed_size = 0;
114 size_t uncompressed_size = 0;
115 for (const auto& block : delta_generator.compressed_file_info.blocks) {
116 compressed_size += block.compressed_length;
117 uncompressed_size += block.uncompressed_length;
118 }
119 ASSERT_GE(uncompressed_size,
120 static_cast<size_t>(delta_generator.file_stat.st_size))
121 << "Uncompressed data should be at least as big as original file, plus "
122 "possible trailing data.";
123 const auto total_blocks = utils::BlocksInExtents(delta_generator.extents);
124 ASSERT_EQ(compressed_size, total_blocks * kBlockSize);
125 }
126
127 } // namespace chromeos_update_engine
128