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 |
| 86 | sha1sum(old_file, &old_sha1); |
| 87 | sha1sum(new_file, &new_sha1); |
| 88 | srand(time(nullptr)); |
| 89 | bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 90 | bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 91 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 92 | struct stat st; |
| 93 | stat(&new_file[0], &st); |
| 94 | new_size = st.st_size; |
| 95 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 96 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 97 | static std::string old_file; |
| 98 | static std::string new_file; |
| 99 | static std::string rand_file; |
| 100 | static std::string cache_file; |
| 101 | static std::string patch_file; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 102 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 103 | static std::string old_sha1; |
| 104 | static std::string new_sha1; |
| 105 | static std::string bad_sha1_a; |
| 106 | static std::string bad_sha1_b; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 107 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 108 | static size_t new_size; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | std::string ApplyPatchTest::old_file; |
| 112 | std::string ApplyPatchTest::new_file; |
| 113 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 114 | static void cp(const std::string& src, const std::string& tgt) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 115 | std::string cmd = "cp " + src + " " + tgt; |
| 116 | system(cmd.c_str()); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void backup_old() { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 120 | cp(ApplyPatchTest::old_file, ApplyPatchTest::cache_file); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void restore_old() { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 124 | cp(ApplyPatchTest::cache_file, ApplyPatchTest::old_file); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | class ApplyPatchCacheTest : public ApplyPatchTest { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 128 | public: |
| 129 | virtual void SetUp() { |
| 130 | backup_old(); |
| 131 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 132 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 133 | virtual void TearDown() { |
| 134 | restore_old(); |
| 135 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | class ApplyPatchFullTest : public ApplyPatchCacheTest { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 139 | public: |
| 140 | static void SetUpTestCase() { |
| 141 | ApplyPatchTest::SetUpTestCase(); |
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 | output_f = new TemporaryFile(); |
| 144 | output_loc = std::string(output_f->path); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 145 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 146 | struct FileContents fc; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 147 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 148 | ASSERT_EQ(0, LoadFileContents(&rand_file[0], &fc)); |
| 149 | patches.push_back( |
| 150 | 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] | 151 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 152 | ASSERT_EQ(0, LoadFileContents(&patch_file[0], &fc)); |
| 153 | patches.push_back( |
| 154 | std::make_unique<Value>(VAL_BLOB, std::string(fc.data.begin(), fc.data.end()))); |
| 155 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 156 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 157 | static void TearDownTestCase() { |
| 158 | delete output_f; |
| 159 | patches.clear(); |
| 160 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 161 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 162 | static std::vector<std::unique_ptr<Value>> patches; |
| 163 | static TemporaryFile* output_f; |
| 164 | static std::string output_loc; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | class ApplyPatchDoubleCacheTest : public ApplyPatchFullTest { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 168 | public: |
| 169 | virtual void SetUp() { |
| 170 | ApplyPatchCacheTest::SetUp(); |
| 171 | cp(cache_file, "/cache/reallysaved.file"); |
| 172 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 173 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 174 | virtual void TearDown() { |
| 175 | cp("/cache/reallysaved.file", cache_file); |
| 176 | ApplyPatchCacheTest::TearDown(); |
| 177 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | std::string ApplyPatchTest::rand_file; |
| 181 | std::string ApplyPatchTest::patch_file; |
| 182 | std::string ApplyPatchTest::cache_file; |
| 183 | std::string ApplyPatchTest::old_sha1; |
| 184 | std::string ApplyPatchTest::new_sha1; |
| 185 | std::string ApplyPatchTest::bad_sha1_a; |
| 186 | std::string ApplyPatchTest::bad_sha1_b; |
| 187 | |
| 188 | size_t ApplyPatchTest::new_size; |
| 189 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 190 | std::vector<std::unique_ptr<Value>> ApplyPatchFullTest::patches; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 191 | TemporaryFile* ApplyPatchFullTest::output_f; |
| 192 | std::string ApplyPatchFullTest::output_loc; |
| 193 | |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 194 | TEST_F(ApplyPatchTest, CheckModeSkip) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 195 | std::vector<std::string> sha1s; |
| 196 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 199 | TEST_F(ApplyPatchTest, CheckModeSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 200 | std::vector<std::string> sha1s = { old_sha1 }; |
| 201 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(ApplyPatchTest, CheckModeMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 205 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 206 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(ApplyPatchTest, CheckModeFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 210 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 211 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 215 | mangle_file(old_file); |
| 216 | std::vector<std::string> sha1s = { old_sha1 }; |
| 217 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 221 | mangle_file(old_file); |
| 222 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 223 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 227 | mangle_file(old_file); |
| 228 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 229 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 233 | unlink(&old_file[0]); |
| 234 | std::vector<std::string> sha1s = { old_sha1 }; |
| 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, CheckCacheMissingMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 239 | unlink(&old_file[0]); |
| 240 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 241 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | TEST_F(ApplyPatchCacheTest, CheckCacheMissingFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 245 | unlink(&old_file[0]); |
| 246 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 247 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | TEST_F(ApplyPatchFullTest, ApplyInPlace) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 251 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 }; |
| 252 | ASSERT_EQ(0, applypatch(&old_file[0], "-", &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 253 | ASSERT_TRUE(file_cmp(old_file, new_file)); |
| 254 | |
| 255 | // reapply, applypatch is idempotent so it should succeed |
| 256 | ASSERT_EQ(0, applypatch(&old_file[0], "-", &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 257 | ASSERT_TRUE(file_cmp(old_file, new_file)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | TEST_F(ApplyPatchFullTest, ApplyInNewLocation) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 261 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 }; |
| 262 | // Apply bsdiff patch to new location. |
| 263 | ASSERT_EQ( |
| 264 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 265 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 266 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 267 | // Reapply to the same location. |
| 268 | ASSERT_EQ( |
| 269 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 270 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | TEST_F(ApplyPatchFullTest, ApplyCorruptedInNewLocation) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 274 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 }; |
| 275 | // Apply bsdiff patch to new location with corrupted source. |
| 276 | mangle_file(old_file); |
| 277 | ASSERT_EQ( |
| 278 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 279 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 280 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 281 | // Reapply bsdiff patch to new location with corrupted source. |
| 282 | ASSERT_EQ( |
| 283 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 284 | ASSERT_TRUE(file_cmp(output_loc, new_file)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | TEST_F(ApplyPatchDoubleCacheTest, ApplyDoubleCorruptedInNewLocation) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 288 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 }; |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 289 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 290 | // Apply bsdiff patch to new location with corrupted source and copy (no new file). |
| 291 | // Expected to fail. |
| 292 | mangle_file(old_file); |
| 293 | mangle_file(cache_file); |
| 294 | ASSERT_NE( |
| 295 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 296 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 297 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 298 | // Expected to fail again on retry. |
| 299 | ASSERT_NE( |
| 300 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 301 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 302 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 303 | // Expected to fail with incorrect new file. |
| 304 | mangle_file(output_loc); |
| 305 | ASSERT_NE( |
| 306 | 0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr)); |
| 307 | ASSERT_FALSE(file_cmp(output_loc, new_file)); |
| 308 | } |
| 309 | |
| 310 | TEST(ApplyPatchModes, InvalidArgs) { |
| 311 | // At least two args (including the filename). |
| 312 | ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" })); |
| 313 | |
| 314 | // Unrecognized args. |
| 315 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" })); |
| 316 | } |
| 317 | |
| 318 | TEST(ApplyPatchModes, PatchMode) { |
| 319 | std::string boot_img = from_testdata_base("boot.img"); |
| 320 | size_t boot_img_size; |
| 321 | std::string boot_img_sha1; |
| 322 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 323 | |
| 324 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 325 | size_t recovery_img_size; |
| 326 | std::string recovery_img_sha1; |
| 327 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 328 | |
| 329 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 330 | |
| 331 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 332 | TemporaryFile tmp1; |
| 333 | std::vector<const char*> args = { |
| 334 | "applypatch", |
| 335 | "-b", |
| 336 | bonus_file.c_str(), |
| 337 | boot_img.c_str(), |
| 338 | tmp1.path, |
| 339 | recovery_img_sha1.c_str(), |
| 340 | std::to_string(recovery_img_size).c_str(), |
| 341 | (boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p")).c_str() |
| 342 | }; |
| 343 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 344 | |
| 345 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 346 | TemporaryFile tmp2; |
| 347 | std::vector<const char*> args2 = { |
| 348 | "applypatch", |
| 349 | boot_img.c_str(), |
| 350 | tmp2.path, |
| 351 | recovery_img_sha1.c_str(), |
| 352 | std::to_string(recovery_img_size).c_str(), |
| 353 | (boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p")).c_str() |
| 354 | }; |
| 355 | ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data())); |
| 356 | |
| 357 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \ |
| 358 | // <src-sha1-fake>:<patch1> <src-sha1>:<patch2> |
| 359 | TemporaryFile tmp3; |
| 360 | std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 361 | std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
| 362 | std::vector<const char*> args3 = { |
| 363 | "applypatch", |
| 364 | "-b", |
| 365 | bonus_file.c_str(), |
| 366 | boot_img.c_str(), |
| 367 | tmp3.path, |
| 368 | recovery_img_sha1.c_str(), |
| 369 | std::to_string(recovery_img_size).c_str(), |
| 370 | (bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p")).c_str(), |
| 371 | (boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p")).c_str(), |
| 372 | (bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p")).c_str(), |
| 373 | }; |
| 374 | ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); |
| 375 | } |
| 376 | |
| 377 | TEST(ApplyPatchModes, PatchModeInvalidArgs) { |
| 378 | // Invalid bonus file. |
| 379 | ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); |
| 380 | |
| 381 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 382 | // With bonus file, but missing args. |
| 383 | ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() })); |
| 384 | |
| 385 | std::string boot_img = from_testdata_base("boot.img"); |
| 386 | size_t boot_img_size; |
| 387 | std::string boot_img_sha1; |
| 388 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 389 | |
| 390 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 391 | size_t recovery_img_size; |
| 392 | std::string recovery_img_sha1; |
| 393 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 394 | |
| 395 | // Bonus file is not supported in flash mode. |
| 396 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> |
| 397 | TemporaryFile tmp4; |
| 398 | std::vector<const char*> args4 = { |
| 399 | "applypatch", |
| 400 | "-b", |
| 401 | bonus_file.c_str(), |
| 402 | boot_img.c_str(), |
| 403 | tmp4.path, |
| 404 | recovery_img_sha1.c_str(), |
| 405 | std::to_string(recovery_img_size).c_str() }; |
| 406 | ASSERT_NE(0, applypatch_modes(args4.size(), args4.data())); |
| 407 | |
| 408 | // Failed to parse patch args. |
| 409 | TemporaryFile tmp5; |
| 410 | std::vector<const char*> args5 = { |
| 411 | "applypatch", |
| 412 | boot_img.c_str(), |
| 413 | tmp5.path, |
| 414 | recovery_img_sha1.c_str(), |
| 415 | std::to_string(recovery_img_size).c_str(), |
| 416 | ("invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p")).c_str(), |
| 417 | }; |
| 418 | ASSERT_NE(0, applypatch_modes(args5.size(), args5.data())); |
| 419 | |
| 420 | // Target size cannot be zero. |
| 421 | TemporaryFile tmp6; |
| 422 | std::vector<const char*> args6 = { |
| 423 | "applypatch", |
| 424 | boot_img.c_str(), |
| 425 | tmp6.path, |
| 426 | recovery_img_sha1.c_str(), |
| 427 | "0", // target size |
| 428 | (boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p")).c_str() |
| 429 | }; |
| 430 | ASSERT_NE(0, applypatch_modes(args6.size(), args6.data())); |
| 431 | } |
| 432 | |
| 433 | TEST(ApplyPatchModes, CheckModeInvalidArgs) { |
| 434 | // Insufficient args. |
| 435 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" })); |
| 436 | } |
| 437 | |
| 438 | TEST(ApplyPatchModes, SpaceModeInvalidArgs) { |
| 439 | // Insufficient args. |
| 440 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-s" })); |
| 441 | |
| 442 | // Invalid bytes arg. |
| 443 | ASSERT_EQ(1, applypatch_modes(3, (const char* []){ "applypatch", "-s", "x" })); |
| 444 | |
| 445 | // 0 is invalid. |
| 446 | ASSERT_EQ(1, applypatch_modes(3, (const char* []){ "applypatch", "-s", "0" })); |
| 447 | |
| 448 | // 0x10 is fine. |
| 449 | ASSERT_EQ(0, applypatch_modes(3, (const char* []){ "applypatch", "-s", "0x10" })); |
| 450 | } |
| 451 | |
| 452 | TEST(ApplyPatchModes, ShowLicenses) { |
| 453 | ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" })); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 454 | } |