575N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
575N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
575N/A *
575N/A * This code is free software; you can redistribute it and/or modify it
575N/A * under the terms of the GNU General Public License version 2 only, as
575N/A * published by the Free Software Foundation.
575N/A *
575N/A * This code is distributed in the hope that it will be useful, but WITHOUT
575N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
575N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
575N/A * version 2 for more details (a copy is included in the LICENSE file that
575N/A * accompanied this code).
575N/A *
575N/A * You should have received a copy of the GNU General Public License version
575N/A * 2 along with this work; if not, write to the Free Software Foundation,
575N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
575N/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.
575N/A */
575N/A
575N/A/* @test
575N/A * @bug 6334003 6440786
575N/A * @summary Test ability to write and read zip files that have no entries.
575N/A * @author Dave Bristor
575N/A */
575N/A
575N/Aimport java.io.*;
575N/Aimport java.util.*;
575N/Aimport java.util.zip.*;
575N/A
575N/Apublic class TestEmptyZip {
575N/A public static void realMain(String[] args) throws Throwable {
575N/A String zipName = "foo.zip";
575N/A File f = new File(System.getProperty("test.scratch", "."), zipName);
575N/A if (f.exists() && !f.delete()) {
575N/A throw new Exception("failed to delete " + zipName);
575N/A }
575N/A
575N/A f.createNewFile();
575N/A try {
701N/A // Verify 0-length file cannot be read
701N/A checkCannotRead(f);
575N/A
701N/A // Verify non-zip file cannot be read
701N/A OutputStream out = new FileOutputStream(f);
701N/A try {
701N/A out.write("class Foo { }".getBytes());
701N/A } finally {
701N/A out.close();
701N/A }
701N/A checkCannotRead(f);
701N/A
575N/A } finally {
701N/A f.delete();
575N/A }
575N/A
575N/A // Verify 0-entries file can be written
575N/A write(f);
575N/A
575N/A // Verify 0-entries file can be read
575N/A readFile(f);
575N/A readStream(f);
575N/A
575N/A f.delete();
575N/A }
575N/A
701N/A static void checkCannotRead(File f) throws IOException {
701N/A try {
701N/A new ZipFile(f).close();
701N/A fail();
701N/A } catch (ZipException ze) {
701N/A if (f.length() == 0) {
701N/A check(ze.getMessage().contains("zip file is empty"));
701N/A } else {
701N/A pass();
701N/A }
701N/A }
3656N/A try (FileInputStream fis = new FileInputStream(f);
3656N/A ZipInputStream zis = new ZipInputStream(fis))
3656N/A {
701N/A ZipEntry ze = zis.getNextEntry();
701N/A check(ze == null);
701N/A } catch (IOException ex) {
701N/A unexpected(ex);
701N/A }
701N/A }
701N/A
575N/A static void write(File f) throws Exception {
3656N/A try (FileOutputStream fis = new FileOutputStream(f);
3656N/A ZipOutputStream zos = new ZipOutputStream(fis))
3656N/A {
575N/A zos.finish();
575N/A pass();
575N/A } catch (Exception ex) {
575N/A unexpected(ex);
575N/A }
575N/A }
575N/A
575N/A static void readFile(File f) throws Exception {
3656N/A try (ZipFile zf = new ZipFile(f)) {
575N/A
575N/A Enumeration e = zf.entries();
575N/A while (e.hasMoreElements()) {
575N/A ZipEntry entry = (ZipEntry) e.nextElement();
575N/A fail();
575N/A }
575N/A pass();
575N/A } catch (Exception ex) {
575N/A unexpected(ex);
575N/A }
575N/A }
575N/A
575N/A static void readStream(File f) throws Exception {
3656N/A try (FileInputStream fis = new FileInputStream(f);
3656N/A ZipInputStream zis = new ZipInputStream(fis))
3656N/A {
575N/A ZipEntry ze = zis.getNextEntry();
575N/A check(ze == null);
575N/A byte[] buf = new byte[1024];
575N/A check(zis.read(buf, 0, 1024) == -1);
575N/A }
575N/A }
575N/A
575N/A //--------------------- Infrastructure ---------------------------
575N/A static volatile int passed = 0, failed = 0;
575N/A static boolean pass() {passed++; return true;}
575N/A static boolean fail() {failed++; Thread.dumpStack(); return false;}
575N/A static boolean fail(String msg) {System.out.println(msg); return fail();}
575N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
575N/A static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
575N/A static boolean equal(Object x, Object y) {
575N/A if (x == null ? y == null : x.equals(y)) return pass();
575N/A else return fail(x + " not equal to " + y);}
575N/A public static void main(String[] args) throws Throwable {
575N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
575N/A System.out.println("\nPassed = " + passed + " failed = " + failed);
575N/A if (failed > 0) throw new AssertionError("Some tests failed");}
575N/A}