xref: /aosp_15_r20/external/puffin/src/integration_test.cc (revision 07fb1d065b7cfb4729786fadd42a612532d2f466)
1*07fb1d06SElliott Hughes // Copyright 2021 The ChromiumOS Authors
2*07fb1d06SElliott Hughes // Use of this source code is governed by a BSD-style license that can be
3*07fb1d06SElliott Hughes // found in the LICENSE file.
4*07fb1d06SElliott Hughes 
5*07fb1d06SElliott Hughes #include <string>
6*07fb1d06SElliott Hughes #include <vector>
7*07fb1d06SElliott Hughes 
8*07fb1d06SElliott Hughes #include "gtest/gtest.h"
9*07fb1d06SElliott Hughes 
10*07fb1d06SElliott Hughes #include "puffin/memory_stream.h"
11*07fb1d06SElliott Hughes #include "puffin/src/include/puffin/puffdiff.h"
12*07fb1d06SElliott Hughes #include "puffin/src/include/puffin/puffpatch.h"
13*07fb1d06SElliott Hughes #include "puffin/src/include/puffin/utils.h"
14*07fb1d06SElliott Hughes #include "puffin/src/puffin_stream.h"
15*07fb1d06SElliott Hughes #include "puffin/src/unittest_common.h"
16*07fb1d06SElliott Hughes 
17*07fb1d06SElliott Hughes namespace puffin {
18*07fb1d06SElliott Hughes 
19*07fb1d06SElliott Hughes namespace {
20*07fb1d06SElliott Hughes // xxd -i <name>.zip
21*07fb1d06SElliott Hughes const Buffer kTestZipA = {
22*07fb1d06SElliott Hughes     0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x79,
23*07fb1d06SElliott Hughes     0x0d, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24*07fb1d06SElliott Hughes     0x00, 0x00, 0x01, 0x00, 0x1c, 0x00, 0x31, 0x55, 0x54, 0x09, 0x00, 0x03,
25*07fb1d06SElliott Hughes     0x5c, 0xed, 0x16, 0x61, 0x5c, 0xed, 0x16, 0x61, 0x75, 0x78, 0x0b, 0x00,
26*07fb1d06SElliott Hughes     0x01, 0x04, 0x8f, 0x66, 0x05, 0x00, 0x04, 0x53, 0x5f, 0x01, 0x00, 0x50,
27*07fb1d06SElliott Hughes     0x4b, 0x01, 0x02, 0x1e, 0x03, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
28*07fb1d06SElliott Hughes     0x79, 0x0d, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29*07fb1d06SElliott Hughes     0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30*07fb1d06SElliott Hughes     0x00, 0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x00, 0x00, 0x31, 0x55, 0x54,
31*07fb1d06SElliott Hughes     0x05, 0x00, 0x03, 0x5c, 0xed, 0x16, 0x61, 0x75, 0x78, 0x0b, 0x00, 0x01,
32*07fb1d06SElliott Hughes     0x04, 0x8f, 0x66, 0x05, 0x00, 0x04, 0x53, 0x5f, 0x01, 0x00, 0x50, 0x4b,
33*07fb1d06SElliott Hughes     0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x47, 0x00,
34*07fb1d06SElliott Hughes     0x00, 0x00, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x00};
35*07fb1d06SElliott Hughes 
36*07fb1d06SElliott Hughes const Buffer kTestZipB = {
37*07fb1d06SElliott Hughes     0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x79,
38*07fb1d06SElliott Hughes     0x0d, 0x53, 0x4e, 0x81, 0x88, 0x47, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00,
39*07fb1d06SElliott Hughes     0x00, 0x00, 0x01, 0x00, 0x1c, 0x00, 0x32, 0x55, 0x54, 0x09, 0x00, 0x03,
40*07fb1d06SElliott Hughes     0x88, 0xed, 0x16, 0x61, 0x88, 0xed, 0x16, 0x61, 0x75, 0x78, 0x0b, 0x00,
41*07fb1d06SElliott Hughes     0x01, 0x04, 0x8f, 0x66, 0x05, 0x00, 0x04, 0x53, 0x5f, 0x01, 0x00, 0x61,
42*07fb1d06SElliott Hughes     0x62, 0x63, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x0a, 0x00, 0x00,
43*07fb1d06SElliott Hughes     0x00, 0x00, 0x00, 0x26, 0x79, 0x0d, 0x53, 0x4e, 0x81, 0x88, 0x47, 0x04,
44*07fb1d06SElliott Hughes     0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00,
45*07fb1d06SElliott Hughes     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x00,
46*07fb1d06SElliott Hughes     0x00, 0x32, 0x55, 0x54, 0x05, 0x00, 0x03, 0x88, 0xed, 0x16, 0x61, 0x75,
47*07fb1d06SElliott Hughes     0x78, 0x0b, 0x00, 0x01, 0x04, 0x8f, 0x66, 0x05, 0x00, 0x04, 0x53, 0x5f,
48*07fb1d06SElliott Hughes     0x01, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
49*07fb1d06SElliott Hughes     0x01, 0x00, 0x47, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00};
50*07fb1d06SElliott Hughes 
51*07fb1d06SElliott Hughes }  // namespace
52*07fb1d06SElliott Hughes 
53*07fb1d06SElliott Hughes class PuffinIntegrationTest : public testing::TestWithParam<PatchAlgorithm> {
54*07fb1d06SElliott Hughes  protected:
getPatchType()55*07fb1d06SElliott Hughes   PatchAlgorithm getPatchType() { return GetParam(); }
56*07fb1d06SElliott Hughes };
57*07fb1d06SElliott Hughes 
TEST_P(PuffinIntegrationTest,PuffinDiffPatchTest)58*07fb1d06SElliott Hughes TEST_P(PuffinIntegrationTest, PuffinDiffPatchTest) {
59*07fb1d06SElliott Hughes   std::vector<BitExtent> src_deflates;
60*07fb1d06SElliott Hughes   ASSERT_TRUE(LocateDeflatesInZipArchive(kTestZipA, &src_deflates));
61*07fb1d06SElliott Hughes 
62*07fb1d06SElliott Hughes   std::vector<BitExtent> dst_deflates;
63*07fb1d06SElliott Hughes   ASSERT_TRUE(LocateDeflatesInZipArchive(kTestZipB, &dst_deflates));
64*07fb1d06SElliott Hughes 
65*07fb1d06SElliott Hughes   std::string tmp_file;
66*07fb1d06SElliott Hughes   ASSERT_TRUE(MakeTempFile(&tmp_file, nullptr));
67*07fb1d06SElliott Hughes   Buffer patch;
68*07fb1d06SElliott Hughes   ASSERT_TRUE(PuffDiff(MemoryStream::CreateForRead(kTestZipA),
69*07fb1d06SElliott Hughes                        MemoryStream::CreateForRead(kTestZipB), src_deflates,
70*07fb1d06SElliott Hughes                        dst_deflates, {bsdiff::CompressorType::kBrotli},
71*07fb1d06SElliott Hughes                        getPatchType(), tmp_file, &patch));
72*07fb1d06SElliott Hughes 
73*07fb1d06SElliott Hughes   Buffer patched;
74*07fb1d06SElliott Hughes   auto src_stream = MemoryStream::CreateForRead(kTestZipA);
75*07fb1d06SElliott Hughes   auto dst_stream = MemoryStream::CreateForWrite(&patched);
76*07fb1d06SElliott Hughes   ASSERT_TRUE(PuffPatch(MemoryStream::CreateForRead(kTestZipA),
77*07fb1d06SElliott Hughes                         MemoryStream::CreateForWrite(&patched), patch.data(),
78*07fb1d06SElliott Hughes                         patch.size()));
79*07fb1d06SElliott Hughes 
80*07fb1d06SElliott Hughes   ASSERT_EQ(kTestZipB, patched);
81*07fb1d06SElliott Hughes }
82*07fb1d06SElliott Hughes 
83*07fb1d06SElliott Hughes INSTANTIATE_TEST_CASE_P(TestWithPatchType,
84*07fb1d06SElliott Hughes                         PuffinIntegrationTest,
85*07fb1d06SElliott Hughes                         testing::Values(PatchAlgorithm::kBsdiff,
86*07fb1d06SElliott Hughes                                         PatchAlgorithm::kZucchini));
87*07fb1d06SElliott Hughes 
88*07fb1d06SElliott Hughes }  // namespace puffin