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