StoredCRC.java revision 9330:8b1f1c2a400f
1/*
2 * Copyright (c) 2006, 2013, 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 4879507
27 * @summary ZipInputStream does not check CRC for stored (uncompressed) files
28 * @author Dave Bristor
29 */
30
31import java.io.*;
32import java.util.Arrays;
33import java.util.zip.*;
34
35public class StoredCRC {
36    public static void realMain(String[] args) throws Throwable {
37
38        ByteArrayOutputStream baos = new ByteArrayOutputStream();
39        ZipOutputStream zos = new ZipOutputStream(baos);
40
41        ZipEntry ze = new ZipEntry("test");
42        ze.setMethod(ZipOutputStream.STORED);
43
44        String writtenString = "hello, world";
45        byte[] writtenData = writtenString.getBytes("ASCII");
46        ze.setSize(writtenData.length);
47        CRC32 crc = new CRC32();
48        crc.update(writtenData);
49        ze.setCrc(crc.getValue());
50
51        zos.putNextEntry(ze);
52        zos.write(writtenData, 0, writtenData.length);
53        zos.close();
54
55        byte[] data = baos.toByteArray();
56
57        // Run with an arg to create a test file that can be used to figure
58        // out what position in the data stream can be changed.
59        if (args.length > 0) {
60            FileOutputStream fos = new FileOutputStream("stored.zip");
61            fos.write(data, 0, data.length);
62            fos.close();
63        } else {
64            // Test that reading byte-at-a-time works
65            ZipInputStream zis = new ZipInputStream(
66                new ByteArrayInputStream(data));
67            ze = zis.getNextEntry();
68            int pos = 0;
69            byte[] readData = new byte[256];
70            try {
71                int count = zis.read(readData, pos, 1);
72                while (count > 0) {
73                    count = zis.read(readData, ++pos, 1);
74                }
75                check(writtenString.equals(new String(readData, 0, pos, "ASCII")));
76            } catch (Throwable t) {
77                unexpected(t);
78            }
79
80            // Test that data corruption is detected.  "offset" was
81            // determined to be in the entry's uncompressed data.
82            data[getDataOffset(data) + 4] ^= 1;
83
84            zis = new ZipInputStream(
85                new ByteArrayInputStream(data));
86            ze = zis.getNextEntry();
87
88            try {
89                zis.read(readData, 0, readData.length);
90                fail("Did not catch expected ZipException" );
91            } catch (ZipException ex) {
92                String msg = ex.getMessage();
93                check(msg != null && msg.startsWith("invalid entry CRC (expected 0x"));
94            } catch (Throwable t) {
95                unexpected(t);
96            }
97        }
98    }
99
100    public static final int getDataOffset(byte b[]) {
101        final int LOCHDR = 30;       // LOC header size
102        final int LOCEXT = 28;       // extra field length
103        final int LOCNAM = 26;       // filename length
104        int lenExt = Byte.toUnsignedInt(b[LOCEXT]) | (Byte.toUnsignedInt(b[LOCEXT + 1]) << 8);
105        int lenNam = Byte.toUnsignedInt(b[LOCNAM]) | (Byte.toUnsignedInt(b[LOCNAM + 1]) << 8);
106        return LOCHDR + lenExt + lenNam;
107    }
108
109    //--------------------- Infrastructure ---------------------------
110    static volatile int passed = 0, failed = 0;
111    static boolean pass() {passed++; return true;}
112    static boolean fail() {failed++; Thread.dumpStack(); return false;}
113    static boolean fail(String msg) {System.out.println(msg); return fail();}
114    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
115    static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
116    static boolean equal(Object x, Object y) {
117        if (x == null ? y == null : x.equals(y)) return pass();
118        else return fail(x + " not equal to " + y);}
119    public static void main(String[] args) throws Throwable {
120        try {realMain(args);} catch (Throwable t) {unexpected(t);}
121        System.out.println("\nPassed = " + passed + " failed = " + failed);
122        if (failed > 0) throw new AssertionError("Some tests failed");}
123}
124