0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4879507
0N/A * @summary ZipInputStream does not check CRC for stored (uncompressed) files
0N/A * @author Dave Bristor
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.zip.*;
0N/A
0N/Apublic class StoredCRC {
0N/A public static void realMain(String[] args) throws Throwable {
0N/A
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A ZipOutputStream zos = new ZipOutputStream(baos);
0N/A
0N/A ZipEntry ze = new ZipEntry("test");
0N/A ze.setMethod(ZipOutputStream.STORED);
0N/A
0N/A String writtenString = "hello, world";
0N/A byte[] writtenData = writtenString.getBytes("ASCII");
0N/A ze.setSize(writtenData.length);
0N/A CRC32 crc = new CRC32();
0N/A crc.update(writtenData);
0N/A ze.setCrc(crc.getValue());
0N/A
0N/A zos.putNextEntry(ze);
0N/A zos.write(writtenData, 0, writtenData.length);
0N/A zos.close();
0N/A
0N/A byte[] data = baos.toByteArray();
0N/A
0N/A // Run with an arg to create a test file that can be used to figure
0N/A // out what position in the data stream can be changed.
0N/A if (args.length > 0) {
0N/A FileOutputStream fos = new FileOutputStream("stored.zip");
0N/A fos.write(data, 0, data.length);
0N/A fos.close();
0N/A } else {
0N/A // Test that reading byte-at-a-time works
0N/A ZipInputStream zis = new ZipInputStream(
0N/A new ByteArrayInputStream(data));
0N/A ze = zis.getNextEntry();
0N/A int pos = 0;
0N/A byte[] readData = new byte[256];
0N/A try {
0N/A int count = zis.read(readData, pos, 1);
0N/A while (count > 0) {
0N/A count = zis.read(readData, ++pos, 1);
0N/A }
0N/A check(writtenString.equals(new String(readData, 0, pos, "ASCII")));
0N/A } catch (Throwable t) {
0N/A unexpected(t);
0N/A }
0N/A
0N/A // Test that data corruption is detected. Offset 39 was
0N/A // determined to be in the entry's uncompressed data.
0N/A data[39] ^= 1;
0N/A
0N/A zis = new ZipInputStream(
0N/A new ByteArrayInputStream(data));
0N/A ze = zis.getNextEntry();
0N/A
0N/A try {
0N/A zis.read(readData, 0, readData.length);
0N/A fail("Did not catch expected ZipException" );
0N/A } catch (ZipException ex) {
0N/A String msg = ex.getMessage();
0N/A check(msg != null && msg.startsWith("invalid entry CRC (expected 0x"));
0N/A } catch (Throwable t) {
0N/A unexpected(t);
0N/A }
0N/A }
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static boolean pass() {passed++; return true;}
0N/A static boolean fail() {failed++; Thread.dumpStack(); return false;}
0N/A static boolean fail(String msg) {System.out.println(msg); return fail();}
0N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
0N/A static boolean equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) return pass();
0N/A else return fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
0N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.println("\nPassed = " + passed + " failed = " + failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
0N/A}