summaryrefslogtreecommitdiff
path: root/bsdiff_unittest.cc
blob: ef4d3ef8186fab18ab330256dd144c2f6818930b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "bsdiff/bsdiff.h"

#include <gtest/gtest.h>
#include <algorithm>
#include <random>
#include <string>
#include <vector>

#include "bsdiff/fake_patch_writer.h"

namespace {

// Generate deterministic random data in the output buffer. The buffer must be
// already allocated with the desired size. The data generated depends on the
// selected size.
void GenerateRandomBuffer(std::vector<uint8_t>* buffer) {
  std::minstd_rand prng(1234 + buffer->size());
  std::generate(buffer->begin(), buffer->end(), prng);
}

}  // namespace

namespace bsdiff {

class BsdiffTest : public testing::Test {
 protected:
  BsdiffTest() = default;
  ~BsdiffTest() override = default;

  void RunBsdiff() {
    EXPECT_EQ(0, bsdiff(old_file_.data(), old_file_.size(), new_file_.data(),
                        new_file_.size(), min_len_, &patch_writer_, nullptr));
  }

  std::vector<uint8_t> old_file_;
  std::vector<uint8_t> new_file_;
  size_t min_len_ = 0;  // 0 means the default.
  FakePatchWriter patch_writer_;
};

// Check that a file with no changes has a very small patch (no extra data).
TEST_F(BsdiffTest, EqualEmptyFiles) {
  // Empty old and new files.
  RunBsdiff();

  // No entries should be generated on an empty new file.
  EXPECT_TRUE(patch_writer_.entries().empty());
}

TEST_F(BsdiffTest, EqualSmallFiles) {
  std::string some_text = "Hello world!";
  old_file_.insert(old_file_.begin(), some_text.begin(), some_text.end());
  new_file_.insert(new_file_.begin(), some_text.begin(), some_text.end());
  RunBsdiff();

  EXPECT_EQ(1U, patch_writer_.entries().size());
  ControlEntry entry = patch_writer_.entries()[0];
  EXPECT_EQ(some_text.size(), entry.diff_size);
  EXPECT_EQ(0U, entry.extra_size);
}

TEST_F(BsdiffTest, FileWithSmallErrorsTest) {
  old_file_.resize(100);
  GenerateRandomBuffer(&old_file_);
  new_file_ = old_file_;
  // Break a few bytes somewhere in the middle.
  new_file_[20]++;
  new_file_[30] += 2;
  new_file_[31] += 2;

  RunBsdiff();

  // We expect that the result has only one entry with all in the diff stream
  // since the two files are very similar.
  EXPECT_EQ(1U, patch_writer_.entries().size());
  ControlEntry entry = patch_writer_.entries()[0];
  EXPECT_EQ(100U, entry.diff_size);
  EXPECT_EQ(0U, entry.extra_size);
}

TEST_F(BsdiffTest, MinLengthConsideredTest) {
  old_file_.resize(100);
  GenerateRandomBuffer(&old_file_);
  new_file_ = old_file_;
  // Copy the first 10 bytes to the middle.
  for (size_t i = 0; i < 10; i++) {
    new_file_[50 + i] = old_file_[i];
  }

  min_len_ = 12;
  RunBsdiff();

  // We expect that the 10 bytes in the middle that match the beginning are
  // ignored and just emitted as diff data because the min_len is bigger than
  // 10.
  EXPECT_EQ(1U, patch_writer_.entries().size());
  ControlEntry entry = patch_writer_.entries()[0];
  EXPECT_EQ(100U, entry.diff_size);
  EXPECT_EQ(0U, entry.extra_size);
}

}  // namespace bsdiff