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.BufferedInputStream;
     20 import java.io.ByteArrayOutputStream;
     21 import java.io.FileNotFoundException;
     22 import java.io.IOException;
     23 import java.io.UnsupportedEncodingException;
     24 import java.util.zip.Adler32;
     25 import java.util.zip.DataFormatException;
     26 import java.util.zip.Deflater;
     27 import java.util.zip.DeflaterOutputStream;
     28 import java.util.zip.Inflater;
     29 import java.util.zip.ZipException;
     30 
     31 import tests.support.resource.Support_Resources;
     32 
     33 public class InflaterTest extends junit.framework.TestCase {
     34 	byte outPutBuff1[] = new byte[500];
     35 
     36 	byte outPutDiction[] = new byte[500];
     37 
     38 	/**
     39 	 * @tests java.util.zip.Inflater#end()
     40 	 */
     41 	public void test_end() {
     42 		// test method of java.util.zip.inflater.end()
     43 		byte byteArray[] = { 5, 2, 3, 7, 8 };
     44 
     45 		int r = 0;
     46 		Inflater inflate = new Inflater();
     47 		inflate.setInput(byteArray);
     48 		inflate.end();
     49 		try {
     50 			inflate.reset();
     51 			inflate.setInput(byteArray);
     52 		} catch (NullPointerException e) {
     53 			r = 1;
     54 		}
     55 		assertEquals("inflate can still be used after end is called", 1, r);
     56 
     57 		Inflater i = new Inflater();
     58 		i.end();
     59 		// check for exception
     60 		i.end();
     61 	}
     62 
     63 	/**
     64 	 * @tests java.util.zip.Inflater#finished()
     65 	 */
     66 	public void test_finished() {
     67 		// test method of java.util.zip.inflater.finished()
     68 		byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
     69 		Inflater inflate = new Inflater(false);
     70 		byte outPutInf[] = new byte[500];
     71 		try {
     72 			while (!(inflate.finished())) {
     73 				if (inflate.needsInput()) {
     74 					inflate.setInput(outPutBuff1);
     75 				}
     76 
     77 				inflate.inflate(outPutInf);
     78 			}
     79 			assertTrue(
     80 					"the method finished() returned false when no more data needs to be decompressed",
     81 					inflate.finished());
     82 		} catch (DataFormatException e) {
     83 			fail("Invalid input to be decompressed");
     84 		}
     85 		for (int i = 0; i < byteArray.length; i++) {
     86 			assertEquals(
     87 					"Final decompressed data does not equal the original data",
     88 					outPutInf[i], byteArray[i]);
     89 		}
     90 		assertEquals("final decompressed data contained more bytes than original - finished()",
     91 				0, outPutInf[byteArray.length]);
     92 	}
     93 
     94 	/**
     95 	 * @tests java.util.zip.Inflater#getAdler()
     96 	 */
     97 	public void test_getAdler() {
     98 		// test method of java.util.zip.inflater.getAdler()
     99 		byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
    100 
    101 		Inflater inflateDiction = new Inflater();
    102 		inflateDiction.setInput(outPutDiction);
    103 		if (inflateDiction.needsDictionary() == true) {
    104 			// getting the checkSum value through the Adler32 class
    105 			Adler32 adl = new Adler32();
    106 			adl.update(dictionaryArray);
    107 			long checkSumR = adl.getValue();
    108 			assertEquals(
    109 					"the checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance",
    110 					inflateDiction.getAdler(), checkSumR);
    111 		}
    112 	}
    113 
    114 	/**
    115 	 * @tests java.util.zip.Inflater#getRemaining()
    116 	 */
    117 	public void test_getRemaining() {
    118 		// test method of java.util.zip.inflater.getRemaining()
    119 		byte byteArray[] = { 1, 3, 5, 6, 7 };
    120 		Inflater inflate = new Inflater();
    121 		assertEquals("upon creating an instance of inflate, getRemaining returned a non zero value",
    122 				0, inflate.getRemaining());
    123 		inflate.setInput(byteArray);
    124 		assertTrue(
    125 				"getRemaining returned zero when there is input in the input buffer",
    126 				inflate.getRemaining() != 0);
    127 	}
    128 
    129 	/**
    130 	 * @tests java.util.zip.Inflater#getTotalIn()
    131 	 */
    132 	public void test_getTotalIn() {
    133 		// test method of java.util.zip.inflater.getTotalIn()
    134 		// creating the decompressed data
    135 		byte outPutBuf[] = new byte[500];
    136 		byte byteArray[] = { 1, 3, 4, 7, 8 };
    137 		byte outPutInf[] = new byte[500];
    138 		int x = 0;
    139 		Deflater deflate = new Deflater(1);
    140 		deflate.setInput(byteArray);
    141 		while (!(deflate.needsInput())) {
    142 			x += deflate.deflate(outPutBuf, x, outPutBuf.length - x);
    143 		}
    144 		deflate.finish();
    145 		while (!(deflate.finished())) {
    146 			x = x + deflate.deflate(outPutBuf, x, outPutBuf.length - x);
    147 		}
    148 
    149 		Inflater inflate = new Inflater();
    150 		try {
    151 			while (!(inflate.finished())) {
    152 				if (inflate.needsInput()) {
    153 					inflate.setInput(outPutBuf);
    154 				}
    155 
    156 				inflate.inflate(outPutInf);
    157 			}
    158 		} catch (DataFormatException e) {
    159 			fail("Input to inflate is invalid or corrupted - getTotalIn");
    160 		}
    161 		// System.out.print(deflate.getTotalOut() + " " + inflate.getTotalIn());
    162 		assertEquals(
    163 				"the total byte in outPutBuf did not equal the byte returned in getTotalIn",
    164 				deflate.getTotalOut(), inflate.getTotalIn());
    165 
    166 		Inflater inflate2 = new Inflater();
    167 		int offSet = 0;// seems only can start as 0
    168 		int length = 4;
    169 		try {
    170 			// seems no while loops allowed
    171 			if (inflate2.needsInput()) {
    172 				inflate2.setInput(outPutBuff1, offSet, length);
    173 			}
    174 
    175 			inflate2.inflate(outPutInf);
    176 
    177 		} catch (DataFormatException e) {
    178 			fail("Input to inflate is invalid or corrupted - getTotalIn");
    179 		}
    180 		// System.out.print(inflate2.getTotalIn() + " " + length);
    181 		assertEquals(
    182 				"total byte dictated by length did not equal byte returned in getTotalIn",
    183 				length, inflate2.getTotalIn());
    184 	}
    185 
    186 	/**
    187 	 * @tests java.util.zip.Inflater#getTotalOut()
    188 	 */
    189 	public void test_getTotalOut() {
    190 		// test method of java.util.zip.inflater.Inflater()
    191 		// creating the decompressed data
    192 		byte outPutBuf[] = new byte[500];
    193 		byte byteArray[] = { 1, 3, 4, 7, 8 };
    194 		int y = 0;
    195 		int x = 0;
    196 		Deflater deflate = new Deflater(1);
    197 		deflate.setInput(byteArray);
    198 		while (!(deflate.needsInput())) {
    199 			x += deflate.deflate(outPutBuf, x, outPutBuf.length - x);
    200 		}
    201 		deflate.finish();
    202 		while (!(deflate.finished())) {
    203 			x = x + deflate.deflate(outPutBuf, x, outPutBuf.length - x);
    204 		}
    205 
    206 		Inflater inflate = new Inflater();
    207 		byte outPutInf[] = new byte[500];
    208 		try {
    209 			while (!(inflate.finished())) {
    210 				if (inflate.needsInput()) {
    211 					inflate.setInput(outPutBuf);
    212 				}
    213 
    214 				y += inflate.inflate(outPutInf);
    215 			}
    216 		} catch (DataFormatException e) {
    217 			fail("Input to inflate is invalid or corrupted - getTotalIn");
    218 		}
    219 
    220 		assertEquals(
    221 				"the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
    222 				inflate.getTotalOut(), y);
    223 		assertEquals(
    224 				"the total number of bytes to be compressed does not equal the total bytes decompressed",
    225 				deflate.getTotalIn(), inflate.getTotalOut());
    226 
    227 		// testing inflate(byte,int,int)
    228 		inflate.reset();
    229 		y = 0;
    230 		int offSet = 0;// seems only can start as 0
    231 		int length = 4;
    232 		try {
    233 			while (!(inflate.finished())) {
    234 				if (inflate.needsInput()) {
    235 					inflate.setInput(outPutBuf);
    236 				}
    237 
    238 				y += inflate.inflate(outPutInf, offSet, length);
    239 			}
    240 		} catch (DataFormatException e) {
    241 			System.out
    242 					.println("Input to inflate is invalid or corrupted - getTotalIn");
    243 		}
    244 		assertEquals(
    245 				"the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
    246 				y, inflate.getTotalOut());
    247 		assertEquals(
    248 				"the total number of bytes to be compressed does not equal the total bytes decompressed",
    249 				deflate.getTotalIn(), inflate.getTotalOut());
    250 	}
    251 
    252 	/**
    253 	 * @tests java.util.zip.Inflater#inflate(byte[])
    254 	 */
    255 	public void test_inflate$B() {
    256 		// test method of java.util.zip.inflater.inflate(byte)
    257 
    258 		byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
    259 		byte outPutInf[] = new byte[500];
    260 		Inflater inflate = new Inflater();
    261 		try {
    262 			while (!(inflate.finished())) {
    263 				if (inflate.needsInput()) {
    264 					inflate.setInput(outPutBuff1);
    265 				}
    266 				inflate.inflate(outPutInf);
    267 			}
    268 		} catch (DataFormatException e) {
    269 			fail("Invalid input to be decompressed");
    270 		}
    271 		for (int i = 0; i < byteArray.length; i++) {
    272 			assertEquals(
    273 					"Final decompressed data does not equal the original data",
    274 					byteArray[i], outPutInf[i]);
    275 		}
    276 		assertEquals("final decompressed data contained more bytes than original - inflateB",
    277 				0, outPutInf[byteArray.length]);
    278 		// testing for an empty input array
    279 		byte outPutBuf[] = new byte[500];
    280 		byte emptyArray[] = new byte[11];
    281 		int x = 0;
    282 		Deflater defEmpty = new Deflater(3);
    283 		defEmpty.setInput(emptyArray);
    284 		while (!(defEmpty.needsInput())) {
    285 			x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
    286 		}
    287 		defEmpty.finish();
    288 		while (!(defEmpty.finished())) {
    289 			x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
    290 		}
    291 		assertEquals(
    292 				"the total number of byte from deflate did not equal getTotalOut - inflate(byte)",
    293 				x, defEmpty.getTotalOut());
    294 		assertEquals(
    295 				"the number of input byte from the array did not correspond with getTotalIn - inflate(byte)",
    296 				emptyArray.length, defEmpty.getTotalIn());
    297 		Inflater infEmpty = new Inflater();
    298 		try {
    299 			while (!(infEmpty.finished())) {
    300 				if (infEmpty.needsInput()) {
    301 					infEmpty.setInput(outPutBuf);
    302 				}
    303 				infEmpty.inflate(outPutInf);
    304 			}
    305 		} catch (DataFormatException e) {
    306 			fail("Invalid input to be decompressed");
    307 		}
    308 		for (int i = 0; i < emptyArray.length; i++) {
    309 			assertEquals(
    310 					"Final decompressed data does not equal the original data",
    311 					emptyArray[i], outPutInf[i]);
    312 			assertEquals("Final decompressed data does not equal zero",
    313 					0, outPutInf[i]);
    314 		}
    315 		assertEquals("Final decompressed data contains more element than original data",
    316 				0, outPutInf[emptyArray.length]);
    317 	}
    318 
    319     public void test_inflate$B1() {
    320         byte codedData[] = {
    321                 120, -38, 75, -54, 73, -52, 80, 40, 46, 41, -54, -52, 75, 87,
    322                 72, -50, -49, 43, 73, -52, -52, 43, 86, 72, 2, 10, 34, 99,
    323                 -123, -60, -68, 20, -80, 32, 0, -101, -69, 17, 84};
    324         String codedString = "blah string contains blahblahblahblah and blah";
    325 
    326         Inflater infl1 = new Inflater();
    327         Inflater infl2 = new Inflater();
    328 
    329         byte[] result = new byte[100];
    330         int decLen = 0;
    331 
    332         infl1.setInput(codedData, 0, codedData.length);
    333         try {
    334             decLen = infl1.inflate(result);
    335         } catch (DataFormatException e) {
    336             fail("Unexpected DataFormatException");
    337         }
    338 
    339         infl1.end();
    340         assertEquals(codedString, new String(result, 0, decLen));
    341         codedData[5] = 0;
    342 
    343         infl2.setInput(codedData, 0, codedData.length);
    344         try {
    345             decLen = infl2.inflate(result);
    346             fail("Expected DataFormatException");
    347         } catch (DataFormatException e) {
    348             // expected
    349         }
    350 
    351         infl2.end();
    352     }
    353 
    354 	/**
    355 	 * @tests java.util.zip.Inflater#inflate(byte[], int, int)
    356 	 */
    357 	public void test_inflate$BII() {
    358 		// test method of java.util.zip.inflater.inflate(byte,int,int)
    359 
    360 		byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
    361 		byte outPutInf[] = new byte[100];
    362 		int y = 0;
    363 		Inflater inflate = new Inflater();
    364 		try {
    365 			while (!(inflate.finished())) {
    366 				if (inflate.needsInput()) {
    367 				    assertEquals(0, inflate.inflate(outPutInf, 0, 1));
    368 					inflate.setInput(outPutBuff1);
    369 				}
    370 				y += inflate.inflate(outPutInf, y, outPutInf.length - y);
    371 			}
    372 		} catch (DataFormatException e) {
    373 			fail("Invalid input to be decompressed");
    374 		}
    375 		for (int i = 0; i < byteArray.length; i++) {
    376 			assertEquals(
    377 					"Final decompressed data does not equal the original data",
    378 					byteArray[i], outPutInf[i]);
    379 		}
    380 		assertEquals("final decompressed data contained more bytes than original - inflateB",
    381 				0, outPutInf[byteArray.length]);
    382 
    383 		// test boundary checks
    384 		inflate.reset();
    385 		int r = 0;
    386 		int offSet = 0;
    387 		int lengthError = 101;
    388 		try {
    389 			if (inflate.needsInput()) {
    390 				inflate.setInput(outPutBuff1);
    391 			}
    392 			inflate.inflate(outPutInf, offSet, lengthError);
    393 
    394 		} catch (DataFormatException e) {
    395 			fail("Invalid input to be decompressed");
    396 		} catch (ArrayIndexOutOfBoundsException e) {
    397 			r = 1;
    398 		}
    399 		assertEquals("out of bounds error did not get caught", 1, r);
    400 
    401         try {
    402             assertEquals(0, inflate.inflate(outPutInf, offSet, 0));
    403         } catch (DataFormatException e) {
    404             fail("Invalid input to be decompressed");
    405         }
    406         inflate.end();
    407         try {
    408             inflate.inflate(outPutInf, offSet, 1);
    409             fail("IllegalStateException expected");
    410         } catch (DataFormatException e) {
    411             fail("Invalid input to be decompressed");
    412         } catch (IllegalStateException e) {
    413             //expected
    414         }
    415 	}
    416 
    417     public void test_inflate$BII1() {
    418         byte codedData[] = {
    419                 120, -38, 75, -54, 73, -52, 80, 40, 46, 41, -54, -52, 75, 87,
    420                 72, -50, -49, 43, 73, -52, -52, 43, 86, 72, 2, 10, 34, 99,
    421                 -123, -60, -68, 20, -80, 32, 0, -101, -69, 17, 84};
    422         String codedString = "blah string";
    423 
    424         Inflater infl1 = new Inflater();
    425         Inflater infl2 = new Inflater();
    426 
    427         byte[] result = new byte[100];
    428         int decLen = 0;
    429 
    430         infl1.setInput(codedData, 0, codedData.length);
    431         try {
    432             decLen = infl1.inflate(result, 10, 11);
    433         } catch (DataFormatException e) {
    434             fail("Unexpected DataFormatException");
    435         }
    436 
    437         infl1.end();
    438         assertEquals(codedString, new String(result, 10, decLen));
    439         codedData[5] = 0;
    440 
    441         infl2.setInput(codedData, 0, codedData.length);
    442         try {
    443             decLen = infl2.inflate(result, 10, 11);
    444             fail("Expected DataFormatException");
    445         } catch (DataFormatException e) {
    446             // expected
    447         }
    448 
    449         infl2.end();
    450     }
    451 
    452     /*
    453      * Regression test for HARMONY-6637
    454      */
    455     public void testInflateZero() throws Exception {
    456         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    457         DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(
    458                 byteArrayOutputStream);
    459         deflaterOutputStream.close();
    460         byte[] input = byteArrayOutputStream.toByteArray();
    461 
    462         Inflater inflater = new Inflater();
    463         inflater.setInput(input);
    464         byte[] buffer = new byte[0];
    465         int numRead = 0;
    466         while (!inflater.finished()) {
    467             int inflatedChunkSize = inflater.inflate(buffer, numRead,
    468                     buffer.length - numRead);
    469             numRead += inflatedChunkSize;
    470         }
    471         inflater.end();
    472     }
    473 
    474 	/**
    475 	 * @tests java.util.zip.Inflater#Inflater()
    476 	 */
    477 	public void test_Constructor() {
    478 		// test method of java.util.zip.inflater.Inflater()
    479 		Inflater inflate = new Inflater();
    480 		assertNotNull("failed to create the instance of inflater",
    481 				inflate);
    482 	}
    483 
    484 	/**
    485 	 * @tests java.util.zip.Inflater#Inflater(boolean)
    486 	 */
    487 	public void test_ConstructorZ() {
    488 		// test method of java.util.zip.inflater.Inflater(boolean)
    489 		// note does not throw exception if deflater has a header, but inflater
    490 		// doesn't or vice versa.
    491 		byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
    492 		Inflater inflate = new Inflater(true);
    493 		assertNotNull("failed to create the instance of inflater", inflate);
    494 		byte outPutInf[] = new byte[500];
    495 		int r = 0;
    496 		try {
    497 			while (!(inflate.finished())) {
    498 				if (inflate.needsInput()) {
    499 					inflate.setInput(outPutBuff1);
    500 				}
    501 
    502 				inflate.inflate(outPutInf);
    503 			}
    504 			for (int i = 0; i < byteArray.length; i++) {
    505 				assertEquals("the output array from inflate should contain 0 because the header of inflate and deflate did not match, but this failed",
    506 						0, outPutBuff1[i]);
    507 			}
    508 		} catch (DataFormatException e) {
    509 			r = 1;
    510 		}
    511 		assertEquals("Error: exception should be thrown because of header inconsistency",
    512 				1, r);
    513 
    514 	}
    515 
    516 	/**
    517 	 * @tests java.util.zip.Inflater#needsDictionary()
    518 	 */
    519 	public void test_needsDictionary() {
    520 		// test method of java.util.zip.inflater.needsDictionary()
    521 		// note: this flag is set after inflate is called
    522 		byte outPutInf[] = new byte[500];
    523 
    524 		// testing with dictionary set.
    525 		Inflater inflateDiction = new Inflater();
    526 		if (inflateDiction.needsInput()) {
    527 			inflateDiction.setInput(outPutDiction);
    528 		}
    529 		try {
    530 			assertEquals("should return 0 because needs dictionary",
    531 					0, inflateDiction.inflate(outPutInf));
    532 		} catch (DataFormatException e) {
    533 			fail("Should not cause exception");
    534 		}
    535 		assertTrue(
    536 				"method needsDictionary returned false when dictionary was used in deflater",
    537 				inflateDiction.needsDictionary());
    538 
    539 		// testing without dictionary
    540 		Inflater inflate = new Inflater();
    541 		try {
    542 			inflate.setInput(outPutBuff1);
    543 			inflate.inflate(outPutInf);
    544 			assertFalse(
    545 					"method needsDictionary returned true when dictionary was not used in deflater",
    546 					inflate.needsDictionary());
    547 		} catch (DataFormatException e) {
    548 			fail(
    549 					"Input to inflate is invalid or corrupted - needsDictionary");
    550 		}
    551 
    552         // Regression test for HARMONY-86
    553         Inflater inf = new Inflater();
    554         assertFalse(inf.needsDictionary());
    555         assertEquals(0,inf.getTotalIn());
    556         assertEquals(0,inf.getTotalOut());
    557         assertEquals(0,inf.getBytesRead());
    558         assertEquals(0,inf.getBytesWritten());
    559         assertEquals(1, inf.getAdler());
    560 	}
    561 
    562 	/**
    563 	 * @tests java.util.zip.Inflater#needsInput()
    564 	 */
    565 	public void test_needsInput() {
    566 		// test method of java.util.zip.inflater.needsInput()
    567 		Inflater inflate = new Inflater();
    568 		assertTrue(
    569 				"needsInput give the wrong boolean value as a result of no input buffer",
    570 				inflate.needsInput());
    571 
    572 		byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
    573 		inflate.setInput(byteArray);
    574 		assertFalse(
    575 				"methodNeedsInput returned true when the input buffer is full",
    576 				inflate.needsInput());
    577 
    578 		inflate.reset();
    579 		byte byteArrayEmpty[] = new byte[0];
    580 		inflate.setInput(byteArrayEmpty);
    581 		assertTrue(
    582 				"needsInput give wrong boolean value as a result of an empty input buffer",
    583 				inflate.needsInput());
    584 	}
    585 
    586 	/**
    587 	 * @tests java.util.zip.Inflater#reset()
    588 	 */
    589 	public void test_reset() {
    590 		// test method of java.util.zip.inflater.reset()
    591 		byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
    592 		byte outPutInf[] = new byte[100];
    593 		int y = 0;
    594 		Inflater inflate = new Inflater();
    595 		try {
    596 			while (!(inflate.finished())) {
    597 				if (inflate.needsInput()) {
    598 					inflate.setInput(outPutBuff1);
    599 				}
    600 				y += inflate.inflate(outPutInf, y, outPutInf.length - y);
    601 			}
    602 		} catch (DataFormatException e) {
    603 			fail("Invalid input to be decompressed");
    604 		}
    605 		for (int i = 0; i < byteArray.length; i++) {
    606 			assertEquals(
    607 					"Final decompressed data does not equal the original data",
    608 					byteArray[i], outPutInf[i]);
    609 		}
    610 		assertEquals("final decompressed data contained more bytes than original - reset",
    611 				0, outPutInf[byteArray.length]);
    612 
    613 		// testing that resetting the inflater will also return the correct
    614 		// decompressed data
    615 
    616 		inflate.reset();
    617 		try {
    618 			while (!(inflate.finished())) {
    619 				if (inflate.needsInput()) {
    620 					inflate.setInput(outPutBuff1);
    621 				}
    622 				inflate.inflate(outPutInf);
    623 			}
    624 		} catch (DataFormatException e) {
    625 			fail("Invalid input to be decompressed");
    626 		}
    627 		for (int i = 0; i < byteArray.length; i++) {
    628 			assertEquals(
    629 					"Final decompressed data does not equal the original data",
    630 					byteArray[i], outPutInf[i]);
    631 		}
    632 		assertEquals("final decompressed data contained more bytes than original - reset",
    633 				0, outPutInf[byteArray.length]);
    634 
    635 	}
    636 
    637 	/**
    638 	 * @tests java.util.zip.Inflater#setDictionary(byte[])
    639 	 */
    640 	public void test_setDictionary$B() {
    641         //FIXME This test doesn't pass in Harmony classlib or Sun 5.0_7 RI
    642         /*
    643 		// test method of java.util.zip.inflater.setDictionary(byte)
    644 		byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
    645 		byte byteArray[] = { 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3',
    646 				'w', 'r' };
    647 
    648 		byte outPutInf[] = new byte[100];
    649 
    650 		// trying to inflate without setting a dictionary
    651 
    652 		Inflater inflateWO = new Inflater();
    653 		byte outPutInf2[] = new byte[100];
    654 		int r = 0;
    655 		try {
    656 			while (!(inflateWO.finished())) {
    657 				if (inflateWO.needsInput()) {
    658 					inflateWO.setInput(outPutDiction);
    659 				}
    660 				inflateWO.inflate(outPutInf2);
    661 			}
    662 		} catch (DataFormatException e) {
    663 			r = 1;
    664 		}
    665 		assertEquals("invalid input to be decompressed due to dictionary not set",
    666 				1, r);
    667 		// now setting the dictionary in inflater
    668 		Inflater inflate = new Inflater();
    669 		try {
    670 			while (!(inflate.finished())) {
    671 				if (inflate.needsInput()) {
    672 					inflate.setInput(outPutDiction);
    673 				}
    674 				if (inflate.needsDictionary()) {
    675 					inflate.setDictionary(dictionaryArray);
    676 				}
    677 				inflate.inflate(outPutInf);
    678 			}
    679 		} catch (DataFormatException e) {
    680 			fail("Invalid input to be decompressed");
    681 		}
    682 		for (int i = 0; i < byteArray.length; i++) {
    683 			assertTrue(
    684 					"Final decompressed data does not equal the original data",
    685 					byteArray[i] == outPutInf[i]);
    686 		}
    687 		assertEquals("final decompressed data contained more bytes than original - deflateB",
    688 				0, outPutInf[byteArray.length]);
    689                 */
    690 	}
    691 
    692 	/**
    693 	 * @tests java.util.zip.Inflater#setInput(byte[])
    694 	 */
    695 	public void test_setInput$B() {
    696 		// test method of java.util.zip.inflater.setInput(byte)
    697 		byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
    698 		Inflater inflate = new Inflater();
    699 		inflate.setInput(byteArray);
    700 		assertTrue("setInputB did not deliver any byte to the input buffer",
    701 				inflate.getRemaining() != 0);
    702 	}
    703 
    704 	/**
    705 	 * @tests java.util.zip.Inflater#setInput(byte[], int, int)
    706 	 */
    707 	public void test_setInput$BII() {
    708 		// test method of java.util.zip.inflater.setInput(byte,int,int)
    709 		byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
    710 		int offSet = 6;
    711 		int length = 6;
    712 		Inflater inflate = new Inflater();
    713 		inflate.setInput(byteArray, offSet, length);
    714 		assertEquals(
    715 				"setInputBII did not deliver the right number of bytes to the input buffer",
    716 				length, inflate.getRemaining());
    717 		// boundary check
    718 		inflate.reset();
    719 		int r = 0;
    720 		try {
    721 			inflate.setInput(byteArray, 100, 100);
    722 		} catch (ArrayIndexOutOfBoundsException e) {
    723 			r = 1;
    724 		}
    725 		assertEquals("boundary check is not present for setInput", 1, r);
    726 	}
    727 
    728 	@Override
    729     protected void setUp() {
    730 		try {
    731 			java.io.InputStream infile = Support_Resources
    732 					.getStream("hyts_compressD.bin");
    733 			BufferedInputStream inflatIP = new BufferedInputStream(infile);
    734 			inflatIP.read(outPutBuff1, 0, outPutBuff1.length);
    735 			inflatIP.close();
    736 
    737 			java.io.InputStream infile2 = Support_Resources
    738 					.getStream("hyts_compDiction.bin");
    739 			BufferedInputStream inflatIP2 = new BufferedInputStream(infile2);
    740 			inflatIP2.read(outPutDiction, 0, outPutDiction.length);
    741 			inflatIP2.close();
    742 
    743 		} catch (FileNotFoundException e) {
    744 			fail(
    745 					"input file to test InflaterInputStream constructor is not found");
    746 		} catch (ZipException e) {
    747 			fail(
    748 					"read() threw an zip exception while testing constructor");
    749 		} catch (IOException e) {
    750 			fail("read() threw an exception while testing constructor");
    751 		}
    752 	}
    753 
    754 	@Override
    755     protected void tearDown() {
    756 	}
    757 
    758     /**
    759      * @tests java.util.zip.Deflater#getBytesRead()
    760      */
    761     public void test_getBytesRead() throws DataFormatException,
    762             UnsupportedEncodingException {
    763         // Regression test for HARMONY-158
    764         Deflater def = new Deflater();
    765         Inflater inf = new Inflater();
    766         assertEquals(0, def.getTotalIn());
    767         assertEquals(0, def.getTotalOut());
    768         assertEquals(0, def.getBytesRead());
    769         // Encode a String into bytes
    770         String inputString = "blahblahblah??";
    771         byte[] input = inputString.getBytes("UTF-8");
    772 
    773         // Compress the bytes
    774         byte[] output = new byte[100];
    775         def.setInput(input);
    776         def.finish();
    777         def.deflate(output);
    778         inf.setInput(output);
    779         int compressedDataLength =inf.inflate(input);
    780         assertEquals(16, inf.getTotalIn());
    781         assertEquals(compressedDataLength, inf.getTotalOut());
    782         assertEquals(16, inf.getBytesRead());
    783     }
    784 
    785     /**
    786      * @tests java.util.zip.Deflater#getBytesRead()
    787      */
    788     public void test_getBytesWritten() throws DataFormatException, UnsupportedEncodingException {
    789         // Regression test for HARMONY-158
    790         Deflater def = new Deflater();
    791         Inflater inf = new Inflater();
    792         assertEquals(0, def.getTotalIn());
    793         assertEquals(0, def.getTotalOut());
    794         assertEquals(0, def.getBytesWritten());
    795         // Encode a String into bytes
    796         String inputString = "blahblahblah??";
    797         byte[] input = inputString.getBytes("UTF-8");
    798 
    799         // Compress the bytes
    800         byte[] output = new byte[100];
    801         def.setInput(input);
    802         def.finish();
    803         def.deflate(output);
    804         inf.setInput(output);
    805         int compressedDataLength =inf.inflate(input);
    806         assertEquals(16, inf.getTotalIn());
    807         assertEquals(compressedDataLength, inf.getTotalOut());
    808         assertEquals(14, inf.getBytesWritten());
    809     }
    810 
    811     /**
    812      * @tests java.util.zip.Deflater#inflate(byte[], int, int)
    813      */
    814     public void testInflate() throws Exception {
    815         // Regression for HARMONY-81
    816         Inflater inf = new Inflater();
    817         int res = inf.inflate(new byte[0], 0, 0);
    818 
    819         assertEquals(0, res);
    820 
    821         // Regression for HARMONY-2508
    822         Inflater inflater = new Inflater();
    823         byte[] b = new byte[1024];
    824         assertEquals(0, inflater.inflate(b));
    825         inflater.end();
    826 
    827         // Regression for HARMONY-2510
    828         inflater = new Inflater();
    829         inflater.setInput(new byte[] { -1 });
    830         try {
    831             inflater.inflate(b);
    832 
    833             // The RI detects malformed data on the malformed input { -1 }. Both
    834             // this implementation and the native zlib API return "need input"
    835             // on that data. This is an error if the stream is exhausted, but
    836             // not one that results in an exception in the Inflater API.
    837             assertTrue(inflater.needsInput());
    838         } catch (DataFormatException e) {
    839             // expected
    840         }
    841 
    842         inflater = new Inflater();
    843         inflater.setInput(new byte[] { -1, -1, -1 });
    844         try {
    845             inflater.inflate(b);
    846         } catch (DataFormatException e) {
    847             // expected
    848         }
    849     }
    850 
    851     public void testSetDictionary$B() throws Exception {
    852         int i = 0;
    853         String inputString = "blah string contains blahblahblahblah and blah";
    854         String dictionary1 = "blah";
    855         String dictionary2 = "1234";
    856 
    857         byte[] outputNo = new byte[100];
    858         byte[] output1 = new byte[100];
    859         byte[] output2 = new byte[100];
    860         Deflater defDictNo = new Deflater(9);
    861         Deflater defDict1 = new Deflater(9);
    862         Deflater defDict2 = new Deflater(9);
    863 
    864         defDict1.setDictionary(dictionary1.getBytes());
    865         defDict2.setDictionary(dictionary2.getBytes());
    866 
    867         defDictNo.setInput(inputString.getBytes());
    868         defDict1.setInput(inputString.getBytes());
    869         defDict2.setInput(inputString.getBytes());
    870 
    871         defDictNo.finish();
    872         defDict1.finish();
    873         defDict2.finish();
    874 
    875         int dataLenNo = defDictNo.deflate(outputNo);
    876         int dataLen1 = defDict1.deflate(output1);
    877         int dataLen2 = defDict2.deflate(output2);
    878 
    879         boolean passNo1 = false;
    880         boolean passNo2 = false;
    881         boolean pass12 = false;
    882 
    883         for (i = 0; i < (dataLenNo < dataLen1 ? dataLenNo : dataLen1); i++) {
    884             if (outputNo[i] != output1[i]) {
    885                 passNo1 = true;
    886                 break;
    887             }
    888         }
    889         for (i = 0; i < (dataLenNo < dataLen1 ? dataLenNo : dataLen2); i++) {
    890             if (outputNo[i] != output2[i]) {
    891                 passNo2 = true;
    892                 break;
    893             }
    894         }
    895         for (i = 0; i < (dataLen1 < dataLen2 ? dataLen1 : dataLen2); i++) {
    896             if (output1[i] != output2[i]) {
    897                 pass12 = true;
    898                 break;
    899             }
    900         }
    901 
    902         assertTrue(
    903                 "Compressed data the same for stream with dictionary and without it.",
    904                 passNo1);
    905         assertTrue(
    906                 "Compressed data the same for stream with dictionary and without it.",
    907                 passNo2);
    908         assertTrue(
    909                 "Compressed data the same for stream with different dictionaries.",
    910                 pass12);
    911 
    912         Inflater inflNo = new Inflater();
    913         Inflater infl1 = new Inflater();
    914         Inflater infl2 = new Inflater();
    915 
    916         byte[] result = new byte[100];
    917         int decLen;
    918 
    919         inflNo.setInput(outputNo, 0, dataLenNo);
    920         decLen = inflNo.inflate(result);
    921 
    922         assertFalse(inflNo.needsDictionary());
    923         inflNo.end();
    924         assertEquals(inputString, new String(result, 0, decLen));
    925 
    926         infl1.setInput(output1, 0, dataLen1);
    927         decLen = infl1.inflate(result);
    928 
    929         assertTrue(infl1.needsDictionary());
    930         infl1.setDictionary(dictionary1.getBytes());
    931         decLen = infl1.inflate(result);
    932         infl1.end();
    933         assertEquals(inputString, new String(result, 0, decLen));
    934 
    935         infl2.setInput(output2, 0, dataLen2);
    936         decLen = infl2.inflate(result);
    937 
    938         assertTrue(infl2.needsDictionary());
    939         infl2.setDictionary(dictionary2.getBytes());
    940         decLen = infl2.inflate(result);
    941         infl2.end();
    942         assertEquals(inputString, new String(result, 0, decLen));
    943 
    944 
    945         inflNo = new Inflater();
    946         infl1 = new Inflater();
    947         inflNo.setInput(outputNo, 0, dataLenNo);
    948         try {
    949             infl1.setDictionary(dictionary1.getBytes());
    950             fail("IllegalArgumentException expected.");
    951         } catch (IllegalArgumentException ee) {
    952             // expected.
    953         }
    954         inflNo.end();
    955 
    956         infl1.setInput(output1, 0, dataLen1);
    957         decLen = infl1.inflate(result);
    958 
    959         assertTrue(infl1.needsDictionary());
    960         try {
    961             infl1.setDictionary(dictionary2.getBytes());
    962             fail("IllegalArgumentException expected.");
    963         } catch (IllegalArgumentException ee) {
    964             // expected.
    965         }
    966         infl1.end();
    967         try{
    968             infl1.setDictionary(dictionary2.getBytes());
    969             fail("IllegalStateException expected");
    970         }catch(IllegalStateException ise){
    971             //expected
    972         }
    973     }
    974 
    975     public void testSetDictionary$BII() throws Exception {
    976         int i = 0;
    977         String inputString = "blah string contains blahblahblahblah and blah";
    978         String dictionary1 = "blah";
    979         String dictionary2 = "blahblahblah";
    980 
    981         byte[] output1 = new byte[100];
    982         byte[] output2 = new byte[100];
    983         byte[] output3 = new byte[100];
    984 
    985         Deflater defDict1 = new Deflater(9);
    986         Deflater defDict2 = new Deflater(9);
    987         Deflater defDict3 = new Deflater(9);
    988 
    989         defDict1.setDictionary(dictionary1.getBytes());
    990         defDict2.setDictionary(dictionary2.getBytes());
    991         defDict3.setDictionary(dictionary2.getBytes(), 4, 4);
    992 
    993         defDict1.setInput(inputString.getBytes());
    994         defDict2.setInput(inputString.getBytes());
    995         defDict3.setInput(inputString.getBytes());
    996 
    997         defDict1.finish();
    998         defDict2.finish();
    999         defDict3.finish();
   1000 
   1001         int dataLen1 = defDict1.deflate(output1);
   1002         int dataLen2 = defDict2.deflate(output2);
   1003         int dataLen3 = defDict3.deflate(output3);
   1004 
   1005         boolean pass12 = false;
   1006         boolean pass23 = false;
   1007         boolean pass13 = true;
   1008 
   1009         for (i = 0; i < (dataLen1 < dataLen2 ? dataLen1 : dataLen2); i++) {
   1010             if (output1[i] != output2[i]) {
   1011                 pass12 = true;
   1012                 break;
   1013             }
   1014         }
   1015         for (i = 0; i < (dataLen2 < dataLen3 ? dataLen2 : dataLen3); i++) {
   1016             if (output2[i] != output3[i]) {
   1017                 pass23 = true;
   1018                 break;
   1019             }
   1020         }
   1021         for (i = 0; i < (dataLen1 < dataLen3 ? dataLen1 : dataLen3); i++) {
   1022             if (output1[i] != output3[i]) {
   1023                 pass13 = false;
   1024                 break;
   1025             }
   1026         }
   1027 
   1028         assertTrue(
   1029                 "Compressed data the same for stream with different dictionaries.",
   1030                 pass12);
   1031         assertTrue(
   1032                 "Compressed data the same for stream with different dictionaries.",
   1033                 pass23);
   1034         assertTrue(
   1035                 "Compressed data the differs for stream with the same dictionaries.",
   1036                 pass13);
   1037 
   1038         Inflater infl1 = new Inflater();
   1039         Inflater infl2 = new Inflater();
   1040         Inflater infl3 = new Inflater();
   1041         Inflater infl4 = new Inflater();
   1042 
   1043         byte[] result = new byte[100];
   1044         int decLen;
   1045 
   1046         infl1.setInput(output1, 0, dataLen1);
   1047         decLen = infl1.inflate(result);
   1048 
   1049         assertTrue(infl1.needsDictionary());
   1050         infl1.setDictionary(dictionary2.getBytes(), 4, 4);
   1051         decLen = infl1.inflate(result);
   1052         infl1.end();
   1053         assertEquals(inputString, new String(result, 0, decLen));
   1054 
   1055         infl2.setInput(output2, 0, dataLen2);
   1056         decLen = infl2.inflate(result);
   1057 
   1058         assertTrue(infl2.needsDictionary());
   1059         try {
   1060             infl2.setDictionary(dictionary1.getBytes());
   1061             fail("IllegalArgumentException expected.");
   1062         } catch (IllegalArgumentException ee) {
   1063             // expected
   1064         }
   1065         infl2.end();
   1066 
   1067         infl3.setInput(output3, 0, dataLen3);
   1068         decLen = infl3.inflate(result);
   1069 
   1070         assertTrue(infl3.needsDictionary());
   1071         infl3.setDictionary(dictionary1.getBytes());
   1072         decLen = infl3.inflate(result);
   1073         infl3.end();
   1074         assertEquals(inputString, new String(result, 0, decLen));
   1075 
   1076         //exception test
   1077         infl4.setInput(output3, 0, dataLen3);
   1078         decLen = infl4.inflate(result);
   1079         assertTrue(infl4.needsDictionary());
   1080 
   1081         try{
   1082             infl4.setDictionary(dictionary1.getBytes(), 4, 4);
   1083             fail("ArrayIndexOutOfBoundsException expected");
   1084         }catch(ArrayIndexOutOfBoundsException aiob){
   1085             //expected
   1086         }
   1087     }
   1088 
   1089     public void testExceptions() throws Exception {
   1090         byte byteArray[] = { 5, 2, 3, 7, 8 };
   1091 
   1092         int r = 0;
   1093         Inflater inflate = new Inflater();
   1094         inflate.setInput(byteArray);
   1095         inflate.end();
   1096 
   1097         try{
   1098             inflate.getAdler();
   1099             fail("IllegalStateException expected");
   1100         }catch(IllegalStateException expected){
   1101             //expected
   1102         }
   1103 
   1104         try{
   1105             inflate.getBytesRead();
   1106             fail("NullPointerException expected");
   1107         }catch(IllegalStateException expected){
   1108         }catch(NullPointerException expected){
   1109             //expected
   1110         }
   1111 
   1112         try{
   1113             inflate.getBytesWritten();
   1114             fail("NullPointerException expected");
   1115         }catch(NullPointerException expected){
   1116         }catch(IllegalStateException expected){
   1117             //expected
   1118         }
   1119 
   1120 
   1121         try{
   1122             inflate.getTotalIn();
   1123             fail("IllegalStateException expected");
   1124         }catch(IllegalStateException ise){
   1125             //expected
   1126         }
   1127 
   1128         try{
   1129             inflate.getTotalOut();
   1130             fail("IllegalStateException expected");
   1131         }catch(IllegalStateException ise){
   1132             //expected
   1133         }
   1134 
   1135     }
   1136 }
   1137