jdk/test/java/util/zip/StoredCRC.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
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 2006 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4879507
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary ZipInputStream does not check CRC for stored (uncompressed) files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Dave Bristor
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.Arrays;
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 StoredCRC {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        ZipOutputStream zos = new ZipOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        ZipEntry ze = new ZipEntry("test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        ze.setMethod(ZipOutputStream.STORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        String writtenString = "hello, world";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        byte[] writtenData = writtenString.getBytes("ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        ze.setSize(writtenData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        crc.update(writtenData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        ze.setCrc(crc.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        zos.putNextEntry(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        zos.write(writtenData, 0, writtenData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        byte[] data = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // Run with an arg to create a test file that can be used to figure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // out what position in the data stream can be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            FileOutputStream fos = new FileOutputStream("stored.zip");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            fos.write(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // Test that reading byte-at-a-time works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            ZipInputStream zis = new ZipInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                new ByteArrayInputStream(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            ze = zis.getNextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            byte[] readData = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                int count = zis.read(readData, pos, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                while (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    count = zis.read(readData, ++pos, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                check(writtenString.equals(new String(readData, 0, pos, "ASCII")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            // Test that data corruption is detected.  Offset 39 was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // determined to be in the entry's uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            data[39] ^= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            zis = new ZipInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                new ByteArrayInputStream(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ze = zis.getNextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                zis.read(readData, 0, readData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                fail("Did not catch expected ZipException" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            } catch (ZipException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                String msg = ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                check(msg != null && msg.startsWith("invalid entry CRC (expected 0x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static boolean pass() {passed++; return true;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static boolean fail() {failed++; Thread.dumpStack(); return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static boolean fail(String msg) {System.out.println(msg); return fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static boolean equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (x == null ? y == null : x.equals(y)) return pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        else return fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println("\nPassed = " + passed + " failed = " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}