OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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. |
| 4 |
| 5 #include "courgette/third_party/bsdiff.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/file_util.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/string_util.h" |
| 12 |
| 13 #include "courgette/courgette.h" |
| 14 #include "courgette/streams.h" |
| 15 |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 class BSDiffMemoryTest : public testing::Test { |
| 19 public: |
| 20 std::string FileContents(const char *file_name) const; |
| 21 void GenerateAndTestPatch(const std::string& a, const std::string& b) const; |
| 22 |
| 23 private: |
| 24 void SetUp() { |
| 25 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_); |
| 26 file_util::AppendToPath(&test_dir_, L"courgette"); |
| 27 file_util::AppendToPath(&test_dir_, L"testdata"); |
| 28 } |
| 29 |
| 30 std::wstring test_dir_; |
| 31 }; |
| 32 |
| 33 // Reads a test file into a string. |
| 34 std::string BSDiffMemoryTest::FileContents(const char *file_name) const { |
| 35 std::wstring file_path = test_dir_; |
| 36 file_util::AppendToPath(&file_path, UTF8ToWide(file_name)); |
| 37 std::string file_bytes; |
| 38 if (!file_util::ReadFileToString(file_path, &file_bytes)) { |
| 39 EXPECT_TRUE(!"Could not read test data"); |
| 40 } |
| 41 return file_bytes; |
| 42 } |
| 43 |
| 44 void BSDiffMemoryTest::GenerateAndTestPatch(const std::string& old_text, |
| 45 const std::string& new_text) const { |
| 46 courgette::SourceStream old1; |
| 47 courgette::SourceStream new1; |
| 48 old1.Init(old_text.c_str(), old_text.length()); |
| 49 new1.Init(new_text.c_str(), new_text.length()); |
| 50 |
| 51 courgette::SinkStream patch1; |
| 52 courgette::BSDiffStatus status = CreateBinaryPatch(&old1, &new1, &patch1); |
| 53 EXPECT_EQ(courgette::OK, status); |
| 54 |
| 55 courgette::SourceStream old2; |
| 56 courgette::SourceStream patch2; |
| 57 old2.Init(old_text.c_str(), old_text.length()); |
| 58 patch2.Init(patch1); |
| 59 |
| 60 courgette::SinkStream new2; |
| 61 status = ApplyBinaryPatch(&old2, &patch2, &new2); |
| 62 EXPECT_EQ(courgette::OK, status); |
| 63 EXPECT_EQ(new_text.length(), new2.Length()); |
| 64 EXPECT_EQ(0, memcmp(new_text.c_str(), new2.Buffer(), new_text.length())); |
| 65 } |
| 66 |
| 67 TEST_F(BSDiffMemoryTest, TestEmpty) { |
| 68 GenerateAndTestPatch("", ""); |
| 69 } |
| 70 |
| 71 TEST_F(BSDiffMemoryTest, TestEmptyVsNonempty) { |
| 72 GenerateAndTestPatch("", "xxx"); |
| 73 } |
| 74 |
| 75 TEST_F(BSDiffMemoryTest, TestNonemptyVsEmpty) { |
| 76 GenerateAndTestPatch("xxx", ""); |
| 77 } |
| 78 |
| 79 TEST_F(BSDiffMemoryTest, TestSmallInputsWithSmallChanges) { |
| 80 std::string file1 = |
| 81 "I would not, could not, in a box.\n" |
| 82 "I could not, would not, with a fox.\n" |
| 83 "I will not eat them with a mouse.\n" |
| 84 "I will not eat them in a house.\n" |
| 85 "I will not eat them here or there.\n" |
| 86 "I will not eat them anywhere.\n" |
| 87 "I do not eat green eggs and ham.\n" |
| 88 "I do not like them, Sam-I-am.\n"; |
| 89 std::string file2 = |
| 90 "I would not, could not, in a BOX.\n" |
| 91 "I could not, would not, with a FOX.\n" |
| 92 "I will not eat them with a MOUSE.\n" |
| 93 "I will not eat them in a HOUSE.\n" |
| 94 "I will not eat them in a HOUSE.\n" // Extra line. |
| 95 "I will not eat them here or THERE.\n" |
| 96 "I will not eat them ANYWHERE.\n" |
| 97 "I do not eat green eggs and HAM.\n" |
| 98 "I do not like them, Sam-I-am.\n"; |
| 99 GenerateAndTestPatch(file1, file2); |
| 100 } |
| 101 |
| 102 TEST_F(BSDiffMemoryTest, TestIndenticalDlls) { |
| 103 std::string file1 = FileContents("en-US.dll"); |
| 104 GenerateAndTestPatch(file1, file1); |
| 105 } |
| 106 |
| 107 TEST_F(BSDiffMemoryTest, TestDifferentExes) { |
| 108 std::string file1 = FileContents("setup1.exe"); |
| 109 std::string file2 = FileContents("setup2.exe"); |
| 110 GenerateAndTestPatch(file1, file2); |
| 111 } |
OLD | NEW |