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.TestLevel; 21import dalvik.annotation.TestTargetClass; 22import dalvik.annotation.TestTargetNew; 23 24import junit.framework.TestCase; 25 26import tests.support.resource.Support_Resources; 27 28import java.io.ByteArrayInputStream; 29import java.io.ByteArrayOutputStream; 30import java.io.File; 31import java.io.FileInputStream; 32import java.io.FilterInputStream; 33import java.io.IOException; 34import java.io.InputStream; 35import java.util.zip.ZipEntry; 36import java.util.zip.ZipException; 37import java.util.zip.ZipInputStream; 38import java.util.zip.ZipOutputStream; 39 40@TestTargetClass(ZipInputStream.class) 41public class ZipInputStreamTest extends TestCase { 42 // the file hyts_zipFile.zip used in setup needs to included as a resource 43 private ZipEntry zentry; 44 45 private ZipInputStream zis; 46 47 private byte[] zipBytes; 48 49 private byte[] dataBytes = "Some data in my file".getBytes(); 50 51 @Override 52 protected void setUp() { 53 try { 54 InputStream is = Support_Resources.getStream("hyts_ZipFile.zip"); 55 if (is == null) { 56 System.out.println("file hyts_ZipFile.zip can not be found"); 57 } 58 zis = new ZipInputStream(is); 59 60 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 61 ZipOutputStream zos = new ZipOutputStream(bos); 62 ZipEntry entry = new ZipEntry("myFile"); 63 zos.putNextEntry(entry); 64 zos.write(dataBytes); 65 zos.closeEntry(); 66 zos.close(); 67 zipBytes = bos.toByteArray(); 68 } catch (Exception e) { 69 System.out.println("Exception during ZipFile setup:"); 70 e.printStackTrace(); 71 } 72 } 73 74 @Override 75 protected void tearDown() { 76 if (zis != null) { 77 try { 78 zis.close(); 79 } catch (Exception e) { 80 } 81 } 82 } 83 84 /** 85 * @tests java.util.zip.ZipInputStream#ZipInputStream(java.io.InputStream) 86 */ 87 @TestTargetNew( 88 level = TestLevel.COMPLETE, 89 notes = "", 90 method = "ZipInputStream", 91 args = {java.io.InputStream.class} 92 ) 93 public void test_ConstructorLjava_io_InputStream() throws Exception { 94 zentry = zis.getNextEntry(); 95 zis.closeEntry(); 96 } 97 98 /** 99 * @tests java.util.zip.ZipInputStream#close() 100 */ 101 @TestTargetNew( 102 level = TestLevel.COMPLETE, 103 notes = "", 104 method = "close", 105 args = {} 106 ) 107 public void test_close() { 108 try { 109 zis.close(); 110 byte[] rbuf = new byte[10]; 111 zis.read(rbuf, 0, 1); 112 } catch (IOException e) { 113 return; 114 } 115 fail("Read data after stream was closed"); 116 } 117 118 /** 119 * @tests java.util.zip.ZipInputStream#close() 120 */ 121 @TestTargetNew( 122 level = TestLevel.PARTIAL_COMPLETE, 123 notes = "Checks calling method two times", 124 method = "close", 125 args = {} 126 ) 127 public void test_close2() throws Exception { 128 // Regression for HARMONY-1101 129 zis.close(); 130 // another call to close should NOT cause an exception 131 zis.close(); 132 } 133 134 /** 135 * @tests java.util.zip.ZipInputStream#closeEntry() 136 */ 137 @TestTargetNew( 138 level = TestLevel.COMPLETE, 139 notes = "", 140 method = "closeEntry", 141 args = {} 142 ) 143 public void test_closeEntry() throws Exception { 144 zentry = zis.getNextEntry(); 145 zis.closeEntry(); 146 zentry = zis.getNextEntry(); 147 zis.close(); 148 try { 149 zis.closeEntry(); 150 fail("IOException expected"); 151 } catch (IOException ee) { 152 // expected 153 } 154 File resources = Support_Resources.createTempFolder(); 155 Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); 156 FileInputStream fis = new FileInputStream(new File(resources, 157 "Broken_manifest.jar")); 158 159 ZipInputStream zis1 = new ZipInputStream(fis); 160 161 try { 162 for (int i = 0; i < 6; i++) { 163 zis1.getNextEntry(); 164 zis1.closeEntry(); 165 } 166 fail("ZipException expected"); 167 } catch (ZipException ee) { 168 // expected 169 } 170 } 171 172 @TestTargetNew( 173 level = TestLevel.COMPLETE, 174 method = "close", 175 args = {} 176 ) 177 public void test_closeAfterException() throws Exception { 178 File resources = Support_Resources.createTempFolder(); 179 Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); 180 FileInputStream fis = new FileInputStream(new File(resources, 181 "Broken_manifest.jar")); 182 183 ZipInputStream zis1 = new ZipInputStream(fis); 184 185 try { 186 for (int i = 0; i < 6; i++) { 187 zis1.getNextEntry(); 188 } 189 fail("ZipException expected"); 190 } catch (ZipException ee) { 191 // expected 192 } 193 194 zis1.close(); 195 try { 196 zis1.getNextEntry(); 197 fail("IOException expected"); 198 } catch (IOException ee) { 199 // expected 200 } 201 } 202 203 /** 204 * @tests java.util.zip.ZipInputStream#getNextEntry() 205 */ 206 @TestTargetNew( 207 level = TestLevel.COMPLETE, 208 method = "getNextEntry", 209 args = {} 210 ) 211 public void test_getNextEntry() throws Exception { 212 assertNotNull("getNextEntry failed", zis.getNextEntry()); 213 214 File resources = Support_Resources.createTempFolder(); 215 Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); 216 FileInputStream fis = new FileInputStream(new File(resources, 217 "Broken_manifest.jar")); 218 219 ZipInputStream zis1 = new ZipInputStream(fis); 220 221 try { 222 for (int i = 0; i < 6; i++) { 223 zis1.getNextEntry(); 224 } 225 fail("ZipException expected"); 226 } catch (ZipException ee) { 227 // expected 228 } 229 230 try { 231 zis1.close(); // Android throws exception here, already! 232 zis1.getNextEntry(); // But RI here, only! 233 fail("IOException expected"); 234 } catch (IOException ee) { 235 // expected 236 } 237 } 238 239 /** 240 * @tests java.util.zip.ZipInputStream#read(byte[], int, int) 241 */ 242 @TestTargetNew( 243 level = TestLevel.COMPLETE, 244 method = "read", 245 args = {byte[].class, int.class, int.class} 246 ) 247 public void test_read$BII() throws Exception { 248 zentry = zis.getNextEntry(); 249 byte[] rbuf = new byte[(int) zentry.getSize()]; 250 int r = zis.read(rbuf, 0, rbuf.length); 251 new String(rbuf, 0, r); 252 assertEquals("Failed to read entry", 12, r); 253 254 File resources = Support_Resources.createTempFolder(); 255 Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); 256 FileInputStream fis = new FileInputStream(new File(resources, 257 "Broken_manifest.jar")); 258 259 ZipInputStream zis1 = new ZipInputStream(fis); 260 261 zis1.getNextEntry(); 262 zis1.getNextEntry(); 263 264 rbuf = new byte[100]; 265 266 try { 267 zis1.read(rbuf, 10, 90); 268 fail("ZipException expected"); 269 } catch (ZipException ee) { 270 // expected 271 } 272 273 try { 274 zis1.close(); // Android throws exception here, already! 275 zis1.read(rbuf, 10, 90); // But RI here, only! 276 fail("IOException expected"); 277 } catch (IOException ee) { 278 // expected 279 } 280 } 281 282 @TestTargetNew( 283 level = TestLevel.PARTIAL_COMPLETE, 284 method = "read", 285 args = {byte[].class, int.class, int.class} 286 ) 287 public void testReadOneByteAtATime() throws IOException { 288 InputStream in = new FilterInputStream(Support_Resources.getStream("hyts_ZipFile.zip")) { 289 @Override 290 public int read(byte[] buffer, int offset, int count) throws IOException { 291 return super.read(buffer, offset, 1); // one byte at a time 292 } 293 294 @Override 295 public int read(byte[] buffer) throws IOException { 296 return super.read(buffer, 0, 1); // one byte at a time 297 } 298 }; 299 300 zis = new ZipInputStream(in); 301 while ((zentry = zis.getNextEntry()) != null) { 302 zentry.getName(); 303 } 304 zis.close(); 305 } 306 307 /** 308 * @tests java.util.zip.ZipInputStream#skip(long) 309 */ 310 @TestTargetNew( 311 level = TestLevel.COMPLETE, 312 method = "skip", 313 args = {long.class} 314 ) 315 public void test_skipJ() throws Exception { 316 zentry = zis.getNextEntry(); 317 byte[] rbuf = new byte[(int) zentry.getSize()]; 318 zis.skip(2); 319 int r = zis.read(rbuf, 0, rbuf.length); 320 assertEquals("Failed to skip data", 10, r); 321 322 zentry = zis.getNextEntry(); 323 zentry = zis.getNextEntry(); 324 long s = zis.skip(1025); 325 assertTrue("invalid skip: " + s, s == 1025); 326 327 ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream( 328 zipBytes)); 329 zis.getNextEntry(); 330 long skipLen = dataBytes.length / 2; 331 assertEquals("Assert 0: failed valid skip", skipLen, zis.skip(skipLen)); 332 zis.skip(dataBytes.length); 333 assertEquals("Assert 1: performed invalid skip", 0, zis.skip(1)); 334 assertEquals("Assert 2: failed zero len skip", 0, zis.skip(0)); 335 try { 336 zis.skip(-1); 337 fail("Assert 3: Expected Illegal argument exception"); 338 } catch (IllegalArgumentException e) { 339 // Expected 340 } 341 342 File resources = Support_Resources.createTempFolder(); 343 Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); 344 FileInputStream fis = new FileInputStream(new File(resources, 345 "Broken_manifest.jar")); 346 347 ZipInputStream zis1 = new ZipInputStream(fis); 348 349 zis1.getNextEntry(); 350 zis1.getNextEntry(); 351 352 try { 353 zis1.skip(10); 354 fail("ZipException expected"); 355 } catch (ZipException ee) { 356 // expected 357 } 358 359 try { 360 zis1.close(); // Android throws exception here, already! 361 zis1.skip(10); // But RI here, only! 362 fail("IOException expected"); 363 } catch (IOException ee) { 364 // expected 365 } 366 } 367 368 @TestTargetNew( 369 level = TestLevel.COMPLETE, 370 method = "available", 371 args = {} 372 ) 373 public void test_available() throws Exception { 374 375 File resources = Support_Resources.createTempFolder(); 376 Support_Resources.copyFile(resources, null, "hyts_ZipFile.zip"); 377 File fl = new File(resources, "hyts_ZipFile.zip"); 378 FileInputStream fis = new FileInputStream(fl); 379 380 ZipInputStream zis1 = new ZipInputStream(fis); 381 ZipEntry entry = zis1.getNextEntry(); 382 assertNotNull("No entry in the archive.", entry); 383 long entrySize = entry.getSize(); 384 assertTrue("Entry size was < 1", entrySize > 0); 385 int i = 0; 386 while (zis1.available() > 0) { 387 zis1.skip(1); 388 i++; 389 } 390 if (i != entrySize) { 391 fail("ZipInputStream.available or ZipInputStream.skip does not " + 392 "working properly. Only skipped " + i + 393 " bytes instead of " + entrySize); 394 } 395 assertEquals(0, zis1.skip(1)); 396 assertEquals(0, zis1.available()); 397 zis1.closeEntry(); 398 assertEquals(1, zis.available()); 399 zis1.close(); 400 try { 401 zis1.available(); 402 fail("IOException expected"); 403 } catch (IOException ee) { 404 // expected 405 } 406 } 407 408 class Mock_ZipInputStream extends ZipInputStream { 409 boolean createFlag = false; 410 411 public Mock_ZipInputStream(InputStream arg0) { 412 super(arg0); 413 } 414 415 boolean getCreateFlag() { 416 return createFlag; 417 } 418 419 protected ZipEntry createZipEntry(String name) { 420 createFlag = true; 421 return super.createZipEntry(name); 422 } 423 } 424 425 @TestTargetNew( 426 level = TestLevel.COMPLETE, 427 notes = "", 428 method = "createZipEntry", 429 args = {java.lang.String.class} 430 ) 431 public void test_createZipEntryLjava_lang_String() throws Exception { 432 433 File resources = Support_Resources.createTempFolder(); 434 Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); 435 File fl = new File(resources, "Broken_manifest.jar"); 436 FileInputStream fis = new FileInputStream(fl); 437 438 Mock_ZipInputStream zis1 = new Mock_ZipInputStream(fis); 439 assertFalse(zis1.getCreateFlag()); 440 zis1.getNextEntry(); 441 assertTrue(zis1.getCreateFlag()); 442 } 443} 444