blob: 8f77635f87473ffb1b5ecc543471c8a6b91c584a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 39 was
81 // determined to be in the entry's uncompressed data.
82 data[39] ^= 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 //--------------------- Infrastructure ---------------------------
101 static volatile int passed = 0, failed = 0;
102 static boolean pass() {passed++; return true;}
103 static boolean fail() {failed++; Thread.dumpStack(); return false;}
104 static boolean fail(String msg) {System.out.println(msg); return fail();}
105 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
106 static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
107 static boolean equal(Object x, Object y) {
108 if (x == null ? y == null : x.equals(y)) return pass();
109 else return fail(x + " not equal to " + y);}
110 public static void main(String[] args) throws Throwable {
111 try {realMain(args);} catch (Throwable t) {unexpected(t);}
112 System.out.println("\nPassed = " + passed + " failed = " + failed);
113 if (failed > 0) throw new AssertionError("Some tests failed");}
114}