blob: 09bfd2b810054d171d4a809038bb93f9bfa38d20 [file] [log] [blame]
[email protected]bf0ece42011-10-18 01:08:351// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]04ca1bc2009-05-08 23:00:292// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
altimin979ea2e12016-05-18 16:16:245#include "courgette/third_party/bsdiff/bsdiff.h"
[email protected]04ca1bc2009-05-08 23:00:296
aviab98dcc92015-12-21 19:35:337#include <stddef.h>
8
[email protected]bf0ece42011-10-18 01:08:359#include "courgette/base_test_unittest.h"
[email protected]04ca1bc2009-05-08 23:00:2910#include "courgette/courgette.h"
11#include "courgette/streams.h"
12
[email protected]bf0ece42011-10-18 01:08:3513class BSDiffMemoryTest : public BaseTest {
[email protected]04ca1bc2009-05-08 23:00:2914 public:
[email protected]04ca1bc2009-05-08 23:00:2915 void GenerateAndTestPatch(const std::string& a, const std::string& b) const;
16
[email protected]b6732ba2010-05-29 00:47:4817 std::string GenerateSyntheticInput(size_t length, int seed) const;
[email protected]04ca1bc2009-05-08 23:00:2918};
19
[email protected]04ca1bc2009-05-08 23:00:2920void 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());
26
27 courgette::SinkStream patch1;
huangs7054b5a22016-07-26 21:46:1328 bsdiff::BSDiffStatus status =
29 bsdiff::CreateBinaryPatch(&old1, &new1, &patch1);
30 EXPECT_EQ(bsdiff::OK, status);
[email protected]04ca1bc2009-05-08 23:00:2931
32 courgette::SourceStream old2;
33 courgette::SourceStream patch2;
34 old2.Init(old_text.c_str(), old_text.length());
35 patch2.Init(patch1);
36
37 courgette::SinkStream new2;
huangs7054b5a22016-07-26 21:46:1338 status = bsdiff::ApplyBinaryPatch(&old2, &patch2, &new2);
39 EXPECT_EQ(bsdiff::OK, status);
[email protected]04ca1bc2009-05-08 23:00:2940 EXPECT_EQ(new_text.length(), new2.Length());
41 EXPECT_EQ(0, memcmp(new_text.c_str(), new2.Buffer(), new_text.length()));
42}
43
[email protected]b6732ba2010-05-29 00:47:4844std::string BSDiffMemoryTest::GenerateSyntheticInput(size_t length, int seed)
45 const {
46 static const char* a[8] = {"O", "A", "x", "-", "y", ".", "|", ":"};
47 std::string result;
48 while (result.length() < length) {
49 seed = (seed + 17) * 1049 + (seed >> 27);
50 result.append(a[seed & 7]);
51 }
52 result.resize(length);
53 return result;
54}
55
[email protected]04ca1bc2009-05-08 23:00:2956TEST_F(BSDiffMemoryTest, TestEmpty) {
[email protected]007b3f82013-04-09 08:46:4557 GenerateAndTestPatch(std::string(), std::string());
[email protected]04ca1bc2009-05-08 23:00:2958}
59
60TEST_F(BSDiffMemoryTest, TestEmptyVsNonempty) {
[email protected]007b3f82013-04-09 08:46:4561 GenerateAndTestPatch(std::string(), "xxx");
[email protected]04ca1bc2009-05-08 23:00:2962}
63
64TEST_F(BSDiffMemoryTest, TestNonemptyVsEmpty) {
[email protected]007b3f82013-04-09 08:46:4565 GenerateAndTestPatch("xxx", std::string());
[email protected]04ca1bc2009-05-08 23:00:2966}
67
68TEST_F(BSDiffMemoryTest, TestSmallInputsWithSmallChanges) {
69 std::string file1 =
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";
78 std::string file2 =
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);
89}
90
[email protected]b6732ba2010-05-29 00:47:4891TEST_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;
95
96 // Test first-inputs with sizes that straddle the magic size to test this
97 // PageArray's internal boundary condition.
98
99 std::string file1 = GenerateSyntheticInput(critical_size, 0);
100 std::string file2 = GenerateSyntheticInput(critical_size, 1);
101 GenerateAndTestPatch(file1, file2);
102
103 std::string file1a = file1.substr(0, critical_size - 1);
104 GenerateAndTestPatch(file1a, file2);
105
106 std::string file1b = file1.substr(0, critical_size - 2);
107 GenerateAndTestPatch(file1b, file2);
108
109 std::string file1c = file1 + file1.substr(0, 1);
110 GenerateAndTestPatch(file1c, file2);
111}
112
[email protected]04ca1bc2009-05-08 23:00:29113TEST_F(BSDiffMemoryTest, TestIndenticalDlls) {
114 std::string file1 = FileContents("en-US.dll");
115 GenerateAndTestPatch(file1, file1);
116}
117
118TEST_F(BSDiffMemoryTest, TestDifferentExes) {
119 std::string file1 = FileContents("setup1.exe");
120 std::string file2 = FileContents("setup2.exe");
121 GenerateAndTestPatch(file1, file2);
122}
[email protected]4b3d192b2011-11-08 20:32:26123
124TEST_F(BSDiffMemoryTest, TestDifferentElfs) {
125 std::string file1 = FileContents("elf-32-1");
126 std::string file2 = FileContents("elf-32-2");
127 GenerateAndTestPatch(file1, file2);
128}