jdk/test/java/util/zip/DataDescriptor.java
author michaelm
Fri, 02 Oct 2009 13:57:41 +0100
changeset 3952 dc329398de30
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 6334171
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test that zip file's data descriptor is written correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Bug 6252735 ZipEntry contains wasteful "temporary storage" fields introduced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * a regression.  The value of the general purpose flag bit in a LOC header is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * written incorrectly, because it is computed on stale data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * With the bug present, zipbytes2 is written incorrectly: when the LOC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * header is written, (flag(e) & 8) == 8.  This is correct: the data will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * compressed, so we don't have data length, etc. yet; that should be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * in the DataDescriptor after the data itself is written.  However, when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * ZipOutputStream that wraps zipbytes2 is closed, the data length _is_
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * available, therefore (flag(e) & 8) = 0), therefore the DataDescriptor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * not written.  This is why, with the bug, zipbytes1.length == sizeof(ext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * header) + zipbytes2.length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The result is an invalid LOC header in zipbytes2.  So when we again use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * copyZip, we attempt to read a data length not from the LOC header but from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the non-existent EXT header, and at that position in the file is some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * arbitrary and incorrect value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class DataDescriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static void copyZip(ZipInputStream in, ZipOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        byte[] buffer = new byte[1 << 14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        for (ZipEntry ze; (ze = in.getNextEntry()) != null; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            out.putNextEntry(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            // When the bug is present, it shows up here.  The second call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            // copyZip will throw an exception while reading data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            for (int nr; 0 < (nr = in.read(buffer)); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                out.write(buffer, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // Create zip output in byte array zipbytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ZipOutputStream zos = new ZipOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        ZipEntry e = new ZipEntry("testdir/foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        byte[] data = "entry data".getBytes("ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        zos.write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        byte[] zipbytes1 = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int length1 = zipbytes1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println("zip bytes pre-copy length=" + length1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Make a ZipInputStream around zipbytes, and use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // copyZip to get a new byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ZipInputStream zis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            new ZipInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                new ByteArrayInputStream(zipbytes1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        zos = new ZipOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        copyZip(zis, zos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        byte[] zipbytes2 = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int length2 = zipbytes2.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // When the bug is present, pre- and post-copy lengths are different!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        System.out.println("zip bytes post-copy length=" + length2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        equal(length1, length2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        check(Arrays.equals(zipbytes1, zipbytes2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // Now use copyZip again on the bytes resulting from the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // copy.  When the bug is present, copyZip will get an exception this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        zos = new ZipOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        copyZip(new ZipInputStream(new ByteArrayInputStream(zipbytes2)), zos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        byte[] zipbytes3 = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int length3 = zipbytes3.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.out.println("zip bytes post-copy length=" + length3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        equal(length1, length3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        check(Arrays.equals(zipbytes1, zipbytes3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}