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