Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/SetFactoryPermission/SetFactoryPermission.java
38811 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @bug 804805227* @summary Test a series of methods which requires "setFactory" runtime permission28* @run main SetFactoryPermission success29* @run main/othervm/policy=policy.fail SetFactoryPermission fail30* @run main/othervm/policy=policy.success SetFactoryPermission success31*/32import java.net.ServerSocket;33import java.net.Socket;34import java.net.URL;35import java.net.URLConnection;36import java.rmi.server.RMISocketFactory;37import java.security.AccessControlException;3839public class SetFactoryPermission {40static boolean success = false;4142interface Runner {43public void run() throws Exception;44}4546public static void main (String[] args) throws Exception {47if (args.length > 0) {48success = System.getSecurityManager() == null || args[0].equals("success");49}5051doTest(()->{52System.out.println("Verify URLConnection.setContentHandlerFactor()");53URLConnection.setContentHandlerFactory(null);54});55doTest(()->{56System.out.println("Verify URL.setURLStreamHandlerFactory()");57URL.setURLStreamHandlerFactory(null);58});59doTest(()->{60System.out.println("Verify ServerSocket.setSocketFactory()");61ServerSocket.setSocketFactory(null);62});63doTest(()->{64System.out.println("Verify Socket.setSocketImplFactory()");65Socket.setSocketImplFactory(null);66});67doTest(()->{68System.out.println("Verify RMISocketFactory.setSocketFactory()");69RMISocketFactory.setSocketFactory(null);70});71}7273static void doTest(Runner func) throws Exception {74try {75func.run();76if (!success) {77throw new RuntimeException("AccessControlException is not thrown. Test failed");78}79} catch (SecurityException e) {80if (success) {81e.printStackTrace();82throw new RuntimeException("AccessControlException is thrown unexpectedly. Test failed");83}84}85}86}878889