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