Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [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 "bsdiff/patch_writer.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include "bsdiff/test_utils.h" |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | // Generated with: |
| 14 | // echo 'Hello World' | hexdump -v -e '" " 12/1 "0x%02x, " "\n"' |
| 15 | const 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"' |
| 21 | const 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"' |
| 25 | const 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"' |
| 36 | const 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 | |
| 45 | namespace bsdiff { |
| 46 | |
| 47 | class BsdiffPatchWriterTest : public testing::Test { |
| 48 | protected: |
| 49 | test_utils::ScopedTempFile patch_file_{"bsdiff_newfile.XXXXXX"}; |
| 50 | BsdiffPatchWriter patch_writer_{patch_file_.filename()}; |
| 51 | }; |
| 52 | |
| 53 | TEST_F(BsdiffPatchWriterTest, CreateEmptyPatchTest) { |
| 54 | EXPECT_TRUE(patch_writer_.InitializeBuffers(nullptr, 0, nullptr, 0)); |
| 55 | EXPECT_TRUE(patch_writer_.Close()); |
| 56 | |
| 57 | test_utils::BsdiffPatchFile patch; |
| 58 | EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename())); |
| 59 | EXPECT_TRUE(patch.IsValid()); |
| 60 | |
| 61 | // An empty bz2 file will have 14 bytes. |
| 62 | EXPECT_EQ(sizeof(kCompressedEmpty), static_cast<uint64_t>(patch.diff_len)); |
| 63 | EXPECT_EQ(sizeof(kCompressedEmpty), patch.extra_len); |
| 64 | } |
| 65 | |
| 66 | TEST_F(BsdiffPatchWriterTest, AllInExtraStreamTest) { |
| 67 | EXPECT_TRUE(patch_writer_.InitializeBuffers( |
| 68 | nullptr, 0, kHelloWorld, sizeof(kHelloWorld))); |
| 69 | // Write to the extra stream in two parts: first 5 bytes, then the rest. |
| 70 | EXPECT_TRUE(patch_writer_.AddControlEntry(ControlEntry(0, 5, 0))); |
| 71 | EXPECT_TRUE(patch_writer_.AddControlEntry( |
| 72 | ControlEntry(0, sizeof(kHelloWorld) - 5, 0))); |
| 73 | EXPECT_TRUE(patch_writer_.Close()); |
| 74 | |
| 75 | test_utils::BsdiffPatchFile patch; |
| 76 | EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename())); |
| 77 | EXPECT_TRUE(patch.IsValid()); |
| 78 | EXPECT_EQ(patch.bz2_diff, |
| 79 | std::vector<uint8_t>(kCompressedEmpty, |
| 80 | kCompressedEmpty + sizeof(kCompressedEmpty))); |
| 81 | EXPECT_EQ(patch.bz2_extra, |
| 82 | std::vector<uint8_t>( |
| 83 | kCompressedHelloWorld, |
| 84 | kCompressedHelloWorld + sizeof(kCompressedHelloWorld))); |
| 85 | |
| 86 | EXPECT_EQ(static_cast<int64_t>(sizeof(kHelloWorld)), patch.new_file_len); |
| 87 | } |
| 88 | |
| 89 | TEST_F(BsdiffPatchWriterTest, AllInDiffStreamTest) { |
| 90 | EXPECT_TRUE(patch_writer_.InitializeBuffers( |
| 91 | kHelloWorld, sizeof(kHelloWorld), kHelloWorld, sizeof(kHelloWorld))); |
| 92 | // Write to the extra stream in two parts: first 5 bytes, then the rest. |
| 93 | EXPECT_TRUE( |
| 94 | patch_writer_.AddControlEntry(ControlEntry(sizeof(kHelloWorld), 0, 0))); |
| 95 | EXPECT_TRUE(patch_writer_.Close()); |
| 96 | |
| 97 | test_utils::BsdiffPatchFile patch; |
| 98 | EXPECT_TRUE(patch.LoadFromFile(patch_file_.filename())); |
| 99 | EXPECT_TRUE(patch.IsValid()); |
| 100 | EXPECT_EQ(patch.bz2_extra, |
| 101 | std::vector<uint8_t>(kCompressedEmpty, |
| 102 | kCompressedEmpty + sizeof(kCompressedEmpty))); |
| 103 | EXPECT_EQ(patch.bz2_diff, |
| 104 | std::vector<uint8_t>(kCompressedZeros, |
| 105 | kCompressedZeros + sizeof(kCompressedZeros))); |
| 106 | |
| 107 | EXPECT_EQ(static_cast<int64_t>(sizeof(kHelloWorld)), patch.new_file_len); |
| 108 | } |
| 109 | |
| 110 | } // namespace bsdiff |