jdk/test/java/util/zip/TestZipError.java
author sherman
Fri, 03 Aug 2012 13:40:03 -0700
changeset 13409 fe3ab27ef1a6
parent 5506 202f599c92aa
child 34686 29ea8310a27a
permissions -rw-r--r--
7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native Summary: re-implemented getBytesRead/Writtten() at java level Reviewed-by: andrew, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4615343
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that ZipError is thrown instead of InternalError when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * iterating entries of an invalid zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class TestZipError {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        // Causing a ZipError is hard, especially on non-Windows systems.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        // comments below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        if (!System.getProperty("os.name").startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        String fileName = "error4615343.zip";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        File f = new File(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        ZipOutputStream zos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        ZipEntry ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // Create a zip file with two entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        zos = new ZipOutputStream(new FileOutputStream(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ze = new ZipEntry("one");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        zos.putNextEntry(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        zos.write("hello".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        zos.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ze = new ZipEntry("two");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        zos.putNextEntry(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        zos.write("world".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        zos.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // Open the ZipFile.  This will read the zip file's central
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // directory into in-memory data structures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        ZipFile zf = new ZipFile(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Delete the file; of course this does not change the in-memory data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // structures that represent the central directory!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // Re-create zip file, with different entries than earlier.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // recall that we have in-memory information about the central
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // directory of the file at its previous state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        zos = new ZipOutputStream(new FileOutputStream(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ze = new ZipEntry("uno");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        zos.putNextEntry(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        zos.write("hola".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        zos.closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Iterate zip file's contents.  On Windows, this will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // ZipError, because the data in the file differs from the in-memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // central directory information we read earlier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Enumeration<? extends ZipEntry> entries = zf.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            while (entries.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                ze = entries.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            fail("Did not get expected exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (ZipError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch (InternalError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            fail("Caught InternalError instead of expected ZipError");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            zf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.out.println("\nPassed = " + passed + " failed = " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}