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 | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 33 | #include <openssl/sha.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 34 | |
| 35 | #include "applypatch/applypatch.h" |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 36 | #include "applypatch/applypatch_modes.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 37 | #include "common/test_constants.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 38 | #include "otautil/print_sha1.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 39 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 40 | static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) { |
| 41 | ASSERT_NE(nullptr, sha1); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 42 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 43 | std::string data; |
| 44 | ASSERT_TRUE(android::base::ReadFileToString(fname, &data)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 45 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 46 | if (fsize != nullptr) { |
| 47 | *fsize = data.size(); |
| 48 | } |
| 49 | |
| 50 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 51 | SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest); |
| 52 | *sha1 = print_sha1(digest); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void mangle_file(const std::string& fname) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 56 | std::string content; |
| 57 | content.reserve(1024); |
| 58 | for (size_t i = 0; i < 1024; i++) { |
| 59 | content[i] = rand() % 256; |
| 60 | } |
| 61 | ASSERT_TRUE(android::base::WriteStringToFile(content, fname)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 64 | class ApplyPatchTest : public ::testing::Test { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 65 | public: |
| 66 | static void SetUpTestCase() { |
| 67 | // set up files |
| 68 | old_file = from_testdata_base("old.file"); |
| 69 | new_file = from_testdata_base("new.file"); |
| 70 | patch_file = from_testdata_base("patch.bsdiff"); |
| 71 | rand_file = "/cache/applypatch_test_rand.file"; |
| 72 | cache_file = "/cache/saved.file"; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 73 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 74 | // write stuff to rand_file |
| 75 | ASSERT_TRUE(android::base::WriteStringToFile("hello", rand_file)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 76 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 77 | // set up SHA constants |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 78 | sha1sum(old_file, &old_sha1, &old_size); |
| 79 | sha1sum(new_file, &new_sha1, &new_size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 80 | srand(time(nullptr)); |
| 81 | bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 82 | bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 83 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 84 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 85 | static std::string old_file; |
| 86 | static std::string new_file; |
| 87 | static std::string rand_file; |
| 88 | static std::string cache_file; |
| 89 | static std::string patch_file; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 90 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 91 | static std::string old_sha1; |
| 92 | static std::string new_sha1; |
| 93 | static std::string bad_sha1_a; |
| 94 | static std::string bad_sha1_b; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 95 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 96 | static size_t old_size; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 97 | static size_t new_size; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 100 | static void cp(const std::string& src, const std::string& tgt) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 101 | std::string cmd = "cp " + src + " " + tgt; |
| 102 | system(cmd.c_str()); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void backup_old() { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 106 | cp(ApplyPatchTest::old_file, ApplyPatchTest::cache_file); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void restore_old() { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 110 | cp(ApplyPatchTest::cache_file, ApplyPatchTest::old_file); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | class ApplyPatchCacheTest : public ApplyPatchTest { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 114 | public: |
| 115 | virtual void SetUp() { |
| 116 | backup_old(); |
| 117 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 118 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 119 | virtual void TearDown() { |
| 120 | restore_old(); |
| 121 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
Tao Bao | 0a3e4dc | 2017-04-21 09:26:48 -0700 | [diff] [blame] | 124 | std::string ApplyPatchTest::old_file; |
| 125 | std::string ApplyPatchTest::new_file; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 126 | std::string ApplyPatchTest::rand_file; |
| 127 | std::string ApplyPatchTest::patch_file; |
| 128 | std::string ApplyPatchTest::cache_file; |
| 129 | std::string ApplyPatchTest::old_sha1; |
| 130 | std::string ApplyPatchTest::new_sha1; |
| 131 | std::string ApplyPatchTest::bad_sha1_a; |
| 132 | std::string ApplyPatchTest::bad_sha1_b; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 133 | size_t ApplyPatchTest::old_size; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 134 | size_t ApplyPatchTest::new_size; |
| 135 | |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 136 | TEST_F(ApplyPatchTest, CheckModeSkip) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 137 | std::vector<std::string> sha1s; |
| 138 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 141 | TEST_F(ApplyPatchTest, CheckModeSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 142 | std::vector<std::string> sha1s = { old_sha1 }; |
| 143 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | TEST_F(ApplyPatchTest, CheckModeMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 147 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 148 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST_F(ApplyPatchTest, CheckModeFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 152 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 153 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 156 | TEST_F(ApplyPatchTest, CheckModeEmmcTarget) { |
| 157 | // EMMC:old_file:size:sha1 should pass the check. |
| 158 | std::string src_file = |
| 159 | "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1; |
| 160 | std::vector<std::string> sha1s; |
| 161 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 162 | |
| 163 | // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 164 | src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 165 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 166 | ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s)); |
| 167 | |
| 168 | // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 169 | src_file = "EMMC:" + old_file + ":" + |
| 170 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 171 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 172 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 173 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 174 | |
| 175 | // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check. |
| 176 | src_file = "EMMC:" + old_file + ":" + |
| 177 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 178 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 179 | std::to_string(old_size) + ":" + old_sha1; |
| 180 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 181 | |
| 182 | // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1 |
| 183 | // should pass the check. |
| 184 | src_file = "EMMC:" + new_file + ":" + |
| 185 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 186 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 187 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 188 | std::to_string(new_size) + ":" + new_sha1; |
| 189 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 190 | } |
| 191 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 192 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 193 | mangle_file(old_file); |
| 194 | std::vector<std::string> sha1s = { old_sha1 }; |
| 195 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 199 | mangle_file(old_file); |
| 200 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 201 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 205 | mangle_file(old_file); |
| 206 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 207 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 211 | unlink(&old_file[0]); |
| 212 | std::vector<std::string> sha1s = { old_sha1 }; |
| 213 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 217 | unlink(&old_file[0]); |
| 218 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 219 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 223 | unlink(&old_file[0]); |
| 224 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 225 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 228 | TEST(ApplyPatchModesTest, InvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 229 | // At least two args (including the filename). |
| 230 | ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" })); |
| 231 | |
| 232 | // Unrecognized args. |
| 233 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" })); |
| 234 | } |
| 235 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 236 | TEST(ApplyPatchModesTest, PatchModeEmmcTarget) { |
| 237 | std::string boot_img = from_testdata_base("boot.img"); |
| 238 | size_t boot_img_size; |
| 239 | std::string boot_img_sha1; |
| 240 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 241 | |
| 242 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 243 | size_t size; |
| 244 | std::string recovery_img_sha1; |
| 245 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 246 | std::string recovery_img_size = std::to_string(size); |
| 247 | |
| 248 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 249 | |
| 250 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 251 | TemporaryFile tmp1; |
| 252 | std::string src_file = |
| 253 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 254 | std::string tgt_file = "EMMC:" + std::string(tmp1.path); |
| 255 | std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 256 | std::vector<const char*> args = { |
| 257 | "applypatch", |
| 258 | "-b", |
| 259 | bonus_file.c_str(), |
| 260 | src_file.c_str(), |
| 261 | tgt_file.c_str(), |
| 262 | recovery_img_sha1.c_str(), |
| 263 | recovery_img_size.c_str(), |
| 264 | patch.c_str() |
| 265 | }; |
| 266 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 267 | |
| 268 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 269 | TemporaryFile tmp2; |
| 270 | patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
| 271 | tgt_file = "EMMC:" + std::string(tmp2.path); |
| 272 | std::vector<const char*> args2 = { |
| 273 | "applypatch", |
| 274 | src_file.c_str(), |
| 275 | tgt_file.c_str(), |
| 276 | recovery_img_sha1.c_str(), |
| 277 | recovery_img_size.c_str(), |
| 278 | patch.c_str() |
| 279 | }; |
| 280 | ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data())); |
| 281 | |
| 282 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \ |
| 283 | // <src-sha1-fake>:<patch1> <src-sha1>:<patch2> |
| 284 | TemporaryFile tmp3; |
| 285 | tgt_file = "EMMC:" + std::string(tmp3.path); |
| 286 | std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 287 | std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
| 288 | std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); |
| 289 | std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 290 | std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p"); |
| 291 | std::vector<const char*> args3 = { |
| 292 | "applypatch", |
| 293 | "-b", |
| 294 | bonus_file.c_str(), |
| 295 | src_file.c_str(), |
| 296 | tgt_file.c_str(), |
| 297 | recovery_img_sha1.c_str(), |
| 298 | recovery_img_size.c_str(), |
| 299 | patch1.c_str(), |
| 300 | patch2.c_str(), |
| 301 | patch3.c_str() |
| 302 | }; |
| 303 | ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); |
| 304 | } |
| 305 | |
| 306 | TEST(ApplyPatchModesTest, PatchModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 307 | // Invalid bonus file. |
| 308 | ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); |
| 309 | |
| 310 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 311 | // With bonus file, but missing args. |
| 312 | ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() })); |
| 313 | |
| 314 | std::string boot_img = from_testdata_base("boot.img"); |
| 315 | size_t boot_img_size; |
| 316 | std::string boot_img_sha1; |
| 317 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 318 | |
| 319 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 320 | size_t size; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 321 | std::string recovery_img_sha1; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 322 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 323 | std::string recovery_img_size = std::to_string(size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 324 | |
| 325 | // Bonus file is not supported in flash mode. |
| 326 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> |
| 327 | TemporaryFile tmp4; |
| 328 | std::vector<const char*> args4 = { |
| 329 | "applypatch", |
| 330 | "-b", |
| 331 | bonus_file.c_str(), |
| 332 | boot_img.c_str(), |
| 333 | tmp4.path, |
| 334 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 335 | recovery_img_size.c_str() |
| 336 | }; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 337 | ASSERT_NE(0, applypatch_modes(args4.size(), args4.data())); |
| 338 | |
| 339 | // Failed to parse patch args. |
| 340 | TemporaryFile tmp5; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 341 | std::string bad_arg1 = |
| 342 | "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 343 | std::vector<const char*> args5 = { |
| 344 | "applypatch", |
| 345 | boot_img.c_str(), |
| 346 | tmp5.path, |
| 347 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 348 | recovery_img_size.c_str(), |
| 349 | bad_arg1.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 350 | }; |
| 351 | ASSERT_NE(0, applypatch_modes(args5.size(), args5.data())); |
| 352 | |
| 353 | // Target size cannot be zero. |
| 354 | TemporaryFile tmp6; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 355 | 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] | 356 | std::vector<const char*> args6 = { |
| 357 | "applypatch", |
| 358 | boot_img.c_str(), |
| 359 | tmp6.path, |
| 360 | recovery_img_sha1.c_str(), |
| 361 | "0", // target size |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 362 | patch.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 363 | }; |
| 364 | ASSERT_NE(0, applypatch_modes(args6.size(), args6.data())); |
| 365 | } |
| 366 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 367 | TEST(ApplyPatchModesTest, CheckModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 368 | // Insufficient args. |
| 369 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" })); |
| 370 | } |
| 371 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 372 | TEST(ApplyPatchModesTest, ShowLicenses) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 373 | ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" })); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 374 | } |