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 | |
| 108 | std::string ApplyPatchTest::old_file; |
| 109 | std::string ApplyPatchTest::new_file; |
| 110 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 111 | static void cp(const std::string& src, const std::string& tgt) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 112 | std::string cmd = "cp " + src + " " + tgt; |
| 113 | system(cmd.c_str()); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void backup_old() { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 117 | cp(ApplyPatchTest::old_file, ApplyPatchTest::cache_file); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void restore_old() { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 121 | cp(ApplyPatchTest::cache_file, ApplyPatchTest::old_file); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | class ApplyPatchCacheTest : public ApplyPatchTest { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 125 | public: |
| 126 | virtual void SetUp() { |
| 127 | backup_old(); |
| 128 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 129 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 130 | virtual void TearDown() { |
| 131 | restore_old(); |
| 132 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class ApplyPatchFullTest : public ApplyPatchCacheTest { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 136 | public: |
| 137 | static void SetUpTestCase() { |
| 138 | ApplyPatchTest::SetUpTestCase(); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 139 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 140 | output_f = new TemporaryFile(); |
| 141 | output_loc = std::string(output_f->path); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 142 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 143 | struct FileContents fc; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 144 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 145 | ASSERT_EQ(0, LoadFileContents(&rand_file[0], &fc)); |
| 146 | patches.push_back( |
| 147 | std::make_unique<Value>(VAL_BLOB, std::string(fc.data.begin(), fc.data.end()))); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 148 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 149 | ASSERT_EQ(0, LoadFileContents(&patch_file[0], &fc)); |
| 150 | patches.push_back( |
| 151 | std::make_unique<Value>(VAL_BLOB, std::string(fc.data.begin(), fc.data.end()))); |
| 152 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 153 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 154 | static void TearDownTestCase() { |
| 155 | delete output_f; |
| 156 | patches.clear(); |
| 157 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 158 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 159 | static std::vector<std::unique_ptr<Value>> patches; |
| 160 | static TemporaryFile* output_f; |
| 161 | static std::string output_loc; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | class ApplyPatchDoubleCacheTest : public ApplyPatchFullTest { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 165 | public: |
| 166 | virtual void SetUp() { |
| 167 | ApplyPatchCacheTest::SetUp(); |
| 168 | cp(cache_file, "/cache/reallysaved.file"); |
| 169 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 170 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 171 | virtual void TearDown() { |
| 172 | cp("/cache/reallysaved.file", cache_file); |
| 173 | ApplyPatchCacheTest::TearDown(); |
| 174 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | std::string ApplyPatchTest::rand_file; |
| 178 | std::string ApplyPatchTest::patch_file; |
| 179 | std::string ApplyPatchTest::cache_file; |
| 180 | std::string ApplyPatchTest::old_sha1; |
| 181 | std::string ApplyPatchTest::new_sha1; |
| 182 | std::string ApplyPatchTest::bad_sha1_a; |
| 183 | std::string ApplyPatchTest::bad_sha1_b; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 184 | size_t ApplyPatchTest::old_size; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 185 | size_t ApplyPatchTest::new_size; |
| 186 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 187 | std::vector<std::unique_ptr<Value>> ApplyPatchFullTest::patches; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 188 | TemporaryFile* ApplyPatchFullTest::output_f; |
| 189 | std::string ApplyPatchFullTest::output_loc; |
| 190 | |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 191 | TEST_F(ApplyPatchTest, CheckModeSkip) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 192 | std::vector<std::string> sha1s; |
| 193 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 196 | TEST_F(ApplyPatchTest, CheckModeSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 197 | std::vector<std::string> sha1s = { old_sha1 }; |
| 198 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | TEST_F(ApplyPatchTest, CheckModeMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 202 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 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(ApplyPatchTest, CheckModeFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 207 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 208 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 211 | TEST_F(ApplyPatchTest, CheckModeEmmcTarget) { |
| 212 | // EMMC:old_file:size:sha1 should pass the check. |
| 213 | std::string src_file = |
| 214 | "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1; |
| 215 | std::vector<std::string> sha1s; |
| 216 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 217 | |
| 218 | // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 219 | src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 220 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 221 | ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s)); |
| 222 | |
| 223 | // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 224 | src_file = "EMMC:" + old_file + ":" + |
| 225 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 226 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 227 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 228 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 229 | |
| 230 | // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check. |
| 231 | src_file = "EMMC:" + old_file + ":" + |
| 232 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 233 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 234 | std::to_string(old_size) + ":" + old_sha1; |
| 235 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 236 | |
| 237 | // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1 |
| 238 | // should pass the check. |
| 239 | src_file = "EMMC:" + new_file + ":" + |
| 240 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 241 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 242 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 243 | std::to_string(new_size) + ":" + new_sha1; |
| 244 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 245 | } |
| 246 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 247 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 248 | mangle_file(old_file); |
| 249 | std::vector<std::string> sha1s = { old_sha1 }; |
| 250 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 254 | mangle_file(old_file); |
| 255 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 256 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 260 | mangle_file(old_file); |
| 261 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 262 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 266 | unlink(&old_file[0]); |
| 267 | std::vector<std::string> sha1s = { old_sha1 }; |
| 268 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 272 | unlink(&old_file[0]); |
| 273 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 274 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 278 | unlink(&old_file[0]); |
| 279 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 280 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | TEST_F(ApplyPatchFullTest, ApplyInPlace) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 284 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 }; |
| 285 | ASSERT_EQ(0, applypatch(&old_file[0], "-", &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 286 | ASSERT_TRUE(file_cmp(old_file, new_file)); |
| 287 | |
| 288 | // reapply, applypatch is idempotent so it should succeed |
| 289 | ASSERT_EQ(0, applypatch(&old_file[0], "-", &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 290 | ASSERT_TRUE(file_cmp(old_file, new_file)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | TEST_F(ApplyPatchFullTest, ApplyInNewLocation) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 294 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 }; |
| 295 | // Apply bsdiff patch to new location. |
| 296 | ASSERT_EQ( |
| 297 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 298 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 299 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 300 | // Reapply to the same location. |
| 301 | ASSERT_EQ( |
| 302 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 303 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | TEST_F(ApplyPatchFullTest, ApplyCorruptedInNewLocation) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 307 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 }; |
| 308 | // Apply bsdiff patch to new location with corrupted source. |
| 309 | mangle_file(old_file); |
| 310 | ASSERT_EQ( |
| 311 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 312 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 313 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 314 | // Reapply bsdiff patch to new location with corrupted source. |
| 315 | ASSERT_EQ( |
| 316 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 317 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | TEST_F(ApplyPatchDoubleCacheTest, ApplyDoubleCorruptedInNewLocation) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 321 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 }; |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 322 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 323 | // Apply bsdiff patch to new location with corrupted source and copy (no new file). |
| 324 | // Expected to fail. |
| 325 | mangle_file(old_file); |
| 326 | mangle_file(cache_file); |
| 327 | ASSERT_NE( |
| 328 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 329 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 330 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 331 | // Expected to fail again on retry. |
| 332 | ASSERT_NE( |
| 333 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 334 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 335 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 336 | // Expected to fail with incorrect new file. |
| 337 | mangle_file(output_loc); |
| 338 | ASSERT_NE( |
| 339 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 340 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
| 341 | } |
| 342 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 343 | TEST(ApplyPatchModesTest, InvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 344 | // At least two args (including the filename). |
| 345 | ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" })); |
| 346 | |
| 347 | // Unrecognized args. |
| 348 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" })); |
| 349 | } |
| 350 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 351 | TEST(ApplyPatchModesTest, PatchMode) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 352 | std::string boot_img = from_testdata_base("boot.img"); |
| 353 | size_t boot_img_size; |
| 354 | std::string boot_img_sha1; |
| 355 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 356 | |
| 357 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 358 | std::string recovery_img_sha1; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 359 | size_t size; |
| 360 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 361 | std::string recovery_img_size = std::to_string(size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 362 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 363 | |
| 364 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 365 | TemporaryFile tmp1; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 366 | std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 367 | std::vector<const char*> args = { |
| 368 | "applypatch", |
| 369 | "-b", |
| 370 | bonus_file.c_str(), |
| 371 | boot_img.c_str(), |
| 372 | tmp1.path, |
| 373 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 374 | recovery_img_size.c_str(), |
| 375 | patch.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 376 | }; |
| 377 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 378 | |
| 379 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 380 | TemporaryFile tmp2; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 381 | 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] | 382 | std::vector<const char*> args2 = { |
| 383 | "applypatch", |
| 384 | boot_img.c_str(), |
| 385 | tmp2.path, |
| 386 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 387 | recovery_img_size.c_str(), |
| 388 | patch.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 389 | }; |
| 390 | ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data())); |
| 391 | |
| 392 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \ |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 393 | // <src-sha1-fake>:<patch1> <src-sha1>:<patch2> |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 394 | TemporaryFile tmp3; |
| 395 | std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 396 | std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 397 | std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); |
| 398 | std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 399 | std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 400 | std::vector<const char*> args3 = { |
| 401 | "applypatch", |
| 402 | "-b", |
| 403 | bonus_file.c_str(), |
| 404 | boot_img.c_str(), |
| 405 | tmp3.path, |
| 406 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 407 | recovery_img_size.c_str(), |
| 408 | patch1.c_str(), |
| 409 | patch2.c_str(), |
| 410 | patch3.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 411 | }; |
| 412 | ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); |
| 413 | } |
| 414 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 415 | TEST(ApplyPatchModesTest, PatchModeEmmcTarget) { |
| 416 | std::string boot_img = from_testdata_base("boot.img"); |
| 417 | size_t boot_img_size; |
| 418 | std::string boot_img_sha1; |
| 419 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 420 | |
| 421 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 422 | size_t size; |
| 423 | std::string recovery_img_sha1; |
| 424 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 425 | std::string recovery_img_size = std::to_string(size); |
| 426 | |
| 427 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 428 | |
| 429 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 430 | TemporaryFile tmp1; |
| 431 | std::string src_file = |
| 432 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 433 | std::string tgt_file = "EMMC:" + std::string(tmp1.path); |
| 434 | std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 435 | std::vector<const char*> args = { |
| 436 | "applypatch", |
| 437 | "-b", |
| 438 | bonus_file.c_str(), |
| 439 | src_file.c_str(), |
| 440 | tgt_file.c_str(), |
| 441 | recovery_img_sha1.c_str(), |
| 442 | recovery_img_size.c_str(), |
| 443 | patch.c_str() |
| 444 | }; |
| 445 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 446 | |
| 447 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 448 | TemporaryFile tmp2; |
| 449 | patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
| 450 | tgt_file = "EMMC:" + std::string(tmp2.path); |
| 451 | std::vector<const char*> args2 = { |
| 452 | "applypatch", |
| 453 | src_file.c_str(), |
| 454 | tgt_file.c_str(), |
| 455 | recovery_img_sha1.c_str(), |
| 456 | recovery_img_size.c_str(), |
| 457 | patch.c_str() |
| 458 | }; |
| 459 | ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data())); |
| 460 | |
| 461 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \ |
| 462 | // <src-sha1-fake>:<patch1> <src-sha1>:<patch2> |
| 463 | TemporaryFile tmp3; |
| 464 | tgt_file = "EMMC:" + std::string(tmp3.path); |
| 465 | std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 466 | std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
| 467 | std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); |
| 468 | std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 469 | std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p"); |
| 470 | std::vector<const char*> args3 = { |
| 471 | "applypatch", |
| 472 | "-b", |
| 473 | bonus_file.c_str(), |
| 474 | src_file.c_str(), |
| 475 | tgt_file.c_str(), |
| 476 | recovery_img_sha1.c_str(), |
| 477 | recovery_img_size.c_str(), |
| 478 | patch1.c_str(), |
| 479 | patch2.c_str(), |
| 480 | patch3.c_str() |
| 481 | }; |
| 482 | ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); |
| 483 | } |
| 484 | |
| 485 | TEST(ApplyPatchModesTest, PatchModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 486 | // Invalid bonus file. |
| 487 | ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); |
| 488 | |
| 489 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 490 | // With bonus file, but missing args. |
| 491 | ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() })); |
| 492 | |
| 493 | std::string boot_img = from_testdata_base("boot.img"); |
| 494 | size_t boot_img_size; |
| 495 | std::string boot_img_sha1; |
| 496 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 497 | |
| 498 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 499 | size_t size; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 500 | std::string recovery_img_sha1; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 501 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 502 | std::string recovery_img_size = std::to_string(size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 503 | |
| 504 | // Bonus file is not supported in flash mode. |
| 505 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> |
| 506 | TemporaryFile tmp4; |
| 507 | std::vector<const char*> args4 = { |
| 508 | "applypatch", |
| 509 | "-b", |
| 510 | bonus_file.c_str(), |
| 511 | boot_img.c_str(), |
| 512 | tmp4.path, |
| 513 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 514 | recovery_img_size.c_str() |
| 515 | }; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 516 | ASSERT_NE(0, applypatch_modes(args4.size(), args4.data())); |
| 517 | |
| 518 | // Failed to parse patch args. |
| 519 | TemporaryFile tmp5; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 520 | std::string bad_arg1 = |
| 521 | "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 522 | std::vector<const char*> args5 = { |
| 523 | "applypatch", |
| 524 | boot_img.c_str(), |
| 525 | tmp5.path, |
| 526 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 527 | recovery_img_size.c_str(), |
| 528 | bad_arg1.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 529 | }; |
| 530 | ASSERT_NE(0, applypatch_modes(args5.size(), args5.data())); |
| 531 | |
| 532 | // Target size cannot be zero. |
| 533 | TemporaryFile tmp6; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 534 | 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] | 535 | std::vector<const char*> args6 = { |
| 536 | "applypatch", |
| 537 | boot_img.c_str(), |
| 538 | tmp6.path, |
| 539 | recovery_img_sha1.c_str(), |
| 540 | "0", // target size |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 541 | patch.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 542 | }; |
| 543 | ASSERT_NE(0, applypatch_modes(args6.size(), args6.data())); |
| 544 | } |
| 545 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 546 | TEST(ApplyPatchModesTest, CheckModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 547 | // Insufficient args. |
| 548 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" })); |
| 549 | } |
| 550 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 551 | TEST(ApplyPatchModesTest, SpaceModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 552 | // Insufficient args. |
| 553 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-s" })); |
| 554 | |
| 555 | // Invalid bytes arg. |
| 556 | ASSERT_EQ(1, applypatch_modes(3, (const char* []){ "applypatch", "-s", "x" })); |
| 557 | |
| 558 | // 0 is invalid. |
| 559 | ASSERT_EQ(1, applypatch_modes(3, (const char* []){ "applypatch", "-s", "0" })); |
| 560 | |
| 561 | // 0x10 is fine. |
| 562 | ASSERT_EQ(0, applypatch_modes(3, (const char* []){ "applypatch", "-s", "0x10" })); |
| 563 | } |
| 564 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 565 | TEST(ApplyPatchModesTest, ShowLicenses) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 566 | ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" })); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 567 | } |