blob: 4204074e7cbf2f9b9b0f69329d2e4839fad6891d [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>
35#include <android-base/test_utils.h>
36#include <android-base/unique_fd.h>
37#include <gtest/gtest.h>
Tao Baob8cb2e62018-07-06 12:14:36 -070038
39#include "applypatch/applypatch.h"
40#include "common/test_constants.h"
Tao Bao5609bc82018-06-20 00:30:48 -070041#include "edify/expr.h"
Tao Baob8cb2e62018-07-06 12:14:36 -070042#include "otautil/paths.h"
43#include "otautil/print_sha1.h"
44
45using namespace std::string_literals;
46
Tao Baob8cb2e62018-07-06 12:14:36 -070047class ApplyPatchTest : public ::testing::Test {
Tao Bao7c1d4262018-07-06 23:18:14 -070048 protected:
Tao Baob8cb2e62018-07-06 12:14:36 -070049 void SetUp() override {
Tao Baocdbe58a2018-08-20 08:57:19 -070050 source_file = from_testdata_base("boot.img");
51 FileContents boot_fc;
52 ASSERT_EQ(0, LoadFileContents(source_file, &boot_fc));
53 source_size = boot_fc.data.size();
54 source_sha1 = print_sha1(boot_fc.sha1);
Tao Baob8cb2e62018-07-06 12:14:36 -070055
Tao Baocdbe58a2018-08-20 08:57:19 -070056 target_file = from_testdata_base("recovery.img");
57 FileContents recovery_fc;
58 ASSERT_EQ(0, LoadFileContents(target_file, &recovery_fc));
59 target_size = recovery_fc.data.size();
60 target_sha1 = print_sha1(recovery_fc.sha1);
Tao Bao7c1d4262018-07-06 23:18:14 -070061
Tao Bao5609bc82018-06-20 00:30:48 -070062 source_partition = Partition(source_file, source_size, source_sha1);
63 target_partition = Partition(partition_file.path, target_size, target_sha1);
64
Tao Baob8cb2e62018-07-06 12:14:36 -070065 srand(time(nullptr));
66 bad_sha1_a = android::base::StringPrintf("%040x", rand());
67 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao7c1d4262018-07-06 23:18:14 -070068
69 // Reset the cache backup file.
Tao Bao5609bc82018-06-20 00:30:48 -070070 Paths::Get().set_cache_temp_source(cache_temp_source.path);
71 }
72
73 void TearDown() override {
74 ASSERT_TRUE(android::base::RemoveFileIfExists(cache_temp_source.path));
Tao Baob8cb2e62018-07-06 12:14:36 -070075 }
76
Tao Baocdbe58a2018-08-20 08:57:19 -070077 std::string source_file;
78 std::string source_sha1;
79 size_t source_size;
Tao Bao7c1d4262018-07-06 23:18:14 -070080
Tao Baocdbe58a2018-08-20 08:57:19 -070081 std::string target_file;
82 std::string target_sha1;
83 size_t target_size;
Tao Bao7c1d4262018-07-06 23:18:14 -070084
Tao Baob8cb2e62018-07-06 12:14:36 -070085 std::string bad_sha1_a;
86 std::string bad_sha1_b;
Tao Bao5609bc82018-06-20 00:30:48 -070087
88 Partition source_partition;
89 Partition target_partition;
90
91 private:
92 TemporaryFile partition_file;
93 TemporaryFile cache_temp_source;
Tao Baob8cb2e62018-07-06 12:14:36 -070094};
95
Tao Bao5609bc82018-06-20 00:30:48 -070096TEST_F(ApplyPatchTest, CheckPartition) {
97 ASSERT_TRUE(CheckPartition(source_partition));
Tao Baob8cb2e62018-07-06 12:14:36 -070098}
99
Tao Bao5609bc82018-06-20 00:30:48 -0700100TEST_F(ApplyPatchTest, CheckPartition_Mismatching) {
101 ASSERT_FALSE(CheckPartition(Partition(source_file, target_size, target_sha1)));
102 ASSERT_FALSE(CheckPartition(Partition(source_file, source_size, bad_sha1_a)));
103
104 ASSERT_FALSE(CheckPartition(Partition(source_file, source_size - 1, source_sha1)));
105 ASSERT_FALSE(CheckPartition(Partition(source_file, source_size + 1, source_sha1)));
Tao Baob8cb2e62018-07-06 12:14:36 -0700106}
107
Tao Bao5609bc82018-06-20 00:30:48 -0700108TEST_F(ApplyPatchTest, PatchPartitionCheck) {
109 ASSERT_TRUE(PatchPartitionCheck(target_partition, source_partition));
Tao Baob8cb2e62018-07-06 12:14:36 -0700110
Tao Bao5609bc82018-06-20 00:30:48 -0700111 ASSERT_TRUE(
112 PatchPartitionCheck(Partition(source_file, source_size - 1, source_sha1), source_partition));
Tao Baob8cb2e62018-07-06 12:14:36 -0700113
Tao Bao5609bc82018-06-20 00:30:48 -0700114 ASSERT_TRUE(
115 PatchPartitionCheck(Partition(source_file, source_size + 1, source_sha1), source_partition));
Tao Baob8cb2e62018-07-06 12:14:36 -0700116}
117
Tao Bao5609bc82018-06-20 00:30:48 -0700118TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup) {
119 ASSERT_FALSE(
120 PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
Tao Baob8cb2e62018-07-06 12:14:36 -0700121
Tao Baocdbe58a2018-08-20 08:57:19 -0700122 Paths::Get().set_cache_temp_source(source_file);
Tao Bao5609bc82018-06-20 00:30:48 -0700123 ASSERT_TRUE(
124 PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
Tao Baob8cb2e62018-07-06 12:14:36 -0700125}
126
Tao Bao5609bc82018-06-20 00:30:48 -0700127TEST_F(ApplyPatchTest, PatchPartitionCheck_UseBackup_BothCorrupted) {
128 ASSERT_FALSE(
129 PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
Tao Baob8cb2e62018-07-06 12:14:36 -0700130
Tao Bao5609bc82018-06-20 00:30:48 -0700131 Paths::Get().set_cache_temp_source(target_file);
132 ASSERT_FALSE(
133 PatchPartitionCheck(target_partition, Partition(target_file, source_size, source_sha1)));
134}
135
136TEST_F(ApplyPatchTest, PatchPartition) {
137 FileContents patch_fc;
138 ASSERT_EQ(0, LoadFileContents(from_testdata_base("recovery-from-boot.p"), &patch_fc));
139 Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend()));
140
141 FileContents bonus_fc;
142 ASSERT_EQ(0, LoadFileContents(from_testdata_base("bonus.file"), &bonus_fc));
143 Value bonus(Value::Type::BLOB, std::string(bonus_fc.data.cbegin(), bonus_fc.data.cend()));
144
145 ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, &bonus));
146}
147
148// Tests patching an eMMC target without a separate bonus file (i.e. recovery-from-boot patch has
149// everything).
150TEST_F(ApplyPatchTest, PatchPartitionWithoutBonusFile) {
151 FileContents patch_fc;
152 ASSERT_EQ(0, LoadFileContents(from_testdata_base("recovery-from-boot-with-bonus.p"), &patch_fc));
153 Value patch(Value::Type::BLOB, std::string(patch_fc.data.cbegin(), patch_fc.data.cend()));
154
155 ASSERT_TRUE(PatchPartition(target_partition, source_partition, patch, nullptr));
Tao Baob8cb2e62018-07-06 12:14:36 -0700156}
157
158class FreeCacheTest : public ::testing::Test {
159 protected:
160 static constexpr size_t PARTITION_SIZE = 4096 * 10;
161
162 // Returns a sorted list of files in |dirname|.
163 static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
164 std::vector<std::string> file_list;
165
166 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
167 struct dirent* de;
168 while ((de = readdir(d.get())) != 0) {
169 std::string path = dirname + "/" + de->d_name;
170
171 struct stat st;
172 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
173 file_list.emplace_back(de->d_name);
174 }
175 }
176
177 std::sort(file_list.begin(), file_list.end());
178 return file_list;
179 }
180
Tao Baob1c5b622018-07-12 11:49:05 -0700181 void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700182 std::string zeros(4096, 0);
183 for (const auto& file : files) {
Tao Baob1c5b622018-07-12 11:49:05 -0700184 temporary_files_.push_back(dir + "/" + file);
185 ASSERT_TRUE(android::base::WriteStringToFile(zeros, temporary_files_.back()));
Tao Baob8cb2e62018-07-06 12:14:36 -0700186 }
187 }
188
189 void SetUp() override {
190 Paths::Get().set_cache_log_directory(mock_log_dir.path);
Tao Baob1c5b622018-07-12 11:49:05 -0700191 temporary_files_.clear();
192 }
193
194 void TearDown() override {
195 for (const auto& file : temporary_files_) {
196 ASSERT_TRUE(android::base::RemoveFileIfExists(file));
197 }
Tao Baob8cb2e62018-07-06 12:14:36 -0700198 }
199
200 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
201 // bytes and all files are 4096 bytes in size.
Tao Baob1c5b622018-07-12 11:49:05 -0700202 static size_t MockFreeSpaceChecker(const std::string& dirname) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700203 std::vector<std::string> files = FindFilesInDir(dirname);
204 return PARTITION_SIZE - 4096 * files.size();
205 }
206
207 TemporaryDir mock_cache;
208 TemporaryDir mock_log_dir;
Tao Baob1c5b622018-07-12 11:49:05 -0700209
210 private:
211 std::vector<std::string> temporary_files_;
Tao Baob8cb2e62018-07-06 12:14:36 -0700212};
213
214TEST_F(FreeCacheTest, FreeCacheSmoke) {
215 std::vector<std::string> files = { "file1", "file2", "file3" };
216 AddFilesToDir(mock_cache.path, files);
217 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
218 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
219
Tao Baob1c5b622018-07-12 11:49:05 -0700220 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700221
222 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
223 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
224}
225
Tao Bao49750f12018-07-11 16:32:10 -0700226TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) {
227 std::vector<std::string> files{ "file1", "file2", "file3" };
228 AddFilesToDir(mock_cache.path, files);
229 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
230 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
231
232 ASSERT_FALSE(
233 RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; }));
234}
235
Tao Baob8cb2e62018-07-06 12:14:36 -0700236TEST_F(FreeCacheTest, FreeCacheOpenFile) {
237 std::vector<std::string> files = { "file1", "file2" };
238 AddFilesToDir(mock_cache.path, files);
239 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
240 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
241
242 std::string file1_path = mock_cache.path + "/file1"s;
243 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
244
245 // file1 can't be deleted as it's opened by us.
Tao Baob1c5b622018-07-12 11:49:05 -0700246 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700247
248 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
249}
250
251TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
252 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
253 "last_log.10" };
254 AddFilesToDir(mock_log_dir.path, log_files);
255 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
256
Tao Baob1c5b622018-07-12 11:49:05 -0700257 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700258
259 // Logs with a higher index will be deleted first
260 std::vector<std::string> expected = { "last_log", "last_log.1" };
261 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
262 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
263}
264
265TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
266 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
267 "last_kmsgrandom" };
268 AddFilesToDir(mock_log_dir.path, log_files);
269 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
270
Tao Baob1c5b622018-07-12 11:49:05 -0700271 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700272
273 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
274 // deleted before last_log.
275 std::vector<std::string> expected = { "last_log.1" };
276 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
277 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
278}
279
280TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
281 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
282 "last_kmsg.1" };
283 AddFilesToDir(mock_log_dir.path, log_files);
284 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
285
Tao Baob1c5b622018-07-12 11:49:05 -0700286 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700287
288 // Non log files in /cache/recovery won't be deleted.
289 std::vector<std::string> expected = { "block.map", "command", "last_install" };
290 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
291}