blob: 21c9a52dc9594890ce23b8e844eae4fc0aadea8d [file] [log] [blame]
Jed Estepb8a693b2016-03-09 17:51:34 -08001/*
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 <fcntl.h>
18#include <gtest/gtest.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22#include <sys/statvfs.h>
23#include <sys/types.h>
24#include <time.h>
25
Tao Baofada91c2016-10-27 18:16:06 -070026#include <memory>
Jed Estepb8a693b2016-03-09 17:51:34 -080027#include <string>
Tao Baofada91c2016-10-27 18:16:06 -070028#include <vector>
Jed Estepb8a693b2016-03-09 17:51:34 -080029
30#include <android-base/file.h>
31#include <android-base/stringprintf.h>
32#include <android-base/test_utils.h>
Tao Baofada91c2016-10-27 18:16:06 -070033#include <openssl/sha.h>
Jed Estepb8a693b2016-03-09 17:51:34 -080034
35#include "applypatch/applypatch.h"
Tao Bao36c35112016-10-25 14:17:26 -070036#include "applypatch/applypatch_modes.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080037#include "common/test_constants.h"
Tao Bao09e468f2017-09-29 14:39:33 -070038#include "otautil/print_sha1.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080039
Tao Bao36c35112016-10-25 14:17:26 -070040static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
41 ASSERT_NE(nullptr, sha1);
Tao Baofada91c2016-10-27 18:16:06 -070042
Tao Bao36c35112016-10-25 14:17:26 -070043 std::string data;
44 ASSERT_TRUE(android::base::ReadFileToString(fname, &data));
Jed Estepb8a693b2016-03-09 17:51:34 -080045
Tao Bao36c35112016-10-25 14:17:26 -070046 if (fsize != nullptr) {
47 *fsize = data.size();
48 }
49
50 uint8_t digest[SHA_DIGEST_LENGTH];
51 SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
52 *sha1 = print_sha1(digest);
Jed Estepb8a693b2016-03-09 17:51:34 -080053}
54
55static void mangle_file(const std::string& fname) {
Tianjie Xua88cc542017-10-25 13:16:54 -070056 std::string content(1024, '\0');
Tao Bao36c35112016-10-25 14:17:26 -070057 for (size_t i = 0; i < 1024; i++) {
58 content[i] = rand() % 256;
59 }
60 ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
Jed Estepb8a693b2016-03-09 17:51:34 -080061}
62
Jed Estepb8a693b2016-03-09 17:51:34 -080063class ApplyPatchTest : public ::testing::Test {
Tao Bao36c35112016-10-25 14:17:26 -070064 public:
Tianjie Xua88cc542017-10-25 13:16:54 -070065 virtual void SetUp() override {
Tao Bao36c35112016-10-25 14:17:26 -070066 // set up files
67 old_file = from_testdata_base("old.file");
68 new_file = from_testdata_base("new.file");
Tianjie Xua88cc542017-10-25 13:16:54 -070069 nonexistent_file = from_testdata_base("nonexistent.file");
Jed Estepb8a693b2016-03-09 17:51:34 -080070
Tao Bao36c35112016-10-25 14:17:26 -070071 // set up SHA constants
Tao Bao8dd44e92016-11-21 11:16:56 -080072 sha1sum(old_file, &old_sha1, &old_size);
73 sha1sum(new_file, &new_sha1, &new_size);
Tao Bao36c35112016-10-25 14:17:26 -070074 srand(time(nullptr));
75 bad_sha1_a = android::base::StringPrintf("%040x", rand());
76 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao36c35112016-10-25 14:17:26 -070077 }
Jed Estepb8a693b2016-03-09 17:51:34 -080078
Tianjie Xua88cc542017-10-25 13:16:54 -070079 std::string old_file;
80 std::string new_file;
81 std::string nonexistent_file;
Jed Estepb8a693b2016-03-09 17:51:34 -080082
Tianjie Xua88cc542017-10-25 13:16:54 -070083 std::string old_sha1;
84 std::string new_sha1;
85 std::string bad_sha1_a;
86 std::string bad_sha1_b;
Jed Estepb8a693b2016-03-09 17:51:34 -080087
Tianjie Xua88cc542017-10-25 13:16:54 -070088 size_t old_size;
89 size_t new_size;
Jed Estepb8a693b2016-03-09 17:51:34 -080090};
91
Jed Estepb8a693b2016-03-09 17:51:34 -080092class ApplyPatchCacheTest : public ApplyPatchTest {
Tianjie Xua88cc542017-10-25 13:16:54 -070093 protected:
94 void SetUp() override {
95 ApplyPatchTest::SetUp();
96 cache_temp_source = old_file;
Tao Bao36c35112016-10-25 14:17:26 -070097 }
Jed Estepb8a693b2016-03-09 17:51:34 -080098};
99
Tianjie Xua88cc542017-10-25 13:16:54 -0700100class ApplyPatchModesTest : public ::testing::Test {
101 protected:
102 void SetUp() override {
103 cache_temp_source = cache_source.path;
104 }
105
106 TemporaryFile cache_source;
107};
Jed Estepb8a693b2016-03-09 17:51:34 -0800108
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700109TEST_F(ApplyPatchTest, CheckModeSkip) {
Tao Bao36c35112016-10-25 14:17:26 -0700110 std::vector<std::string> sha1s;
111 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700112}
113
Jed Estepb8a693b2016-03-09 17:51:34 -0800114TEST_F(ApplyPatchTest, CheckModeSingle) {
Tao Bao36c35112016-10-25 14:17:26 -0700115 std::vector<std::string> sha1s = { old_sha1 };
116 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800117}
118
119TEST_F(ApplyPatchTest, CheckModeMultiple) {
Tao Bao36c35112016-10-25 14:17:26 -0700120 std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
121 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800122}
123
124TEST_F(ApplyPatchTest, CheckModeFailure) {
Tao Bao36c35112016-10-25 14:17:26 -0700125 std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
126 ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800127}
128
Tao Bao8dd44e92016-11-21 11:16:56 -0800129TEST_F(ApplyPatchTest, CheckModeEmmcTarget) {
130 // EMMC:old_file:size:sha1 should pass the check.
131 std::string src_file =
132 "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
133 std::vector<std::string> sha1s;
134 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
135
136 // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
137 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
138 std::to_string(old_size + 1) + ":" + old_sha1;
139 ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s));
140
141 // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
142 src_file = "EMMC:" + old_file + ":" +
143 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
144 std::to_string(old_size) + ":" + old_sha1 + ":" +
145 std::to_string(old_size + 1) + ":" + old_sha1;
146 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
147
148 // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
149 src_file = "EMMC:" + old_file + ":" +
150 std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
151 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
152 std::to_string(old_size) + ":" + old_sha1;
153 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
154
155 // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
156 // should pass the check.
157 src_file = "EMMC:" + new_file + ":" +
158 std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
159 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
160 std::to_string(old_size) + ":" + old_sha1 + ":" +
161 std::to_string(new_size) + ":" + new_sha1;
162 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
163}
164
Tianjie Xua88cc542017-10-25 13:16:54 -0700165TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) {
166 TemporaryFile temp_file;
167 mangle_file(temp_file.path);
168 std::vector<std::string> sha1s_single = { old_sha1 };
169 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single));
170 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single));
Jed Estepb8a693b2016-03-09 17:51:34 -0800171}
172
Tianjie Xua88cc542017-10-25 13:16:54 -0700173TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) {
174 TemporaryFile temp_file;
175 mangle_file(temp_file.path);
176 std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b };
177 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple));
178 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple));
Jed Estepb8a693b2016-03-09 17:51:34 -0800179}
180
Tianjie Xua88cc542017-10-25 13:16:54 -0700181TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) {
182 TemporaryFile temp_file;
183 mangle_file(temp_file.path);
184 std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b };
185 ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure));
186 ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure));
Jed Estepb8a693b2016-03-09 17:51:34 -0800187}
188
Tianjie Xua88cc542017-10-25 13:16:54 -0700189TEST_F(ApplyPatchModesTest, InvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700190 // At least two args (including the filename).
191 ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" }));
192
193 // Unrecognized args.
194 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" }));
195}
196
Tianjie Xua88cc542017-10-25 13:16:54 -0700197TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) {
Tao Bao8dd44e92016-11-21 11:16:56 -0800198 std::string boot_img = from_testdata_base("boot.img");
199 size_t boot_img_size;
200 std::string boot_img_sha1;
201 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
202
203 std::string recovery_img = from_testdata_base("recovery.img");
204 size_t size;
205 std::string recovery_img_sha1;
206 sha1sum(recovery_img, &recovery_img_sha1, &size);
207 std::string recovery_img_size = std::to_string(size);
208
209 std::string bonus_file = from_testdata_base("bonus.file");
210
211 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
212 TemporaryFile tmp1;
213 std::string src_file =
214 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
215 std::string tgt_file = "EMMC:" + std::string(tmp1.path);
216 std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p");
217 std::vector<const char*> args = {
218 "applypatch",
219 "-b",
220 bonus_file.c_str(),
221 src_file.c_str(),
222 tgt_file.c_str(),
223 recovery_img_sha1.c_str(),
224 recovery_img_size.c_str(),
225 patch.c_str()
226 };
227 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
228
229 // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
230 TemporaryFile tmp2;
231 patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
232 tgt_file = "EMMC:" + std::string(tmp2.path);
233 std::vector<const char*> args2 = {
234 "applypatch",
235 src_file.c_str(),
236 tgt_file.c_str(),
237 recovery_img_sha1.c_str(),
238 recovery_img_size.c_str(),
239 patch.c_str()
240 };
241 ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data()));
242
243 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \
244 // <src-sha1-fake>:<patch1> <src-sha1>:<patch2>
245 TemporaryFile tmp3;
246 tgt_file = "EMMC:" + std::string(tmp3.path);
247 std::string bad_sha1_a = android::base::StringPrintf("%040x", rand());
248 std::string bad_sha1_b = android::base::StringPrintf("%040x", rand());
249 std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p");
250 std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p");
251 std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p");
252 std::vector<const char*> args3 = {
253 "applypatch",
254 "-b",
255 bonus_file.c_str(),
256 src_file.c_str(),
257 tgt_file.c_str(),
258 recovery_img_sha1.c_str(),
259 recovery_img_size.c_str(),
260 patch1.c_str(),
261 patch2.c_str(),
262 patch3.c_str()
263 };
264 ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data()));
265}
266
Tianjie Xua88cc542017-10-25 13:16:54 -0700267TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700268 // Invalid bonus file.
269 ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" }));
270
271 std::string bonus_file = from_testdata_base("bonus.file");
272 // With bonus file, but missing args.
273 ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() }));
274
275 std::string boot_img = from_testdata_base("boot.img");
276 size_t boot_img_size;
277 std::string boot_img_sha1;
278 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
279
280 std::string recovery_img = from_testdata_base("recovery.img");
Tao Bao8dd44e92016-11-21 11:16:56 -0800281 size_t size;
Tao Bao36c35112016-10-25 14:17:26 -0700282 std::string recovery_img_sha1;
Tao Bao8dd44e92016-11-21 11:16:56 -0800283 sha1sum(recovery_img, &recovery_img_sha1, &size);
284 std::string recovery_img_size = std::to_string(size);
Tao Bao36c35112016-10-25 14:17:26 -0700285
286 // Bonus file is not supported in flash mode.
287 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size>
288 TemporaryFile tmp4;
289 std::vector<const char*> args4 = {
290 "applypatch",
291 "-b",
292 bonus_file.c_str(),
293 boot_img.c_str(),
294 tmp4.path,
295 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800296 recovery_img_size.c_str()
297 };
Tao Bao36c35112016-10-25 14:17:26 -0700298 ASSERT_NE(0, applypatch_modes(args4.size(), args4.data()));
299
300 // Failed to parse patch args.
301 TemporaryFile tmp5;
Tao Bao8dd44e92016-11-21 11:16:56 -0800302 std::string bad_arg1 =
303 "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700304 std::vector<const char*> args5 = {
305 "applypatch",
306 boot_img.c_str(),
307 tmp5.path,
308 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800309 recovery_img_size.c_str(),
310 bad_arg1.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700311 };
312 ASSERT_NE(0, applypatch_modes(args5.size(), args5.data()));
313
314 // Target size cannot be zero.
315 TemporaryFile tmp6;
Tao Bao8dd44e92016-11-21 11:16:56 -0800316 std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700317 std::vector<const char*> args6 = {
318 "applypatch",
319 boot_img.c_str(),
320 tmp6.path,
321 recovery_img_sha1.c_str(),
322 "0", // target size
Tao Bao8dd44e92016-11-21 11:16:56 -0800323 patch.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700324 };
325 ASSERT_NE(0, applypatch_modes(args6.size(), args6.data()));
326}
327
Tianjie Xua88cc542017-10-25 13:16:54 -0700328TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700329 // Insufficient args.
330 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" }));
331}
332
Tianjie Xua88cc542017-10-25 13:16:54 -0700333TEST_F(ApplyPatchModesTest, ShowLicenses) {
Tao Bao36c35112016-10-25 14:17:26 -0700334 ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" }));
Jed Estepb8a693b2016-03-09 17:51:34 -0800335}