aboutsummaryrefslogtreecommitdiffstats
blob: 1a0b19113370864049b9c1dc6fd7d5d0cdc974dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agree to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <fcntl.h>
#include <gtest/gtest.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <time.h>

#include <memory>
#include <string>
#include <vector>

#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <android-base/test_utils.h>
#include <openssl/sha.h>

#include "applypatch/applypatch.h"
#include "applypatch/applypatch_modes.h"
#include "common/test_constants.h"
#include "print_sha1.h"

static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
  ASSERT_NE(nullptr, sha1);

  std::string data;
  ASSERT_TRUE(android::base::ReadFileToString(fname, &data));

  if (fsize != nullptr) {
    *fsize = data.size();
  }

  uint8_t digest[SHA_DIGEST_LENGTH];
  SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
  *sha1 = print_sha1(digest);
}

static void mangle_file(const std::string& fname) {
  std::string content;
  content.reserve(1024);
  for (size_t i = 0; i < 1024; i++) {
    content[i] = rand() % 256;
  }
  ASSERT_TRUE(android::base::WriteStringToFile(content, fname));
}

static bool file_cmp(const std::string& f1, const std::string& f2) {
  std::string c1;
  android::base::ReadFileToString(f1, &c1);
  std::string c2;
  android::base::ReadFileToString(f2, &c2);
  return c1 == c2;
}

class ApplyPatchTest : public ::testing::Test {
 public:
  static void SetUpTestCase() {
    // set up files
    old_file = from_testdata_base("old.file");
    new_file = from_testdata_base("new.file");
    patch_file = from_testdata_base("patch.bsdiff");
    rand_file = "/cache/applypatch_test_rand.file";
    cache_file = "/cache/saved.file";

    // write stuff to rand_file
    ASSERT_TRUE(android::base::WriteStringToFile("hello", rand_file));

    // set up SHA constants
    sha1sum(old_file, &old_sha1);
    sha1sum(new_file, &new_sha1);
    srand(time(nullptr));
    bad_sha1_a = android::base::StringPrintf("%040x", rand());
    bad_sha1_b = android::base::StringPrintf("%040x", rand());

    struct stat st;
    stat(&new_file[0], &st);
    new_size = st.st_size;
  }

  static std::string old_file;
  static std::string new_file;
  static std::string rand_file;
  static std::string cache_file;
  static std::string patch_file;

  static std::string old_sha1;
  static std::string new_sha1;
  static std::string bad_sha1_a;
  static std::string bad_sha1_b;

  static size_t new_size;
};

std::string ApplyPatchTest::old_file;
std::string ApplyPatchTest::new_file;

static void cp(const std::string& src, const std::string& tgt) {
  std::string cmd = "cp " + src + " " + tgt;
  system(cmd.c_str());
}

static void backup_old() {
  cp(ApplyPatchTest::old_file, ApplyPatchTest::cache_file);
}

static void restore_old() {
  cp(ApplyPatchTest::cache_file, ApplyPatchTest::old_file);
}

class ApplyPatchCacheTest : public ApplyPatchTest {
 public:
  virtual void SetUp() {
    backup_old();
  }

  virtual void TearDown() {
    restore_old();
  }
};

class ApplyPatchFullTest : public ApplyPatchCacheTest {
 public:
  static void SetUpTestCase() {
    ApplyPatchTest::SetUpTestCase();

    output_f = new TemporaryFile();
    output_loc = std::string(output_f->path);

    struct FileContents fc;

    ASSERT_EQ(0, LoadFileContents(&rand_file[0], &fc));
    patches.push_back(
        std::make_unique<Value>(VAL_BLOB, std::string(fc.data.begin(), fc.data.end())));

    ASSERT_EQ(0, LoadFileContents(&patch_file[0], &fc));
    patches.push_back(
        std::make_unique<Value>(VAL_BLOB, std::string(fc.data.begin(), fc.data.end())));
  }

  static void TearDownTestCase() {
    delete output_f;
    patches.clear();
  }

  static std::vector<std::unique_ptr<Value>> patches;
  static TemporaryFile* output_f;
  static std::string output_loc;
};

class ApplyPatchDoubleCacheTest : public ApplyPatchFullTest {
 public:
  virtual void SetUp() {
    ApplyPatchCacheTest::SetUp();
    cp(cache_file, "/cache/reallysaved.file");
  }

  virtual void TearDown() {
    cp("/cache/reallysaved.file", cache_file);
    ApplyPatchCacheTest::TearDown();
  }
};

std::string ApplyPatchTest::rand_file;
std::string ApplyPatchTest::patch_file;
std::string ApplyPatchTest::cache_file;
std::string ApplyPatchTest::old_sha1;
std::string ApplyPatchTest::new_sha1;
std::string ApplyPatchTest::bad_sha1_a;
std::string ApplyPatchTest::bad_sha1_b;

size_t ApplyPatchTest::new_size;

std::vector<std::unique_ptr<Value>> ApplyPatchFullTest::patches;
TemporaryFile* ApplyPatchFullTest::output_f;
std::string ApplyPatchFullTest::output_loc;

TEST_F(ApplyPatchTest, CheckModeSkip) {
  std::vector<std::string> sha1s;
  ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchTest, CheckModeSingle) {
  std::vector<std::string> sha1s = { old_sha1 };
  ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchTest, CheckModeMultiple) {
  std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
  ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchTest, CheckModeFailure) {
  std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
  ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedSingle) {
  mangle_file(old_file);
  std::vector<std::string> sha1s = { old_sha1 };
  ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedMultiple) {
  mangle_file(old_file);
  std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
  ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchCacheTest, CheckCacheCorruptedFailure) {
  mangle_file(old_file);
  std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
  ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchCacheTest, CheckCacheMissingSingle) {
  unlink(&old_file[0]);
  std::vector<std::string> sha1s = { old_sha1 };
  ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchCacheTest, CheckCacheMissingMultiple) {
  unlink(&old_file[0]);
  std::vector<std::string> sha1s = { bad_sha1_a, old_sha1, bad_sha1_b };
  ASSERT_EQ(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchCacheTest, CheckCacheMissingFailure) {
  unlink(&old_file[0]);
  std::vector<std::string> sha1s = { bad_sha1_a, bad_sha1_b };
  ASSERT_NE(0, applypatch_check(&old_file[0], sha1s));
}

TEST_F(ApplyPatchFullTest, ApplyInPlace) {
  std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 };
  ASSERT_EQ(0, applypatch(&old_file[0], "-", &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_TRUE(file_cmp(old_file, new_file));

  // reapply, applypatch is idempotent so it should succeed
  ASSERT_EQ(0, applypatch(&old_file[0], "-", &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_TRUE(file_cmp(old_file, new_file));
}

TEST_F(ApplyPatchFullTest, ApplyInNewLocation) {
  std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 };
  // Apply bsdiff patch to new location.
  ASSERT_EQ(
      0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_TRUE(file_cmp(output_loc, new_file));

  // Reapply to the same location.
  ASSERT_EQ(
      0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_TRUE(file_cmp(output_loc, new_file));
}

TEST_F(ApplyPatchFullTest, ApplyCorruptedInNewLocation) {
  std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 };
  // Apply bsdiff patch to new location with corrupted source.
  mangle_file(old_file);
  ASSERT_EQ(
      0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_TRUE(file_cmp(output_loc, new_file));

  // Reapply bsdiff patch to new location with corrupted source.
  ASSERT_EQ(
      0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_TRUE(file_cmp(output_loc, new_file));
}

TEST_F(ApplyPatchDoubleCacheTest, ApplyDoubleCorruptedInNewLocation) {
  std::vector<std::string> sha1s = { bad_sha1_a, old_sha1 };

  // Apply bsdiff patch to new location with corrupted source and copy (no new file).
  // Expected to fail.
  mangle_file(old_file);
  mangle_file(cache_file);
  ASSERT_NE(
      0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_FALSE(file_cmp(output_loc, new_file));

  // Expected to fail again on retry.
  ASSERT_NE(
      0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_FALSE(file_cmp(output_loc, new_file));

  // Expected to fail with incorrect new file.
  mangle_file(output_loc);
  ASSERT_NE(
      0, applypatch(&old_file[0], &output_loc[0], &new_sha1[0], new_size, sha1s, patches, nullptr));
  ASSERT_FALSE(file_cmp(output_loc, new_file));
}

TEST(ApplyPatchModes, InvalidArgs) {
  // At least two args (including the filename).
  ASSERT_EQ(2, applypatch_modes(1, (const char* []){ "applypatch" }));

  // Unrecognized args.
  ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-x" }));
}

TEST(ApplyPatchModes, PatchMode) {
  std::string boot_img = from_testdata_base("boot.img");
  size_t boot_img_size;
  std::string boot_img_sha1;
  sha1sum(boot_img, &boot_img_sha1, &boot_img_size);

  std::string recovery_img = from_testdata_base("recovery.img");
  size_t recovery_img_size;
  std::string recovery_img_sha1;
  sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);

  std::string bonus_file = from_testdata_base("bonus.file");

  // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
  TemporaryFile tmp1;
  std::vector<const char*> args = {
    "applypatch",
    "-b",
    bonus_file.c_str(),
    boot_img.c_str(),
    tmp1.path,
    recovery_img_sha1.c_str(),
    std::to_string(recovery_img_size).c_str(),
    (boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p")).c_str()
  };
  ASSERT_EQ(0, applypatch_modes(args.size(), args.data()));

  // applypatch <src-file> <tgt-file> <tgt-sha1> <tgt-size> <src-sha1>:<patch>
  TemporaryFile tmp2;
  std::vector<const char*> args2 = {
    "applypatch",
    boot_img.c_str(),
    tmp2.path,
    recovery_img_sha1.c_str(),
    std::to_string(recovery_img_size).c_str(),
    (boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p")).c_str()
  };
  ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data()));

  // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size> \
    //               <src-sha1-fake>:<patch1> <src-sha1>:<patch2>
  TemporaryFile tmp3;
  std::string bad_sha1_a = android::base::StringPrintf("%040x", rand());
  std::string bad_sha1_b = android::base::StringPrintf("%040x", rand());
  std::vector<const char*> args3 = {
    "applypatch",
    "-b",
    bonus_file.c_str(),
    boot_img.c_str(),
    tmp3.path,
    recovery_img_sha1.c_str(),
    std::to_string(recovery_img_size).c_str(),
    (bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p")).c_str(),
    (boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p")).c_str(),
    (bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p")).c_str(),
  };
  ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data()));
}

TEST(ApplyPatchModes, PatchModeInvalidArgs) {
  // Invalid bonus file.
  ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" }));

  std::string bonus_file = from_testdata_base("bonus.file");
  // With bonus file, but missing args.
  ASSERT_EQ(2, applypatch_modes(3, (const char* []){ "applypatch", "-b", bonus_file.c_str() }));

  std::string boot_img = from_testdata_base("boot.img");
  size_t boot_img_size;
  std::string boot_img_sha1;
  sha1sum(boot_img, &boot_img_sha1, &boot_img_size);

  std::string recovery_img = from_testdata_base("recovery.img");
  size_t recovery_img_size;
  std::string recovery_img_sha1;
  sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size);

  // Bonus file is not supported in flash mode.
  // applypatch -b <bonus-file> <src-file> <tgt-file> <tgt-sha1> <tgt-size>
  TemporaryFile tmp4;
  std::vector<const char*> args4 = {
    "applypatch",
    "-b",
    bonus_file.c_str(),
    boot_img.c_str(),
    tmp4.path,
    recovery_img_sha1.c_str(),
    std::to_string(recovery_img_size).c_str() };
  ASSERT_NE(0, applypatch_modes(args4.size(), args4.data()));

  // Failed to parse patch args.
  TemporaryFile tmp5;
  std::vector<const char*> args5 = {
    "applypatch",
    boot_img.c_str(),
    tmp5.path,
    recovery_img_sha1.c_str(),
    std::to_string(recovery_img_size).c_str(),
    ("invalid-sha1:filename" + from_testdata_base("recovery-from-boot-with-bonus.p")).c_str(),
  };
  ASSERT_NE(0, applypatch_modes(args5.size(), args5.data()));

  // Target size cannot be zero.
  TemporaryFile tmp6;
  std::vector<const char*> args6 = {
    "applypatch",
    boot_img.c_str(),
    tmp6.path,
    recovery_img_sha1.c_str(),
    "0",  // target size
    (boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p")).c_str()
  };
  ASSERT_NE(0, applypatch_modes(args6.size(), args6.data()));
}

TEST(ApplyPatchModes, CheckModeInvalidArgs) {
  // Insufficient args.
  ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-c" }));
}

TEST(ApplyPatchModes, SpaceModeInvalidArgs) {
  // Insufficient args.
  ASSERT_EQ(2, applypatch_modes(2, (const char* []){ "applypatch", "-s" }));

  // Invalid bytes arg.
  ASSERT_EQ(1, applypatch_modes(3, (const char* []){ "applypatch", "-s", "x" }));

  // 0 is invalid.
  ASSERT_EQ(1, applypatch_modes(3, (const char* []){ "applypatch", "-s", "0" }));

  // 0x10 is fine.
  ASSERT_EQ(0, applypatch_modes(3, (const char* []){ "applypatch", "-s", "0x10" }));
}

TEST(ApplyPatchModes, ShowLicenses) {
  ASSERT_EQ(0, applypatch_modes(2, (const char* []){ "applypatch", "-l" }));
}