Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 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 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 5 | #include "bsdiff/patch_reader.h" |
| 6 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <algorithm> |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 10 | #include <limits> |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 16 | #include "bsdiff/brotli_compressor.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 17 | #include "bsdiff/bz2_compressor.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 18 | #include "bsdiff/utils.h" |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | void 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 | |
| 32 | namespace bsdiff { |
| 33 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 34 | class 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 Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 53 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 54 | |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 55 | void ConstructPatchHeader(int64_t ctrl_size, |
| 56 | int64_t diff_size, |
| 57 | int64_t new_size, |
| 58 | std::vector<uint8_t>* patch_data) { |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 59 | EXPECT_EQ(static_cast<size_t>(8), patch_data->size()); |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 60 | // Encode the header. |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 61 | uint8_t buf[24]; |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 62 | EncodeInt64(ctrl_size, buf); |
| 63 | EncodeInt64(diff_size, buf + 8); |
| 64 | EncodeInt64(new_size, buf + 16); |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 65 | std::copy(buf, buf + sizeof(buf), std::back_inserter(*patch_data)); |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 66 | } |
| 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 Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 72 | |
| 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 Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 85 | 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 Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 107 | // 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 Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 130 | 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 Xu | d4875cd | 2017-11-17 16:06:30 -0800 | [diff] [blame] | 143 | TEST_F(PatchReaderTest, PatchReaderLegacyFormatSmoke) { |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 144 | ctrl_stream_.reset(new BZ2Compressor()); |
| 145 | diff_stream_.reset(new BZ2Compressor()); |
| 146 | extra_stream_.reset(new BZ2Compressor()); |
| 147 | |
| 148 | CompressData(); |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 149 | |
| 150 | std::vector<uint8_t> patch_data; |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 151 | std::copy(kLegacyMagicHeader, kLegacyMagicHeader + 8, |
| 152 | std::back_inserter(patch_data)); |
| 153 | ConstructPatchData(&patch_data); |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 154 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 155 | VerifyPatch(patch_data); |
| 156 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 157 | |
Tianjie Xu | d4875cd | 2017-11-17 16:06:30 -0800 | [diff] [blame] | 158 | TEST_F(PatchReaderTest, PatchReaderNewFormatSmoke) { |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 159 | // Compress the data with one bz2 and two brotli compressors. |
| 160 | ctrl_stream_.reset(new BZ2Compressor()); |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 161 | diff_stream_.reset(new BrotliCompressor(11)); |
| 162 | extra_stream_.reset(new BrotliCompressor(11)); |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 163 | |
| 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 Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 177 | TEST_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 | |
| 199 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 200 | } // namespace bsdiff |