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/logging.h"
     15 #include "puffin/src/memory_stream.h"
     16 #include "puffin/src/puffin_stream.h"
     17 #include "puffin/src/unittest_common.h"
     18 
     19 #define PRINT_SAMPLE 0  // Set to 1 if you want to print the generated samples.
     20 
     21 using std::string;
     22 using std::vector;
     23 
     24 namespace puffin {
     25 
     26 namespace {
     27 
     28 #if PRINT_SAMPLE
     29 // Print an array into hex-format to the output. This can be used to create
     30 // static arrays for unit testing of the puffer/huffer.
     31 void PrintArray(const string& name, const Buffer& array) {
     32   std::cout << "const Buffer " << name << " = {" << std::endl << " ";
     33   for (size_t idx = 0; idx < array.size(); idx++) {
     34     std::cout << " 0x" << std::hex << std::uppercase << std::setfill('0')
     35               << std::setw(2) << uint(array[idx]);
     36     if (idx == array.size() - 1) {
     37       std::cout << std::dec << "};" << std::endl;
     38       return;
     39     }
     40     std::cout << ",";
     41     if ((idx + 1) % 12 == 0) {
     42       std::cout << std::endl << " ";
     43     }
     44   }
     45 }
     46 #endif
     47 
     48 const Buffer kPatch1To2 = {
     49     0x50, 0x55, 0x46, 0x31, 0x00, 0x00, 0x00, 0x51, 0x08, 0x01, 0x12, 0x27,
     50     0x0A, 0x04, 0x08, 0x10, 0x10, 0x32, 0x0A, 0x04, 0x08, 0x50, 0x10, 0x0A,
     51     0x0A, 0x04, 0x08, 0x60, 0x10, 0x12, 0x12, 0x04, 0x08, 0x10, 0x10, 0x58,
     52     0x12, 0x04, 0x08, 0x78, 0x10, 0x28, 0x12, 0x05, 0x08, 0xA8, 0x01, 0x10,
     53     0x38, 0x18, 0x1F, 0x1A, 0x24, 0x0A, 0x02, 0x10, 0x32, 0x0A, 0x04, 0x08,
     54     0x48, 0x10, 0x50, 0x0A, 0x05, 0x08, 0x98, 0x01, 0x10, 0x12, 0x12, 0x02,
     55     0x10, 0x58, 0x12, 0x04, 0x08, 0x70, 0x10, 0x58, 0x12, 0x05, 0x08, 0xC8,
     56     0x01, 0x10, 0x38, 0x18, 0x21, 0x42, 0x53, 0x44, 0x46, 0x32, 0x01, 0x01,
     57     0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00,
     58     0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     59     0x00, 0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xD1,
     60     0x20, 0xBB, 0x7E, 0x00, 0x00, 0x03, 0x60, 0x40, 0x78, 0x0E, 0x08, 0x00,
     61     0x40, 0x00, 0x20, 0x00, 0x31, 0x06, 0x4C, 0x40, 0x92, 0x8F, 0x46, 0xA7,
     62     0xA8, 0xE0, 0xF3, 0xD6, 0x21, 0x12, 0xF4, 0xBC, 0x43, 0x32, 0x1F, 0x17,
     63     0x72, 0x45, 0x38, 0x50, 0x90, 0xD1, 0x20, 0xBB, 0x7E, 0x42, 0x5A, 0x68,
     64     0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xF1, 0x20, 0x5F, 0x0D, 0x00,
     65     0x00, 0x02, 0x41, 0x15, 0x42, 0x08, 0x20, 0x00, 0x40, 0x00, 0x00, 0x02,
     66     0x40, 0x00, 0x20, 0x00, 0x22, 0x3D, 0x23, 0x10, 0x86, 0x03, 0x96, 0x54,
     67     0x11, 0x16, 0x5F, 0x17, 0x72, 0x45, 0x38, 0x50, 0x90, 0xF1, 0x20, 0x5F,
     68     0x0D, 0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x07,
     69     0xD4, 0xCB, 0x6E, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x20, 0x00,
     70     0x21, 0x18, 0x46, 0x82, 0xEE, 0x48, 0xA7, 0x0A, 0x12, 0x00, 0xFA, 0x99,
     71     0x6D, 0xC0};
     72 
     73 const Buffer kPatch2To1 = {
     74     0x50, 0x55, 0x46, 0x31, 0x00, 0x00, 0x00, 0x51, 0x08, 0x01, 0x12, 0x24,
     75     0x0A, 0x02, 0x10, 0x32, 0x0A, 0x04, 0x08, 0x48, 0x10, 0x50, 0x0A, 0x05,
     76     0x08, 0x98, 0x01, 0x10, 0x12, 0x12, 0x02, 0x10, 0x58, 0x12, 0x04, 0x08,
     77     0x70, 0x10, 0x58, 0x12, 0x05, 0x08, 0xC8, 0x01, 0x10, 0x38, 0x18, 0x21,
     78     0x1A, 0x27, 0x0A, 0x04, 0x08, 0x10, 0x10, 0x32, 0x0A, 0x04, 0x08, 0x50,
     79     0x10, 0x0A, 0x0A, 0x04, 0x08, 0x60, 0x10, 0x12, 0x12, 0x04, 0x08, 0x10,
     80     0x10, 0x58, 0x12, 0x04, 0x08, 0x78, 0x10, 0x28, 0x12, 0x05, 0x08, 0xA8,
     81     0x01, 0x10, 0x38, 0x18, 0x1F, 0x42, 0x53, 0x44, 0x46, 0x32, 0x01, 0x01,
     82     0x01, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00,
     83     0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     84     0x00, 0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x3D,
     85     0xBD, 0x08, 0x91, 0x00, 0x00, 0x01, 0xE0, 0x40, 0x5C, 0x0A, 0x40, 0x00,
     86     0x40, 0x00, 0x20, 0x00, 0x31, 0x0C, 0x08, 0x23, 0xD2, 0x34, 0xD1, 0xB1,
     87     0x73, 0x60, 0x44, 0x54, 0xE4, 0xFC, 0x5D, 0xC9, 0x14, 0xE1, 0x42, 0x40,
     88     0xF6, 0xF4, 0x22, 0x44, 0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26,
     89     0x53, 0x59, 0x41, 0x62, 0x2E, 0xF0, 0x00, 0x00, 0x00, 0x40, 0x00, 0x40,
     90     0x20, 0x20, 0x00, 0x21, 0x00, 0x82, 0x83, 0x17, 0x72, 0x45, 0x38, 0x50,
     91     0x90, 0x41, 0x62, 0x2E, 0xF0, 0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59,
     92     0x26, 0x53, 0x59, 0xE0, 0x20, 0x04, 0x57, 0x00, 0x00, 0x04, 0x76, 0x50,
     93     0xE0, 0x00, 0x20, 0x00, 0x10, 0x00, 0x04, 0x00, 0x02, 0x00, 0x20, 0x00,
     94     0x40, 0x00, 0x00, 0x00, 0xA0, 0x00, 0x21, 0xA1, 0xA3, 0x10, 0x83, 0x26,
     95     0x21, 0x5E, 0xB2, 0x69, 0xAC, 0x70, 0x60, 0x53, 0xC5, 0xDC, 0x91, 0x4E,
     96     0x14, 0x24, 0x38, 0x08, 0x01, 0x15, 0xC0};
     97 
     98 const Buffer kPatch1ToEmpty = {
     99     0x50, 0x55, 0x46, 0x31, 0x00, 0x00, 0x00, 0x2D, 0x08, 0x01, 0x12, 0x27,
    100     0x0A, 0x04, 0x08, 0x10, 0x10, 0x32, 0x0A, 0x04, 0x08, 0x50, 0x10, 0x0A,
    101     0x0A, 0x04, 0x08, 0x60, 0x10, 0x12, 0x12, 0x04, 0x08, 0x10, 0x10, 0x58,
    102     0x12, 0x04, 0x08, 0x78, 0x10, 0x28, 0x12, 0x05, 0x08, 0xA8, 0x01, 0x10,
    103     0x38, 0x18, 0x1F, 0x1A, 0x00, 0x42, 0x53, 0x44, 0x46, 0x32, 0x01, 0x01,
    104     0x01, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00,
    105     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    106     0x00, 0x42, 0x5A, 0x68, 0x39, 0x17, 0x72, 0x45, 0x38, 0x50, 0x90, 0x00,
    107     0x00, 0x00, 0x00, 0x42, 0x5A, 0x68, 0x39, 0x17, 0x72, 0x45, 0x38, 0x50,
    108     0x90, 0x00, 0x00, 0x00, 0x00, 0x42, 0x5A, 0x68, 0x39, 0x17, 0x72, 0x45,
    109     0x38, 0x50, 0x90, 0x00, 0x00, 0x00, 0x00};
    110 
    111 const Buffer kPatch1ToNoDeflate = {
    112     0x50, 0x55, 0x46, 0x31, 0x00, 0x00, 0x00, 0x2F, 0x08, 0x01, 0x12, 0x27,
    113     0x0A, 0x04, 0x08, 0x10, 0x10, 0x32, 0x0A, 0x04, 0x08, 0x50, 0x10, 0x0A,
    114     0x0A, 0x04, 0x08, 0x60, 0x10, 0x12, 0x12, 0x04, 0x08, 0x10, 0x10, 0x58,
    115     0x12, 0x04, 0x08, 0x78, 0x10, 0x28, 0x12, 0x05, 0x08, 0xA8, 0x01, 0x10,
    116     0x38, 0x18, 0x1F, 0x1A, 0x02, 0x18, 0x04, 0x42, 0x53, 0x44, 0x46, 0x32,
    117     0x01, 0x01, 0x01, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E,
    118     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
    119     0x00, 0x00, 0x00, 0x42, 0x5A, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53,
    120     0x59, 0xBA, 0x8D, 0x7F, 0x2D, 0x00, 0x00, 0x00, 0x40, 0x00, 0x44, 0x08,
    121     0x20, 0x00, 0x30, 0xCC, 0x09, 0x32, 0x54, 0x65, 0x38, 0xBB, 0x92, 0x29,
    122     0xC2, 0x84, 0x85, 0xD4, 0x6B, 0xF9, 0x68, 0x42, 0x5A, 0x68, 0x39, 0x17,
    123     0x72, 0x45, 0x38, 0x50, 0x90, 0x00, 0x00, 0x00, 0x00, 0x42, 0x5A, 0x68,
    124     0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xE7, 0xAA, 0xF1, 0xFC, 0x00,
    125     0x00, 0x00, 0x70, 0x00, 0x00, 0x08, 0x01, 0x00, 0x20, 0x04, 0x20, 0x00,
    126     0x21, 0x9A, 0x68, 0x33, 0x4D, 0x13, 0x3C, 0x5D, 0xC9, 0x14, 0xE1, 0x42,
    127     0x43, 0x9E, 0xAB, 0xC7, 0xF0};
    128 
    129 }  // namespace
    130 
    131 void TestPatching(const Buffer& src_buf,
    132                   const Buffer& dst_buf,
    133                   const vector<BitExtent>& src_deflates,
    134                   const vector<BitExtent>& dst_deflates,
    135                   const Buffer patch) {
    136   Buffer patch_out;
    137   string patch_path;
    138   ASSERT_TRUE(MakeTempFile(&patch_path, nullptr));
    139   ScopedPathUnlinker scoped_unlinker(patch_path);
    140   ASSERT_TRUE(PuffDiff(src_buf, dst_buf, src_deflates, dst_deflates,
    141                        {bsdiff::CompressorType::kBZ2}, patch_path, &patch_out));
    142 
    143 #if PRINT_SAMPLE
    144   PrintArray("kPatchXXXXX", patch_out);
    145 #endif
    146 
    147   EXPECT_EQ(patch_out, patch);
    148 
    149   auto src_stream = MemoryStream::CreateForRead(src_buf);
    150   Buffer dst_buf_out(dst_buf.size());
    151   auto dst_stream = MemoryStream::CreateForWrite(&dst_buf_out);
    152   ASSERT_TRUE(PuffPatch(std::move(src_stream), std::move(dst_stream),
    153                         patch.data(), patch.size()));
    154   EXPECT_EQ(dst_buf_out, dst_buf);
    155 }
    156 
    157 TEST(PatchingTest, Patching1To2Test) {
    158   TestPatching(kDeflatesSample1, kDeflatesSample2,
    159                kSubblockDeflateExtentsSample1, kSubblockDeflateExtentsSample2,
    160                kPatch1To2);
    161 }
    162 
    163 TEST(PatchingTest, Patching2To1Test) {
    164   TestPatching(kDeflatesSample2, kDeflatesSample1,
    165                kSubblockDeflateExtentsSample2, kSubblockDeflateExtentsSample1,
    166                kPatch2To1);
    167 }
    168 
    169 TEST(PatchingTest, Patching1ToEmptyTest) {
    170   TestPatching(kDeflatesSample1, {}, kSubblockDeflateExtentsSample1, {},
    171                kPatch1ToEmpty);
    172 }
    173 
    174 TEST(PatchingTest, Patching1ToNoDeflateTest) {
    175   TestPatching(kDeflatesSample1, {11, 22, 33, 44},
    176                kSubblockDeflateExtentsSample1, {}, kPatch1ToNoDeflate);
    177 }
    178 
    179 // TODO(ahassani): add tests for:
    180 //   TestPatchingEmptyTo2
    181 //   TestPatchingNoDeflateTo2
    182 
    183 // TODO(ahassani): Change tests data if you decided to compress the header of
    184 // the patch.
    185 
    186 }  // namespace puffin
    187