1 /* 2 * Copyright (c) 2006, Oracle and/or its affiliates. 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 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. 22 */ 23 24 /** 25 * @test 26 * @bug 4615343 27 * @summary Check that ZipError is thrown instead of InternalError when 28 * iterating entries of an invalid zip file 29 */ 30 31 import java.io.*; 32 import java.util.*; 33 import java.util.zip.*; 34 35 public class TestZipError { realMain(String[] args)36 public static void realMain(String[] args) throws Throwable { 37 // Causing a ZipError is hard, especially on non-Windows systems. See 38 // comments below. 39 String osName = System.getProperty("os.name"); 40 if (!System.getProperty("os.name").startsWith("Windows")) { 41 return; 42 } 43 44 String fileName = "error4615343.zip"; 45 File f = new File(fileName); 46 f.delete(); 47 ZipOutputStream zos; 48 ZipEntry ze; 49 50 // Create a zip file with two entries. 51 zos = new ZipOutputStream(new FileOutputStream(f)); 52 ze = new ZipEntry("one"); 53 zos.putNextEntry(ze); 54 zos.write("hello".getBytes()); 55 zos.closeEntry(); 56 ze = new ZipEntry("two"); 57 zos.putNextEntry(ze); 58 zos.write("world".getBytes()); 59 zos.closeEntry(); 60 zos.close(); 61 62 // Open the ZipFile. This will read the zip file's central 63 // directory into in-memory data structures. 64 ZipFile zf = new ZipFile(fileName); 65 66 // Delete the file; of course this does not change the in-memory data 67 // structures that represent the central directory! 68 f.delete(); 69 70 // Re-create zip file, with different entries than earlier. However, 71 // recall that we have in-memory information about the central 72 // directory of the file at its previous state. 73 zos = new ZipOutputStream(new FileOutputStream(f)); 74 ze = new ZipEntry("uno"); 75 zos.putNextEntry(ze); 76 zos.write("hola".getBytes()); 77 zos.closeEntry(); 78 zos.close(); 79 80 // Iterate zip file's contents. On Windows, this will result in a 81 // ZipError, because the data in the file differs from the in-memory 82 // central directory information we read earlier. 83 Enumeration<? extends ZipEntry> entries = zf.entries(); 84 try { 85 while (entries.hasMoreElements()) { 86 ze = entries.nextElement(); 87 } 88 fail("Did not get expected exception"); 89 } catch (ZipError e) { 90 pass(); 91 } catch (InternalError e) { 92 fail("Caught InternalError instead of expected ZipError"); 93 } catch (Throwable t) { 94 unexpected(t); 95 } finally { 96 zf.close(); 97 f.delete(); 98 } 99 } 100 101 //--------------------- Infrastructure --------------------------- 102 static volatile int passed = 0, failed = 0; pass()103 static void pass() {passed++;} fail()104 static void fail() {failed++; Thread.dumpStack();} fail(String msg)105 static void fail(String msg) {System.out.println(msg); fail();} unexpected(Throwable t)106 static void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)107 static void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)108 static void equal(Object x, Object y) { 109 if (x == null ? y == null : x.equals(y)) pass(); 110 else fail(x + " not equal to " + y);} main(String[] args)111 public static void main(String[] args) throws Throwable { 112 try {realMain(args);} catch (Throwable t) {unexpected(t);} 113 System.out.println("\nPassed = " + passed + " failed = " + failed); 114 if (failed > 0) throw new AssertionError("Some tests failed");} 115 } 116