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.h"
7 #include "courgette/base_test_unittest.h"
8 #include "courgette/courgette.h"
9 #include "courgette/streams.h"
11 class BSDiffMemoryTest
: public BaseTest
{
13 void GenerateAndTestPatch(const std::string
& a
, const std::string
& b
) const;
15 std::string
GenerateSyntheticInput(size_t length
, int seed
) const;
18 void BSDiffMemoryTest::GenerateAndTestPatch(const std::string
& old_text
,
19 const std::string
& new_text
) const {
20 courgette::SourceStream old1
;
21 courgette::SourceStream new1
;
22 old1
.Init(old_text
.c_str(), old_text
.length());
23 new1
.Init(new_text
.c_str(), new_text
.length());
25 courgette::SinkStream patch1
;
26 courgette::BSDiffStatus status
= CreateBinaryPatch(&old1
, &new1
, &patch1
);
27 EXPECT_EQ(courgette::OK
, status
);
29 courgette::SourceStream old2
;
30 courgette::SourceStream patch2
;
31 old2
.Init(old_text
.c_str(), old_text
.length());
34 courgette::SinkStream new2
;
35 status
= ApplyBinaryPatch(&old2
, &patch2
, &new2
);
36 EXPECT_EQ(courgette::OK
, status
);
37 EXPECT_EQ(new_text
.length(), new2
.Length());
38 EXPECT_EQ(0, memcmp(new_text
.c_str(), new2
.Buffer(), new_text
.length()));
41 std::string
BSDiffMemoryTest::GenerateSyntheticInput(size_t length
, int seed
)
43 static const char* a
[8] = {"O", "A", "x", "-", "y", ".", "|", ":"};
45 while (result
.length() < length
) {
46 seed
= (seed
+ 17) * 1049 + (seed
>> 27);
47 result
.append(a
[seed
& 7]);
49 result
.resize(length
);
53 TEST_F(BSDiffMemoryTest
, TestEmpty
) {
54 GenerateAndTestPatch(std::string(), std::string());
57 TEST_F(BSDiffMemoryTest
, TestEmptyVsNonempty
) {
58 GenerateAndTestPatch(std::string(), "xxx");
61 TEST_F(BSDiffMemoryTest
, TestNonemptyVsEmpty
) {
62 GenerateAndTestPatch("xxx", std::string());
65 TEST_F(BSDiffMemoryTest
, TestSmallInputsWithSmallChanges
) {
67 "I would not, could not, in a box.\n"
68 "I could not, would not, with a fox.\n"
69 "I will not eat them with a mouse.\n"
70 "I will not eat them in a house.\n"
71 "I will not eat them here or there.\n"
72 "I will not eat them anywhere.\n"
73 "I do not eat green eggs and ham.\n"
74 "I do not like them, Sam-I-am.\n";
76 "I would not, could not, in a BOX.\n"
77 "I could not, would not, with a FOX.\n"
78 "I will not eat them with a MOUSE.\n"
79 "I will not eat them in a HOUSE.\n"
80 "I will not eat them in a HOUSE.\n" // Extra line.
81 "I will not eat them here or THERE.\n"
82 "I will not eat them ANYWHERE.\n"
83 "I do not eat green eggs and HAM.\n"
84 "I do not like them, Sam-I-am.\n";
85 GenerateAndTestPatch(file1
, file2
);
88 TEST_F(BSDiffMemoryTest
, TestNearPageArrayPageSize
) {
89 // This magic number is the size of one block of the PageArray in
90 // third_party/bsdiff_create.cc.
91 size_t critical_size
= 1 << 18;
93 // Test first-inputs with sizes that straddle the magic size to test this
94 // PageArray's internal boundary condition.
96 std::string file1
= GenerateSyntheticInput(critical_size
, 0);
97 std::string file2
= GenerateSyntheticInput(critical_size
, 1);
98 GenerateAndTestPatch(file1
, file2
);
100 std::string file1a
= file1
.substr(0, critical_size
- 1);
101 GenerateAndTestPatch(file1a
, file2
);
103 std::string file1b
= file1
.substr(0, critical_size
- 2);
104 GenerateAndTestPatch(file1b
, file2
);
106 std::string file1c
= file1
+ file1
.substr(0, 1);
107 GenerateAndTestPatch(file1c
, file2
);
110 TEST_F(BSDiffMemoryTest
, TestIndenticalDlls
) {
111 std::string file1
= FileContents("en-US.dll");
112 GenerateAndTestPatch(file1
, file1
);
115 TEST_F(BSDiffMemoryTest
, TestDifferentExes
) {
116 std::string file1
= FileContents("setup1.exe");
117 std::string file2
= FileContents("setup2.exe");
118 GenerateAndTestPatch(file1
, file2
);
121 TEST_F(BSDiffMemoryTest
, TestDifferentElfs
) {
122 std::string file1
= FileContents("elf-32-1");
123 std::string file2
= FileContents("elf-32-2");
124 GenerateAndTestPatch(file1
, file2
);