blob: 8de346d77225e82fe08d03cabe90d59cc619fee5 [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>
Alex Deymo7c4bb572018-03-19 20:25:08 +010010#include <limits>
Tianjie Xu65288122017-10-13 15:10:58 -070011#include <string>
12#include <vector>
13
14#include <gtest/gtest.h>
15
Tianjie Xub4cba642017-11-14 22:46:38 -080016#include "bsdiff/brotli_compressor.h"
Tianjie Xu65288122017-10-13 15:10:58 -070017#include "bsdiff/bz2_compressor.h"
Tianjie Xu65288122017-10-13 15:10:58 -070018#include "bsdiff/utils.h"
19
20namespace {
21
22void EncodeInt64(int64_t x, uint8_t* buf) {
23 uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
24 for (int i = 0; i < 8; ++i) {
25 buf[i] = y & 0xff;
26 y /= 256;
27 }
28}
29
30} // namespace
31
32namespace bsdiff {
33
Tianjie Xub4cba642017-11-14 22:46:38 -080034class PatchReaderTest : public testing::Test {
35 protected:
36 void CompressData() {
37 for (size_t i = 0; i < diff_data_.size(); i++) {
38 uint8_t buf[24];
39 EncodeInt64(diff_data_[i].size(), buf);
40 EncodeInt64(extra_data_[i].size(), buf + 8);
41 EncodeInt64(offset_increment_[i], buf + 16);
42 EXPECT_TRUE(ctrl_stream_->Write(buf, sizeof(buf)));
43 EXPECT_TRUE(diff_stream_->Write(
44 reinterpret_cast<const uint8_t*>(diff_data_[i].data()),
45 diff_data_[i].size()));
46 EXPECT_TRUE(extra_stream_->Write(
47 reinterpret_cast<const uint8_t*>(extra_data_[i].data()),
48 extra_data_[i].size()));
49 }
50 EXPECT_TRUE(ctrl_stream_->Finish());
51 EXPECT_TRUE(diff_stream_->Finish());
52 EXPECT_TRUE(extra_stream_->Finish());
Tianjie Xu65288122017-10-13 15:10:58 -070053 }
Tianjie Xu65288122017-10-13 15:10:58 -070054
Alex Deymo7c4bb572018-03-19 20:25:08 +010055 void ConstructPatchHeader(int64_t ctrl_size,
56 int64_t diff_size,
57 int64_t new_size,
58 std::vector<uint8_t>* patch_data) {
Tianjie Xub4cba642017-11-14 22:46:38 -080059 EXPECT_EQ(static_cast<size_t>(8), patch_data->size());
Alex Deymo7c4bb572018-03-19 20:25:08 +010060 // Encode the header.
Tianjie Xub4cba642017-11-14 22:46:38 -080061 uint8_t buf[24];
Alex Deymo7c4bb572018-03-19 20:25:08 +010062 EncodeInt64(ctrl_size, buf);
63 EncodeInt64(diff_size, buf + 8);
64 EncodeInt64(new_size, buf + 16);
Tianjie Xub4cba642017-11-14 22:46:38 -080065 std::copy(buf, buf + sizeof(buf), std::back_inserter(*patch_data));
Alex Deymo7c4bb572018-03-19 20:25:08 +010066 }
67
68 void ConstructPatchData(std::vector<uint8_t>* patch_data) {
69 ConstructPatchHeader(ctrl_stream_->GetCompressedData().size(),
70 diff_stream_->GetCompressedData().size(),
71 new_file_size_, patch_data);
Tianjie Xub4cba642017-11-14 22:46:38 -080072
73 // Concatenate the three streams into one patch.
74 std::copy(ctrl_stream_->GetCompressedData().begin(),
75 ctrl_stream_->GetCompressedData().end(),
76 std::back_inserter(*patch_data));
77 std::copy(diff_stream_->GetCompressedData().begin(),
78 diff_stream_->GetCompressedData().end(),
79 std::back_inserter(*patch_data));
80 std::copy(extra_stream_->GetCompressedData().begin(),
81 extra_stream_->GetCompressedData().end(),
82 std::back_inserter(*patch_data));
83 }
84
Tianjie Xub4cba642017-11-14 22:46:38 -080085 void VerifyPatch(const std::vector<uint8_t>& patch_data) {
86 BsdiffPatchReader patch_reader;
87 EXPECT_TRUE(patch_reader.Init(patch_data.data(), patch_data.size()));
88 EXPECT_EQ(new_file_size_, patch_reader.new_file_size());
89 // Check that the decompressed data matches what we wrote.
90 for (size_t i = 0; i < diff_data_.size(); i++) {
91 ControlEntry control_entry(0, 0, 0);
92 EXPECT_TRUE(patch_reader.ParseControlEntry(&control_entry));
93 EXPECT_EQ(diff_data_[i].size(), control_entry.diff_size);
94 EXPECT_EQ(extra_data_[i].size(), control_entry.extra_size);
95 EXPECT_EQ(offset_increment_[i], control_entry.offset_increment);
96
97 uint8_t buffer[128] = {};
98 EXPECT_TRUE(patch_reader.ReadDiffStream(buffer, diff_data_[i].size()));
99 EXPECT_EQ(0, memcmp(buffer, diff_data_[i].data(), diff_data_[i].size()));
100 EXPECT_TRUE(patch_reader.ReadExtraStream(buffer, extra_data_[i].size()));
101 EXPECT_EQ(0,
102 memcmp(buffer, extra_data_[i].data(), extra_data_[i].size()));
103 }
104 EXPECT_TRUE(patch_reader.Finish());
105 }
106
Alex Deymo7c4bb572018-03-19 20:25:08 +0100107 // Helper function to check that invalid headers are detected. This method
108 // creates a new header with the passed |ctrl_size|, |diff_size| and
109 // |new_size| and appends after the header |compressed_size| bytes of extra
110 // zeros. It then expects that initializing a PatchReader with this will fail.
111 void InvalidHeaderTestHelper(int64_t ctrl_size,
112 int64_t diff_size,
113 int64_t new_size,
114 size_t compressed_size) {
115 std::vector<uint8_t> patch_data;
116 std::copy(kBSDF2MagicHeader, kBSDF2MagicHeader + 5,
117 std::back_inserter(patch_data));
118 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
119 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
120 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
121 ConstructPatchHeader(ctrl_size, diff_size, new_size, &patch_data);
122 patch_data.resize(patch_data.size() + compressed_size);
123
124 BsdiffPatchReader patch_reader;
125 EXPECT_FALSE(patch_reader.Init(patch_data.data(), patch_data.size()))
126 << "Where ctrl_size=" << ctrl_size << " diff_size=" << diff_size
127 << " new_size=" << new_size << " compressed_size=" << compressed_size;
128 }
129
Tianjie Xub4cba642017-11-14 22:46:38 -0800130 size_t new_file_size_{500};
131 std::vector<std::string> diff_data_{"HelloWorld", "BspatchPatchTest",
132 "BspatchDiffData"};
133 std::vector<std::string> extra_data_{"HelloWorld!", "BZ2PatchReaderSmoke",
134 "BspatchExtraData"};
135 std::vector<int64_t> offset_increment_{100, 200, 300};
136
137 // The compressor streams.
138 std::unique_ptr<CompressorInterface> ctrl_stream_{nullptr};
139 std::unique_ptr<CompressorInterface> diff_stream_{nullptr};
140 std::unique_ptr<CompressorInterface> extra_stream_{nullptr};
141};
142
Tianjie Xud4875cd2017-11-17 16:06:30 -0800143TEST_F(PatchReaderTest, PatchReaderLegacyFormatSmoke) {
Tianjie Xub4cba642017-11-14 22:46:38 -0800144 ctrl_stream_.reset(new BZ2Compressor());
145 diff_stream_.reset(new BZ2Compressor());
146 extra_stream_.reset(new BZ2Compressor());
147
148 CompressData();
Tianjie Xu65288122017-10-13 15:10:58 -0700149
150 std::vector<uint8_t> patch_data;
Tianjie Xub4cba642017-11-14 22:46:38 -0800151 std::copy(kLegacyMagicHeader, kLegacyMagicHeader + 8,
152 std::back_inserter(patch_data));
153 ConstructPatchData(&patch_data);
Tianjie Xu65288122017-10-13 15:10:58 -0700154
Tianjie Xub4cba642017-11-14 22:46:38 -0800155 VerifyPatch(patch_data);
156}
Tianjie Xu65288122017-10-13 15:10:58 -0700157
Tianjie Xud4875cd2017-11-17 16:06:30 -0800158TEST_F(PatchReaderTest, PatchReaderNewFormatSmoke) {
Tianjie Xub4cba642017-11-14 22:46:38 -0800159 // Compress the data with one bz2 and two brotli compressors.
160 ctrl_stream_.reset(new BZ2Compressor());
Tianjie Xu1f1cdb22017-11-20 11:05:55 -0800161 diff_stream_.reset(new BrotliCompressor(11));
162 extra_stream_.reset(new BrotliCompressor(11));
Tianjie Xub4cba642017-11-14 22:46:38 -0800163
164 CompressData();
165
166 std::vector<uint8_t> patch_data;
167 std::copy(kBSDF2MagicHeader, kBSDF2MagicHeader + 5,
168 std::back_inserter(patch_data));
169 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBZ2));
170 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
171 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
172 ConstructPatchData(&patch_data);
173
174 VerifyPatch(patch_data);
Tianjie Xu65288122017-10-13 15:10:58 -0700175}
176
Alex Deymo7c4bb572018-03-19 20:25:08 +0100177TEST_F(PatchReaderTest, InvalidHeaderTest) {
178 // Negative values are not allowed.
179 InvalidHeaderTestHelper(-1, 0, 20, 50);
180 InvalidHeaderTestHelper(30, -3, 20, 50);
181 InvalidHeaderTestHelper(30, 8, -20, 50);
182
183 // Values larger than the patch size are also not allowed for ctrl and diff,
184 // or for the sum of both.
185 InvalidHeaderTestHelper(30, 5, 20, 10); // 30 > 10
186 InvalidHeaderTestHelper(5, 30, 20, 10); // 30 > 10
187 InvalidHeaderTestHelper(30, 5, 20, 32); // 30 + 5 > 32
188
189 // Values that overflow int64 are also not allowed when used combined
190 const int64_t kMax64 = std::numeric_limits<int64_t>::max();
191 InvalidHeaderTestHelper(kMax64 - 5, 5, 20, 20);
192 InvalidHeaderTestHelper(5, kMax64 - 5, 20, 20);
193
194 // 2 * (kMax64 - 5) + sizeof(header) is still positive due to overflow, but
195 // the patch size is too small.
196 InvalidHeaderTestHelper(kMax64 - 5, kMax64 - 5, 20, 20);
197}
198
Alex Deymo188fa332018-03-21 13:45:51 +0100199TEST_F(PatchReaderTest, InvalidCompressionHeaderTest) {
200 std::vector<uint8_t> patch_data;
201 std::copy(kBSDF2MagicHeader, kBSDF2MagicHeader + 5,
202 std::back_inserter(patch_data));
203 // Set an invalid compression value.
204 patch_data.push_back(99);
205 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
206 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
207 ConstructPatchHeader(10, 10, 10, &patch_data);
208 patch_data.resize(patch_data.size() + 30);
209
210 BsdiffPatchReader patch_reader;
211 EXPECT_FALSE(patch_reader.Init(patch_data.data(), patch_data.size()));
212}
213
214TEST_F(PatchReaderTest, InvalidControlEntryTest) {
215 // Check that negative diff and extra values in a control entry are not
216 // allowed.
217 ctrl_stream_.reset(new BZ2Compressor());
218 diff_stream_.reset(new BrotliCompressor(11));
219 extra_stream_.reset(new BrotliCompressor(11));
220
221 // Encode the header.
222 uint8_t buf[24];
223 EncodeInt64(-10, buf);
224 EncodeInt64(0, buf + 8);
225 EncodeInt64(0, buf + 16);
226 ctrl_stream_->Write(buf, sizeof(buf));
227
228 CompressData();
229
230 std::vector<uint8_t> patch_data;
231 std::copy(kBSDF2MagicHeader, kBSDF2MagicHeader + 5,
232 std::back_inserter(patch_data));
233 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBZ2));
234 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
235 patch_data.push_back(static_cast<uint8_t>(CompressorType::kBrotli));
236 ConstructPatchData(&patch_data);
237
238 BsdiffPatchReader patch_reader;
239 EXPECT_TRUE(patch_reader.Init(patch_data.data(), patch_data.size()));
240 ControlEntry control_entry(0, 0, 0);
241 EXPECT_FALSE(patch_reader.ParseControlEntry(&control_entry));
242}
Alex Deymo7c4bb572018-03-19 20:25:08 +0100243
Tianjie Xu65288122017-10-13 15:10:58 -0700244} // namespace bsdiff