1 /*
   2  * Copyright (c) 2019, 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 import org.testng.annotations.AfterClass;
  25 import org.testng.annotations.BeforeClass;
  26 import org.testng.annotations.Test;
  27 
  28 import java.io.IOException;
  29 import java.nio.file.FileSystem;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.nio.file.spi.FileSystemProvider;
  34 import java.util.Map;
  35 
  36 /**
  37  * @test
  38  * @bug 8210469
  39  * @summary Verify ZIP FileSystem works with a Security Manager
  40  * @modules jdk.zipfs
  41  * @compile PropertyPermissionTests.java
  42  * @run testng/othervm/java.security.policy=PropertyPermissions.policy  PropertyPermissionTests
  43  */
  44 public class PropertyPermissionTests {
  45 
  46     // Map to used for creating a ZIP archive
  47     private static final Map<String, String> ZIPFS_MAP = Map.of("create", "true");
  48 
  49     // The ZIP file system provider
  50     private static final FileSystemProvider ZIPFS_PROVIDER = getZipFSProvider();
  51 
  52     // Primary jar file used for testing
  53     private static Path jarFile;
  54 
  55     /**
  56      * Create the JAR files used by the tests
  57      */
  58     @BeforeClass
  59     public void setUp()  throws Exception {
  60         jarFile = Utils.createJarFile("basic.jar",
  61                 "META-INF/services/java.nio.file.spi.FileSystemProvider");
  62     }
  63 
  64     /**
  65      * Remove JAR files used by test as part of clean-up
  66      */
  67     @AfterClass
  68     public void tearDown() throws Exception {
  69         Files.deleteIfExists(jarFile);
  70     }
  71 
  72     /**
  73      * Validate that the ZIP File System can be successfully closed when a Security Manager
  74      * has been enabled.
  75      */
  76     @Test
  77     public void test0000() throws IOException {
  78         FileSystem zipfs = ZIPFS_PROVIDER.newFileSystem(
  79                 Paths.get("basic.jar"), ZIPFS_MAP);
  80         zipfs.close();
  81     }
  82 
  83     /**
  84      * Returns the Zip FileSystem Provider
  85      */
  86     private static FileSystemProvider getZipFSProvider() {
  87         for (FileSystemProvider fsProvider : FileSystemProvider.installedProviders()) {
  88             if ("jar".equals(fsProvider.getScheme())) {
  89                 return fsProvider;
  90             }
  91         }
  92         return null;
  93     }
  94 
  95 }