1*07fb1d06SElliott Hughes // Copyright 2017 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 <unistd.h>
6*07fb1d06SElliott Hughes
7*07fb1d06SElliott Hughes #include <vector>
8*07fb1d06SElliott Hughes
9*07fb1d06SElliott Hughes #include "gtest/gtest.h"
10*07fb1d06SElliott Hughes
11*07fb1d06SElliott Hughes #include "puffin/file_stream.h"
12*07fb1d06SElliott Hughes #include "puffin/memory_stream.h"
13*07fb1d06SElliott Hughes #include "puffin/src/include/puffin/common.h"
14*07fb1d06SElliott Hughes #include "puffin/src/include/puffin/utils.h"
15*07fb1d06SElliott Hughes #include "puffin/src/unittest_common.h"
16*07fb1d06SElliott Hughes
17*07fb1d06SElliott Hughes using std::string;
18*07fb1d06SElliott Hughes using std::vector;
19*07fb1d06SElliott Hughes
20*07fb1d06SElliott Hughes namespace puffin {
21*07fb1d06SElliott Hughes
22*07fb1d06SElliott Hughes namespace {
23*07fb1d06SElliott Hughes const uint8_t kZipEntries[] = {
24*07fb1d06SElliott Hughes 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x02, 0x00, 0x08, 0x00, 0xfc, 0x88,
25*07fb1d06SElliott Hughes 0x28, 0x4c, 0xcb, 0x86, 0xe1, 0x80, 0x06, 0x00, 0x00, 0x00, 0x09, 0x00,
26*07fb1d06SElliott Hughes 0x00, 0x00, 0x01, 0x00, 0x1c, 0x00, 0x31, 0x55, 0x54, 0x09, 0x00, 0x03,
27*07fb1d06SElliott Hughes 0xec, 0x15, 0x54, 0x5a, 0x49, 0x10, 0x54, 0x5a, 0x75, 0x78, 0x0b, 0x00,
28*07fb1d06SElliott Hughes 0x01, 0x04, 0x8f, 0x66, 0x05, 0x00, 0x04, 0x88, 0x13, 0x00, 0x00, 0x33,
29*07fb1d06SElliott Hughes 0x34, 0x84, 0x00, 0x2e, 0x00, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x02,
30*07fb1d06SElliott Hughes 0x00, 0x08, 0x00, 0x01, 0x89, 0x28, 0x4c, 0xe0, 0xe8, 0x6f, 0x6d, 0x06,
31*07fb1d06SElliott Hughes 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x1c, 0x00, 0x32,
32*07fb1d06SElliott Hughes 0x55, 0x54, 0x09, 0x00, 0x03, 0xf1, 0x15, 0x54, 0x5a, 0x38, 0x10, 0x54,
33*07fb1d06SElliott Hughes 0x5a, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0x8f, 0x66, 0x05, 0x00, 0x04,
34*07fb1d06SElliott Hughes 0x88, 0x13, 0x00, 0x00, 0x33, 0x32, 0x82, 0x01, 0x2e, 0x00};
35*07fb1d06SElliott Hughes
36*07fb1d06SElliott Hughes // (echo "666666" > 2 && zip -fd test.zip 2 &&
37*07fb1d06SElliott Hughes // cat test.zip | hexdump -v -e '10/1 "0x%02x, " "\n"')
38*07fb1d06SElliott Hughes const uint8_t kZipEntryWithDataDescriptor[] = {
39*07fb1d06SElliott Hughes 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b, 0x74,
40*07fb1d06SElliott Hughes 0x2b, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00,
41*07fb1d06SElliott Hughes 0x00, 0x00, 0x01, 0x00, 0x1c, 0x00, 0x32, 0x55, 0x54, 0x09, 0x00, 0x03,
42*07fb1d06SElliott Hughes 0xf5, 0xe5, 0x57, 0x5a, 0xf2, 0xe5, 0x57, 0x5a, 0x75, 0x78, 0x0b, 0x00,
43*07fb1d06SElliott Hughes 0x01, 0x04, 0x8f, 0x66, 0x05, 0x00, 0x04, 0x88, 0x13, 0x00, 0x00, 0x33,
44*07fb1d06SElliott Hughes 0x33, 0x03, 0x01, 0x2e, 0x00, 0x50, 0x4b, 0x07, 0x08, 0xb4, 0xa0, 0xf2,
45*07fb1d06SElliott Hughes 0x36, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x03,
46*07fb1d06SElliott Hughes 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b, 0x74, 0x2b, 0x4c, 0x00,
47*07fb1d06SElliott Hughes 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01,
48*07fb1d06SElliott Hughes 0x00, 0x1c, 0x00, 0x32, 0x55, 0x54, 0x09, 0x00, 0x03, 0xf5, 0xe5, 0x57,
49*07fb1d06SElliott Hughes 0x5a, 0xf2, 0xe5, 0x57, 0x5a, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0x8f,
50*07fb1d06SElliott Hughes 0x66, 0x05, 0x00, 0x04, 0x88, 0x13, 0x00, 0x00, 0x33, 0x33, 0x03, 0x01,
51*07fb1d06SElliott Hughes 0x2e, 0x00, 0xb4, 0xa0, 0xf2, 0x36, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00,
52*07fb1d06SElliott Hughes 0x00, 0x00};
53*07fb1d06SElliott Hughes
54*07fb1d06SElliott Hughes // echo "0123456789" > test1.txt && echo "9876543210" > test2.txt &&
55*07fb1d06SElliott Hughes // gzip -kf test1.txt test2.txt && cat test1.txt.gz test2.txt.gz |
56*07fb1d06SElliott Hughes // hexdump -v -e '12/1 "0x%02x, " "\n"'
57*07fb1d06SElliott Hughes const uint8_t kGzipEntryWithMultipleMembers[] = {
58*07fb1d06SElliott Hughes 0x1f, 0x8b, 0x08, 0x08, 0x77, 0xd5, 0x84, 0x5a, 0x00, 0x03, 0x74, 0x65,
59*07fb1d06SElliott Hughes 0x73, 0x74, 0x31, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x33, 0x30, 0x34, 0x32,
60*07fb1d06SElliott Hughes 0x36, 0x31, 0x35, 0x33, 0xb7, 0xb0, 0xe4, 0x02, 0x00, 0xd1, 0xe5, 0x76,
61*07fb1d06SElliott Hughes 0x40, 0x0b, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x08, 0x77, 0xd5, 0x84,
62*07fb1d06SElliott Hughes 0x5a, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x32, 0x2e, 0x74, 0x78, 0x74,
63*07fb1d06SElliott Hughes 0x00, 0xb3, 0xb4, 0x30, 0x37, 0x33, 0x35, 0x31, 0x36, 0x32, 0x34, 0xe0,
64*07fb1d06SElliott Hughes 0x02, 0x00, 0x20, 0x9c, 0x5f, 0x89, 0x0b, 0x00, 0x00, 0x00};
65*07fb1d06SElliott Hughes
66*07fb1d06SElliott Hughes // echo "0123456789" > test1.txt && gzip -kf test1.txt && cat test1.txt.gz |
67*07fb1d06SElliott Hughes // hexdump -v -e '12/1 "0x%02x, " "\n"'
68*07fb1d06SElliott Hughes // And manually insert extra field with two byte length (10) followed by:
69*07fb1d06SElliott Hughes // echo "extrafield" | hexdump -v -e '12/1 "0x%02x, " "\n"'
70*07fb1d06SElliott Hughes // Then change the forth byte of array to -x0c to enable the extra field.
71*07fb1d06SElliott Hughes const uint8_t kGzipEntryWithExtraField[] = {
72*07fb1d06SElliott Hughes 0x1f, 0x8b, 0x08, 0x0c, 0xcf, 0x0e, 0x86, 0x5a, 0x00, 0x03,
73*07fb1d06SElliott Hughes // Extra field begin
74*07fb1d06SElliott Hughes 0x0A, 0x00, 0x65, 0x78, 0x74, 0x72, 0x61, 0x66, 0x69, 0x65, 0x6c, 0x64,
75*07fb1d06SElliott Hughes // Extra field end
76*07fb1d06SElliott Hughes 0x74, 0x65, 0x73, 0x74, 0x31, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x33, 0x30,
77*07fb1d06SElliott Hughes 0x34, 0x32, 0x36, 0x31, 0x35, 0x33, 0xb7, 0xb0, 0xe4, 0x02, 0x00, 0xd1,
78*07fb1d06SElliott Hughes 0xe5, 0x76, 0x40, 0x0b, 0x00, 0x00, 0x00};
79*07fb1d06SElliott Hughes
80*07fb1d06SElliott Hughes // echo "0123456789" | zlib-flate -compress |
81*07fb1d06SElliott Hughes // hexdump -v -e '12/1 "0x%02x, " "\n"'
82*07fb1d06SElliott Hughes const uint8_t kZlibEntry[] = {0x78, 0x9c, 0x33, 0x30, 0x34, 0x32, 0x36,
83*07fb1d06SElliott Hughes 0x31, 0x35, 0x33, 0xb7, 0xb0, 0xe4, 0x02,
84*07fb1d06SElliott Hughes 0x00, 0x0d, 0x17, 0x02, 0x18};
85*07fb1d06SElliott Hughes
FindDeflatesInZlibBlocks(const Buffer & src,const vector<ByteExtent> & zlibs,const vector<BitExtent> & deflates)86*07fb1d06SElliott Hughes void FindDeflatesInZlibBlocks(const Buffer& src,
87*07fb1d06SElliott Hughes const vector<ByteExtent>& zlibs,
88*07fb1d06SElliott Hughes const vector<BitExtent>& deflates) {
89*07fb1d06SElliott Hughes string tmp_file;
90*07fb1d06SElliott Hughes ASSERT_TRUE(MakeTempFile(&tmp_file, nullptr));
91*07fb1d06SElliott Hughes ScopedPathUnlinker unlinker(tmp_file);
92*07fb1d06SElliott Hughes auto src_stream = FileStream::Open(tmp_file, false, true);
93*07fb1d06SElliott Hughes ASSERT_TRUE(src_stream);
94*07fb1d06SElliott Hughes ASSERT_TRUE(src_stream->Write(src.data(), src.size()));
95*07fb1d06SElliott Hughes ASSERT_TRUE(src_stream->Close());
96*07fb1d06SElliott Hughes
97*07fb1d06SElliott Hughes vector<BitExtent> deflates_out;
98*07fb1d06SElliott Hughes ASSERT_TRUE(LocateDeflatesInZlibBlocks(tmp_file, zlibs, &deflates_out));
99*07fb1d06SElliott Hughes ASSERT_EQ(deflates, deflates_out);
100*07fb1d06SElliott Hughes }
101*07fb1d06SElliott Hughes
CheckFindPuffLocation(const Buffer & compressed,const vector<BitExtent> & deflates,const vector<ByteExtent> & expected_puffs,uint64_t expected_puff_size)102*07fb1d06SElliott Hughes void CheckFindPuffLocation(const Buffer& compressed,
103*07fb1d06SElliott Hughes const vector<BitExtent>& deflates,
104*07fb1d06SElliott Hughes const vector<ByteExtent>& expected_puffs,
105*07fb1d06SElliott Hughes uint64_t expected_puff_size) {
106*07fb1d06SElliott Hughes auto src = MemoryStream::CreateForRead(compressed);
107*07fb1d06SElliott Hughes vector<ByteExtent> puffs;
108*07fb1d06SElliott Hughes uint64_t puff_size;
109*07fb1d06SElliott Hughes ASSERT_TRUE(FindPuffLocations(src, deflates, &puffs, &puff_size));
110*07fb1d06SElliott Hughes EXPECT_EQ(puffs, expected_puffs);
111*07fb1d06SElliott Hughes EXPECT_EQ(puff_size, expected_puff_size);
112*07fb1d06SElliott Hughes }
113*07fb1d06SElliott Hughes } // namespace
114*07fb1d06SElliott Hughes
115*07fb1d06SElliott Hughes // Test Simple Puffing of the source.
TEST(UtilsTest,FindPuffLocations1Test)116*07fb1d06SElliott Hughes TEST(UtilsTest, FindPuffLocations1Test) {
117*07fb1d06SElliott Hughes CheckFindPuffLocation(kDeflatesSample1, kSubblockDeflateExtentsSample1,
118*07fb1d06SElliott Hughes kPuffExtentsSample1, kPuffsSample1.size());
119*07fb1d06SElliott Hughes }
120*07fb1d06SElliott Hughes
TEST(UtilsTest,FindPuffLocations2Test)121*07fb1d06SElliott Hughes TEST(UtilsTest, FindPuffLocations2Test) {
122*07fb1d06SElliott Hughes CheckFindPuffLocation(kDeflatesSample2, kSubblockDeflateExtentsSample2,
123*07fb1d06SElliott Hughes kPuffExtentsSample2, kPuffsSample2.size());
124*07fb1d06SElliott Hughes }
125*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInZlib)126*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInZlib) {
127*07fb1d06SElliott Hughes Buffer zlib_data(kZlibEntry, std::end(kZlibEntry));
128*07fb1d06SElliott Hughes vector<BitExtent> deflates;
129*07fb1d06SElliott Hughes vector<BitExtent> expected_deflates = {{16, 98}};
130*07fb1d06SElliott Hughes EXPECT_TRUE(LocateDeflatesInZlib(zlib_data, &deflates));
131*07fb1d06SElliott Hughes EXPECT_EQ(deflates, expected_deflates);
132*07fb1d06SElliott Hughes }
133*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInEmptyZlib)134*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInEmptyZlib) {
135*07fb1d06SElliott Hughes Buffer empty;
136*07fb1d06SElliott Hughes vector<ByteExtent> empty_zlibs;
137*07fb1d06SElliott Hughes vector<BitExtent> empty_deflates;
138*07fb1d06SElliott Hughes FindDeflatesInZlibBlocks(empty, empty_zlibs, empty_deflates);
139*07fb1d06SElliott Hughes }
140*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInZlibWithInvalidFields)141*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInZlibWithInvalidFields) {
142*07fb1d06SElliott Hughes Buffer zlib_data(kZlibEntry, std::end(kZlibEntry));
143*07fb1d06SElliott Hughes auto cmf = zlib_data[0];
144*07fb1d06SElliott Hughes auto flag = zlib_data[1];
145*07fb1d06SElliott Hughes
146*07fb1d06SElliott Hughes vector<BitExtent> deflates;
147*07fb1d06SElliott Hughes zlib_data[0] = cmf & 0xF0;
148*07fb1d06SElliott Hughes EXPECT_FALSE(LocateDeflatesInZlib(zlib_data, &deflates));
149*07fb1d06SElliott Hughes zlib_data[0] = cmf | (8 << 4);
150*07fb1d06SElliott Hughes EXPECT_FALSE(LocateDeflatesInZlib(zlib_data, &deflates));
151*07fb1d06SElliott Hughes zlib_data[0] = cmf; // Correct it.
152*07fb1d06SElliott Hughes
153*07fb1d06SElliott Hughes zlib_data[1] = flag & 0xF0;
154*07fb1d06SElliott Hughes EXPECT_FALSE(LocateDeflatesInZlib(zlib_data, &deflates));
155*07fb1d06SElliott Hughes }
156*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInZipArchiveSmoke)157*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInZipArchiveSmoke) {
158*07fb1d06SElliott Hughes Buffer zip_entries(kZipEntries, std::end(kZipEntries));
159*07fb1d06SElliott Hughes vector<BitExtent> deflates;
160*07fb1d06SElliott Hughes vector<BitExtent> expected_deflates = {{472, 46}, {992, 46}};
161*07fb1d06SElliott Hughes EXPECT_TRUE(LocateDeflatesInZipArchive(zip_entries, &deflates));
162*07fb1d06SElliott Hughes EXPECT_EQ(deflates, expected_deflates);
163*07fb1d06SElliott Hughes }
164*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInZipArchiveWithDataDescriptor)165*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInZipArchiveWithDataDescriptor) {
166*07fb1d06SElliott Hughes Buffer zip_entries(kZipEntryWithDataDescriptor,
167*07fb1d06SElliott Hughes std::end(kZipEntryWithDataDescriptor));
168*07fb1d06SElliott Hughes vector<BitExtent> deflates;
169*07fb1d06SElliott Hughes vector<BitExtent> expected_deflates = {{472, 46}, {1120, 46}};
170*07fb1d06SElliott Hughes EXPECT_TRUE(LocateDeflatesInZipArchive(zip_entries, &deflates));
171*07fb1d06SElliott Hughes EXPECT_EQ(deflates, expected_deflates);
172*07fb1d06SElliott Hughes }
173*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInZipArchiveErrorChecks)174*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInZipArchiveErrorChecks) {
175*07fb1d06SElliott Hughes Buffer zip_entries(kZipEntries, std::end(kZipEntries));
176*07fb1d06SElliott Hughes // Construct a invalid zip entry whose size overflows.
177*07fb1d06SElliott Hughes zip_entries[29] = 0xff;
178*07fb1d06SElliott Hughes vector<BitExtent> deflates_overflow;
179*07fb1d06SElliott Hughes vector<BitExtent> expected_deflates = {{992, 46}};
180*07fb1d06SElliott Hughes EXPECT_TRUE(LocateDeflatesInZipArchive(zip_entries, &deflates_overflow));
181*07fb1d06SElliott Hughes EXPECT_EQ(deflates_overflow, expected_deflates);
182*07fb1d06SElliott Hughes
183*07fb1d06SElliott Hughes zip_entries.resize(128);
184*07fb1d06SElliott Hughes vector<BitExtent> deflates_incomplete;
185*07fb1d06SElliott Hughes EXPECT_TRUE(LocateDeflatesInZipArchive(zip_entries, &deflates_incomplete));
186*07fb1d06SElliott Hughes EXPECT_TRUE(deflates_incomplete.empty());
187*07fb1d06SElliott Hughes }
188*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInGzip)189*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInGzip) {
190*07fb1d06SElliott Hughes Buffer gzip_data(kGzipEntryWithMultipleMembers,
191*07fb1d06SElliott Hughes std::end(kGzipEntryWithMultipleMembers));
192*07fb1d06SElliott Hughes vector<BitExtent> deflates;
193*07fb1d06SElliott Hughes vector<BitExtent> expected_deflates = {{160, 98}, {488, 98}};
194*07fb1d06SElliott Hughes EXPECT_TRUE(LocateDeflatesInGzip(gzip_data, &deflates));
195*07fb1d06SElliott Hughes EXPECT_EQ(deflates, expected_deflates);
196*07fb1d06SElliott Hughes }
197*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInGzipFail)198*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInGzipFail) {
199*07fb1d06SElliott Hughes Buffer gzip_data(kGzipEntryWithMultipleMembers,
200*07fb1d06SElliott Hughes std::end(kGzipEntryWithMultipleMembers));
201*07fb1d06SElliott Hughes gzip_data[0] ^= 1;
202*07fb1d06SElliott Hughes vector<BitExtent> deflates;
203*07fb1d06SElliott Hughes EXPECT_FALSE(LocateDeflatesInGzip(gzip_data, &deflates));
204*07fb1d06SElliott Hughes }
205*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInGzipWithPadding)206*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInGzipWithPadding) {
207*07fb1d06SElliott Hughes Buffer gzip_data(kGzipEntryWithMultipleMembers,
208*07fb1d06SElliott Hughes std::end(kGzipEntryWithMultipleMembers));
209*07fb1d06SElliott Hughes gzip_data.resize(gzip_data.size() + 100);
210*07fb1d06SElliott Hughes vector<BitExtent> deflates;
211*07fb1d06SElliott Hughes vector<BitExtent> expected_deflates = {{160, 98}, {488, 98}};
212*07fb1d06SElliott Hughes EXPECT_TRUE(LocateDeflatesInGzip(gzip_data, &deflates));
213*07fb1d06SElliott Hughes EXPECT_EQ(deflates, expected_deflates);
214*07fb1d06SElliott Hughes }
215*07fb1d06SElliott Hughes
TEST(UtilsTest,LocateDeflatesInGzipWithExtraField)216*07fb1d06SElliott Hughes TEST(UtilsTest, LocateDeflatesInGzipWithExtraField) {
217*07fb1d06SElliott Hughes Buffer gzip_data(kGzipEntryWithExtraField,
218*07fb1d06SElliott Hughes std::end(kGzipEntryWithExtraField));
219*07fb1d06SElliott Hughes vector<BitExtent> deflates;
220*07fb1d06SElliott Hughes vector<BitExtent> expected_deflates = {{256, 98}};
221*07fb1d06SElliott Hughes EXPECT_TRUE(LocateDeflatesInGzip(gzip_data, &deflates));
222*07fb1d06SElliott Hughes EXPECT_EQ(deflates, expected_deflates);
223*07fb1d06SElliott Hughes }
224*07fb1d06SElliott Hughes
TEST(UtilsTest,RemoveEqualBitExtents)225*07fb1d06SElliott Hughes TEST(UtilsTest, RemoveEqualBitExtents) {
226*07fb1d06SElliott Hughes Buffer data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
227*07fb1d06SElliott Hughes Buffer data2 = {1, 2, 3, 4, 5, 5, 6, 7, 8, 9};
228*07fb1d06SElliott Hughes vector<BitExtent> ext1 = {{0, 10}, {10, 14}, {25, 15}, {40, 8}, {50, 23}};
229*07fb1d06SElliott Hughes vector<BitExtent> ext2 = {{0, 10}, {17, 15}, {32, 8}, {40, 8}, {50, 23}};
230*07fb1d06SElliott Hughes RemoveEqualBitExtents(data1, data2, &ext1, &ext2);
231*07fb1d06SElliott Hughes vector<BitExtent> expected_ext1 = {{0, 10}, {10, 14}};
232*07fb1d06SElliott Hughes EXPECT_EQ(expected_ext1, ext1);
233*07fb1d06SElliott Hughes vector<BitExtent> expected_ext2 = {{0, 10}};
234*07fb1d06SElliott Hughes EXPECT_EQ(expected_ext2, ext2);
235*07fb1d06SElliott Hughes RemoveEqualBitExtents(data1, data2, &ext1, &ext1);
236*07fb1d06SElliott Hughes EXPECT_EQ(expected_ext1, ext1);
237*07fb1d06SElliott Hughes RemoveEqualBitExtents(data1, data1, &ext1, &ext1);
238*07fb1d06SElliott Hughes EXPECT_TRUE(ext1.empty());
239*07fb1d06SElliott Hughes expected_ext1 = ext1 = {{0, 0}, {1, 1}, {2, 7}};
240*07fb1d06SElliott Hughes RemoveEqualBitExtents(data1, data2, &ext1, &ext2);
241*07fb1d06SElliott Hughes EXPECT_EQ(expected_ext1, ext1);
242*07fb1d06SElliott Hughes EXPECT_EQ(expected_ext2, ext2);
243*07fb1d06SElliott Hughes }
244*07fb1d06SElliott Hughes
TEST(UtilsTest,RemoveDeflatesWithBadDistanceCaches)245*07fb1d06SElliott Hughes TEST(UtilsTest, RemoveDeflatesWithBadDistanceCaches) {
246*07fb1d06SElliott Hughes vector<BitExtent> deflates(kProblematicCacheDeflateExtents), empty;
247*07fb1d06SElliott Hughes EXPECT_TRUE(
248*07fb1d06SElliott Hughes RemoveDeflatesWithBadDistanceCaches(kProblematicCache, &deflates));
249*07fb1d06SElliott Hughes EXPECT_EQ(deflates, empty);
250*07fb1d06SElliott Hughes
251*07fb1d06SElliott Hughes // Just a sanity check to make sure this function is not removing anything
252*07fb1d06SElliott Hughes // else.
253*07fb1d06SElliott Hughes deflates = kSubblockDeflateExtentsSample1;
254*07fb1d06SElliott Hughes EXPECT_TRUE(RemoveDeflatesWithBadDistanceCaches(kDeflatesSample1, &deflates));
255*07fb1d06SElliott Hughes EXPECT_EQ(deflates, kSubblockDeflateExtentsSample1);
256*07fb1d06SElliott Hughes
257*07fb1d06SElliott Hughes // Now combine three deflates and make sure it is doing the right job.
258*07fb1d06SElliott Hughes Buffer data;
259*07fb1d06SElliott Hughes data.insert(data.end(), kDeflatesSample1.begin(), kDeflatesSample1.end());
260*07fb1d06SElliott Hughes data.insert(data.end(), kProblematicCache.begin(), kProblematicCache.end());
261*07fb1d06SElliott Hughes data.insert(data.end(), kDeflatesSample1.begin(), kDeflatesSample1.end());
262*07fb1d06SElliott Hughes
263*07fb1d06SElliott Hughes deflates = kSubblockDeflateExtentsSample1;
264*07fb1d06SElliott Hughes size_t offset = kDeflatesSample1.size() * 8;
265*07fb1d06SElliott Hughes for (const auto& deflate : kProblematicCacheDeflateExtents) {
266*07fb1d06SElliott Hughes deflates.emplace_back(deflate.offset + offset, deflate.length);
267*07fb1d06SElliott Hughes }
268*07fb1d06SElliott Hughes offset += kProblematicCache.size() * 8;
269*07fb1d06SElliott Hughes for (const auto& deflate : kSubblockDeflateExtentsSample1) {
270*07fb1d06SElliott Hughes deflates.emplace_back(deflate.offset + offset, deflate.length);
271*07fb1d06SElliott Hughes }
272*07fb1d06SElliott Hughes
273*07fb1d06SElliott Hughes auto expected_deflates(deflates);
274*07fb1d06SElliott Hughes expected_deflates.erase(expected_deflates.begin() +
275*07fb1d06SElliott Hughes kSubblockDeflateExtentsSample1.size());
276*07fb1d06SElliott Hughes
277*07fb1d06SElliott Hughes EXPECT_TRUE(RemoveDeflatesWithBadDistanceCaches(data, &deflates));
278*07fb1d06SElliott Hughes EXPECT_EQ(deflates, expected_deflates);
279*07fb1d06SElliott Hughes }
280*07fb1d06SElliott Hughes
281*07fb1d06SElliott Hughes } // namespace puffin
282