blob: b6d0925579839034b66c1012bb0c33a2230ea750 [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"
Tianjie Xu3bbb20f2018-02-27 15:56:11 -080038#include "otautil/cache_location.h"
Tao Bao09e468f2017-09-29 14:39:33 -070039#include "otautil/print_sha1.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080040
Tao Bao36c35112016-10-25 14:17:26 -070041static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
42 ASSERT_NE(nullptr, sha1);
Tao Baofada91c2016-10-27 18:16:06 -070043
Tao Bao36c35112016-10-25 14:17:26 -070044 std::string data;
45 ASSERT_TRUE(android::base::ReadFileToString(fname, &data));
Jed Estepb8a693b2016-03-09 17:51:34 -080046
Tao Bao36c35112016-10-25 14:17:26 -070047 if (fsize != nullptr) {
48 *fsize = data.size();
49 }
50
51 uint8_t digest[SHA_DIGEST_LENGTH];
52 SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
53 *sha1 = print_sha1(digest);
Jed Estepb8a693b2016-03-09 17:51:34 -080054}
55
56static void mangle_file(const std::string& fname) {
Tianjie Xua88cc542017-10-25 13:16:54 -070057 std::string content(1024, '\0');
Tao Bao36c35112016-10-25 14:17:26 -070058 for (size_t i = 0; i < 1024; i++) {
59 content[i] = rand() % 256;
60 }
61 ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
Jed Estepb8a693b2016-03-09 17:51:34 -080062}
63
Jed Estepb8a693b2016-03-09 17:51:34 -080064class ApplyPatchTest : public ::testing::Test {
Tao Bao36c35112016-10-25 14:17:26 -070065 public:
Tianjie Xua88cc542017-10-25 13:16:54 -070066 virtual void SetUp() override {
Tao Bao36c35112016-10-25 14:17:26 -070067 // set up files
68 old_file = from_testdata_base("old.file");
69 new_file = from_testdata_base("new.file");
Tianjie Xua88cc542017-10-25 13:16:54 -070070 nonexistent_file = from_testdata_base("nonexistent.file");
Jed Estepb8a693b2016-03-09 17:51:34 -080071
Tao Bao36c35112016-10-25 14:17:26 -070072 // set up SHA constants
Tao Bao8dd44e92016-11-21 11:16:56 -080073 sha1sum(old_file, &old_sha1, &old_size);
74 sha1sum(new_file, &new_sha1, &new_size);
Tao Bao36c35112016-10-25 14:17:26 -070075 srand(time(nullptr));
76 bad_sha1_a = android::base::StringPrintf("%040x", rand());
77 bad_sha1_b = android::base::StringPrintf("%040x", rand());
Tao Bao36c35112016-10-25 14:17:26 -070078 }
Jed Estepb8a693b2016-03-09 17:51:34 -080079
Tianjie Xua88cc542017-10-25 13:16:54 -070080 std::string old_file;
81 std::string new_file;
82 std::string nonexistent_file;
Jed Estepb8a693b2016-03-09 17:51:34 -080083
Tianjie Xua88cc542017-10-25 13:16:54 -070084 std::string old_sha1;
85 std::string new_sha1;
86 std::string bad_sha1_a;
87 std::string bad_sha1_b;
Jed Estepb8a693b2016-03-09 17:51:34 -080088
Tianjie Xua88cc542017-10-25 13:16:54 -070089 size_t old_size;
90 size_t new_size;
Jed Estepb8a693b2016-03-09 17:51:34 -080091};
92
Jed Estepb8a693b2016-03-09 17:51:34 -080093class ApplyPatchCacheTest : public ApplyPatchTest {
Tianjie Xua88cc542017-10-25 13:16:54 -070094 protected:
95 void SetUp() override {
96 ApplyPatchTest::SetUp();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -080097 CacheLocation::location().set_cache_temp_source(old_file);
Tao Bao36c35112016-10-25 14:17:26 -070098 }
Jed Estepb8a693b2016-03-09 17:51:34 -080099};
100
Tianjie Xua88cc542017-10-25 13:16:54 -0700101class ApplyPatchModesTest : public ::testing::Test {
102 protected:
103 void SetUp() override {
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800104 CacheLocation::location().set_cache_temp_source(cache_source.path);
Tianjie Xua88cc542017-10-25 13:16:54 -0700105 }
106
107 TemporaryFile cache_source;
108};
Jed Estepb8a693b2016-03-09 17:51:34 -0800109
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700110TEST_F(ApplyPatchTest, CheckModeSkip) {
Tao Bao36c35112016-10-25 14:17:26 -0700111 std::vector<std::string> sha1s;
112 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Tianjie Xua5fd5ab2016-10-18 15:18:22 -0700113}
114
Jed Estepb8a693b2016-03-09 17:51:34 -0800115TEST_F(ApplyPatchTest, CheckModeSingle) {
Tao Bao36c35112016-10-25 14:17:26 -0700116 std::vector<std::string> sha1s = { old_sha1 };
117 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800118}
119
120TEST_F(ApplyPatchTest, CheckModeMultiple) {
Tao Bao36c35112016-10-25 14:17:26 -0700121 std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
122 ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800123}
124
125TEST_F(ApplyPatchTest, CheckModeFailure) {
Tao Bao36c35112016-10-25 14:17:26 -0700126 std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
127 ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
Jed Estepb8a693b2016-03-09 17:51:34 -0800128}
129
Tao Bao8dd44e92016-11-21 11:16:56 -0800130TEST_F(ApplyPatchTest, CheckModeEmmcTarget) {
131 // EMMC:old_file:size:sha1 should pass the check.
132 std::string src_file =
133 "EMMC:" + old_file + ":" + std::to_string(old_size) + ":" + old_sha1;
134 std::vector<std::string> sha1s;
135 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
136
137 // EMMC:old_file:(size-1):sha1:(size+1):sha1 should fail the check.
138 src_file = "EMMC:" + old_file + ":" + std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
139 std::to_string(old_size + 1) + ":" + old_sha1;
140 ASSERT_EQ(1, applypatch_check(src_file.c_str(), sha1s));
141
142 // EMMC:old_file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
143 src_file = "EMMC:" + old_file + ":" +
144 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
145 std::to_string(old_size) + ":" + old_sha1 + ":" +
146 std::to_string(old_size + 1) + ":" + old_sha1;
147 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
148
149 // EMMC:old_file:(size+1):sha1:(size-1):sha1:size:sha1 should pass the check.
150 src_file = "EMMC:" + old_file + ":" +
151 std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
152 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
153 std::to_string(old_size) + ":" + old_sha1;
154 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
155
156 // EMMC:new_file:(size+1):old_sha1:(size-1):old_sha1:size:old_sha1:size:new_sha1
157 // should pass the check.
158 src_file = "EMMC:" + new_file + ":" +
159 std::to_string(old_size + 1) + ":" + old_sha1 + ":" +
160 std::to_string(old_size - 1) + ":" + old_sha1 + ":" +
161 std::to_string(old_size) + ":" + old_sha1 + ":" +
162 std::to_string(new_size) + ":" + new_sha1;
163 ASSERT_EQ(0, applypatch_check(src_file.c_str(), sha1s));
164}
165
Tianjie Xua88cc542017-10-25 13:16:54 -0700166TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceSingle) {
167 TemporaryFile temp_file;
168 mangle_file(temp_file.path);
169 std::vector<std::string> sha1s_single = { old_sha1 };
170 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_single));
171 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_single));
Jed Estepb8a693b2016-03-09 17:51:34 -0800172}
173
Tianjie Xua88cc542017-10-25 13:16:54 -0700174TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceMultiple) {
175 TemporaryFile temp_file;
176 mangle_file(temp_file.path);
177 std::vector<std::string> sha1s_multiple = { bad_sha1_a, old_sha1, bad_sha1_b };
178 ASSERT_EQ(0, applypatch_check(temp_file.path, sha1s_multiple));
179 ASSERT_EQ(0, applypatch_check(nonexistent_file.c_str(), sha1s_multiple));
Jed Estepb8a693b2016-03-09 17:51:34 -0800180}
181
Tianjie Xua88cc542017-10-25 13:16:54 -0700182TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSourceFailure) {
183 TemporaryFile temp_file;
184 mangle_file(temp_file.path);
185 std::vector<std::string> sha1s_failure = { bad_sha1_a, bad_sha1_b };
186 ASSERT_NE(0, applypatch_check(temp_file.path, sha1s_failure));
187 ASSERT_NE(0, applypatch_check(nonexistent_file.c_str(), sha1s_failure));
Jed Estepb8a693b2016-03-09 17:51:34 -0800188}
189
Tianjie Xua88cc542017-10-25 13:16:54 -0700190TEST_F(ApplyPatchModesTest, InvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700191 // At least two args (including the filename).
192 ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" }));
193
194 // Unrecognized args.
195 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" }));
196}
197
Tianjie Xua88cc542017-10-25 13:16:54 -0700198TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) {
Tao Bao8dd44e92016-11-21 11:16:56 -0800199 std::string boot_img = from_testdata_base("boot.img");
200 size_t boot_img_size;
201 std::string boot_img_sha1;
202 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
203
204 std::string recovery_img = from_testdata_base("recovery.img");
205 size_t size;
206 std::string recovery_img_sha1;
207 sha1sum(recovery_img, &recovery_img_sha1, &size);
208 std::string recovery_img_size = std::to_string(size);
209
210 std::string bonus_file = from_testdata_base("bonus.file");
211
212 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
213 TemporaryFile tmp1;
214 std::string src_file =
215 "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1;
216 std::string tgt_file = "EMMC:" + std::string(tmp1.path);
217 std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p");
218 std::vector<const char*> args = {
219 "applypatch",
220 "-b",
221 bonus_file.c_str(),
222 src_file.c_str(),
223 tgt_file.c_str(),
224 recovery_img_sha1.c_str(),
225 recovery_img_size.c_str(),
226 patch.c_str()
227 };
228 ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));
229
230 // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
231 TemporaryFile tmp2;
232 patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
233 tgt_file = "EMMC:" + std::string(tmp2.path);
234 std::vector<const char*> args2 = {
235 "applypatch",
236 src_file.c_str(),
237 tgt_file.c_str(),
238 recovery_img_sha1.c_str(),
239 recovery_img_size.c_str(),
240 patch.c_str()
241 };
242 ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data()));
243
244 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \
245 // <src-sha1-fake>:<patch1> <src-sha1>:<patch2>
246 TemporaryFile tmp3;
247 tgt_file = "EMMC:" + std::string(tmp3.path);
248 std::string bad_sha1_a = android::base::StringPrintf("%040x", rand());
249 std::string bad_sha1_b = android::base::StringPrintf("%040x", rand());
250 std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p");
251 std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p");
252 std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p");
253 std::vector<const char*> args3 = {
254 "applypatch",
255 "-b",
256 bonus_file.c_str(),
257 src_file.c_str(),
258 tgt_file.c_str(),
259 recovery_img_sha1.c_str(),
260 recovery_img_size.c_str(),
261 patch1.c_str(),
262 patch2.c_str(),
263 patch3.c_str()
264 };
265 ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data()));
266}
267
Tianjie Xua88cc542017-10-25 13:16:54 -0700268TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700269 // Invalid bonus file.
270 ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" }));
271
272 std::string bonus_file = from_testdata_base("bonus.file");
273 // With bonus file, but missing args.
274 ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() }));
275
276 std::string boot_img = from_testdata_base("boot.img");
277 size_t boot_img_size;
278 std::string boot_img_sha1;
279 sha1sum(boot_img, &boot_img_sha1, &boot_img_size);
280
281 std::string recovery_img = from_testdata_base("recovery.img");
Tao Bao8dd44e92016-11-21 11:16:56 -0800282 size_t size;
Tao Bao36c35112016-10-25 14:17:26 -0700283 std::string recovery_img_sha1;
Tao Bao8dd44e92016-11-21 11:16:56 -0800284 sha1sum(recovery_img, &recovery_img_sha1, &size);
285 std::string recovery_img_size = std::to_string(size);
Tao Bao36c35112016-10-25 14:17:26 -0700286
287 // Bonus file is not supported in flash mode.
288 // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size>
289 TemporaryFile tmp4;
290 std::vector<const char*> args4 = {
291 "applypatch",
292 "-b",
293 bonus_file.c_str(),
294 boot_img.c_str(),
295 tmp4.path,
296 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800297 recovery_img_size.c_str()
298 };
Tao Bao36c35112016-10-25 14:17:26 -0700299 ASSERT_NE(0, applypatch_modes(args4.size(), args4.data()));
300
301 // Failed to parse patch args.
302 TemporaryFile tmp5;
Tao Bao8dd44e92016-11-21 11:16:56 -0800303 std::string bad_arg1 =
304 "invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700305 std::vector<const char*> args5 = {
306 "applypatch",
307 boot_img.c_str(),
308 tmp5.path,
309 recovery_img_sha1.c_str(),
Tao Bao8dd44e92016-11-21 11:16:56 -0800310 recovery_img_size.c_str(),
311 bad_arg1.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700312 };
313 ASSERT_NE(0, applypatch_modes(args5.size(), args5.data()));
314
315 // Target size cannot be zero.
316 TemporaryFile tmp6;
Tao Bao8dd44e92016-11-21 11:16:56 -0800317 std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p");
Tao Bao36c35112016-10-25 14:17:26 -0700318 std::vector<const char*> args6 = {
319 "applypatch",
320 boot_img.c_str(),
321 tmp6.path,
322 recovery_img_sha1.c_str(),
323 "0", // target size
Tao Bao8dd44e92016-11-21 11:16:56 -0800324 patch.c_str()
Tao Bao36c35112016-10-25 14:17:26 -0700325 };
326 ASSERT_NE(0, applypatch_modes(args6.size(), args6.data()));
327}
328
Tianjie Xua88cc542017-10-25 13:16:54 -0700329TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
Tao Bao36c35112016-10-25 14:17:26 -0700330 // Insufficient args.
331 ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" }));
332}
333
Tianjie Xua88cc542017-10-25 13:16:54 -0700334TEST_F(ApplyPatchModesTest, ShowLicenses) {
Tao Bao36c35112016-10-25 14:17:26 -0700335 ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" }));
Jed Estepb8a693b2016-03-09 17:51:34 -0800336}