1 /*
   2  * Copyright (c) 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 import java.security.AccessControlException;
  25 import java.security.Permission;
  26 import java.security.Policy;
  27 import java.security.ProtectionDomain;
  28 import java.util.List;
  29 
  30 /*
  31  * @test
  32  * @bug     8042901
  33  * @summary Check permission for PlatformMBeanProvider Constructor
  34  * @modules java.management/sun.management.spi
  35  * @author  Shanliang Jiang
  36  */
  37 public class PlatformMBeanProviderConstructorCheck {
  38     public static void main(String[] args) throws Exception {
  39         Policy origPolicy = Policy.getPolicy();
  40         SecurityManager origSM = System.getSecurityManager();
  41         try {
  42             System.out.println("---PlatformMBeanProviderConstructorCheck starting...");
  43 
  44             Policy.setPolicy(new MyPolicy());
  45             System.setSecurityManager(new SecurityManager());
  46 
  47             System.out.println("---PlatformMBeanProviderConstructorCheck Testing without permission...");
  48             try {
  49                 new MyProvider();
  50                 throw new RuntimeException("Does not get expected AccessControlException!");
  51             } catch (AccessControlException ace) {
  52                 System.out.println("---PlatformMBeanProviderConstructorCheck got the expected exception: "
  53                         + ace);
  54             }
  55 
  56             System.out.println("---PlatformMBeanProviderConstructorCheck Testing with permission...");
  57             MyPolicy.allowed = true;
  58             new MyProvider();
  59 
  60             System.out.println("---PlatformMBeanProviderConstructorCheck PASSED!");
  61         } finally {
  62             System.setSecurityManager(origSM);
  63             Policy.setPolicy(origPolicy);
  64         }
  65     }
  66 
  67     private static class MyPolicy extends Policy {
  68         private static String permName = "sun.management.spi.PlatformMBeanProvider.subclass";
  69         private static boolean allowed = false;
  70 
  71         @Override
  72         public boolean implies(ProtectionDomain domain, Permission permission) {
  73             if (permName.equals(permission.getName())) {
  74                 System.out.println("---MyPolicy-implies checks permission for "
  75                         +permName+" and returns "+allowed);
  76 
  77                 return allowed;
  78             } else {
  79                 return true;
  80             }
  81         }
  82     }
  83 
  84     private static class MyProvider extends sun.management.spi.PlatformMBeanProvider {
  85         @Override
  86         public List<PlatformComponent<?>> getPlatformComponentList() {
  87             return null;
  88         }
  89     }
  90 }