Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 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 | // |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/payload_generator/ext2_filesystem.h" |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <map> |
| 22 | #include <set> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <base/format_macros.h> |
| 27 | #include <base/logging.h> |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 28 | #include <base/strings/string_number_conversions.h> |
| 29 | #include <base/strings/string_util.h> |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include <base/strings/stringprintf.h> |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 31 | #include <gtest/gtest.h> |
| 32 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/test_utils.h" |
| 34 | #include "update_engine/common/utils.h" |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 35 | #include "update_engine/payload_generator/extent_utils.h" |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 36 | |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 37 | using std::map; |
| 38 | using std::set; |
| 39 | using std::string; |
| 40 | using std::unique_ptr; |
| 41 | using std::vector; |
| 42 | |
| 43 | namespace chromeos_update_engine { |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | uint64_t kDefaultFilesystemSize = 4 * 1024 * 1024; |
| 48 | size_t kDefaultFilesystemBlockCount = 1024; |
| 49 | size_t kDefaultFilesystemBlockSize = 4096; |
| 50 | |
| 51 | // Checks that all the blocks in |extents| are in the range [0, total_blocks). |
| 52 | void ExpectBlocksInRange(const vector<Extent>& extents, uint64_t total_blocks) { |
| 53 | for (const Extent& extent : extents) { |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 54 | EXPECT_LE(0U, extent.start_block()); |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 55 | EXPECT_LE(extent.start_block() + extent.num_blocks(), total_blocks); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
Alex Deymo | c90be63 | 2016-02-17 19:25:20 -0800 | [diff] [blame] | 61 | class Ext2FilesystemTest : public ::testing::Test {}; |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 62 | |
| 63 | TEST_F(Ext2FilesystemTest, InvalidFilesystem) { |
Alex Deymo | c90be63 | 2016-02-17 19:25:20 -0800 | [diff] [blame] | 64 | test_utils::ScopedTempFile fs_filename_{"Ext2FilesystemTest-XXXXXX"}; |
| 65 | ASSERT_EQ(0, truncate(fs_filename_.path().c_str(), kDefaultFilesystemSize)); |
| 66 | unique_ptr<Ext2Filesystem> fs = |
| 67 | Ext2Filesystem::CreateFromFile(fs_filename_.path()); |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 68 | ASSERT_EQ(nullptr, fs.get()); |
| 69 | |
| 70 | fs = Ext2Filesystem::CreateFromFile("/path/to/invalid/file"); |
| 71 | ASSERT_EQ(nullptr, fs.get()); |
| 72 | } |
| 73 | |
| 74 | TEST_F(Ext2FilesystemTest, EmptyFilesystem) { |
Alex Deymo | c90be63 | 2016-02-17 19:25:20 -0800 | [diff] [blame] | 75 | base::FilePath path = |
| 76 | test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k_empty.img"); |
| 77 | unique_ptr<Ext2Filesystem> fs = Ext2Filesystem::CreateFromFile(path.value()); |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 78 | |
| 79 | ASSERT_NE(nullptr, fs.get()); |
| 80 | EXPECT_EQ(kDefaultFilesystemBlockCount, fs->GetBlockCount()); |
| 81 | EXPECT_EQ(kDefaultFilesystemBlockSize, fs->GetBlockSize()); |
| 82 | |
| 83 | vector<FilesystemInterface::File> files; |
| 84 | EXPECT_TRUE(fs->GetFiles(&files)); |
| 85 | |
| 86 | map<string, FilesystemInterface::File> map_files; |
| 87 | for (const auto& file : files) { |
| 88 | EXPECT_EQ(map_files.end(), map_files.find(file.name)) |
| 89 | << "File " << file.name << " repeated in the list."; |
| 90 | map_files[file.name] = file; |
| 91 | ExpectBlocksInRange(file.extents, fs->GetBlockCount()); |
| 92 | } |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 93 | EXPECT_EQ(2U, map_files["/"].file_stat.st_ino); |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 94 | EXPECT_FALSE(map_files["<free-space>"].extents.empty()); |
| 95 | } |
| 96 | |
| 97 | // This test parses the sample images generated during build time with the |
| 98 | // "generate_image.sh" script. The expected conditions of each file in these |
| 99 | // images is encoded in the file name, as defined in the mentioned script. |
| 100 | TEST_F(Ext2FilesystemTest, ParseGeneratedImages) { |
| 101 | const vector<string> kGeneratedImages = { |
| 102 | "disk_ext2_1k.img", |
| 103 | "disk_ext2_4k.img" }; |
| 104 | base::FilePath build_path = test_utils::GetBuildArtifactsPath().Append("gen"); |
| 105 | for (const string& fs_name : kGeneratedImages) { |
| 106 | LOG(INFO) << "Testing " << fs_name; |
| 107 | unique_ptr<Ext2Filesystem> fs = Ext2Filesystem::CreateFromFile( |
| 108 | build_path.Append(fs_name).value()); |
| 109 | ASSERT_NE(nullptr, fs.get()); |
| 110 | |
| 111 | vector<FilesystemInterface::File> files; |
| 112 | map<string, FilesystemInterface::File> map_files; |
| 113 | set<string> filenames; |
| 114 | EXPECT_TRUE(fs->GetFiles(&files)); |
| 115 | for (const auto& file : files) { |
| 116 | // Check no repeated files. We should parse hard-links with two different |
| 117 | // names. |
| 118 | EXPECT_EQ(map_files.end(), map_files.find(file.name)) |
| 119 | << "File " << file.name << " repeated in the list."; |
| 120 | map_files[file.name] = file; |
| 121 | filenames.insert(file.name); |
| 122 | ExpectBlocksInRange(file.extents, fs->GetBlockCount()); |
| 123 | } |
| 124 | |
| 125 | // Check that all the files are parsed, and the /removed file should not |
| 126 | // be included in the list. |
| 127 | set<string> kExpectedFiles = { |
| 128 | "/", |
| 129 | "/dir1", |
| 130 | "/dir1/file", |
| 131 | "/dir1/dir2", |
| 132 | "/dir1/dir2/file", |
| 133 | "/dir1/dir2/dir1", |
| 134 | "/empty-file", |
| 135 | "/link-hard-regular-16k", |
| 136 | "/link-long_symlink", |
| 137 | "/link-short_symlink", |
| 138 | "/lost+found", |
| 139 | "/regular-small", |
| 140 | "/regular-16k", |
| 141 | "/regular-32k-zeros", |
| 142 | "/regular-with_net_cap", |
| 143 | "/sparse_empty-10k", |
| 144 | "/sparse_empty-2blocks", |
| 145 | "/sparse-10000blocks", |
| 146 | "/sparse-16k-last_block", |
| 147 | "/sparse-16k-first_block", |
| 148 | "/sparse-16k-holes", |
| 149 | "<inode-blocks>", |
| 150 | "<free-space>", |
| 151 | "<group-descriptors>", |
| 152 | }; |
| 153 | EXPECT_EQ(kExpectedFiles, filenames); |
| 154 | |
| 155 | FilesystemInterface::File file; |
| 156 | |
| 157 | // Small symlinks don't actually have data blocks. |
| 158 | EXPECT_TRUE(map_files["/link-short_symlink"].extents.empty()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 159 | EXPECT_EQ(1U, BlocksInExtents(map_files["/link-long_symlink"].extents)); |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 160 | |
| 161 | // Hard-links report the same list of blocks. |
| 162 | EXPECT_EQ(map_files["/link-hard-regular-16k"].extents, |
| 163 | map_files["/regular-16k"].extents); |
| 164 | EXPECT_FALSE(map_files["/regular-16k"].extents.empty()); |
| 165 | |
| 166 | // The number of blocks in these files doesn't depend on the |
| 167 | // block size. |
| 168 | EXPECT_TRUE(map_files["/empty-file"].extents.empty()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 169 | EXPECT_EQ(1U, BlocksInExtents(map_files["/regular-small"].extents)); |
| 170 | EXPECT_EQ(1U, BlocksInExtents(map_files["/regular-with_net_cap"].extents)); |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 171 | EXPECT_TRUE(map_files["/sparse_empty-10k"].extents.empty()); |
| 172 | EXPECT_TRUE(map_files["/sparse_empty-2blocks"].extents.empty()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 173 | EXPECT_EQ(1U, BlocksInExtents(map_files["/sparse-16k-last_block"].extents)); |
| 174 | EXPECT_EQ(1U, |
| 175 | BlocksInExtents(map_files["/sparse-16k-first_block"].extents)); |
| 176 | EXPECT_EQ(2U, BlocksInExtents(map_files["/sparse-16k-holes"].extents)); |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame] | 180 | TEST_F(Ext2FilesystemTest, LoadSettingsFailsTest) { |
| 181 | base::FilePath path = test_utils::GetBuildArtifactsPath().Append( |
| 182 | "gen/disk_ext2_1k.img"); |
| 183 | unique_ptr<Ext2Filesystem> fs = Ext2Filesystem::CreateFromFile(path.value()); |
Alex Deymo | c90be63 | 2016-02-17 19:25:20 -0800 | [diff] [blame] | 184 | ASSERT_NE(nullptr, fs.get()); |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame] | 185 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 186 | brillo::KeyValueStore store; |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame] | 187 | // disk_ext2_1k.img doesn't have the /etc/update_engine.conf file. |
| 188 | EXPECT_FALSE(fs->LoadSettings(&store)); |
| 189 | } |
| 190 | |
| 191 | TEST_F(Ext2FilesystemTest, LoadSettingsWorksTest) { |
| 192 | base::FilePath path = test_utils::GetBuildArtifactsPath().Append( |
| 193 | "gen/disk_ext2_ue_settings.img"); |
| 194 | unique_ptr<Ext2Filesystem> fs = Ext2Filesystem::CreateFromFile(path.value()); |
Alex Deymo | c90be63 | 2016-02-17 19:25:20 -0800 | [diff] [blame] | 195 | ASSERT_NE(nullptr, fs.get()); |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame] | 196 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 197 | brillo::KeyValueStore store; |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame] | 198 | EXPECT_TRUE(fs->LoadSettings(&store)); |
| 199 | string minor_version; |
| 200 | EXPECT_TRUE(store.GetString("PAYLOAD_MINOR_VERSION", &minor_version)); |
| 201 | EXPECT_EQ("1234", minor_version); |
| 202 | } |
| 203 | |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 204 | } // namespace chromeos_update_engine |