1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.harmony.tests.java.util.zip; 18 19import java.io.ByteArrayInputStream; 20import java.io.ByteArrayOutputStream; 21import java.io.File; 22import java.io.FileInputStream; 23import java.io.FilterInputStream; 24import java.io.IOException; 25import java.io.InputStream; 26import java.util.zip.ZipEntry; 27import java.util.zip.ZipException; 28import java.util.zip.ZipInputStream; 29import java.util.zip.ZipOutputStream; 30 31import junit.framework.TestCase; 32import tests.support.resource.Support_Resources; 33 34public class ZipInputStreamTest extends TestCase { 35 // the file hyts_zipFile.zip used in setup needs to included as a resource 36 private ZipEntry zentry; 37 38 private ZipInputStream zis; 39 40 private byte[] zipBytes; 41 42 private byte[] dataBytes = "Some data in my file".getBytes(); 43 44 @Override 45 protected void setUp() { 46 try { 47 InputStream is = Support_Resources.getStream("hyts_ZipFile.zip"); 48 if (is == null) { 49 System.out.println("file hyts_ZipFile.zip can not be found"); 50 } 51 zis = new ZipInputStream(is); 52 53 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 54 ZipOutputStream zos = new ZipOutputStream(bos); 55 ZipEntry entry = new ZipEntry("myFile"); 56 zos.putNextEntry(entry); 57 zos.write(dataBytes); 58 zos.closeEntry(); 59 zos.close(); 60 zipBytes = bos.toByteArray(); 61 } catch (Exception e) { 62 System.out.println("Exception during ZipFile setup:"); 63 e.printStackTrace(); 64 } 65 } 66 67 @Override 68 protected void tearDown() { 69 if (zis != null) { 70 try { 71 zis.close(); 72 } catch (Exception e) { 73 } 74 } 75 } 76 77 /** 78 * java.util.zip.ZipInputStream#ZipInputStream(java.io.InputStream) 79 */ 80 public void test_ConstructorLjava_io_InputStream() throws Exception { 81 zentry = zis.getNextEntry(); 82 zis.closeEntry(); 83 } 84 85 /** 86 * java.util.zip.ZipInputStream#close() 87 */ 88 public void test_close() { 89 try { 90 zis.close(); 91 byte[] rbuf = new byte[10]; 92 zis.read(rbuf, 0, 1); 93 } catch (IOException e) { 94 return; 95 } 96 fail("Read data after stream was closed"); 97 } 98 99 /** 100 * java.util.zip.ZipInputStream#close() 101 */ 102 public void test_close2() throws Exception { 103 // Regression for HARMONY-1101 104 zis.close(); 105 // another call to close should NOT cause an exception 106 zis.close(); 107 } 108 109 /** 110 * java.util.zip.ZipInputStream#closeEntry() 111 */ 112 public void test_closeEntry() throws Exception { 113 zentry = zis.getNextEntry(); 114 zis.closeEntry(); 115 } 116 117 public void test_closeAfterException() throws Exception { 118 File resources = Support_Resources.createTempFolder(); 119 Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); 120 FileInputStream fis = new FileInputStream(new File(resources, 121 "Broken_manifest.jar")); 122 123 ZipInputStream zis1 = new ZipInputStream(fis); 124 125 try { 126 for (int i = 0; i < 6; i++) { 127 zis1.getNextEntry(); 128 } 129 fail("ZipException expected"); 130 } catch (ZipException ee) { 131 // expected 132 } 133 134 zis1.close(); 135 try { 136 zis1.getNextEntry(); 137 fail("IOException expected"); 138 } catch (IOException ee) { 139 // expected 140 } 141 } 142 143 /** 144 * java.util.zip.ZipInputStream#getNextEntry() 145 */ 146 public void test_getNextEntry() throws Exception { 147 assertNotNull("getNextEntry failed", zis.getNextEntry()); 148 } 149 150 /** 151 * java.util.zip.ZipInputStream#read(byte[], int, int) 152 */ 153 public void test_read$BII() throws Exception { 154 zentry = zis.getNextEntry(); 155 byte[] rbuf = new byte[(int) zentry.getSize()]; 156 int r = zis.read(rbuf, 0, rbuf.length); 157 new String(rbuf, 0, r); 158 assertEquals("Failed to read entry", 12, r); 159 } 160 161 public void testReadOneByteAtATime() throws IOException { 162 InputStream in = new FilterInputStream(Support_Resources.getStream("hyts_ZipFile.zip")) { 163 @Override 164 public int read(byte[] buffer, int offset, int count) throws IOException { 165 return super.read(buffer, offset, 1); // one byte at a time 166 } 167 168 @Override 169 public int read(byte[] buffer) throws IOException { 170 return super.read(buffer, 0, 1); // one byte at a time 171 } 172 }; 173 174 zis = new ZipInputStream(in); 175 while ((zentry = zis.getNextEntry()) != null) { 176 zentry.getName(); 177 } 178 zis.close(); 179 } 180 181 /** 182 * java.util.zip.ZipInputStream#skip(long) 183 */ 184 public void test_skipJ() throws Exception { 185 zentry = zis.getNextEntry(); 186 byte[] rbuf = new byte[(int) zentry.getSize()]; 187 zis.skip(2); 188 int r = zis.read(rbuf, 0, rbuf.length); 189 assertEquals("Failed to skip data", 10, r); 190 191 zentry = zis.getNextEntry(); 192 zentry = zis.getNextEntry(); 193 long s = zis.skip(1025); 194 assertEquals("invalid skip: " + s, 1025, s); 195 196 ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipBytes)); 197 zis.getNextEntry(); 198 long skipLen = dataBytes.length / 2; 199 assertEquals("Assert 0: failed valid skip", skipLen, zis.skip(skipLen)); 200 zis.skip(dataBytes.length); 201 assertEquals("Assert 1: performed invalid skip", 0, zis.skip(1)); 202 assertEquals("Assert 2: failed zero len skip", 0, zis.skip(0)); 203 try { 204 zis.skip(-1); 205 fail("Assert 3: Expected Illegal argument exception"); 206 } catch (IllegalArgumentException e) { 207 // Expected 208 } 209 } 210 211 public void test_available() throws Exception { 212 213 File resources = Support_Resources.createTempFolder(); 214 Support_Resources.copyFile(resources, null, "hyts_ZipFile.zip"); 215 File fl = new File(resources, "hyts_ZipFile.zip"); 216 FileInputStream fis = new FileInputStream(fl); 217 218 ZipInputStream zis1 = new ZipInputStream(fis); 219 ZipEntry entry = zis1.getNextEntry(); 220 assertNotNull("No entry in the archive.", entry); 221 long entrySize = entry.getSize(); 222 assertTrue("Entry size was < 1", entrySize > 0); 223 int i = 0; 224 while (zis1.available() > 0) { 225 zis1.skip(1); 226 i++; 227 } 228 if (i != entrySize) { 229 fail("ZipInputStream.available or ZipInputStream.skip does not " + 230 "working properly. Only skipped " + i + 231 " bytes instead of " + entrySize); 232 } 233 assertEquals(0, zis1.skip(1)); 234 assertEquals(0, zis1.available()); 235 zis1.closeEntry(); 236 assertEquals(1, zis.available()); 237 zis1.close(); 238 try { 239 zis1.available(); 240 fail("IOException expected"); 241 } catch (IOException ee) { 242 // expected 243 } 244 } 245} 246