blob: aa0959bf516141bcd3b362f4c9a79e98c9280e43 [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() };
347 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
Tao Bao8dd44e92016-11-21 11:16:56 -0800348}
349
Tao Baod612b232018-03-12 21:18:52 -0700350// Ensures that applypatch works with a bsdiff based recovery-from-boot.p.
351TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) {
352 std::string boot_img_file = from_testdata_base("boot.img");
353 std::string boot_img_sha1;
354 size_t boot_img_size;
355 sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size);
356
357 std::string recovery_img_file = from_testdata_base("recovery.img");
358 std::string recovery_img_sha1;
359 size_t recovery_img_size;
360 sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size);
361
362 // Generate the bsdiff patch of recovery-from-boot.p.
363 std::string src_content;
364 ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content));
365
366 std::string tgt_content;
367 ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content));
368
369 TemporaryFile patch_file;
370 ASSERT_EQ(0,
371 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
372 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
373 patch_file.path, nullptr));
374
375 // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
376 std::string src_file_arg =
377 "EMMC:" + boot_img_file + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
378 TemporaryFile tgt_file;
379 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
380 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
381 std::string patch_arg = boot_img_sha1 + ":" + patch_file.path;
382 std::vector<const char*> args = { "applypatch",
383 src_file_arg.c_str(),
384 tgt_file_arg.c_str(),
385 recovery_img_sha1.c_str(),
386 recovery_img_size_arg.c_str(),
387 patch_arg.c_str() };
388 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
389
390 // Double check the patched recovery image.
391 std::string tgt_file_sha1;
392 size_t tgt_file_size;
393 sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size);
394 ASSERT_EQ(recovery_img_size, tgt_file_size);
395 ASSERT_EQ(recovery_img_sha1, tgt_file_sha1);
396}
397
Tianjie Xua88cc542017-10-25 13:16:54 -0700398TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700399 // Invalid bonus file.
400 ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" }));
401
402 std::string bonus_file = from_testdata_base("bonus.file");
403 // With bonus file, but missing args.
404 ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() }));
405
406 std::string boot_img = from_testdata_base("boot.img");
407 size_t boot_img_size;
408 std::string boot_img_sha1;
409 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
410
411 std::string recovery_img = from_testdata_base("recovery.img");
Tao Bao8dd44e92016-11-21 11:16:56 -0800412 size_t size;
Tao Bao36c35112016-10-25 14:17:26 -0700413 std::string recovery_img_sha1;
Tao Bao8dd44e92016-11-21 11:16:56 -0800414 sha1sum(recovery_img, &recovery_img_sha1, &size);
415 std::string recovery_img_size = std::to_string(size);
Tao Bao36c35112016-10-25 14:17:26 -0700416
417 // Bonus file is not supported in flash mode.
418 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size>
419 TemporaryFile tmp4;
420 std::vector<const char*> args4 = {
421 "applypatch",
422 "-b",
423 bonus_file.c_str(),
424 boot_img.c_str(),
425 tmp4.path,
426 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800427 recovery_img_size.c_str()
428 };
Tao Bao36c35112016-10-25 14:17:26 -0700429 ASSERT_NE(0, applypatch_modes(args4.size(), args4.data()));
430
431 // Failed to parse patch args.
432 TemporaryFile tmp5;
Tao Bao8dd44e92016-11-21 11:16:56 -0800433 std::string bad_arg1 =
434 "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700435 std::vector<const char*> args5 = {
436 "applypatch",
437 boot_img.c_str(),
438 tmp5.path,
439 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800440 recovery_img_size.c_str(),
441 bad_arg1.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700442 };
443 ASSERT_NE(0, applypatch_modes(args5.size(), args5.data()));
444
445 // Target size cannot be zero.
446 TemporaryFile tmp6;
Tao Bao8dd44e92016-11-21 11:16:56 -0800447 std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700448 std::vector<const char*> args6 = {
449 "applypatch",
450 boot_img.c_str(),
451 tmp6.path,
452 recovery_img_sha1.c_str(),
453 "0", // target size
Tao Bao8dd44e92016-11-21 11:16:56 -0800454 patch.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700455 };
456 ASSERT_NE(0, applypatch_modes(args6.size(), args6.data()));
457}
458
Tianjie Xua88cc542017-10-25 13:16:54 -0700459TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700460 // Insufficient args.
461 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" }));
462}
463
Tianjie Xua88cc542017-10-25 13:16:54 -0700464TEST_F(ApplyPatchModesTest, ShowLicenses) {
Tao Bao36c35112016-10-25 14:17:26 -0700465 ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" }));
Jed Estepb8a693b2016-03-09 17:51:34 -0800466}
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700467
468TEST_F(FreeCacheTest, FreeCacheSmoke) {
469 std::vector<std::string> files = { "file1", "file2", "file3" };
470 AddFilesToDir(mock_cache.path, files);
471 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
472 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
473
474 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) {
475 return this->MockFreeSpaceChecker(dir);
476 }));
477
478 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
479 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
480}
481
482TEST_F(FreeCacheTest, FreeCacheOpenFile) {
483 std::vector<std::string> files = { "file1", "file2" };
484 AddFilesToDir(mock_cache.path, files);
485 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
486 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
487
488 std::string file1_path = mock_cache.path + "/file1"s;
489 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
490
491 // file1 can't be deleted as it's opened by us.
492 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) {
493 return this->MockFreeSpaceChecker(dir);
494 }));
495
496 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
497}
498
499TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
500 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
501 "last_log.10" };
502 AddFilesToDir(mock_log_dir.path, log_files);
503 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
504
505 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
506 return this->MockFreeSpaceChecker(dir);
507 }));
508
509 // Logs with a higher index will be deleted first
510 std::vector<std::string> expected = { "last_log", "last_log.1" };
511 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
512 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
513}
514
515TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
516 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
517 "last_kmsgrandom" };
518 AddFilesToDir(mock_log_dir.path, log_files);
519 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
520
521 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) {
522 return this->MockFreeSpaceChecker(dir);
523 }));
524
525 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
526 // deleted before last_log.
527 std::vector<std::string> expected = { "last_log.1" };
528 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
529 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
530}
531
532TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
533 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
534 "last_kmsg.1" };
535 AddFilesToDir(mock_log_dir.path, log_files);
536 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
537
538 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
539 return this->MockFreeSpaceChecker(dir);
540 }));
541
542 // Non log files in /cache/recovery won't be deleted.
543 std::vector<std::string> expected = { "block.map", "command", "last_install" };
544 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
545}