aboutsummaryrefslogtreecommitdiff
path: root/applier/src/test/java/com/google/archivepatcher/applier/bsdiff/BsPatchTest.java
blob: ddb1c879be0eb4e26462d6c08253702ddbe28cad (plain)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.archivepatcher.applier.bsdiff;

import com.google.archivepatcher.applier.PatchFormatException;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

/**
 * Tests for {@link BsPatch}.
 */
@RunWith(JUnit4.class)
public class BsPatchTest {

  private static final String SIGNATURE = "ENDSLEY/BSDIFF43";
  private byte[] buffer1;
  private byte[] buffer2;

  /**
   * The tests need access to an actual File object for the "old file", so that it can be used as
   * the argument to a RandomAccessFile constructor... but the old file is a resource loaded at test
   * run-time, potentially from a JAR, and therefore a copy must be made in the filesystem to access
   * via RandomAccessFile. This is not true for the new file or the patch file, both of which are
   * streamable.
   */
  private File oldFile;

  @Before
  public void setUp() throws IOException {
    buffer1 = new byte[6];
    buffer2 = new byte[6];
    try {
      oldFile = File.createTempFile("archive_patcher", "old");
      oldFile.deleteOnExit();
    } catch (IOException e) {
      if (oldFile != null) {
        oldFile.delete();
      }
      throw e;
    }
  }

  @After
  public void tearDown() {
    if (oldFile != null) {
      oldFile.delete();
    }
    oldFile = null;
  }

  @Test
  public void testTransformBytes() throws IOException {
    // In this case the "patch stream" is just a stream of addends that transformBytes(...) will
    // apply to the old data file.
    final byte[] patchInput = "this is a sample string to read".getBytes("US-ASCII");
    final ByteArrayInputStream patchInputStream = new ByteArrayInputStream(patchInput);
    copyToOldFile("bsdifftest_partial_a.txt");
    RandomAccessFile oldData = new RandomAccessFile(oldFile, "r");
    final byte[] expectedNewData = readTestData("bsdifftest_partial_b.bin");
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    BsPatch.transformBytes(patchInput.length, patchInputStream, oldData, newData, buffer1, buffer2);
    byte[] actual = newData.toByteArray();
    Assert.assertArrayEquals(expectedNewData, actual);
  }

  @Test
  public void testTransformBytes_Error_NotEnoughBytes() throws IOException {
    // This test sets up a trivial 1-byte "patch" (addends) stream but then asks
    // transformBytes(...) to apply *2* bytes, which should fail when it hits EOF.
    final InputStream patchIn = new ByteArrayInputStream(new byte[] {(byte) 0x00});
    copyToOldFile("bsdifftest_partial_a.txt"); // Any file would work here
    RandomAccessFile oldData = new RandomAccessFile(oldFile, "r");
    try {
      BsPatch.transformBytes(2, patchIn, oldData, new ByteArrayOutputStream(), buffer1, buffer2);
      Assert.fail("Read past EOF");
    } catch (IOException expected) {
      // Pass
    }
  }

  @Test
  public void testTransformBytes_Error_JunkPatch() throws IOException {
    final byte[] patchInput = "this is a second sample string to read".getBytes("US-ASCII");
    final ByteArrayInputStream patchInputStream = new ByteArrayInputStream(patchInput);
    copyToOldFile("bsdifftest_partial_a.txt"); // Any file would work here
    RandomAccessFile oldData = new RandomAccessFile(oldFile, "r");
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.transformBytes(
          patchInput.length, patchInputStream, oldData, newData, buffer1, buffer2);
      Assert.fail("Should have thrown an IOException");
    } catch (IOException expected) {
      // Pass
    }
  }

  @Test
  public void testTransformBytes_Error_JunkPatch_Underflow() throws IOException {
    final byte[] patchInput = "this is a sample string".getBytes("US-ASCII");
    final ByteArrayInputStream patchInputStream = new ByteArrayInputStream(patchInput);
    copyToOldFile("bsdifftest_partial_a.txt");
    RandomAccessFile oldData = new RandomAccessFile(oldFile, "r");
    final byte[] buffer1 = new byte[6];
    final byte[] buffer2 = new byte[6];

    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.transformBytes(
          patchInput.length + 1, patchInputStream, oldData, newData, buffer1, buffer2);
      Assert.fail("Should have thrown an IOException");
    } catch (IOException expected) {
      // Pass
    }
  }

  @Test
  public void testApplyPatch_ContrivedData() throws Exception {
    invokeApplyPatch(
        "bsdifftest_internal_blob_a.bin",
        "bsdifftest_internal_patch_a_to_b.bin",
        "bsdifftest_internal_blob_b.bin");
  }

  @Test
  public void testApplyPatch_BetterData() throws Exception {
    invokeApplyPatch(
        "bsdifftest_minimal_blob_a.bin",
        "bsdifftest_minimal_patch_a_to_b.bin",
        "bsdifftest_minimal_blob_b.bin");
  }

  @Test
  public void testApplyPatch_BadSignature() throws Exception {
    createEmptyOldFile(10);
    String junkSignature = "WOOOOOO/BSDIFF43"; // Correct length, wrong content
    InputStream patchIn =
        makePatch(
            junkSignature,
            10, // newLength
            10, // diffSegmentLength
            0, // copySegmentLength
            0, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with bad signature");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("bad signature", actual);
    }
  }

  @Test
  public void testApplyPatch_NewLengthNegative() throws Exception {
    createEmptyOldFile(10);
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            -10, // newLength (illegal)
            10, // diffSegmentLength
            0, // copySegmentLength
            0, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with negative newLength");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("bad newSize", actual);
    }
  }

  @Test
  public void testApplyPatch_NewLengthTooLarge() throws Exception {
    createEmptyOldFile(10);
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            Integer.MAX_VALUE + 1, // newLength (max supported is Integer.MAX_VALUE)
            10, // diffSegmentLength
            0, // copySegmentLength
            0, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(
          new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with excessive newLength");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("bad newSize", actual);
    }
  }

  @Test
  public void testApplyPatch_DiffSegmentLengthNegative() throws Exception {
    createEmptyOldFile(10);
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            10, // newLength
            -10, // diffSegmentLength (negative)
            0, // copySegmentLength
            0, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with negative diffSegmentLength");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("bad diffSegmentLength", actual);
    }
  }

  @Test
  public void testApplyPatch_DiffSegmentLengthTooLarge() throws Exception {
    createEmptyOldFile(10);
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            10, // newLength
            Integer.MAX_VALUE + 1, // diffSegmentLength (too big)
            0, // copySegmentLength
            0, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with excessive diffSegmentLength");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("bad diffSegmentLength", actual);
    }
  }

  @Test
  public void testApplyPatch_CopySegmentLengthNegative() throws Exception {
    createEmptyOldFile(10);
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            10, // newLength
            10, // diffSegmentLength
            -10, // copySegmentLength (negative)
            0, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with negative copySegmentLength");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("bad copySegmentLength", actual);
    }
  }

  @Test
  public void testApplyPatch_CopySegmentLengthTooLarge() throws Exception {
    createEmptyOldFile(10);
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            10, // newLength
            0, // diffSegmentLength
            Integer.MAX_VALUE + 1, // copySegmentLength (too big)
            0, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with excessive copySegmentLength");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("bad copySegmentLength", actual);
    }
  }

  // ExpectedFinalNewDataBytesWritten_Negative case is impossible in code, so no need to test
  // that; just the TooLarge condition.
  @Test
  public void testApplyPatch_ExpectedFinalNewDataBytesWritten_PastEOF() throws Exception {
    createEmptyOldFile(10);
    // Make diffSegmentLength + copySegmentLength > newLength
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            10, // newLength
            10, // diffSegmentLength
            1, // copySegmentLength
            0, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch that moves past EOF in new file");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("expectedFinalNewDataBytesWritten too large", actual);
    }
  }

  @Test
  public void testApplyPatch_ExpectedFinalOldDataOffset_Negative() throws Exception {
    createEmptyOldFile(10);
    // Make diffSegmentLength + offsetToNextInput < 0
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            10, // newLength
            10, // diffSegmentLength
            0, // copySegmentLength
            -11, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with that moves to a negative offset in old file");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("expectedFinalOldDataOffset is negative", actual);
    }
  }

  @Test
  public void testApplyPatch_ExpectedFinalOldDataOffset_PastEOF() throws Exception {
    createEmptyOldFile(10);
    // Make diffSegmentLength + offsetToNextInput > oldLength
    InputStream patchIn =
        makePatch(
            SIGNATURE,
            10, // newLength
            10, // diffSegmentLength
            0, // copySegmentLength
            1, // offsetToNextInput
            new byte[10] // addends
            );
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with that moves past EOF in old file");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("expectedFinalOldDataOffset too large", actual);
    }
  }

  @Test
  public void testApplyPatch_TruncatedSignature() throws Exception {
    createEmptyOldFile(10);
    InputStream patchIn = new ByteArrayInputStream("X".getBytes("US-ASCII"));
    ByteArrayOutputStream newData = new ByteArrayOutputStream();
    try {
      BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn);
      Assert.fail("Read patch with truncated signature");
    } catch (PatchFormatException expected) {
      // No way to mock the internal logic, so resort to testing exception string for coverage
      String actual = expected.getMessage();
      Assert.assertEquals("truncated signature", actual);
    }
  }

  @Test
  public void testReadBsdiffLong() throws Exception {
    byte[] data = {
      (byte) 0x78,
      (byte) 0x56,
      (byte) 0x34,
      (byte) 0x12,
      (byte) 0,
      (byte) 0,
      (byte) 0,
      (byte) 0,
      (byte) 0xef,
      (byte) 0xbe,
      (byte) 0xad,
      (byte) 0x0e,
      (byte) 0,
      (byte) 0,
      (byte) 0,
      (byte) 0
    };
    ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
    long actual = BsPatch.readBsdiffLong(inputStream);
    Assert.assertEquals(0x12345678, actual);
    actual = BsPatch.readBsdiffLong(inputStream);
    Assert.assertEquals(0x0eadbeef, actual);
  }

  @Test
  public void testReadBsdiffLong_Zero() throws Exception {
    long expected = 0x00000000L;
    long actual =
        BsPatch.readBsdiffLong(
            new ByteArrayInputStream(
                new byte[] {
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00
                }));
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void testReadBsdiffLong_IntegerMaxValue() throws Exception {
    long expected = 0x7fffffffL;
    long actual =
        BsPatch.readBsdiffLong(
            new ByteArrayInputStream(
                new byte[] {
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0x7f,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00
                }));
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void testReadBsdiffLong_IntegerMinValue() throws Exception {
    long expected = -0x80000000L;
    long actual =
        BsPatch.readBsdiffLong(
            new ByteArrayInputStream(
                new byte[] {
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x80,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x00,
                  (byte) 0x80
                }));
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void testReadBsdiffLong_LongMaxValue() throws Exception {
    long expected = 0x7fffffffffffffffL;
    long actual =
        BsPatch.readBsdiffLong(
            new ByteArrayInputStream(
                new byte[] {
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0x7f
                }));
    Assert.assertEquals(expected, actual);
  }

  // Can't read Long.MIN_VALUE because the signed-magnitude representation stops at
  // Long.MIN_VALUE+1.
  @Test
  public void testReadBsdiffLong_LongMinValueIsh() throws Exception {
    long expected = -0x7fffffffffffffffL;
    long actual =
        BsPatch.readBsdiffLong(
            new ByteArrayInputStream(
                new byte[] {
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff,
                  (byte) 0xff
                }));
    Assert.assertEquals(expected, actual);
  }

  // This is also Java's Long.MAX_VALUE.
  @Test
  public void testReadBsdiffLong_NegativeZero() throws Exception {
    try {
      BsPatch.readBsdiffLong(
          new ByteArrayInputStream(
              new byte[] {
                (byte) 0x00,
                (byte) 0x00,
                (byte) 0x00,
                (byte) 0x00,
                (byte) 0x00,
                (byte) 0x00,
                (byte) 0x00,
                (byte) 0x80
              }));
      Assert.fail("Tolerated negative zero");
    } catch (PatchFormatException expected) {
      // Pass
    }
  }

  @Test
  public void testReadFully() throws IOException {
    final byte[] input = "this is a sample string to read".getBytes();
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    final byte[] dst = new byte[50];

    try {
      BsPatch.readFully(inputStream, dst, 0, 50);
      Assert.fail("Should've thrown an IOException");
    } catch (IOException expected) {
      // Pass
    }

    inputStream.reset();
    BsPatch.readFully(inputStream, dst, 0, input.length);
    Assert.assertTrue(regionEquals(dst, 0, input, 0, input.length));

    inputStream.reset();
    BsPatch.readFully(inputStream, dst, 40, 10);
    Assert.assertTrue(regionEquals(dst, 40, input, 0, 10));

    inputStream.reset();
    try {
      BsPatch.readFully(inputStream, dst, 45, 11);
      Assert.fail("Should've thrown an IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException expected) {
      // Pass
    }
  }

  @Test
  public void testPipe() throws IOException {
    final String inputString = "this is a sample string to read";
    final byte[] input = inputString.getBytes("US-ASCII");
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    final byte[] buffer = new byte[5];
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    BsPatch.pipe(inputStream, outputStream, buffer, 0);
    int actualLength = outputStream.toByteArray().length;
    Assert.assertEquals(0, actualLength);

    inputStream.reset();
    BsPatch.pipe(inputStream, outputStream, buffer, 1);
    actualLength = outputStream.toByteArray().length;
    Assert.assertEquals(1, actualLength);
    byte actualByte = outputStream.toByteArray()[0];
    Assert.assertEquals((byte) 't', actualByte);

    outputStream = new ByteArrayOutputStream();
    inputStream.reset();
    BsPatch.pipe(inputStream, outputStream, buffer, 5);
    actualLength = outputStream.toByteArray().length;
    Assert.assertEquals(5, actualLength);
    String actualOutput = outputStream.toString();
    String expectedOutput = inputString.substring(0, 5);
    Assert.assertEquals(expectedOutput, actualOutput);

    outputStream = new ByteArrayOutputStream();
    inputStream.reset();
    BsPatch.pipe(inputStream, outputStream, buffer, input.length);
    actualLength = outputStream.toByteArray().length;
    Assert.assertEquals(input.length, actualLength);
    expectedOutput = outputStream.toString();
    Assert.assertEquals(inputString, expectedOutput);
  }

  @Test
  public void testPipe_Underrun() {
    int dataLength = 10;
    ByteArrayInputStream in = new ByteArrayInputStream(new byte[dataLength]);
    try {
      // Tell pipe to copy 1 more byte than is actually available
      BsPatch.pipe(in, new ByteArrayOutputStream(), new byte[dataLength], dataLength + 1);
      Assert.fail("Should've thrown an IOException");
    } catch (IOException expected) {
      // Pass
    }
  }

  @Test
  public void testPipe_CopyZeroBytes() throws IOException {
    int dataLength = 0;
    ByteArrayInputStream in = new ByteArrayInputStream(new byte[dataLength]);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BsPatch.pipe(in, out, new byte[100], dataLength);
    int actualLength = out.toByteArray().length;
    Assert.assertEquals(0, actualLength);
  }

  /**
   * Invoke applyPatch(...) and verify that the results are as expected.
   * @param oldPath the path to the old asset in /assets
   * @param patchPatch the path to the patch asset in /assets
   * @param newPath the path to the new asset in /assets
   * @throws IOException if unable to read/write
   * @throws PatchFormatException if the patch is invalid
   */
  private void invokeApplyPatch(String oldPath, String patchPatch, String newPath)
      throws IOException, PatchFormatException {
    copyToOldFile(oldPath);
    RandomAccessFile oldData = new RandomAccessFile(oldFile, "r");
    InputStream patchInputStream = new ByteArrayInputStream(readTestData(patchPatch));
    byte[] expectedNewDataBytes = readTestData(newPath);
    ByteArrayOutputStream actualNewData = new ByteArrayOutputStream();
    BsPatch.applyPatch(oldData, actualNewData, patchInputStream);
    byte[] actualNewDataBytes = actualNewData.toByteArray();
    Assert.assertArrayEquals(expectedNewDataBytes, actualNewDataBytes);
  }

  /**
   * Checks two byte ranges for equivalence.
   *
   * @param data1  first array
   * @param data2  second array
   * @param start1 first byte to compare in |data1|
   * @param start2 first byte to compare in |data2|
   * @param length the number of bytes to compare
   */
  private static boolean regionEquals(
      final byte[] data1,
      final int start1,
      final byte[] data2,
      final int start2,
      final int length) {
    for (int x = 0; x < length; x++) {
      if (data1[x + start1] != data2[x + start2]) {
        return false;
      }
    }
    return true;
  }

  // (Copied from BsDiffTest)
  // Some systems force all text files to end in a newline, which screws up this test.
  private static byte[] stripNewlineIfNecessary(byte[] b) {
    if (b[b.length - 1] != (byte) '\n') {
      return b;
    }

    byte[] ret = new byte[b.length - 1];
    System.arraycopy(b, 0, ret, 0, ret.length);
    return ret;
  }

  // (Copied from BsDiffTest)
  private byte[] readTestData(String testDataFileName) throws IOException {
    InputStream in = getClass().getResourceAsStream("testdata/" + testDataFileName);
    Assert.assertNotNull("test data file doesn't exist: " + testDataFileName, in);
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[32768];
    int numRead = 0;
    while ((numRead = in.read(buffer)) >= 0) {
      result.write(buffer, 0, numRead);
    }
    return stripNewlineIfNecessary(result.toByteArray());
  }

  /**
   * Copy the contents of the specified testdata asset into {@link #oldFile}.
   * @param testDataFileName the name of the testdata asset to read
   * @throws IOException if unable to complete the copy
   */
  private void copyToOldFile(String testDataFileName) throws IOException {
    oldFile = File.createTempFile("archive_patcher", "temp");
    Assert.assertNotNull("cant create file!", oldFile);
    byte[] buffer = readTestData(testDataFileName);
    FileOutputStream out = new FileOutputStream(oldFile);
    out.write(buffer);
    out.flush();
    out.close();
  }

  /**
   * Make {@link #oldFile} an empty file (full of binary zeroes) of the specified length.
   * @param desiredLength the desired length in bytes
   * @throws IOException if unable to write the file
   */
  private void createEmptyOldFile(int desiredLength) throws IOException {
    OutputStream out = new FileOutputStream(oldFile);
    for (int x = 0; x < desiredLength; x++) {
      out.write(0);
    }
    out.close();
  }

  /**
   * Create an arbitrary patch that consists of a signature, a length, and a directive sequence.
   * Used to manufacture junk for failure and edge cases.
   * @param signature the signature to use
   * @param newLength the expected length of the "new" file produced by applying the patch
   * @param diffSegmentLength the value to supply as diffSegmentLength
   * @param copySegmentLength the value to supply as copySegmentLength
   * @param offsetToNextInput the value to supply as offsetToNextInput
   * @param addends a byte array of addends; all are written, ignoring |diffSegmentLength|.
   * @return the bytes constituting the patch
   * @throws IOException
   */
  private static InputStream makePatch(
      String signature,
      long newLength,
      long diffSegmentLength,
      long copySegmentLength,
      long offsetToNextInput,
      byte[] addends)
      throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write(signature.getBytes("US-ASCII"));
    writeBsdiffLong(newLength, out);
    writeBsdiffLong(diffSegmentLength, out);
    writeBsdiffLong(copySegmentLength, out);
    writeBsdiffLong(offsetToNextInput, out);
    out.write(addends);
    return new ByteArrayInputStream(out.toByteArray());
  }

  // Copied from com.google.archivepatcher.generator.bsdiff.BsUtil for convenience.
  private static void writeBsdiffLong(final long value, OutputStream out) throws IOException {
    long y = value;
    if (y < 0) {
      y = (-y) | (1L << 63);
    }
    for (int i = 0; i < 8; ++i) {
      out.write((byte) (y & 0xff));
      y >>>= 8;
    }
  }
}