blob: 4b757feab430d5108363234b9fbf6b59837c2efb [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 Baob8cb2e62018-07-06 12:14:36 -070049 old_file = from_testdata_base("old.file");
Tao Bao7c1d4262018-07-06 23:18:14 -070050 FileContents old_fc;
51 ASSERT_EQ(0, LoadFileContents(old_file, &old_fc));
52 old_sha1 = print_sha1(old_fc.sha1);
53 old_size = old_fc.data.size();
Tao Baob8cb2e62018-07-06 12:14:36 -070054
Tao Bao7c1d4262018-07-06 23:18:14 -070055 new_file = from_testdata_base("new.file");
56 FileContents new_fc;
57 ASSERT_EQ(0, LoadFileContents(new_file, &new_fc));
58 new_sha1 = print_sha1(new_fc.sha1);
59 new_size = new_fc.data.size();
60
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
69 std::string old_file;
Tao Baob8cb2e62018-07-06 12:14:36 -070070 std::string old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -070071 size_t old_size;
72
73 std::string new_file;
Tao Baob8cb2e62018-07-06 12:14:36 -070074 std::string new_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -070075 size_t new_size;
76
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) {
82 std::string partition = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
83 ASSERT_EQ(0, applypatch_check(partition, {}));
84 ASSERT_EQ(0, applypatch_check(partition, { old_sha1 }));
85 ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, bad_sha1_b }));
86 ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, old_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -070087}
88
Tao Bao7c1d4262018-07-06 23:18:14 -070089TEST_F(ApplyPatchTest, CheckMode_NonEmmcTarget) {
90 ASSERT_NE(0, applypatch_check(old_file, {}));
91 ASSERT_NE(0, applypatch_check(old_file, { old_sha1 }));
92 ASSERT_NE(0, applypatch_check(old_file, { bad_sha1_a, bad_sha1_b }));
93 ASSERT_NE(0, applypatch_check(old_file, { bad_sha1_a, old_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -070094}
95
Tao Bao7c1d4262018-07-06 23:18:14 -070096TEST_F(ApplyPatchTest, CheckMode_EmmcTarget) {
Tao Baob8cb2e62018-07-06 12:14:36 -070097 // EMMC:old_file:size:sha1 should pass the check.
98 std::string src_file = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -070099 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700100
101 // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
102 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
103 std::to_string(old_size + 1) + ":" + old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700104 ASSERT_NE(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700105
106 // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
107 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
108 std::to_string(old_size) + ":" + old_sha1 + ":" + std::to_string(old_size + 1) + ":" +
109 old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700110 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700111
112 // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
113 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
114 std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
115 old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700116 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700117
118 // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
119 // should pass the check.
120 src_file = "EMMC:" + new_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
121 std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
122 old_sha1 + ":" + std::to_string(new_size) + ":" + new_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700123 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700124}
125
Tao Bao7c1d4262018-07-06 23:18:14 -0700126TEST_F(ApplyPatchTest, CheckMode_UseBackup) {
127 std::string corrupted = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + bad_sha1_a;
128 ASSERT_NE(0, applypatch_check(corrupted, { old_sha1 }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700129
Tao Bao7c1d4262018-07-06 23:18:14 -0700130 Paths::Get().set_cache_temp_source(old_file);
131 ASSERT_EQ(0, applypatch_check(corrupted, { old_sha1 }));
132 ASSERT_EQ(0, applypatch_check(corrupted, { bad_sha1_a, old_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700133}
134
Tao Bao7c1d4262018-07-06 23:18:14 -0700135TEST_F(ApplyPatchTest, CheckMode_UseBackup_BothCorrupted) {
136 std::string corrupted = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + bad_sha1_a;
137 ASSERT_NE(0, applypatch_check(corrupted, {}));
138 ASSERT_NE(0, applypatch_check(corrupted, { old_sha1 }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700139
Tao Bao7c1d4262018-07-06 23:18:14 -0700140 Paths::Get().set_cache_temp_source(old_file);
141 ASSERT_NE(0, applypatch_check(corrupted, { bad_sha1_a, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700142}
143
144class FreeCacheTest : public ::testing::Test {
145 protected:
146 static constexpr size_t PARTITION_SIZE = 4096 * 10;
147
148 // Returns a sorted list of files in |dirname|.
149 static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
150 std::vector<std::string> file_list;
151
152 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
153 struct dirent* de;
154 while ((de = readdir(d.get())) != 0) {
155 std::string path = dirname + "/" + de->d_name;
156
157 struct stat st;
158 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
159 file_list.emplace_back(de->d_name);
160 }
161 }
162
163 std::sort(file_list.begin(), file_list.end());
164 return file_list;
165 }
166
167 static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
168 std::string zeros(4096, 0);
169 for (const auto& file : files) {
170 std::string path = dir + "/" + file;
171 ASSERT_TRUE(android::base::WriteStringToFile(zeros, path));
172 }
173 }
174
175 void SetUp() override {
176 Paths::Get().set_cache_log_directory(mock_log_dir.path);
177 }
178
179 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
180 // bytes and all files are 4096 bytes in size.
Tao Bao49750f12018-07-11 16:32:10 -0700181 int64_t MockFreeSpaceChecker(const std::string& dirname) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700182 std::vector<std::string> files = FindFilesInDir(dirname);
183 return PARTITION_SIZE - 4096 * files.size();
184 }
185
186 TemporaryDir mock_cache;
187 TemporaryDir mock_log_dir;
188};
189
190TEST_F(FreeCacheTest, FreeCacheSmoke) {
191 std::vector<std::string> files = { "file1", "file2", "file3" };
192 AddFilesToDir(mock_cache.path, files);
193 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
194 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
195
Tao Bao49750f12018-07-11 16:32:10 -0700196 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [this](const std::string& dir) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700197 return this->MockFreeSpaceChecker(dir);
198 }));
199
200 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
201 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
202}
203
Tao Bao49750f12018-07-11 16:32:10 -0700204TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) {
205 std::vector<std::string> files{ "file1", "file2", "file3" };
206 AddFilesToDir(mock_cache.path, files);
207 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
208 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
209
210 ASSERT_FALSE(
211 RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; }));
212}
213
Tao Baob8cb2e62018-07-06 12:14:36 -0700214TEST_F(FreeCacheTest, FreeCacheOpenFile) {
215 std::vector<std::string> files = { "file1", "file2" };
216 AddFilesToDir(mock_cache.path, files);
217 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
218 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
219
220 std::string file1_path = mock_cache.path + "/file1"s;
221 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
222
223 // file1 can't be deleted as it's opened by us.
Tao Bao49750f12018-07-11 16:32:10 -0700224 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [this](const std::string& dir) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700225 return this->MockFreeSpaceChecker(dir);
226 }));
227
228 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
229}
230
231TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
232 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
233 "last_log.10" };
234 AddFilesToDir(mock_log_dir.path, log_files);
235 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
236
Tao Bao49750f12018-07-11 16:32:10 -0700237 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [this](const std::string& dir) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700238 return this->MockFreeSpaceChecker(dir);
239 }));
240
241 // Logs with a higher index will be deleted first
242 std::vector<std::string> expected = { "last_log", "last_log.1" };
243 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
244 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
245}
246
247TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
248 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
249 "last_kmsgrandom" };
250 AddFilesToDir(mock_log_dir.path, log_files);
251 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
252
Tao Bao49750f12018-07-11 16:32:10 -0700253 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [this](const std::string& dir) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700254 return this->MockFreeSpaceChecker(dir);
255 }));
256
257 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
258 // deleted before last_log.
259 std::vector<std::string> expected = { "last_log.1" };
260 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
261 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
262}
263
264TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
265 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
266 "last_kmsg.1" };
267 AddFilesToDir(mock_log_dir.path, log_files);
268 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
269
Tao Bao49750f12018-07-11 16:32:10 -0700270 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [this](const std::string& dir) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700271 return this->MockFreeSpaceChecker(dir);
272 }));
273
274 // Non log files in /cache/recovery won't be deleted.
275 std::vector<std::string> expected = { "block.map", "command", "last_install" };
276 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
277}