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