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 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 18 | #include <fcntl.h> |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 19 | #include <libgen.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/statvfs.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <time.h> |
| 26 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 27 | #include <algorithm> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 28 | #include <memory> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 29 | #include <string> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 30 | #include <vector> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 31 | |
| 32 | #include <android-base/file.h> |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
| 35 | #include <android-base/test_utils.h> |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 36 | #include <android-base/unique_fd.h> |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 37 | #include <bsdiff/bsdiff.h> |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 38 | #include <gtest/gtest.h> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 39 | #include <openssl/sha.h> |
Tianjie Xu | 9e1ccd4 | 2018-04-25 17:19:20 -0700 | [diff] [blame] | 40 | #include <zlib.h> |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 41 | |
| 42 | #include "applypatch/applypatch.h" |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 43 | #include "applypatch/applypatch_modes.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 44 | #include "common/test_constants.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 45 | #include "otautil/paths.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 46 | #include "otautil/print_sha1.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 47 | |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 48 | using namespace std::string_literals; |
| 49 | |
Tianjie Xu | 9e1ccd4 | 2018-04-25 17:19:20 -0700 | [diff] [blame] | 50 | // TODO(b/67849209) Remove after debug the flakiness. |
| 51 | static void DecompressAndDumpRecoveryImage(const std::string& image_path) { |
| 52 | // Expected recovery_image structure |
| 53 | // chunk normal: 45066 bytes |
| 54 | // chunk deflate: 479442 bytes |
| 55 | // chunk normal: 5199 bytes |
| 56 | std::string recovery_content; |
| 57 | ASSERT_TRUE(android::base::ReadFileToString(image_path, &recovery_content)); |
| 58 | ASSERT_GT(recovery_content.size(), 45066 + 5199); |
| 59 | |
| 60 | z_stream strm = {}; |
| 61 | strm.avail_in = recovery_content.size() - 45066 - 5199; |
| 62 | strm.next_in = |
| 63 | const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(recovery_content.data())) + 45066; |
| 64 | |
| 65 | ASSERT_EQ(Z_OK, inflateInit2(&strm, -15)); |
| 66 | |
| 67 | constexpr unsigned int BUFFER_SIZE = 32768; |
| 68 | std::vector<uint8_t> uncompressed_data(BUFFER_SIZE); |
| 69 | size_t uncompressed_length = 0; |
| 70 | SHA_CTX ctx; |
| 71 | SHA1_Init(&ctx); |
| 72 | int ret; |
| 73 | do { |
| 74 | strm.avail_out = BUFFER_SIZE; |
| 75 | strm.next_out = uncompressed_data.data(); |
| 76 | |
| 77 | ret = inflate(&strm, Z_NO_FLUSH); |
| 78 | ASSERT_GE(ret, 0); |
| 79 | |
| 80 | SHA1_Update(&ctx, uncompressed_data.data(), BUFFER_SIZE - strm.avail_out); |
| 81 | uncompressed_length += BUFFER_SIZE - strm.avail_out; |
| 82 | } while (ret != Z_STREAM_END); |
| 83 | inflateEnd(&strm); |
| 84 | |
| 85 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 86 | SHA1_Final(digest, &ctx); |
| 87 | GTEST_LOG_(INFO) << "uncompressed length " << uncompressed_length |
| 88 | << " sha1: " << short_sha1(digest); |
| 89 | } |
| 90 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 91 | static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) { |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(sha1 != nullptr); |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 93 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 94 | std::string data; |
| 95 | ASSERT_TRUE(android::base::ReadFileToString(fname, &data)); |
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 | if (fsize != nullptr) { |
| 98 | *fsize = data.size(); |
| 99 | } |
| 100 | |
| 101 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 102 | SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest); |
| 103 | *sha1 = print_sha1(digest); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void mangle_file(const std::string& fname) { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 107 | std::string content(1024, '\0'); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 108 | for (size_t i = 0; i < 1024; i++) { |
| 109 | content[i] = rand() % 256; |
| 110 | } |
| 111 | ASSERT_TRUE(android::base::WriteStringToFile(content, fname)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 114 | static void test_logger(android::base::LogId /* id */, android::base::LogSeverity severity, |
| 115 | const char* /* tag */, const char* /* file */, unsigned int /* line */, |
| 116 | const char* message) { |
| 117 | if (severity >= android::base::GetMinimumLogSeverity()) { |
| 118 | fprintf(stdout, "%s\n", message); |
| 119 | } |
| 120 | } |
| 121 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 122 | class ApplyPatchTest : public ::testing::Test { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 123 | public: |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 124 | virtual void SetUp() override { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 125 | // set up files |
| 126 | old_file = from_testdata_base("old.file"); |
| 127 | new_file = from_testdata_base("new.file"); |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 128 | nonexistent_file = from_testdata_base("nonexistent.file"); |
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 | // set up SHA constants |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 131 | sha1sum(old_file, &old_sha1, &old_size); |
| 132 | sha1sum(new_file, &new_sha1, &new_size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 133 | srand(time(nullptr)); |
| 134 | bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 135 | bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 136 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 137 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 138 | std::string old_file; |
| 139 | std::string new_file; |
| 140 | std::string nonexistent_file; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 141 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 142 | std::string old_sha1; |
| 143 | std::string new_sha1; |
| 144 | std::string bad_sha1_a; |
| 145 | std::string bad_sha1_b; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 146 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 147 | size_t old_size; |
| 148 | size_t new_size; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 151 | class ApplyPatchCacheTest : public ApplyPatchTest { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 152 | protected: |
| 153 | void SetUp() override { |
| 154 | ApplyPatchTest::SetUp(); |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 155 | Paths::Get().set_cache_temp_source(old_file); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 156 | } |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 157 | }; |
| 158 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 159 | class ApplyPatchModesTest : public ::testing::Test { |
| 160 | protected: |
| 161 | void SetUp() override { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 162 | Paths::Get().set_cache_temp_source(cache_source.path); |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 163 | android::base::InitLogging(nullptr, &test_logger); |
| 164 | android::base::SetMinimumLogSeverity(android::base::LogSeverity::DEBUG); |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TemporaryFile cache_source; |
| 168 | }; |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 169 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 170 | class FreeCacheTest : public ::testing::Test { |
| 171 | protected: |
| 172 | static constexpr size_t PARTITION_SIZE = 4096 * 10; |
| 173 | |
| 174 | // Returns a sorted list of files in |dirname|. |
| 175 | static std::vector<std::string> FindFilesInDir(const std::string& dirname) { |
| 176 | std::vector<std::string> file_list; |
| 177 | |
| 178 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir); |
| 179 | struct dirent* de; |
| 180 | while ((de = readdir(d.get())) != 0) { |
| 181 | std::string path = dirname + "/" + de->d_name; |
| 182 | |
| 183 | struct stat st; |
| 184 | if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { |
| 185 | file_list.emplace_back(de->d_name); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | std::sort(file_list.begin(), file_list.end()); |
| 190 | return file_list; |
| 191 | } |
| 192 | |
| 193 | static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) { |
| 194 | std::string zeros(4096, 0); |
| 195 | for (const auto& file : files) { |
| 196 | std::string path = dir + "/" + file; |
| 197 | ASSERT_TRUE(android::base::WriteStringToFile(zeros, path)); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void SetUp() override { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 202 | Paths::Get().set_cache_log_directory(mock_log_dir.path); |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // A mock method to calculate the free space. It assumes the partition has a total size of 40960 |
| 206 | // bytes and all files are 4096 bytes in size. |
| 207 | size_t MockFreeSpaceChecker(const std::string& dirname) { |
| 208 | std::vector<std::string> files = FindFilesInDir(dirname); |
| 209 | return PARTITION_SIZE - 4096 * files.size(); |
| 210 | } |
| 211 | |
| 212 | TemporaryDir mock_cache; |
| 213 | TemporaryDir mock_log_dir; |
| 214 | }; |
| 215 | |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 216 | TEST_F(ApplyPatchTest, CheckModeSkip) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 217 | std::vector<std::string> sha1s; |
| 218 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Tianjie Xu | a5fd5ab | 2016-10-18 15:18:22 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 221 | TEST_F(ApplyPatchTest, CheckModeSingle) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 222 | std::vector<std::string> sha1s = { old_sha1 }; |
| 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(ApplyPatchTest, CheckModeMultiple) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 227 | std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 228 | ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TEST_F(ApplyPatchTest, CheckModeFailure) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 232 | std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b }; |
| 233 | ASSERT_NE(0, applypatch_check(&old_file[0], sha1s)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 236 | TEST_F(ApplyPatchTest, CheckModeEmmcTarget) { |
| 237 | // EMMC:old_file:size:sha1 should pass the check. |
| 238 | std::string src_file = |
| 239 | "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1; |
| 240 | std::vector<std::string> sha1s; |
| 241 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 242 | |
| 243 | // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 244 | src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 245 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 246 | ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s)); |
| 247 | |
| 248 | // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 249 | src_file = "EMMC:" + old_file + ":" + |
| 250 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 251 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 252 | std::to_string(old_size + 1) + ":" + old_sha1; |
| 253 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 254 | |
| 255 | // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check. |
| 256 | src_file = "EMMC:" + old_file + ":" + |
| 257 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 258 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 259 | std::to_string(old_size) + ":" + old_sha1; |
| 260 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 261 | |
| 262 | // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1 |
| 263 | // should pass the check. |
| 264 | src_file = "EMMC:" + new_file + ":" + |
| 265 | std::to_string(old_size + 1) + ":" + old_sha1 + ":" + |
| 266 | std::to_string(old_size - 1) + ":" + old_sha1 + ":" + |
| 267 | std::to_string(old_size) + ":" + old_sha1 + ":" + |
| 268 | std::to_string(new_size) + ":" + new_sha1; |
| 269 | ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s)); |
| 270 | } |
| 271 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 272 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) { |
| 273 | TemporaryFile temp_file; |
| 274 | mangle_file(temp_file.path); |
| 275 | std::vector<std::string> sha1s_single = { old_sha1 }; |
| 276 | ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single)); |
| 277 | ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 280 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) { |
| 281 | TemporaryFile temp_file; |
| 282 | mangle_file(temp_file.path); |
| 283 | std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b }; |
| 284 | ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple)); |
| 285 | ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 288 | TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) { |
| 289 | TemporaryFile temp_file; |
| 290 | mangle_file(temp_file.path); |
| 291 | std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b }; |
| 292 | ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure)); |
| 293 | ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure)); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 296 | TEST_F(ApplyPatchModesTest, InvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 297 | // At least two args (including the filename). |
| 298 | ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" })); |
| 299 | |
| 300 | // Unrecognized args. |
| 301 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" })); |
| 302 | } |
| 303 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 304 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) { |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 305 | std::string boot_img = from_testdata_base("boot.img"); |
| 306 | size_t boot_img_size; |
| 307 | std::string boot_img_sha1; |
| 308 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 309 | |
| 310 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 311 | size_t recovery_img_size; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 312 | std::string recovery_img_sha1; |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 313 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 314 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 315 | |
| 316 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 317 | |
| 318 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 319 | std::string src_file_arg = |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 320 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 321 | TemporaryFile tgt_file; |
| 322 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 323 | std::string patch_arg = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 324 | std::vector<const char*> args = { "applypatch", |
| 325 | "-b", |
| 326 | bonus_file.c_str(), |
| 327 | src_file_arg.c_str(), |
| 328 | tgt_file_arg.c_str(), |
| 329 | recovery_img_sha1.c_str(), |
| 330 | recovery_img_size_arg.c_str(), |
| 331 | patch_arg.c_str() }; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 332 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // Tests patching the EMMC target without a separate bonus file (i.e. recovery-from-boot patch has |
| 336 | // everything). |
| 337 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithoutBonusFile) { |
| 338 | std::string boot_img = from_testdata_base("boot.img"); |
| 339 | size_t boot_img_size; |
| 340 | std::string boot_img_sha1; |
| 341 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 342 | |
| 343 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 344 | size_t recovery_img_size; |
| 345 | std::string recovery_img_sha1; |
| 346 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 347 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 348 | |
| 349 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 350 | std::string src_file_arg = |
| 351 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 352 | TemporaryFile tgt_file; |
| 353 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 354 | std::string patch_arg = |
| 355 | boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
| 356 | std::vector<const char*> args = { "applypatch", |
| 357 | src_file_arg.c_str(), |
| 358 | tgt_file_arg.c_str(), |
| 359 | recovery_img_sha1.c_str(), |
| 360 | recovery_img_size_arg.c_str(), |
| 361 | patch_arg.c_str() }; |
Tianjie Xu | 9e1ccd4 | 2018-04-25 17:19:20 -0700 | [diff] [blame] | 362 | |
| 363 | if (applypatch_modes(args.size(), args.data()) != 0) { |
| 364 | DecompressAndDumpRecoveryImage(tgt_file.path); |
| 365 | FAIL(); |
| 366 | } |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithMultiplePatches) { |
| 370 | std::string boot_img = from_testdata_base("boot.img"); |
| 371 | size_t boot_img_size; |
| 372 | std::string boot_img_sha1; |
| 373 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 374 | |
| 375 | std::string recovery_img = from_testdata_base("recovery.img"); |
| 376 | size_t recovery_img_size; |
| 377 | std::string recovery_img_sha1; |
| 378 | sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); |
| 379 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
| 380 | |
| 381 | std::string bonus_file = from_testdata_base("bonus.file"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 382 | |
| 383 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \ |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 384 | // <src-sha1-fake1>:<patch1> <src-sha1>:<patch2> <src-sha1-fake2>:<patch3> |
| 385 | std::string src_file_arg = |
| 386 | "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 387 | TemporaryFile tgt_file; |
| 388 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 389 | std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); |
| 390 | std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); |
| 391 | std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); |
| 392 | std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); |
| 393 | std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p"); |
Tao Bao | e3499f9 | 2018-03-20 10:33:42 -0700 | [diff] [blame] | 394 | std::vector<const char*> args = { "applypatch", |
| 395 | "-b", |
| 396 | bonus_file.c_str(), |
| 397 | src_file_arg.c_str(), |
| 398 | tgt_file_arg.c_str(), |
| 399 | recovery_img_sha1.c_str(), |
| 400 | recovery_img_size_arg.c_str(), |
| 401 | patch1.c_str(), |
| 402 | patch2.c_str(), |
| 403 | patch3.c_str() }; |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 404 | // TODO(b/67849209): Remove after addressing the flakiness. |
| 405 | printf("Calling applypatch_modes with the following args:\n"); |
| 406 | for (const auto& arg : args) { |
| 407 | printf(" %s\n", arg); |
| 408 | } |
Tianjie Xu | 9e1ccd4 | 2018-04-25 17:19:20 -0700 | [diff] [blame] | 409 | |
| 410 | if (applypatch_modes(args.size(), args.data()) != 0) { |
| 411 | DecompressAndDumpRecoveryImage(tgt_file.path); |
| 412 | FAIL(); |
| 413 | } |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Tao Bao | d612b23 | 2018-03-12 21:18:52 -0700 | [diff] [blame] | 416 | // Ensures that applypatch works with a bsdiff based recovery-from-boot.p. |
| 417 | TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) { |
| 418 | std::string boot_img_file = from_testdata_base("boot.img"); |
| 419 | std::string boot_img_sha1; |
| 420 | size_t boot_img_size; |
| 421 | sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size); |
| 422 | |
| 423 | std::string recovery_img_file = from_testdata_base("recovery.img"); |
| 424 | std::string recovery_img_sha1; |
| 425 | size_t recovery_img_size; |
| 426 | sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size); |
| 427 | |
| 428 | // Generate the bsdiff patch of recovery-from-boot.p. |
| 429 | std::string src_content; |
| 430 | ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content)); |
| 431 | |
| 432 | std::string tgt_content; |
| 433 | ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content)); |
| 434 | |
| 435 | TemporaryFile patch_file; |
| 436 | ASSERT_EQ(0, |
| 437 | bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(), |
| 438 | reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(), |
| 439 | patch_file.path, nullptr)); |
| 440 | |
| 441 | // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch> |
| 442 | std::string src_file_arg = |
| 443 | "EMMC:" + boot_img_file + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; |
| 444 | TemporaryFile tgt_file; |
| 445 | std::string tgt_file_arg = "EMMC:"s + tgt_file.path; |
| 446 | std::string recovery_img_size_arg = std::to_string(recovery_img_size); |
| 447 | std::string patch_arg = boot_img_sha1 + ":" + patch_file.path; |
| 448 | std::vector<const char*> args = { "applypatch", |
| 449 | src_file_arg.c_str(), |
| 450 | tgt_file_arg.c_str(), |
| 451 | recovery_img_sha1.c_str(), |
| 452 | recovery_img_size_arg.c_str(), |
| 453 | patch_arg.c_str() }; |
| 454 | ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); |
| 455 | |
| 456 | // Double check the patched recovery image. |
| 457 | std::string tgt_file_sha1; |
| 458 | size_t tgt_file_size; |
| 459 | sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size); |
| 460 | ASSERT_EQ(recovery_img_size, tgt_file_size); |
| 461 | ASSERT_EQ(recovery_img_sha1, tgt_file_sha1); |
| 462 | } |
| 463 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 464 | TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 465 | // Invalid bonus file. |
| 466 | ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); |
| 467 | |
| 468 | std::string bonus_file = from_testdata_base("bonus.file"); |
| 469 | // With bonus file, but missing args. |
| 470 | ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() })); |
| 471 | |
| 472 | std::string boot_img = from_testdata_base("boot.img"); |
| 473 | size_t boot_img_size; |
| 474 | std::string boot_img_sha1; |
| 475 | sha1sum(boot_img, &boot_img_sha1, &boot_img_size); |
| 476 | |
| 477 | std::string recovery_img = from_testdata_base("recovery.img"); |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 478 | size_t size; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 479 | std::string recovery_img_sha1; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 480 | sha1sum(recovery_img, &recovery_img_sha1, &size); |
| 481 | std::string recovery_img_size = std::to_string(size); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 482 | |
| 483 | // Bonus file is not supported in flash mode. |
| 484 | // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> |
| 485 | TemporaryFile tmp4; |
| 486 | std::vector<const char*> args4 = { |
| 487 | "applypatch", |
| 488 | "-b", |
| 489 | bonus_file.c_str(), |
| 490 | boot_img.c_str(), |
| 491 | tmp4.path, |
| 492 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 493 | recovery_img_size.c_str() |
| 494 | }; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 495 | ASSERT_NE(0, applypatch_modes(args4.size(), args4.data())); |
| 496 | |
| 497 | // Failed to parse patch args. |
| 498 | TemporaryFile tmp5; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 499 | std::string bad_arg1 = |
| 500 | "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p"); |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 501 | std::vector<const char*> args5 = { |
| 502 | "applypatch", |
| 503 | boot_img.c_str(), |
| 504 | tmp5.path, |
| 505 | recovery_img_sha1.c_str(), |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 506 | recovery_img_size.c_str(), |
| 507 | bad_arg1.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 508 | }; |
| 509 | ASSERT_NE(0, applypatch_modes(args5.size(), args5.data())); |
| 510 | |
| 511 | // Target size cannot be zero. |
| 512 | TemporaryFile tmp6; |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 513 | 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] | 514 | std::vector<const char*> args6 = { |
| 515 | "applypatch", |
| 516 | boot_img.c_str(), |
| 517 | tmp6.path, |
| 518 | recovery_img_sha1.c_str(), |
| 519 | "0", // target size |
Tao Bao | 8dd44e9 | 2016-11-21 11:16:56 -0800 | [diff] [blame] | 520 | patch.c_str() |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 521 | }; |
| 522 | ASSERT_NE(0, applypatch_modes(args6.size(), args6.data())); |
| 523 | } |
| 524 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 525 | TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 526 | // Insufficient args. |
| 527 | ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" })); |
| 528 | } |
| 529 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 530 | TEST_F(ApplyPatchModesTest, ShowLicenses) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 531 | ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" })); |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 532 | } |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 533 | |
| 534 | TEST_F(FreeCacheTest, FreeCacheSmoke) { |
| 535 | std::vector<std::string> files = { "file1", "file2", "file3" }; |
| 536 | AddFilesToDir(mock_cache.path, files); |
| 537 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 538 | ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path)); |
| 539 | |
| 540 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) { |
| 541 | return this->MockFreeSpaceChecker(dir); |
| 542 | })); |
| 543 | |
| 544 | ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path)); |
| 545 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path)); |
| 546 | } |
| 547 | |
| 548 | TEST_F(FreeCacheTest, FreeCacheOpenFile) { |
| 549 | std::vector<std::string> files = { "file1", "file2" }; |
| 550 | AddFilesToDir(mock_cache.path, files); |
| 551 | ASSERT_EQ(files, FindFilesInDir(mock_cache.path)); |
| 552 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path)); |
| 553 | |
| 554 | std::string file1_path = mock_cache.path + "/file1"s; |
| 555 | android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY)); |
| 556 | |
| 557 | // file1 can't be deleted as it's opened by us. |
| 558 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) { |
| 559 | return this->MockFreeSpaceChecker(dir); |
| 560 | })); |
| 561 | |
| 562 | ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path)); |
| 563 | } |
| 564 | |
| 565 | TEST_F(FreeCacheTest, FreeCacheLogsSmoke) { |
| 566 | std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5", |
| 567 | "last_log.10" }; |
| 568 | AddFilesToDir(mock_log_dir.path, log_files); |
| 569 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 570 | |
| 571 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) { |
| 572 | return this->MockFreeSpaceChecker(dir); |
| 573 | })); |
| 574 | |
| 575 | // Logs with a higher index will be deleted first |
| 576 | std::vector<std::string> expected = { "last_log", "last_log.1" }; |
| 577 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 578 | ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path)); |
| 579 | } |
| 580 | |
| 581 | TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) { |
| 582 | std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number", |
| 583 | "last_kmsgrandom" }; |
| 584 | AddFilesToDir(mock_log_dir.path, log_files); |
| 585 | ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path)); |
| 586 | |
| 587 | ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) { |
| 588 | return this->MockFreeSpaceChecker(dir); |
| 589 | })); |
| 590 | |
| 591 | // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is |
| 592 | // deleted before last_log. |
| 593 | std::vector<std::string> expected = { "last_log.1" }; |
| 594 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 595 | ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path)); |
| 596 | } |
| 597 | |
| 598 | TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) { |
| 599 | std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log", |
| 600 | "last_kmsg.1" }; |
| 601 | AddFilesToDir(mock_log_dir.path, log_files); |
| 602 | ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path)); |
| 603 | |
| 604 | ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) { |
| 605 | return this->MockFreeSpaceChecker(dir); |
| 606 | })); |
| 607 | |
| 608 | // Non log files in /cache/recovery won't be deleted. |
| 609 | std::vector<std::string> expected = { "block.map", "command", "last_install" }; |
| 610 | ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path)); |
| 611 | } |