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