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 | |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | #include "bsdiff/bz2_compressor.h" |
| 14 | #include "bsdiff/patch_reader.h" |
| 15 | #include "bsdiff/utils.h" |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | void EncodeInt64(int64_t x, uint8_t* buf) { |
| 20 | uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x; |
| 21 | for (int i = 0; i < 8; ++i) { |
| 22 | buf[i] = y & 0xff; |
| 23 | y /= 256; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | namespace bsdiff { |
| 30 | |
| 31 | TEST(PatchReaderTest, BZ2PatchReaderSmoke) { |
| 32 | BZ2Compressor ctrl_stream; |
| 33 | BZ2Compressor diff_stream; |
| 34 | BZ2Compressor extra_stream; |
| 35 | |
| 36 | // Write 3 blocks of data to diff stream & extra stream respectively. |
| 37 | std::vector<std::string> diff_data = {"HelloWorld", "BspatchPatchTest", |
| 38 | "BspatchDiffData"}; |
| 39 | std::vector<std::string> extra_data = {"HelloWorld!", "BZ2PatchReaderSmoke", |
| 40 | "BspatchExtraData"}; |
| 41 | std::vector<int64_t> offset_increment = {100, 200, 300}; |
| 42 | for (size_t i = 0; i < diff_data.size(); i++) { |
| 43 | uint8_t buf[24]; |
| 44 | EncodeInt64(diff_data[i].size(), buf); |
| 45 | EncodeInt64(extra_data[i].size(), buf + 8); |
| 46 | EncodeInt64(offset_increment[i], buf + 16); |
| 47 | EXPECT_TRUE(ctrl_stream.Write(buf, sizeof(buf))); |
| 48 | EXPECT_TRUE( |
| 49 | diff_stream.Write(reinterpret_cast<const uint8_t*>(diff_data[i].data()), |
| 50 | diff_data[i].size())); |
| 51 | EXPECT_TRUE(extra_stream.Write( |
| 52 | reinterpret_cast<const uint8_t*>(extra_data[i].data()), |
| 53 | extra_data[i].size())); |
| 54 | } |
| 55 | EXPECT_TRUE(ctrl_stream.Finish()); |
| 56 | EXPECT_TRUE(diff_stream.Finish()); |
| 57 | EXPECT_TRUE(extra_stream.Finish()); |
| 58 | |
| 59 | // Encode the header |
| 60 | uint8_t buf[24]; |
| 61 | EncodeInt64(ctrl_stream.GetCompressedData().size(), buf); |
| 62 | EncodeInt64(diff_stream.GetCompressedData().size(), buf + 8); |
| 63 | EncodeInt64(500, buf + 16); |
| 64 | |
| 65 | std::vector<uint8_t> patch_data; |
| 66 | std::copy(kMagicHeader, kMagicHeader + 8, std::back_inserter(patch_data)); |
| 67 | std::copy(buf, buf + sizeof(buf), std::back_inserter(patch_data)); |
| 68 | |
| 69 | // Concatenate the three streams into one patch. |
| 70 | std::copy(ctrl_stream.GetCompressedData().begin(), |
| 71 | ctrl_stream.GetCompressedData().end(), |
| 72 | std::back_inserter(patch_data)); |
| 73 | std::copy(diff_stream.GetCompressedData().begin(), |
| 74 | diff_stream.GetCompressedData().end(), |
| 75 | std::back_inserter(patch_data)); |
| 76 | std::copy(extra_stream.GetCompressedData().begin(), |
| 77 | extra_stream.GetCompressedData().end(), |
| 78 | std::back_inserter(patch_data)); |
| 79 | |
| 80 | BsdiffPatchReader patch_reader; |
| 81 | EXPECT_TRUE(patch_reader.Init(patch_data.data(), patch_data.size())); |
| 82 | EXPECT_EQ(500ull, patch_reader.new_file_size()); |
| 83 | // Check that the decompressed data matches what we wrote. |
| 84 | for (size_t i = 0; i < diff_data.size(); i++) { |
| 85 | ControlEntry control_entry(0, 0, 0); |
| 86 | EXPECT_TRUE(patch_reader.ParseControlEntry(&control_entry)); |
| 87 | uint8_t buffer[128] = {}; |
| 88 | EXPECT_TRUE(patch_reader.ReadDiffStream(buffer, diff_data[i].size())); |
| 89 | EXPECT_EQ(0, memcmp(buffer, diff_data[i].data(), diff_data[i].size())); |
| 90 | EXPECT_TRUE(patch_reader.ReadExtraStream(buffer, extra_data[i].size())); |
| 91 | EXPECT_EQ(0, memcmp(buffer, extra_data[i].data(), extra_data[i].size())); |
| 92 | } |
| 93 | EXPECT_TRUE(patch_reader.Finish()); |
| 94 | } |
| 95 | |
| 96 | } // namespace bsdiff |