Home | History | Annotate | Download | only in zip
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 package org.apache.harmony.archive.tests.java.util.zip;
     18 
     19 import java.io.ByteArrayInputStream;
     20 import java.io.ByteArrayOutputStream;
     21 import java.io.EOFException;
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 import java.io.File;
     25 import java.io.FileInputStream;
     26 import java.util.zip.DeflaterOutputStream;
     27 import java.util.zip.Inflater;
     28 import java.util.zip.InflaterInputStream;
     29 
     30 import junit.framework.TestCase;
     31 import tests.support.resource.Support_Resources;
     32 
     33 public class InflaterInputStreamTest extends TestCase {
     34 
     35 	// files hyts_constru(O),hyts_constru(OD),hyts_constru(ODI) needs to be
     36 	// included as resources
     37         // android-changed: we removed the parentheses for the benefit of our broken build system.
     38 	byte outPutBuf[] = new byte[500];
     39 
     40 	class MyInflaterInputStream extends InflaterInputStream {
     41 		MyInflaterInputStream(InputStream in) {
     42 			super(in);
     43 		}
     44 
     45 		MyInflaterInputStream(InputStream in, Inflater infl) {
     46 			super(in, infl);
     47 		}
     48 
     49 		MyInflaterInputStream(InputStream in, Inflater infl, int size) {
     50 			super(in, infl, size);
     51 		}
     52 
     53 		void myFill() throws IOException {
     54 			fill();
     55 		}
     56 	}
     57 
     58 	/**
     59 	 * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream)
     60 	 */
     61 	public void test_ConstructorLjava_io_InputStream() throws IOException {
     62 	    //FIXME This test doesn't pass in Harmony classlib or Sun 5.0_7 RI
     63         /*
     64 		int result = 0;
     65 		int buffer[] = new int[500];
     66 		InputStream infile = Support_Resources
     67 				.getStream("hyts_constru_O.txt"); // android-changed
     68 
     69 		InflaterInputStream inflatIP = new InflaterInputStream(infile);
     70 
     71 		int i = 0;
     72 		while ((result = inflatIP.read()) != -1) {
     73 			buffer[i] = result;
     74 			i++;
     75 		}
     76 		inflatIP.close();
     77         */
     78 	}
     79 
     80 	/**
     81 	 * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream,
     82 	 *        java.util.zip.Inflater)
     83 	 */
     84 	public void test_ConstructorLjava_io_InputStreamLjava_util_zip_Inflater() throws IOException {
     85 		byte byteArray[] = new byte[100];
     86 		InputStream infile = Support_Resources.getStream("hyts_constru_OD.txt"); // android-changed
     87 		Inflater inflate = new Inflater();
     88 		InflaterInputStream inflatIP = new InflaterInputStream(infile,
     89 				inflate);
     90 
     91 		inflatIP.read(byteArray, 0, 5);// only suppose to read in 5 bytes
     92 		inflatIP.close();
     93 	}
     94 
     95 	/**
     96 	 * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream,
     97 	 *        java.util.zip.Inflater, int)
     98 	 */
     99 	public void test_ConstructorLjava_io_InputStreamLjava_util_zip_InflaterI() throws IOException {
    100 		int result = 0;
    101 		int buffer[] = new int[500];
    102 		InputStream infile = Support_Resources.getStream("hyts_constru_ODI.txt"); // android-changed
    103 		Inflater inflate = new Inflater();
    104 		InflaterInputStream inflatIP = new InflaterInputStream(infile,
    105 				inflate, 1);
    106 
    107 		int i = 0;
    108 		while ((result = inflatIP.read()) != -1) {
    109 			buffer[i] = result;
    110 			i++;
    111 		}
    112 		inflatIP.close();
    113 	}
    114 
    115     /**
    116      * @tests java.util.zip.InflaterInputStream#mark(int)
    117      */
    118     public void test_markI() {
    119         InputStream is = new ByteArrayInputStream(new byte[10]);
    120         InflaterInputStream iis = new InflaterInputStream(is);
    121         // mark do nothing, do no check
    122         iis.mark(0);
    123         iis.mark(-1);
    124         iis.mark(10000000);
    125     }
    126 
    127     /**
    128      * @tests java.util.zip.InflaterInputStream#markSupported()
    129      */
    130     public void test_markSupported() {
    131         InputStream is = new ByteArrayInputStream(new byte[10]);
    132         InflaterInputStream iis = new InflaterInputStream(is);
    133         assertFalse(iis.markSupported());
    134         assertTrue(is.markSupported());
    135     }
    136 
    137 	/**
    138      * @tests java.util.zip.InflaterInputStream#read()
    139      */
    140 	public void test_read() throws IOException {
    141 		int result = 0;
    142 		int buffer[] = new int[500];
    143 		byte orgBuffer[] = { 1, 3, 4, 7, 8 };
    144 		InputStream infile = Support_Resources
    145 				.getStream("hyts_constru_OD.txt"); // android-changed
    146 		Inflater inflate = new Inflater();
    147 		InflaterInputStream inflatIP = new InflaterInputStream(infile,
    148 				inflate);
    149 
    150 		int i = 0;
    151 		while ((result = inflatIP.read()) != -1) {
    152 			buffer[i] = result;
    153 			i++;
    154 		}
    155 		inflatIP.close();
    156 
    157 		for (int j = 0; j < orgBuffer.length; j++) {
    158 			assertTrue(
    159 				"original compressed data did not equal decompressed data",
    160 				buffer[j] == orgBuffer[j]);
    161 		}
    162 	}
    163 
    164     public void testAvailableNonEmptySource() throws Exception {
    165         // this byte[] is a deflation of these bytes: { 1, 3, 4, 6 }
    166         byte[] deflated = { 72, -119, 99, 100, 102, 97, 3, 0, 0, 31, 0, 15, 0 };
    167         InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
    168         // InflaterInputStream.available() returns either 1 or 0, even though
    169         // that contradicts the behavior defined in InputStream.available()
    170         assertEquals(1, in.read());
    171         assertEquals(1, in.available());
    172         assertEquals(3, in.read());
    173         assertEquals(1, in.available());
    174         assertEquals(4, in.read());
    175         assertEquals(1, in.available());
    176         assertEquals(6, in.read());
    177         assertEquals(0, in.available());
    178         assertEquals(-1, in.read());
    179         assertEquals(-1, in.read());
    180     }
    181 
    182     public void testAvailableSkip() throws Exception {
    183         // this byte[] is a deflation of these bytes: { 1, 3, 4, 6 }
    184         byte[] deflated = { 72, -119, 99, 100, 102, 97, 3, 0, 0, 31, 0, 15, 0 };
    185         InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
    186         assertEquals(1, in.available());
    187         assertEquals(4, in.skip(4));
    188         assertEquals(0, in.available());
    189     }
    190 
    191     public void testAvailableEmptySource() throws Exception {
    192         // this byte[] is a deflation of the empty file
    193         byte[] deflated = { 120, -100, 3, 0, 0, 0, 0, 1 };
    194         InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
    195         assertEquals(-1, in.read());
    196         assertEquals(-1, in.read());
    197         assertEquals(0, in.available());
    198     }
    199 
    200 	/**
    201 	 * @tests java.util.zip.InflaterInputStream#read(byte[], int, int)
    202 	 */
    203 	public void test_read$BII() throws IOException{
    204         byte[] test = new byte[507];
    205         for (int i = 0; i < 256; i++) {
    206             test[i] = (byte) i;
    207         }
    208         for (int i = 256; i < test.length; i++) {
    209             test[i] = (byte) (256 - i);
    210         }
    211         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    212         DeflaterOutputStream dos = new DeflaterOutputStream(baos);
    213         dos.write(test);
    214         dos.close();
    215         InputStream is = new ByteArrayInputStream(baos.toByteArray());
    216         InflaterInputStream iis = new InflaterInputStream(is);
    217         byte[] outBuf = new byte[530];
    218         int result = 0;
    219         while (true) {
    220             result = iis.read(outBuf, 0, 5);
    221             if (result == -1) {
    222                 //"EOF was reached";
    223                 break;
    224             }
    225         }
    226         try {
    227             iis.read(outBuf, -1, 10);
    228             fail("should throw IOOBE.");
    229         } catch (IndexOutOfBoundsException e) {
    230             // expected;
    231         }
    232 	}
    233 
    234     public void test_read$BII2() throws IOException {
    235         File resources = Support_Resources.createTempFolder();
    236         Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
    237         FileInputStream fis = new FileInputStream(new File(resources,
    238                 "Broken_manifest.jar"));
    239         InflaterInputStream iis = new InflaterInputStream(fis);
    240         byte[] outBuf = new byte[530];
    241 
    242         iis.close();
    243         try {
    244             iis.read(outBuf, 0, 5);
    245             fail("IOException expected");
    246         } catch (IOException ee) {
    247             // expected.
    248         }
    249     }
    250 
    251     public void test_read$BII3() throws IOException {
    252         File resources = Support_Resources.createTempFolder();
    253         Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
    254         FileInputStream fis = new FileInputStream(new File(resources,
    255                 "Broken_manifest.jar"));
    256         InflaterInputStream iis = new InflaterInputStream(fis);
    257         byte[] outBuf = new byte[530];
    258 
    259         try {
    260             iis.read();
    261             fail("IOException expected.");
    262         } catch (IOException ee) {
    263             // expected
    264         }
    265     }
    266 
    267     /**
    268      * @tests java.util.zip.InflaterInputStream#reset()
    269      */
    270     public void test_reset() {
    271         InputStream is = new ByteArrayInputStream(new byte[10]);
    272         InflaterInputStream iis = new InflaterInputStream(is);
    273         try {
    274             iis.reset();
    275             fail("Should throw IOException");
    276         } catch (IOException e) {
    277             // correct
    278         }
    279     }
    280 
    281 	/**
    282 	 * @tests java.util.zip.InflaterInputStream#skip(long)
    283 	 */
    284 	public void test_skipJ() throws IOException {
    285 		InputStream is = Support_Resources.getStream("hyts_available.tst");
    286 		InflaterInputStream iis = new InflaterInputStream(is);
    287 
    288 		// Tests for skipping a negative number of bytes.
    289 		try {
    290 			iis.skip(-3);
    291 			fail("IllegalArgumentException not thrown");
    292 		} catch (IllegalArgumentException e) {
    293             // Expected
    294 		}
    295 		assertEquals("Incorrect Byte Returned.", 5, iis.read());
    296 
    297 		try {
    298 			iis.skip(Integer.MIN_VALUE);
    299 			fail("IllegalArgumentException not thrown");
    300 		} catch (IllegalArgumentException e) {
    301             // Expected
    302 		}
    303 		assertEquals("Incorrect Byte Returned.", 4, iis.read());
    304 
    305 		// Test to make sure the correct number of bytes were skipped
    306 		assertEquals("Incorrect Number Of Bytes Skipped.", 3, iis.skip(3));
    307 
    308 		// Test to see if the number of bytes skipped returned is true.
    309 		assertEquals("Incorrect Byte Returned.", 7, iis.read());
    310 
    311 		assertEquals("Incorrect Number Of Bytes Skipped.", 0, iis.skip(0));
    312 		assertEquals("Incorrect Byte Returned.", 0, iis.read());
    313 
    314 		// Test for skipping more bytes than available in the stream
    315 		assertEquals("Incorrect Number Of Bytes Skipped.", 2, iis.skip(4));
    316 		assertEquals("Incorrect Byte Returned.", -1, iis.read());
    317 		iis.close();
    318 	}
    319 
    320 	/**
    321 	 * @tests java.util.zip.InflaterInputStream#skip(long)
    322 	 */
    323 	public void test_skipJ2() throws IOException {
    324 		int result = 0;
    325 		int buffer[] = new int[100];
    326 		byte orgBuffer[] = { 1, 3, 4, 7, 8 };
    327 
    328         // testing for negative input to skip
    329 		InputStream infile = Support_Resources
    330 				.getStream("hyts_constru_OD.txt"); // android-changed
    331 		Inflater inflate = new Inflater();
    332 		InflaterInputStream inflatIP = new InflaterInputStream(infile,
    333 				inflate, 10);
    334 		long skip;
    335 		try {
    336 			skip = inflatIP.skip(Integer.MIN_VALUE);
    337 			fail("Expected IllegalArgumentException when skip() is called with negative parameter");
    338 		} catch (IllegalArgumentException e) {
    339             // Expected
    340 		}
    341 		inflatIP.close();
    342 
    343 		// testing for number of bytes greater than input.
    344 		InputStream infile2 = Support_Resources
    345 				.getStream("hyts_constru_OD.txt"); // android-changed
    346 		InflaterInputStream inflatIP2 = new InflaterInputStream(infile2);
    347 
    348 		// looked at how many bytes the skip skipped. It is
    349 		// 5 and its supposed to be the entire input stream.
    350 
    351 		skip = inflatIP2.skip(Integer.MAX_VALUE);
    352 		// System.out.println(skip);
    353 		assertEquals("method skip() returned wrong number of bytes skipped",
    354 				5, skip);
    355 
    356 		// test for skipping of 2 bytes
    357 		InputStream infile3 = Support_Resources
    358 				.getStream("hyts_constru_OD.txt"); // android-changed
    359 		InflaterInputStream inflatIP3 = new InflaterInputStream(infile3);
    360 		skip = inflatIP3.skip(2);
    361 		assertEquals("the number of bytes returned by skip did not correspond with its input parameters",
    362 				2, skip);
    363 		int i = 0;
    364 		result = 0;
    365 		while ((result = inflatIP3.read()) != -1) {
    366 			buffer[i] = result;
    367 			i++;
    368 		}
    369 		inflatIP2.close();
    370 
    371 		for (int j = 2; j < orgBuffer.length; j++) {
    372 			assertTrue(
    373 				"original compressed data did not equal decompressed data",
    374 				buffer[j - 2] == orgBuffer[j]);
    375 		}
    376 	}
    377 
    378 	/**
    379 	 * @tests java.util.zip.InflaterInputStream#available()
    380 	 */
    381 	public void test_available() throws IOException {
    382 		InputStream is = Support_Resources.getStream("hyts_available.tst");
    383 		InflaterInputStream iis = new InflaterInputStream(is);
    384 
    385         int available;
    386         for (int i = 0; i < 11; i++) {
    387             iis.read();
    388             available = iis.available();
    389             if (available == 0) {
    390                 assertEquals("Expected no more bytes to read", -1, iis.read());
    391             } else {
    392                 assertEquals("Bytes Available Should Return 1.", 1, available);
    393             }
    394         }
    395 
    396 		iis.close();
    397 		try {
    398 			iis.available();
    399 			fail("available after close should throw IOException.");
    400 		} catch (IOException e) {
    401             // Expected
    402 		}
    403 	}
    404 
    405 	/**
    406 	 * @tests java.util.zip.InflaterInputStream#close()
    407 	 */
    408 	public void test_close() throws IOException {
    409 		InflaterInputStream iin = new InflaterInputStream(
    410 				new ByteArrayInputStream(new byte[0]));
    411 		iin.close();
    412 
    413         // test for exception
    414 		iin.close();
    415 	}
    416 }
    417