blob: ef4d3ef8186fab18ab330256dd144c2f6818930b [file] [log] [blame]
Alex Deymoa5cff222015-04-08 14:10:30 -07001// Copyright 2015 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
Alex Deymoddf9db52017-03-02 16:10:41 -08005#include "bsdiff/bsdiff.h"
Alex Deymoa5cff222015-04-08 14:10:30 -07006
7#include <gtest/gtest.h>
Alex Deymofb3b6322017-09-27 14:28:54 +02008#include <algorithm>
9#include <random>
Alex Deymoa5cff222015-04-08 14:10:30 -070010#include <string>
11#include <vector>
12
Alex Deymofb3b6322017-09-27 14:28:54 +020013#include "bsdiff/fake_patch_writer.h"
Alex Deymoa5cff222015-04-08 14:10:30 -070014
Alex Deymofb3b6322017-09-27 14:28:54 +020015namespace {
16
17// Generate deterministic random data in the output buffer. The buffer must be
18// already allocated with the desired size. The data generated depends on the
19// selected size.
20void GenerateRandomBuffer(std::vector<uint8_t>* buffer) {
21 std::minstd_rand prng(1234 + buffer->size());
22 std::generate(buffer->begin(), buffer->end(), prng);
23}
24
25} // namespace
Alex Deymoa5cff222015-04-08 14:10:30 -070026
Alex Deymo20891f92015-10-12 17:28:04 -070027namespace bsdiff {
28
Alex Deymoa5cff222015-04-08 14:10:30 -070029class BsdiffTest : public testing::Test {
30 protected:
Alex Deymofb3b6322017-09-27 14:28:54 +020031 BsdiffTest() = default;
32 ~BsdiffTest() override = default;
Alex Deymoa5cff222015-04-08 14:10:30 -070033
Alex Deymofb3b6322017-09-27 14:28:54 +020034 void RunBsdiff() {
35 EXPECT_EQ(0, bsdiff(old_file_.data(), old_file_.size(), new_file_.data(),
Alex Deymo383f6772018-02-08 15:50:11 +010036 new_file_.size(), min_len_, &patch_writer_, nullptr));
Alex Deymofb3b6322017-09-27 14:28:54 +020037 }
38
39 std::vector<uint8_t> old_file_;
40 std::vector<uint8_t> new_file_;
Alex Deymo383f6772018-02-08 15:50:11 +010041 size_t min_len_ = 0; // 0 means the default.
Alex Deymofb3b6322017-09-27 14:28:54 +020042 FakePatchWriter patch_writer_;
Alex Deymoa5cff222015-04-08 14:10:30 -070043};
44
45// Check that a file with no changes has a very small patch (no extra data).
46TEST_F(BsdiffTest, EqualEmptyFiles) {
47 // Empty old and new files.
Alex Deymofb3b6322017-09-27 14:28:54 +020048 RunBsdiff();
Alex Deymoa5cff222015-04-08 14:10:30 -070049
Alex Deymofb3b6322017-09-27 14:28:54 +020050 // No entries should be generated on an empty new file.
51 EXPECT_TRUE(patch_writer_.entries().empty());
Alex Deymoa5cff222015-04-08 14:10:30 -070052}
53
54TEST_F(BsdiffTest, EqualSmallFiles) {
Alex Deymoc0461f02017-09-22 15:05:42 +020055 std::string some_text = "Hello world!";
Alex Deymofb3b6322017-09-27 14:28:54 +020056 old_file_.insert(old_file_.begin(), some_text.begin(), some_text.end());
57 new_file_.insert(new_file_.begin(), some_text.begin(), some_text.end());
58 RunBsdiff();
Alex Deymoa5cff222015-04-08 14:10:30 -070059
Alex Deymofb3b6322017-09-27 14:28:54 +020060 EXPECT_EQ(1U, patch_writer_.entries().size());
61 ControlEntry entry = patch_writer_.entries()[0];
62 EXPECT_EQ(some_text.size(), entry.diff_size);
63 EXPECT_EQ(0U, entry.extra_size);
64}
65
66TEST_F(BsdiffTest, FileWithSmallErrorsTest) {
67 old_file_.resize(100);
68 GenerateRandomBuffer(&old_file_);
69 new_file_ = old_file_;
70 // Break a few bytes somewhere in the middle.
71 new_file_[20]++;
72 new_file_[30] += 2;
73 new_file_[31] += 2;
74
75 RunBsdiff();
76
77 // We expect that the result has only one entry with all in the diff stream
78 // since the two files are very similar.
79 EXPECT_EQ(1U, patch_writer_.entries().size());
80 ControlEntry entry = patch_writer_.entries()[0];
81 EXPECT_EQ(100U, entry.diff_size);
82 EXPECT_EQ(0U, entry.extra_size);
Alex Deymoa5cff222015-04-08 14:10:30 -070083}
Alex Deymo20891f92015-10-12 17:28:04 -070084
Alex Deymo383f6772018-02-08 15:50:11 +010085TEST_F(BsdiffTest, MinLengthConsideredTest) {
86 old_file_.resize(100);
87 GenerateRandomBuffer(&old_file_);
88 new_file_ = old_file_;
89 // Copy the first 10 bytes to the middle.
90 for (size_t i = 0; i < 10; i++) {
91 new_file_[50 + i] = old_file_[i];
92 }
93
94 min_len_ = 12;
95 RunBsdiff();
96
97 // We expect that the 10 bytes in the middle that match the beginning are
98 // ignored and just emitted as diff data because the min_len is bigger than
99 // 10.
100 EXPECT_EQ(1U, patch_writer_.entries().size());
101 ControlEntry entry = patch_writer_.entries()[0];
102 EXPECT_EQ(100U, entry.diff_size);
103 EXPECT_EQ(0U, entry.extra_size);
104}
105
Alex Deymo20891f92015-10-12 17:28:04 -0700106} // namespace bsdiff