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