1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 @bug 7156873 8199776 26 @summary ZipFileSystem regression tests 27 */ 28 29 import java.io.IOException; 30 import java.io.OutputStream; 31 import java.net.URI; 32 import java.nio.file.*; 33 import java.nio.file.attribute.BasicFileAttributes; 34 import java.util.Arrays; 35 import java.util.Collections; 36 import java.util.HashSet; 37 import java.util.Map; 38 import java.util.HashMap; 39 import java.util.Set; 40 import java.util.concurrent.atomic.AtomicInteger; 41 import java.util.zip.ZipEntry; 42 import java.util.zip.ZipOutputStream; 43 44 public class ZFSTests { 45 46 public static void main(String[] args) throws Throwable { 47 test7156873(); 48 test8199776(); 49 } 50 51 static void test7156873() throws Throwable { 52 String DIRWITHSPACE = "testdir with spaces"; 53 Path dir = Paths.get(DIRWITHSPACE); 54 Path path = Paths.get(DIRWITHSPACE, "file.zip"); 55 try { 56 Files.createDirectory(dir); 57 URI uri = URI.create("jar:" + path.toUri()); 58 Map<String, Object> env = new HashMap<String, Object>(); 59 env.put("create", "true"); 60 try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {} 61 } finally { 62 Files.deleteIfExists(path); 63 Files.deleteIfExists(dir); 64 } 65 } 66 67 static void test8199776() throws Throwable { 68 69 // root entry "/" 70 Path path = Paths.get("rootdir.zip"); 71 URI uri = URI.create("jar:" + path.toUri()); 72 73 Set<String> dirs = new HashSet<>(); 74 Set<String> files = new HashSet<>(); 75 try (OutputStream os = Files.newOutputStream(path); 76 ZipOutputStream zos = new ZipOutputStream(os)) { 77 zos.putNextEntry(new ZipEntry("/")); 78 dirs.add("/"); 79 zos.putNextEntry(new ZipEntry("foo")); 80 files.add("/foo"); 81 zos.write("/foo".getBytes()); 82 zos.putNextEntry(new ZipEntry("bar")); 83 files.add("/bar"); 84 zos.write("/bar".getBytes()); 85 } 86 AtomicInteger cnt = new AtomicInteger(); 87 int max = 3; 88 try (FileSystem fs = FileSystems.newFileSystem(uri, 89 Collections.emptyMap())) { 90 Files.walkFileTree(fs.getRootDirectories().iterator().next(), 91 new SimpleFileVisitor<Path>() { 92 93 @Override 94 public FileVisitResult visitFile(Path file, 95 BasicFileAttributes attrs) throws IOException { 96 if (cnt.incrementAndGet() > max) 97 throw new RuntimeException( 98 "visited too many files/dirs"); 99 files.remove(file.toString()); 100 if (!Arrays.equals(Files.readAllBytes(file), file 101 .toString().getBytes())) 102 throw new RuntimeException( 103 "visited files has wrong content: " 104 + file); 105 return FileVisitResult.CONTINUE; 106 } 107 108 @Override 109 public FileVisitResult preVisitDirectory(Path dir, 110 BasicFileAttributes attrs) throws IOException { 111 if (cnt.incrementAndGet() > max) 112 throw new RuntimeException( 113 "visited too many files/dirs"); 114 dirs.remove(dir.toString()); 115 return FileVisitResult.CONTINUE; 116 } 117 }); 118 if (cnt.get() != max || dirs.size() != 0 || files.size() != 0) 119 throw new RuntimeException("walk files/dirs failed"); 120 121 } finally { 122 Files.deleteIfExists(path); 123 } 124 125 // absolute file/dir, no need to test cnt again.. 126 dirs.clear(); 127 files.clear(); 128 try (OutputStream os = Files.newOutputStream(path); 129 ZipOutputStream zos = new ZipOutputStream(os)) { 130 zos.putNextEntry(new ZipEntry("/")); 131 dirs.add("/"); 132 zos.putNextEntry(new ZipEntry("/fooo/")); 133 dirs.add("/fooo/"); 134 zos.putNextEntry(new ZipEntry("/foo")); 135 files.add("/foo"); 136 zos.write("/foo".getBytes()); 137 zos.putNextEntry(new ZipEntry("/bar")); 138 files.add("/bar"); 139 zos.write("/bar".getBytes()); 140 zos.putNextEntry(new ZipEntry("/fooo/bar")); 141 files.add("/fooo/bar"); 142 zos.write("/fooo/bar".getBytes()); 143 } 144 145 try (FileSystem fs = FileSystems.newFileSystem(uri, 146 Collections.emptyMap())) { 147 Files.walkFileTree(fs.getRootDirectories().iterator().next(), 148 new SimpleFileVisitor<Path>() { 149 @Override 150 public FileVisitResult visitFile(Path file, 151 BasicFileAttributes attrs) throws IOException { 152 files.remove(file.toString()); 153 if (!Arrays.equals(Files.readAllBytes(file), file 154 .toString().getBytes())) 155 throw new RuntimeException( 156 "visited files has wrong content: " 157 + file); 158 return FileVisitResult.CONTINUE; 159 } 160 161 @Override 162 public FileVisitResult preVisitDirectory(Path dir, 163 BasicFileAttributes attrs) throws IOException { 164 dirs.remove(dir.toString()); 165 return FileVisitResult.CONTINUE; 166 } 167 }); 168 if (dirs.size() != 0 || files.size() != 0) 169 throw new RuntimeException("walk files/dirs failed"); 170 } finally { 171 Files.deleteIfExists(path); 172 } 173 } 174 }