liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "gtest/gtest.h"
 
#include "puffin/src/puff_reader.h"
#include "puffin/src/puff_writer.h"
#include "puffin/src/unittest_common.h"
 
namespace puffin {
 
namespace {
void TestLiteralLength(size_t length) {
  Buffer buf(length + 10);
  PuffData pd;
 
  BufferPuffWriter pw(buf.data(), buf.size());
  // We need to insert a metadata otherwise it will fail.
  pd.type = PuffData::Type::kBlockMetadata;
  pd.length = 1;
  ASSERT_TRUE(pw.Insert(pd));
 
  BufferPuffReader pr(buf.data(), buf.size());
  ASSERT_TRUE(pr.GetNext(&pd));
  ASSERT_EQ(pd.type, PuffData::Type::kBlockMetadata);
  ASSERT_EQ(pd.length, 1);
 
  // We insert |length| bytes.
  pd.type = PuffData::Type::kLiterals;
  pd.length = length;
  pd.read_fn = [](uint8_t* buffer, size_t count) {
    std::fill(buffer, buffer + count, 10);
    return true;
  };
  ASSERT_TRUE(pw.Insert(pd));
  ASSERT_TRUE(pw.Flush());
 
  pd.type = PuffData::Type::kLenDist;
  pd.distance = 1;
  pd.length = 3;
  ASSERT_TRUE(pw.Insert(pd));
 
  ASSERT_TRUE(pr.GetNext(&pd));
  if (length == 0) {
    // If length is zero, then nothing should've been inserted.
    ASSERT_EQ(pd.type, PuffData::Type::kLenDist);
  } else {
    // We have to see |length| bytes.
    ASSERT_EQ(pd.type, PuffData::Type::kLiterals);
    ASSERT_EQ(pd.length, length);
    for (size_t i = 0; i < pd.length; i++) {
      uint8_t byte;
      pd.read_fn(&byte, 1);
      EXPECT_EQ(byte, 10);
    }
  }
}
}  // namespace
 
// Testing read/write from/into a puff buffer using |PuffReader|/|PuffWriter|.
TEST(PuffIOTest, InputOutputTest) {
  Buffer buf(100);
  BufferPuffReader pr(buf.data(), buf.size());
  BufferPuffWriter pw(buf.data(), buf.size());
  BufferPuffWriter epw(nullptr, 0);
  uint8_t block = 123;
 
  {
    PuffData pd;
    pd.type = PuffData::Type::kBlockMetadata;
    pd.block_metadata[0] = 0xCC;  // header
    memcpy(&pd.block_metadata[1], &block, sizeof(block));
    pd.length = sizeof(block) + 1;
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(epw.Insert(pd));
    ASSERT_TRUE(pw.Flush());
    ASSERT_TRUE(epw.Flush());
  }
  {
    PuffData pd;
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kBlockMetadata);
    ASSERT_EQ(pd.length, sizeof(block) + 1);
    ASSERT_EQ(pd.block_metadata[0], 0xCC);
    ASSERT_EQ(pd.block_metadata[1], block);
  }
  {
    PuffData pd;
    pd.type = PuffData::Type::kLenDist;
    pd.distance = 321;
    pd.length = 3;
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(epw.Insert(pd));
    pd.length = 127;
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(epw.Insert(pd));
    pd.length = 258;
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(epw.Insert(pd));
    ASSERT_TRUE(pw.Flush());
    ASSERT_TRUE(epw.Flush());
 
    pd.length = 259;
    ASSERT_FALSE(pw.Insert(pd));
    ASSERT_FALSE(epw.Insert(pd));
  }
  {
    PuffData pd;
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kLenDist);
    ASSERT_EQ(pd.distance, 321);
    ASSERT_EQ(pd.length, 3);
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kLenDist);
    ASSERT_EQ(pd.length, 127);
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kLenDist);
    ASSERT_EQ(pd.length, 258);
  }
  {
    PuffData pd;
    pd.type = PuffData::Type::kEndOfBlock;
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(epw.Insert(pd));
    ASSERT_TRUE(pw.Flush());
    ASSERT_TRUE(epw.Flush());
  }
  {
    PuffData pd;
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kEndOfBlock);
  }
  {
    PuffData pd;
    pd.type = PuffData::Type::kBlockMetadata;
    block = 123;
    pd.block_metadata[0] = 0xCC;  // header
    memcpy(&pd.block_metadata[1], &block, sizeof(block));
    pd.length = sizeof(block) + 1;
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(epw.Insert(pd));
    ASSERT_TRUE(pw.Flush());
    ASSERT_TRUE(epw.Flush());
  }
  {
    PuffData pd;
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kBlockMetadata);
    ASSERT_EQ(pd.length, sizeof(block) + 1);
    ASSERT_EQ(pd.block_metadata[0], 0xCC);
    ASSERT_EQ(pd.block_metadata[1], block);
  }
 
  uint8_t tmp[] = {1, 2, 100};
  {
    PuffData pd;
    size_t index = 0;
    pd.type = PuffData::Type::kLiterals;
    pd.length = 3;
    pd.read_fn = [&tmp, &index](uint8_t* buffer, size_t count) {
      if (count > 3 - index)
        return false;
      if (buffer != nullptr) {
        memcpy(buffer, &tmp[index], count);
      }
      index += count;
      return true;
    };
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(pw.Flush());
    // We have to refresh the read_fn function for the second insert.
    index = 0;
    ASSERT_TRUE(epw.Insert(pd));
    ASSERT_TRUE(epw.Flush());
  }
  {
    PuffData pd;
    pd.type = PuffData::Type::kLiteral;
    pd.byte = 10;
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(epw.Insert(pd));
    ASSERT_TRUE(pw.Flush());
    ASSERT_TRUE(epw.Flush());
  }
 
  uint8_t tmp3[3];
  {
    PuffData pd;
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kLiterals);
    ASSERT_EQ(pd.length, 3);
    ASSERT_TRUE(pd.read_fn(tmp3, 3));
    ASSERT_FALSE(pd.read_fn(tmp3, 1));
    ASSERT_EQ(0, memcmp(tmp3, tmp, 3));
  }
  {
    PuffData pd;
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kLiterals);
    ASSERT_EQ(pd.length, 1);
    ASSERT_TRUE(pd.read_fn(tmp3, 1));
    ASSERT_EQ(tmp3[0], 10);
    ASSERT_FALSE(pd.read_fn(tmp3, 2));
  }
  {
    PuffData pd;
    pd.type = PuffData::Type::kEndOfBlock;
    ASSERT_TRUE(pw.Insert(pd));
    ASSERT_TRUE(epw.Insert(pd));
    ASSERT_TRUE(pw.Flush());
    ASSERT_TRUE(epw.Flush());
  }
  {
    PuffData pd;
    ASSERT_TRUE(pr.GetNext(&pd));
    ASSERT_EQ(pd.type, PuffData::Type::kEndOfBlock);
  }
 
  ASSERT_EQ(buf.size() - pr.BytesLeft(), pw.Size());
  ASSERT_EQ(buf.size() - pr.BytesLeft(), epw.Size());
}
 
// Testing metadata boundary.
TEST(PuffIOTest, MetadataBoundaryTest) {
  PuffData pd;
  Buffer buf(3);
  BufferPuffWriter pw(buf.data(), buf.size());
 
  // Block metadata takes two + varied bytes, so on a thre byte buffer, only one
  // bytes is left for the varied part of metadata.
  pd.type = PuffData::Type::kBlockMetadata;
  pd.length = 2;
  ASSERT_FALSE(pw.Insert(pd));
  pd.length = 0;  // length should be at least 1.
  ASSERT_FALSE(pw.Insert(pd));
  pd.length = 1;
  ASSERT_TRUE(pw.Insert(pd));
 
  Buffer puff_buffer = {0x00, 0x03, 0x02, 0x00, 0x00};
  BufferPuffReader pr(puff_buffer.data(), puff_buffer.size());
  ASSERT_FALSE(pr.GetNext(&pd));
}
 
TEST(PuffIOTest, InvalidCopyLengthsDistanceTest) {
  PuffData pd;
  Buffer puff_buffer(20);
  BufferPuffWriter pw(puff_buffer.data(), puff_buffer.size());
 
  // Invalid Lenght values.
  pd.type = PuffData::Type::kLenDist;
  pd.distance = 1;
  pd.length = 0;
  EXPECT_FALSE(pw.Insert(pd));
  pd.length = 1;
  EXPECT_FALSE(pw.Insert(pd));
  pd.length = 2;
  EXPECT_FALSE(pw.Insert(pd));
  pd.length = 3;
  EXPECT_TRUE(pw.Insert(pd));
  pd.length = 259;
  EXPECT_FALSE(pw.Insert(pd));
  pd.length = 258;
  EXPECT_TRUE(pw.Insert(pd));
 
  // Invalid distance values.
  pd.length = 3;
  pd.distance = 0;
  EXPECT_FALSE(pw.Insert(pd));
  pd.distance = 1;
  EXPECT_TRUE(pw.Insert(pd));
  pd.distance = 32769;
  EXPECT_FALSE(pw.Insert(pd));
  pd.distance = 32768;
  EXPECT_TRUE(pw.Insert(pd));
 
  // First three bytes header, four bytes value lit/len, and four bytes
  // invalid lit/len.
  puff_buffer = {0x00, 0x00, 0xFF, 0xFF, 0x80, 0x00,
                 0x00, 0xFF, 0x82, 0x00, 0x00};
  BufferPuffReader pr(puff_buffer.data(), puff_buffer.size());
  EXPECT_TRUE(pr.GetNext(&pd));
  EXPECT_EQ(pd.type, PuffData::Type::kBlockMetadata);
  EXPECT_TRUE(pr.GetNext(&pd));
  EXPECT_EQ(pd.type, PuffData::Type::kLenDist);
  EXPECT_FALSE(pr.GetNext(&pd));
}
 
TEST(PuffIOTest, InvalidCopyLenghtDistanceBoundaryTest) {
  PuffData pd;
  Buffer puff_buffer(5);
 
  pd.type = PuffData::Type::kLenDist;
  pd.distance = 1;
  pd.length = 129;
  for (size_t i = 1; i < 2; i++) {
    BufferPuffWriter pw(puff_buffer.data(), i);
    EXPECT_FALSE(pw.Insert(pd));
  }
 
  pd.length = 130;
  for (size_t i = 1; i < 3; i++) {
    BufferPuffWriter pw(puff_buffer.data(), i);
    EXPECT_FALSE(pw.Insert(pd));
  }
 
  // First three bytes header, three bytes value lit/len.
  puff_buffer = {0x00, 0x00, 0xFF, 0xFF, 0x80, 0x00};
  BufferPuffReader pr(puff_buffer.data(), puff_buffer.size());
  EXPECT_TRUE(pr.GetNext(&pd));
  EXPECT_EQ(pd.type, PuffData::Type::kBlockMetadata);
  EXPECT_FALSE(pr.GetNext(&pd));
}
 
TEST(PuffIOTest, LiteralsTest) {
  TestLiteralLength(0);
  TestLiteralLength(1);
  TestLiteralLength(2);
  TestLiteralLength(126);
  TestLiteralLength(127);
  TestLiteralLength(128);
}
 
// Testing maximum literals length.
TEST(PuffIOTest, MaxLiteralsTest) {
  Buffer buf((1 << 16) + 127 + 20);
  PuffData pd;
 
  BufferPuffWriter pw(buf.data(), buf.size());
  // We need to insert a metadata otherwise it will fail.
  pd.type = PuffData::Type::kBlockMetadata;
  pd.length = 1;
  ASSERT_TRUE(pw.Insert(pd));
 
  pd.type = PuffData::Type::kLiterals;
  pd.length = (1 << 16);
  pd.read_fn = [](uint8_t* buffer, size_t count) {
    std::fill(buffer, buffer + count, 10);
    return true;
  };
  ASSERT_TRUE(pw.Insert(pd));
  ASSERT_TRUE(pw.Flush());
 
  BufferPuffReader pr(buf.data(), buf.size());
  ASSERT_TRUE(pr.GetNext(&pd));
  ASSERT_EQ(pd.type, PuffData::Type::kBlockMetadata);
  ASSERT_EQ(pd.length, 1);
 
  ASSERT_TRUE(pr.GetNext(&pd));
  ASSERT_EQ(pd.type, PuffData::Type::kLiterals);
  ASSERT_EQ(pd.length, 1 << 16);
  for (size_t i = 0; i < pd.length; i++) {
    uint8_t byte;
    pd.read_fn(&byte, 1);
    ASSERT_EQ(byte, 10);
  }
 
  BufferPuffWriter pw2(buf.data(), buf.size());
  pd.type = PuffData::Type::kBlockMetadata;
  pd.length = 1;
  ASSERT_TRUE(pw2.Insert(pd));
 
  pd.type = PuffData::Type::kLiteral;
  pd.length = 1;
  pd.byte = 12;
  // We have to be able to fill 65663 bytes.
  for (size_t i = 0; i < ((1 << 16) + 127); i++) {
    ASSERT_TRUE(pw2.Insert(pd));
  }
  // If we add one more, then it should have been flushed.
  pd.byte = 13;
  ASSERT_TRUE(pw2.Insert(pd));
  ASSERT_TRUE(pw2.Flush());
 
  // Now read it back.
  BufferPuffReader pr2(buf.data(), buf.size());
  ASSERT_TRUE(pr2.GetNext(&pd));
  ASSERT_EQ(pd.type, PuffData::Type::kBlockMetadata);
 
  // Now we should read on kLiterals with lenght 1 << 16 and just one literal
  // after that.
  ASSERT_TRUE(pr2.GetNext(&pd));
  ASSERT_EQ(pd.type, PuffData::Type::kLiterals);
  ASSERT_EQ(pd.length, (1 << 16) + 127);
  for (size_t i = 0; i < pd.length; i++) {
    uint8_t byte;
    pd.read_fn(&byte, 1);
    ASSERT_EQ(byte, 12);
  }
 
  ASSERT_TRUE(pr2.GetNext(&pd));
  ASSERT_EQ(pd.type, PuffData::Type::kLiterals);
  ASSERT_EQ(pd.length, 1);
  uint8_t byte;
  pd.read_fn(&byte, 1);
  ASSERT_EQ(byte, 13);
}
 
}  // namespace puffin