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