blob: 292d76e43b5a3b80cd8c13da92576b24d510c56c [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>
Tianjie Xuffed57a2018-04-23 17:51:45 -070034#include <android-base/logging.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080035#include <android-base/stringprintf.h>
36#include <android-base/test_utils.h>
Tianjie Xud5fbcc12018-04-11 14:38:09 -070037#include <android-base/unique_fd.h>
Tao Baod612b232018-03-12 21:18:52 -070038#include <bsdiff/bsdiff.h>
Tao Baofada91c2016-10-27 18:16:06 -070039#include <openssl/sha.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080040
41#include "applypatch/applypatch.h"
Tao Bao36c35112016-10-25 14:17:26 -070042#include "applypatch/applypatch_modes.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080043#include "common/test_constants.h"
Tianjie Xu3bbb20f2018-02-27 15:56:11 -080044#include "otautil/cache_location.h"
Tao Bao09e468f2017-09-29 14:39:33 -070045#include "otautil/print_sha1.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080046
Tao Baod612b232018-03-12 21:18:52 -070047using namespace std::string_literals;
48
Tao Bao36c35112016-10-25 14:17:26 -070049static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
Tianjie Xuffed57a2018-04-23 17:51:45 -070050 ASSERT_TRUE(sha1 != nullptr);
Tao Baofada91c2016-10-27 18:16:06 -070051
Tao Bao36c35112016-10-25 14:17:26 -070052 std::string data;
53 ASSERT_TRUE(android::base::ReadFileToString(fname, &data));
Jed Estepb8a693b2016-03-09 17:51:34 -080054
Tao Bao36c35112016-10-25 14:17:26 -070055 if (fsize != nullptr) {
56 *fsize = data.size();
57 }
58
59 uint8_t digest[SHA_DIGEST_LENGTH];
60 SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
61 *sha1 = print_sha1(digest);
Jed Estepb8a693b2016-03-09 17:51:34 -080062}
63
64static void mangle_file(const std::string& fname) {
Tianjie Xua88cc542017-10-25 13:16:54 -070065 std::string content(1024, '\0');
Tao Bao36c35112016-10-25 14:17:26 -070066 for (size_t i = 0; i < 1024; i++) {
67 content[i] = rand() % 256;
68 }
69 ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
Jed Estepb8a693b2016-03-09 17:51:34 -080070}
71
Tianjie Xuffed57a2018-04-23 17:51:45 -070072static void test_logger(android::base::LogId /* id */, android::base::LogSeverity severity,
73 const char* /* tag */, const char* /* file */, unsigned int /* line */,
74 const char* message) {
75 if (severity >= android::base::GetMinimumLogSeverity()) {
76 fprintf(stdout, "%s\n", message);
77 }
78}
79
Jed Estepb8a693b2016-03-09 17:51:34 -080080class ApplyPatchTest : public ::testing::Test {
Tao Bao36c35112016-10-25 14:17:26 -070081 public:
Tianjie Xua88cc542017-10-25 13:16:54 -070082 virtual void SetUp() override {
Tao Bao36c35112016-10-25 14:17:26 -070083 // set up files
84 old_file = from_testdata_base("old.file");
85 new_file = from_testdata_base("new.file");
Tianjie Xua88cc542017-10-25 13:16:54 -070086 nonexistent_file = from_testdata_base("nonexistent.file");
Jed Estepb8a693b2016-03-09 17:51:34 -080087
Tao Bao36c35112016-10-25 14:17:26 -070088 // set up SHA constants
Tao Bao8dd44e92016-11-21 11:16:56 -080089 sha1sum(old_file, &old_sha1, &old_size);
90 sha1sum(new_file, &new_sha1, &new_size);
Tao Bao36c35112016-10-25 14:17:26 -070091 srand(time(nullptr));
92 bad_sha1_a = android::base::StringPrintf("%040x", rand());
93 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao36c35112016-10-25 14:17:26 -070094 }
Jed Estepb8a693b2016-03-09 17:51:34 -080095
Tianjie Xua88cc542017-10-25 13:16:54 -070096 std::string old_file;
97 std::string new_file;
98 std::string nonexistent_file;
Jed Estepb8a693b2016-03-09 17:51:34 -080099
Tianjie Xua88cc542017-10-25 13:16:54 -0700100 std::string old_sha1;
101 std::string new_sha1;
102 std::string bad_sha1_a;
103 std::string bad_sha1_b;
Jed Estepb8a693b2016-03-09 17:51:34 -0800104
Tianjie Xua88cc542017-10-25 13:16:54 -0700105 size_t old_size;
106 size_t new_size;
Jed Estepb8a693b2016-03-09 17:51:34 -0800107};
108
Jed Estepb8a693b2016-03-09 17:51:34 -0800109class ApplyPatchCacheTest : public ApplyPatchTest {
Tianjie Xua88cc542017-10-25 13:16:54 -0700110 protected:
111 void SetUp() override {
112 ApplyPatchTest::SetUp();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800113 CacheLocation::location().set_cache_temp_source(old_file);
Tao Bao36c35112016-10-25 14:17:26 -0700114 }
Jed Estepb8a693b2016-03-09 17:51:34 -0800115};
116
Tianjie Xua88cc542017-10-25 13:16:54 -0700117class ApplyPatchModesTest : public ::testing::Test {
118 protected:
119 void SetUp() override {
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800120 CacheLocation::location().set_cache_temp_source(cache_source.path);
Tianjie Xuffed57a2018-04-23 17:51:45 -0700121 android::base::InitLogging(nullptr, &test_logger);
122 android::base::SetMinimumLogSeverity(android::base::LogSeverity::DEBUG);
Tianjie Xua88cc542017-10-25 13:16:54 -0700123 }
124
125 TemporaryFile cache_source;
126};
Jed Estepb8a693b2016-03-09 17:51:34 -0800127
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700128class FreeCacheTest : public ::testing::Test {
129 protected:
130 static constexpr size_t PARTITION_SIZE = 4096 * 10;
131
132 // Returns a sorted list of files in |dirname|.
133 static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
134 std::vector<std::string> file_list;
135
136 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
137 struct dirent* de;
138 while ((de = readdir(d.get())) != 0) {
139 std::string path = dirname + "/" + de->d_name;
140
141 struct stat st;
142 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
143 file_list.emplace_back(de->d_name);
144 }
145 }
146
147 std::sort(file_list.begin(), file_list.end());
148 return file_list;
149 }
150
151 static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
152 std::string zeros(4096, 0);
153 for (const auto& file : files) {
154 std::string path = dir + "/" + file;
155 ASSERT_TRUE(android::base::WriteStringToFile(zeros, path));
156 }
157 }
158
159 void SetUp() override {
160 CacheLocation::location().set_cache_log_directory(mock_log_dir.path);
161 }
162
163 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
164 // bytes and all files are 4096 bytes in size.
165 size_t MockFreeSpaceChecker(const std::string& dirname) {
166 std::vector<std::string> files = FindFilesInDir(dirname);
167 return PARTITION_SIZE - 4096 * files.size();
168 }
169
170 TemporaryDir mock_cache;
171 TemporaryDir mock_log_dir;
172};
173
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700174TEST_F(ApplyPatchTest, CheckModeSkip) {
Tao Bao36c35112016-10-25 14:17:26 -0700175 std::vector<std::string> sha1s;
176 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700177}
178
Jed Estepb8a693b2016-03-09 17:51:34 -0800179TEST_F(ApplyPatchTest, CheckModeSingle) {
Tao Bao36c35112016-10-25 14:17:26 -0700180 std::vector<std::string> sha1s = { old_sha1 };
181 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800182}
183
184TEST_F(ApplyPatchTest, CheckModeMultiple) {
Tao Bao36c35112016-10-25 14:17:26 -0700185 std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
186 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800187}
188
189TEST_F(ApplyPatchTest, CheckModeFailure) {
Tao Bao36c35112016-10-25 14:17:26 -0700190 std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
191 ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800192}
193
Tao Bao8dd44e92016-11-21 11:16:56 -0800194TEST_F(ApplyPatchTest, CheckModeEmmcTarget) {
195 // EMMC:old_file:size:sha1 should pass the check.
196 std::string src_file =
197 "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
198 std::vector<std::string> sha1s;
199 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
200
201 // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
202 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
203 std::to_string(old_size + 1) + ":" + old_sha1;
204 ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s));
205
206 // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
207 src_file = "EMMC:" + old_file + ":" +
208 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
209 std::to_string(old_size) + ":" + old_sha1 + ":" +
210 std::to_string(old_size + 1) + ":" + old_sha1;
211 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
212
213 // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
214 src_file = "EMMC:" + old_file + ":" +
215 std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
216 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
217 std::to_string(old_size) + ":" + old_sha1;
218 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
219
220 // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
221 // should pass the check.
222 src_file = "EMMC:" + new_file + ":" +
223 std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
224 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
225 std::to_string(old_size) + ":" + old_sha1 + ":" +
226 std::to_string(new_size) + ":" + new_sha1;
227 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
228}
229
Tianjie Xua88cc542017-10-25 13:16:54 -0700230TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) {
231 TemporaryFile temp_file;
232 mangle_file(temp_file.path);
233 std::vector<std::string> sha1s_single = { old_sha1 };
234 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single));
235 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single));
Jed Estepb8a693b2016-03-09 17:51:34 -0800236}
237
Tianjie Xua88cc542017-10-25 13:16:54 -0700238TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) {
239 TemporaryFile temp_file;
240 mangle_file(temp_file.path);
241 std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b };
242 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple));
243 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple));
Jed Estepb8a693b2016-03-09 17:51:34 -0800244}
245
Tianjie Xua88cc542017-10-25 13:16:54 -0700246TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) {
247 TemporaryFile temp_file;
248 mangle_file(temp_file.path);
249 std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b };
250 ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure));
251 ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure));
Jed Estepb8a693b2016-03-09 17:51:34 -0800252}
253
Tianjie Xua88cc542017-10-25 13:16:54 -0700254TEST_F(ApplyPatchModesTest, InvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700255 // At least two args (including the filename).
256 ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" }));
257
258 // Unrecognized args.
259 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" }));
260}
261
Tianjie Xua88cc542017-10-25 13:16:54 -0700262TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) {
Tao Bao8dd44e92016-11-21 11:16:56 -0800263 std::string boot_img = from_testdata_base("boot.img");
264 size_t boot_img_size;
265 std::string boot_img_sha1;
266 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
267
268 std::string recovery_img = from_testdata_base("recovery.img");
Tao Baoe3499f92018-03-20 10:33:42 -0700269 size_t recovery_img_size;
Tao Bao8dd44e92016-11-21 11:16:56 -0800270 std::string recovery_img_sha1;
Tao Baoe3499f92018-03-20 10:33:42 -0700271 sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);
272 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
Tao Bao8dd44e92016-11-21 11:16:56 -0800273
274 std::string bonus_file = from_testdata_base("bonus.file");
275
276 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
Tao Baoe3499f92018-03-20 10:33:42 -0700277 std::string src_file_arg =
Tao Bao8dd44e92016-11-21 11:16:56 -0800278 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
Tao Baoe3499f92018-03-20 10:33:42 -0700279 TemporaryFile tgt_file;
280 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
281 std::string patch_arg = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p");
282 std::vector<const char*> args = { "applypatch",
283 "-b",
284 bonus_file.c_str(),
285 src_file_arg.c_str(),
286 tgt_file_arg.c_str(),
287 recovery_img_sha1.c_str(),
288 recovery_img_size_arg.c_str(),
289 patch_arg.c_str() };
Tao Bao8dd44e92016-11-21 11:16:56 -0800290 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
Tao Baoe3499f92018-03-20 10:33:42 -0700291}
292
293// Tests patching the EMMC target without a separate bonus file (i.e. recovery-from-boot patch has
294// everything).
295TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithoutBonusFile) {
296 std::string boot_img = from_testdata_base("boot.img");
297 size_t boot_img_size;
298 std::string boot_img_sha1;
299 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
300
301 std::string recovery_img = from_testdata_base("recovery.img");
302 size_t recovery_img_size;
303 std::string recovery_img_sha1;
304 sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);
305 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
Tao Bao8dd44e92016-11-21 11:16:56 -0800306
307 // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
Tao Baoe3499f92018-03-20 10:33:42 -0700308 std::string src_file_arg =
309 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
310 TemporaryFile tgt_file;
311 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
312 std::string patch_arg =
313 boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
314 std::vector<const char*> args = { "applypatch",
315 src_file_arg.c_str(),
316 tgt_file_arg.c_str(),
317 recovery_img_sha1.c_str(),
318 recovery_img_size_arg.c_str(),
319 patch_arg.c_str() };
320 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
321}
322
323TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithMultiplePatches) {
324 std::string boot_img = from_testdata_base("boot.img");
325 size_t boot_img_size;
326 std::string boot_img_sha1;
327 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
328
329 std::string recovery_img = from_testdata_base("recovery.img");
330 size_t recovery_img_size;
331 std::string recovery_img_sha1;
332 sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);
333 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
334
335 std::string bonus_file = from_testdata_base("bonus.file");
Tao Bao8dd44e92016-11-21 11:16:56 -0800336
337 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \
Tao Baoe3499f92018-03-20 10:33:42 -0700338 // <src-sha1-fake1>:<patch1> <src-sha1>:<patch2> <src-sha1-fake2>:<patch3>
339 std::string src_file_arg =
340 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
341 TemporaryFile tgt_file;
342 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
Tao Bao8dd44e92016-11-21 11:16:56 -0800343 std::string bad_sha1_a = android::base::StringPrintf("%040x", rand());
344 std::string bad_sha1_b = android::base::StringPrintf("%040x", rand());
345 std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p");
346 std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p");
347 std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p");
Tao Baoe3499f92018-03-20 10:33:42 -0700348 std::vector<const char*> args = { "applypatch",
349 "-b",
350 bonus_file.c_str(),
351 src_file_arg.c_str(),
352 tgt_file_arg.c_str(),
353 recovery_img_sha1.c_str(),
354 recovery_img_size_arg.c_str(),
355 patch1.c_str(),
356 patch2.c_str(),
357 patch3.c_str() };
Tao Bao4f834302018-04-19 12:35:14 -0700358 // TODO(b/67849209): Remove after addressing the flakiness.
359 printf("Calling applypatch_modes with the following args:\n");
360 for (const auto& arg : args) {
361 printf(" %s\n", arg);
362 }
Tao Baoe3499f92018-03-20 10:33:42 -0700363 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
Tao Bao8dd44e92016-11-21 11:16:56 -0800364}
365
Tao Baod612b232018-03-12 21:18:52 -0700366// Ensures that applypatch works with a bsdiff based recovery-from-boot.p.
367TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) {
368 std::string boot_img_file = from_testdata_base("boot.img");
369 std::string boot_img_sha1;
370 size_t boot_img_size;
371 sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size);
372
373 std::string recovery_img_file = from_testdata_base("recovery.img");
374 std::string recovery_img_sha1;
375 size_t recovery_img_size;
376 sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size);
377
378 // Generate the bsdiff patch of recovery-from-boot.p.
379 std::string src_content;
380 ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content));
381
382 std::string tgt_content;
383 ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content));
384
385 TemporaryFile patch_file;
386 ASSERT_EQ(0,
387 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
388 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
389 patch_file.path, nullptr));
390
391 // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
392 std::string src_file_arg =
393 "EMMC:" + boot_img_file + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
394 TemporaryFile tgt_file;
395 std::string tgt_file_arg = "EMMC:"s + tgt_file.path;
396 std::string recovery_img_size_arg = std::to_string(recovery_img_size);
397 std::string patch_arg = boot_img_sha1 + ":" + patch_file.path;
398 std::vector<const char*> args = { "applypatch",
399 src_file_arg.c_str(),
400 tgt_file_arg.c_str(),
401 recovery_img_sha1.c_str(),
402 recovery_img_size_arg.c_str(),
403 patch_arg.c_str() };
404 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
405
406 // Double check the patched recovery image.
407 std::string tgt_file_sha1;
408 size_t tgt_file_size;
409 sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size);
410 ASSERT_EQ(recovery_img_size, tgt_file_size);
411 ASSERT_EQ(recovery_img_sha1, tgt_file_sha1);
412}
413
Tianjie Xua88cc542017-10-25 13:16:54 -0700414TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700415 // Invalid bonus file.
416 ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" }));
417
418 std::string bonus_file = from_testdata_base("bonus.file");
419 // With bonus file, but missing args.
420 ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() }));
421
422 std::string boot_img = from_testdata_base("boot.img");
423 size_t boot_img_size;
424 std::string boot_img_sha1;
425 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
426
427 std::string recovery_img = from_testdata_base("recovery.img");
Tao Bao8dd44e92016-11-21 11:16:56 -0800428 size_t size;
Tao Bao36c35112016-10-25 14:17:26 -0700429 std::string recovery_img_sha1;
Tao Bao8dd44e92016-11-21 11:16:56 -0800430 sha1sum(recovery_img, &recovery_img_sha1, &size);
431 std::string recovery_img_size = std::to_string(size);
Tao Bao36c35112016-10-25 14:17:26 -0700432
433 // Bonus file is not supported in flash mode.
434 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size>
435 TemporaryFile tmp4;
436 std::vector<const char*> args4 = {
437 "applypatch",
438 "-b",
439 bonus_file.c_str(),
440 boot_img.c_str(),
441 tmp4.path,
442 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800443 recovery_img_size.c_str()
444 };
Tao Bao36c35112016-10-25 14:17:26 -0700445 ASSERT_NE(0, applypatch_modes(args4.size(), args4.data()));
446
447 // Failed to parse patch args.
448 TemporaryFile tmp5;
Tao Bao8dd44e92016-11-21 11:16:56 -0800449 std::string bad_arg1 =
450 "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700451 std::vector<const char*> args5 = {
452 "applypatch",
453 boot_img.c_str(),
454 tmp5.path,
455 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800456 recovery_img_size.c_str(),
457 bad_arg1.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700458 };
459 ASSERT_NE(0, applypatch_modes(args5.size(), args5.data()));
460
461 // Target size cannot be zero.
462 TemporaryFile tmp6;
Tao Bao8dd44e92016-11-21 11:16:56 -0800463 std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700464 std::vector<const char*> args6 = {
465 "applypatch",
466 boot_img.c_str(),
467 tmp6.path,
468 recovery_img_sha1.c_str(),
469 "0", // target size
Tao Bao8dd44e92016-11-21 11:16:56 -0800470 patch.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700471 };
472 ASSERT_NE(0, applypatch_modes(args6.size(), args6.data()));
473}
474
Tianjie Xua88cc542017-10-25 13:16:54 -0700475TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700476 // Insufficient args.
477 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" }));
478}
479
Tianjie Xua88cc542017-10-25 13:16:54 -0700480TEST_F(ApplyPatchModesTest, ShowLicenses) {
Tao Bao36c35112016-10-25 14:17:26 -0700481 ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" }));
Jed Estepb8a693b2016-03-09 17:51:34 -0800482}
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700483
484TEST_F(FreeCacheTest, FreeCacheSmoke) {
485 std::vector<std::string> files = { "file1", "file2", "file3" };
486 AddFilesToDir(mock_cache.path, files);
487 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
488 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
489
490 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) {
491 return this->MockFreeSpaceChecker(dir);
492 }));
493
494 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
495 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
496}
497
498TEST_F(FreeCacheTest, FreeCacheOpenFile) {
499 std::vector<std::string> files = { "file1", "file2" };
500 AddFilesToDir(mock_cache.path, files);
501 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
502 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
503
504 std::string file1_path = mock_cache.path + "/file1"s;
505 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
506
507 // file1 can't be deleted as it's opened by us.
508 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) {
509 return this->MockFreeSpaceChecker(dir);
510 }));
511
512 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
513}
514
515TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
516 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
517 "last_log.10" };
518 AddFilesToDir(mock_log_dir.path, log_files);
519 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
520
521 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
522 return this->MockFreeSpaceChecker(dir);
523 }));
524
525 // Logs with a higher index will be deleted first
526 std::vector<std::string> expected = { "last_log", "last_log.1" };
527 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
528 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
529}
530
531TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
532 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
533 "last_kmsgrandom" };
534 AddFilesToDir(mock_log_dir.path, log_files);
535 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
536
537 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) {
538 return this->MockFreeSpaceChecker(dir);
539 }));
540
541 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
542 // deleted before last_log.
543 std::vector<std::string> expected = { "last_log.1" };
544 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
545 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
546}
547
548TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
549 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
550 "last_kmsg.1" };
551 AddFilesToDir(mock_log_dir.path, log_files);
552 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
553
554 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
555 return this->MockFreeSpaceChecker(dir);
556 }));
557
558 // Non log files in /cache/recovery won't be deleted.
559 std::vector<std::string> expected = { "block.map", "command", "last_install" };
560 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
561}