blob: 218a224f8f99fc18b0a43d19982827d0e7bf5075 [file] [log] [blame]
Tao Baob8cb2e62018-07-06 12:14:36 -07001/*
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
17#include <dirent.h>
18#include <fcntl.h>
Tao Bao49750f12018-07-11 16:32:10 -070019#include <inttypes.h>
Tao Baob8cb2e62018-07-06 12:14:36 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <time.h>
25#include <unistd.h>
26
27#include <algorithm>
28#include <memory>
29#include <string>
30#include <vector>
31
32#include <android-base/file.h>
Tao Bao49750f12018-07-11 16:32:10 -070033#include <android-base/logging.h>
Tao Baob8cb2e62018-07-06 12:14:36 -070034#include <android-base/stringprintf.h>
Tao Baob8cb2e62018-07-06 12:14:36 -070035#include <android-base/unique_fd.h>
36#include <gtest/gtest.h>
Tao Baob8cb2e62018-07-06 12:14:36 -070037
38#include "applypatch/applypatch.h"
39#include "common/test_constants.h"
Tao Bao5609bc82018-06-20 00:30:48 -070040#include "edify/expr.h"
Tao Baob8cb2e62018-07-06 12:14:36 -070041#include "otautil/paths.h"
42#include "otautil/print_sha1.h"
43
44using namespace std::string_literals;
45
Tao Baob8cb2e62018-07-06 12:14:36 -070046class ApplyPatchTest : public ::testing::Test {
Tao Bao7c1d4262018-07-06 23:18:14 -070047 protected:
Tao Baob8cb2e62018-07-06 12:14:36 -070048 void SetUp() override {
Tao Baocdbe58a2018-08-20 08:57:19 -070049 source_file = from_testdata_base("boot.img");
50 FileContents boot_fc;
Tao Bao09e84932018-08-31 11:25:05 -070051 ASSERT_TRUE(LoadFileContents(source_file, &boot_fc));
Tao Baocdbe58a2018-08-20 08:57:19 -070052 source_size = boot_fc.data.size();
53 source_sha1 = print_sha1(boot_fc.sha1);
Tao Baob8cb2e62018-07-06 12:14:36 -070054
Tao Baocdbe58a2018-08-20 08:57:19 -070055 target_file = from_testdata_base("recovery.img");
56 FileContents recovery_fc;
Tao Bao09e84932018-08-31 11:25:05 -070057 ASSERT_TRUE(LoadFileContents(target_file, &recovery_fc));
Tao Baocdbe58a2018-08-20 08:57:19 -070058 target_size = recovery_fc.data.size();
59 target_sha1 = print_sha1(recovery_fc.sha1);
Tao Bao7c1d4262018-07-06 23:18:14 -070060
Tao Bao5609bc82018-06-20 00:30:48 -070061 source_partition = Partition(source_file, source_size, source_sha1);
62 target_partition = Partition(partition_file.path, target_size, target_sha1);
63
Tao Baob8cb2e62018-07-06 12:14:36 -070064 srand(time(nullptr));
65 bad_sha1_a = android::base::StringPrintf("%040x", rand());
66 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao7c1d4262018-07-06 23:18:14 -070067
68 // Reset the cache backup file.
Tao Bao5609bc82018-06-20 00:30:48 -070069 Paths::Get().set_cache_temp_source(cache_temp_source.path);
70 }
71
72 void TearDown() override {
73 ASSERT_TRUE(android::base::RemoveFileIfExists(cache_temp_source.path));
Tao Baob8cb2e62018-07-06 12:14:36 -070074 }
75
Tao Baocdbe58a2018-08-20 08:57:19 -070076 std::string source_file;
77 std::string source_sha1;
78 size_t source_size;
Tao Bao7c1d4262018-07-06 23:18:14 -070079
Tao Baocdbe58a2018-08-20 08:57:19 -070080 std::string target_file;
81 std::string target_sha1;
82 size_t target_size;
Tao Bao7c1d4262018-07-06 23:18:14 -070083
Tao Baob8cb2e62018-07-06 12:14:36 -070084 std::string bad_sha1_a;
85 std::string bad_sha1_b;
Tao Bao5609bc82018-06-20 00:30:48 -070086
87 Partition source_partition;
88 Partition target_partition;
89
90 private:
91 TemporaryFile partition_file;
92 TemporaryFile cache_temp_source;
Tao Baob8cb2e62018-07-06 12:14:36 -070093};
94
Tao Bao5609bc82018-06-20 00:30:48 -070095TEST_F(ApplyPatchTest, CheckPartition) {
96 ASSERT_TRUE(CheckPartition(source_partition));
Tao Baob8cb2e62018-07-06 12:14:36 -070097}
98
Tao Bao5609bc82018-06-20 00:30:48 -070099TEST_F(ApplyPatchTest, CheckPartition_Mismatching) {
100 ASSERT_FALSE(CheckPartition(Partition(source_file, target_size, target_sha1)));
101 ASSERT_FALSE(CheckPartition(Partition(source_file, source_size, bad_sha1_a)));
102
103 ASSERT_FALSE(CheckPartition(Partition(source_file, source_size - 1, source_sha1)));
104 ASSERT_FALSE(CheckPartition(Partition(source_file, source_size + 1, source_sha1)));
Tao Baob8cb2e62018-07-06 12:14:36 -0700105}
106
Tao Bao5609bc82018-06-20 00:30:48 -0700107TEST_F(ApplyPatchTest, PatchPartitionCheck) {
108 ASSERT_TRUE(PatchPartitionCheck(target_partition, source_partition));
Tao Baob8cb2e62018-07-06 12:14:36 -0700109
Tao Bao5609bc82018-06-20 00:30:48 -0700110 ASSERT_TRUE(
111 PatchPartitionCheck(Partition(source_file, source_size - 1, source_sha1), source_partition));
Tao Baob8cb2e62018-07-06 12:14:36 -0700112
Tao Bao5609bc82018-06-20 00:30:48 -0700113 ASSERT_TRUE(
114 PatchPartitionCheck(Partition(source_file, source_size + 1, source_sha1), source_partition));
Tao Baob8cb2e62018-07-06 12:14:36 -0700115}
116
Tao Bao5609bc82018-06-20 00:30:48 -0700117TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup) {
118 ASSERT_FALSE(
119 PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
Tao Baob8cb2e62018-07-06 12:14:36 -0700120
Tao Baocdbe58a2018-08-20 08:57:19 -0700121 Paths::Get().set_cache_temp_source(source_file);
Tao Bao5609bc82018-06-20 00:30:48 -0700122 ASSERT_TRUE(
123 PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
Tao Baob8cb2e62018-07-06 12:14:36 -0700124}
125
Tao Bao5609bc82018-06-20 00:30:48 -0700126TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup_BothCorrupted) {
127 ASSERT_FALSE(
128 PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
Tao Baob8cb2e62018-07-06 12:14:36 -0700129
Tao Bao5609bc82018-06-20 00:30:48 -0700130 Paths::Get().set_cache_temp_source(target_file);
131 ASSERT_FALSE(
132 PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
133}
134
Kelvin Zhang07ba4482021-01-12 16:48:12 -0500135TEST_F(ApplyPatchTest, PatchPartition) {
Tao Bao5609bc82018-06-20 00:30:48 -0700136 FileContents patch_fc;
Tao Bao09e84932018-08-31 11:25:05 -0700137 ASSERT_TRUE(LoadFileContents(from_testdata_base("recovery-from-boot.p"), &patch_fc));
Tao Bao5609bc82018-06-20 00:30:48 -0700138 Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend()));
139
140 FileContents bonus_fc;
Tao Bao09e84932018-08-31 11:25:05 -0700141 ASSERT_TRUE(LoadFileContents(from_testdata_base("bonus.file"), &bonus_fc));
Tao Bao5609bc82018-06-20 00:30:48 -0700142 Value bonus(Value::Type::BLOB, std::string(bonus_fc.data.cbegin(), bonus_fc.data.cend()));
143
Tao Bao5234ad42019-09-23 10:28:54 -0700144 ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, &bonus, false));
Tao Bao5609bc82018-06-20 00:30:48 -0700145}
146
147// Tests patching an eMMC target without a separate bonus file (i.e. recovery-from-boot patch has
148// everything).
Kelvin Zhang07ba4482021-01-12 16:48:12 -0500149TEST_F(ApplyPatchTest, PatchPartitionWithoutBonusFile) {
Tao Bao5609bc82018-06-20 00:30:48 -0700150 FileContents patch_fc;
Tao Bao09e84932018-08-31 11:25:05 -0700151 ASSERT_TRUE(LoadFileContents(from_testdata_base("recovery-from-boot-with-bonus.p"), &patch_fc));
Tao Bao5609bc82018-06-20 00:30:48 -0700152 Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend()));
153
Tao Bao5234ad42019-09-23 10:28:54 -0700154 ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, nullptr, false));
Tao Baob8cb2e62018-07-06 12:14:36 -0700155}
156
157class FreeCacheTest : public ::testing::Test {
158 protected:
159 static constexpr size_t PARTITION_SIZE = 4096 * 10;
160
161 // Returns a sorted list of files in |dirname|.
162 static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
163 std::vector<std::string> file_list;
164
165 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
166 struct dirent* de;
167 while ((de = readdir(d.get())) != 0) {
168 std::string path = dirname + "/" + de->d_name;
169
170 struct stat st;
171 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
172 file_list.emplace_back(de->d_name);
173 }
174 }
175
176 std::sort(file_list.begin(), file_list.end());
177 return file_list;
178 }
179
Tao Baob1c5b622018-07-12 11:49:05 -0700180 void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700181 std::string zeros(4096, 0);
182 for (const auto& file : files) {
Tao Baob1c5b622018-07-12 11:49:05 -0700183 temporary_files_.push_back(dir + "/" + file);
184 ASSERT_TRUE(android::base::WriteStringToFile(zeros, temporary_files_.back()));
Tao Baob8cb2e62018-07-06 12:14:36 -0700185 }
186 }
187
188 void SetUp() override {
189 Paths::Get().set_cache_log_directory(mock_log_dir.path);
Tao Baob1c5b622018-07-12 11:49:05 -0700190 temporary_files_.clear();
191 }
192
193 void TearDown() override {
194 for (const auto& file : temporary_files_) {
195 ASSERT_TRUE(android::base::RemoveFileIfExists(file));
196 }
Tao Baob8cb2e62018-07-06 12:14:36 -0700197 }
198
199 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
200 // bytes and all files are 4096 bytes in size.
Tao Baob1c5b622018-07-12 11:49:05 -0700201 static size_t MockFreeSpaceChecker(const std::string& dirname) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700202 std::vector<std::string> files = FindFilesInDir(dirname);
203 return PARTITION_SIZE - 4096 * files.size();
204 }
205
206 TemporaryDir mock_cache;
207 TemporaryDir mock_log_dir;
Tao Baob1c5b622018-07-12 11:49:05 -0700208
209 private:
210 std::vector<std::string> temporary_files_;
Tao Baob8cb2e62018-07-06 12:14:36 -0700211};
212
213TEST_F(FreeCacheTest, FreeCacheSmoke) {
214 std::vector<std::string> files = { "file1", "file2", "file3" };
215 AddFilesToDir(mock_cache.path, files);
216 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
217 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
218
Tao Baob1c5b622018-07-12 11:49:05 -0700219 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700220
221 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
222 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
223}
224
Tao Bao49750f12018-07-11 16:32:10 -0700225TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) {
226 std::vector<std::string> files{ "file1", "file2", "file3" };
227 AddFilesToDir(mock_cache.path, files);
228 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
229 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
230
231 ASSERT_FALSE(
232 RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; }));
233}
234
Tao Baob8cb2e62018-07-06 12:14:36 -0700235TEST_F(FreeCacheTest, FreeCacheOpenFile) {
236 std::vector<std::string> files = { "file1", "file2" };
237 AddFilesToDir(mock_cache.path, files);
238 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
239 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
240
241 std::string file1_path = mock_cache.path + "/file1"s;
242 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
243
244 // file1 can't be deleted as it's opened by us.
Tao Baob1c5b622018-07-12 11:49:05 -0700245 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700246
247 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
248}
249
250TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
251 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
252 "last_log.10" };
253 AddFilesToDir(mock_log_dir.path, log_files);
254 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
255
Tao Baob1c5b622018-07-12 11:49:05 -0700256 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700257
258 // Logs with a higher index will be deleted first
259 std::vector<std::string> expected = { "last_log", "last_log.1" };
260 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
261 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
262}
263
264TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
265 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
266 "last_kmsgrandom" };
267 AddFilesToDir(mock_log_dir.path, log_files);
268 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
269
Tao Baob1c5b622018-07-12 11:49:05 -0700270 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700271
272 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
273 // deleted before last_log.
274 std::vector<std::string> expected = { "last_log.1" };
275 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
276 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
277}
278
279TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
280 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
281 "last_kmsg.1" };
282 AddFilesToDir(mock_log_dir.path, log_files);
283 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
284
Tao Baob1c5b622018-07-12 11:49:05 -0700285 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700286
287 // Non log files in /cache/recovery won't be deleted.
288 std::vector<std::string> expected = { "block.map", "command", "last_install" };
289 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
290}