summaryrefslogtreecommitdiffstats
path: root/courgette/bsdiff_memory_unittest.cc
blob: 75de082c7ae5e81038103020a197640a6ab71a47 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2009 The Chromium 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 "courgette/third_party/bsdiff.h"

#include <string>

#include "base/file_util.h"
#include "base/path_service.h"
#include "base/string_util.h"

#include "courgette/courgette.h"
#include "courgette/streams.h"

#include "testing/gtest/include/gtest/gtest.h"

class BSDiffMemoryTest : public testing::Test {
 public:
  std::string FileContents(const char* file_name) const;
  void GenerateAndTestPatch(const std::string& a, const std::string& b) const;

  std::string GenerateSyntheticInput(size_t length, int seed) const;

 private:
  void SetUp() {
    PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_);
    test_dir_ = test_dir_.AppendASCII("courgette");
    test_dir_ = test_dir_.AppendASCII("testdata");
  }

  FilePath test_dir_;
};

//  Reads a test file into a string.
std::string BSDiffMemoryTest::FileContents(const char* file_name) const {
  FilePath file_path = test_dir_;
  file_path = file_path.AppendASCII(file_name);
  std::string file_bytes;
  if (!file_util::ReadFileToString(file_path, &file_bytes)) {
    EXPECT_TRUE(!"Could not read test data");
  }
  return file_bytes;
}

void BSDiffMemoryTest::GenerateAndTestPatch(const std::string& old_text,
                                            const std::string& new_text) const {
  courgette::SourceStream old1;
  courgette::SourceStream new1;
  old1.Init(old_text.c_str(), old_text.length());
  new1.Init(new_text.c_str(), new_text.length());

  courgette::SinkStream patch1;
  courgette::BSDiffStatus status = CreateBinaryPatch(&old1, &new1, &patch1);
  EXPECT_EQ(courgette::OK, status);

  courgette::SourceStream old2;
  courgette::SourceStream patch2;
  old2.Init(old_text.c_str(), old_text.length());
  patch2.Init(patch1);

  courgette::SinkStream new2;
  status = ApplyBinaryPatch(&old2, &patch2, &new2);
  EXPECT_EQ(courgette::OK, status);
  EXPECT_EQ(new_text.length(), new2.Length());
  EXPECT_EQ(0, memcmp(new_text.c_str(), new2.Buffer(), new_text.length()));
}

std::string BSDiffMemoryTest::GenerateSyntheticInput(size_t length, int seed)
  const {
  static const char* a[8] = {"O", "A", "x", "-", "y", ".", "|", ":"};
  std::string result;
  while (result.length() < length) {
    seed = (seed + 17) * 1049 + (seed >> 27);
    result.append(a[seed & 7]);
  }
  result.resize(length);
  return result;
}

TEST_F(BSDiffMemoryTest, TestEmpty) {
  GenerateAndTestPatch("", "");
}

TEST_F(BSDiffMemoryTest, TestEmptyVsNonempty) {
  GenerateAndTestPatch("", "xxx");
}

TEST_F(BSDiffMemoryTest, TestNonemptyVsEmpty) {
  GenerateAndTestPatch("xxx", "");
}

TEST_F(BSDiffMemoryTest, TestSmallInputsWithSmallChanges) {
  std::string file1 =
      "I would not, could not, in a box.\n"
      "I could not, would not, with a fox.\n"
      "I will not eat them with a mouse.\n"
      "I will not eat them in a house.\n"
      "I will not eat them here or there.\n"
      "I will not eat them anywhere.\n"
      "I do not eat green eggs and ham.\n"
      "I do not like them, Sam-I-am.\n";
  std::string file2 =
      "I would not, could not, in a BOX.\n"
      "I could not, would not, with a FOX.\n"
      "I will not eat them with a MOUSE.\n"
      "I will not eat them in a HOUSE.\n"
      "I will not eat them in a HOUSE.\n"     // Extra line.
      "I will not eat them here or THERE.\n"
      "I will not eat them ANYWHERE.\n"
      "I do not eat green eggs and HAM.\n"
      "I do not like them, Sam-I-am.\n";
  GenerateAndTestPatch(file1, file2);
}

TEST_F(BSDiffMemoryTest, TestNearPageArrayPageSize) {
  // This magic number is the size of one block of the PageArray in
  // third_party/bsdiff_create.cc.
  size_t critical_size = 1 << 18;

  // Test first-inputs with sizes that straddle the magic size to test this
  // PageArray's internal boundary condition.

  std::string file1 = GenerateSyntheticInput(critical_size, 0);
  std::string file2 = GenerateSyntheticInput(critical_size, 1);
  GenerateAndTestPatch(file1, file2);

  std::string file1a = file1.substr(0, critical_size - 1);
  GenerateAndTestPatch(file1a, file2);

  std::string file1b = file1.substr(0, critical_size - 2);
  GenerateAndTestPatch(file1b, file2);

  std::string file1c = file1 + file1.substr(0, 1);
  GenerateAndTestPatch(file1c, file2);
}

TEST_F(BSDiffMemoryTest, TestIndenticalDlls) {
  std::string file1 = FileContents("en-US.dll");
  GenerateAndTestPatch(file1, file1);
}

TEST_F(BSDiffMemoryTest, TestDifferentExes) {
  std::string file1 = FileContents("setup1.exe");
  std::string file2 = FileContents("setup2.exe");
  GenerateAndTestPatch(file1, file2);
}