blob: ab20388e5cf561247057e7e2a9e3d7f777b63474 [file] [log] [blame]
Alex Deymofb3b6322017-09-27 14:28:54 +02001// 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
11namespace {
12
13// Generated with:
14// echo 'Hello World' | hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
15const 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"'
21const 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"'
25const 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"'
36const 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
45namespace bsdiff {
46
47class BsdiffPatchWriterTest : public testing::Test {
48 protected:
Alex Deymo4dadd8b2017-10-26 16:19:33 +020049 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 }
Alex Deymo68c0e7f2017-10-02 20:38:12 +020054
Alex Deymofb3b6322017-09-27 14:28:54 +020055 test_utils::ScopedTempFile patch_file_{"bsdiff_newfile.XXXXXX"};
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080056 BsdiffPatchWriter patch_writer_{patch_file_.filename()};
Alex Deymofb3b6322017-09-27 14:28:54 +020057};
58
59TEST_F(BsdiffPatchWriterTest, CreateEmptyPatchTest) {
Alex Deymofb3b6322017-09-27 14:28:54 +020060 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
71TEST_F(BsdiffPatchWriterTest, AllInExtraStreamTest) {
Alex Deymofb3b6322017-09-27 14:28:54 +020072 // 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)));
Alex Deymo68c0e7f2017-10-02 20:38:12 +020076 EXPECT_TRUE(patch_writer_.WriteExtraStream(kHelloWorld, sizeof(kHelloWorld)));
Alex Deymofb3b6322017-09-27 14:28:54 +020077 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
93TEST_F(BsdiffPatchWriterTest, AllInDiffStreamTest) {
Alex Deymofb3b6322017-09-27 14:28:54 +020094 // 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)));
Alex Deymo68c0e7f2017-10-02 20:38:12 +020097 std::vector<uint8_t> zeros(sizeof(kHelloWorld), 0);
98 EXPECT_TRUE(patch_writer_.WriteDiffStream(zeros.data(), zeros.size()));
Alex Deymofb3b6322017-09-27 14:28:54 +020099 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