Home | History | Annotate | Download | only in shared
      1 // Copyright 2016 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package com.google.archivepatcher.shared;
     16 
     17 import com.google.archivepatcher.shared.PartiallyUncompressingPipe.Mode;
     18 
     19 import org.junit.Assert;
     20 import org.junit.Before;
     21 import org.junit.Test;
     22 import org.junit.runner.RunWith;
     23 import org.junit.runners.JUnit4;
     24 
     25 import java.io.ByteArrayInputStream;
     26 import java.io.ByteArrayOutputStream;
     27 import java.io.IOException;
     28 
     29 /**
     30  * Tests for {@link PartiallyUncompressingPipe}.
     31  */
     32 @RunWith(JUnit4.class)
     33 @SuppressWarnings("javadoc")
     34 public class PartiallyUncompressingPipeTest {
     35   private ByteArrayOutputStream outBuffer;
     36   private PartiallyUncompressingPipe stream;
     37 
     38   @Before
     39   public void setup() {
     40     outBuffer = new ByteArrayOutputStream();
     41     stream = new PartiallyUncompressingPipe(outBuffer, 32768);
     42   }
     43 
     44   @Test
     45   public void testWriteAll_Uncompressed() throws IOException {
     46     byte[] expectedBytes = new byte[] {1, 2, 3, 4, 5};
     47     stream.pipe(new ByteArrayInputStream(expectedBytes), Mode.COPY);
     48     Assert.assertArrayEquals(expectedBytes, outBuffer.toByteArray());
     49   }
     50 
     51   @Test
     52   public void testWriteAll_Compressed_NoWrapTrue() throws IOException {
     53     UnitTestZipEntry entry = UnitTestZipArchive.makeUnitTestZipEntry("/foo", 7, "frobozz", null);
     54     stream.pipe(
     55         new ByteArrayInputStream(entry.getCompressedBinaryContent()), Mode.UNCOMPRESS_NOWRAP);
     56     Assert.assertArrayEquals(entry.getUncompressedBinaryContent(), outBuffer.toByteArray());
     57   }
     58 
     59   @Test
     60   public void testWriteAll_Compressed_NoWrapFalse() throws IOException {
     61     UnitTestZipEntry entry = UnitTestZipArchive.makeUnitTestZipEntry("/foo", 6, "frobozz", null);
     62 
     63     // Make a compressor with nowrap set to *false* (unusual) and pump the uncompressed entry
     64     // content through it.
     65     DeflateCompressor compressor = new DeflateCompressor();
     66     compressor.setNowrap(false);
     67     ByteArrayOutputStream compressBuffer = new ByteArrayOutputStream();
     68     compressor.compress(
     69         new ByteArrayInputStream(entry.getUncompressedBinaryContent()), compressBuffer);
     70 
     71     // Now use the compressed data as input to the PartiallyUncompressingPipe.
     72     stream.pipe(new ByteArrayInputStream(compressBuffer.toByteArray()), Mode.UNCOMPRESS_WRAPPED);
     73     Assert.assertArrayEquals(entry.getUncompressedBinaryContent(), outBuffer.toByteArray());
     74   }
     75 
     76   @Test
     77   public void testWriteAll_Multiple() throws IOException {
     78     // A series of uncompressed, compressed, uncompressed, compressed, uncompressed bytes.
     79     UnitTestZipEntry entryA =
     80         UnitTestZipArchive.makeUnitTestZipEntry("/bar", 3, "dragon lance", null);
     81     UnitTestZipEntry entryB =
     82         UnitTestZipArchive.makeUnitTestZipEntry("/baz", 8, "kender & hoopak", null);
     83     ByteArrayOutputStream expected = new ByteArrayOutputStream();
     84 
     85     // Write everything
     86     byte[] expectedBytes1 = new byte[] {1, 2, 3, 4, 5};
     87     expected.write(expectedBytes1);
     88     stream.pipe(new ByteArrayInputStream(expectedBytes1), Mode.COPY);
     89 
     90     stream.pipe(
     91         new ByteArrayInputStream(entryA.getCompressedBinaryContent()), Mode.UNCOMPRESS_NOWRAP);
     92     expected.write(entryA.getUncompressedBinaryContent());
     93 
     94     byte[] expectedBytes3 = new byte[] {6, 7, 8, 9, 0};
     95     stream.pipe(new ByteArrayInputStream(expectedBytes3), Mode.COPY);
     96     expected.write(expectedBytes3);
     97 
     98     stream.pipe(
     99         new ByteArrayInputStream(entryB.getCompressedBinaryContent()), Mode.UNCOMPRESS_NOWRAP);
    100     expected.write(entryB.getUncompressedBinaryContent());
    101 
    102     byte[] expectedBytes5 = new byte[] {127, 127, 127, 127, 127, 127};
    103     stream.pipe(new ByteArrayInputStream(expectedBytes5), Mode.COPY);
    104     expected.write(expectedBytes5);
    105 
    106     Assert.assertArrayEquals(expected.toByteArray(), outBuffer.toByteArray());
    107   }
    108 }
    109