blob: 61b63b7c7cd58de4b6f10a460407dd8e2f2b410e [file] [log] [blame]
Tianjie Xu65288122017-10-13 15:10:58 -07001// 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
Tianjie Xub4cba642017-11-14 22:46:38 -08005#include "bsdiff/patch_reader.h"
6
Tianjie Xu65288122017-10-13 15:10:58 -07007#include <unistd.h>
8
9#include <algorithm>
10#include <string>
11#include <vector>
12
13#include <gtest/gtest.h>
14
Tianjie Xub4cba642017-11-14 22:46:38 -080015#include "bsdiff/brotli_compressor.h"
Tianjie Xu65288122017-10-13 15:10:58 -070016#include "bsdiff/bz2_compressor.h"
Tianjie Xu65288122017-10-13 15:10:58 -070017#include "bsdiff/utils.h"
18
19namespace {
20
21void EncodeInt64(int64_t x, uint8_t* buf) {
22 uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
23 for (int i = 0; i < 8; ++i) {
24 buf[i] = y & 0xff;
25 y /= 256;
26 }
27}
28
29} // namespace
30
31namespace bsdiff {
32
Tianjie Xub4cba642017-11-14 22:46:38 -080033class PatchReaderTest : public testing::Test {
34 protected:
35 void CompressData() {
36 for (size_t i = 0; i < diff_data_.size(); i++) {
37 uint8_t buf[24];
38 EncodeInt64(diff_data_[i].size(), buf);
39 EncodeInt64(extra_data_[i].size(), buf + 8);
40 EncodeInt64(offset_increment_[i], buf + 16);
41 EXPECT_TRUE(ctrl_stream_->Write(buf, sizeof(buf)));
42 EXPECT_TRUE(diff_stream_->Write(
43 reinterpret_cast<const uint8_t*>(diff_data_[i].data()),
44 diff_data_[i].size()));
45 EXPECT_TRUE(extra_stream_->Write(
46 reinterpret_cast<const uint8_t*>(extra_data_[i].data()),
47 extra_data_[i].size()));
48 }
49 EXPECT_TRUE(ctrl_stream_->Finish());
50 EXPECT_TRUE(diff_stream_->Finish());
51 EXPECT_TRUE(extra_stream_->Finish());
Tianjie Xu65288122017-10-13 15:10:58 -070052 }
Tianjie Xu65288122017-10-13 15:10:58 -070053
Tianjie Xub4cba642017-11-14 22:46:38 -080054 void ConstructPatchData(std::vector<uint8_t>* patch_data) {
55 EXPECT_EQ(static_cast<size_t>(8), patch_data->size());
56 // Encode the header
57 uint8_t buf[24];
58 EncodeInt64(ctrl_stream_->GetCompressedData().size(), buf);
59 EncodeInt64(diff_stream_->GetCompressedData().size(), buf + 8);
60 EncodeInt64(new_file_size_, buf + 16);
61 std::copy(buf, buf + sizeof(buf), std::back_inserter(*patch_data));
62
63 // Concatenate the three streams into one patch.
64 std::copy(ctrl_stream_->GetCompressedData().begin(),
65 ctrl_stream_->GetCompressedData().end(),
66 std::back_inserter(*patch_data));
67 std::copy(diff_stream_->GetCompressedData().begin(),
68 diff_stream_->GetCompressedData().end(),
69 std::back_inserter(*patch_data));
70 std::copy(extra_stream_->GetCompressedData().begin(),
71 extra_stream_->GetCompressedData().end(),
72 std::back_inserter(*patch_data));
73 }
74
75
76 void VerifyPatch(const std::vector<uint8_t>& patch_data) {
77 BsdiffPatchReader patch_reader;
78 EXPECT_TRUE(patch_reader.Init(patch_data.data(), patch_data.size()));
79 EXPECT_EQ(new_file_size_, patch_reader.new_file_size());
80 // Check that the decompressed data matches what we wrote.
81 for (size_t i = 0; i < diff_data_.size(); i++) {
82 ControlEntry control_entry(0, 0, 0);
83 EXPECT_TRUE(patch_reader.ParseControlEntry(&control_entry));
84 EXPECT_EQ(diff_data_[i].size(), control_entry.diff_size);
85 EXPECT_EQ(extra_data_[i].size(), control_entry.extra_size);
86 EXPECT_EQ(offset_increment_[i], control_entry.offset_increment);
87
88 uint8_t buffer[128] = {};
89 EXPECT_TRUE(patch_reader.ReadDiffStream(buffer, diff_data_[i].size()));
90 EXPECT_EQ(0, memcmp(buffer, diff_data_[i].data(), diff_data_[i].size()));
91 EXPECT_TRUE(patch_reader.ReadExtraStream(buffer, extra_data_[i].size()));
92 EXPECT_EQ(0,
93 memcmp(buffer, extra_data_[i].data(), extra_data_[i].size()));
94 }
95 EXPECT_TRUE(patch_reader.Finish());
96 }
97
98 size_t new_file_size_{500};
99 std::vector<std::string> diff_data_{"HelloWorld", "BspatchPatchTest",
100 "BspatchDiffData"};
101 std::vector<std::string> extra_data_{"HelloWorld!", "BZ2PatchReaderSmoke",
102 "BspatchExtraData"};
103 std::vector<int64_t> offset_increment_{100, 200, 300};
104
105 // The compressor streams.
106 std::unique_ptr<CompressorInterface> ctrl_stream_{nullptr};
107 std::unique_ptr<CompressorInterface> diff_stream_{nullptr};
108 std::unique_ptr<CompressorInterface> extra_stream_{nullptr};
109};
110
111TEST_F(PatchReaderTest, BZ2PatchReaderLegacyFormatSmoke) {
112 ctrl_stream_.reset(new BZ2Compressor());
113 diff_stream_.reset(new BZ2Compressor());
114 extra_stream_.reset(new BZ2Compressor());
115
116 CompressData();
Tianjie Xu65288122017-10-13 15:10:58 -0700117
118 std::vector<uint8_t> patch_data;
Tianjie Xub4cba642017-11-14 22:46:38 -0800119 std::copy(kLegacyMagicHeader, kLegacyMagicHeader + 8,
120 std::back_inserter(patch_data));
121 ConstructPatchData(&patch_data);
Tianjie Xu65288122017-10-13 15:10:58 -0700122
Tianjie Xub4cba642017-11-14 22:46:38 -0800123 VerifyPatch(patch_data);
124}
Tianjie Xu65288122017-10-13 15:10:58 -0700125
Tianjie Xub4cba642017-11-14 22:46:38 -0800126TEST_F(PatchReaderTest, BZ2PatchReaderNewFormatSmoke) {
127 // Compress the data with one bz2 and two brotli compressors.
128 ctrl_stream_.reset(new BZ2Compressor());
129 diff_stream_.reset(new BrotliCompressor());
130 extra_stream_.reset(new BrotliCompressor());
131
132 CompressData();
133
134 std::vector<uint8_t> patch_data;
135 std::copy(kBSDF2MagicHeader, kBSDF2MagicHeader + 5,
136 std::back_inserter(patch_data));
137 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBZ2));
138 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
139 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
140 ConstructPatchData(&patch_data);
141
142 VerifyPatch(patch_data);
Tianjie Xu65288122017-10-13 15:10:58 -0700143}
144
145} // namespace bsdiff