/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test @bug 7156873,8199776 @summary ZipFileSystem regression tests */ import java.io.IOException; import java.io.OutputStream; import java.net.URI; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.HashMap; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZFSTests { public static void main(String[] args) throws Throwable { test7156873(); test8199776(); } static void test7156873() throws Throwable { String DIRWITHSPACE = "testdir with spaces"; Path dir = Paths.get(DIRWITHSPACE); Path path = Paths.get(DIRWITHSPACE, "file.zip"); try { Files.createDirectory(dir); URI uri = URI.create("jar:" + path.toUri()); Map env = new HashMap(); env.put("create", "true"); try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {} } finally { Files.deleteIfExists(path); Files.deleteIfExists(dir); } } static void test8199776() throws Throwable { // root entry "/" Path path = Paths.get("rootdir.zip"); URI uri = URI.create("jar:" + path.toUri()); Set dirs = new HashSet<>(); Set files = new HashSet<>(); try (OutputStream os = Files.newOutputStream(path); ZipOutputStream zos = new ZipOutputStream(os)) { zos.putNextEntry(new ZipEntry("/")); dirs.add("/"); zos.putNextEntry(new ZipEntry("foo")); files.add("/foo"); zos.write("/foo".getBytes()); zos.putNextEntry(new ZipEntry("bar")); files.add("/bar"); zos.write("/bar".getBytes()); } AtomicInteger cnt = new AtomicInteger(); int max = 3; try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) { Files.walkFileTree(fs.getRootDirectories().iterator().next(), new SimpleFileVisitor() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (cnt.incrementAndGet() > max) throw new RuntimeException( "visited too many files/dirs"); files.remove(file.toString()); if (!Arrays.equals(Files.readAllBytes(file), file .toString().getBytes())) throw new RuntimeException( "visited files has wrong content: " + file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (cnt.incrementAndGet() > max) throw new RuntimeException( "visited too many files/dirs"); dirs.remove(dir.toString()); return FileVisitResult.CONTINUE; } }); if (cnt.get() != max || dirs.size() != 0 || files.size() != 0) throw new RuntimeException("walk files/dirs failed"); } finally { Files.deleteIfExists(path); } // absolute file/dir, no need to test cnt again.. dirs.clear(); files.clear(); try (OutputStream os = Files.newOutputStream(path); ZipOutputStream zos = new ZipOutputStream(os)) { zos.putNextEntry(new ZipEntry("/")); dirs.add("/"); zos.putNextEntry(new ZipEntry("/fooo/")); dirs.add("/fooo/"); zos.putNextEntry(new ZipEntry("/foo")); files.add("/foo"); zos.write("/foo".getBytes()); zos.putNextEntry(new ZipEntry("/bar")); files.add("/bar"); zos.write("/bar".getBytes()); zos.putNextEntry(new ZipEntry("/fooo/bar")); files.add("/fooo/bar"); zos.write("/fooo/bar".getBytes()); } try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) { Files.walkFileTree(fs.getRootDirectories().iterator().next(), new SimpleFileVisitor() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { files.remove(file.toString()); if (!Arrays.equals(Files.readAllBytes(file), file .toString().getBytes())) throw new RuntimeException( "visited files has wrong content: " + file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { dirs.remove(dir.toString()); return FileVisitResult.CONTINUE; } }); if (dirs.size() != 0 || files.size() != 0) throw new RuntimeException("walk files/dirs failed"); } } }