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 | |
| 17 | #include <fcntl.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/statvfs.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <time.h> |
| 25 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 26 | #include <memory> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 27 | #include <string> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 28 | #include <vector> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 29 | |
| 30 | #include <android-base/file.h> |
| 31 | #include <android-base/stringprintf.h> |
| 32 | #include <android-base/test_utils.h> |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 33 | #include <bsdiff/bsdiff.h> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 34 | #include <openssl/sha.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 35 | |
| 36 | #include "applypatch/applypatch.h" |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 37 | #include "applypatch/applypatch_modes.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 38 | #include "common/test_constants.h" |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 39 | #include "otautil/cache_location.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 40 | #include "otautil/print_sha1.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 41 | |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 42 | using namespace std::string_literals; |
| 43 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 44 | static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) { |
| 45 | ASSERT_NE(nullptr, sha1); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 46 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 47 | std::string data; |
| 48 | ASSERT_TRUE(android::base::ReadFileToString(fname, &data)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 49 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 50 | if (fsize != nullptr) { |
| 51 | *fsize = data.size(); |
| 52 | } |
| 53 | |
| 54 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 55 | SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest); |
| 56 | *sha1 = print_sha1(digest); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void mangle_file(const std::string& fname) { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 60 | std::string content(1024, '\0'); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 61 | for (size_t i = 0; i < 1024; i++) { |
| 62 | content[i] = rand() % 256; |
| 63 | } |
| 64 | ASSERT_TRUE(android::base::WriteStringToFile(content, fname)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 67 | class ApplyPatchTest : public ::testing::Test { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 68 | public: |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 69 | virtual void SetUp() override { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 70 | // set up files |
| 71 | old_file = from_testdata_base("old.file"); |
| 72 | new_file = from_testdata_base("new.file"); |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 73 | nonexistent_file = from_testdata_base("nonexistent.file"); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 74 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 75 | // set up SHA constants |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 76 | sha1sum(old_file, &old_sha1, &old_size); |
| 77 | sha1sum(new_file, &new_sha1, &new_size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 78 | srand(time(nullptr)); |
| 79 | bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 80 | bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 81 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 82 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 83 | std::string old_file; |
| 84 | std::string new_file; |
| 85 | std::string nonexistent_file; |
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_sha1; |
| 88 | std::string new_sha1; |
| 89 | std::string bad_sha1_a; |
| 90 | std::string bad_sha1_b; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 91 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 92 | size_t old_size; |
| 93 | size_t new_size; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 96 | class ApplyPatchCacheTest : public ApplyPatchTest { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 97 | protected: |
| 98 | void SetUp() override { |
| 99 | ApplyPatchTest::SetUp(); |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 100 | CacheLocation::location().set_cache_temp_source(old_file); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 101 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 104 | class ApplyPatchModesTest : public ::testing::Test { |
| 105 | protected: |
| 106 | void SetUp() override { |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 107 | CacheLocation::location().set_cache_temp_source(cache_source.path); |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TemporaryFile cache_source; |
| 111 | }; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 112 | |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 113 | TEST_F(ApplyPatchTest, CheckModeSkip) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 114 | std::vector<std::string> sha1s; |
| 115 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 118 | TEST_F(ApplyPatchTest, CheckModeSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 119 | std::vector<std::string> sha1s = { old_sha1 }; |
| 120 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(ApplyPatchTest, CheckModeMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 124 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 125 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(ApplyPatchTest, CheckModeFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 129 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 130 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 133 | TEST_F(ApplyPatchTest, CheckModeEmmcTarget) { |
| 134 | // EMMC:old_file:size:sha1 should pass the check. |
| 135 | std::string src_file = |
| 136 | "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1; |
| 137 | std::vector<std::string> sha1s; |
| 138 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 139 | |
| 140 | // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 141 | src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 142 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 143 | ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s)); |
| 144 | |
| 145 | // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 146 | src_file = "EMMC:" + old_file + ":" + |
| 147 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 148 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 149 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 150 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 151 | |
| 152 | // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check. |
| 153 | src_file = "EMMC:" + old_file + ":" + |
| 154 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 155 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 156 | std::to_string(old_size) + ":" + old_sha1; |
| 157 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 158 | |
| 159 | // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1 |
| 160 | // should pass the check. |
| 161 | src_file = "EMMC:" + new_file + ":" + |
| 162 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 163 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 164 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 165 | std::to_string(new_size) + ":" + new_sha1; |
| 166 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 167 | } |
| 168 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 169 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) { |
| 170 | TemporaryFile temp_file; |
| 171 | mangle_file(temp_file.path); |
| 172 | std::vector<std::string> sha1s_single = { old_sha1 }; |
| 173 | ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single)); |
| 174 | ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 177 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) { |
| 178 | TemporaryFile temp_file; |
| 179 | mangle_file(temp_file.path); |
| 180 | std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 181 | ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple)); |
| 182 | ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 185 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) { |
| 186 | TemporaryFile temp_file; |
| 187 | mangle_file(temp_file.path); |
| 188 | std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b }; |
| 189 | ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure)); |
| 190 | ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 193 | TEST_F(ApplyPatchModesTest, InvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 194 | // At least two args (including the filename). |
| 195 | ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" })); |
| 196 | |
| 197 | // Unrecognized args. |
| 198 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" })); |
| 199 | } |
| 200 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 201 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) { |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 202 | std::string boot_img = from_testdata_base("boot.img"); |
| 203 | size_t boot_img_size; |
| 204 | std::string boot_img_sha1; |
| 205 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 206 | |
| 207 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 208 | size_t recovery_img_size; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 209 | std::string recovery_img_sha1; |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 210 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 211 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 212 | |
| 213 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 214 | |
| 215 | // 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] | 216 | std::string src_file_arg = |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 217 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 218 | TemporaryFile tgt_file; |
| 219 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 220 | std::string patch_arg = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 221 | std::vector<const char*> args = { "applypatch", |
| 222 | "-b", |
| 223 | bonus_file.c_str(), |
| 224 | src_file_arg.c_str(), |
| 225 | tgt_file_arg.c_str(), |
| 226 | recovery_img_sha1.c_str(), |
| 227 | recovery_img_size_arg.c_str(), |
| 228 | patch_arg.c_str() }; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 229 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Tests patching the EMMC target without a separate bonus file (i.e. recovery-from-boot patch has |
| 233 | // everything). |
| 234 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithoutBonusFile) { |
| 235 | std::string boot_img = from_testdata_base("boot.img"); |
| 236 | size_t boot_img_size; |
| 237 | std::string boot_img_sha1; |
| 238 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 239 | |
| 240 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 241 | size_t recovery_img_size; |
| 242 | std::string recovery_img_sha1; |
| 243 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 244 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 245 | |
| 246 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 247 | std::string src_file_arg = |
| 248 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 249 | TemporaryFile tgt_file; |
| 250 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 251 | std::string patch_arg = |
| 252 | boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
| 253 | std::vector<const char*> args = { "applypatch", |
| 254 | src_file_arg.c_str(), |
| 255 | tgt_file_arg.c_str(), |
| 256 | recovery_img_sha1.c_str(), |
| 257 | recovery_img_size_arg.c_str(), |
| 258 | patch_arg.c_str() }; |
| 259 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 260 | } |
| 261 | |
| 262 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithMultiplePatches) { |
| 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"); |
| 269 | size_t recovery_img_size; |
| 270 | std::string recovery_img_sha1; |
| 271 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 272 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
| 273 | |
| 274 | std::string bonus_file = from_testdata_base("bonus.file"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 275 | |
| 276 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \ |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 277 | // <src-sha1-fake1>:<patch1> <src-sha1>:<patch2> <src-sha1-fake2>:<patch3> |
| 278 | std::string src_file_arg = |
| 279 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 280 | TemporaryFile tgt_file; |
| 281 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 282 | std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 283 | std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
| 284 | std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); |
| 285 | std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 286 | 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] | 287 | std::vector<const char*> args = { "applypatch", |
| 288 | "-b", |
| 289 | bonus_file.c_str(), |
| 290 | src_file_arg.c_str(), |
| 291 | tgt_file_arg.c_str(), |
| 292 | recovery_img_sha1.c_str(), |
| 293 | recovery_img_size_arg.c_str(), |
| 294 | patch1.c_str(), |
| 295 | patch2.c_str(), |
| 296 | patch3.c_str() }; |
| 297 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 300 | // Ensures that applypatch works with a bsdiff based recovery-from-boot.p. |
| 301 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) { |
| 302 | std::string boot_img_file = from_testdata_base("boot.img"); |
| 303 | std::string boot_img_sha1; |
| 304 | size_t boot_img_size; |
| 305 | sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size); |
| 306 | |
| 307 | std::string recovery_img_file = from_testdata_base("recovery.img"); |
| 308 | std::string recovery_img_sha1; |
| 309 | size_t recovery_img_size; |
| 310 | sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size); |
| 311 | |
| 312 | // Generate the bsdiff patch of recovery-from-boot.p. |
| 313 | std::string src_content; |
| 314 | ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content)); |
| 315 | |
| 316 | std::string tgt_content; |
| 317 | ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content)); |
| 318 | |
| 319 | TemporaryFile patch_file; |
| 320 | ASSERT_EQ(0, |
| 321 | bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(), |
| 322 | reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(), |
| 323 | patch_file.path, nullptr)); |
| 324 | |
| 325 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 326 | std::string src_file_arg = |
| 327 | "EMMC:" + boot_img_file + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 328 | TemporaryFile tgt_file; |
| 329 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 330 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
| 331 | std::string patch_arg = boot_img_sha1 + ":" + patch_file.path; |
| 332 | std::vector<const char*> args = { "applypatch", |
| 333 | src_file_arg.c_str(), |
| 334 | tgt_file_arg.c_str(), |
| 335 | recovery_img_sha1.c_str(), |
| 336 | recovery_img_size_arg.c_str(), |
| 337 | patch_arg.c_str() }; |
| 338 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 339 | |
| 340 | // Double check the patched recovery image. |
| 341 | std::string tgt_file_sha1; |
| 342 | size_t tgt_file_size; |
| 343 | sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size); |
| 344 | ASSERT_EQ(recovery_img_size, tgt_file_size); |
| 345 | ASSERT_EQ(recovery_img_sha1, tgt_file_sha1); |
| 346 | } |
| 347 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 348 | TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 349 | // Invalid bonus file. |
| 350 | ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); |
| 351 | |
| 352 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 353 | // With bonus file, but missing args. |
| 354 | ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() })); |
| 355 | |
| 356 | std::string boot_img = from_testdata_base("boot.img"); |
| 357 | size_t boot_img_size; |
| 358 | std::string boot_img_sha1; |
| 359 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 360 | |
| 361 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 362 | size_t size; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 363 | std::string recovery_img_sha1; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 364 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 365 | std::string recovery_img_size = std::to_string(size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 366 | |
| 367 | // Bonus file is not supported in flash mode. |
| 368 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> |
| 369 | TemporaryFile tmp4; |
| 370 | std::vector<const char*> args4 = { |
| 371 | "applypatch", |
| 372 | "-b", |
| 373 | bonus_file.c_str(), |
| 374 | boot_img.c_str(), |
| 375 | tmp4.path, |
| 376 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 377 | recovery_img_size.c_str() |
| 378 | }; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 379 | ASSERT_NE(0, applypatch_modes(args4.size(), args4.data())); |
| 380 | |
| 381 | // Failed to parse patch args. |
| 382 | TemporaryFile tmp5; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 383 | std::string bad_arg1 = |
| 384 | "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 385 | std::vector<const char*> args5 = { |
| 386 | "applypatch", |
| 387 | boot_img.c_str(), |
| 388 | tmp5.path, |
| 389 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 390 | recovery_img_size.c_str(), |
| 391 | bad_arg1.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 392 | }; |
| 393 | ASSERT_NE(0, applypatch_modes(args5.size(), args5.data())); |
| 394 | |
| 395 | // Target size cannot be zero. |
| 396 | TemporaryFile tmp6; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 397 | 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] | 398 | std::vector<const char*> args6 = { |
| 399 | "applypatch", |
| 400 | boot_img.c_str(), |
| 401 | tmp6.path, |
| 402 | recovery_img_sha1.c_str(), |
| 403 | "0", // target size |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 404 | patch.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 405 | }; |
| 406 | ASSERT_NE(0, applypatch_modes(args6.size(), args6.data())); |
| 407 | } |
| 408 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 409 | TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 410 | // Insufficient args. |
| 411 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" })); |
| 412 | } |
| 413 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 414 | TEST_F(ApplyPatchModesTest, ShowLicenses) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 415 | ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" })); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 416 | } |