bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 1 | /* |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 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. |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 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 | |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 42 | f.createNewFile(); |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 43 | try { |
alanb | 92d5cf9 | 2008-11-11 08:59:43 +0000 | [diff] [blame] | 44 | // Verify 0-length file cannot be read |
| 45 | checkCannotRead(f); |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 46 | |
alanb | 92d5cf9 | 2008-11-11 08:59:43 +0000 | [diff] [blame] | 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(); |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 53 | } |
alanb | 92d5cf9 | 2008-11-11 08:59:43 +0000 | [diff] [blame] | 54 | checkCannotRead(f); |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 55 | |
alanb | 92d5cf9 | 2008-11-11 08:59:43 +0000 | [diff] [blame] | 56 | } finally { |
| 57 | f.delete(); |
| 58 | } |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 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 | |
alanb | 92d5cf9 | 2008-11-11 08:59:43 +0000 | [diff] [blame] | 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 | ZipInputStream zis = null; |
| 82 | try { |
| 83 | zis = new ZipInputStream(new FileInputStream(f)); |
| 84 | ZipEntry ze = zis.getNextEntry(); |
| 85 | check(ze == null); |
| 86 | } catch (IOException ex) { |
| 87 | unexpected(ex); |
| 88 | } finally { |
| 89 | if (zis != null) zis.close(); |
| 90 | } |
| 91 | } |
| 92 | |
bristor | 5d6ce62 | 2008-09-11 14:58:57 -0700 | [diff] [blame] | 93 | static void write(File f) throws Exception { |
| 94 | ZipOutputStream zos = null; |
| 95 | try { |
| 96 | zos = new ZipOutputStream(new FileOutputStream(f)); |
| 97 | zos.finish(); |
| 98 | zos.close(); |
| 99 | pass(); |
| 100 | } catch (Exception ex) { |
| 101 | unexpected(ex); |
| 102 | } finally { |
| 103 | if (zos != null) { |
| 104 | zos.close(); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void readFile(File f) throws Exception { |
| 110 | ZipFile zf = null; |
| 111 | try { |
| 112 | zf = new ZipFile(f); |
| 113 | |
| 114 | Enumeration e = zf.entries(); |
| 115 | while (e.hasMoreElements()) { |
| 116 | ZipEntry entry = (ZipEntry) e.nextElement(); |
| 117 | fail(); |
| 118 | } |
| 119 | zf.close(); |
| 120 | pass(); |
| 121 | } catch (Exception ex) { |
| 122 | unexpected(ex); |
| 123 | } finally { |
| 124 | if (zf != null) { |
| 125 | zf.close(); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static void readStream(File f) throws Exception { |
| 131 | ZipInputStream zis = null; |
| 132 | try { |
| 133 | zis = new ZipInputStream(new FileInputStream(f)); |
| 134 | ZipEntry ze = zis.getNextEntry(); |
| 135 | check(ze == null); |
| 136 | byte[] buf = new byte[1024]; |
| 137 | check(zis.read(buf, 0, 1024) == -1); |
| 138 | } finally { |
| 139 | if (zis != null) { |
| 140 | zis.close(); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | //--------------------- Infrastructure --------------------------- |
| 146 | static volatile int passed = 0, failed = 0; |
| 147 | static boolean pass() {passed++; return true;} |
| 148 | static boolean fail() {failed++; Thread.dumpStack(); return false;} |
| 149 | static boolean fail(String msg) {System.out.println(msg); return fail();} |
| 150 | static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 151 | static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;} |
| 152 | static boolean equal(Object x, Object y) { |
| 153 | if (x == null ? y == null : x.equals(y)) return pass(); |
| 154 | else return fail(x + " not equal to " + y);} |
| 155 | public static void main(String[] args) throws Throwable { |
| 156 | try {realMain(args);} catch (Throwable t) {unexpected(t);} |
| 157 | System.out.println("\nPassed = " + passed + " failed = " + failed); |
| 158 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 159 | } |