Home | History | Annotate | Download | only in courgette
      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 
      5 #include "courgette/third_party/bsdiff.h"
      6 
      7 #include "courgette/base_test_unittest.h"
      8 #include "courgette/courgette.h"
      9 #include "courgette/streams.h"
     10 
     11 class BSDiffMemoryTest : public BaseTest {
     12  public:
     13   void GenerateAndTestPatch(const std::string& a, const std::string& b) const;
     14 
     15   std::string GenerateSyntheticInput(size_t length, int seed) const;
     16 };
     17 
     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());
     24 
     25   courgette::SinkStream patch1;
     26   courgette::BSDiffStatus status = CreateBinaryPatch(&old1, &new1, &patch1);
     27   EXPECT_EQ(courgette::OK, status);
     28 
     29   courgette::SourceStream old2;
     30   courgette::SourceStream patch2;
     31   old2.Init(old_text.c_str(), old_text.length());
     32   patch2.Init(patch1);
     33 
     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()));
     39 }
     40 
     41 std::string BSDiffMemoryTest::GenerateSyntheticInput(size_t length, int seed)
     42   const {
     43   static const char* a[8] = {"O", "A", "x", "-", "y", ".", "|", ":"};
     44   std::string result;
     45   while (result.length() < length) {
     46     seed = (seed + 17) * 1049 + (seed >> 27);
     47     result.append(a[seed & 7]);
     48   }
     49   result.resize(length);
     50   return result;
     51 }
     52 
     53 TEST_F(BSDiffMemoryTest, TestEmpty) {
     54   GenerateAndTestPatch(std::string(), std::string());
     55 }
     56 
     57 TEST_F(BSDiffMemoryTest, TestEmptyVsNonempty) {
     58   GenerateAndTestPatch(std::string(), "xxx");
     59 }
     60 
     61 TEST_F(BSDiffMemoryTest, TestNonemptyVsEmpty) {
     62   GenerateAndTestPatch("xxx", std::string());
     63 }
     64 
     65 TEST_F(BSDiffMemoryTest, TestSmallInputsWithSmallChanges) {
     66   std::string file1 =
     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";
     75   std::string file2 =
     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);
     86 }
     87 
     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;
     92 
     93   // Test first-inputs with sizes that straddle the magic size to test this
     94   // PageArray's internal boundary condition.
     95 
     96   std::string file1 = GenerateSyntheticInput(critical_size, 0);
     97   std::string file2 = GenerateSyntheticInput(critical_size, 1);
     98   GenerateAndTestPatch(file1, file2);
     99 
    100   std::string file1a = file1.substr(0, critical_size - 1);
    101   GenerateAndTestPatch(file1a, file2);
    102 
    103   std::string file1b = file1.substr(0, critical_size - 2);
    104   GenerateAndTestPatch(file1b, file2);
    105 
    106   std::string file1c = file1 + file1.substr(0, 1);
    107   GenerateAndTestPatch(file1c, file2);
    108 }
    109 
    110 TEST_F(BSDiffMemoryTest, TestIndenticalDlls) {
    111   std::string file1 = FileContents("en-US.dll");
    112   GenerateAndTestPatch(file1, file1);
    113 }
    114 
    115 TEST_F(BSDiffMemoryTest, TestDifferentExes) {
    116   std::string file1 = FileContents("setup1.exe");
    117   std::string file2 = FileContents("setup2.exe");
    118   GenerateAndTestPatch(file1, file2);
    119 }
    120 
    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);
    125 }
    126