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 <string>
19#include "testing.h"
20#include "vcdecoder_test.h"
21#include "vcdiff_defs.h"
22
23namespace open_vcdiff {
24
25TEST_F(VCDiffStandardDecoderTest, DecodeHeaderOnly) {
26  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
27  EXPECT_TRUE(decoder_.DecodeChunk(delta_file_header_.data(),
28                                   delta_file_header_.size(),
29                                   &output_));
30  EXPECT_TRUE(decoder_.FinishDecoding());
31  EXPECT_EQ("", output_);
32}
33
34TEST_F(VCDiffStandardDecoderTest, Decode) {
35  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
36  EXPECT_TRUE(decoder_.DecodeChunk(delta_file_.data(),
37                                   delta_file_.size(),
38                                   &output_));
39  EXPECT_TRUE(decoder_.FinishDecoding());
40  EXPECT_EQ(expected_target_.c_str(), output_);
41}
42
43// If we add a checksum to a standard-format delta file (without using format
44// extensions), it will be interpreted as random bytes inserted into the middle
45// of the file.  The decode operation should fail, but where exactly it fails is
46// not easy to predict.
47TEST_F(VCDiffStandardDecoderTest, StandardFormatDoesNotSupportChecksum) {
48  ComputeAndAddChecksum();
49  InitializeDeltaFile();
50  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
51  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
52                                    delta_file_.size(),
53                                    &output_));
54  EXPECT_EQ("", output_);
55}
56
57// Remove one byte from the length of the chunk to process, and
58// verify that an error is returned for FinishDecoding().
59TEST_F(VCDiffStandardDecoderTest, FinishAfterDecodingPartialWindow) {
60  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
61  EXPECT_TRUE(decoder_.DecodeChunk(delta_file_.data(),
62                                   delta_file_.size() - 1,
63                                   &output_));
64  EXPECT_FALSE(decoder_.FinishDecoding());
65  // The decoder should not create more target bytes than were expected.
66  EXPECT_GE(expected_target_.size(), output_.size());
67}
68
69TEST_F(VCDiffStandardDecoderTest, FinishAfterDecodingPartialWindowHeader) {
70  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
71  EXPECT_TRUE(decoder_.DecodeChunk(delta_file_.data(),
72                                   delta_file_header_.size()
73                                       + delta_window_header_.size() - 1,
74                                   &output_));
75  EXPECT_FALSE(decoder_.FinishDecoding());
76  EXPECT_EQ("", output_);
77}
78
79TEST_F(VCDiffStandardDecoderTest, TargetMatchesWindowSizeLimit) {
80  decoder_.SetMaximumTargetWindowSize(expected_target_.size());
81  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
82  EXPECT_TRUE(decoder_.DecodeChunk(delta_file_.data(),
83                                   delta_file_.size(),
84                                   &output_));
85  EXPECT_TRUE(decoder_.FinishDecoding());
86  EXPECT_EQ(expected_target_.c_str(), output_);
87}
88
89TEST_F(VCDiffStandardDecoderTest, TargetMatchesFileSizeLimit) {
90  decoder_.SetMaximumTargetFileSize(expected_target_.size());
91  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
92  EXPECT_TRUE(decoder_.DecodeChunk(delta_file_.data(),
93                                   delta_file_.size(),
94                                   &output_));
95  EXPECT_TRUE(decoder_.FinishDecoding());
96  EXPECT_EQ(expected_target_.c_str(), output_);
97}
98
99TEST_F(VCDiffStandardDecoderTest, TargetExceedsWindowSizeLimit) {
100  decoder_.SetMaximumTargetWindowSize(expected_target_.size() - 1);
101  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
102  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
103                                    delta_file_.size(),
104                                    &output_));
105  EXPECT_EQ("", output_);
106}
107
108TEST_F(VCDiffStandardDecoderTest, TargetExceedsFileSizeLimit) {
109  decoder_.SetMaximumTargetFileSize(expected_target_.size() - 1);
110  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
111  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
112                                    delta_file_.size(),
113                                    &output_));
114  EXPECT_EQ("", output_);
115}
116
117// Fuzz bits to make sure decoder does not violently crash.
118// This test has no expected behavior except that no crashes should occur.
119// In some cases, changing bits will still decode to the correct target;
120// for example, changing unused bits within a bitfield.
121TEST_F(VCDiffStandardDecoderTest, FuzzBits) {
122  while (FuzzOneByteInDeltaFile()) {
123    decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
124    if (decoder_.DecodeChunk(delta_file_.data(),
125                             delta_file_.size(),
126                             &output_)) {
127      decoder_.FinishDecoding();
128    }
129    InitializeDeltaFile();
130    output_.clear();
131  }
132}
133
134// Change each element of the delta file window to an erroneous value
135// and make sure it's caught as an error.
136
137TEST_F(VCDiffStandardDecoderTest, WinIndicatorHasBothSourceAndTarget) {
138  delta_file_[delta_file_header_.size()] = VCD_SOURCE + VCD_TARGET;
139  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
140  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
141                                    delta_file_.size(),
142                                    &output_));
143  EXPECT_EQ("", output_);
144}
145
146TEST_F(VCDiffStandardDecoderTest, OkayToSetUpperBitsOfWinIndicator) {
147  // It is not an error to set any of the other bits in Win_Indicator
148  // besides VCD_SOURCE and VCD_TARGET.
149  delta_file_[delta_file_header_.size()] = 0xFD;
150  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
151  EXPECT_TRUE(decoder_.DecodeChunk(delta_file_.data(),
152                                   delta_file_.size(),
153                                   &output_));
154  EXPECT_TRUE(decoder_.FinishDecoding());
155  EXPECT_EQ(expected_target_.c_str(), output_);
156}
157
158TEST_F(VCDiffStandardDecoderTest, CopyInstructionsShouldFailIfNoSourceSegment) {
159  // Replace the Win_Indicator and the source size and source offset with a
160  // single 0 byte (a Win_Indicator for a window with no source segment.)
161  delta_window_header_.replace(0, 4, "\0", 1);
162  InitializeDeltaFile();
163  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
164  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
165                                    delta_file_.size(),
166                                    &output_));
167  // The first COPY instruction should fail, so there should be no output
168  EXPECT_EQ("", output_);
169}
170
171TEST_F(VCDiffStandardDecoderTest, SourceSegmentSizeExceedsDictionarySize) {
172  ++delta_file_[delta_file_header_.size() + 2];  // increment size
173  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
174  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
175                                    delta_file_.size(),
176                                    &output_));
177  EXPECT_EQ("", output_);
178}
179
180TEST_F(VCDiffStandardDecoderTest, SourceSegmentSizeMaxInt) {
181  WriteMaxVarintAtOffset(1, 2);
182  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
183  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
184                                    delta_file_.size(),
185                                    &output_));
186  EXPECT_EQ("", output_);
187}
188
189TEST_F(VCDiffStandardDecoderTest, SourceSegmentSizeNegative) {
190  WriteNegativeVarintAtOffset(1, 2);
191  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
192  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
193                                    delta_file_.size(),
194                                    &output_));
195  EXPECT_EQ("", output_);
196}
197
198TEST_F(VCDiffStandardDecoderTest, SourceSegmentSizeInvalid) {
199  WriteInvalidVarintAtOffset(1, 2);
200  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
201  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
202                                    delta_file_.size(),
203                                    &output_));
204  EXPECT_EQ("", output_);
205}
206
207TEST_F(VCDiffStandardDecoderTest, SourceSegmentEndExceedsDictionarySize) {
208  ++delta_file_[delta_file_header_.size() + 3];  // increment start pos
209  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
210  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
211                                    delta_file_.size(),
212                                    &output_));
213  EXPECT_EQ("", output_);
214}
215
216TEST_F(VCDiffStandardDecoderTest, SourceSegmentPosMaxInt) {
217  WriteMaxVarintAtOffset(3, 1);
218  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
219  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
220                                    delta_file_.size(),
221                                    &output_));
222  EXPECT_EQ("", output_);
223}
224
225TEST_F(VCDiffStandardDecoderTest, SourceSegmentPosNegative) {
226  WriteNegativeVarintAtOffset(3, 1);
227  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
228  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
229                                    delta_file_.size(),
230                                    &output_));
231  EXPECT_EQ("", output_);
232}
233
234TEST_F(VCDiffStandardDecoderTest, SourceSegmentPosInvalid) {
235  WriteInvalidVarintAtOffset(3, 1);
236  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
237  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
238                                    delta_file_.size(),
239                                    &output_));
240  EXPECT_EQ("", output_);
241}
242
243TEST_F(VCDiffStandardDecoderTest, DeltaEncodingLengthZero) {
244  delta_file_[delta_file_header_.size() + 4] = 0;
245  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
246  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
247                                    delta_file_.size(),
248                                    &output_));
249  EXPECT_EQ("", output_);
250}
251
252TEST_F(VCDiffStandardDecoderTest, DeltaEncodingLengthTooLargeByOne) {
253  ++delta_file_[delta_file_header_.size() + 4];
254  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
255  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
256                                    delta_file_.size(),
257                                    &output_));
258  EXPECT_EQ("", output_);
259}
260
261TEST_F(VCDiffStandardDecoderTest, DeltaEncodingLengthTooSmallByOne) {
262  --delta_file_[delta_file_header_.size() + 4];
263  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
264  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
265                                    delta_file_.size(),
266                                    &output_));
267  EXPECT_EQ("", output_);
268}
269
270TEST_F(VCDiffStandardDecoderTest, DeltaEncodingLengthMaxInt) {
271  WriteMaxVarintAtOffset(4, 1);
272  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
273  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
274                                    delta_file_.size(),
275                                    &output_));
276  EXPECT_EQ("", output_);
277}
278
279TEST_F(VCDiffStandardDecoderTest, DeltaEncodingLengthNegative) {
280  WriteNegativeVarintAtOffset(4, 1);
281  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
282  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
283                                    delta_file_.size(),
284                                    &output_));
285  EXPECT_EQ("", output_);
286}
287
288TEST_F(VCDiffStandardDecoderTest, DeltaEncodingLengthInvalid) {
289  WriteInvalidVarintAtOffset(4, 1);
290  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
291  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
292                                    delta_file_.size(),
293                                    &output_));
294  EXPECT_EQ("", output_);
295}
296
297TEST_F(VCDiffStandardDecoderTest, TargetWindowSizeZero) {
298  static const char zero_size[] = { 0x00 };
299  delta_file_.replace(delta_file_header_.size() + 5, 2, zero_size, 1);
300  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
301  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
302                                    delta_file_.size(),
303                                    &output_));
304  EXPECT_EQ("", output_);
305}
306
307TEST_F(VCDiffStandardDecoderTest, TargetWindowSizeTooLargeByOne) {
308  ++delta_file_[delta_file_header_.size() + 6];
309  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
310  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
311                                    delta_file_.size(),
312                                    &output_));
313  EXPECT_EQ("", output_);
314}
315
316TEST_F(VCDiffStandardDecoderTest, TargetWindowSizeTooSmallByOne) {
317  --delta_file_[delta_file_header_.size() + 6];
318  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
319  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
320                                    delta_file_.size(),
321                                    &output_));
322  EXPECT_EQ("", output_);
323}
324
325TEST_F(VCDiffStandardDecoderTest, TargetWindowSizeMaxInt) {
326  WriteMaxVarintAtOffset(5, 2);
327  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
328  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
329                                    delta_file_.size(),
330                                    &output_));
331  EXPECT_EQ("", output_);
332}
333
334TEST_F(VCDiffStandardDecoderTest, TargetWindowSizeNegative) {
335  WriteNegativeVarintAtOffset(5, 2);
336  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
337  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
338                                    delta_file_.size(),
339                                    &output_));
340  EXPECT_EQ("", output_);
341}
342
343TEST_F(VCDiffStandardDecoderTest, TargetWindowSizeInvalid) {
344  WriteInvalidVarintAtOffset(5, 2);
345  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
346  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
347                                    delta_file_.size(),
348                                    &output_));
349  EXPECT_EQ("", output_);
350}
351
352TEST_F(VCDiffStandardDecoderTest, OkayToSetUpperBitsOfDeltaIndicator) {
353  delta_file_[delta_file_header_.size() + 7] = 0xF8;
354  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
355  EXPECT_TRUE(decoder_.DecodeChunk(delta_file_.data(),
356                                   delta_file_.size(),
357                                   &output_));
358  EXPECT_TRUE(decoder_.FinishDecoding());
359  EXPECT_EQ(expected_target_.c_str(), output_);
360}
361
362TEST_F(VCDiffStandardDecoderTest, DataCompressionNotSupported) {
363  delta_file_[delta_file_header_.size() + 7] = 0x01;
364  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
365  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
366                                    delta_file_.size(),
367                                    &output_));
368  EXPECT_EQ("", output_);
369}
370
371TEST_F(VCDiffStandardDecoderTest, InstructionCompressionNotSupported) {
372  delta_file_[delta_file_header_.size() + 7] = 0x02;
373  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
374  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
375                                    delta_file_.size(),
376                                    &output_));
377  EXPECT_EQ("", output_);
378}
379
380TEST_F(VCDiffStandardDecoderTest, AddressCompressionNotSupported) {
381  delta_file_[delta_file_header_.size() + 7] = 0x04;
382  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
383  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
384                                    delta_file_.size(),
385                                    &output_));
386  EXPECT_EQ("", output_);
387}
388
389TEST_F(VCDiffStandardDecoderTest, AddRunDataSizeZero) {
390  delta_file_[delta_file_header_.size() + 8] = 0;
391  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
392  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
393                                    delta_file_.size(),
394                                    &output_));
395  EXPECT_EQ("", output_);
396}
397
398TEST_F(VCDiffStandardDecoderTest, AddRunDataSizeTooLargeByOne) {
399  ++delta_file_[delta_file_header_.size() + 8];
400  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
401  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
402                                    delta_file_.size(),
403                                    &output_));
404  EXPECT_EQ("", output_);
405}
406
407TEST_F(VCDiffStandardDecoderTest, AddRunDataSizeTooSmallByOne) {
408  --delta_file_[delta_file_header_.size() + 8];
409  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
410  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
411                                    delta_file_.size(),
412                                    &output_));
413  EXPECT_EQ("", output_);
414}
415
416TEST_F(VCDiffStandardDecoderTest, AddRunDataSizeMaxInt) {
417  WriteMaxVarintAtOffset(8, 1);
418  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
419  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
420                                    delta_file_.size(),
421                                    &output_));
422  EXPECT_EQ("", output_);
423}
424
425TEST_F(VCDiffStandardDecoderTest, AddRunDataSizeNegative) {
426  WriteNegativeVarintAtOffset(8, 1);
427  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
428  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
429                                    delta_file_.size(),
430                                    &output_));
431  EXPECT_EQ("", output_);
432}
433
434TEST_F(VCDiffStandardDecoderTest, AddRunDataSizeInvalid) {
435  WriteInvalidVarintAtOffset(8, 1);
436  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
437  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
438                                    delta_file_.size(),
439                                    &output_));
440  EXPECT_EQ("", output_);
441}
442
443TEST_F(VCDiffStandardDecoderTest, InstructionsSizeZero) {
444  delta_file_[delta_file_header_.size() + 9] = 0;
445  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
446  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
447                                    delta_file_.size(),
448                                    &output_));
449  EXPECT_EQ("", output_);
450}
451
452TEST_F(VCDiffStandardDecoderTest, InstructionsSizeTooLargeByOne) {
453  ++delta_file_[delta_file_header_.size() + 9];
454  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
455  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
456                                    delta_file_.size(),
457                                    &output_));
458  EXPECT_EQ("", output_);
459}
460
461TEST_F(VCDiffStandardDecoderTest, InstructionsSizeTooSmallByOne) {
462  --delta_file_[delta_file_header_.size() + 9];
463  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
464  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
465                                    delta_file_.size(),
466                                    &output_));
467  EXPECT_EQ("", output_);
468}
469
470TEST_F(VCDiffStandardDecoderTest, InstructionsSizeMaxInt) {
471  WriteMaxVarintAtOffset(9, 1);
472  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
473  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
474                                    delta_file_.size(),
475                                    &output_));
476  EXPECT_EQ("", output_);
477}
478
479TEST_F(VCDiffStandardDecoderTest, InstructionsSizeNegative) {
480  WriteNegativeVarintAtOffset(9, 1);
481  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
482  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
483                                    delta_file_.size(),
484                                    &output_));
485  EXPECT_EQ("", output_);
486}
487
488TEST_F(VCDiffStandardDecoderTest, InstructionsSizeInvalid) {
489  WriteInvalidVarintAtOffset(9, 1);
490  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
491  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
492                                    delta_file_.size(),
493                                    &output_));
494  EXPECT_EQ("", output_);
495}
496
497TEST_F(VCDiffStandardDecoderTest, CopyAddressSizeZero) {
498  delta_file_[delta_file_header_.size() + 10] = 0;
499  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
500  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
501                                    delta_file_.size(),
502                                    &output_));
503  EXPECT_EQ("", output_);
504}
505
506TEST_F(VCDiffStandardDecoderTest, CopyAddressSizeTooLargeByOne) {
507  ++delta_file_[delta_file_header_.size() + 10];
508  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
509  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
510                                    delta_file_.size(),
511                                    &output_));
512  EXPECT_EQ("", output_);
513}
514
515TEST_F(VCDiffStandardDecoderTest, CopyAddressSizeTooSmallByOne) {
516  --delta_file_[delta_file_header_.size() + 10];
517  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
518  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
519                                    delta_file_.size(),
520                                    &output_));
521  EXPECT_EQ("", output_);
522}
523
524TEST_F(VCDiffStandardDecoderTest, CopyAddressSizeMaxInt) {
525  WriteMaxVarintAtOffset(10, 1);
526  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
527  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
528                                    delta_file_.size(),
529                                    &output_));
530  EXPECT_EQ("", output_);
531}
532
533TEST_F(VCDiffStandardDecoderTest, CopyAddressSizeNegative) {
534  WriteNegativeVarintAtOffset(10, 1);
535  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
536  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
537                                    delta_file_.size(),
538                                    &output_));
539  EXPECT_EQ("", output_);
540}
541
542TEST_F(VCDiffStandardDecoderTest, CopyAddressSizeInvalid) {
543  WriteInvalidVarintAtOffset(10, 1);
544  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
545  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
546                                    delta_file_.size(),
547                                    &output_));
548  EXPECT_EQ("", output_);
549}
550
551TEST_F(VCDiffStandardDecoderTest, InstructionsEndEarly) {
552  --delta_file_[delta_file_header_.size() + 9];
553  ++delta_file_[delta_file_header_.size() + 10];
554  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
555  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
556                                    delta_file_.size(),
557                                    &output_));
558  EXPECT_EQ("", output_);
559}
560
561// From this point on, the tests should also be run against the interleaved
562// format.
563
564TEST_F(VCDiffStandardDecoderTest, CopyMoreThanExpectedTarget) {
565  delta_file_[delta_file_header_.size() + 0x70] =
566      FirstByteOfStringLength(kExpectedTarget);
567  delta_file_[delta_file_header_.size() + 0x71] =
568      SecondByteOfStringLength(kExpectedTarget) + 1;
569  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
570  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
571                                    delta_file_.size(),
572                                    &output_));
573  EXPECT_EQ("", output_);
574}
575
576TEST_F(VCDiffStandardDecoderTest, CopySizeZero) {
577  delta_file_[delta_file_header_.size() + 0x70] = 0;
578  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
579  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
580                                    delta_file_.size(),
581                                    &output_));
582  EXPECT_EQ("", output_);
583}
584
585TEST_F(VCDiffStandardDecoderTest, CopySizeTooLargeByOne) {
586  ++delta_file_[delta_file_header_.size() + 0x70];
587  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
588  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
589                                    delta_file_.size(),
590                                    &output_));
591  EXPECT_EQ("", output_);
592}
593
594TEST_F(VCDiffStandardDecoderTest, CopySizeTooSmallByOne) {
595  --delta_file_[delta_file_header_.size() + 0x70];
596  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
597  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
598                                    delta_file_.size(),
599                                    &output_));
600  EXPECT_EQ("", output_);
601}
602
603TEST_F(VCDiffStandardDecoderTest, CopySizeMaxInt) {
604  WriteMaxVarintAtOffset(0x70, 1);
605  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
606  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
607                                    delta_file_.size(),
608                                    &output_));
609  EXPECT_EQ("", output_);
610}
611
612TEST_F(VCDiffStandardDecoderTest, CopySizeNegative) {
613  WriteNegativeVarintAtOffset(0x70, 1);
614  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
615  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
616                                    delta_file_.size(),
617                                    &output_));
618  EXPECT_EQ("", output_);
619}
620
621TEST_F(VCDiffStandardDecoderTest, CopySizeInvalid) {
622  WriteInvalidVarintAtOffset(0x70, 1);
623  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
624  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
625                                    delta_file_.size(),
626                                    &output_));
627  EXPECT_EQ("", output_);
628}
629
630TEST_F(VCDiffStandardDecoderTest, CopyAddressBeyondHereAddress) {
631  delta_file_[delta_file_header_.size() + 0x7B] =
632      FirstByteOfStringLength(kDictionary);
633  delta_file_[delta_file_header_.size() + 0x7C] =
634      SecondByteOfStringLength(kDictionary);
635  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
636  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
637                                    delta_file_.size(),
638                                    &output_));
639  EXPECT_EQ("", output_);
640}
641
642TEST_F(VCDiffStandardDecoderTest, CopyAddressMaxInt) {
643  WriteMaxVarintAtOffset(0x7B, 1);
644  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
645  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
646                                    delta_file_.size(),
647                                    &output_));
648  EXPECT_EQ("", output_);
649}
650
651TEST_F(VCDiffStandardDecoderTest, CopyAddressNegative) {
652  WriteNegativeVarintAtOffset(0x70, 1);
653  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
654  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
655                                    delta_file_.size(),
656                                    &output_));
657  EXPECT_EQ("", output_);
658}
659
660TEST_F(VCDiffStandardDecoderTest, CopyAddressInvalid) {
661  WriteInvalidVarintAtOffset(0x70, 1);
662  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
663  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
664                                    delta_file_.size(),
665                                    &output_));
666  EXPECT_EQ("", output_);
667}
668
669TEST_F(VCDiffStandardDecoderTest, AddMoreThanExpectedTarget) {
670  delta_file_[delta_file_header_.size() + 0x72] =
671      FirstByteOfStringLength(kExpectedTarget);
672  delta_file_[delta_file_header_.size() + 0x73] =
673      SecondByteOfStringLength(kExpectedTarget) + 1;
674  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
675  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
676                                    delta_file_.size(),
677                                    &output_));
678  EXPECT_EQ("", output_);
679}
680
681TEST_F(VCDiffStandardDecoderTest, AddSizeZero) {
682  delta_file_[delta_file_header_.size() + 0x72] = 0;
683  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
684  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
685                                    delta_file_.size(),
686                                    &output_));
687  EXPECT_EQ("", output_);
688}
689
690TEST_F(VCDiffStandardDecoderTest, AddSizeTooLargeByOne) {
691  ++delta_file_[delta_file_header_.size() + 0x72];
692  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
693  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
694                                    delta_file_.size(),
695                                    &output_));
696  EXPECT_EQ("", output_);
697}
698
699TEST_F(VCDiffStandardDecoderTest, AddSizeTooSmallByOne) {
700  --delta_file_[delta_file_header_.size() + 0x72];
701  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
702  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
703                                    delta_file_.size(),
704                                    &output_));
705  EXPECT_EQ("", output_);
706}
707
708TEST_F(VCDiffStandardDecoderTest, AddSizeMaxInt) {
709  WriteMaxVarintAtOffset(0x72, 1);
710  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
711  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
712                                    delta_file_.size(),
713                                    &output_));
714  EXPECT_EQ("", output_);
715}
716
717TEST_F(VCDiffStandardDecoderTest, AddSizeNegative) {
718  WriteNegativeVarintAtOffset(0x72, 1);
719  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
720  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
721                                    delta_file_.size(),
722                                    &output_));
723  EXPECT_EQ("", output_);
724}
725
726TEST_F(VCDiffStandardDecoderTest, AddSizeInvalid) {
727  WriteInvalidVarintAtOffset(0x72, 1);
728  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
729  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
730                                    delta_file_.size(),
731                                    &output_));
732  EXPECT_EQ("", output_);
733}
734
735TEST_F(VCDiffStandardDecoderTest, RunMoreThanExpectedTarget) {
736  delta_file_[delta_file_header_.size() + 0x78] =
737      FirstByteOfStringLength(kExpectedTarget);
738  delta_file_[delta_file_header_.size() + 0x79] =
739      SecondByteOfStringLength(kExpectedTarget) + 1;
740  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
741  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
742                                    delta_file_.size(),
743                                    &output_));
744  EXPECT_EQ("", output_);
745}
746
747TEST_F(VCDiffStandardDecoderTest, RunSizeZero) {
748  delta_file_[delta_file_header_.size() + 0x78] = 0;
749  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
750  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
751                                    delta_file_.size(),
752                                    &output_));
753  EXPECT_EQ("", output_);
754}
755
756TEST_F(VCDiffStandardDecoderTest, RunSizeTooLargeByOne) {
757  ++delta_file_[delta_file_header_.size() + 0x78];
758  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
759  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
760                                    delta_file_.size(),
761                                    &output_));
762  EXPECT_EQ("", output_);
763}
764
765TEST_F(VCDiffStandardDecoderTest, RunSizeTooSmallByOne) {
766  --delta_file_[delta_file_header_.size() + 0x78];
767  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
768  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
769                                    delta_file_.size(),
770                                    &output_));
771  EXPECT_EQ("", output_);
772}
773
774TEST_F(VCDiffStandardDecoderTest, RunSizeMaxInt) {
775  WriteMaxVarintAtOffset(0x78, 1);
776  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
777  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
778                                    delta_file_.size(),
779                                    &output_));
780  EXPECT_EQ("", output_);
781}
782
783TEST_F(VCDiffStandardDecoderTest, RunSizeNegative) {
784  WriteNegativeVarintAtOffset(0x78, 1);
785  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
786  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
787                                    delta_file_.size(),
788                                    &output_));
789  EXPECT_EQ("", output_);
790}
791
792TEST_F(VCDiffStandardDecoderTest, RunSizeInvalid) {
793  WriteInvalidVarintAtOffset(0x78, 1);
794  decoder_.StartDecoding(dictionary_.data(), dictionary_.size());
795  EXPECT_FALSE(decoder_.DecodeChunk(delta_file_.data(),
796                                    delta_file_.size(),
797                                    &output_));
798  EXPECT_EQ("", output_);
799}
800
801}  // namespace open_vcdiff
802