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" |
| 41 | #include "otautil/paths.h" |
| 42 | #include "otautil/print_sha1.h" |
| 43 | |
| 44 | using namespace std::string_literals; |
| 45 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 46 | class ApplyPatchTest : public ::testing::Test { |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 47 | protected: |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 48 | void SetUp() override { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 49 | old_file = from_testdata_base("old.file"); |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 50 | FileContents old_fc; |
| 51 | ASSERT_EQ(0, LoadFileContents(old_file, &old_fc)); |
| 52 | old_sha1 = print_sha1(old_fc.sha1); |
| 53 | old_size = old_fc.data.size(); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 54 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 55 | new_file = from_testdata_base("new.file"); |
| 56 | FileContents new_fc; |
| 57 | ASSERT_EQ(0, LoadFileContents(new_file, &new_fc)); |
| 58 | new_sha1 = print_sha1(new_fc.sha1); |
| 59 | new_size = new_fc.data.size(); |
| 60 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 61 | srand(time(nullptr)); |
| 62 | bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 63 | bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 64 | |
| 65 | // Reset the cache backup file. |
| 66 | Paths::Get().set_cache_temp_source("/cache/saved.file"); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | std::string old_file; |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 70 | std::string old_sha1; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 71 | size_t old_size; |
| 72 | |
| 73 | std::string new_file; |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 74 | std::string new_sha1; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 75 | size_t new_size; |
| 76 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 77 | std::string bad_sha1_a; |
| 78 | std::string bad_sha1_b; |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 81 | TEST_F(ApplyPatchTest, CheckMode) { |
| 82 | std::string partition = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1; |
| 83 | ASSERT_EQ(0, applypatch_check(partition, {})); |
| 84 | ASSERT_EQ(0, applypatch_check(partition, { old_sha1 })); |
| 85 | ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, bad_sha1_b })); |
| 86 | ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, old_sha1, bad_sha1_b })); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 89 | TEST_F(ApplyPatchTest, CheckMode_NonEmmcTarget) { |
| 90 | ASSERT_NE(0, applypatch_check(old_file, {})); |
| 91 | ASSERT_NE(0, applypatch_check(old_file, { old_sha1 })); |
| 92 | ASSERT_NE(0, applypatch_check(old_file, { bad_sha1_a, bad_sha1_b })); |
| 93 | ASSERT_NE(0, applypatch_check(old_file, { bad_sha1_a, old_sha1, bad_sha1_b })); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 96 | TEST_F(ApplyPatchTest, CheckMode_EmmcTarget) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 97 | // EMMC:old_file:size:sha1 should pass the check. |
| 98 | std::string src_file = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 99 | ASSERT_EQ(0, applypatch_check(src_file, {})); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 100 | |
| 101 | // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 102 | src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 103 | std::to_string(old_size + 1) + ":" + old_sha1; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 104 | ASSERT_NE(0, applypatch_check(src_file, {})); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 105 | |
| 106 | // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 107 | src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 108 | std::to_string(old_size) + ":" + old_sha1 + ":" + std::to_string(old_size + 1) + ":" + |
| 109 | old_sha1; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 110 | ASSERT_EQ(0, applypatch_check(src_file, {})); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 111 | |
| 112 | // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check. |
| 113 | src_file = "EMMC:" + old_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 114 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" + |
| 115 | old_sha1; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 116 | ASSERT_EQ(0, applypatch_check(src_file, {})); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 117 | |
| 118 | // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1 |
| 119 | // should pass the check. |
| 120 | src_file = "EMMC:" + new_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 121 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" + |
| 122 | old_sha1 + ":" + std::to_string(new_size) + ":" + new_sha1; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 123 | ASSERT_EQ(0, applypatch_check(src_file, {})); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 126 | TEST_F(ApplyPatchTest, CheckMode_UseBackup) { |
| 127 | std::string corrupted = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + bad_sha1_a; |
| 128 | ASSERT_NE(0, applypatch_check(corrupted, { old_sha1 })); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 129 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 130 | Paths::Get().set_cache_temp_source(old_file); |
| 131 | ASSERT_EQ(0, applypatch_check(corrupted, { old_sha1 })); |
| 132 | ASSERT_EQ(0, applypatch_check(corrupted, { bad_sha1_a, old_sha1, bad_sha1_b })); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 135 | TEST_F(ApplyPatchTest, CheckMode_UseBackup_BothCorrupted) { |
| 136 | std::string corrupted = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + bad_sha1_a; |
| 137 | ASSERT_NE(0, applypatch_check(corrupted, {})); |
| 138 | ASSERT_NE(0, applypatch_check(corrupted, { old_sha1 })); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 139 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 140 | Paths::Get().set_cache_temp_source(old_file); |
| 141 | ASSERT_NE(0, applypatch_check(corrupted, { bad_sha1_a, bad_sha1_b })); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | class FreeCacheTest : public ::testing::Test { |
| 145 | protected: |
| 146 | static constexpr size_t PARTITION_SIZE = 4096 * 10; |
| 147 | |
| 148 | // Returns a sorted list of files in |dirname|. |
| 149 | static std::vector<std::string> FindFilesInDir(const std::string& dirname) { |
| 150 | std::vector<std::string> file_list; |
| 151 | |
| 152 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir); |
| 153 | struct dirent* de; |
| 154 | while ((de = readdir(d.get())) != 0) { |
| 155 | std::string path = dirname + "/" + de->d_name; |
| 156 | |
| 157 | struct stat st; |
| 158 | if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { |
| 159 | file_list.emplace_back(de->d_name); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | std::sort(file_list.begin(), file_list.end()); |
| 164 | return file_list; |
| 165 | } |
| 166 | |
| 167 | static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) { |
| 168 | std::string zeros(4096, 0); |
| 169 | for (const auto& file : files) { |
| 170 | std::string path = dir + "/" + file; |
| 171 | ASSERT_TRUE(android::base::WriteStringToFile(zeros, path)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void SetUp() override { |
| 176 | Paths::Get().set_cache_log_directory(mock_log_dir.path); |
| 177 | } |
| 178 | |
| 179 | // A mock method to calculate the free space. It assumes the partition has a total size of 40960 |
| 180 | // bytes and all files are 4096 bytes in size. |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 181 | int64_t MockFreeSpaceChecker(const std::string& dirname) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 182 | std::vector<std::string> files = FindFilesInDir(dirname); |
| 183 | return PARTITION_SIZE - 4096 * files.size(); |
| 184 | } |
| 185 | |
| 186 | TemporaryDir mock_cache; |
| 187 | TemporaryDir mock_log_dir; |
| 188 | }; |
| 189 | |
| 190 | TEST_F(FreeCacheTest, FreeCacheSmoke) { |
| 191 | std::vector<std::string> files = { "file1", "file2", "file3" }; |
| 192 | AddFilesToDir(mock_cache.path, files); |
| 193 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 194 | ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); |
| 195 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 196 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [this](const std::string& dir) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 197 | return this->MockFreeSpaceChecker(dir); |
| 198 | })); |
| 199 | |
| 200 | ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path)); |
| 201 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path)); |
| 202 | } |
| 203 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 204 | TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) { |
| 205 | std::vector<std::string> files{ "file1", "file2", "file3" }; |
| 206 | AddFilesToDir(mock_cache.path, files); |
| 207 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 208 | ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); |
| 209 | |
| 210 | ASSERT_FALSE( |
| 211 | RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; })); |
| 212 | } |
| 213 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 214 | TEST_F(FreeCacheTest, FreeCacheOpenFile) { |
| 215 | std::vector<std::string> files = { "file1", "file2" }; |
| 216 | AddFilesToDir(mock_cache.path, files); |
| 217 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 218 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path)); |
| 219 | |
| 220 | std::string file1_path = mock_cache.path + "/file1"s; |
| 221 | android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY)); |
| 222 | |
| 223 | // file1 can't be deleted as it's opened by us. |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 224 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [this](const std::string& dir) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 225 | return this->MockFreeSpaceChecker(dir); |
| 226 | })); |
| 227 | |
| 228 | ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path)); |
| 229 | } |
| 230 | |
| 231 | TEST_F(FreeCacheTest, FreeCacheLogsSmoke) { |
| 232 | std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5", |
| 233 | "last_log.10" }; |
| 234 | AddFilesToDir(mock_log_dir.path, log_files); |
| 235 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 236 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 237 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [this](const std::string& dir) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 238 | return this->MockFreeSpaceChecker(dir); |
| 239 | })); |
| 240 | |
| 241 | // Logs with a higher index will be deleted first |
| 242 | std::vector<std::string> expected = { "last_log", "last_log.1" }; |
| 243 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 244 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path)); |
| 245 | } |
| 246 | |
| 247 | TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) { |
| 248 | std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number", |
| 249 | "last_kmsgrandom" }; |
| 250 | AddFilesToDir(mock_log_dir.path, log_files); |
| 251 | ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path)); |
| 252 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 253 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [this](const std::string& dir) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 254 | return this->MockFreeSpaceChecker(dir); |
| 255 | })); |
| 256 | |
| 257 | // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is |
| 258 | // deleted before last_log. |
| 259 | std::vector<std::string> expected = { "last_log.1" }; |
| 260 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 261 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path)); |
| 262 | } |
| 263 | |
| 264 | TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) { |
| 265 | std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log", |
| 266 | "last_kmsg.1" }; |
| 267 | AddFilesToDir(mock_log_dir.path, log_files); |
| 268 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 269 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 270 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [this](const std::string& dir) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 271 | return this->MockFreeSpaceChecker(dir); |
| 272 | })); |
| 273 | |
| 274 | // Non log files in /cache/recovery won't be deleted. |
| 275 | std::vector<std::string> expected = { "block.map", "command", "last_install" }; |
| 276 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 277 | } |