--- old/test/demo/zipfs/ZFSTests.java 2018-11-29 12:29:05.863934400 +0530 +++ new/test/demo/zipfs/ZFSTests.java 2018-11-29 12:29:04.846218200 +0530 @@ -22,20 +22,30 @@ */ /* @test - @bug 7156873 - @summary ZipFileSystem regression tests + @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 { @@ -53,4 +63,112 @@ 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"); + } finally { + Files.deleteIfExists(path); + } + } }