[email protected] | bf0ece4 | 2011-10-18 01:08:35 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
altimin | 979ea2e1 | 2016-05-18 16:16:24 | [diff] [blame] | 5 | #include "courgette/third_party/bsdiff/bsdiff.h" |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 6 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | bf0ece4 | 2011-10-18 01:08:35 | [diff] [blame] | 9 | #include "courgette/base_test_unittest.h" |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 10 | #include "courgette/courgette.h" |
| 11 | #include "courgette/streams.h" |
| 12 | |
[email protected] | bf0ece4 | 2011-10-18 01:08:35 | [diff] [blame] | 13 | class BSDiffMemoryTest : public BaseTest { |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 14 | public: |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 15 | void GenerateAndTestPatch(const std::string& a, const std::string& b) const; |
| 16 | |
[email protected] | b6732ba | 2010-05-29 00:47:48 | [diff] [blame] | 17 | std::string GenerateSyntheticInput(size_t length, int seed) const; |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 18 | }; |
| 19 | |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 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()); |
| 26 | |
| 27 | courgette::SinkStream patch1; |
huangs | 7054b5a2 | 2016-07-26 21:46:13 | [diff] [blame] | 28 | bsdiff::BSDiffStatus status = |
| 29 | bsdiff::CreateBinaryPatch(&old1, &new1, &patch1); |
| 30 | EXPECT_EQ(bsdiff::OK, status); |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 31 | |
| 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; |
huangs | 7054b5a2 | 2016-07-26 21:46:13 | [diff] [blame] | 38 | status = bsdiff::ApplyBinaryPatch(&old2, &patch2, &new2); |
| 39 | EXPECT_EQ(bsdiff::OK, status); |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 40 | 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] | b6732ba | 2010-05-29 00:47:48 | [diff] [blame] | 44 | std::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] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 56 | TEST_F(BSDiffMemoryTest, TestEmpty) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 57 | GenerateAndTestPatch(std::string(), std::string()); |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST_F(BSDiffMemoryTest, TestEmptyVsNonempty) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 61 | GenerateAndTestPatch(std::string(), "xxx"); |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | TEST_F(BSDiffMemoryTest, TestNonemptyVsEmpty) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 65 | GenerateAndTestPatch("xxx", std::string()); |
[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST_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] | b6732ba | 2010-05-29 00:47:48 | [diff] [blame] | 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; |
| 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] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 113 | TEST_F(BSDiffMemoryTest, TestIndenticalDlls) { |
| 114 | std::string file1 = FileContents("en-US.dll"); |
| 115 | GenerateAndTestPatch(file1, file1); |
| 116 | } |
| 117 | |
| 118 | TEST_F(BSDiffMemoryTest, TestDifferentExes) { |
| 119 | std::string file1 = FileContents("setup1.exe"); |
| 120 | std::string file2 = FileContents("setup2.exe"); |
| 121 | GenerateAndTestPatch(file1, file2); |
| 122 | } |
[email protected] | 4b3d192b | 2011-11-08 20:32:26 | [diff] [blame] | 123 | |
| 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); |
| 128 | } |