1 /*
2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /* @test
25  * @bug 6334003 6440786
26  * @summary Test ability to write and read zip files that have no entries.
27  * @author Dave Bristor
28  */
29 
30 import java.io.*;
31 import java.util.*;
32 import java.util.zip.*;
33 
34 public class TestEmptyZip {
realMain(String[] args)35     public static void realMain(String[] args) throws Throwable {
36         String zipName = "foo.zip";
37         File f = new File(System.getProperty("test.scratch", "."), zipName);
38         if (f.exists() && !f.delete()) {
39             throw new Exception("failed to delete " + zipName);
40         }
41 
42         f.createNewFile();
43         try {
44             // Verify 0-length file cannot be read
45             checkCannotRead(f);
46 
47             // Verify non-zip file cannot be read
48             OutputStream out = new FileOutputStream(f);
49             try {
50                 out.write("class Foo { }".getBytes());
51             } finally {
52                 out.close();
53             }
54             checkCannotRead(f);
55 
56         } finally {
57             f.delete();
58         }
59 
60         // Verify 0-entries file can be written
61         write(f);
62 
63         // Verify 0-entries file can be read
64         readFile(f);
65         readStream(f);
66 
67         f.delete();
68     }
69 
checkCannotRead(File f)70     static void checkCannotRead(File f) throws IOException {
71         try {
72             new ZipFile(f).close();
73             fail();
74         } catch (ZipException ze) {
75             if (f.length() == 0) {
76                 check(ze.getMessage().contains("zip file is empty"));
77             } else {
78                 pass();
79             }
80         }
81         try (FileInputStream fis = new FileInputStream(f);
82              ZipInputStream zis = new ZipInputStream(fis))
83         {
84             ZipEntry ze = zis.getNextEntry();
85             check(ze == null);
86         } catch (IOException ex) {
87             unexpected(ex);
88         }
89     }
90 
write(File f)91     static void write(File f) throws Exception {
92         try (FileOutputStream fis = new FileOutputStream(f);
93              ZipOutputStream zos = new ZipOutputStream(fis))
94         {
95             zos.finish();
96             pass();
97         } catch (Exception ex) {
98             unexpected(ex);
99         }
100     }
101 
readFile(File f)102     static void readFile(File f) throws Exception {
103         try (ZipFile zf = new ZipFile(f)) {
104 
105             Enumeration e = zf.entries();
106             while (e.hasMoreElements()) {
107                 ZipEntry entry = (ZipEntry) e.nextElement();
108                 fail();
109             }
110             pass();
111         } catch (Exception ex) {
112             unexpected(ex);
113         }
114     }
115 
readStream(File f)116     static void readStream(File f) throws Exception {
117         try (FileInputStream fis = new FileInputStream(f);
118              ZipInputStream zis = new ZipInputStream(fis))
119         {
120             ZipEntry ze = zis.getNextEntry();
121             check(ze == null);
122             byte[] buf = new byte[1024];
123             check(zis.read(buf, 0, 1024) == -1);
124         }
125     }
126 
127     //--------------------- Infrastructure ---------------------------
128     static volatile int passed = 0, failed = 0;
pass()129     static boolean pass() {passed++; return true;}
fail()130     static boolean fail() {failed++; Thread.dumpStack(); return false;}
fail(String msg)131     static boolean fail(String msg) {System.out.println(msg); return fail();}
unexpected(Throwable t)132     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)133     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
equal(Object x, Object y)134     static boolean equal(Object x, Object y) {
135         if (x == null ? y == null : x.equals(y)) return pass();
136         else return fail(x + " not equal to " + y);}
main(String[] args)137     public static void main(String[] args) throws Throwable {
138         try {realMain(args);} catch (Throwable t) {unexpected(t);}
139         System.out.println("\nPassed = " + passed + " failed = " + failed);
140         if (failed > 0) throw new AssertionError("Some tests failed");}
141 }
142