author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8553 | 46c2babb1e44 |
permissions | -rw-r--r-- |
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8553
diff
changeset
|
2 |
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. |
1228
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 |
* |
5506 | 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. |
|
1228
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 |
f.createNewFile(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
43 |
try { |
1572
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
44 |
// Verify 0-length file cannot be read |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
45 |
checkCannotRead(f); |
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
46 |
|
1572
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
47 |
// Verify non-zip file cannot be read |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
48 |
OutputStream out = new FileOutputStream(f); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
49 |
try { |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
50 |
out.write("class Foo { }".getBytes()); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
51 |
} finally { |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
52 |
out.close(); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
53 |
} |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
54 |
checkCannotRead(f); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
55 |
|
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
56 |
} finally { |
1572
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
57 |
f.delete(); |
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
58 |
} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
59 |
|
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
60 |
// Verify 0-entries file can be written |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
61 |
write(f); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
62 |
|
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
63 |
// Verify 0-entries file can be read |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
64 |
readFile(f); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
65 |
readStream(f); |
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 |
f.delete(); |
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 |
|
1572
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
70 |
static void checkCannotRead(File f) throws IOException { |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
71 |
try { |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
72 |
new ZipFile(f).close(); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
73 |
fail(); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
74 |
} catch (ZipException ze) { |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
75 |
if (f.length() == 0) { |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
76 |
check(ze.getMessage().contains("zip file is empty")); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
77 |
} else { |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
78 |
pass(); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
79 |
} |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
80 |
} |
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
81 |
try (FileInputStream fis = new FileInputStream(f); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
82 |
ZipInputStream zis = new ZipInputStream(fis)) |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
83 |
{ |
1572
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
84 |
ZipEntry ze = zis.getNextEntry(); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
85 |
check(ze == null); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
86 |
} catch (IOException ex) { |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
87 |
unexpected(ex); |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
88 |
} |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
89 |
} |
9fed7b18f517
6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents:
1228
diff
changeset
|
90 |
|
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
91 |
static void write(File f) throws Exception { |
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
92 |
try (FileOutputStream fis = new FileOutputStream(f); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
93 |
ZipOutputStream zos = new ZipOutputStream(fis)) |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
94 |
{ |
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
95 |
zos.finish(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
96 |
pass(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
97 |
} catch (Exception ex) { |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
98 |
unexpected(ex); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
99 |
} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
100 |
} |
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 |
static void readFile(File f) throws Exception { |
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
103 |
try (ZipFile zf = new ZipFile(f)) { |
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
104 |
|
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
105 |
Enumeration e = zf.entries(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
106 |
while (e.hasMoreElements()) { |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
107 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
108 |
fail(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
109 |
} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
110 |
pass(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
111 |
} catch (Exception ex) { |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
112 |
unexpected(ex); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
113 |
} |
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 |
static void readStream(File f) throws Exception { |
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
117 |
try (FileInputStream fis = new FileInputStream(f); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
118 |
ZipInputStream zis = new ZipInputStream(fis)) |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
5506
diff
changeset
|
119 |
{ |
1228
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
120 |
ZipEntry ze = zis.getNextEntry(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
121 |
check(ze == null); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
122 |
byte[] buf = new byte[1024]; |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
123 |
check(zis.read(buf, 0, 1024) == -1); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
124 |
} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
125 |
} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
126 |
|
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
127 |
//--------------------- Infrastructure --------------------------- |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
128 |
static volatile int passed = 0, failed = 0; |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
129 |
static boolean pass() {passed++; return true;} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
130 |
static boolean fail() {failed++; Thread.dumpStack(); return false;} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
131 |
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
|
132 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
133 |
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
|
134 |
static boolean equal(Object x, Object y) { |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
135 |
if (x == null ? y == null : x.equals(y)) return pass(); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
136 |
else return fail(x + " not equal to " + y);} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
137 |
public static void main(String[] args) throws Throwable { |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
138 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
139 |
System.out.println("\nPassed = " + passed + " failed = " + failed); |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
140 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
1515928f48cd
6440786: Cannot create a ZIP file containing zero entries
bristor
parents:
diff
changeset
|
141 |
} |