blob: 0d4123b5ed2f636dd93472628ab499341a530bd7 [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
Tao Baob1c5b622018-07-12 11:49:05 -0700167 void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700168 std::string zeros(4096, 0);
169 for (const auto& file : files) {
Tao Baob1c5b622018-07-12 11:49:05 -0700170 temporary_files_.push_back(dir + "/" + file);
171 ASSERT_TRUE(android::base::WriteStringToFile(zeros, temporary_files_.back()));
Tao Baob8cb2e62018-07-06 12:14:36 -0700172 }
173 }
174
175 void SetUp() override {
176 Paths::Get().set_cache_log_directory(mock_log_dir.path);
Tao Baob1c5b622018-07-12 11:49:05 -0700177 temporary_files_.clear();
178 }
179
180 void TearDown() override {
181 for (const auto& file : temporary_files_) {
182 ASSERT_TRUE(android::base::RemoveFileIfExists(file));
183 }
Tao Baob8cb2e62018-07-06 12:14:36 -0700184 }
185
186 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
187 // bytes and all files are 4096 bytes in size.
Tao Baob1c5b622018-07-12 11:49:05 -0700188 static size_t MockFreeSpaceChecker(const std::string& dirname) {
Tao Baob8cb2e62018-07-06 12:14:36 -0700189 std::vector<std::string> files = FindFilesInDir(dirname);
190 return PARTITION_SIZE - 4096 * files.size();
191 }
192
193 TemporaryDir mock_cache;
194 TemporaryDir mock_log_dir;
Tao Baob1c5b622018-07-12 11:49:05 -0700195
196 private:
197 std::vector<std::string> temporary_files_;
Tao Baob8cb2e62018-07-06 12:14:36 -0700198};
199
200TEST_F(FreeCacheTest, FreeCacheSmoke) {
201 std::vector<std::string> files = { "file1", "file2", "file3" };
202 AddFilesToDir(mock_cache.path, files);
203 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
204 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
205
Tao Baob1c5b622018-07-12 11:49:05 -0700206 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700207
208 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
209 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
210}
211
Tao Bao49750f12018-07-11 16:32:10 -0700212TEST_F(FreeCacheTest, FreeCacheFreeSpaceCheckerError) {
213 std::vector<std::string> files{ "file1", "file2", "file3" };
214 AddFilesToDir(mock_cache.path, files);
215 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
216 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
217
218 ASSERT_FALSE(
219 RemoveFilesInDirectory(4096 * 9, mock_cache.path, [](const std::string&) { return -1; }));
220}
221
Tao Baob8cb2e62018-07-06 12:14:36 -0700222TEST_F(FreeCacheTest, FreeCacheOpenFile) {
223 std::vector<std::string> files = { "file1", "file2" };
224 AddFilesToDir(mock_cache.path, files);
225 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
226 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
227
228 std::string file1_path = mock_cache.path + "/file1"s;
229 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
230
231 // file1 can't be deleted as it's opened by us.
Tao Baob1c5b622018-07-12 11:49:05 -0700232 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700233
234 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
235}
236
237TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
238 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
239 "last_log.10" };
240 AddFilesToDir(mock_log_dir.path, log_files);
241 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
242
Tao Baob1c5b622018-07-12 11:49:05 -0700243 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700244
245 // Logs with a higher index will be deleted first
246 std::vector<std::string> expected = { "last_log", "last_log.1" };
247 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
248 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
249}
250
251TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
252 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
253 "last_kmsgrandom" };
254 AddFilesToDir(mock_log_dir.path, log_files);
255 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
256
Tao Baob1c5b622018-07-12 11:49:05 -0700257 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700258
259 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
260 // deleted before last_log.
261 std::vector<std::string> expected = { "last_log.1" };
262 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
263 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
264}
265
266TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
267 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
268 "last_kmsg.1" };
269 AddFilesToDir(mock_log_dir.path, log_files);
270 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
271
Tao Baob1c5b622018-07-12 11:49:05 -0700272 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, MockFreeSpaceChecker));
Tao Baob8cb2e62018-07-06 12:14:36 -0700273
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}