1 /* 2 * Copyright (C) 2010 The Android Open Source Project 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 17 package libcore.java.util.zip; 18 19 import java.io.ByteArrayOutputStream; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.OutputStream; 23 import java.util.Arrays; 24 import java.util.Random; 25 import java.util.zip.GZIPOutputStream; 26 import libcore.junit.junit3.TestCaseWithRules; 27 import libcore.junit.util.ResourceLeakageDetector; 28 import org.junit.Rule; 29 import org.junit.rules.TestRule; 30 31 public final class GZIPOutputStreamTest extends TestCaseWithRules { 32 @Rule 33 public TestRule resourceLeakageDetectorRule = ResourceLeakageDetector.getRule(); 34 35 public void testShortMessage() throws IOException { 36 byte[] data = gzip(("Hello World").getBytes("UTF-8")); 37 assertEquals("[31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, 72, -51, -55, -55, 87, 8, -49, " + 38 "47, -54, 73, 1, 0, 86, -79, 23, 74, 11, 0, 0, 0]", Arrays.toString(data)); 39 } 40 41 public void testLongMessage() throws IOException { 42 byte[] data = new byte[1024 * 1024]; 43 new Random().nextBytes(data); 44 assertTrue(Arrays.equals(data, GZIPInputStreamTest.gunzip(gzip(data)))); 45 } 46 47 public static byte[] gzip(byte[] bytes) throws IOException { 48 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 49 OutputStream gzippedOut = new GZIPOutputStream(bytesOut); 50 gzippedOut.write(bytes); 51 gzippedOut.close(); 52 return bytesOut.toByteArray(); 53 } 54 55 public void testSyncFlushEnabled() throws Exception { 56 InputStream in = DeflaterOutputStreamTest.createInflaterStream(GZIPOutputStream.class, true); 57 assertEquals(1, in.read()); 58 assertEquals(2, in.read()); 59 assertEquals(3, in.read()); 60 in.close(); 61 } 62 63 public void testSyncFlushDisabled() throws Exception { 64 InputStream in = DeflaterOutputStreamTest.createInflaterStream(GZIPOutputStream.class, false); 65 try { 66 in.read(); 67 fail(); 68 } catch (IOException expected) { 69 } 70 in.close(); 71 } 72 73 // https://code.google.com/p/android/issues/detail?id=62589 74 public void testFlushAfterFinish() throws Exception { 75 byte[] responseBytes = "Some data to gzip".getBytes(); 76 ByteArrayOutputStream output = new ByteArrayOutputStream(responseBytes.length); 77 GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output, true); 78 gzipOutputStream.write(responseBytes); 79 gzipOutputStream.finish(); 80 // Calling flush() after finish() shouldn't throw. 81 gzipOutputStream.flush(); 82 gzipOutputStream.close(); 83 } 84 } 85