1 // Copyright (c) 2011 The Chromium 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.
5 #include "courgette/third_party/bsdiff/bsdiff.h"
9 #include "courgette/base_test_unittest.h"
10 #include "courgette/courgette.h"
11 #include "courgette/streams.h"
13 class BSDiffMemoryTest : public BaseTest {
15 void GenerateAndTestPatch(const std::string& a, const std::string& b) const;
17 std::string GenerateSyntheticInput(size_t length, int seed) const;
20 void BSDiffMemoryTest::GenerateAndTestPatch(const std::string& old_text,
21 const std::string& new_text) const {
22 courgette::SourceStream old1;
23 courgette::SourceStream new1;
24 old1.Init(old_text.c_str(), old_text.length());
25 new1.Init(new_text.c_str(), new_text.length());
27 courgette::SinkStream patch1;
28 bsdiff::BSDiffStatus status =
29 bsdiff::CreateBinaryPatch(&old1, &new1, &patch1);
30 EXPECT_EQ(bsdiff::OK, status);
32 courgette::SourceStream old2;
33 courgette::SourceStream patch2;
34 old2.Init(old_text.c_str(), old_text.length());
37 courgette::SinkStream new2;
38 status = bsdiff::ApplyBinaryPatch(&old2, &patch2, &new2);
39 EXPECT_EQ(bsdiff::OK, status);
40 EXPECT_EQ(new_text.length(), new2.Length());
41 EXPECT_EQ(0, memcmp(new_text.c_str(), new2.Buffer(), new_text.length()));
44 std::string BSDiffMemoryTest::GenerateSyntheticInput(size_t length, int seed)
46 static const char* a[8] = {"O", "A", "x", "-", "y", ".", "|", ":"};
48 while (result.length() < length) {
49 seed = (seed + 17) * 1049 + (seed >> 27);
50 result.append(a[seed & 7]);
52 result.resize(length);
56 TEST_F(BSDiffMemoryTest, TestEmpty) {
57 GenerateAndTestPatch(std::string(), std::string());
60 TEST_F(BSDiffMemoryTest, TestEmptyVsNonempty) {
61 GenerateAndTestPatch(std::string(), "xxx");
64 TEST_F(BSDiffMemoryTest, TestNonemptyVsEmpty) {
65 GenerateAndTestPatch("xxx", std::string());
68 TEST_F(BSDiffMemoryTest, TestSmallInputsWithSmallChanges) {
70 "I would not, could not, in a box.\n"
71 "I could not, would not, with a fox.\n"
72 "I will not eat them with a mouse.\n"
73 "I will not eat them in a house.\n"
74 "I will not eat them here or there.\n"
75 "I will not eat them anywhere.\n"
76 "I do not eat green eggs and ham.\n"
77 "I do not like them, Sam-I-am.\n";
79 "I would not, could not, in a BOX.\n"
80 "I could not, would not, with a FOX.\n"
81 "I will not eat them with a MOUSE.\n"
82 "I will not eat them in a HOUSE.\n"
83 "I will not eat them in a HOUSE.\n" // Extra line.
84 "I will not eat them here or THERE.\n"
85 "I will not eat them ANYWHERE.\n"
86 "I do not eat green eggs and HAM.\n"
87 "I do not like them, Sam-I-am.\n";
88 GenerateAndTestPatch(file1, file2);
91 TEST_F(BSDiffMemoryTest, TestNearPageArrayPageSize) {
92 // This magic number is the size of one block of the PageArray in
93 // third_party/bsdiff_create.cc.
94 size_t critical_size = 1 << 18;
96 // Test first-inputs with sizes that straddle the magic size to test this
97 // PageArray's internal boundary condition.
99 std::string file1 = GenerateSyntheticInput(critical_size, 0);
100 std::string file2 = GenerateSyntheticInput(critical_size, 1);
101 GenerateAndTestPatch(file1, file2);
103 std::string file1a = file1.substr(0, critical_size - 1);
104 GenerateAndTestPatch(file1a, file2);
106 std::string file1b = file1.substr(0, critical_size - 2);
107 GenerateAndTestPatch(file1b, file2);
109 std::string file1c = file1 + file1.substr(0, 1);
110 GenerateAndTestPatch(file1c, file2);
113 TEST_F(BSDiffMemoryTest, TestIndenticalDlls) {
114 std::string file1 = FileContents("en-US.dll");
115 GenerateAndTestPatch(file1, file1);
118 TEST_F(BSDiffMemoryTest, TestDifferentExes) {
119 std::string file1 = FileContents("setup1.exe");
120 std::string file2 = FileContents("setup2.exe");
121 GenerateAndTestPatch(file1, file2);
124 TEST_F(BSDiffMemoryTest, TestDifferentElfs) {
125 std::string file1 = FileContents("elf-32-1");
126 std::string file2 = FileContents("elf-32-2");
127 GenerateAndTestPatch(file1, file2);