blob: 4e4430919d9dc68d3c3f6e2719bc88a87d005dd3 [file] [log] [blame]
Jed Estepb8a693b2016-03-09 17:51:34 -08001/*
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 Xud5fbcc12018-04-11 14:38:09 -070017#include <dirent.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080018#include <fcntl.h>
Tianjie Xud5fbcc12018-04-11 14:38:09 -070019#include <libgen.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080020#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 Xud5fbcc12018-04-11 14:38:09 -070027#include <algorithm>
Tao Baofada91c2016-10-27 18:16:06 -070028#include <memory>
Jed Estepb8a693b2016-03-09 17:51:34 -080029#include <string>
Tao Baofada91c2016-10-27 18:16:06 -070030#include <vector>
Jed Estepb8a693b2016-03-09 17:51:34 -080031
32#include <android-base/file.h>
Tianjie Xuffed57a2018-04-23 17:51:45 -070033#include <android-base/logging.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080034#include <android-base/stringprintf.h>
35#include <android-base/test_utils.h>
Tianjie Xud5fbcc12018-04-11 14:38:09 -070036#include <android-base/unique_fd.h>
Tao Baod612b232018-03-12 21:18:52 -070037#include <bsdiff/bsdiff.h>
Tao Bao641fa972018-04-25 18:59:40 -070038#include <gtest/gtest.h>
Tao Baofada91c2016-10-27 18:16:06 -070039#include <openssl/sha.h>
Tianjie Xu9e1ccd42018-04-25 17:19:20 -070040#include <zlib.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080041
42#include "applypatch/applypatch.h"
Tao Bao36c35112016-10-25 14:17:26 -070043#include "applypatch/applypatch_modes.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080044#include "common/test_constants.h"
Tao Bao641fa972018-04-25 18:59:40 -070045#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070046#include "otautil/print_sha1.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080047
Tao Baod612b232018-03-12 21:18:52 -070048using namespace std::string_literals;
49
Tianjie Xu9e1ccd42018-04-25 17:19:20 -070050// TODO(b/67849209) Remove after debug the flakiness.
51static 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 Bao36c35112016-10-25 14:17:26 -070091static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
Tianjie Xuffed57a2018-04-23 17:51:45 -070092 ASSERT_TRUE(sha1 != nullptr);
Tao Baofada91c2016-10-27 18:16:06 -070093
Tao Bao36c35112016-10-25 14:17:26 -070094 std::string data;
95 ASSERT_TRUE(android::base::ReadFileToString(fname, &data));
Jed Estepb8a693b2016-03-09 17:51:34 -080096
Tao Bao36c35112016-10-25 14:17:26 -070097 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 Estepb8a693b2016-03-09 17:51:34 -0800104}
105
106static void mangle_file(const std::string& fname) {
Tianjie Xua88cc542017-10-25 13:16:54 -0700107 std::string content(1024, '\0');
Tao Bao36c35112016-10-25 14:17:26 -0700108 for (size_t i = 0; i < 1024; i++) {
109 content[i] = rand() % 256;
110 }
111 ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
Jed Estepb8a693b2016-03-09 17:51:34 -0800112}
113
Tianjie Xuffed57a2018-04-23 17:51:45 -0700114static 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 Estepb8a693b2016-03-09 17:51:34 -0800122class ApplyPatchTest : public ::testing::Test {
Tao Bao36c35112016-10-25 14:17:26 -0700123 public:
Tianjie Xua88cc542017-10-25 13:16:54 -0700124 virtual void SetUp() override {
Tao Bao36c35112016-10-25 14:17:26 -0700125 // set up files
126 old_file = from_testdata_base("old.file");
127 new_file = from_testdata_base("new.file");
Tianjie Xua88cc542017-10-25 13:16:54 -0700128 nonexistent_file = from_testdata_base("nonexistent.file");
Jed Estepb8a693b2016-03-09 17:51:34 -0800129
Tao Bao36c35112016-10-25 14:17:26 -0700130 // set up SHA constants
Tao Bao8dd44e92016-11-21 11:16:56 -0800131 sha1sum(old_file, &old_sha1, &old_size);
132 sha1sum(new_file, &new_sha1, &new_size);
Tao Bao36c35112016-10-25 14:17:26 -0700133 srand(time(nullptr));
134 bad_sha1_a = android::base::StringPrintf("%040x", rand());
135 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao36c35112016-10-25 14:17:26 -0700136 }
Jed Estepb8a693b2016-03-09 17:51:34 -0800137
Tianjie Xua88cc542017-10-25 13:16:54 -0700138 std::string old_file;
139 std::string new_file;
140 std::string nonexistent_file;
Jed Estepb8a693b2016-03-09 17:51:34 -0800141
Tianjie Xua88cc542017-10-25 13:16:54 -0700142 std::string old_sha1;
143 std::string new_sha1;
144 std::string bad_sha1_a;
145 std::string bad_sha1_b;
Jed Estepb8a693b2016-03-09 17:51:34 -0800146
Tianjie Xua88cc542017-10-25 13:16:54 -0700147 size_t old_size;
148 size_t new_size;
Jed Estepb8a693b2016-03-09 17:51:34 -0800149};
150
Jed Estepb8a693b2016-03-09 17:51:34 -0800151class ApplyPatchCacheTest : public ApplyPatchTest {
Tianjie Xua88cc542017-10-25 13:16:54 -0700152 protected:
153 void SetUp() override {
154 ApplyPatchTest::SetUp();
Tao Bao641fa972018-04-25 18:59:40 -0700155 Paths::Get().set_cache_temp_source(old_file);
Tao Bao36c35112016-10-25 14:17:26 -0700156 }
Jed Estepb8a693b2016-03-09 17:51:34 -0800157};
158
Tianjie Xua88cc542017-10-25 13:16:54 -0700159class ApplyPatchModesTest : public ::testing::Test {
160 protected:
161 void SetUp() override {
Tao Bao641fa972018-04-25 18:59:40 -0700162 Paths::Get().set_cache_temp_source(cache_source.path);
Tianjie Xuffed57a2018-04-23 17:51:45 -0700163 android::base::InitLogging(nullptr, &test_logger);
164 android::base::SetMinimumLogSeverity(android::base::LogSeverity::DEBUG);
Tianjie Xua88cc542017-10-25 13:16:54 -0700165 }
166
167 TemporaryFile cache_source;
168};
Jed Estepb8a693b2016-03-09 17:51:34 -0800169
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700170class 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 Bao641fa972018-04-25 18:59:40 -0700202 Paths::Get().set_cache_log_directory(mock_log_dir.path);
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700203 }
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 Xua5fd5ab2016-10-18 15:18:22 -0700216TEST_F(ApplyPatchTest, CheckModeSkip) {
Tao Bao36c35112016-10-25 14:17:26 -0700217 std::vector<std::string> sha1s;
218 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700219}
220
Jed Estepb8a693b2016-03-09 17:51:34 -0800221TEST_F(ApplyPatchTest, CheckModeSingle) {
Tao Bao36c35112016-10-25 14:17:26 -0700222 std::vector<std::string> sha1s = { old_sha1 };
223 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800224}
225
226TEST_F(ApplyPatchTest, CheckModeMultiple) {
Tao Bao36c35112016-10-25 14:17:26 -0700227 std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
228 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800229}
230
231TEST_F(ApplyPatchTest, CheckModeFailure) {
Tao Bao36c35112016-10-25 14:17:26 -0700232 std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
233 ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800234}
235
Tao Bao8dd44e92016-11-21 11:16:56 -0800236TEST_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 Xua88cc542017-10-25 13:16:54 -0700272TEST_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 Estepb8a693b2016-03-09 17:51:34 -0800278}
279
Tianjie Xua88cc542017-10-25 13:16:54 -0700280TEST_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 Estepb8a693b2016-03-09 17:51:34 -0800286}
287
Tianjie Xua88cc542017-10-25 13:16:54 -0700288TEST_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 Estepb8a693b2016-03-09 17:51:34 -0800294}
295
Tianjie Xua88cc542017-10-25 13:16:54 -0700296TEST_F(ApplyPatchModesTest, InvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700297 // 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 Xua88cc542017-10-25 13:16:54 -0700304TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) {
Tao Bao8dd44e92016-11-21 11:16:56 -0800305 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 Baoe3499f92018-03-20 10:33:42 -0700311 size_t recovery_img_size;
Tao Bao8dd44e92016-11-21 11:16:56 -0800312 std::string recovery_img_sha1;
Tao Baoe3499f92018-03-20 10:33:42 -0700313 sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);
314 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
Tao Bao8dd44e92016-11-21 11:16:56 -0800315
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 Baoe3499f92018-03-20 10:33:42 -0700319 std::string src_file_arg =
Tao Bao8dd44e92016-11-21 11:16:56 -0800320 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
Tao Baoe3499f92018-03-20 10:33:42 -0700321 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 Bao8dd44e92016-11-21 11:16:56 -0800332 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
Tao Baoe3499f92018-03-20 10:33:42 -0700333}
334
335// Tests patching the EMMC target without a separate bonus file (i.e. recovery-from-boot patch has
336// everything).
337TEST_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 Bao8dd44e92016-11-21 11:16:56 -0800348
349 // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
Tao Baoe3499f92018-03-20 10:33:42 -0700350 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 Xu9e1ccd42018-04-25 17:19:20 -0700362
363 if (applypatch_modes(args.size(), args.data()) != 0) {
364 DecompressAndDumpRecoveryImage(tgt_file.path);
365 FAIL();
366 }
Tao Baoe3499f92018-03-20 10:33:42 -0700367}
368
369TEST_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 Bao8dd44e92016-11-21 11:16:56 -0800382
383 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \
Tao Baoe3499f92018-03-20 10:33:42 -0700384 // <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 Bao8dd44e92016-11-21 11:16:56 -0800389 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 Baoe3499f92018-03-20 10:33:42 -0700394 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 Bao4f834302018-04-19 12:35:14 -0700404 // 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 Xu9e1ccd42018-04-25 17:19:20 -0700409
410 if (applypatch_modes(args.size(), args.data()) != 0) {
411 DecompressAndDumpRecoveryImage(tgt_file.path);
412 FAIL();
413 }
Tao Bao8dd44e92016-11-21 11:16:56 -0800414}
415
Tao Baod612b232018-03-12 21:18:52 -0700416// Ensures that applypatch works with a bsdiff based recovery-from-boot.p.
417TEST_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 Xua88cc542017-10-25 13:16:54 -0700464TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700465 // 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 Bao8dd44e92016-11-21 11:16:56 -0800478 size_t size;
Tao Bao36c35112016-10-25 14:17:26 -0700479 std::string recovery_img_sha1;
Tao Bao8dd44e92016-11-21 11:16:56 -0800480 sha1sum(recovery_img, &recovery_img_sha1, &size);
481 std::string recovery_img_size = std::to_string(size);
Tao Bao36c35112016-10-25 14:17:26 -0700482
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 Bao8dd44e92016-11-21 11:16:56 -0800493 recovery_img_size.c_str()
494 };
Tao Bao36c35112016-10-25 14:17:26 -0700495 ASSERT_NE(0, applypatch_modes(args4.size(), args4.data()));
496
497 // Failed to parse patch args.
498 TemporaryFile tmp5;
Tao Bao8dd44e92016-11-21 11:16:56 -0800499 std::string bad_arg1 =
500 "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700501 std::vector<const char*> args5 = {
502 "applypatch",
503 boot_img.c_str(),
504 tmp5.path,
505 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800506 recovery_img_size.c_str(),
507 bad_arg1.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700508 };
509 ASSERT_NE(0, applypatch_modes(args5.size(), args5.data()));
510
511 // Target size cannot be zero.
512 TemporaryFile tmp6;
Tao Bao8dd44e92016-11-21 11:16:56 -0800513 std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700514 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 Bao8dd44e92016-11-21 11:16:56 -0800520 patch.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700521 };
522 ASSERT_NE(0, applypatch_modes(args6.size(), args6.data()));
523}
524
Tianjie Xua88cc542017-10-25 13:16:54 -0700525TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700526 // Insufficient args.
527 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" }));
528}
529
Tianjie Xua88cc542017-10-25 13:16:54 -0700530TEST_F(ApplyPatchModesTest, ShowLicenses) {
Tao Bao36c35112016-10-25 14:17:26 -0700531 ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" }));
Jed Estepb8a693b2016-03-09 17:51:34 -0800532}
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700533
534TEST_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
548TEST_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
565TEST_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
581TEST_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
598TEST_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}