1 /*
   2  * Copyright (c) 2012, 2015, 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 8040059 8028480 8034773 8153248 8061777
  26  * @summary ZipFileSystem regression tests
  27  *
  28  * @run main ZFSTests
  29  * @run main/othervm/java.security.policy=test.policy ZFSTests
  30  * @modules jdk.zipfs
  31  */
  32 
  33 
  34 import java.io.OutputStream;
  35 import java.net.URI;
  36 import java.nio.ByteBuffer;
  37 import java.nio.channels.*;
  38 import java.nio.file.*;
  39 import java.nio.file.spi.*;
  40 import java.util.*;
  41 
  42 public class ZFSTests {
  43 
  44     public static void main(String[] args) throws Throwable {
  45         test7156873();
  46         test8061777();
  47         tests();
  48     }
  49 
  50     static void test7156873() throws Throwable {
  51         String DIRWITHSPACE = "testdir with spaces";
  52         Path dir = Paths.get(DIRWITHSPACE);
  53         Path path = Paths.get(DIRWITHSPACE, "file.zip");
  54         try {
  55             Files.createDirectory(dir);
  56             URI uri = URI.create("jar:" + path.toUri());
  57             Map<String, Object> env = new HashMap<String, Object>();
  58             env.put("create", "true");
  59             try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {}
  60         } finally {
  61             Files.deleteIfExists(path);
  62             Files.deleteIfExists(dir);
  63         }
  64     }
  65 
  66     static void test8061777() throws Throwable {
  67         Path path = Paths.get("file.zip");
  68         try {
  69             URI uri = URI.create("jar:" + path.toUri());
  70             Map<String, Object> env = new HashMap<String, Object>();
  71             env.put("create", "true");
  72             env.put("encoding", "Shift_JIS");
  73             try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
  74                 FileSystemProvider fsp = fs.provider();
  75                 Path p = fs.getPath("/\u8868\u7533.txt");  // 0x95 0x5c 0x90 0x5c
  76                 try (OutputStream os = fsp.newOutputStream(p)) {
  77                     os.write("Hello!".getBytes("ASCII"));
  78                 }
  79                 Path dir = fs.getPath("/");
  80                 Files.list(dir)
  81                      .forEach( child -> {
  82                              System.out.println("child:" + child);
  83                              if (!child.toString().equals(p.toString()))
  84                                  throw new RuntimeException("wrong path name created");
  85                           });
  86                 if (!"Hello!".equals(new String(Files.readAllBytes(p), "ASCII")))
  87                     throw new RuntimeException("wrong content in newly created file");
  88             }
  89         } finally {
  90             Files.deleteIfExists(path);
  91         }
  92     }
  93 
  94     static void tests() throws Throwable {
  95         Path path = Paths.get("file.zip");
  96         try {
  97             URI uri = URI.create("jar:" + path.toUri());
  98             Map<String, Object> env = new HashMap<String, Object>();
  99             env.put("create", "true");
 100             try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
 101                 FileSystemProvider fsp = fs.provider();
 102                 Set<? extends OpenOption> options;
 103                 Path p = fs.getPath("test.txt");
 104                 // 8028480
 105                 options = EnumSet.of(StandardOpenOption.CREATE,
 106                                      StandardOpenOption.WRITE,
 107                                      StandardOpenOption.APPEND);
 108                 try (FileChannel ch = fsp.newFileChannel(p, options)) {
 109                     ch.write(ByteBuffer.wrap("Hello!".getBytes("ASCII")));
 110                 }
 111                 // 8034773
 112                 try (OutputStream os = fsp.newOutputStream(p, new OpenOption[0])) {
 113                     os.write("Hello2!".getBytes("ASCII"));
 114                 }
 115                 if (!"Hello2!".equals(new String(
 116                         Files.readAllBytes(fs.getPath("test.txt"))))) {
 117                     throw new RuntimeException("failed to open as truncate_existing");
 118                 }
 119 
 120                 options = EnumSet.of(StandardOpenOption.CREATE,
 121                                      StandardOpenOption.APPEND,
 122                                      StandardOpenOption.TRUNCATE_EXISTING);
 123                 try (FileChannel ch = fsp.newFileChannel(p, options)) {
 124                     throw new RuntimeException("expected IAE not thrown!");
 125                 } catch (IllegalArgumentException x) {
 126                     // expected x.printStackTrace();
 127                 }
 128 
 129                 //8153248
 130                 Path dir = fs.getPath("/dir");
 131                 Path subdir = fs.getPath("/dir/subdir");
 132                 Files.createDirectory(dir);
 133                 Files.createDirectory(subdir);
 134                 Files.list(dir)
 135                      .forEach( child -> {
 136                              System.out.println("child:" + child);
 137                              if (child.toString().endsWith("/"))
 138                                  throw new RuntimeException("subdir names ends with /");
 139                           });
 140             }
 141         } finally {
 142             Files.deleteIfExists(path);
 143         }
 144     }
 145 }