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"); |
| 208 | size_t size; |
| 209 | std::string recovery_img_sha1; |
| 210 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 211 | std::string recovery_img_size = std::to_string(size); |
| 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> |
| 216 | TemporaryFile tmp1; |
| 217 | std::string src_file = |
| 218 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 219 | std::string tgt_file = "EMMC:" + std::string(tmp1.path); |
| 220 | std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 221 | std::vector<const char*> args = { |
| 222 | "applypatch", |
| 223 | "-b", |
| 224 | bonus_file.c_str(), |
| 225 | src_file.c_str(), |
| 226 | tgt_file.c_str(), |
| 227 | recovery_img_sha1.c_str(), |
| 228 | recovery_img_size.c_str(), |
| 229 | patch.c_str() |
| 230 | }; |
| 231 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 232 | |
| 233 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 234 | TemporaryFile tmp2; |
| 235 | patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
| 236 | tgt_file = "EMMC:" + std::string(tmp2.path); |
| 237 | std::vector<const char*> args2 = { |
| 238 | "applypatch", |
| 239 | src_file.c_str(), |
| 240 | tgt_file.c_str(), |
| 241 | recovery_img_sha1.c_str(), |
| 242 | recovery_img_size.c_str(), |
| 243 | patch.c_str() |
| 244 | }; |
| 245 | ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data())); |
| 246 | |
| 247 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \ |
| 248 | // <src-sha1-fake>:<patch1> <src-sha1>:<patch2> |
| 249 | TemporaryFile tmp3; |
| 250 | tgt_file = "EMMC:" + std::string(tmp3.path); |
| 251 | std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 252 | std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
| 253 | std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); |
| 254 | std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 255 | std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p"); |
| 256 | std::vector<const char*> args3 = { |
| 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 | patch1.c_str(), |
| 265 | patch2.c_str(), |
| 266 | patch3.c_str() |
| 267 | }; |
| 268 | ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); |
| 269 | } |
| 270 | |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 271 | // Ensures that applypatch works with a bsdiff based recovery-from-boot.p. |
| 272 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) { |
| 273 | std::string boot_img_file = from_testdata_base("boot.img"); |
| 274 | std::string boot_img_sha1; |
| 275 | size_t boot_img_size; |
| 276 | sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size); |
| 277 | |
| 278 | std::string recovery_img_file = from_testdata_base("recovery.img"); |
| 279 | std::string recovery_img_sha1; |
| 280 | size_t recovery_img_size; |
| 281 | sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size); |
| 282 | |
| 283 | // Generate the bsdiff patch of recovery-from-boot.p. |
| 284 | std::string src_content; |
| 285 | ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content)); |
| 286 | |
| 287 | std::string tgt_content; |
| 288 | ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content)); |
| 289 | |
| 290 | TemporaryFile patch_file; |
| 291 | ASSERT_EQ(0, |
| 292 | bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(), |
| 293 | reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(), |
| 294 | patch_file.path, nullptr)); |
| 295 | |
| 296 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 297 | std::string src_file_arg = |
| 298 | "EMMC:" + boot_img_file + ":" + 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 recovery_img_size_arg = std::to_string(recovery_img_size); |
| 302 | std::string patch_arg = boot_img_sha1 + ":" + patch_file.path; |
| 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 | // Double check the patched recovery image. |
| 312 | std::string tgt_file_sha1; |
| 313 | size_t tgt_file_size; |
| 314 | sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size); |
| 315 | ASSERT_EQ(recovery_img_size, tgt_file_size); |
| 316 | ASSERT_EQ(recovery_img_sha1, tgt_file_sha1); |
| 317 | } |
| 318 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 319 | TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 320 | // Invalid bonus file. |
| 321 | ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); |
| 322 | |
| 323 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 324 | // With bonus file, but missing args. |
| 325 | ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() })); |
| 326 | |
| 327 | std::string boot_img = from_testdata_base("boot.img"); |
| 328 | size_t boot_img_size; |
| 329 | std::string boot_img_sha1; |
| 330 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 331 | |
| 332 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 333 | size_t size; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 334 | std::string recovery_img_sha1; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 335 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 336 | std::string recovery_img_size = std::to_string(size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 337 | |
| 338 | // Bonus file is not supported in flash mode. |
| 339 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> |
| 340 | TemporaryFile tmp4; |
| 341 | std::vector<const char*> args4 = { |
| 342 | "applypatch", |
| 343 | "-b", |
| 344 | bonus_file.c_str(), |
| 345 | boot_img.c_str(), |
| 346 | tmp4.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 | }; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 350 | ASSERT_NE(0, applypatch_modes(args4.size(), args4.data())); |
| 351 | |
| 352 | // Failed to parse patch args. |
| 353 | TemporaryFile tmp5; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 354 | std::string bad_arg1 = |
| 355 | "invalid-sha1:filename" + 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*> args5 = { |
| 357 | "applypatch", |
| 358 | boot_img.c_str(), |
| 359 | tmp5.path, |
| 360 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 361 | recovery_img_size.c_str(), |
| 362 | bad_arg1.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 363 | }; |
| 364 | ASSERT_NE(0, applypatch_modes(args5.size(), args5.data())); |
| 365 | |
| 366 | // Target size cannot be zero. |
| 367 | TemporaryFile tmp6; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 368 | 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] | 369 | std::vector<const char*> args6 = { |
| 370 | "applypatch", |
| 371 | boot_img.c_str(), |
| 372 | tmp6.path, |
| 373 | recovery_img_sha1.c_str(), |
| 374 | "0", // target size |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 375 | patch.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 376 | }; |
| 377 | ASSERT_NE(0, applypatch_modes(args6.size(), args6.data())); |
| 378 | } |
| 379 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 380 | TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 381 | // Insufficient args. |
| 382 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" })); |
| 383 | } |
| 384 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 385 | TEST_F(ApplyPatchModesTest, ShowLicenses) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 386 | ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" })); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 387 | } |