1// Copyright 2008 Google Inc.
2// Author: Lincoln Smith
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#include <config.h>
17#include "google/vcdecoder.h"
18#include "testing.h"
19#include "vcdecoder_test.h"
20#include "vcdiff_defs.h"  // VCD_SOURCE
21
22namespace open_vcdiff {
23
24// These are the same tests as for VCDiffStandardDecoderTest, with the added
25// complication that instead of calling DecodeChunk() once with the entire data
26// set, DecodeChunk() is called once for each byte of input.  This is intended
27// to shake out any bugs with rewind and resume while parsing chunked data.
28
29typedef VCDiffStandardDecoderTest VCDiffStandardDecoderTestByteByByte;
30
31TEST_F(VCDiffStandardDecoderTestByteByByte, DecodeHeaderOnly) {
32  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
33  for (size_t i = 0; i < delta_file_header_.size(); ++i) {
34    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_header_[i], 1, &output_));
35  }
36  EXPECT_TRUE(decoder_.FinishDecoding());
37  EXPECT_EQ("", output_);
38}
39
40TEST_F(VCDiffStandardDecoderTestByteByByte, Decode) {
41  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
42  for (size_t i = 0; i < delta_file_.size(); ++i) {
43    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_[i], 1, &output_));
44  }
45  EXPECT_TRUE(decoder_.FinishDecoding());
46  EXPECT_EQ(expected_target_.c_str(), output_);
47}
48
49TEST_F(VCDiffStandardDecoderTestByteByByte, DecodeNoVcdTarget) {
50  decoder_.SetAllowVcdTarget(false);
51  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
52  for (size_t i = 0; i < delta_file_.size(); ++i) {
53    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_[i], 1, &output_));
54  }
55  EXPECT_TRUE(decoder_.FinishDecoding());
56  EXPECT_EQ(expected_target_.c_str(), output_);
57}
58
59// Remove one byte from the length of the chunk to process, and
60// verify that an error is returned for FinishDecoding().
61TEST_F(VCDiffStandardDecoderTestByteByByte, FinishAfterDecodingPartialWindow) {
62  delta_file_.resize(delta_file_.size() - 1);
63  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
64  for (size_t i = 0; i < delta_file_.size(); ++i) {
65    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_[i], 1, &output_));
66  }
67  EXPECT_FALSE(decoder_.FinishDecoding());
68  // The decoder should not create more target bytes than were expected.
69  EXPECT_GE(expected_target_.size(), output_.size());
70}
71
72TEST_F(VCDiffStandardDecoderTestByteByByte,
73       FinishAfterDecodingPartialWindowHeader) {
74  delta_file_.resize(delta_file_header_.size()
75                       + delta_window_header_.size() - 1);
76  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
77  for (size_t i = 0; i < delta_file_.size(); ++i) {
78    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_[i], 1, &output_));
79  }
80  EXPECT_FALSE(decoder_.FinishDecoding());
81  EXPECT_EQ("", output_);
82}
83
84// If we add a checksum to a standard-format delta file (without using format
85// extensions), it will be interpreted as random bytes inserted into the middle
86// of the file.  The decode operation should fail, but where exactly it fails is
87// undefined.
88TEST_F(VCDiffStandardDecoderTestByteByByte,
89       StandardFormatDoesNotSupportChecksum) {
90  ComputeAndAddChecksum();
91  InitializeDeltaFile();
92  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
93  bool failed = false;
94  for (size_t i = 0; i < delta_file_.size(); ++i) {
95    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
96      failed = true;
97      break;
98    }
99  }
100  EXPECT_TRUE(failed);
101  // The decoder should not create more target bytes than were expected.
102  EXPECT_GE(expected_target_.size(), output_.size());
103}
104
105TEST_F(VCDiffStandardDecoderTestByteByByte, TargetMatchesWindowSizeLimit) {
106  decoder_.SetMaximumTargetWindowSize(expected_target_.size());
107  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
108  for (size_t i = 0; i < delta_file_.size(); ++i) {
109    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_[i], 1, &output_));
110  }
111  EXPECT_TRUE(decoder_.FinishDecoding());
112  EXPECT_EQ(expected_target_.c_str(), output_);
113}
114
115TEST_F(VCDiffStandardDecoderTestByteByByte, TargetMatchesFileSizeLimit) {
116  decoder_.SetMaximumTargetFileSize(expected_target_.size());
117  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
118  for (size_t i = 0; i < delta_file_.size(); ++i) {
119    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_[i], 1, &output_));
120  }
121  EXPECT_TRUE(decoder_.FinishDecoding());
122  EXPECT_EQ(expected_target_.c_str(), output_);
123}
124
125TEST_F(VCDiffStandardDecoderTestByteByByte, TargetExceedsWindowSizeLimit) {
126  decoder_.SetMaximumTargetWindowSize(expected_target_.size() - 1);
127  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
128  bool failed = false;
129  for (size_t i = 0; i < delta_file_.size(); ++i) {
130    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
131      failed = true;
132      break;
133    }
134  }
135  EXPECT_TRUE(failed);
136  EXPECT_EQ("", output_);
137}
138
139TEST_F(VCDiffStandardDecoderTestByteByByte, TargetExceedsFileSizeLimit) {
140  decoder_.SetMaximumTargetFileSize(expected_target_.size() - 1);
141  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
142  bool failed = false;
143  for (size_t i = 0; i < delta_file_.size(); ++i) {
144    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
145      failed = true;
146      break;
147    }
148  }
149  EXPECT_TRUE(failed);
150  EXPECT_EQ("", output_);
151}
152
153// Fuzz bits to make sure decoder does not violently crash.
154// This test has no expected behavior except that no crashes should occur.
155// In some cases, changing bits will still decode to the correct target;
156// for example, changing unused bits within a bitfield.
157TEST_F(VCDiffStandardDecoderTestByteByByte, FuzzBits) {
158  while (FuzzOneByteInDeltaFile()) {
159    decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
160    bool failed = false;
161    for (size_t i = 0; i < delta_file_.size(); ++i) {
162      if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
163        failed = true;
164        break;
165      }
166    }
167    if (!failed) {
168      decoder_.FinishDecoding();
169    }
170    // The decoder should not create more target bytes than were expected.
171    EXPECT_GE(expected_target_.size(), output_.size());
172    InitializeDeltaFile();
173    output_.clear();
174  }
175}
176
177// Change each element of the delta file window to an erroneous value
178// and make sure it's caught as an error.
179
180TEST_F(VCDiffStandardDecoderTestByteByByte,
181       WinIndicatorHasBothSourceAndTarget) {
182  delta_file_[delta_file_header_.size()] = VCD_SOURCE + VCD_TARGET;
183  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
184  bool failed = false;
185  for (size_t i = 0; i < delta_file_.size(); ++i) {
186    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
187      failed = true;
188      // It should fail at the position that was altered
189      EXPECT_EQ(delta_file_header_.size(), i);
190      break;
191    }
192  }
193  EXPECT_TRUE(failed);
194  EXPECT_EQ("", output_);
195}
196
197TEST_F(VCDiffStandardDecoderTestByteByByte, OkayToSetUpperBitsOfWinIndicator) {
198  // It is not an error to set any of the other bits in Win_Indicator
199  // besides VCD_SOURCE and VCD_TARGET.
200  delta_file_[delta_file_header_.size()] = 0xFD;
201  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
202  for (size_t i = 0; i < delta_file_.size(); ++i) {
203    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_[i], 1, &output_));
204  }
205  EXPECT_TRUE(decoder_.FinishDecoding());
206  EXPECT_EQ(expected_target_.c_str(), output_);
207}
208
209TEST_F(VCDiffStandardDecoderTestByteByByte,
210       CopyInstructionsShouldFailIfNoSourceSegment) {
211  // Replace the Win_Indicator and the source size and source offset with a
212  // single 0 byte (a Win_Indicator for a window with no source segment.)
213  delta_window_header_.replace(0, 4, "\0", 1);
214  InitializeDeltaFile();
215  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
216  bool failed = false;
217  for (size_t i = 0; i < delta_file_.size(); ++i) {
218    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
219      failed = true;
220      // The first COPY instruction should fail.  With the standard format,
221      // it may need to see the whole delta window before knowing that it is
222      // invalid.
223      break;
224    }
225  }
226  EXPECT_TRUE(failed);
227  EXPECT_EQ("", output_);
228}
229
230TEST_F(VCDiffStandardDecoderTestByteByByte,
231       SourceSegmentSizeExceedsDictionarySize) {
232  ++delta_file_[delta_file_header_.size() + 2];  // increment size
233  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
234  bool failed = false;
235  for (size_t i = 0; i < delta_file_.size(); ++i) {
236    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
237      failed = true;
238      // It should fail after decoding the source segment size
239      EXPECT_EQ(delta_file_header_.size() + 2, i);
240      break;
241    }
242  }
243  EXPECT_TRUE(failed);
244  EXPECT_EQ("", output_);
245}
246
247TEST_F(VCDiffStandardDecoderTestByteByByte, SourceSegmentSizeMaxInt) {
248  WriteMaxVarintAtOffset(1, 2);
249  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
250  bool failed = false;
251  for (size_t i = 0; i < delta_file_.size(); ++i) {
252    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
253      failed = true;
254      // It should fail after decoding the source segment size
255      EXPECT_EQ(delta_file_header_.size() + 5, i);
256      break;
257    }
258  }
259  EXPECT_TRUE(failed);
260  EXPECT_EQ("", output_);
261}
262
263TEST_F(VCDiffStandardDecoderTestByteByByte, SourceSegmentSizeNegative) {
264  WriteNegativeVarintAtOffset(1, 2);
265  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
266  bool failed = false;
267  for (size_t i = 0; i < delta_file_.size(); ++i) {
268    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
269      failed = true;
270      // It should fail after decoding the source segment size
271      EXPECT_EQ(delta_file_header_.size() + 4, i);
272      break;
273    }
274  }
275  EXPECT_TRUE(failed);
276  EXPECT_EQ("", output_);
277}
278
279TEST_F(VCDiffStandardDecoderTestByteByByte, SourceSegmentSizeInvalid) {
280  WriteInvalidVarintAtOffset(1, 2);
281  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
282  bool failed = false;
283  for (size_t i = 0; i < delta_file_.size(); ++i) {
284    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
285      failed = true;
286      // It should fail after decoding the source segment size
287      EXPECT_GE(delta_file_header_.size() + 6, i);
288      break;
289    }
290  }
291  EXPECT_TRUE(failed);
292  EXPECT_EQ("", output_);
293}
294
295TEST_F(VCDiffStandardDecoderTestByteByByte,
296       SourceSegmentEndExceedsDictionarySize) {
297  ++delta_file_[delta_file_header_.size() + 3];  // increment start pos
298  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
299  bool failed = false;
300  for (size_t i = 0; i < delta_file_.size(); ++i) {
301    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
302      failed = true;
303      // It should fail after decoding the source segment end
304      EXPECT_EQ(delta_file_header_.size() + 3, i);
305      break;
306    }
307  }
308  EXPECT_TRUE(failed);
309  EXPECT_EQ("", output_);
310}
311
312TEST_F(VCDiffStandardDecoderTestByteByByte, SourceSegmentPosMaxInt) {
313  WriteMaxVarintAtOffset(3, 1);
314  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
315  bool failed = false;
316  for (size_t i = 0; i < delta_file_.size(); ++i) {
317    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
318      failed = true;
319      // It should fail after decoding the source segment pos
320      EXPECT_EQ(delta_file_header_.size() + 7, i);
321      break;
322    }
323  }
324  EXPECT_TRUE(failed);
325  EXPECT_EQ("", output_);
326}
327
328TEST_F(VCDiffStandardDecoderTestByteByByte, SourceSegmentPosNegative) {
329  WriteNegativeVarintAtOffset(3, 1);
330  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
331  bool failed = false;
332  for (size_t i = 0; i < delta_file_.size(); ++i) {
333    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
334      failed = true;
335      // It should fail after decoding the source segment pos
336      EXPECT_EQ(delta_file_header_.size() + 6, i);
337      break;
338    }
339  }
340  EXPECT_TRUE(failed);
341  EXPECT_EQ("", output_);
342}
343
344TEST_F(VCDiffStandardDecoderTestByteByByte, SourceSegmentPosInvalid) {
345  WriteInvalidVarintAtOffset(3, 1);
346  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
347  bool failed = false;
348  for (size_t i = 0; i < delta_file_.size(); ++i) {
349    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
350      failed = true;
351      // It should fail after decoding the source segment pos
352      EXPECT_GE(delta_file_header_.size() + 8, i);
353      break;
354    }
355  }
356  EXPECT_TRUE(failed);
357  EXPECT_EQ("", output_);
358}
359
360TEST_F(VCDiffStandardDecoderTestByteByByte, DeltaEncodingLengthZero) {
361  delta_file_[delta_file_header_.size() + 4] = 0;
362  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
363  bool failed = false;
364  for (size_t i = 0; i < delta_file_.size(); ++i) {
365    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
366      failed = true;
367      // It should fail after decoding the copy address segment size
368      EXPECT_EQ(delta_file_header_.size() + 10, i);
369      break;
370    }
371  }
372  EXPECT_TRUE(failed);
373  EXPECT_EQ("", output_);
374}
375
376TEST_F(VCDiffStandardDecoderTestByteByByte, DeltaEncodingLengthTooLargeByOne) {
377  ++delta_file_[delta_file_header_.size() + 4];
378  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
379  bool failed = false;
380  for (size_t i = 0; i < delta_file_.size(); ++i) {
381    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
382      failed = true;
383      // It should fail after decoding the copy address segment size
384      EXPECT_EQ(delta_file_header_.size() + 10, i);
385      break;
386    }
387  }
388  EXPECT_TRUE(failed);
389  EXPECT_EQ("", output_);
390}
391
392TEST_F(VCDiffStandardDecoderTestByteByByte, DeltaEncodingLengthTooSmallByOne) {
393  --delta_file_[delta_file_header_.size() + 4];
394  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
395  bool failed = false;
396  for (size_t i = 0; i < delta_file_.size(); ++i) {
397    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
398      failed = true;
399      // It should fail after decoding the copy address segment size
400      EXPECT_EQ(delta_file_header_.size() + 10, i);
401      break;
402    }
403  }
404  EXPECT_TRUE(failed);
405  EXPECT_EQ("", output_);
406}
407
408TEST_F(VCDiffStandardDecoderTestByteByByte, DeltaEncodingLengthMaxInt) {
409  WriteMaxVarintAtOffset(4, 1);
410  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
411  bool failed = false;
412  for (size_t i = 0; i < delta_file_.size(); ++i) {
413    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
414      failed = true;
415      // It should fail before finishing the window header
416      EXPECT_GE(delta_file_header_.size() + delta_window_header_.size() + 4,
417                i);
418      break;
419    }
420  }
421  EXPECT_TRUE(failed);
422  EXPECT_EQ("", output_);
423}
424
425TEST_F(VCDiffStandardDecoderTestByteByByte, DeltaEncodingLengthNegative) {
426  WriteNegativeVarintAtOffset(4, 1);
427  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
428  bool failed = false;
429  for (size_t i = 0; i < delta_file_.size(); ++i) {
430    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
431      failed = true;
432      // It should fail after decoding the delta encoding length
433      EXPECT_EQ(delta_file_header_.size() + 7, i);
434      break;
435    }
436  }
437  EXPECT_TRUE(failed);
438  EXPECT_EQ("", output_);
439}
440
441TEST_F(VCDiffStandardDecoderTestByteByByte, DeltaEncodingLengthInvalid) {
442  WriteInvalidVarintAtOffset(4, 1);
443  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
444  bool failed = false;
445  for (size_t i = 0; i < delta_file_.size(); ++i) {
446    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
447      failed = true;
448      // It should fail after decoding the delta encoding length
449      EXPECT_GE(delta_file_header_.size() + 9, i);
450      break;
451    }
452  }
453  EXPECT_TRUE(failed);
454  EXPECT_EQ("", output_);
455}
456
457TEST_F(VCDiffStandardDecoderTestByteByByte, TargetWindowSizeZero) {
458  static const char zero_size[] = { 0x00 };
459  delta_file_.replace(delta_file_header_.size() + 5, 2, zero_size, 1);
460  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
461  bool failed = false;
462  for (size_t i = 0; i < delta_file_.size(); ++i) {
463    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
464      failed = true;
465      break;
466    }
467  }
468  EXPECT_TRUE(failed);
469  EXPECT_EQ("", output_);
470}
471
472TEST_F(VCDiffStandardDecoderTestByteByByte, TargetWindowSizeTooLargeByOne) {
473  ++delta_file_[delta_file_header_.size() + 6];
474  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
475  bool failed = false;
476  for (size_t i = 0; i < delta_file_.size(); ++i) {
477    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
478      failed = true;
479      break;
480    }
481  }
482  EXPECT_TRUE(failed);
483  // The decoder should not create more target bytes than were expected.
484  EXPECT_GE(expected_target_.size(), output_.size());
485}
486
487TEST_F(VCDiffStandardDecoderTestByteByByte, TargetWindowSizeTooSmallByOne) {
488  --delta_file_[delta_file_header_.size() + 6];
489  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
490  bool failed = false;
491  for (size_t i = 0; i < delta_file_.size(); ++i) {
492    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
493      failed = true;
494      break;
495    }
496  }
497  EXPECT_TRUE(failed);
498  // The decoder should not create more target bytes than were expected.
499  EXPECT_GE(expected_target_.size(), output_.size());
500}
501
502TEST_F(VCDiffStandardDecoderTestByteByByte, TargetWindowSizeMaxInt) {
503  WriteMaxVarintAtOffset(5, 2);
504  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
505  bool failed = false;
506  for (size_t i = 0; i < delta_file_.size(); ++i) {
507    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
508      failed = true;
509      // It should fail after decoding the target window size
510      EXPECT_EQ(delta_file_header_.size() + 9, i);
511      break;
512    }
513  }
514  EXPECT_TRUE(failed);
515  EXPECT_EQ("", output_);
516}
517
518TEST_F(VCDiffStandardDecoderTestByteByByte, TargetWindowSizeNegative) {
519  WriteNegativeVarintAtOffset(5, 2);
520  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
521  bool failed = false;
522  for (size_t i = 0; i < delta_file_.size(); ++i) {
523    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
524      failed = true;
525      // It should fail after decoding the target window size
526      EXPECT_EQ(delta_file_header_.size() + 8, i);
527      break;
528    }
529  }
530  EXPECT_TRUE(failed);
531  EXPECT_EQ("", output_);
532}
533
534TEST_F(VCDiffStandardDecoderTestByteByByte, TargetWindowSizeInvalid) {
535  WriteInvalidVarintAtOffset(5, 2);
536  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
537  bool failed = false;
538  for (size_t i = 0; i < delta_file_.size(); ++i) {
539    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
540      failed = true;
541      // It should fail after decoding the target window size
542      EXPECT_GE(delta_file_header_.size() + 10, i);
543      break;
544    }
545  }
546  EXPECT_TRUE(failed);
547  EXPECT_EQ("", output_);
548}
549
550TEST_F(VCDiffStandardDecoderTestByteByByte,
551       OkayToSetUpperBitsOfDeltaIndicator) {
552  delta_file_[delta_file_header_.size() + 7] = 0xF8;
553  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
554  for (size_t i = 0; i < delta_file_.size(); ++i) {
555    EXPECT_TRUE(decoder_.DecodeChunk(&delta_file_[i], 1, &output_));
556  }
557  EXPECT_TRUE(decoder_.FinishDecoding());
558  EXPECT_EQ(expected_target_.c_str(), output_);
559}
560
561TEST_F(VCDiffStandardDecoderTestByteByByte, DataCompressionNotSupported) {
562  delta_file_[delta_file_header_.size() + 7] = 0x01;
563  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
564  bool failed = false;
565  for (size_t i = 0; i < delta_file_.size(); ++i) {
566    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
567      failed = true;
568      // It should fail after decoding the delta indicator
569      EXPECT_EQ(delta_file_header_.size() + 7, i);
570      break;
571    }
572  }
573  EXPECT_TRUE(failed);
574  EXPECT_EQ("", output_);
575}
576
577TEST_F(VCDiffStandardDecoderTestByteByByte,
578       InstructionCompressionNotSupported) {
579  delta_file_[delta_file_header_.size() + 7] = 0x02;
580  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
581  bool failed = false;
582  for (size_t i = 0; i < delta_file_.size(); ++i) {
583    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
584      failed = true;
585      // It should fail after decoding the delta indicator
586      EXPECT_EQ(delta_file_header_.size() + 7, i);
587      break;
588    }
589  }
590  EXPECT_TRUE(failed);
591  EXPECT_EQ("", output_);
592}
593
594TEST_F(VCDiffStandardDecoderTestByteByByte, AddressCompressionNotSupported) {
595  delta_file_[delta_file_header_.size() + 7] = 0x04;
596  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
597  bool failed = false;
598  for (size_t i = 0; i < delta_file_.size(); ++i) {
599    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
600      failed = true;
601      // It should fail after decoding the delta indicator
602      EXPECT_EQ(delta_file_header_.size() + 7, i);
603      break;
604    }
605  }
606  EXPECT_TRUE(failed);
607  EXPECT_EQ("", output_);
608}
609
610TEST_F(VCDiffStandardDecoderTestByteByByte, AddRunDataSizeZero) {
611  delta_file_[delta_file_header_.size() + 8] = 0;
612  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
613  bool failed = false;
614  for (size_t i = 0; i < delta_file_.size(); ++i) {
615    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
616      failed = true;
617      // It should fail after decoding the copy address segment size
618      EXPECT_EQ(delta_file_header_.size() + 10, i);
619      break;
620    }
621  }
622  EXPECT_TRUE(failed);
623  EXPECT_EQ("", output_);
624}
625
626TEST_F(VCDiffStandardDecoderTestByteByByte, AddRunDataSizeTooLargeByOne) {
627  ++delta_file_[delta_file_header_.size() + 8];
628  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
629  bool failed = false;
630  for (size_t i = 0; i < delta_file_.size(); ++i) {
631    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
632      failed = true;
633      // It should fail after decoding the copy address segment size
634      EXPECT_EQ(delta_file_header_.size() + 10, i);
635      break;
636    }
637  }
638  EXPECT_TRUE(failed);
639  EXPECT_EQ("", output_);
640}
641
642TEST_F(VCDiffStandardDecoderTestByteByByte, AddRunDataSizeTooSmallByOne) {
643  --delta_file_[delta_file_header_.size() + 8];
644  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
645  bool failed = false;
646  for (size_t i = 0; i < delta_file_.size(); ++i) {
647    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
648      failed = true;
649      // It should fail after decoding the copy address segment size
650      EXPECT_EQ(delta_file_header_.size() + 10, i);
651      break;
652    }
653  }
654  EXPECT_TRUE(failed);
655  EXPECT_EQ("", output_);
656}
657
658TEST_F(VCDiffStandardDecoderTestByteByByte, AddRunDataSizeMaxInt) {
659  WriteMaxVarintAtOffset(8, 1);
660  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
661  bool failed = false;
662  for (size_t i = 0; i < delta_file_.size(); ++i) {
663    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
664      failed = true;
665      // It should fail before finishing the window header
666      EXPECT_GE(delta_file_header_.size() + delta_window_header_.size() + 4,
667                i);
668      break;
669    }
670  }
671  EXPECT_TRUE(failed);
672  EXPECT_EQ("", output_);
673}
674
675TEST_F(VCDiffStandardDecoderTestByteByByte, AddRunDataSizeNegative) {
676  WriteNegativeVarintAtOffset(8, 1);
677  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
678  bool failed = false;
679  for (size_t i = 0; i < delta_file_.size(); ++i) {
680    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
681      failed = true;
682      // It should fail after decoding the add/run data segment size
683      EXPECT_EQ(delta_file_header_.size() + 11, i);
684      break;
685    }
686  }
687  EXPECT_TRUE(failed);
688  EXPECT_EQ("", output_);
689}
690
691TEST_F(VCDiffStandardDecoderTestByteByByte, AddRunDataSizeInvalid) {
692  WriteInvalidVarintAtOffset(8, 1);
693  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
694  bool failed = false;
695  for (size_t i = 0; i < delta_file_.size(); ++i) {
696    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
697      failed = true;
698      // It should fail after decoding the add/run data segment size
699      EXPECT_GE(delta_file_header_.size() + 13, i);
700      break;
701    }
702  }
703  EXPECT_TRUE(failed);
704  EXPECT_EQ("", output_);
705}
706
707TEST_F(VCDiffStandardDecoderTestByteByByte, InstructionsSizeZero) {
708  delta_file_[delta_file_header_.size() + 9] = 0;
709  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
710  bool failed = false;
711  for (size_t i = 0; i < delta_file_.size(); ++i) {
712    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
713      failed = true;
714      // It should fail after decoding the copy address segment size
715      EXPECT_EQ(delta_file_header_.size() + 10, i);
716      break;
717    }
718  }
719  EXPECT_TRUE(failed);
720  EXPECT_EQ("", output_);
721}
722
723TEST_F(VCDiffStandardDecoderTestByteByByte, InstructionsSizeTooLargeByOne) {
724  ++delta_file_[delta_file_header_.size() + 9];
725  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
726  bool failed = false;
727  for (size_t i = 0; i < delta_file_.size(); ++i) {
728    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
729      failed = true;
730      // It should fail after decoding the copy address segment size
731      EXPECT_EQ(delta_file_header_.size() + 10, i);
732      break;
733    }
734  }
735  EXPECT_TRUE(failed);
736  EXPECT_EQ("", output_);
737}
738
739TEST_F(VCDiffStandardDecoderTestByteByByte, InstructionsSizeTooSmallByOne) {
740  --delta_file_[delta_file_header_.size() + 9];
741  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
742  bool failed = false;
743  for (size_t i = 0; i < delta_file_.size(); ++i) {
744    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
745      failed = true;
746      // It should fail after decoding the copy address segment size
747      EXPECT_EQ(delta_file_header_.size() + 10, i);
748      break;
749    }
750  }
751  EXPECT_TRUE(failed);
752  EXPECT_EQ("", output_);
753}
754
755TEST_F(VCDiffStandardDecoderTestByteByByte, InstructionsSizeMaxInt) {
756  WriteMaxVarintAtOffset(9, 1);
757  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
758  bool failed = false;
759  for (size_t i = 0; i < delta_file_.size(); ++i) {
760    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
761      failed = true;
762      // It should fail before finishing the window header
763      EXPECT_GE(delta_file_header_.size() + delta_window_header_.size() + 4,
764                i);
765      break;
766    }
767  }
768  EXPECT_TRUE(failed);
769  EXPECT_EQ("", output_);
770}
771
772TEST_F(VCDiffStandardDecoderTestByteByByte, InstructionsSizeNegative) {
773  WriteNegativeVarintAtOffset(9, 1);
774  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
775  bool failed = false;
776  for (size_t i = 0; i < delta_file_.size(); ++i) {
777    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
778      failed = true;
779      // It should fail after decoding the instructions segment size
780      EXPECT_EQ(delta_file_header_.size() + 12, i);
781      break;
782    }
783  }
784  EXPECT_TRUE(failed);
785  EXPECT_EQ("", output_);
786}
787
788TEST_F(VCDiffStandardDecoderTestByteByByte, InstructionsSizeInvalid) {
789  WriteInvalidVarintAtOffset(9, 1);
790  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
791  bool failed = false;
792  for (size_t i = 0; i < delta_file_.size(); ++i) {
793    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
794      failed = true;
795      // It should fail after decoding the instructions segment size
796      EXPECT_GE(delta_file_header_.size() + 14, i);
797      break;
798    }
799  }
800  EXPECT_TRUE(failed);
801  EXPECT_EQ("", output_);
802}
803
804TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressSizeZero) {
805  delta_file_[delta_file_header_.size() + 10] = 0;
806  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
807  bool failed = false;
808  for (size_t i = 0; i < delta_file_.size(); ++i) {
809    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
810      failed = true;
811      // It should fail after decoding the copy address segment size
812      EXPECT_EQ(delta_file_header_.size() + 10, i);
813      break;
814    }
815  }
816  EXPECT_TRUE(failed);
817  EXPECT_EQ("", output_);
818}
819
820TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressSizeTooLargeByOne) {
821  ++delta_file_[delta_file_header_.size() + 10];
822  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
823  bool failed = false;
824  for (size_t i = 0; i < delta_file_.size(); ++i) {
825    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
826      failed = true;
827      // It should fail after decoding the copy address segment size
828      EXPECT_EQ(delta_file_header_.size() + 10, i);
829      break;
830    }
831  }
832  EXPECT_TRUE(failed);
833  EXPECT_EQ("", output_);
834}
835
836TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressSizeTooSmallByOne) {
837  --delta_file_[delta_file_header_.size() + 10];
838  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
839  bool failed = false;
840  for (size_t i = 0; i < delta_file_.size(); ++i) {
841    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
842      failed = true;
843      // It should fail after decoding the copy address segment size
844      EXPECT_EQ(delta_file_header_.size() + 10, i);
845      break;
846    }
847  }
848  EXPECT_TRUE(failed);
849  EXPECT_EQ("", output_);
850}
851
852TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressSizeMaxInt) {
853  WriteMaxVarintAtOffset(10, 1);
854  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
855  bool failed = false;
856  for (size_t i = 0; i < delta_file_.size(); ++i) {
857    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
858      failed = true;
859      // It should fail after decoding the copy address segment size
860      EXPECT_EQ(delta_file_header_.size() + 14, i);
861      break;
862    }
863  }
864  EXPECT_TRUE(failed);
865  EXPECT_EQ("", output_);
866}
867
868TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressSizeNegative) {
869  WriteNegativeVarintAtOffset(10, 1);
870  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
871  bool failed = false;
872  for (size_t i = 0; i < delta_file_.size(); ++i) {
873    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
874      failed = true;
875      // It should fail after decoding the copy address segment size
876      EXPECT_EQ(delta_file_header_.size() + 13, i);
877      break;
878    }
879  }
880  EXPECT_TRUE(failed);
881  EXPECT_EQ("", output_);
882}
883
884TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressSizeInvalid) {
885  WriteInvalidVarintAtOffset(10, 1);
886  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
887  bool failed = false;
888  for (size_t i = 0; i < delta_file_.size(); ++i) {
889    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
890      failed = true;
891      // It should fail after decoding the copy address segment size
892      EXPECT_GE(delta_file_header_.size() + 15, i);
893      break;
894    }
895  }
896  EXPECT_TRUE(failed);
897  EXPECT_EQ("", output_);
898}
899
900TEST_F(VCDiffStandardDecoderTestByteByByte, InstructionsEndEarly) {
901  --delta_file_[delta_file_header_.size() + 9];
902  ++delta_file_[delta_file_header_.size() + 10];
903  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
904  bool failed = false;
905  for (size_t i = 0; i < delta_file_.size(); ++i) {
906    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
907      failed = true;
908      break;
909    }
910  }
911  EXPECT_TRUE(failed);
912  // The decoder should not create more target bytes than were expected.
913  EXPECT_GE(expected_target_.size(), output_.size());
914}
915
916// From this point on, the tests should also be run against the interleaved
917// format.
918
919TEST_F(VCDiffStandardDecoderTestByteByByte, CopyMoreThanExpectedTarget) {
920  delta_file_[delta_file_header_.size() + 0x70] =
921      FirstByteOfStringLength(kExpectedTarget);
922  delta_file_[delta_file_header_.size() + 0x71] =
923      SecondByteOfStringLength(kExpectedTarget) + 1;
924  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
925  bool failed = false;
926  for (size_t i = 0; i < delta_file_.size(); ++i) {
927    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
928      failed = true;
929      break;
930    }
931  }
932  EXPECT_TRUE(failed);
933  // The decoder should not create more target bytes than were expected.
934  EXPECT_GE(expected_target_.size(), output_.size());
935}
936
937TEST_F(VCDiffStandardDecoderTestByteByByte, CopySizeZero) {
938  delta_file_[delta_file_header_.size() + 0x70] = 0;
939  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
940  bool failed = false;
941  for (size_t i = 0; i < delta_file_.size(); ++i) {
942    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
943      failed = true;
944      break;
945    }
946  }
947  EXPECT_TRUE(failed);
948  // The decoder should not create more target bytes than were expected.
949  EXPECT_GE(expected_target_.size(), output_.size());
950}
951
952TEST_F(VCDiffStandardDecoderTestByteByByte, CopySizeTooLargeByOne) {
953  ++delta_file_[delta_file_header_.size() + 0x70];
954  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
955  bool failed = false;
956  for (size_t i = 0; i < delta_file_.size(); ++i) {
957    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
958      failed = true;
959      break;
960    }
961  }
962  EXPECT_TRUE(failed);
963  // The decoder should not create more target bytes than were expected.
964  EXPECT_GE(expected_target_.size(), output_.size());
965}
966
967TEST_F(VCDiffStandardDecoderTestByteByByte, CopySizeTooSmallByOne) {
968  --delta_file_[delta_file_header_.size() + 0x70];
969  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
970  bool failed = false;
971  for (size_t i = 0; i < delta_file_.size(); ++i) {
972    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
973      failed = true;
974      break;
975    }
976  }
977  EXPECT_TRUE(failed);
978  // The decoder should not create more target bytes than were expected.
979  EXPECT_GE(expected_target_.size(), output_.size());
980}
981
982TEST_F(VCDiffStandardDecoderTestByteByByte, CopySizeMaxInt) {
983  WriteMaxVarintAtOffset(0x70, 1);
984  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
985  bool failed = false;
986  for (size_t i = 0; i < delta_file_.size(); ++i) {
987    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
988      failed = true;
989      break;
990    }
991  }
992  EXPECT_TRUE(failed);
993  // The decoder should not create more target bytes than were expected.
994  EXPECT_GE(expected_target_.size(), output_.size());
995}
996
997TEST_F(VCDiffStandardDecoderTestByteByByte, CopySizeNegative) {
998  WriteNegativeVarintAtOffset(0x70, 1);
999  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1000  bool failed = false;
1001  for (size_t i = 0; i < delta_file_.size(); ++i) {
1002    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1003      failed = true;
1004      break;
1005    }
1006  }
1007  EXPECT_TRUE(failed);
1008  // The decoder should not create more target bytes than were expected.
1009  EXPECT_GE(expected_target_.size(), output_.size());
1010}
1011
1012TEST_F(VCDiffStandardDecoderTestByteByByte, CopySizeInvalid) {
1013  WriteInvalidVarintAtOffset(0x70, 1);
1014  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1015  bool failed = false;
1016  for (size_t i = 0; i < delta_file_.size(); ++i) {
1017    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1018      failed = true;
1019      break;
1020    }
1021  }
1022  EXPECT_TRUE(failed);
1023  // The decoder should not create more target bytes than were expected.
1024  EXPECT_GE(expected_target_.size(), output_.size());
1025}
1026
1027TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressBeyondHereAddress) {
1028  delta_file_[delta_file_header_.size() + 0x7B] =
1029      FirstByteOfStringLength(kDictionary);
1030  delta_file_[delta_file_header_.size() + 0x7C] =
1031      SecondByteOfStringLength(kDictionary);
1032  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1033  bool failed = false;
1034  for (size_t i = 0; i < delta_file_.size(); ++i) {
1035    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1036      failed = true;
1037      break;
1038    }
1039  }
1040  EXPECT_TRUE(failed);
1041  // The decoder should not create more target bytes than were expected.
1042  EXPECT_GE(expected_target_.size(), output_.size());
1043}
1044
1045TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressMaxInt) {
1046  WriteMaxVarintAtOffset(0x7B, 1);
1047  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1048  bool failed = false;
1049  for (size_t i = 0; i < delta_file_.size(); ++i) {
1050    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1051      failed = true;
1052      break;
1053    }
1054  }
1055  EXPECT_TRUE(failed);
1056  // The decoder should not create more target bytes than were expected.
1057  EXPECT_GE(expected_target_.size(), output_.size());
1058}
1059
1060TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressNegative) {
1061  WriteNegativeVarintAtOffset(0x70, 1);
1062  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1063  bool failed = false;
1064  for (size_t i = 0; i < delta_file_.size(); ++i) {
1065    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1066      failed = true;
1067      break;
1068    }
1069  }
1070  EXPECT_TRUE(failed);
1071  // The decoder should not create more target bytes than were expected.
1072  EXPECT_GE(expected_target_.size(), output_.size());
1073}
1074
1075TEST_F(VCDiffStandardDecoderTestByteByByte, CopyAddressInvalid) {
1076  WriteInvalidVarintAtOffset(0x70, 1);
1077  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1078  bool failed = false;
1079  for (size_t i = 0; i < delta_file_.size(); ++i) {
1080    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1081      failed = true;
1082      break;
1083    }
1084  }
1085  EXPECT_TRUE(failed);
1086  // The decoder should not create more target bytes than were expected.
1087  EXPECT_GE(expected_target_.size(), output_.size());
1088}
1089
1090TEST_F(VCDiffStandardDecoderTestByteByByte, AddMoreThanExpectedTarget) {
1091  delta_file_[delta_file_header_.size() + 0x72] =
1092      FirstByteOfStringLength(kExpectedTarget);
1093  delta_file_[delta_file_header_.size() + 0x73] =
1094      SecondByteOfStringLength(kExpectedTarget) + 1;
1095  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1096  bool failed = false;
1097  for (size_t i = 0; i < delta_file_.size(); ++i) {
1098    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1099      failed = true;
1100      break;
1101    }
1102  }
1103  EXPECT_TRUE(failed);
1104  // The decoder should not create more target bytes than were expected.
1105  EXPECT_GE(expected_target_.size(), output_.size());
1106}
1107
1108TEST_F(VCDiffStandardDecoderTestByteByByte, AddSizeZero) {
1109  delta_file_[delta_file_header_.size() + 0x72] = 0;
1110  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1111  bool failed = false;
1112  for (size_t i = 0; i < delta_file_.size(); ++i) {
1113    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1114      failed = true;
1115      break;
1116    }
1117  }
1118  EXPECT_TRUE(failed);
1119  // The decoder should not create more target bytes than were expected.
1120  EXPECT_GE(expected_target_.size(), output_.size());
1121}
1122
1123TEST_F(VCDiffStandardDecoderTestByteByByte, AddSizeTooLargeByOne) {
1124  ++delta_file_[delta_file_header_.size() + 0x72];
1125  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1126  bool failed = false;
1127  for (size_t i = 0; i < delta_file_.size(); ++i) {
1128    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1129      failed = true;
1130      break;
1131    }
1132  }
1133  EXPECT_TRUE(failed);
1134  // The decoder should not create more target bytes than were expected.
1135  EXPECT_GE(expected_target_.size(), output_.size());
1136}
1137
1138TEST_F(VCDiffStandardDecoderTestByteByByte, AddSizeTooSmallByOne) {
1139  --delta_file_[delta_file_header_.size() + 0x72];
1140  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1141  bool failed = false;
1142  for (size_t i = 0; i < delta_file_.size(); ++i) {
1143    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1144      failed = true;
1145      break;
1146    }
1147  }
1148  EXPECT_TRUE(failed);
1149  // The decoder should not create more target bytes than were expected.
1150  EXPECT_GE(expected_target_.size(), output_.size());
1151}
1152
1153TEST_F(VCDiffStandardDecoderTestByteByByte, AddSizeMaxInt) {
1154  WriteMaxVarintAtOffset(0x72, 1);
1155  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1156  bool failed = false;
1157  for (size_t i = 0; i < delta_file_.size(); ++i) {
1158    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1159      failed = true;
1160      break;
1161    }
1162  }
1163  EXPECT_TRUE(failed);
1164  // The decoder should not create more target bytes than were expected.
1165  EXPECT_GE(expected_target_.size(), output_.size());
1166}
1167
1168TEST_F(VCDiffStandardDecoderTestByteByByte, AddSizeNegative) {
1169  WriteNegativeVarintAtOffset(0x72, 1);
1170  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1171  bool failed = false;
1172  for (size_t i = 0; i < delta_file_.size(); ++i) {
1173    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1174      failed = true;
1175      break;
1176    }
1177  }
1178  EXPECT_TRUE(failed);
1179  // The decoder should not create more target bytes than were expected.
1180  EXPECT_GE(expected_target_.size(), output_.size());
1181}
1182
1183TEST_F(VCDiffStandardDecoderTestByteByByte, AddSizeInvalid) {
1184  WriteInvalidVarintAtOffset(0x72, 1);
1185  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1186  bool failed = false;
1187  for (size_t i = 0; i < delta_file_.size(); ++i) {
1188    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1189      failed = true;
1190      break;
1191    }
1192  }
1193  EXPECT_TRUE(failed);
1194  // The decoder should not create more target bytes than were expected.
1195  EXPECT_GE(expected_target_.size(), output_.size());
1196}
1197
1198TEST_F(VCDiffStandardDecoderTestByteByByte, RunMoreThanExpectedTarget) {
1199  delta_file_[delta_file_header_.size() + 0x78] =
1200      FirstByteOfStringLength(kExpectedTarget);
1201  delta_file_[delta_file_header_.size() + 0x79] =
1202      SecondByteOfStringLength(kExpectedTarget) + 1;
1203  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1204  bool failed = false;
1205  for (size_t i = 0; i < delta_file_.size(); ++i) {
1206    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1207      failed = true;
1208      break;
1209    }
1210  }
1211  EXPECT_TRUE(failed);
1212  // The decoder should not create more target bytes than were expected.
1213  EXPECT_GE(expected_target_.size(), output_.size());
1214}
1215
1216TEST_F(VCDiffStandardDecoderTestByteByByte, RunSizeZero) {
1217  delta_file_[delta_file_header_.size() + 0x78] = 0;
1218  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1219  bool failed = false;
1220  for (size_t i = 0; i < delta_file_.size(); ++i) {
1221    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1222      failed = true;
1223      break;
1224    }
1225  }
1226  EXPECT_TRUE(failed);
1227  // The decoder should not create more target bytes than were expected.
1228  EXPECT_GE(expected_target_.size(), output_.size());
1229}
1230
1231TEST_F(VCDiffStandardDecoderTestByteByByte, RunSizeTooLargeByOne) {
1232  ++delta_file_[delta_file_header_.size() + 0x78];
1233  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1234  bool failed = false;
1235  for (size_t i = 0; i < delta_file_.size(); ++i) {
1236    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1237      failed = true;
1238      break;
1239    }
1240  }
1241  EXPECT_TRUE(failed);
1242  // The decoder should not create more target bytes than were expected.
1243  EXPECT_GE(expected_target_.size(), output_.size());
1244}
1245
1246TEST_F(VCDiffStandardDecoderTestByteByByte, RunSizeTooSmallByOne) {
1247  --delta_file_[delta_file_header_.size() + 0x78];
1248  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1249  bool failed = false;
1250  for (size_t i = 0; i < delta_file_.size(); ++i) {
1251    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1252      failed = true;
1253      break;
1254    }
1255  }
1256  EXPECT_TRUE(failed);
1257  // The decoder should not create more target bytes than were expected.
1258  EXPECT_GE(expected_target_.size(), output_.size());
1259}
1260
1261TEST_F(VCDiffStandardDecoderTestByteByByte, RunSizeMaxInt) {
1262  WriteMaxVarintAtOffset(0x78, 1);
1263  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1264  bool failed = false;
1265  for (size_t i = 0; i < delta_file_.size(); ++i) {
1266    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1267      failed = true;
1268      break;
1269    }
1270  }
1271  EXPECT_TRUE(failed);
1272  // The decoder should not create more target bytes than were expected.
1273  EXPECT_GE(expected_target_.size(), output_.size());
1274}
1275
1276TEST_F(VCDiffStandardDecoderTestByteByByte, RunSizeNegative) {
1277  WriteNegativeVarintAtOffset(0x78, 1);
1278  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1279  bool failed = false;
1280  for (size_t i = 0; i < delta_file_.size(); ++i) {
1281    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1282      failed = true;
1283      break;
1284    }
1285  }
1286  EXPECT_TRUE(failed);
1287  // The decoder should not create more target bytes than were expected.
1288  EXPECT_GE(expected_target_.size(), output_.size());
1289}
1290
1291TEST_F(VCDiffStandardDecoderTestByteByByte, RunSizeInvalid) {
1292  WriteInvalidVarintAtOffset(0x78, 1);
1293  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
1294  bool failed = false;
1295  for (size_t i = 0; i < delta_file_.size(); ++i) {
1296    if (!decoder_.DecodeChunk(&delta_file_[i], 1, &output_)) {
1297      failed = true;
1298      break;
1299    }
1300  }
1301  EXPECT_TRUE(failed);
1302  // The decoder should not create more target bytes than were expected.
1303  EXPECT_GE(expected_target_.size(), output_.size());
1304}
1305
1306}  // namespace open_vcdiff
1307