Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [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 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <gtest/gtest.h> |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 20 | #include <libgen.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/statvfs.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <time.h> |
| 27 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 28 | #include <algorithm> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 29 | #include <memory> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 30 | #include <string> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 31 | #include <vector> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 32 | |
| 33 | #include <android-base/file.h> |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 34 | #include <android-base/logging.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
| 36 | #include <android-base/test_utils.h> |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 37 | #include <android-base/unique_fd.h> |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 38 | #include <bsdiff/bsdiff.h> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 39 | #include <openssl/sha.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 40 | |
| 41 | #include "applypatch/applypatch.h" |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 42 | #include "applypatch/applypatch_modes.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 43 | #include "common/test_constants.h" |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 44 | #include "otautil/cache_location.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 45 | #include "otautil/print_sha1.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 46 | |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 47 | using namespace std::string_literals; |
| 48 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 49 | static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) { |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 50 | ASSERT_TRUE(sha1 != nullptr); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 51 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 52 | std::string data; |
| 53 | ASSERT_TRUE(android::base::ReadFileToString(fname, &data)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 54 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 55 | if (fsize != nullptr) { |
| 56 | *fsize = data.size(); |
| 57 | } |
| 58 | |
| 59 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 60 | SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest); |
| 61 | *sha1 = print_sha1(digest); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static void mangle_file(const std::string& fname) { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 65 | std::string content(1024, '\0'); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 66 | for (size_t i = 0; i < 1024; i++) { |
| 67 | content[i] = rand() % 256; |
| 68 | } |
| 69 | ASSERT_TRUE(android::base::WriteStringToFile(content, fname)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 72 | static void test_logger(android::base::LogId /* id */, android::base::LogSeverity severity, |
| 73 | const char* /* tag */, const char* /* file */, unsigned int /* line */, |
| 74 | const char* message) { |
| 75 | if (severity >= android::base::GetMinimumLogSeverity()) { |
| 76 | fprintf(stdout, "%s\n", message); |
| 77 | } |
| 78 | } |
| 79 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 80 | class ApplyPatchTest : public ::testing::Test { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 81 | public: |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 82 | virtual void SetUp() override { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 83 | // set up files |
| 84 | old_file = from_testdata_base("old.file"); |
| 85 | new_file = from_testdata_base("new.file"); |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 86 | nonexistent_file = from_testdata_base("nonexistent.file"); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 87 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 88 | // set up SHA constants |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 89 | sha1sum(old_file, &old_sha1, &old_size); |
| 90 | sha1sum(new_file, &new_sha1, &new_size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 91 | srand(time(nullptr)); |
| 92 | bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 93 | bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 94 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 95 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 96 | std::string old_file; |
| 97 | std::string new_file; |
| 98 | std::string nonexistent_file; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 99 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 100 | std::string old_sha1; |
| 101 | std::string new_sha1; |
| 102 | std::string bad_sha1_a; |
| 103 | std::string bad_sha1_b; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 104 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 105 | size_t old_size; |
| 106 | size_t new_size; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 109 | class ApplyPatchCacheTest : public ApplyPatchTest { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 110 | protected: |
| 111 | void SetUp() override { |
| 112 | ApplyPatchTest::SetUp(); |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 113 | CacheLocation::location().set_cache_temp_source(old_file); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 114 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 117 | class ApplyPatchModesTest : public ::testing::Test { |
| 118 | protected: |
| 119 | void SetUp() override { |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 120 | CacheLocation::location().set_cache_temp_source(cache_source.path); |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 121 | android::base::InitLogging(nullptr, &test_logger); |
| 122 | android::base::SetMinimumLogSeverity(android::base::LogSeverity::DEBUG); |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TemporaryFile cache_source; |
| 126 | }; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 127 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 128 | class FreeCacheTest : public ::testing::Test { |
| 129 | protected: |
| 130 | static constexpr size_t PARTITION_SIZE = 4096 * 10; |
| 131 | |
| 132 | // Returns a sorted list of files in |dirname|. |
| 133 | static std::vector<std::string> FindFilesInDir(const std::string& dirname) { |
| 134 | std::vector<std::string> file_list; |
| 135 | |
| 136 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir); |
| 137 | struct dirent* de; |
| 138 | while ((de = readdir(d.get())) != 0) { |
| 139 | std::string path = dirname + "/" + de->d_name; |
| 140 | |
| 141 | struct stat st; |
| 142 | if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { |
| 143 | file_list.emplace_back(de->d_name); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | std::sort(file_list.begin(), file_list.end()); |
| 148 | return file_list; |
| 149 | } |
| 150 | |
| 151 | static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) { |
| 152 | std::string zeros(4096, 0); |
| 153 | for (const auto& file : files) { |
| 154 | std::string path = dir + "/" + file; |
| 155 | ASSERT_TRUE(android::base::WriteStringToFile(zeros, path)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void SetUp() override { |
| 160 | CacheLocation::location().set_cache_log_directory(mock_log_dir.path); |
| 161 | } |
| 162 | |
| 163 | // A mock method to calculate the free space. It assumes the partition has a total size of 40960 |
| 164 | // bytes and all files are 4096 bytes in size. |
| 165 | size_t MockFreeSpaceChecker(const std::string& dirname) { |
| 166 | std::vector<std::string> files = FindFilesInDir(dirname); |
| 167 | return PARTITION_SIZE - 4096 * files.size(); |
| 168 | } |
| 169 | |
| 170 | TemporaryDir mock_cache; |
| 171 | TemporaryDir mock_log_dir; |
| 172 | }; |
| 173 | |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 174 | TEST_F(ApplyPatchTest, CheckModeSkip) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 175 | std::vector<std::string> sha1s; |
| 176 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 179 | TEST_F(ApplyPatchTest, CheckModeSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 180 | std::vector<std::string> sha1s = { old_sha1 }; |
| 181 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_F(ApplyPatchTest, CheckModeMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 185 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 186 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TEST_F(ApplyPatchTest, CheckModeFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 190 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 191 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 194 | TEST_F(ApplyPatchTest, CheckModeEmmcTarget) { |
| 195 | // EMMC:old_file:size:sha1 should pass the check. |
| 196 | std::string src_file = |
| 197 | "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1; |
| 198 | std::vector<std::string> sha1s; |
| 199 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 200 | |
| 201 | // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 202 | src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 203 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 204 | ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s)); |
| 205 | |
| 206 | // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 207 | src_file = "EMMC:" + old_file + ":" + |
| 208 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 209 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 210 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 211 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 212 | |
| 213 | // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check. |
| 214 | src_file = "EMMC:" + old_file + ":" + |
| 215 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 216 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 217 | std::to_string(old_size) + ":" + old_sha1; |
| 218 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 219 | |
| 220 | // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1 |
| 221 | // should pass the check. |
| 222 | src_file = "EMMC:" + new_file + ":" + |
| 223 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 224 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 225 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 226 | std::to_string(new_size) + ":" + new_sha1; |
| 227 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 228 | } |
| 229 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 230 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) { |
| 231 | TemporaryFile temp_file; |
| 232 | mangle_file(temp_file.path); |
| 233 | std::vector<std::string> sha1s_single = { old_sha1 }; |
| 234 | ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single)); |
| 235 | ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 238 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) { |
| 239 | TemporaryFile temp_file; |
| 240 | mangle_file(temp_file.path); |
| 241 | std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 242 | ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple)); |
| 243 | ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 246 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) { |
| 247 | TemporaryFile temp_file; |
| 248 | mangle_file(temp_file.path); |
| 249 | std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b }; |
| 250 | ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure)); |
| 251 | ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 254 | TEST_F(ApplyPatchModesTest, InvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 255 | // At least two args (including the filename). |
| 256 | ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" })); |
| 257 | |
| 258 | // Unrecognized args. |
| 259 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" })); |
| 260 | } |
| 261 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 262 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) { |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 263 | std::string boot_img = from_testdata_base("boot.img"); |
| 264 | size_t boot_img_size; |
| 265 | std::string boot_img_sha1; |
| 266 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 267 | |
| 268 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 269 | size_t recovery_img_size; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 270 | std::string recovery_img_sha1; |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 271 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 272 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 273 | |
| 274 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 275 | |
| 276 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 277 | std::string src_file_arg = |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 278 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 279 | TemporaryFile tgt_file; |
| 280 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 281 | std::string patch_arg = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 282 | std::vector<const char*> args = { "applypatch", |
| 283 | "-b", |
| 284 | bonus_file.c_str(), |
| 285 | src_file_arg.c_str(), |
| 286 | tgt_file_arg.c_str(), |
| 287 | recovery_img_sha1.c_str(), |
| 288 | recovery_img_size_arg.c_str(), |
| 289 | patch_arg.c_str() }; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 290 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Tests patching the EMMC target without a separate bonus file (i.e. recovery-from-boot patch has |
| 294 | // everything). |
| 295 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithoutBonusFile) { |
| 296 | std::string boot_img = from_testdata_base("boot.img"); |
| 297 | size_t boot_img_size; |
| 298 | std::string boot_img_sha1; |
| 299 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 300 | |
| 301 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 302 | size_t recovery_img_size; |
| 303 | std::string recovery_img_sha1; |
| 304 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 305 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 306 | |
| 307 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 308 | std::string src_file_arg = |
| 309 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 310 | TemporaryFile tgt_file; |
| 311 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 312 | std::string patch_arg = |
| 313 | boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
| 314 | std::vector<const char*> args = { "applypatch", |
| 315 | src_file_arg.c_str(), |
| 316 | tgt_file_arg.c_str(), |
| 317 | recovery_img_sha1.c_str(), |
| 318 | recovery_img_size_arg.c_str(), |
| 319 | patch_arg.c_str() }; |
| 320 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 321 | } |
| 322 | |
| 323 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithMultiplePatches) { |
| 324 | std::string boot_img = from_testdata_base("boot.img"); |
| 325 | size_t boot_img_size; |
| 326 | std::string boot_img_sha1; |
| 327 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 328 | |
| 329 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 330 | size_t recovery_img_size; |
| 331 | std::string recovery_img_sha1; |
| 332 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 333 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
| 334 | |
| 335 | std::string bonus_file = from_testdata_base("bonus.file"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 336 | |
| 337 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \ |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 338 | // <src-sha1-fake1>:<patch1> <src-sha1>:<patch2> <src-sha1-fake2>:<patch3> |
| 339 | std::string src_file_arg = |
| 340 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 341 | TemporaryFile tgt_file; |
| 342 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 343 | std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 344 | std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
| 345 | std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); |
| 346 | std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 347 | std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p"); |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 348 | std::vector<const char*> args = { "applypatch", |
| 349 | "-b", |
| 350 | bonus_file.c_str(), |
| 351 | src_file_arg.c_str(), |
| 352 | tgt_file_arg.c_str(), |
| 353 | recovery_img_sha1.c_str(), |
| 354 | recovery_img_size_arg.c_str(), |
| 355 | patch1.c_str(), |
| 356 | patch2.c_str(), |
| 357 | patch3.c_str() }; |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 358 | // TODO(b/67849209): Remove after addressing the flakiness. |
| 359 | printf("Calling applypatch_modes with the following args:\n"); |
| 360 | for (const auto& arg : args) { |
| 361 | printf(" %s\n", arg); |
| 362 | } |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 363 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 366 | // Ensures that applypatch works with a bsdiff based recovery-from-boot.p. |
| 367 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) { |
| 368 | std::string boot_img_file = from_testdata_base("boot.img"); |
| 369 | std::string boot_img_sha1; |
| 370 | size_t boot_img_size; |
| 371 | sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size); |
| 372 | |
| 373 | std::string recovery_img_file = from_testdata_base("recovery.img"); |
| 374 | std::string recovery_img_sha1; |
| 375 | size_t recovery_img_size; |
| 376 | sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size); |
| 377 | |
| 378 | // Generate the bsdiff patch of recovery-from-boot.p. |
| 379 | std::string src_content; |
| 380 | ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content)); |
| 381 | |
| 382 | std::string tgt_content; |
| 383 | ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content)); |
| 384 | |
| 385 | TemporaryFile patch_file; |
| 386 | ASSERT_EQ(0, |
| 387 | bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(), |
| 388 | reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(), |
| 389 | patch_file.path, nullptr)); |
| 390 | |
| 391 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 392 | std::string src_file_arg = |
| 393 | "EMMC:" + boot_img_file + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 394 | TemporaryFile tgt_file; |
| 395 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 396 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
| 397 | std::string patch_arg = boot_img_sha1 + ":" + patch_file.path; |
| 398 | std::vector<const char*> args = { "applypatch", |
| 399 | src_file_arg.c_str(), |
| 400 | tgt_file_arg.c_str(), |
| 401 | recovery_img_sha1.c_str(), |
| 402 | recovery_img_size_arg.c_str(), |
| 403 | patch_arg.c_str() }; |
| 404 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 405 | |
| 406 | // Double check the patched recovery image. |
| 407 | std::string tgt_file_sha1; |
| 408 | size_t tgt_file_size; |
| 409 | sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size); |
| 410 | ASSERT_EQ(recovery_img_size, tgt_file_size); |
| 411 | ASSERT_EQ(recovery_img_sha1, tgt_file_sha1); |
| 412 | } |
| 413 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 414 | TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 415 | // Invalid bonus file. |
| 416 | ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); |
| 417 | |
| 418 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 419 | // With bonus file, but missing args. |
| 420 | ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() })); |
| 421 | |
| 422 | std::string boot_img = from_testdata_base("boot.img"); |
| 423 | size_t boot_img_size; |
| 424 | std::string boot_img_sha1; |
| 425 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 426 | |
| 427 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 428 | size_t size; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 429 | std::string recovery_img_sha1; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 430 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 431 | std::string recovery_img_size = std::to_string(size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 432 | |
| 433 | // Bonus file is not supported in flash mode. |
| 434 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> |
| 435 | TemporaryFile tmp4; |
| 436 | std::vector<const char*> args4 = { |
| 437 | "applypatch", |
| 438 | "-b", |
| 439 | bonus_file.c_str(), |
| 440 | boot_img.c_str(), |
| 441 | tmp4.path, |
| 442 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 443 | recovery_img_size.c_str() |
| 444 | }; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 445 | ASSERT_NE(0, applypatch_modes(args4.size(), args4.data())); |
| 446 | |
| 447 | // Failed to parse patch args. |
| 448 | TemporaryFile tmp5; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 449 | std::string bad_arg1 = |
| 450 | "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 451 | std::vector<const char*> args5 = { |
| 452 | "applypatch", |
| 453 | boot_img.c_str(), |
| 454 | tmp5.path, |
| 455 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 456 | recovery_img_size.c_str(), |
| 457 | bad_arg1.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 458 | }; |
| 459 | ASSERT_NE(0, applypatch_modes(args5.size(), args5.data())); |
| 460 | |
| 461 | // Target size cannot be zero. |
| 462 | TemporaryFile tmp6; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 463 | std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 464 | std::vector<const char*> args6 = { |
| 465 | "applypatch", |
| 466 | boot_img.c_str(), |
| 467 | tmp6.path, |
| 468 | recovery_img_sha1.c_str(), |
| 469 | "0", // target size |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 470 | patch.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 471 | }; |
| 472 | ASSERT_NE(0, applypatch_modes(args6.size(), args6.data())); |
| 473 | } |
| 474 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 475 | TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 476 | // Insufficient args. |
| 477 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" })); |
| 478 | } |
| 479 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 480 | TEST_F(ApplyPatchModesTest, ShowLicenses) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 481 | ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" })); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 482 | } |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 483 | |
| 484 | TEST_F(FreeCacheTest, FreeCacheSmoke) { |
| 485 | std::vector<std::string> files = { "file1", "file2", "file3" }; |
| 486 | AddFilesToDir(mock_cache.path, files); |
| 487 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 488 | ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); |
| 489 | |
| 490 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) { |
| 491 | return this->MockFreeSpaceChecker(dir); |
| 492 | })); |
| 493 | |
| 494 | ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path)); |
| 495 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path)); |
| 496 | } |
| 497 | |
| 498 | TEST_F(FreeCacheTest, FreeCacheOpenFile) { |
| 499 | std::vector<std::string> files = { "file1", "file2" }; |
| 500 | AddFilesToDir(mock_cache.path, files); |
| 501 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 502 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path)); |
| 503 | |
| 504 | std::string file1_path = mock_cache.path + "/file1"s; |
| 505 | android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY)); |
| 506 | |
| 507 | // file1 can't be deleted as it's opened by us. |
| 508 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) { |
| 509 | return this->MockFreeSpaceChecker(dir); |
| 510 | })); |
| 511 | |
| 512 | ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path)); |
| 513 | } |
| 514 | |
| 515 | TEST_F(FreeCacheTest, FreeCacheLogsSmoke) { |
| 516 | std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5", |
| 517 | "last_log.10" }; |
| 518 | AddFilesToDir(mock_log_dir.path, log_files); |
| 519 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 520 | |
| 521 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) { |
| 522 | return this->MockFreeSpaceChecker(dir); |
| 523 | })); |
| 524 | |
| 525 | // Logs with a higher index will be deleted first |
| 526 | std::vector<std::string> expected = { "last_log", "last_log.1" }; |
| 527 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 528 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path)); |
| 529 | } |
| 530 | |
| 531 | TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) { |
| 532 | std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number", |
| 533 | "last_kmsgrandom" }; |
| 534 | AddFilesToDir(mock_log_dir.path, log_files); |
| 535 | ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path)); |
| 536 | |
| 537 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) { |
| 538 | return this->MockFreeSpaceChecker(dir); |
| 539 | })); |
| 540 | |
| 541 | // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is |
| 542 | // deleted before last_log. |
| 543 | std::vector<std::string> expected = { "last_log.1" }; |
| 544 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 545 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path)); |
| 546 | } |
| 547 | |
| 548 | TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) { |
| 549 | std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log", |
| 550 | "last_kmsg.1" }; |
| 551 | AddFilesToDir(mock_log_dir.path, log_files); |
| 552 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 553 | |
| 554 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) { |
| 555 | return this->MockFreeSpaceChecker(dir); |
| 556 | })); |
| 557 | |
| 558 | // Non log files in /cache/recovery won't be deleted. |
| 559 | std::vector<std::string> expected = { "block.map", "command", "last_install" }; |
| 560 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 561 | } |