blob: 5cc03bc7bb96b141a45cff1cb3233ff1a87fe6b2 [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>
19#include <stdio.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <time.h>
24#include <unistd.h>
25
26#include <algorithm>
27#include <memory>
28#include <string>
29#include <vector>
30
31#include <android-base/file.h>
32#include <android-base/stringprintf.h>
33#include <android-base/test_utils.h>
34#include <android-base/unique_fd.h>
35#include <gtest/gtest.h>
Tao Baob8cb2e62018-07-06 12:14:36 -070036
37#include "applypatch/applypatch.h"
38#include "common/test_constants.h"
39#include "otautil/paths.h"
40#include "otautil/print_sha1.h"
41
42using namespace std::string_literals;
43
Tao Baob8cb2e62018-07-06 12:14:36 -070044class ApplyPatchTest : public ::testing::Test {
Tao Bao7c1d4262018-07-06 23:18:14 -070045 protected:
Tao Baob8cb2e62018-07-06 12:14:36 -070046 void SetUp() override {
Tao Baob8cb2e62018-07-06 12:14:36 -070047 old_file = from_testdata_base("old.file");
Tao Bao7c1d4262018-07-06 23:18:14 -070048 FileContents old_fc;
49 ASSERT_EQ(0, LoadFileContents(old_file, &old_fc));
50 old_sha1 = print_sha1(old_fc.sha1);
51 old_size = old_fc.data.size();
Tao Baob8cb2e62018-07-06 12:14:36 -070052
Tao Bao7c1d4262018-07-06 23:18:14 -070053 new_file = from_testdata_base("new.file");
54 FileContents new_fc;
55 ASSERT_EQ(0, LoadFileContents(new_file, &new_fc));
56 new_sha1 = print_sha1(new_fc.sha1);
57 new_size = new_fc.data.size();
58
Tao Baob8cb2e62018-07-06 12:14:36 -070059 srand(time(nullptr));
60 bad_sha1_a = android::base::StringPrintf("%040x", rand());
61 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao7c1d4262018-07-06 23:18:14 -070062
63 // Reset the cache backup file.
64 Paths::Get().set_cache_temp_source("/cache/saved.file");
Tao Baob8cb2e62018-07-06 12:14:36 -070065 }
66
67 std::string old_file;
Tao Baob8cb2e62018-07-06 12:14:36 -070068 std::string old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -070069 size_t old_size;
70
71 std::string new_file;
Tao Baob8cb2e62018-07-06 12:14:36 -070072 std::string new_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -070073 size_t new_size;
74
Tao Baob8cb2e62018-07-06 12:14:36 -070075 std::string bad_sha1_a;
76 std::string bad_sha1_b;
Tao Baob8cb2e62018-07-06 12:14:36 -070077};
78
Tao Bao7c1d4262018-07-06 23:18:14 -070079TEST_F(ApplyPatchTest, CheckMode) {
80 std::string partition = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
81 ASSERT_EQ(0, applypatch_check(partition, {}));
82 ASSERT_EQ(0, applypatch_check(partition, { old_sha1 }));
83 ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, bad_sha1_b }));
84 ASSERT_EQ(0, applypatch_check(partition, { bad_sha1_a, old_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -070085}
86
Tao Bao7c1d4262018-07-06 23:18:14 -070087TEST_F(ApplyPatchTest, CheckMode_NonEmmcTarget) {
88 ASSERT_NE(0, applypatch_check(old_file, {}));
89 ASSERT_NE(0, applypatch_check(old_file, { old_sha1 }));
90 ASSERT_NE(0, applypatch_check(old_file, { bad_sha1_a, bad_sha1_b }));
91 ASSERT_NE(0, applypatch_check(old_file, { bad_sha1_a, old_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -070092}
93
Tao Bao7c1d4262018-07-06 23:18:14 -070094TEST_F(ApplyPatchTest, CheckMode_EmmcTarget) {
Tao Baob8cb2e62018-07-06 12:14:36 -070095 // EMMC:old_file:size:sha1 should pass the check.
96 std::string src_file = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -070097 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -070098
99 // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
100 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
101 std::to_string(old_size + 1) + ":" + old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700102 ASSERT_NE(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700103
104 // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
105 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
106 std::to_string(old_size) + ":" + old_sha1 + ":" + std::to_string(old_size + 1) + ":" +
107 old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700108 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700109
110 // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
111 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
112 std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
113 old_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700114 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700115
116 // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
117 // should pass the check.
118 src_file = "EMMC:" + new_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
119 std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
120 old_sha1 + ":" + std::to_string(new_size) + ":" + new_sha1;
Tao Bao7c1d4262018-07-06 23:18:14 -0700121 ASSERT_EQ(0, applypatch_check(src_file, {}));
Tao Baob8cb2e62018-07-06 12:14:36 -0700122}
123
Tao Bao7c1d4262018-07-06 23:18:14 -0700124TEST_F(ApplyPatchTest, CheckMode_UseBackup) {
125 std::string corrupted = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + bad_sha1_a;
126 ASSERT_NE(0, applypatch_check(corrupted, { old_sha1 }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700127
Tao Bao7c1d4262018-07-06 23:18:14 -0700128 Paths::Get().set_cache_temp_source(old_file);
129 ASSERT_EQ(0, applypatch_check(corrupted, { old_sha1 }));
130 ASSERT_EQ(0, applypatch_check(corrupted, { bad_sha1_a, old_sha1, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700131}
132
Tao Bao7c1d4262018-07-06 23:18:14 -0700133TEST_F(ApplyPatchTest, CheckMode_UseBackup_BothCorrupted) {
134 std::string corrupted = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + bad_sha1_a;
135 ASSERT_NE(0, applypatch_check(corrupted, {}));
136 ASSERT_NE(0, applypatch_check(corrupted, { old_sha1 }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700137
Tao Bao7c1d4262018-07-06 23:18:14 -0700138 Paths::Get().set_cache_temp_source(old_file);
139 ASSERT_NE(0, applypatch_check(corrupted, { bad_sha1_a, bad_sha1_b }));
Tao Baob8cb2e62018-07-06 12:14:36 -0700140}
141
142class FreeCacheTest : public ::testing::Test {
143 protected:
144 static constexpr size_t PARTITION_SIZE = 4096 * 10;
145
146 // Returns a sorted list of files in |dirname|.
147 static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
148 std::vector<std::string> file_list;
149
150 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
151 struct dirent* de;
152 while ((de = readdir(d.get())) != 0) {
153 std::string path = dirname + "/" + de->d_name;
154
155 struct stat st;
156 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
157 file_list.emplace_back(de->d_name);
158 }
159 }
160
161 std::sort(file_list.begin(), file_list.end());
162 return file_list;
163 }
164
165 static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
166 std::string zeros(4096, 0);
167 for (const auto& file : files) {
168 std::string path = dir + "/" + file;
169 ASSERT_TRUE(android::base::WriteStringToFile(zeros, path));
170 }
171 }
172
173 void SetUp() override {
174 Paths::Get().set_cache_log_directory(mock_log_dir.path);
175 }
176
177 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
178 // bytes and all files are 4096 bytes in size.
179 size_t MockFreeSpaceChecker(const std::string& dirname) {
180 std::vector<std::string> files = FindFilesInDir(dirname);
181 return PARTITION_SIZE - 4096 * files.size();
182 }
183
184 TemporaryDir mock_cache;
185 TemporaryDir mock_log_dir;
186};
187
188TEST_F(FreeCacheTest, FreeCacheSmoke) {
189 std::vector<std::string> files = { "file1", "file2", "file3" };
190 AddFilesToDir(mock_cache.path, files);
191 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
192 ASSERT_EQ(4096 * 7, MockFreeSpaceChecker(mock_cache.path));
193
194 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) {
195 return this->MockFreeSpaceChecker(dir);
196 }));
197
198 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
199 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
200}
201
202TEST_F(FreeCacheTest, FreeCacheOpenFile) {
203 std::vector<std::string> files = { "file1", "file2" };
204 AddFilesToDir(mock_cache.path, files);
205 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
206 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
207
208 std::string file1_path = mock_cache.path + "/file1"s;
209 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
210
211 // file1 can't be deleted as it's opened by us.
212 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) {
213 return this->MockFreeSpaceChecker(dir);
214 }));
215
216 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
217}
218
219TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
220 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
221 "last_log.10" };
222 AddFilesToDir(mock_log_dir.path, log_files);
223 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
224
225 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
226 return this->MockFreeSpaceChecker(dir);
227 }));
228
229 // Logs with a higher index will be deleted first
230 std::vector<std::string> expected = { "last_log", "last_log.1" };
231 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
232 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
233}
234
235TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
236 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
237 "last_kmsgrandom" };
238 AddFilesToDir(mock_log_dir.path, log_files);
239 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
240
241 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) {
242 return this->MockFreeSpaceChecker(dir);
243 }));
244
245 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
246 // deleted before last_log.
247 std::vector<std::string> expected = { "last_log.1" };
248 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
249 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
250}
251
252TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
253 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
254 "last_kmsg.1" };
255 AddFilesToDir(mock_log_dir.path, log_files);
256 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
257
258 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
259 return this->MockFreeSpaceChecker(dir);
260 }));
261
262 // Non log files in /cache/recovery won't be deleted.
263 std::vector<std::string> expected = { "block.map", "command", "last_install" };
264 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
265}