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.tests.java.util.zip;
19
20import java.io.File;
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.OutputStream;
25import java.util.Enumeration;
26import java.util.zip.ZipEntry;
27import java.util.zip.ZipException;
28import java.util.zip.ZipFile;
29import libcore.io.Streams;
30import libcore.java.lang.ref.FinalizationTester;
31import tests.support.resource.Support_Resources;
32
33public class ZipFileTest extends junit.framework.TestCase {
34
35    // the file hyts_zipFile.zip in setup must be included as a resource
36    private String tempFileName;
37    private ZipFile zfile;
38
39    /**
40     * java.util.zip.ZipFile#ZipFile(java.io.File, int)
41     */
42    public void test_ConstructorLjava_io_FileI() throws IOException {
43        zfile.close(); // about to reopen the same temp file
44
45        File file = new File(tempFileName);
46        ZipFile zip = new ZipFile(file, ZipFile.OPEN_DELETE | ZipFile.OPEN_READ);
47        zip.close();
48        assertTrue("Zip should not exist", !file.exists());
49
50        file = new File(tempFileName);
51        try {
52            zip = new ZipFile(file, ZipFile.OPEN_READ);
53            fail("IOException expected");
54        } catch (IOException ee) {
55            // expected
56        }
57        file = new File(tempFileName);
58        try {
59            zip = new ZipFile(file, -1);
60            fail("IllegalArgumentException expected");
61        } catch (IllegalArgumentException ee) {
62            // expected
63        }
64    }
65
66    /**
67     * @throws IOException
68     * java.util.zip.ZipFile#ZipFile(java.lang.String)
69     */
70    public void test_ConstructorLjava_lang_String() throws IOException {
71        zfile.close(); // about to reopen the same temp file
72        ZipFile zip = new ZipFile(tempFileName);
73        zip.close();
74        File file = File.createTempFile("zip", "tmp");
75        try {
76            zip = new ZipFile(file.getAbsolutePath());
77            fail("ZipException expected");
78        } catch (ZipException ee) {
79            // expected
80        }
81        file.delete();
82    }
83
84    protected ZipEntry test_finalize1(ZipFile zip) {
85        return zip.getEntry("File1.txt");
86    }
87
88    protected ZipFile test_finalize2(File file) throws IOException {
89        return new ZipFile(file);
90    }
91
92    /**
93     * java.util.zip.ZipFile#finalize()
94     */
95    public void test_finalize() throws IOException {
96        InputStream in = Support_Resources.getStream("hyts_ZipFile.zip");
97        File file = Support_Resources.createTempFile(".jar");
98        OutputStream out = new FileOutputStream(file);
99        int result;
100        byte[] buf = new byte[4096];
101        while ((result = in.read(buf)) != -1) {
102            out.write(buf, 0, result);
103        }
104        in.close();
105        out.close();
106        /*
107         * ZipFile zip = new ZipFile(file); ZipEntry entry1 =
108         * zip.getEntry("File1.txt"); assertNotNull("Did not find entry",
109         * entry1); entry1 = null; zip = null;
110         */
111
112        assertNotNull("Did not find entry", test_finalize1(test_finalize2(file)));
113        FinalizationTester.induceFinalization();
114        file.delete();
115        assertTrue("Zip should not exist", !file.exists());
116    }
117
118    /**
119     * @throws IOException
120     * java.util.zip.ZipFile#close()
121     */
122    public void test_close() throws IOException {
123        // Test for method void java.util.zip.ZipFile.close()
124        File fl = new File(tempFileName);
125        ZipFile zf = new ZipFile(fl);
126        InputStream is1 = zf.getInputStream(zf.getEntry("File1.txt"));
127        InputStream is2 = zf.getInputStream(zf.getEntry("File2.txt"));
128
129        is1.read();
130        is2.read();
131
132        zf.close();
133
134        try {
135            is1.read();
136            fail("IOException expected");
137        } catch (IOException ee) {
138            // expected
139        }
140
141        try {
142            is2.read();
143            fail("IOException expected");
144        } catch (IOException ee) {
145            // expected
146        }
147    }
148
149    /**
150     * java.util.zip.ZipFile#entries()
151     */
152    public void test_entries() throws Exception {
153        // Test for method java.util.Enumeration java.util.zip.ZipFile.entries()
154        Enumeration<? extends ZipEntry> enumer = zfile.entries();
155        int c = 0;
156        while (enumer.hasMoreElements()) {
157            ++c;
158            enumer.nextElement();
159        }
160        assertTrue("Incorrect number of entries returned: " + c, c == 6);
161
162        Enumeration<? extends ZipEntry> enumeration = zfile.entries();
163        zfile.close();
164        try {
165            enumeration.nextElement();
166            fail("did not detect closed file");
167        } catch (IllegalStateException expected) {
168        }
169
170        try {
171            enumeration.hasMoreElements();
172            fail("did not detect closed file");
173        } catch (IllegalStateException expected) {
174        }
175
176        try {
177            zfile.entries();
178            fail("did not detect closed file");
179        } catch (IllegalStateException expected) {
180        }
181    }
182
183    /**
184     * java.util.zip.ZipFile#getEntry(java.lang.String)
185     */
186    public void test_getEntryLjava_lang_String() throws IOException {
187        // Test for method java.util.zip.ZipEntry
188        // java.util.zip.ZipFile.getEntry(java.lang.String)
189        java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt");
190        assertNotNull("Could not obtain ZipEntry", zentry);
191        int r;
192        InputStream in;
193
194        zentry = zfile.getEntry("testdir1/File1.txt");
195        assertNotNull("Could not obtain ZipEntry: testdir1/File1.txt", zentry);
196        zentry = zfile.getEntry("testdir1/");
197        assertNotNull("Could not obtain ZipEntry: testdir1/", zentry);
198        in = zfile.getInputStream(zentry);
199        assertNotNull("testdir1/ should not have null input stream", in);
200        r = in.read();
201        in.close();
202        assertEquals("testdir1/ should not contain data", -1, r);
203
204        zentry = zfile.getEntry("testdir1/testdir1");
205        assertNotNull("Could not obtain ZipEntry: testdir1/testdir1", zentry);
206        in = zfile.getInputStream(zentry);
207        byte[] buf = new byte[256];
208        r = in.read(buf);
209        in.close();
210        assertEquals("incorrect contents", "This is also text", new String(buf,
211                0, r));
212    }
213
214    public void test_getEntryLjava_lang_String_AndroidOnly() throws IOException {
215        java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt");
216        assertNotNull("Could not obtain ZipEntry", zentry);
217        int r;
218        InputStream in;
219
220        zentry = zfile.getEntry("testdir1");
221        assertNotNull("Must be able to obtain ZipEntry: testdir1", zentry);
222        in = zfile.getInputStream(zentry);
223        /*
224         * Android delivers empty InputStream, RI no InputStream at all. The
225         * spec doesn't clarify this, so we need to deal with both situations.
226         */
227        int data = -1;
228        if (in != null) {
229            data = in.read();
230            in.close();
231        }
232        assertEquals("Must not be able to read directory data", -1, data);
233    }
234
235    public void test_getEntryLjava_lang_String_Ex() throws IOException {
236        java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt");
237        assertNotNull("Could not obtain ZipEntry", zentry);
238
239        zfile.close();
240        try {
241            zfile.getEntry("File2.txt");
242            fail("IllegalStateException expected");
243        } catch (IllegalStateException ee) {
244        }
245    }
246
247    /**
248     * @throws IOException
249     * java.util.zip.ZipFile#getInputStream(java.util.zip.ZipEntry)
250     */
251    public void test_getInputStreamLjava_util_zip_ZipEntry() throws IOException {
252        // Test for method java.io.InputStream
253        // java.util.zip.ZipFile.getInputStream(java.util.zip.ZipEntry)
254        ZipEntry zentry = null;
255        InputStream is = null;
256        try {
257            zentry = zfile.getEntry("File1.txt");
258            is = zfile.getInputStream(zentry);
259            byte[] rbuf = new byte[1000];
260            int r;
261            is.read(rbuf, 0, r = (int) zentry.getSize());
262            assertEquals("getInputStream read incorrect data", "This is text",
263                    new String(rbuf, 0, r));
264        } catch (java.io.IOException e) {
265            fail("IOException during getInputStream");
266        } finally {
267            try {
268                is.close();
269            } catch (java.io.IOException e) {
270                fail("Failed to close input stream");
271            }
272        }
273
274        zentry = zfile.getEntry("File2.txt");
275        zfile.close();
276        try {
277            is = zfile.getInputStream(zentry);
278            fail("IllegalStateException expected");
279        } catch (IllegalStateException ee) {
280            // expected
281        }
282
283        // ZipException can not be checked. Stream object returned or null.
284    }
285
286    /**
287     * java.util.zip.ZipFile#getName()
288     */
289    public void test_getName() {
290        // Test for method java.lang.String java.util.zip.ZipFile.getName()
291        assertTrue("Returned incorrect name: " + zfile.getName(), zfile
292                .getName().equals(tempFileName));
293    }
294
295    /**
296     * @throws IOException
297     * java.util.zip.ZipFile#size()
298     */
299    public void test_size() throws IOException {
300        assertEquals(6, zfile.size());
301        zfile.close();
302        try {
303            zfile.size();
304            fail("IllegalStateException expected");
305        } catch (IllegalStateException expected) {
306        }
307    }
308
309    /**
310     * java.io.InputStream#reset()
311     */
312    public void test_reset() throws IOException {
313        // read an uncompressed entry
314        ZipEntry zentry = zfile.getEntry("File1.txt");
315        InputStream is = zfile.getInputStream(zentry);
316        byte[] rbuf1 = new byte[6];
317        byte[] rbuf2 = new byte[6];
318        int r1, r2;
319        r1 = is.read(rbuf1);
320        assertEquals(rbuf1.length, r1);
321        r2 = is.read(rbuf2);
322        assertEquals(rbuf2.length, r2);
323
324        try {
325            is.reset();
326            fail();
327        } catch (IOException expected) {
328        }
329        is.close();
330
331        // read a compressed entry
332        byte[] rbuf3 = new byte[4185];
333        ZipEntry zentry2 = zfile.getEntry("File3.txt");
334        is = zfile.getInputStream(zentry2);
335        r1 = is.read(rbuf3);
336        assertEquals(4183, r1);
337        try {
338            is.reset();
339            fail();
340        } catch (IOException expected) {
341        }
342        is.close();
343
344        is = zfile.getInputStream(zentry2);
345        r1 = is.read(rbuf3, 0, 3000);
346        assertEquals(3000, r1);
347        try {
348            is.reset();
349            fail();
350        } catch (IOException expected) {
351        }
352        is.close();
353    }
354
355    /**
356     * java.io.InputStream#reset()
357     */
358    public void test_reset_subtest0() throws IOException {
359        // read an uncompressed entry
360        ZipEntry zentry = zfile.getEntry("File1.txt");
361        InputStream is = zfile.getInputStream(zentry);
362        byte[] rbuf1 = new byte[12];
363        byte[] rbuf2 = new byte[12];
364        int r = is.read(rbuf1, 0, 4);
365        assertEquals(4, r);
366        is.mark(0);
367        r = is.read(rbuf1);
368        assertEquals(8, r);
369        assertEquals(-1, is.read());
370
371        try {
372            is.reset();
373            fail();
374        } catch (IOException expected) {
375        }
376
377        is.close();
378
379        // read a compressed entry
380        byte[] rbuf3 = new byte[4185];
381        ZipEntry zentry2 = zfile.getEntry("File3.txt");
382        is = zfile.getInputStream(zentry2);
383        r = is.read(rbuf3, 0, 3000);
384        assertEquals(3000, r);
385        is.mark(0);
386        r = is.read(rbuf3);
387        assertEquals(1183, r);
388        assertEquals(-1, is.read());
389
390        try {
391            is.reset();
392            fail();
393        } catch (IOException expected) {
394        }
395
396        is.close();
397    }
398
399    @Override
400    protected void setUp() throws IOException {
401        // Create a local copy of the file since some tests want to alter information.
402        File tempFile = File.createTempFile("OldZipFileTest", "zip");
403        tempFileName = tempFile.getAbsolutePath();
404
405
406        InputStream is = Support_Resources.getStream("hyts_ZipFile.zip");
407        FileOutputStream fos = new FileOutputStream(tempFile);
408        Streams.copy(is, fos);
409
410        is.close();
411        fos.close();
412        zfile = new ZipFile(tempFile);
413    }
414
415    @Override
416    protected void tearDown() throws IOException {
417        if (zfile != null) {
418            zfile.close();
419        }
420    }
421}
422