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 tests.support.resource.Support_Resources; 26 27import java.io.ByteArrayOutputStream; 28import java.io.File; 29import java.io.IOException; 30import java.io.InputStream; 31import java.util.TimeZone; 32import java.util.zip.ZipEntry; 33 34@TestTargetClass(ZipEntry.class) 35public class ZipEntryTest extends junit.framework.TestCase { 36 37 // BEGIN android-added 38 public byte[] getAllBytesFromStream(InputStream is) throws IOException { 39 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 40 byte[] buf = new byte[512]; 41 int iRead; 42 int off; 43 while (is.available() > 0) { 44 iRead = is.read(buf, 0, buf.length); 45 if (iRead > 0) bs.write(buf, 0, iRead); 46 } 47 return bs.toByteArray(); 48 } 49 50 // END android-added 51 52 // zip file hyts_ZipFile.zip must be included as a resource 53 java.util.zip.ZipEntry zentry; 54 55 java.util.zip.ZipFile zfile; 56 57 private static final String platformId = System.getProperty( 58 "com.ibm.oti.configuration", "JDK") 59 + System.getProperty("java.vm.version"); 60 61 static final String tempFileName = platformId + "zipentrytest.zip"; 62 63 long orgSize; 64 65 long orgCompressedSize; 66 67 long orgCrc; 68 69 long orgTime; 70 71 String orgComment; 72 73 /** 74 * @tests java.util.zip.ZipEntry#ZipEntry(java.lang.String) 75 */ 76 @TestTargetNew( 77 level = TestLevel.COMPLETE, 78 notes = "", 79 method = "ZipEntry", 80 args = {java.lang.String.class} 81 ) 82 public void test_ConstructorLjava_lang_String() { 83 // Test for method java.util.zip.ZipEntry(java.lang.String) 84 zentry = zfile.getEntry("File3.txt"); 85 assertNotNull("Failed to create ZipEntry", zentry); 86 try { 87 zentry = zfile.getEntry(null); 88 fail("NullPointerException not thrown"); 89 } catch (NullPointerException e) { 90 } 91 StringBuffer s = new StringBuffer(); 92 for (int i = 0; i < 65535; i++) { 93 s.append('a'); 94 } 95 try { 96 zentry = new ZipEntry(s.toString()); 97 } catch (IllegalArgumentException e) { 98 fail("Unexpected IllegalArgumentException During Test."); 99 } 100 try { 101 s.append('a'); 102 zentry = new ZipEntry(s.toString()); 103 fail("IllegalArgumentException not thrown"); 104 } catch (IllegalArgumentException e) { 105 } 106 try { 107 String n = null; 108 zentry = new ZipEntry(n); 109 fail("NullPointerException not thrown"); 110 } catch (NullPointerException e) { 111 } 112 } 113 114 /** 115 * @tests java.util.zip.ZipEntry#getComment() 116 */ 117 @TestTargetNew( 118 level = TestLevel.COMPLETE, 119 notes = "", 120 method = "getComment", 121 args = {} 122 ) 123 public void test_getComment() { 124 // Test for method java.lang.String java.util.zip.ZipEntry.getComment() 125 ZipEntry zipEntry = new ZipEntry("zippy.zip"); 126 assertNull("Incorrect Comment Returned.", zipEntry.getComment()); 127 zipEntry.setComment("This Is A Comment"); 128 assertEquals("Incorrect Comment Returned.", "This Is A Comment", 129 zipEntry.getComment()); 130 } 131 132 /** 133 * @tests java.util.zip.ZipEntry#getCompressedSize() 134 */ 135 @TestTargetNew( 136 level = TestLevel.COMPLETE, 137 notes = "", 138 method = "getCompressedSize", 139 args = {} 140 ) 141 public void test_getCompressedSize() { 142 // Test for method long java.util.zip.ZipEntry.getCompressedSize() 143 assertTrue("Incorrect compressed size returned", zentry 144 .getCompressedSize() == orgCompressedSize); 145 } 146 147 /** 148 * @tests java.util.zip.ZipEntry#getCrc() 149 */ 150 @TestTargetNew( 151 level = TestLevel.COMPLETE, 152 notes = "", 153 method = "getCrc", 154 args = {} 155 ) 156 public void test_getCrc() { 157 // Test for method long java.util.zip.ZipEntry.getCrc() 158 assertTrue("Failed to get Crc", zentry.getCrc() == orgCrc); 159 } 160 161 /** 162 * @tests java.util.zip.ZipEntry#getExtra() 163 */ 164 @TestTargetNew( 165 level = TestLevel.COMPLETE, 166 notes = "", 167 method = "getExtra", 168 args = {} 169 ) 170 public void test_getExtra() { 171 // Test for method byte [] java.util.zip.ZipEntry.getExtra() 172 assertNull("Incorrect extra information returned", zentry.getExtra()); 173 byte[] ba = {'T', 'E', 'S', 'T'}; 174 zentry = new ZipEntry("test.tst"); 175 zentry.setExtra(ba); 176 assertTrue("Incorrect Extra Information Returned.", 177 zentry.getExtra() == ba); 178 } 179 180 /** 181 * @tests java.util.zip.ZipEntry#getMethod() 182 */ 183 @TestTargetNew( 184 level = TestLevel.COMPLETE, 185 notes = "", 186 method = "getMethod", 187 args = {} 188 ) 189 public void test_getMethod() { 190 // Test for method int java.util.zip.ZipEntry.getMethod() 191 zentry = zfile.getEntry("File1.txt"); 192 assertTrue("Incorrect compression method returned", 193 zentry.getMethod() == java.util.zip.ZipEntry.STORED); 194 zentry = zfile.getEntry("File3.txt"); 195 assertTrue("Incorrect compression method returned", 196 zentry.getMethod() == java.util.zip.ZipEntry.DEFLATED); 197 zentry = new ZipEntry("test.tst"); 198 assertEquals("Incorrect Method Returned.", -1, zentry.getMethod()); 199 } 200 201 /** 202 * @tests java.util.zip.ZipEntry#getName() 203 */ 204 @TestTargetNew( 205 level = TestLevel.COMPLETE, 206 notes = "", 207 method = "getName", 208 args = {} 209 ) 210 public void test_getName() { 211 // Test for method java.lang.String java.util.zip.ZipEntry.getName() 212 assertEquals( 213 "Incorrect name returned - Note return result somewhat ambiguous in spec", 214 "File1.txt", zentry.getName()); 215 } 216 217 /** 218 * @tests java.util.zip.ZipEntry#getSize() 219 */ 220 @TestTargetNew( 221 level = TestLevel.COMPLETE, 222 notes = "", 223 method = "getSize", 224 args = {} 225 ) 226 public void test_getSize() { 227 // Test for method long java.util.zip.ZipEntry.getSize() 228 assertTrue("Incorrect size returned", zentry.getSize() == orgSize); 229 } 230 231 /** 232 * @tests java.util.zip.ZipEntry#getTime() 233 */ 234 @TestTargetNew( 235 level = TestLevel.COMPLETE, 236 notes = "", 237 method = "getTime", 238 args = {} 239 ) 240 public void test_getTime() { 241 // Test for method long java.util.zip.ZipEntry.getTime() 242 assertTrue("Failed to get time", zentry.getTime() == orgTime); 243 } 244 245 /** 246 * @tests java.util.zip.ZipEntry#isDirectory() 247 */ 248 @TestTargetNew( 249 level = TestLevel.COMPLETE, 250 notes = "", 251 method = "isDirectory", 252 args = {} 253 ) 254 public void test_isDirectory() { 255 // Test for method boolean java.util.zip.ZipEntry.isDirectory() 256 assertTrue("Entry should not answer true to isDirectory", !zentry 257 .isDirectory()); 258 zentry = new ZipEntry("Directory/"); 259 assertTrue("Entry should answer true to isDirectory", zentry 260 .isDirectory()); 261 } 262 263 /** 264 * @tests java.util.zip.ZipEntry#setComment(java.lang.String) 265 */ 266 @TestTargetNew( 267 level = TestLevel.COMPLETE, 268 notes = "", 269 method = "setComment", 270 args = {java.lang.String.class} 271 ) 272 public void test_setCommentLjava_lang_String() { 273 // Test for method void 274 // java.util.zip.ZipEntry.setComment(java.lang.String) 275 zentry = zfile.getEntry("File1.txt"); 276 zentry.setComment("Set comment using api"); 277 assertEquals("Comment not correctly set", "Set comment using api", 278 zentry.getComment()); 279 String n = null; 280 zentry.setComment(n); 281 assertNull("Comment not correctly set", zentry.getComment()); 282 StringBuffer s = new StringBuffer(); 283 for (int i = 0; i < 0xFFFF; i++) { 284 s.append('a'); 285 } 286 try { 287 zentry.setComment(s.toString()); 288 } catch (IllegalArgumentException e) { 289 fail("Unexpected IllegalArgumentException During Test."); 290 } 291 try { 292 s.append('a'); 293 zentry.setComment(s.toString()); 294 fail("IllegalArgumentException not thrown"); 295 } catch (IllegalArgumentException e) { 296 } 297 } 298 299 /** 300 * @tests java.util.zip.ZipEntry#setCompressedSize(long) 301 */ 302 @TestTargetNew( 303 level = TestLevel.COMPLETE, 304 notes = "", 305 method = "setCompressedSize", 306 args = {long.class} 307 ) 308 public void test_setCompressedSizeJ() { 309 // Test for method void java.util.zip.ZipEntry.setCompressedSize(long) 310 zentry.setCompressedSize(orgCompressedSize + 10); 311 assertTrue("Set compressed size failed", 312 zentry.getCompressedSize() == (orgCompressedSize + 10)); 313 zentry.setCompressedSize(0); 314 assertEquals("Set compressed size failed", 0, zentry 315 .getCompressedSize()); 316 zentry.setCompressedSize(-25); 317 assertEquals("Set compressed size failed", -25, zentry 318 .getCompressedSize()); 319 zentry.setCompressedSize(4294967296l); 320 assertTrue("Set compressed size failed", 321 zentry.getCompressedSize() == 4294967296l); 322 } 323 324 /** 325 * @tests java.util.zip.ZipEntry#setCrc(long) 326 */ 327 @TestTargetNew( 328 level = TestLevel.COMPLETE, 329 notes = "", 330 method = "setCrc", 331 args = {long.class} 332 ) 333 public void test_setCrcJ() { 334 // Test for method void java.util.zip.ZipEntry.setCrc(long) 335 zentry.setCrc(orgCrc + 100); 336 assertTrue("Failed to set Crc", zentry.getCrc() == (orgCrc + 100)); 337 zentry.setCrc(0); 338 assertEquals("Failed to set Crc", 0, zentry.getCrc()); 339 try { 340 zentry.setCrc(-25); 341 fail("IllegalArgumentException not thrown"); 342 } catch (IllegalArgumentException e) { 343 } 344 try { 345 zentry.setCrc(4294967295l); 346 } catch (IllegalArgumentException e) { 347 fail("Unexpected IllegalArgumentException during test"); 348 } 349 try { 350 zentry.setCrc(4294967296l); 351 fail("IllegalArgumentException not thrown"); 352 } catch (IllegalArgumentException e) { 353 } 354 } 355 356 /** 357 * @tests java.util.zip.ZipEntry#setExtra(byte[]) 358 */ 359 @TestTargetNew( 360 level = TestLevel.COMPLETE, 361 notes = "", 362 method = "setExtra", 363 args = {byte[].class} 364 ) 365 public void test_setExtra$B() { 366 // Test for method void java.util.zip.ZipEntry.setExtra(byte []) 367 zentry = zfile.getEntry("File1.txt"); 368 zentry.setExtra("Test setting extra information".getBytes()); 369 assertEquals("Extra information not written properly", 370 "Test setting extra information", new String(zentry.getExtra(), 371 0, zentry.getExtra().length)); 372 zentry = new ZipEntry("test.tst"); 373 byte[] ba = new byte[0xFFFF]; 374 try { 375 zentry.setExtra(ba); 376 } catch (IllegalArgumentException e) { 377 fail("Unexpected IllegalArgumentException during test"); 378 } 379 try { 380 ba = new byte[0xFFFF + 1]; 381 zentry.setExtra(ba); 382 fail("IllegalArgumentException not thrown"); 383 } catch (IllegalArgumentException e) { 384 } 385 386 // One constructor 387 ZipEntry zeInput = new ZipEntry("InputZIP"); 388 byte[] extraB = {'a', 'b', 'd', 'e'}; 389 zeInput.setExtra(extraB); 390 assertEquals(extraB, zeInput.getExtra()); 391 assertEquals(extraB[3], zeInput.getExtra()[3]); 392 assertEquals(extraB.length, zeInput.getExtra().length); 393 394 // test another constructor 395 ZipEntry zeOutput = new ZipEntry(zeInput); 396 assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]); 397 assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length); 398 assertEquals(extraB[3], zeOutput.getExtra()[3]); 399 assertEquals(extraB.length, zeOutput.getExtra().length); 400 } 401 402 /** 403 * @tests java.util.zip.ZipEntry#setMethod(int) 404 */ 405 @TestTargetNew( 406 level = TestLevel.COMPLETE, 407 notes = "", 408 method = "setMethod", 409 args = {int.class} 410 ) 411 public void test_setMethodI() { 412 // Test for method void java.util.zip.ZipEntry.setMethod(int) 413 zentry = zfile.getEntry("File3.txt"); 414 zentry.setMethod(ZipEntry.STORED); 415 assertTrue("Failed to set compression method", 416 zentry.getMethod() == ZipEntry.STORED); 417 zentry.setMethod(ZipEntry.DEFLATED); 418 assertTrue("Failed to set compression method", 419 zentry.getMethod() == ZipEntry.DEFLATED); 420 try { 421 int error = 1; 422 zentry = new ZipEntry("test.tst"); 423 zentry.setMethod(error); 424 fail("IllegalArgumentException not thrown"); 425 } catch (IllegalArgumentException e) { 426 } 427 } 428 429 /** 430 * @tests java.util.zip.ZipEntry#setSize(long) 431 */ 432 @TestTargetNew( 433 level = TestLevel.COMPLETE, 434 notes = "", 435 method = "setSize", 436 args = {long.class} 437 ) 438 public void test_setSizeJ() { 439 // Test for method void java.util.zip.ZipEntry.setSize(long) 440 zentry.setSize(orgSize + 10); 441 assertTrue("Set size failed", zentry.getSize() == (orgSize + 10)); 442 zentry.setSize(0); 443 assertEquals("Set size failed", 0, zentry.getSize()); 444 try { 445 zentry.setSize(-25); 446 fail("IllegalArgumentException not thrown"); 447 } catch (IllegalArgumentException e) { 448 } 449 try { 450 zentry.setCrc(4294967295l); 451 } catch (IllegalArgumentException e) { 452 fail("Unexpected IllegalArgumentException during test"); 453 } 454 try { 455 zentry.setCrc(4294967296l); 456 fail("IllegalArgumentException not thrown"); 457 } catch (IllegalArgumentException e) { 458 } 459 } 460 461 /** 462 * @tests java.util.zip.ZipEntry#setTime(long) 463 */ 464 @TestTargetNew( 465 level = TestLevel.COMPLETE, 466 notes = "", 467 method = "setTime", 468 args = {long.class} 469 ) 470 public void test_setTimeJ() { 471 // Test for method void java.util.zip.ZipEntry.setTime(long) 472 zentry.setTime(orgTime + 10000); 473 assertTrue("Test 1: Failed to set time: " + zentry.getTime(), zentry 474 .getTime() == (orgTime + 10000)); 475 zentry.setTime(orgTime - 10000); 476 assertTrue("Test 2: Failed to set time: " + zentry.getTime(), zentry 477 .getTime() == (orgTime - 10000)); 478 TimeZone zone = TimeZone.getDefault(); 479 try { 480 TimeZone.setDefault(TimeZone.getTimeZone("EST")); 481 zentry.setTime(0); 482 assertTrue("Test 3: Failed to set time: " + zentry.getTime(), 483 zentry.getTime() == 315550800000L); 484 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); 485 assertTrue("Test 3a: Failed to set time: " + zentry.getTime(), 486 zentry.getTime() == 315532800000L); 487 zentry.setTime(0); 488 TimeZone.setDefault(TimeZone.getTimeZone("EST")); 489 assertTrue("Test 3b: Failed to set time: " + zentry.getTime(), 490 zentry.getTime() == 315550800000L); 491 492 zentry.setTime(-25); 493 assertTrue("Test 4: Failed to set time: " + zentry.getTime(), 494 zentry.getTime() == 315550800000L); 495 zentry.setTime(4354837200000L); 496 assertTrue("Test 5: Failed to set time: " + zentry.getTime(), 497 zentry.getTime() == 315550800000L); 498 } finally { 499 TimeZone.setDefault(zone); 500 } 501 } 502 503 /** 504 * @tests java.util.zip.ZipEntry#toString() 505 */ 506 @TestTargetNew( 507 level = TestLevel.COMPLETE, 508 notes = "", 509 method = "toString", 510 args = {} 511 ) 512 public void test_toString() { 513 // Test for method java.lang.String java.util.zip.ZipEntry.toString() 514 assertTrue("Returned incorrect entry name", zentry.toString().indexOf( 515 "File1.txt") >= 0); 516 } 517 518 /** 519 * @tests java.util.zip.ZipEntry#ZipEntry(java.util.zip.ZipEntry) 520 */ 521 @TestTargetNew( 522 level = TestLevel.COMPLETE, 523 notes = "", 524 method = "ZipEntry", 525 args = {java.util.zip.ZipEntry.class} 526 ) 527 public void test_ConstructorLjava_util_zip_ZipEntry() { 528 // Test for method java.util.zip.ZipEntry(util.zip.ZipEntry) 529 zentry.setSize(2); 530 zentry.setCompressedSize(4); 531 zentry.setComment("Testing"); 532 ZipEntry zentry2 = new ZipEntry(zentry); 533 assertEquals("ZipEntry Created With Incorrect Size.", 2, zentry2 534 .getSize()); 535 assertEquals("ZipEntry Created With Incorrect Compressed Size.", 4, 536 zentry2.getCompressedSize()); 537 assertEquals("ZipEntry Created With Incorrect Comment.", "Testing", 538 zentry2.getComment()); 539 assertTrue("ZipEntry Created With Incorrect Crc.", 540 zentry2.getCrc() == orgCrc); 541 assertTrue("ZipEntry Created With Incorrect Time.", 542 zentry2.getTime() == orgTime); 543 } 544 545 /** 546 * @tests java.util.zip.ZipEntry#clone() 547 */ 548 @TestTargetNew( 549 level = TestLevel.COMPLETE, 550 notes = "", 551 method = "clone", 552 args = {} 553 ) 554 public void test_clone() { 555 // Test for method java.util.zip.ZipEntry.clone() 556 Object obj = zentry.clone(); 557 assertTrue("toString()", obj.toString().equals(zentry.toString())); 558 assertTrue("hashCode()", obj.hashCode() == zentry.hashCode()); 559 560 // One constructor 561 ZipEntry zeInput = new ZipEntry("InputZIP"); 562 byte[] extraB = {'a', 'b', 'd', 'e'}; 563 zeInput.setExtra(extraB); 564 assertEquals(extraB, zeInput.getExtra()); 565 assertEquals(extraB[3], zeInput.getExtra()[3]); 566 assertEquals(extraB.length, zeInput.getExtra().length); 567 568 // test Clone() 569 ZipEntry zeOutput = (ZipEntry) zeInput.clone(); 570 assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]); 571 assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length); 572 assertEquals(extraB[3], zeOutput.getExtra()[3]); 573 assertEquals(extraB.length, zeOutput.getExtra().length); 574 } 575 576 @TestTargetNew( 577 level = TestLevel.COMPLETE, 578 notes = "", 579 method = "hashCode", 580 args = {} 581 ) 582 public void test_hashCode() { 583 try { 584 zentry.hashCode(); 585 } catch (Exception ee) { 586 fail("Unexpected exception " + ee); 587 } 588 } 589 590 /** 591 * Sets up the fixture, for example, open a network connection. This method 592 * is called before a test is executed. 593 */ 594 595 @Override 596 protected void setUp() { 597 java.io.File f = null; 598 try { 599 // BEGIN android-changed 600 // Create a local copy of the file since some tests want to alter 601 // information. 602 f = File.createTempFile(tempFileName, ".zip"); 603 // Create absolute filename as ZipFile does not resolve using 604 // user.dir 605 f = new java.io.File(f.getAbsolutePath()); 606 f.delete(); 607 java.io.InputStream is = Support_Resources 608 .getStream("hyts_ZipFile.zip"); 609 java.io.FileOutputStream fos = new java.io.FileOutputStream(f); 610 byte[] rbuf = getAllBytesFromStream(is); 611 // END android-changed 612 fos.write(rbuf, 0, rbuf.length); 613 is.close(); 614 fos.close(); 615 zfile = new java.util.zip.ZipFile(f); 616 zentry = zfile.getEntry("File1.txt"); 617 orgSize = zentry.getSize(); 618 orgCompressedSize = zentry.getCompressedSize(); 619 orgCrc = zentry.getCrc(); 620 orgTime = zentry.getTime(); 621 orgComment = zentry.getComment(); 622 } catch (Exception e) { 623 System.out.println("Exception during ZipFile setup <" 624 + f.getAbsolutePath() + ">: "); 625 e.printStackTrace(); 626 } 627 } 628 629 /** 630 * Tears down the fixture, for example, close a network connection. This 631 * method is called after a test is executed. 632 */ 633 634 @Override 635 protected void tearDown() { 636 try { 637 if (zfile != null) { 638 zfile.close(); 639 } 640 java.io.File f = new java.io.File(tempFileName); 641 f.delete(); 642 } catch (java.io.IOException e) { 643 System.out.println("Exception during tearDown"); 644 } 645 } 646 647} 648