• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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 static org.junit.Assert.assertTrue;
18 
19 import com.google.archivepatcher.shared.DeflateUncompressor;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.util.zip.Deflater;
26 import java.util.zip.DeflaterOutputStream;
27 import java.util.zip.Inflater;
28 
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 /**
36  * Tests for {@link DeflateUncompressor}.
37  */
38 @RunWith(JUnit4.class)
39 @SuppressWarnings("javadoc")
40 public class DeflateUncompressorTest {
41 
42   /**
43    * Test data for compression. Uses the {@link DefaultDeflateCompatibilityWindow}'s corpus because
44    * it is already set up to produce different outputs for each compression level.
45    */
46   private final static byte[] CONTENT = new DefaultDeflateCompatibilityWindow().getCorpus();
47 
48   private byte[] compressedContent;
49   private ByteArrayInputStream compressedContentIn;
50   private DeflateUncompressor uncompressor;
51   private ByteArrayOutputStream uncompressedContentOut;
52 
53   @Before
setUp()54   public void setUp() throws IOException {
55     ByteArrayOutputStream compressedContentBuffer = new ByteArrayOutputStream();
56     Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
57     DeflaterOutputStream deflateOut = new DeflaterOutputStream(compressedContentBuffer, deflater);
58     deflateOut.write(CONTENT);
59     deflateOut.finish();
60     deflateOut.close();
61     deflater.end();
62     compressedContent = compressedContentBuffer.toByteArray();
63     uncompressor = new DeflateUncompressor();
64     compressedContentIn = new ByteArrayInputStream(compressedContent);
65     uncompressedContentOut = new ByteArrayOutputStream();
66   }
67 
68   @Test
testUncompress()69   public void testUncompress() throws IOException {
70     uncompressor.uncompress(compressedContentIn, uncompressedContentOut);
71     assertTrue(Arrays.equals(CONTENT, uncompressedContentOut.toByteArray()));
72   }
73 
74   @Test
testCorrectDefaults()75   public void testCorrectDefaults() {
76     // Sanity check to ensure that defaults are as we want them to be. Arguably crufty but nobody
77     // should change these without some thought, particularly the wrapping choice should not be
78     // changed in the compressor without also changing it in the *un*compressor.
79     Assert.assertTrue(uncompressor.isNowrap());
80   }
81 
82   @Test
testNowrap()83   public void testNowrap() throws IOException {
84     // Recompress with nowrap set to false.
85     Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, false /* nowrap */);
86     ByteArrayOutputStream compressedContentBuffer = new ByteArrayOutputStream();
87     DeflaterOutputStream deflateOut = new DeflaterOutputStream(compressedContentBuffer, deflater);
88     deflateOut.write(CONTENT);
89     deflateOut.finish();
90     deflateOut.close();
91     deflater.end();
92     compressedContent = compressedContentBuffer.toByteArray();
93     compressedContentIn = new ByteArrayInputStream(compressedContent);
94 
95     // Now expect wrapped content in the uncompressor, and uncompressing should "just work".
96     uncompressor.setNowrap(false);
97     uncompressor.uncompress(compressedContentIn, uncompressedContentOut);
98     assertTrue(Arrays.equals(CONTENT, uncompressedContentOut.toByteArray()));
99   }
100 
101   @Test
testSetInputBufferSize()102   public void testSetInputBufferSize() throws IOException {
103     Assert.assertNotEquals(17, uncompressor.getInputBufferSize()); // Ensure test is valid
104     uncompressor.setInputBufferSize(17); // Arbitrary non-default value
105     Assert.assertEquals(17, uncompressor.getInputBufferSize());
106     uncompressor.uncompress(compressedContentIn, uncompressedContentOut);
107     Assert.assertArrayEquals(CONTENT, uncompressedContentOut.toByteArray());
108   }
109 
110   @Test
testSetOutputBufferSize()111   public void testSetOutputBufferSize() throws IOException {
112     Assert.assertNotEquals(17, uncompressor.getOutputBufferSize()); // Ensure test is valid
113     uncompressor.setOutputBufferSize(17); // Arbitrary non-default value
114     Assert.assertEquals(17, uncompressor.getOutputBufferSize());
115     uncompressor.uncompress(compressedContentIn, uncompressedContentOut);
116     Assert.assertArrayEquals(CONTENT, uncompressedContentOut.toByteArray());
117   }
118 
119   @Test
testCreateOrResetInflater_Uncached()120   public void testCreateOrResetInflater_Uncached() {
121     uncompressor.setCaching(false);
122     Inflater inflater1 = uncompressor.createOrResetInflater();
123     Inflater inflater2 = uncompressor.createOrResetInflater();
124     Assert.assertNotSame(inflater1, inflater2);
125   }
126 
127   @Test
testCreateOrResetInflater_Cached()128   public void testCreateOrResetInflater_Cached() {
129     uncompressor.setCaching(true);
130     Inflater inflater1 = uncompressor.createOrResetInflater();
131     Inflater inflater2 = uncompressor.createOrResetInflater();
132     Assert.assertSame(inflater1, inflater2);
133   }
134 
135   @Test
testRelease()136   public void testRelease() {
137     uncompressor.setCaching(true);
138     Inflater inflater1 = uncompressor.createOrResetInflater();
139     uncompressor.release();
140     Inflater inflater2 = uncompressor.createOrResetInflater();
141     Assert.assertNotSame(inflater1, inflater2);
142   }
143 
144   @Test
testReusability()145   public void testReusability() throws IOException {
146     // Checks that the uncompressor produces correct output when cached, i.e. that it is being
147     // properly reset between runs.
148     uncompressor.setCaching(true);
149     uncompressor.uncompress(compressedContentIn, uncompressedContentOut);
150     Assert.assertArrayEquals(CONTENT, uncompressedContentOut.toByteArray());
151 
152     // Caching is on, try to reuse it without any changes.
153     compressedContentIn = new ByteArrayInputStream(compressedContent);
154     uncompressedContentOut = new ByteArrayOutputStream();
155     uncompressor.uncompress(compressedContentIn, uncompressedContentOut);
156     Assert.assertArrayEquals(CONTENT, uncompressedContentOut.toByteArray());
157 
158     // Caching is still on, reverse the wrapping style and try again. Changing the wrapping style
159     // invalidates the cached uncompressor because the wrapping style cannot be changed, this is a
160     // special code path we need to exercise.
161     Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, false /* nowrap */);
162     ByteArrayOutputStream compressedContentBuffer = new ByteArrayOutputStream();
163     DeflaterOutputStream deflateOut = new DeflaterOutputStream(compressedContentBuffer, deflater);
164     deflateOut.write(CONTENT);
165     deflateOut.finish();
166     deflateOut.close();
167     deflater.end();
168     compressedContent = compressedContentBuffer.toByteArray();
169     compressedContentIn = new ByteArrayInputStream(compressedContent);
170 
171     // Now expect wrapped content in the uncompressor, and uncompressing should "just work".
172     uncompressor.setNowrap(false);
173     uncompressedContentOut = new ByteArrayOutputStream();
174     uncompressor.uncompress(compressedContentIn, uncompressedContentOut);
175     assertTrue(Arrays.equals(CONTENT, uncompressedContentOut.toByteArray()));
176   }
177 }
178