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