blob: 11d744246854abff59461de78154d50dad4f2f7e [file] [log] [blame]
bristor5d6ce622008-09-11 14:58:57 -07001/*
ohairbf91ea12011-04-06 22:06:11 -07002 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
bristor5d6ce622008-09-11 14:58:57 -07003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
bristor5d6ce622008-09-11 14:58:57 -070022 */
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
30import java.io.*;
31import java.util.*;
32import java.util.zip.*;
33
34public 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
bristor5d6ce622008-09-11 14:58:57 -070042 f.createNewFile();
bristor5d6ce622008-09-11 14:58:57 -070043 try {
alanb92d5cf92008-11-11 08:59:43 +000044 // Verify 0-length file cannot be read
45 checkCannotRead(f);
bristor5d6ce622008-09-11 14:58:57 -070046
alanb92d5cf92008-11-11 08:59:43 +000047 // 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();
bristor5d6ce622008-09-11 14:58:57 -070053 }
alanb92d5cf92008-11-11 08:59:43 +000054 checkCannotRead(f);
bristor5d6ce622008-09-11 14:58:57 -070055
alanb92d5cf92008-11-11 08:59:43 +000056 } finally {
57 f.delete();
58 }
bristor5d6ce622008-09-11 14:58:57 -070059
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
alanb92d5cf92008-11-11 08:59:43 +000070 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 }
smarkse4682702011-02-25 02:06:10 -080081 try (FileInputStream fis = new FileInputStream(f);
82 ZipInputStream zis = new ZipInputStream(fis))
83 {
alanb92d5cf92008-11-11 08:59:43 +000084 ZipEntry ze = zis.getNextEntry();
85 check(ze == null);
86 } catch (IOException ex) {
87 unexpected(ex);
alanb92d5cf92008-11-11 08:59:43 +000088 }
89 }
90
bristor5d6ce622008-09-11 14:58:57 -070091 static void write(File f) throws Exception {
smarkse4682702011-02-25 02:06:10 -080092 try (FileOutputStream fis = new FileOutputStream(f);
93 ZipOutputStream zos = new ZipOutputStream(fis))
94 {
bristor5d6ce622008-09-11 14:58:57 -070095 zos.finish();
bristor5d6ce622008-09-11 14:58:57 -070096 pass();
97 } catch (Exception ex) {
98 unexpected(ex);
bristor5d6ce622008-09-11 14:58:57 -070099 }
100 }
101
102 static void readFile(File f) throws Exception {
smarkse4682702011-02-25 02:06:10 -0800103 try (ZipFile zf = new ZipFile(f)) {
bristor5d6ce622008-09-11 14:58:57 -0700104
105 Enumeration e = zf.entries();
106 while (e.hasMoreElements()) {
107 ZipEntry entry = (ZipEntry) e.nextElement();
108 fail();
109 }
bristor5d6ce622008-09-11 14:58:57 -0700110 pass();
111 } catch (Exception ex) {
112 unexpected(ex);
bristor5d6ce622008-09-11 14:58:57 -0700113 }
114 }
115
116 static void readStream(File f) throws Exception {
smarkse4682702011-02-25 02:06:10 -0800117 try (FileInputStream fis = new FileInputStream(f);
118 ZipInputStream zis = new ZipInputStream(fis))
119 {
bristor5d6ce622008-09-11 14:58:57 -0700120 ZipEntry ze = zis.getNextEntry();
121 check(ze == null);
122 byte[] buf = new byte[1024];
123 check(zis.read(buf, 0, 1024) == -1);
bristor5d6ce622008-09-11 14:58:57 -0700124 }
125 }
126
127 //--------------------- Infrastructure ---------------------------
128 static volatile int passed = 0, failed = 0;
129 static boolean pass() {passed++; return true;}
130 static boolean fail() {failed++; Thread.dumpStack(); return false;}
131 static boolean fail(String msg) {System.out.println(msg); return fail();}
132 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
133 static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
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);}
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}