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