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" |
| 36 | #include "common/test_constants.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 37 | #include "print_sha1.h" |
| 38 | |
| 39 | static const std::string DATA_PATH = getenv("ANDROID_DATA"); |
| 40 | static const std::string TESTDATA_PATH = "/recovery/testdata"; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 41 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 42 | static void sha1sum(const std::string& fname, std::string* sha1) { |
| 43 | ASSERT_NE(nullptr, sha1); |
| 44 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 45 | std::string data; |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 46 | ASSERT_TRUE(android::base::ReadFileToString(fname, &data)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 47 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 48 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 49 | SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest); |
| 50 | *sha1 = print_sha1(digest); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void mangle_file(const std::string& fname) { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 54 | std::string content; |
| 55 | content.reserve(1024); |
| 56 | for (size_t i = 0; i < 1024; i++) { |
| 57 | content[i] = rand() % 256; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 58 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 59 | ASSERT_TRUE(android::base::WriteStringToFile(content, fname)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 62 | static bool file_cmp(const std::string& f1, const std::string& f2) { |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 63 | std::string c1; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 64 | android::base::ReadFileToString(f1, &c1); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 65 | std::string c2; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 66 | android::base::ReadFileToString(f2, &c2); |
| 67 | return c1 == c2; |
| 68 | } |
| 69 | |
Chih-Hung Hsieh | 8b23811 | 2016-08-26 14:54:29 -0700 | [diff] [blame] | 70 | static std::string from_testdata_base(const std::string& fname) { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 71 | return DATA_PATH + NATIVE_TEST_PATH + TESTDATA_PATH + "/" + fname; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | class ApplyPatchTest : public ::testing::Test { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 75 | public: |
| 76 | static void SetUpTestCase() { |
| 77 | // set up files |
| 78 | old_file = from_testdata_base("old.file"); |
| 79 | new_file = from_testdata_base("new.file"); |
| 80 | patch_file = from_testdata_base("patch.bsdiff"); |
| 81 | rand_file = "/cache/applypatch_test_rand.file"; |
| 82 | cache_file = "/cache/saved.file"; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 83 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 84 | // write stuff to rand_file |
| 85 | ASSERT_TRUE(android::base::WriteStringToFile("hello", rand_file)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 86 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 87 | // set up SHA constants |
| 88 | sha1sum(old_file, &old_sha1); |
| 89 | sha1sum(new_file, &new_sha1); |
| 90 | srand(time(NULL)); |
| 91 | bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 92 | bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 93 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 94 | struct stat st; |
| 95 | stat(&new_file[0], &st); |
| 96 | new_size = st.st_size; |
| 97 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 98 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 99 | static std::string old_file; |
| 100 | static std::string new_file; |
| 101 | static std::string rand_file; |
| 102 | static std::string cache_file; |
| 103 | static std::string patch_file; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 104 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 105 | static std::string old_sha1; |
| 106 | static std::string new_sha1; |
| 107 | static std::string bad_sha1_a; |
| 108 | static std::string bad_sha1_b; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 109 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 110 | static size_t new_size; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | std::string ApplyPatchTest::old_file; |
| 114 | std::string ApplyPatchTest::new_file; |
| 115 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 116 | static void cp(const std::string& src, const std::string& tgt) { |
| 117 | std::string cmd = "cp " + src + " " + tgt; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 118 | system(&cmd[0]); |
| 119 | } |
| 120 | |
| 121 | static void backup_old() { |
| 122 | cp(ApplyPatchTest::old_file, ApplyPatchTest::cache_file); |
| 123 | } |
| 124 | |
| 125 | static void restore_old() { |
| 126 | cp(ApplyPatchTest::cache_file, ApplyPatchTest::old_file); |
| 127 | } |
| 128 | |
| 129 | class ApplyPatchCacheTest : public ApplyPatchTest { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 130 | public: |
| 131 | virtual void SetUp() { |
| 132 | backup_old(); |
| 133 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 134 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 135 | virtual void TearDown() { |
| 136 | restore_old(); |
| 137 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | class ApplyPatchFullTest : public ApplyPatchCacheTest { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 141 | public: |
| 142 | static void SetUpTestCase() { |
| 143 | ApplyPatchTest::SetUpTestCase(); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 144 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 145 | output_f = new TemporaryFile(); |
| 146 | output_loc = std::string(output_f->path); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 147 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 148 | struct FileContents fc; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 149 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 150 | ASSERT_EQ(0, LoadFileContents(&rand_file[0], &fc)); |
| 151 | patches.push_back( |
| 152 | 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] | 153 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 154 | ASSERT_EQ(0, LoadFileContents(&patch_file[0], &fc)); |
| 155 | patches.push_back( |
| 156 | std::make_unique<Value>(VAL_BLOB, std::string(fc.data.begin(), fc.data.end()))); |
| 157 | } |
| 158 | |
| 159 | static void TearDownTestCase() { |
| 160 | delete output_f; |
| 161 | } |
| 162 | |
| 163 | static std::vector<std::unique_ptr<Value>> patches; |
| 164 | static TemporaryFile* output_f; |
| 165 | static std::string output_loc; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | class ApplyPatchDoubleCacheTest : public ApplyPatchFullTest { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 169 | public: |
| 170 | virtual void SetUp() { |
| 171 | ApplyPatchCacheTest::SetUp(); |
| 172 | cp(cache_file, "/cache/reallysaved.file"); |
| 173 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 174 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 175 | virtual void TearDown() { |
| 176 | cp("/cache/reallysaved.file", cache_file); |
| 177 | ApplyPatchCacheTest::TearDown(); |
| 178 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | std::string ApplyPatchTest::rand_file; |
| 182 | std::string ApplyPatchTest::patch_file; |
| 183 | std::string ApplyPatchTest::cache_file; |
| 184 | std::string ApplyPatchTest::old_sha1; |
| 185 | std::string ApplyPatchTest::new_sha1; |
| 186 | std::string ApplyPatchTest::bad_sha1_a; |
| 187 | std::string ApplyPatchTest::bad_sha1_b; |
| 188 | |
| 189 | size_t ApplyPatchTest::new_size; |
| 190 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 191 | std::vector<std::unique_ptr<Value>> ApplyPatchFullTest::patches; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 192 | TemporaryFile* ApplyPatchFullTest::output_f; |
| 193 | std::string ApplyPatchFullTest::output_loc; |
| 194 | |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 195 | TEST_F(ApplyPatchTest, CheckModeSkip) { |
| 196 | std::vector<std::string> sha1s; |
| 197 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
| 198 | } |
| 199 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 200 | TEST_F(ApplyPatchTest, CheckModeSingle) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 201 | std::vector<std::string> sha1s = { old_sha1 }; |
| 202 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | TEST_F(ApplyPatchTest, CheckModeMultiple) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 206 | std::vector<std::string> sha1s = { |
| 207 | bad_sha1_a, |
| 208 | old_sha1, |
| 209 | bad_sha1_b |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 210 | }; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 211 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TEST_F(ApplyPatchTest, CheckModeFailure) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 215 | std::vector<std::string> sha1s = { |
| 216 | bad_sha1_a, |
| 217 | bad_sha1_b |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 218 | }; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 219 | ASSERT_NE(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, CheckCacheCorruptedSingle) { |
| 223 | mangle_file(old_file); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 224 | std::vector<std::string> sha1s = { old_sha1 }; |
| 225 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedMultiple) { |
| 229 | mangle_file(old_file); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 230 | std::vector<std::string> sha1s = { |
| 231 | bad_sha1_a, |
| 232 | old_sha1, |
| 233 | bad_sha1_b |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 234 | }; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 235 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedFailure) { |
| 239 | mangle_file(old_file); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 240 | std::vector<std::string> sha1s = { |
| 241 | bad_sha1_a, |
| 242 | bad_sha1_b |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 243 | }; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 244 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingSingle) { |
| 248 | unlink(&old_file[0]); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 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, CheckCacheMissingMultiple) { |
| 254 | unlink(&old_file[0]); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 255 | std::vector<std::string> sha1s = { |
| 256 | bad_sha1_a, |
| 257 | old_sha1, |
| 258 | bad_sha1_b |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 259 | }; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 260 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingFailure) { |
| 264 | unlink(&old_file[0]); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 265 | std::vector<std::string> sha1s = { |
| 266 | bad_sha1_a, |
| 267 | bad_sha1_b |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 268 | }; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 269 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | TEST_F(ApplyPatchFullTest, ApplyInPlace) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 273 | std::vector<std::string> sha1s = { |
| 274 | bad_sha1_a, |
| 275 | old_sha1 |
| 276 | }; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 277 | int ap_result = applypatch(&old_file[0], |
| 278 | "-", |
| 279 | &new_sha1[0], |
| 280 | new_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 281 | sha1s, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 282 | patches, |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 283 | nullptr); |
| 284 | ASSERT_EQ(0, ap_result); |
| 285 | ASSERT_TRUE(file_cmp(old_file, new_file)); |
| 286 | // reapply, applypatch is idempotent so it should succeed |
| 287 | ap_result = applypatch(&old_file[0], |
| 288 | "-", |
| 289 | &new_sha1[0], |
| 290 | new_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 291 | sha1s, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 292 | patches, |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 293 | nullptr); |
| 294 | ASSERT_EQ(0, ap_result); |
| 295 | ASSERT_TRUE(file_cmp(old_file, new_file)); |
| 296 | } |
| 297 | |
| 298 | TEST_F(ApplyPatchFullTest, ApplyInNewLocation) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 299 | std::vector<std::string> sha1s = { |
| 300 | bad_sha1_a, |
| 301 | old_sha1 |
| 302 | }; |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 303 | // Apply bsdiff patch to new location. |
| 304 | ASSERT_EQ(0, applypatch(&old_file[0], |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 305 | &output_loc[0], |
| 306 | &new_sha1[0], |
| 307 | new_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 308 | sha1s, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 309 | patches, |
| 310 | nullptr)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 311 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 312 | |
| 313 | // Reapply to the same location. |
| 314 | ASSERT_EQ(0, applypatch(&old_file[0], |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 315 | &output_loc[0], |
| 316 | &new_sha1[0], |
| 317 | new_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 318 | sha1s, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 319 | patches, |
| 320 | nullptr)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 321 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
| 322 | } |
| 323 | |
| 324 | TEST_F(ApplyPatchFullTest, ApplyCorruptedInNewLocation) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 325 | std::vector<std::string> sha1s = { |
| 326 | bad_sha1_a, |
| 327 | old_sha1 |
| 328 | }; |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 329 | // Apply bsdiff patch to new location with corrupted source. |
| 330 | mangle_file(old_file); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 331 | int ap_result = applypatch(&old_file[0], |
| 332 | &output_loc[0], |
| 333 | &new_sha1[0], |
| 334 | new_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 335 | sha1s, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 336 | patches, |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 337 | nullptr); |
| 338 | ASSERT_EQ(0, ap_result); |
| 339 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 340 | |
| 341 | // Reapply bsdiff patch to new location with corrupted source. |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 342 | ap_result = applypatch(&old_file[0], |
| 343 | &output_loc[0], |
| 344 | &new_sha1[0], |
| 345 | new_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 346 | sha1s, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 347 | patches, |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 348 | nullptr); |
| 349 | ASSERT_EQ(0, ap_result); |
| 350 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
| 351 | } |
| 352 | |
| 353 | TEST_F(ApplyPatchDoubleCacheTest, ApplyDoubleCorruptedInNewLocation) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 354 | std::vector<std::string> sha1s = { |
| 355 | bad_sha1_a, |
| 356 | old_sha1 |
| 357 | }; |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 358 | |
| 359 | // Apply bsdiff patch to new location with corrupted source and copy (no new file). |
| 360 | // Expected to fail. |
| 361 | mangle_file(old_file); |
| 362 | mangle_file(cache_file); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 363 | int ap_result = applypatch(&old_file[0], |
| 364 | &output_loc[0], |
| 365 | &new_sha1[0], |
| 366 | new_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 367 | sha1s, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 368 | patches, |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 369 | nullptr); |
| 370 | ASSERT_NE(0, ap_result); |
| 371 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 372 | |
| 373 | // Expected to fail again on retry. |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 374 | ap_result = applypatch(&old_file[0], |
| 375 | &output_loc[0], |
| 376 | &new_sha1[0], |
| 377 | new_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 378 | sha1s, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 379 | patches, |
| 380 | nullptr); |
| 381 | ASSERT_NE(0, ap_result); |
| 382 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
| 383 | |
| 384 | // Expected to fail with incorrect new file. |
| 385 | mangle_file(output_loc); |
| 386 | ap_result = applypatch(&old_file[0], |
| 387 | &output_loc[0], |
| 388 | &new_sha1[0], |
| 389 | new_size, |
| 390 | sha1s, |
| 391 | patches, |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 392 | nullptr); |
| 393 | ASSERT_NE(0, ap_result); |
| 394 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
| 395 | } |