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 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 167 | void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 168 | std::string zeros(4096, 0); |
| 169 | for (const auto& file : files) { |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 170 | temporary_files_.push_back(dir + "/" + file); |
| 171 | ASSERT_TRUE(android::base::WriteStringToFile(zeros, temporary_files_.back())); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | void SetUp() override { |
| 176 | Paths::Get().set_cache_log_directory(mock_log_dir.path); |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 177 | temporary_files_.clear(); |
| 178 | } |
| 179 | |
| 180 | void TearDown() override { |
| 181 | for (const auto& file : temporary_files_) { |
| 182 | ASSERT_TRUE(android::base::RemoveFileIfExists(file)); |
| 183 | } |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // A mock method to calculate the free space. It assumes the partition has a total size of 40960 |
| 187 | // bytes and all files are 4096 bytes in size. |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 188 | static size_t MockFreeSpaceChecker(const std::string& dirname) { |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 189 | std::vector<std::string> files = FindFilesInDir(dirname); |
| 190 | return PARTITION_SIZE - 4096 * files.size(); |
| 191 | } |
| 192 | |
| 193 | TemporaryDir mock_cache; |
| 194 | TemporaryDir mock_log_dir; |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 195 | |
| 196 | private: |
| 197 | std::vector<std::string> temporary_files_; |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | TEST_F(FreeCacheTest, FreeCacheSmoke) { |
| 201 | std::vector<std::string> files = { "file1", "file2", "file3" }; |
| 202 | AddFilesToDir(mock_cache.path, files); |
| 203 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 204 | ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); |
| 205 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 206 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 207 | |
| 208 | ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path)); |
| 209 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path)); |
| 210 | } |
| 211 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 212 | TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) { |
| 213 | std::vector<std::string> files{ "file1", "file2", "file3" }; |
| 214 | AddFilesToDir(mock_cache.path, files); |
| 215 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 216 | ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); |
| 217 | |
| 218 | ASSERT_FALSE( |
| 219 | RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; })); |
| 220 | } |
| 221 | |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 222 | TEST_F(FreeCacheTest, FreeCacheOpenFile) { |
| 223 | std::vector<std::string> files = { "file1", "file2" }; |
| 224 | AddFilesToDir(mock_cache.path, files); |
| 225 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 226 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path)); |
| 227 | |
| 228 | std::string file1_path = mock_cache.path + "/file1"s; |
| 229 | android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY)); |
| 230 | |
| 231 | // file1 can't be deleted as it's opened by us. |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 232 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 233 | |
| 234 | ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path)); |
| 235 | } |
| 236 | |
| 237 | TEST_F(FreeCacheTest, FreeCacheLogsSmoke) { |
| 238 | std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5", |
| 239 | "last_log.10" }; |
| 240 | AddFilesToDir(mock_log_dir.path, log_files); |
| 241 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 242 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 243 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 244 | |
| 245 | // Logs with a higher index will be deleted first |
| 246 | std::vector<std::string> expected = { "last_log", "last_log.1" }; |
| 247 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 248 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path)); |
| 249 | } |
| 250 | |
| 251 | TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) { |
| 252 | std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number", |
| 253 | "last_kmsgrandom" }; |
| 254 | AddFilesToDir(mock_log_dir.path, log_files); |
| 255 | ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path)); |
| 256 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 257 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 258 | |
| 259 | // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is |
| 260 | // deleted before last_log. |
| 261 | std::vector<std::string> expected = { "last_log.1" }; |
| 262 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 263 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path)); |
| 264 | } |
| 265 | |
| 266 | TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) { |
| 267 | std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log", |
| 268 | "last_kmsg.1" }; |
| 269 | AddFilesToDir(mock_log_dir.path, log_files); |
| 270 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 271 | |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 272 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker)); |
Tao Bao | b8cb2e6 | 2018-07-06 12:14:36 -0700 | [diff] [blame] | 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 | } |