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