Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 agree 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 <dirent.h> |
| 18 | #include <fcntl.h> |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <time.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <algorithm> |
| 28 | #include <memory> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include <android-base/file.h> |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
| 35 | #include <android-base/test_utils.h> |
| 36 | #include <android-base/unique_fd.h> |
| 37 | #include <gtest/gtest.h> |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include "applypatch/applypatch.h" |
| 40 | #include "common/test_constants.h" |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 41 | #include "edify/expr.h" |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 42 | #include "otautil/paths.h" |
| 43 | #include "otautil/print_sha1.h" |
| 44 | |
| 45 | using namespace std::string_literals; |
| 46 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 47 | class ApplyPatchTest : public ::testing::Test { |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 48 | protected: |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 49 | void SetUp() override { |
Tao Bao | cdbe58a | 2018-08-20 08:57:19 -0700 | [diff] [blame] | 50 | source_file = from_testdata_base("boot.img"); |
| 51 | FileContents boot_fc; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 52 | ASSERT_TRUE(LoadFileContents(source_file, &boot_fc)); |
Tao Bao | cdbe58a | 2018-08-20 08:57:19 -0700 | [diff] [blame] | 53 | source_size = boot_fc.data.size(); |
| 54 | source_sha1 = print_sha1(boot_fc.sha1); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 55 | |
Tao Bao | cdbe58a | 2018-08-20 08:57:19 -0700 | [diff] [blame] | 56 | target_file = from_testdata_base("recovery.img"); |
| 57 | FileContents recovery_fc; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 58 | ASSERT_TRUE(LoadFileContents(target_file, &recovery_fc)); |
Tao Bao | cdbe58a | 2018-08-20 08:57:19 -0700 | [diff] [blame] | 59 | target_size = recovery_fc.data.size(); |
| 60 | target_sha1 = print_sha1(recovery_fc.sha1); |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 61 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 62 | source_partition = Partition(source_file, source_size, source_sha1); |
| 63 | target_partition = Partition(partition_file.path, target_size, target_sha1); |
| 64 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 65 | srand(time(nullptr)); |
| 66 | bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 67 | bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 68 | |
| 69 | // Reset the cache backup file. |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 70 | Paths::Get().set_cache_temp_source(cache_temp_source.path); |
| 71 | } |
| 72 | |
| 73 | void TearDown() override { |
| 74 | ASSERT_TRUE(android::base::RemoveFileIfExists(cache_temp_source.path)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Tao Bao | cdbe58a | 2018-08-20 08:57:19 -0700 | [diff] [blame] | 77 | std::string source_file; |
| 78 | std::string source_sha1; |
| 79 | size_t source_size; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 80 | |
Tao Bao | cdbe58a | 2018-08-20 08:57:19 -0700 | [diff] [blame] | 81 | std::string target_file; |
| 82 | std::string target_sha1; |
| 83 | size_t target_size; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 84 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 85 | std::string bad_sha1_a; |
| 86 | std::string bad_sha1_b; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 87 | |
| 88 | Partition source_partition; |
| 89 | Partition target_partition; |
| 90 | |
| 91 | private: |
| 92 | TemporaryFile partition_file; |
| 93 | TemporaryFile cache_temp_source; |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 96 | TEST_F(ApplyPatchTest, CheckPartition) { |
| 97 | ASSERT_TRUE(CheckPartition(source_partition)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 100 | TEST_F(ApplyPatchTest, CheckPartition_Mismatching) { |
| 101 | ASSERT_FALSE(CheckPartition(Partition(source_file, target_size, target_sha1))); |
| 102 | ASSERT_FALSE(CheckPartition(Partition(source_file, source_size, bad_sha1_a))); |
| 103 | |
| 104 | ASSERT_FALSE(CheckPartition(Partition(source_file, source_size - 1, source_sha1))); |
| 105 | ASSERT_FALSE(CheckPartition(Partition(source_file, source_size + 1, source_sha1))); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 108 | TEST_F(ApplyPatchTest, PatchPartitionCheck) { |
| 109 | ASSERT_TRUE(PatchPartitionCheck(target_partition, source_partition)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 110 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 111 | ASSERT_TRUE( |
| 112 | PatchPartitionCheck(Partition(source_file, source_size - 1, source_sha1), source_partition)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 113 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 114 | ASSERT_TRUE( |
| 115 | PatchPartitionCheck(Partition(source_file, source_size + 1, source_sha1), source_partition)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 118 | TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup) { |
| 119 | ASSERT_FALSE( |
| 120 | PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1))); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 121 | |
Tao Bao | cdbe58a | 2018-08-20 08:57:19 -0700 | [diff] [blame] | 122 | Paths::Get().set_cache_temp_source(source_file); |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 123 | ASSERT_TRUE( |
| 124 | PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1))); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 127 | TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup_BothCorrupted) { |
| 128 | ASSERT_FALSE( |
| 129 | PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1))); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 130 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 131 | Paths::Get().set_cache_temp_source(target_file); |
| 132 | ASSERT_FALSE( |
| 133 | PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1))); |
| 134 | } |
| 135 | |
| 136 | TEST_F(ApplyPatchTest, PatchPartition) { |
| 137 | FileContents patch_fc; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 138 | ASSERT_TRUE(LoadFileContents(from_testdata_base("recovery-from-boot.p"), &patch_fc)); |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 139 | Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend())); |
| 140 | |
| 141 | FileContents bonus_fc; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 142 | ASSERT_TRUE(LoadFileContents(from_testdata_base("bonus.file"), &bonus_fc)); |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 143 | Value bonus(Value::Type::BLOB, std::string(bonus_fc.data.cbegin(), bonus_fc.data.cend())); |
| 144 | |
| 145 | ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, &bonus)); |
| 146 | } |
| 147 | |
| 148 | // Tests patching an eMMC target without a separate bonus file (i.e. recovery-from-boot patch has |
| 149 | // everything). |
| 150 | TEST_F(ApplyPatchTest, PatchPartitionWithoutBonusFile) { |
| 151 | FileContents patch_fc; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 152 | ASSERT_TRUE(LoadFileContents(from_testdata_base("recovery-from-boot-with-bonus.p"), &patch_fc)); |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 153 | Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend())); |
| 154 | |
| 155 | ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, nullptr)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | class FreeCacheTest : public ::testing::Test { |
| 159 | protected: |
| 160 | static constexpr size_t PARTITION_SIZE = 4096 * 10; |
| 161 | |
| 162 | // Returns a sorted list of files in |dirname|. |
| 163 | static std::vector<std::string> FindFilesInDir(const std::string& dirname) { |
| 164 | std::vector<std::string> file_list; |
| 165 | |
| 166 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir); |
| 167 | struct dirent* de; |
| 168 | while ((de = readdir(d.get())) != 0) { |
| 169 | std::string path = dirname + "/" + de->d_name; |
| 170 | |
| 171 | struct stat st; |
| 172 | if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { |
| 173 | file_list.emplace_back(de->d_name); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | std::sort(file_list.begin(), file_list.end()); |
| 178 | return file_list; |
| 179 | } |
| 180 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 181 | void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 182 | std::string zeros(4096, 0); |
| 183 | for (const auto& file : files) { |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 184 | temporary_files_.push_back(dir + "/" + file); |
| 185 | ASSERT_TRUE(android::base::WriteStringToFile(zeros, temporary_files_.back())); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | void SetUp() override { |
| 190 | Paths::Get().set_cache_log_directory(mock_log_dir.path); |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 191 | temporary_files_.clear(); |
| 192 | } |
| 193 | |
| 194 | void TearDown() override { |
| 195 | for (const auto& file : temporary_files_) { |
| 196 | ASSERT_TRUE(android::base::RemoveFileIfExists(file)); |
| 197 | } |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // A mock method to calculate the free space. It assumes the partition has a total size of 40960 |
| 201 | // bytes and all files are 4096 bytes in size. |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 202 | static size_t MockFreeSpaceChecker(const std::string& dirname) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 203 | std::vector<std::string> files = FindFilesInDir(dirname); |
| 204 | return PARTITION_SIZE - 4096 * files.size(); |
| 205 | } |
| 206 | |
| 207 | TemporaryDir mock_cache; |
| 208 | TemporaryDir mock_log_dir; |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 209 | |
| 210 | private: |
| 211 | std::vector<std::string> temporary_files_; |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | TEST_F(FreeCacheTest, FreeCacheSmoke) { |
| 215 | std::vector<std::string> files = { "file1", "file2", "file3" }; |
| 216 | AddFilesToDir(mock_cache.path, files); |
| 217 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 218 | ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); |
| 219 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 220 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 221 | |
| 222 | ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path)); |
| 223 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path)); |
| 224 | } |
| 225 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 226 | TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) { |
| 227 | std::vector<std::string> files{ "file1", "file2", "file3" }; |
| 228 | AddFilesToDir(mock_cache.path, files); |
| 229 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 230 | ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); |
| 231 | |
| 232 | ASSERT_FALSE( |
| 233 | RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; })); |
| 234 | } |
| 235 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 236 | TEST_F(FreeCacheTest, FreeCacheOpenFile) { |
| 237 | std::vector<std::string> files = { "file1", "file2" }; |
| 238 | AddFilesToDir(mock_cache.path, files); |
| 239 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 240 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path)); |
| 241 | |
| 242 | std::string file1_path = mock_cache.path + "/file1"s; |
| 243 | android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY)); |
| 244 | |
| 245 | // file1 can't be deleted as it's opened by us. |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 246 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 247 | |
| 248 | ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path)); |
| 249 | } |
| 250 | |
| 251 | TEST_F(FreeCacheTest, FreeCacheLogsSmoke) { |
| 252 | std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5", |
| 253 | "last_log.10" }; |
| 254 | AddFilesToDir(mock_log_dir.path, log_files); |
| 255 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 256 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 257 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 258 | |
| 259 | // Logs with a higher index will be deleted first |
| 260 | std::vector<std::string> expected = { "last_log", "last_log.1" }; |
| 261 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 262 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path)); |
| 263 | } |
| 264 | |
| 265 | TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) { |
| 266 | std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number", |
| 267 | "last_kmsgrandom" }; |
| 268 | AddFilesToDir(mock_log_dir.path, log_files); |
| 269 | ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path)); |
| 270 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 271 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 272 | |
| 273 | // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is |
| 274 | // deleted before last_log. |
| 275 | std::vector<std::string> expected = { "last_log.1" }; |
| 276 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 277 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path)); |
| 278 | } |
| 279 | |
| 280 | TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) { |
| 281 | std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log", |
| 282 | "last_kmsg.1" }; |
| 283 | AddFilesToDir(mock_log_dir.path, log_files); |
| 284 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 285 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 286 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 287 | |
| 288 | // Non log files in /cache/recovery won't be deleted. |
| 289 | std::vector<std::string> expected = { "block.map", "command", "last_install" }; |
| 290 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 291 | } |