1 /*
   2  * Copyright (c) 2008, 2011, 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 4313887
  26  * @summary Unit test for Watchable#register's permission checks
  27  * @modules java.base/com.sun.nio.file
  28  * @build WithSecurityManager
  29  * @run main/othervm WithSecurityManager denyAll.policy - fail
  30  * @run main/othervm WithSecurityManager denyAll.policy tree fail
  31  * @run main/othervm WithSecurityManager grantDirOnly.policy - pass
  32  * @run main/othervm WithSecurityManager grantDirOnly.policy tree fail
  33  * @run main/othervm WithSecurityManager grantDirAndOneLevel.policy - pass
  34  * @run main/othervm WithSecurityManager grantDirAndOneLevel.policy tree fail
  35  * @run main/othervm WithSecurityManager grantDirAndTree.policy - pass
  36  * @run main/othervm WithSecurityManager grantDirAndTree.policy tree pass
  37  */
  38 
  39 import java.nio.file.*;
  40 import java.io.IOException;
  41 import com.sun.nio.file.ExtendedWatchEventModifier;
  42 
  43 public class WithSecurityManager {
  44 
  45     public static void main(String[] args) throws IOException {
  46         String policyFile = args[0];
  47         boolean recursive = args[1].equals("tree");
  48         boolean expectedToFail = args[2].equals("fail");
  49 
  50         // install security manager with the given policy file
  51         String testSrc = System.getProperty("test.src");
  52         if (testSrc == null)
  53             throw new RuntimeException("This test must be run by jtreg");
  54         Path dir = Paths.get(testSrc);
  55         System.setProperty("java.security.policy", dir.resolve(policyFile).toString());
  56         System.setSecurityManager(new SecurityManager());
  57 
  58         // initialize optional modifier
  59         WatchEvent.Modifier[] modifiers;
  60         if (recursive) {
  61             modifiers = new WatchEvent.Modifier[1];
  62             modifiers[0] = ExtendedWatchEventModifier.FILE_TREE;
  63         } else {
  64             modifiers = new WatchEvent.Modifier[0];
  65         }
  66 
  67         // attempt to register directory
  68         try {
  69             dir.register(dir.getFileSystem().newWatchService(),
  70                          new WatchEvent.Kind<?>[]{ StandardWatchEventKinds.ENTRY_CREATE },
  71                          modifiers);
  72             if (expectedToFail)
  73                 throw new RuntimeException("SecurityException not thrown");
  74         } catch (SecurityException e) {
  75             if (!expectedToFail)
  76                 throw e;
  77         } catch (UnsupportedOperationException e) {
  78             // FILE_TREE modifier only supported on some platforms
  79             if (!recursive)
  80                 throw new RuntimeException(e);
  81             System.out.println("FILE_TREE option not supported");
  82         }
  83     }
  84 }