bsdiff_memory_unittest.cc 4.1 KB

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