1 /*
  2  * Copyright (c) 2021, 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.Assert;
 25 import org.testng.annotations.Test;
 26 
 27 import java.net.InetAddress;
 28 import java.security.Permission;
 29 import java.util.ServiceConfigurationError;
 30 import java.util.logging.Logger;
 31 
 32 /*
 33  * @test
 34  * @summary Test that instantiation of InetAddressResolverProvider requires "inetAddressResolverProvider"
 35  *          RuntimePermission when running with security manager.
 36  * @library lib providers/simple
 37  * @build test.library/testlib.ResolutionRegistry simple.provider/impl.SimpleResolverProviderImpl
 38  *        RuntimePermissionTest
 39  * @run testng/othervm RuntimePermissionTest
 40  */
 41 
 42 public class RuntimePermissionTest {
 43 
 44     @Test
 45     public void withRuntimePermission() throws Exception {
 46         testRuntimePermission(true);
 47     }
 48 
 49     @Test
 50     public void noRuntimePermission() throws Exception {
 51         testRuntimePermission(false);
 52     }
 53 
 54     @SuppressWarnings("removal")
 55     private void testRuntimePermission(boolean permitInetAddressResolver) throws Exception {
 56         // Set security manager which grants all permissions + RuntimePermission("inetAddressResolverProvider")
 57         var securityManager = new TestSecurityManager(permitInetAddressResolver);
 58         try {
 59             System.setSecurityManager(securityManager);
 60             if (permitInetAddressResolver) {
 61                 InetAddress.getByName("javaTest.org");
 62             } else {
 63                 ServiceConfigurationError sce =
 64                         Assert.expectThrows(ServiceConfigurationError.class,
 65                                             () -> InetAddress.getByName("javaTest.org"));
 66                 LOGGER.info("Got ServiceConfigurationError: " + sce);
 67                 Throwable cause = sce.getCause();
 68                 Assert.assertTrue(cause instanceof SecurityException);
 69                 Assert.assertTrue(cause.getMessage().contains(RUNTIME_PERMISSION_NAME));
 70             }
 71         } finally {
 72             System.setSecurityManager(null);
 73         }
 74     }
 75 
 76     static class TestSecurityManager extends SecurityManager {
 77         final boolean permitInetAddressResolver;
 78 
 79         public TestSecurityManager(boolean permitInetAddressResolver) {
 80             this.permitInetAddressResolver = permitInetAddressResolver;
 81             LOGGER.info("inetAddressResolverProvider permission is " +
 82                         (permitInetAddressResolver ? "granted" : "not granted"));
 83         }
 84 
 85         @Override
 86         public void checkPermission(Permission permission) {
 87             if (permission instanceof RuntimePermission) {
 88                 LOGGER.info("Checking RuntimePermission: " + permission);
 89                 if (RUNTIME_PERMISSION_NAME.equals(permission.getName()) && !permitInetAddressResolver) {
 90                     LOGGER.info("Denying '" + RUNTIME_PERMISSION_NAME + "' permission");
 91                     throw new SecurityException("Access Denied: " + RUNTIME_PERMISSION_NAME);
 92                 }
 93             }
 94         }
 95     }
 96 
 97     private static final String RUNTIME_PERMISSION_NAME = "inetAddressResolverProvider";
 98     private static final Logger LOGGER = Logger.getLogger(RuntimePermissionTest.class.getName());
 99 
100 }