blob: 4fbdd37fa65b8c9c7dc40cc1fd5c4ffefce165c6 [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>
36#include <openssl/sha.h>
37
38#include "applypatch/applypatch.h"
39#include "common/test_constants.h"
40#include "otautil/paths.h"
41#include "otautil/print_sha1.h"
42
43using namespace std::string_literals;
44
45static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
46 ASSERT_TRUE(sha1 != nullptr);
47
48 std::string data;
49 ASSERT_TRUE(android::base::ReadFileToString(fname, &data));
50
51 if (fsize != nullptr) {
52 *fsize = data.size();
53 }
54
55 uint8_t digest[SHA_DIGEST_LENGTH];
56 SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
57 *sha1 = print_sha1(digest);
58}
59
60static void mangle_file(const std::string& fname) {
61 std::string content(1024, '\0');
62 for (size_t i = 0; i < 1024; i++) {
63 content[i] = rand() % 256;
64 }
65 ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
66}
67
68class ApplyPatchTest : public ::testing::Test {
69 public:
70 void SetUp() override {
71 // set up files
72 old_file = from_testdata_base("old.file");
73 new_file = from_testdata_base("new.file");
74 nonexistent_file = from_testdata_base("nonexistent.file");
75
76 // set up SHA constants
77 sha1sum(old_file, &old_sha1, &old_size);
78 sha1sum(new_file, &new_sha1, &new_size);
79 srand(time(nullptr));
80 bad_sha1_a = android::base::StringPrintf("%040x", rand());
81 bad_sha1_b = android::base::StringPrintf("%040x", rand());
82 }
83
84 std::string old_file;
85 std::string new_file;
86 std::string nonexistent_file;
87
88 std::string old_sha1;
89 std::string new_sha1;
90 std::string bad_sha1_a;
91 std::string bad_sha1_b;
92
93 size_t old_size;
94 size_t new_size;
95};
96
97TEST_F(ApplyPatchTest, CheckModeSkip) {
98 std::vector<std::string> sha1s;
99 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
100}
101
102TEST_F(ApplyPatchTest, CheckModeSingle) {
103 std::vector<std::string> sha1s = { old_sha1 };
104 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
105}
106
107TEST_F(ApplyPatchTest, CheckModeMultiple) {
108 std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
109 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
110}
111
112TEST_F(ApplyPatchTest, CheckModeFailure) {
113 std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
114 ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
115}
116
117TEST_F(ApplyPatchTest, CheckModeEmmcTarget) {
118 // EMMC:old_file:size:sha1 should pass the check.
119 std::string src_file = "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
120 std::vector<std::string> sha1s;
121 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
122
123 // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
124 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
125 std::to_string(old_size + 1) + ":" + old_sha1;
126 ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s));
127
128 // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
129 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
130 std::to_string(old_size) + ":" + old_sha1 + ":" + std::to_string(old_size + 1) + ":" +
131 old_sha1;
132 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
133
134 // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
135 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
136 std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
137 old_sha1;
138 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
139
140 // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
141 // should pass the check.
142 src_file = "EMMC:" + new_file + ":" + std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
143 std::to_string(old_size - 1) + ":" + old_sha1 + ":" + std::to_string(old_size) + ":" +
144 old_sha1 + ":" + std::to_string(new_size) + ":" + new_sha1;
145 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
146}
147
148class ApplyPatchCacheTest : public ApplyPatchTest {
149 protected:
150 void SetUp() override {
151 ApplyPatchTest::SetUp();
152 Paths::Get().set_cache_temp_source(old_file);
153 }
154};
155
156TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) {
157 TemporaryFile temp_file;
158 mangle_file(temp_file.path);
159 std::vector<std::string> sha1s_single = { old_sha1 };
160 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single));
161 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single));
162}
163
164TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) {
165 TemporaryFile temp_file;
166 mangle_file(temp_file.path);
167 std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b };
168 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple));
169 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple));
170}
171
172TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) {
173 TemporaryFile temp_file;
174 mangle_file(temp_file.path);
175 std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b };
176 ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure));
177 ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure));
178}
179
180class FreeCacheTest : public ::testing::Test {
181 protected:
182 static constexpr size_t PARTITION_SIZE = 4096 * 10;
183
184 // Returns a sorted list of files in |dirname|.
185 static std::vector<std::string> FindFilesInDir(const std::string& dirname) {
186 std::vector<std::string> file_list;
187
188 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir);
189 struct dirent* de;
190 while ((de = readdir(d.get())) != 0) {
191 std::string path = dirname + "/" + de->d_name;
192
193 struct stat st;
194 if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
195 file_list.emplace_back(de->d_name);
196 }
197 }
198
199 std::sort(file_list.begin(), file_list.end());
200 return file_list;
201 }
202
203 static void AddFilesToDir(const std::string& dir, const std::vector<std::string>& files) {
204 std::string zeros(4096, 0);
205 for (const auto& file : files) {
206 std::string path = dir + "/" + file;
207 ASSERT_TRUE(android::base::WriteStringToFile(zeros, path));
208 }
209 }
210
211 void SetUp() override {
212 Paths::Get().set_cache_log_directory(mock_log_dir.path);
213 }
214
215 // A mock method to calculate the free space. It assumes the partition has a total size of 40960
216 // bytes and all files are 4096 bytes in size.
217 size_t MockFreeSpaceChecker(const std::string& dirname) {
218 std::vector<std::string> files = FindFilesInDir(dirname);
219 return PARTITION_SIZE - 4096 * files.size();
220 }
221
222 TemporaryDir mock_cache;
223 TemporaryDir mock_log_dir;
224};
225
226TEST_F(FreeCacheTest, FreeCacheSmoke) {
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_TRUE(RemoveFilesInDirectory(4096 * 9, mock_cache.path, [&](const std::string& dir) {
233 return this->MockFreeSpaceChecker(dir);
234 }));
235
236 ASSERT_EQ(std::vector<std::string>{ "file3" }, FindFilesInDir(mock_cache.path));
237 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_cache.path));
238}
239
240TEST_F(FreeCacheTest, FreeCacheOpenFile) {
241 std::vector<std::string> files = { "file1", "file2" };
242 AddFilesToDir(mock_cache.path, files);
243 ASSERT_EQ(files, FindFilesInDir(mock_cache.path));
244 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_cache.path));
245
246 std::string file1_path = mock_cache.path + "/file1"s;
247 android::base::unique_fd fd(open(file1_path.c_str(), O_RDONLY));
248
249 // file1 can't be deleted as it's opened by us.
250 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 10, mock_cache.path, [&](const std::string& dir) {
251 return this->MockFreeSpaceChecker(dir);
252 }));
253
254 ASSERT_EQ(std::vector<std::string>{ "file1" }, FindFilesInDir(mock_cache.path));
255}
256
257TEST_F(FreeCacheTest, FreeCacheLogsSmoke) {
258 std::vector<std::string> log_files = { "last_log", "last_log.1", "last_kmsg.2", "last_log.5",
259 "last_log.10" };
260 AddFilesToDir(mock_log_dir.path, log_files);
261 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
262
263 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
264 return this->MockFreeSpaceChecker(dir);
265 }));
266
267 // Logs with a higher index will be deleted first
268 std::vector<std::string> expected = { "last_log", "last_log.1" };
269 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
270 ASSERT_EQ(4096 * 8, MockFreeSpaceChecker(mock_log_dir.path));
271}
272
273TEST_F(FreeCacheTest, FreeCacheLogsStringComparison) {
274 std::vector<std::string> log_files = { "last_log.1", "last_kmsg.1", "last_log.not_number",
275 "last_kmsgrandom" };
276 AddFilesToDir(mock_log_dir.path, log_files);
277 ASSERT_EQ(4096 * 6, MockFreeSpaceChecker(mock_log_dir.path));
278
279 ASSERT_TRUE(RemoveFilesInDirectory(4096 * 9, mock_log_dir.path, [&](const std::string& dir) {
280 return this->MockFreeSpaceChecker(dir);
281 }));
282
283 // Logs with incorrect format will be deleted first; and the last_kmsg with the same index is
284 // deleted before last_log.
285 std::vector<std::string> expected = { "last_log.1" };
286 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
287 ASSERT_EQ(4096 * 9, MockFreeSpaceChecker(mock_log_dir.path));
288}
289
290TEST_F(FreeCacheTest, FreeCacheLogsOtherFiles) {
291 std::vector<std::string> log_files = { "last_install", "command", "block.map", "last_log",
292 "last_kmsg.1" };
293 AddFilesToDir(mock_log_dir.path, log_files);
294 ASSERT_EQ(4096 * 5, MockFreeSpaceChecker(mock_log_dir.path));
295
296 ASSERT_FALSE(RemoveFilesInDirectory(4096 * 8, mock_log_dir.path, [&](const std::string& dir) {
297 return this->MockFreeSpaceChecker(dir);
298 }));
299
300 // Non log files in /cache/recovery won't be deleted.
301 std::vector<std::string> expected = { "block.map", "command", "last_install" };
302 ASSERT_EQ(expected, FindFilesInDir(mock_log_dir.path));
303}