blob: f19f28c603cf09f4badbd8456ddbac9eae58cb9c [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>
19#include <gtest/gtest.h>
Tianjie Xud5fbcc12018-04-11 14:38:09 -070020#include <libgen.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080021#include <stdio.h>
22#include <stdlib.h>
23#include <sys/stat.h>
24#include <sys/statvfs.h>
25#include <sys/types.h>
26#include <time.h>
27
Tianjie Xud5fbcc12018-04-11 14:38:09 -070028#include <algorithm>
Tao Baofada91c2016-10-27 18:16:06 -070029#include <memory>
Jed Estepb8a693b2016-03-09 17:51:34 -080030#include <string>
Tao Baofada91c2016-10-27 18:16:06 -070031#include <vector>
Jed Estepb8a693b2016-03-09 17:51:34 -080032
33#include <android-base/file.h>
34#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 Baofada91c2016-10-27 18:16:06 -070038#include <openssl/sha.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080039
40#include "applypatch/applypatch.h"
Tao Bao36c35112016-10-25 14:17:26 -070041#include "applypatch/applypatch_modes.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080042#include "common/test_constants.h"
Tianjie Xu3bbb20f2018-02-27 15:56:11 -080043#include "otautil/cache_location.h"
Tao Bao09e468f2017-09-29 14:39:33 -070044#include "otautil/print_sha1.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080045
Tao Baod612b232018-03-12 21:18:52 -070046using namespace std::string_literals;
47
Tao Bao36c35112016-10-25 14:17:26 -070048static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
49 ASSERT_NE(nullptr, sha1);
Tao Baofada91c2016-10-27 18:16:06 -070050
Tao Bao36c35112016-10-25 14:17:26 -070051 std::string data;
52 ASSERT_TRUE(android::base::ReadFileToString(fname, &data));
Jed Estepb8a693b2016-03-09 17:51:34 -080053
Tao Bao36c35112016-10-25 14:17:26 -070054 if (fsize != nullptr) {
55 *fsize = data.size();
56 }
57
58 uint8_t digest[SHA_DIGEST_LENGTH];
59 SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
60 *sha1 = print_sha1(digest);
Jed Estepb8a693b2016-03-09 17:51:34 -080061}
62
63static void mangle_file(const std::string& fname) {
Tianjie Xua88cc542017-10-25 13:16:54 -070064 std::string content(1024, '\0');
Tao Bao36c35112016-10-25 14:17:26 -070065 for (size_t i = 0; i < 1024; i++) {
66 content[i] = rand() % 256;
67 }
68 ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
Jed Estepb8a693b2016-03-09 17:51:34 -080069}
70
Jed Estepb8a693b2016-03-09 17:51:34 -080071class ApplyPatchTest : public ::testing::Test {
Tao Bao36c35112016-10-25 14:17:26 -070072 public:
Tianjie Xua88cc542017-10-25 13:16:54 -070073 virtual void SetUp() override {
Tao Bao36c35112016-10-25 14:17:26 -070074 // set up files
75 old_file = from_testdata_base("old.file");
76 new_file = from_testdata_base("new.file");
Tianjie Xua88cc542017-10-25 13:16:54 -070077 nonexistent_file = from_testdata_base("nonexistent.file");
Jed Estepb8a693b2016-03-09 17:51:34 -080078
Tao Bao36c35112016-10-25 14:17:26 -070079 // set up SHA constants
Tao Bao8dd44e92016-11-21 11:16:56 -080080 sha1sum(old_file, &old_sha1, &old_size);
81 sha1sum(new_file, &new_sha1, &new_size);
Tao Bao36c35112016-10-25 14:17:26 -070082 srand(time(nullptr));
83 bad_sha1_a = android::base::StringPrintf("%040x", rand());
84 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao36c35112016-10-25 14:17:26 -070085 }
Jed Estepb8a693b2016-03-09 17:51:34 -080086
Tianjie Xua88cc542017-10-25 13:16:54 -070087 std::string old_file;
88 std::string new_file;
89 std::string nonexistent_file;
Jed Estepb8a693b2016-03-09 17:51:34 -080090
Tianjie Xua88cc542017-10-25 13:16:54 -070091 std::string old_sha1;
92 std::string new_sha1;
93 std::string bad_sha1_a;
94 std::string bad_sha1_b;
Jed Estepb8a693b2016-03-09 17:51:34 -080095
Tianjie Xua88cc542017-10-25 13:16:54 -070096 size_t old_size;
97 size_t new_size;
Jed Estepb8a693b2016-03-09 17:51:34 -080098};
99
Jed Estepb8a693b2016-03-09 17:51:34 -0800100class ApplyPatchCacheTest : public ApplyPatchTest {
Tianjie Xua88cc542017-10-25 13:16:54 -0700101 protected:
102 void SetUp() override {
103 ApplyPatchTest::SetUp();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800104 CacheLocation::location().set_cache_temp_source(old_file);
Tao Bao36c35112016-10-25 14:17:26 -0700105 }
Jed Estepb8a693b2016-03-09 17:51:34 -0800106};
107
Tianjie Xua88cc542017-10-25 13:16:54 -0700108class ApplyPatchModesTest : public ::testing::Test {
109 protected:
110 void SetUp() override {
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800111 CacheLocation::location().set_cache_temp_source(cache_source.path);
Tianjie Xua88cc542017-10-25 13:16:54 -0700112 }
113
114 TemporaryFile cache_source;
115};
Jed Estepb8a693b2016-03-09 17:51:34 -0800116
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700117class FreeCacheTest : public ::testing::Test {
118 protected:
119 static constexpr size_t PARTITION_SIZE = 4096 * 10;
120
121 // Returns a sorted list of files in |dirname|.
122 static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
123 std::vector<std::string> file_list;
124
125 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
126 struct dirent* de;
127 while ((de = readdir(d.get())) != 0) {
128 std::string path = dirname + "/" + de->d_name;
129
130 struct stat st;
131 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
132 file_list.emplace_back(de->d_name);
133 }
134 }
135
136 std::sort(file_list.begin(), file_list.end());
137 return file_list;
138 }
139
140 static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
141 std::string zeros(4096, 0);
142 for (const auto& file : files) {
143 std::string path = dir + "/" + file;
144 ASSERT_TRUE(android::base::WriteStringToFile(zeros, path));
145 }
146 }
147
148 void SetUp() override {
149 CacheLocation::location().set_cache_log_directory(mock_log_dir.path);
150 }
151
152 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
153 // bytes and all files are 4096 bytes in size.
154 size_t MockFreeSpaceChecker(const std::string& dirname) {
155 std::vector<std::string> files = FindFilesInDir(dirname);
156 return PARTITION_SIZE - 4096 * files.size();
157 }
158
159 TemporaryDir mock_cache;
160 TemporaryDir mock_log_dir;
161};
162
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700163TEST_F(ApplyPatchTest, CheckModeSkip) {
Tao Bao36c35112016-10-25 14:17:26 -0700164 std::vector<std::string> sha1s;
165 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700166}
167
Jed Estepb8a693b2016-03-09 17:51:34 -0800168TEST_F(ApplyPatchTest, CheckModeSingle) {
Tao Bao36c35112016-10-25 14:17:26 -0700169 std::vector<std::string> sha1s = { old_sha1 };
170 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800171}
172
173TEST_F(ApplyPatchTest, CheckModeMultiple) {
Tao Bao36c35112016-10-25 14:17:26 -0700174 std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
175 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800176}
177
178TEST_F(ApplyPatchTest, CheckModeFailure) {
Tao Bao36c35112016-10-25 14:17:26 -0700179 std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
180 ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800181}
182
Tao Bao8dd44e92016-11-21 11:16:56 -0800183TEST_F(ApplyPatchTest, CheckModeEmmcTarget) {
184 // EMMC:old_file:size:sha1 should pass the check.
185 std::string src_file =
186 "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
187 std::vector<std::string> sha1s;
188 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
189
190 // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
191 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
192 std::to_string(old_size + 1) + ":" + old_sha1;
193 ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s));
194
195 // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
196 src_file = "EMMC:" + old_file + ":" +
197 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
198 std::to_string(old_size) + ":" + old_sha1 + ":" +
199 std::to_string(old_size + 1) + ":" + old_sha1;
200 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
201
202 // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
203 src_file = "EMMC:" + old_file + ":" +
204 std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
205 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
206 std::to_string(old_size) + ":" + old_sha1;
207 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
208
209 // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
210 // should pass the check.
211 src_file = "EMMC:" + new_file + ":" +
212 std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
213 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
214 std::to_string(old_size) + ":" + old_sha1 + ":" +
215 std::to_string(new_size) + ":" + new_sha1;
216 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
217}
218
Tianjie Xua88cc542017-10-25 13:16:54 -0700219TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) {
220 TemporaryFile temp_file;
221 mangle_file(temp_file.path);
222 std::vector<std::string> sha1s_single = { old_sha1 };
223 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single));
224 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single));
Jed Estepb8a693b2016-03-09 17:51:34 -0800225}
226
Tianjie Xua88cc542017-10-25 13:16:54 -0700227TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) {
228 TemporaryFile temp_file;
229 mangle_file(temp_file.path);
230 std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b };
231 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple));
232 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple));
Jed Estepb8a693b2016-03-09 17:51:34 -0800233}
234
Tianjie Xua88cc542017-10-25 13:16:54 -0700235TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) {
236 TemporaryFile temp_file;
237 mangle_file(temp_file.path);
238 std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b };
239 ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure));
240 ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure));
Jed Estepb8a693b2016-03-09 17:51:34 -0800241}
242
Tianjie Xua88cc542017-10-25 13:16:54 -0700243TEST_F(ApplyPatchModesTest, InvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700244 // At least two args (including the filename).
245 ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" }));
246
247 // Unrecognized args.
248 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" }));
249}
250
Tianjie Xua88cc542017-10-25 13:16:54 -0700251TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) {
Tao Bao8dd44e92016-11-21 11:16:56 -0800252 std::string boot_img = from_testdata_base("boot.img");
253 size_t boot_img_size;
254 std::string boot_img_sha1;
255 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
256
257 std::string recovery_img = from_testdata_base("recovery.img");
Tao Baoe3499f92018-03-20 10:33:42 -0700258 size_t recovery_img_size;
Tao Bao8dd44e92016-11-21 11:16:56 -0800259 std::string recovery_img_sha1;
Tao Baoe3499f92018-03-20 10:33:42 -0700260 sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);
261 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
Tao Bao8dd44e92016-11-21 11:16:56 -0800262
263 std::string bonus_file = from_testdata_base("bonus.file");
264
265 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
Tao Baoe3499f92018-03-20 10:33:42 -0700266 std::string src_file_arg =
Tao Bao8dd44e92016-11-21 11:16:56 -0800267 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
Tao Baoe3499f92018-03-20 10:33:42 -0700268 TemporaryFile tgt_file;
269 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
270 std::string patch_arg = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p");
271 std::vector<const char*> args = { "applypatch",
272 "-b",
273 bonus_file.c_str(),
274 src_file_arg.c_str(),
275 tgt_file_arg.c_str(),
276 recovery_img_sha1.c_str(),
277 recovery_img_size_arg.c_str(),
278 patch_arg.c_str() };
Tao Bao8dd44e92016-11-21 11:16:56 -0800279 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
Tao Baoe3499f92018-03-20 10:33:42 -0700280}
281
282// Tests patching the EMMC target without a separate bonus file (i.e. recovery-from-boot patch has
283// everything).
284TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithoutBonusFile) {
285 std::string boot_img = from_testdata_base("boot.img");
286 size_t boot_img_size;
287 std::string boot_img_sha1;
288 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
289
290 std::string recovery_img = from_testdata_base("recovery.img");
291 size_t recovery_img_size;
292 std::string recovery_img_sha1;
293 sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);
294 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
Tao Bao8dd44e92016-11-21 11:16:56 -0800295
296 // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
Tao Baoe3499f92018-03-20 10:33:42 -0700297 std::string src_file_arg =
298 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
299 TemporaryFile tgt_file;
300 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
301 std::string patch_arg =
302 boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
303 std::vector<const char*> args = { "applypatch",
304 src_file_arg.c_str(),
305 tgt_file_arg.c_str(),
306 recovery_img_sha1.c_str(),
307 recovery_img_size_arg.c_str(),
308 patch_arg.c_str() };
309 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
310}
311
312TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithMultiplePatches) {
313 std::string boot_img = from_testdata_base("boot.img");
314 size_t boot_img_size;
315 std::string boot_img_sha1;
316 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
317
318 std::string recovery_img = from_testdata_base("recovery.img");
319 size_t recovery_img_size;
320 std::string recovery_img_sha1;
321 sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);
322 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
323
324 std::string bonus_file = from_testdata_base("bonus.file");
Tao Bao8dd44e92016-11-21 11:16:56 -0800325
326 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \
Tao Baoe3499f92018-03-20 10:33:42 -0700327 // <src-sha1-fake1>:<patch1> <src-sha1>:<patch2> <src-sha1-fake2>:<patch3>
328 std::string src_file_arg =
329 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
330 TemporaryFile tgt_file;
331 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
Tao Bao8dd44e92016-11-21 11:16:56 -0800332 std::string bad_sha1_a = android::base::StringPrintf("%040x", rand());
333 std::string bad_sha1_b = android::base::StringPrintf("%040x", rand());
334 std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p");
335 std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p");
336 std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p");
Tao Baoe3499f92018-03-20 10:33:42 -0700337 std::vector<const char*> args = { "applypatch",
338 "-b",
339 bonus_file.c_str(),
340 src_file_arg.c_str(),
341 tgt_file_arg.c_str(),
342 recovery_img_sha1.c_str(),
343 recovery_img_size_arg.c_str(),
344 patch1.c_str(),
345 patch2.c_str(),
346 patch3.c_str() };
Tao Bao4f834302018-04-19 12:35:14 -0700347 // TODO(b/67849209): Remove after addressing the flakiness.
348 printf("Calling applypatch_modes with the following args:\n");
349 for (const auto& arg : args) {
350 printf(" %s\n", arg);
351 }
Tao Baoe3499f92018-03-20 10:33:42 -0700352 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
Tao Bao8dd44e92016-11-21 11:16:56 -0800353}
354
Tao Baod612b232018-03-12 21:18:52 -0700355// Ensures that applypatch works with a bsdiff based recovery-from-boot.p.
356TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) {
357 std::string boot_img_file = from_testdata_base("boot.img");
358 std::string boot_img_sha1;
359 size_t boot_img_size;
360 sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size);
361
362 std::string recovery_img_file = from_testdata_base("recovery.img");
363 std::string recovery_img_sha1;
364 size_t recovery_img_size;
365 sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size);
366
367 // Generate the bsdiff patch of recovery-from-boot.p.
368 std::string src_content;
369 ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content));
370
371 std::string tgt_content;
372 ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content));
373
374 TemporaryFile patch_file;
375 ASSERT_EQ(0,
376 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
377 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
378 patch_file.path, nullptr));
379
380 // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
381 std::string src_file_arg =
382 "EMMC:" + boot_img_file + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
383 TemporaryFile tgt_file;
384 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
385 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
386 std::string patch_arg = boot_img_sha1 + ":" + patch_file.path;
387 std::vector<const char*> args = { "applypatch",
388 src_file_arg.c_str(),
389 tgt_file_arg.c_str(),
390 recovery_img_sha1.c_str(),
391 recovery_img_size_arg.c_str(),
392 patch_arg.c_str() };
393 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
394
395 // Double check the patched recovery image.
396 std::string tgt_file_sha1;
397 size_t tgt_file_size;
398 sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size);
399 ASSERT_EQ(recovery_img_size, tgt_file_size);
400 ASSERT_EQ(recovery_img_sha1, tgt_file_sha1);
401}
402
Tianjie Xua88cc542017-10-25 13:16:54 -0700403TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700404 // Invalid bonus file.
405 ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" }));
406
407 std::string bonus_file = from_testdata_base("bonus.file");
408 // With bonus file, but missing args.
409 ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() }));
410
411 std::string boot_img = from_testdata_base("boot.img");
412 size_t boot_img_size;
413 std::string boot_img_sha1;
414 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
415
416 std::string recovery_img = from_testdata_base("recovery.img");
Tao Bao8dd44e92016-11-21 11:16:56 -0800417 size_t size;
Tao Bao36c35112016-10-25 14:17:26 -0700418 std::string recovery_img_sha1;
Tao Bao8dd44e92016-11-21 11:16:56 -0800419 sha1sum(recovery_img, &recovery_img_sha1, &size);
420 std::string recovery_img_size = std::to_string(size);
Tao Bao36c35112016-10-25 14:17:26 -0700421
422 // Bonus file is not supported in flash mode.
423 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size>
424 TemporaryFile tmp4;
425 std::vector<const char*> args4 = {
426 "applypatch",
427 "-b",
428 bonus_file.c_str(),
429 boot_img.c_str(),
430 tmp4.path,
431 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800432 recovery_img_size.c_str()
433 };
Tao Bao36c35112016-10-25 14:17:26 -0700434 ASSERT_NE(0, applypatch_modes(args4.size(), args4.data()));
435
436 // Failed to parse patch args.
437 TemporaryFile tmp5;
Tao Bao8dd44e92016-11-21 11:16:56 -0800438 std::string bad_arg1 =
439 "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700440 std::vector<const char*> args5 = {
441 "applypatch",
442 boot_img.c_str(),
443 tmp5.path,
444 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800445 recovery_img_size.c_str(),
446 bad_arg1.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700447 };
448 ASSERT_NE(0, applypatch_modes(args5.size(), args5.data()));
449
450 // Target size cannot be zero.
451 TemporaryFile tmp6;
Tao Bao8dd44e92016-11-21 11:16:56 -0800452 std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700453 std::vector<const char*> args6 = {
454 "applypatch",
455 boot_img.c_str(),
456 tmp6.path,
457 recovery_img_sha1.c_str(),
458 "0", // target size
Tao Bao8dd44e92016-11-21 11:16:56 -0800459 patch.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700460 };
461 ASSERT_NE(0, applypatch_modes(args6.size(), args6.data()));
462}
463
Tianjie Xua88cc542017-10-25 13:16:54 -0700464TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700465 // Insufficient args.
466 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" }));
467}
468
Tianjie Xua88cc542017-10-25 13:16:54 -0700469TEST_F(ApplyPatchModesTest, ShowLicenses) {
Tao Bao36c35112016-10-25 14:17:26 -0700470 ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" }));
Jed Estepb8a693b2016-03-09 17:51:34 -0800471}
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700472
473TEST_F(FreeCacheTest, FreeCacheSmoke) {
474 std::vector<std::string> files = { "file1", "file2", "file3" };
475 AddFilesToDir(mock_cache.path, files);
476 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
477 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
478
479 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) {
480 return this->MockFreeSpaceChecker(dir);
481 }));
482
483 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
484 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
485}
486
487TEST_F(FreeCacheTest, FreeCacheOpenFile) {
488 std::vector<std::string> files = { "file1", "file2" };
489 AddFilesToDir(mock_cache.path, files);
490 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
491 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
492
493 std::string file1_path = mock_cache.path + "/file1"s;
494 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
495
496 // file1 can't be deleted as it's opened by us.
497 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) {
498 return this->MockFreeSpaceChecker(dir);
499 }));
500
501 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
502}
503
504TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
505 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
506 "last_log.10" };
507 AddFilesToDir(mock_log_dir.path, log_files);
508 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
509
510 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
511 return this->MockFreeSpaceChecker(dir);
512 }));
513
514 // Logs with a higher index will be deleted first
515 std::vector<std::string> expected = { "last_log", "last_log.1" };
516 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
517 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
518}
519
520TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
521 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
522 "last_kmsgrandom" };
523 AddFilesToDir(mock_log_dir.path, log_files);
524 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
525
526 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) {
527 return this->MockFreeSpaceChecker(dir);
528 }));
529
530 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
531 // deleted before last_log.
532 std::vector<std::string> expected = { "last_log.1" };
533 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
534 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
535}
536
537TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
538 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
539 "last_kmsg.1" };
540 AddFilesToDir(mock_log_dir.path, log_files);
541 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
542
543 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
544 return this->MockFreeSpaceChecker(dir);
545 }));
546
547 // Non log files in /cache/recovery won't be deleted.
548 std::vector<std::string> expected = { "block.map", "command", "last_install" };
549 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
550}