• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "bsdiff/patch_writer.h"
6 
7 #include <gtest/gtest.h>
8 
9 #include "bsdiff/test_utils.h"
10 
11 namespace {
12 
13 // Generated with:
14 // echo 'Hello World' | hexdump -v -e '"    " 12/1 "0x%02x, " "\n"'
15 const uint8_t kHelloWorld[] = {
16     0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a,
17 };
18 
19 // Compressed empty file.
20 // bzip2 -9 </dev/null | hexdump -v -e '"    " 7/1 "0x%02x, " "\n"'
21 const uint8_t kCompressedEmpty[] = {0x42, 0x5a, 0x68, 0x39, 0x17, 0x72, 0x45,
22                                     0x38, 0x50, 0x90, 0x00, 0x00, 0x00, 0x00};
23 
24 // echo 'Hello World' | bzip2 -9 | hexdump -v -e '"    " 12/1 "0x%02x, " "\n"'
25 const uint8_t kCompressedHelloWorld[] = {
26     0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xd8, 0x72,
27     0x01, 0x2f, 0x00, 0x00, 0x01, 0x57, 0x80, 0x00, 0x10, 0x40, 0x00, 0x00,
28     0x40, 0x00, 0x80, 0x06, 0x04, 0x90, 0x00, 0x20, 0x00, 0x22, 0x06, 0x86,
29     0xd4, 0x20, 0xc9, 0x88, 0xc7, 0x69, 0xe8, 0x28, 0x1f, 0x8b, 0xb9, 0x22,
30     0x9c, 0x28, 0x48, 0x6c, 0x39, 0x00, 0x97, 0x80,
31 };
32 
33 // Compressed a buffer of zeros of the same length of the kHelloWorld.
34 // head -c `echo 'Hello World' | wc -c` /dev/zero | bzip2 -9 |
35 //   hexdump -v -e '"    " 10/1 "0x%02x, " "\n"'
36 const uint8_t kCompressedZeros[] = {
37     0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59,
38     0xf6, 0x63, 0xab, 0xde, 0x00, 0x00, 0x00, 0x40, 0x00, 0x40,
39     0x40, 0x20, 0x00, 0x21, 0x00, 0x82, 0x83, 0x17, 0x72, 0x45,
40     0x38, 0x50, 0x90, 0xf6, 0x63, 0xab, 0xde,
41 };
42 
43 }  // namespace
44 
45 namespace bsdiff {
46 
47 class BsdiffPatchWriterTest : public testing::Test {
48  protected:
SetUp()49   void SetUp() override {
50     // This patch writer doesn't use the |new_size| value passed on init, so we
51     // don't pass any meaningful value.
52     EXPECT_TRUE(patch_writer_.Init(0));
53   }
54 
55   test_utils::ScopedTempFile patch_file_{"bsdiff_newfile.XXXXXX"};
56   BsdiffPatchWriter patch_writer_{patch_file_.filename()};
57 };
58 
TEST_F(BsdiffPatchWriterTest,CreateEmptyPatchTest)59 TEST_F(BsdiffPatchWriterTest, CreateEmptyPatchTest) {
60   EXPECT_TRUE(patch_writer_.Close());
61 
62   test_utils::BsdiffPatchFile patch;
63   EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename()));
64   EXPECT_TRUE(patch.IsValid());
65 
66   // An empty bz2 file will have 14 bytes.
67   EXPECT_EQ(sizeof(kCompressedEmpty), static_cast<uint64_t>(patch.diff_len));
68   EXPECT_EQ(sizeof(kCompressedEmpty), patch.extra_len);
69 }
70 
TEST_F(BsdiffPatchWriterTest,AllInExtraStreamTest)71 TEST_F(BsdiffPatchWriterTest, AllInExtraStreamTest) {
72   // Write to the extra stream in two parts: first 5 bytes, then the rest.
73   EXPECT_TRUE(patch_writer_.AddControlEntry(ControlEntry(0, 5, 0)));
74   EXPECT_TRUE(patch_writer_.AddControlEntry(
75       ControlEntry(0, sizeof(kHelloWorld) - 5, 0)));
76   EXPECT_TRUE(patch_writer_.WriteExtraStream(kHelloWorld, sizeof(kHelloWorld)));
77   EXPECT_TRUE(patch_writer_.Close());
78 
79   test_utils::BsdiffPatchFile patch;
80   EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename()));
81   EXPECT_TRUE(patch.IsValid());
82   EXPECT_EQ(patch.bz2_diff,
83             std::vector<uint8_t>(kCompressedEmpty,
84                                  kCompressedEmpty + sizeof(kCompressedEmpty)));
85   EXPECT_EQ(patch.bz2_extra,
86             std::vector<uint8_t>(
87                 kCompressedHelloWorld,
88                 kCompressedHelloWorld + sizeof(kCompressedHelloWorld)));
89 
90   EXPECT_EQ(static_cast<int64_t>(sizeof(kHelloWorld)), patch.new_file_len);
91 }
92 
TEST_F(BsdiffPatchWriterTest,AllInDiffStreamTest)93 TEST_F(BsdiffPatchWriterTest, AllInDiffStreamTest) {
94   // Write to the extra stream in two parts: first 5 bytes, then the rest.
95   EXPECT_TRUE(
96       patch_writer_.AddControlEntry(ControlEntry(sizeof(kHelloWorld), 0, 0)));
97   std::vector<uint8_t> zeros(sizeof(kHelloWorld), 0);
98   EXPECT_TRUE(patch_writer_.WriteDiffStream(zeros.data(), zeros.size()));
99   EXPECT_TRUE(patch_writer_.Close());
100 
101   test_utils::BsdiffPatchFile patch;
102   EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename()));
103   EXPECT_TRUE(patch.IsValid());
104   EXPECT_EQ(patch.bz2_extra,
105             std::vector<uint8_t>(kCompressedEmpty,
106                                  kCompressedEmpty + sizeof(kCompressedEmpty)));
107   EXPECT_EQ(patch.bz2_diff,
108             std::vector<uint8_t>(kCompressedZeros,
109                                  kCompressedZeros + sizeof(kCompressedZeros)));
110 
111   EXPECT_EQ(static_cast<int64_t>(sizeof(kHelloWorld)), patch.new_file_len);
112 }
113 
114 }  // namespace bsdiff
115