|
1 /* |
|
2 * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any 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 { |
|
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 // Verify 0-length file cannot be read |
|
43 f.createNewFile(); |
|
44 ZipFile zf = null; |
|
45 try { |
|
46 zf = new ZipFile(f); |
|
47 fail(); |
|
48 } catch (Exception ex) { |
|
49 check(ex.getMessage().contains("zip file is empty")); |
|
50 } finally { |
|
51 if (zf != null) { |
|
52 zf.close(); |
|
53 } |
|
54 } |
|
55 |
|
56 ZipInputStream zis = null; |
|
57 try { |
|
58 zis = new ZipInputStream(new FileInputStream(f)); |
|
59 ZipEntry ze = zis.getNextEntry(); |
|
60 check(ze == null); |
|
61 } catch (Exception ex) { |
|
62 unexpected(ex); |
|
63 } finally { |
|
64 if (zis != null) { |
|
65 zis.close(); |
|
66 } |
|
67 } |
|
68 |
|
69 f.delete(); |
|
70 |
|
71 // Verify 0-entries file can be written |
|
72 write(f); |
|
73 |
|
74 // Verify 0-entries file can be read |
|
75 readFile(f); |
|
76 readStream(f); |
|
77 |
|
78 f.delete(); |
|
79 } |
|
80 |
|
81 static void write(File f) throws Exception { |
|
82 ZipOutputStream zos = null; |
|
83 try { |
|
84 zos = new ZipOutputStream(new FileOutputStream(f)); |
|
85 zos.finish(); |
|
86 zos.close(); |
|
87 pass(); |
|
88 } catch (Exception ex) { |
|
89 unexpected(ex); |
|
90 } finally { |
|
91 if (zos != null) { |
|
92 zos.close(); |
|
93 } |
|
94 } |
|
95 } |
|
96 |
|
97 static void readFile(File f) throws Exception { |
|
98 ZipFile zf = null; |
|
99 try { |
|
100 zf = new ZipFile(f); |
|
101 |
|
102 Enumeration e = zf.entries(); |
|
103 while (e.hasMoreElements()) { |
|
104 ZipEntry entry = (ZipEntry) e.nextElement(); |
|
105 fail(); |
|
106 } |
|
107 zf.close(); |
|
108 pass(); |
|
109 } catch (Exception ex) { |
|
110 unexpected(ex); |
|
111 } finally { |
|
112 if (zf != null) { |
|
113 zf.close(); |
|
114 } |
|
115 } |
|
116 } |
|
117 |
|
118 static void readStream(File f) throws Exception { |
|
119 ZipInputStream zis = null; |
|
120 try { |
|
121 zis = new ZipInputStream(new FileInputStream(f)); |
|
122 ZipEntry ze = zis.getNextEntry(); |
|
123 check(ze == null); |
|
124 byte[] buf = new byte[1024]; |
|
125 check(zis.read(buf, 0, 1024) == -1); |
|
126 } finally { |
|
127 if (zis != null) { |
|
128 zis.close(); |
|
129 } |
|
130 } |
|
131 } |
|
132 |
|
133 //--------------------- Infrastructure --------------------------- |
|
134 static volatile int passed = 0, failed = 0; |
|
135 static boolean pass() {passed++; return true;} |
|
136 static boolean fail() {failed++; Thread.dumpStack(); return false;} |
|
137 static boolean fail(String msg) {System.out.println(msg); return fail();} |
|
138 static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
139 static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;} |
|
140 static boolean equal(Object x, Object y) { |
|
141 if (x == null ? y == null : x.equals(y)) return pass(); |
|
142 else return fail(x + " not equal to " + y);} |
|
143 public static void main(String[] args) throws Throwable { |
|
144 try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
145 System.out.println("\nPassed = " + passed + " failed = " + failed); |
|
146 if (failed > 0) throw new AssertionError("Some tests failed");} |
|
147 } |