blob: fe00e1e66a02bd8e9c3abd1aae689801fb20674e [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"
41#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;
51 ASSERT_EQ(0, LoadFileContents(source_file, &boot_fc));
52 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;
57 ASSERT_EQ(0, LoadFileContents(target_file, &recovery_fc));
58 target_size = recovery_fc.data.size();
59 target_sha1 = print_sha1(recovery_fc.sha1);
Tao Bao7c1d4262018-07-06 23:18:14 -070060
Tao Baob8cb2e62018-07-06 12:14:36 -070061 srand(time(nullptr));
62 bad_sha1_a = android::base::StringPrintf("%040x", rand());
63 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao7c1d4262018-07-06 23:18:14 -070064
65 // Reset the cache backup file.
66 Paths::Get().set_cache_temp_source("/cache/saved.file");
Tao Baob8cb2e62018-07-06 12:14:36 -070067 }
68
Tao Baocdbe58a2018-08-20 08:57:19 -070069 std::string source_file;
70 std::string source_sha1;
71 size_t source_size;
Tao Bao7c1d4262018-07-06 23:18:14 -070072
Tao Baocdbe58a2018-08-20 08:57:19 -070073 std::string target_file;
74 std::string target_sha1;
75 size_t target_size;
Tao Bao7c1d4262018-07-06 23:18:14 -070076
Tao Baob8cb2e62018-07-06 12:14:36 -070077 std::string bad_sha1_a;
78 std::string bad_sha1_b;
Tao Baob8cb2e62018-07-06 12:14:36 -070079};
80
Tao Bao7c1d4262018-07-06 23:18:14 -070081TEST_F(ApplyPatchTest, CheckMode) {
Tao Baocdbe58a2018-08-20 08:57:19 -070082 std::string partition =
83 "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + source_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -070084 ASSERT_EQ(0, applypatch_check(partition, {}));
Tao Baocdbe58a2018-08-20 08:57:19 -070085 ASSERT_EQ(0, applypatch_check(partition, { source_sha1 }));
Tao Bao7c1d4262018-07-06 23:18:14 -070086 ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, bad_sha1_b }));
Tao Baocdbe58a2018-08-20 08:57:19 -070087 ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, source_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -070088}
89
Tao Bao7c1d4262018-07-06 23:18:14 -070090TEST_F(ApplyPatchTest, CheckMode_NonEmmcTarget) {
Tao Baocdbe58a2018-08-20 08:57:19 -070091 ASSERT_NE(0, applypatch_check(source_file, {}));
92 ASSERT_NE(0, applypatch_check(source_file, { source_sha1 }));
93 ASSERT_NE(0, applypatch_check(source_file, { bad_sha1_a, bad_sha1_b }));
94 ASSERT_NE(0, applypatch_check(source_file, { bad_sha1_a, source_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -070095}
96
Tao Bao7c1d4262018-07-06 23:18:14 -070097TEST_F(ApplyPatchTest, CheckMode_EmmcTarget) {
Tao Baocdbe58a2018-08-20 08:57:19 -070098 // EMMC:source_file:size:sha1 should pass the check.
99 std::string src_file =
100 "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + source_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700101 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700102
Tao Baocdbe58a2018-08-20 08:57:19 -0700103 // EMMC:source_file:(size-1):sha1:(size+1):sha1 should fail the check.
104 src_file = "EMMC:" + source_file + ":" + std::to_string(source_size - 1) + ":" + source_sha1 +
105 ":" + std::to_string(source_size + 1) + ":" + source_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700106 ASSERT_NE(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700107
Tao Baocdbe58a2018-08-20 08:57:19 -0700108 // EMMC:source_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
109 src_file = "EMMC:" + source_file + ":" + std::to_string(source_size - 1) + ":" + source_sha1 +
110 ":" + std::to_string(source_size) + ":" + source_sha1 + ":" +
111 std::to_string(source_size + 1) + ":" + source_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700112 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700113
Tao Baocdbe58a2018-08-20 08:57:19 -0700114 // EMMC:source_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
115 src_file = "EMMC:" + source_file + ":" + std::to_string(source_size + 1) + ":" + source_sha1 +
116 ":" + std::to_string(source_size - 1) + ":" + source_sha1 + ":" +
117 std::to_string(source_size) + ":" + source_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700118 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700119
Tao Baocdbe58a2018-08-20 08:57:19 -0700120 // EMMC:target_file:(size+1):source_sha1:(size-1):source_sha1:size:source_sha1:size:target_sha1
Tao Baob8cb2e62018-07-06 12:14:36 -0700121 // should pass the check.
Tao Baocdbe58a2018-08-20 08:57:19 -0700122 src_file = "EMMC:" + target_file + ":" + std::to_string(source_size + 1) + ":" + source_sha1 +
123 ":" + std::to_string(source_size - 1) + ":" + source_sha1 + ":" +
124 std::to_string(source_size) + ":" + source_sha1 + ":" + std::to_string(target_size) +
125 ":" + target_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700126 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700127}
128
Tao Bao7c1d4262018-07-06 23:18:14 -0700129TEST_F(ApplyPatchTest, CheckMode_UseBackup) {
Tao Baocdbe58a2018-08-20 08:57:19 -0700130 std::string corrupted =
131 "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + bad_sha1_a;
132 ASSERT_NE(0, applypatch_check(corrupted, { source_sha1 }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700133
Tao Baocdbe58a2018-08-20 08:57:19 -0700134 Paths::Get().set_cache_temp_source(source_file);
135 ASSERT_EQ(0, applypatch_check(corrupted, { source_sha1 }));
136 ASSERT_EQ(0, applypatch_check(corrupted, { bad_sha1_a, source_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700137}
138
Tao Bao7c1d4262018-07-06 23:18:14 -0700139TEST_F(ApplyPatchTest, CheckMode_UseBackup_BothCorrupted) {
Tao Baocdbe58a2018-08-20 08:57:19 -0700140 std::string corrupted =
141 "EMMC:" + source_file + ":" + std::to_string(source_size) + ":" + bad_sha1_a;
Tao Bao7c1d4262018-07-06 23:18:14 -0700142 ASSERT_NE(0, applypatch_check(corrupted, {}));
Tao Baocdbe58a2018-08-20 08:57:19 -0700143 ASSERT_NE(0, applypatch_check(corrupted, { source_sha1 }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700144
Tao Baocdbe58a2018-08-20 08:57:19 -0700145 Paths::Get().set_cache_temp_source(source_file);
Tao Bao7c1d4262018-07-06 23:18:14 -0700146 ASSERT_NE(0, applypatch_check(corrupted, { bad_sha1_a, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700147}
148
149class FreeCacheTest : public ::testing::Test {
150 protected:
151 static constexpr size_t PARTITION_SIZE = 4096 * 10;
152
153 // Returns a sorted list of files in |dirname|.
154 static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
155 std::vector<std::string> file_list;
156
157 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
158 struct dirent* de;
159 while ((de = readdir(d.get())) != 0) {
160 std::string path = dirname + "/" + de->d_name;
161
162 struct stat st;
163 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
164 file_list.emplace_back(de->d_name);
165 }
166 }
167
168 std::sort(file_list.begin(), file_list.end());
169 return file_list;
170 }
171
Tao Baob1c5b622018-07-12 11:49:05 -0700172 void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700173 std::string zeros(4096, 0);
174 for (const auto& file : files) {
Tao Baob1c5b622018-07-12 11:49:05 -0700175 temporary_files_.push_back(dir + "/" + file);
176 ASSERT_TRUE(android::base::WriteStringToFile(zeros, temporary_files_.back()));
Tao Baob8cb2e62018-07-06 12:14:36 -0700177 }
178 }
179
180 void SetUp() override {
181 Paths::Get().set_cache_log_directory(mock_log_dir.path);
Tao Baob1c5b622018-07-12 11:49:05 -0700182 temporary_files_.clear();
183 }
184
185 void TearDown() override {
186 for (const auto& file : temporary_files_) {
187 ASSERT_TRUE(android::base::RemoveFileIfExists(file));
188 }
Tao Baob8cb2e62018-07-06 12:14:36 -0700189 }
190
191 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
192 // bytes and all files are 4096 bytes in size.
Tao Baob1c5b622018-07-12 11:49:05 -0700193 static size_t MockFreeSpaceChecker(const std::string& dirname) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700194 std::vector<std::string> files = FindFilesInDir(dirname);
195 return PARTITION_SIZE - 4096 * files.size();
196 }
197
198 TemporaryDir mock_cache;
199 TemporaryDir mock_log_dir;
Tao Baob1c5b622018-07-12 11:49:05 -0700200
201 private:
202 std::vector<std::string> temporary_files_;
Tao Baob8cb2e62018-07-06 12:14:36 -0700203};
204
205TEST_F(FreeCacheTest, FreeCacheSmoke) {
206 std::vector<std::string> files = { "file1", "file2", "file3" };
207 AddFilesToDir(mock_cache.path, files);
208 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
209 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
210
Tao Baob1c5b622018-07-12 11:49:05 -0700211 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700212
213 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
214 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
215}
216
Tao Bao49750f12018-07-11 16:32:10 -0700217TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) {
218 std::vector<std::string> files{ "file1", "file2", "file3" };
219 AddFilesToDir(mock_cache.path, files);
220 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
221 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
222
223 ASSERT_FALSE(
224 RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; }));
225}
226
Tao Baob8cb2e62018-07-06 12:14:36 -0700227TEST_F(FreeCacheTest, FreeCacheOpenFile) {
228 std::vector<std::string> files = { "file1", "file2" };
229 AddFilesToDir(mock_cache.path, files);
230 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
231 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
232
233 std::string file1_path = mock_cache.path + "/file1"s;
234 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
235
236 // file1 can't be deleted as it's opened by us.
Tao Baob1c5b622018-07-12 11:49:05 -0700237 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700238
239 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
240}
241
242TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
243 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
244 "last_log.10" };
245 AddFilesToDir(mock_log_dir.path, log_files);
246 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
247
Tao Baob1c5b622018-07-12 11:49:05 -0700248 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700249
250 // Logs with a higher index will be deleted first
251 std::vector<std::string> expected = { "last_log", "last_log.1" };
252 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
253 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
254}
255
256TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
257 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
258 "last_kmsgrandom" };
259 AddFilesToDir(mock_log_dir.path, log_files);
260 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
261
Tao Baob1c5b622018-07-12 11:49:05 -0700262 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700263
264 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
265 // deleted before last_log.
266 std::vector<std::string> expected = { "last_log.1" };
267 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
268 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
269}
270
271TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
272 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
273 "last_kmsg.1" };
274 AddFilesToDir(mock_log_dir.path, log_files);
275 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
276
Tao Baob1c5b622018-07-12 11:49:05 -0700277 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700278
279 // Non log files in /cache/recovery won't be deleted.
280 std::vector<std::string> expected = { "block.map", "command", "last_install" };
281 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
282}