Home | History | Annotate | Download | only in src
      1 // Copyright 2017 The Chromium OS 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 <string>
      6 #include <vector>
      7 
      8 #include "gtest/gtest.h"
      9 
     10 #include "puffin/src/include/puffin/common.h"
     11 #include "puffin/src/include/puffin/puffdiff.h"
     12 #include "puffin/src/include/puffin/puffpatch.h"
     13 #include "puffin/src/include/puffin/utils.h"
     14 #include "puffin/src/memory_stream.h"
     15 #include "puffin/src/puffin_stream.h"
     16 #include "puffin/src/sample_generator.h"
     17 #include "puffin/src/set_errors.h"
     18 #include "puffin/src/unittest_common.h"
     19 
     20 #define PRINT_SAMPLE 0  // Set to 1 if you want to print the generated samples.
     21 
     22 namespace puffin {
     23 
     24 using std::vector;
     25 using std::string;
     26 
     27 void TestPatching(const Buffer& src_buf,
     28                   const Buffer& dst_buf,
     29                   const vector<BitExtent>& src_deflates,
     30                   const vector<BitExtent>& dst_deflates,
     31                   const Buffer patch) {
     32   Buffer patch_out;
     33   string patch_path;
     34   ASSERT_TRUE(MakeTempFile(&patch_path, nullptr));
     35   ScopedPathUnlinker scoped_unlinker(patch_path);
     36   ASSERT_TRUE(PuffDiff(src_buf, dst_buf, src_deflates, dst_deflates, patch_path,
     37                        &patch_out));
     38 
     39 #if PRINT_SAMPLE
     40   sample_generator::PrintArray("kPatchXXXXX", patch_out);
     41 #endif
     42 
     43   EXPECT_EQ(patch_out, patch);
     44 
     45   auto src_stream = MemoryStream::CreateForRead(src_buf);
     46   Buffer dst_buf_out;
     47   auto dst_stream = MemoryStream::CreateForWrite(&dst_buf_out);
     48   ASSERT_TRUE(PuffPatch(std::move(src_stream), std::move(dst_stream),
     49                         patch.data(), patch.size()));
     50   EXPECT_EQ(dst_buf_out, dst_buf);
     51 }
     52 
     53 TEST(PatchingTest, Patching8To9Test) {
     54   TestPatching(kDeflates8, kDeflates9, kSubblockDeflateExtents8,
     55                kSubblockDeflateExtents9, kPatch8To9);
     56 }
     57 
     58 TEST(PatchingTest, Patching9To8Test) {
     59   TestPatching(kDeflates9, kDeflates8, kSubblockDeflateExtents9,
     60                kSubblockDeflateExtents8, kPatch9To8);
     61 }
     62 
     63 TEST(PatchingTest, Patching8ToEmptyTest) {
     64   TestPatching(kDeflates8, {}, kSubblockDeflateExtents8, {}, kPatch8ToEmpty);
     65 }
     66 
     67 TEST(PatchingTest, Patching8ToNoDeflateTest) {
     68   TestPatching(kDeflates8, {11, 22, 33, 44}, kSubblockDeflateExtents8, {},
     69                kPatch8ToNoDeflate);
     70 }
     71 
     72 // TODO(ahassani): add tests for:
     73 //   TestPatchingEmptyTo9
     74 //   TestPatchingNoDeflateTo9
     75 
     76 // TODO(ahassani): Change tests data if you decided to compress the header of
     77 // the patch.
     78 
     79 }  // namespace puffin
     80