Path: blob/master/test/jdk/java/net/SetFactoryPermission/SetFactoryPermission.java
41149 views
/*1* Copyright (c) 2014, 2016, 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* @modules java.rmi29* @run main/othervm SetFactoryPermission success30* @run main/othervm/policy=policy.fail SetFactoryPermission fail31* @run main/othervm/policy=policy.success SetFactoryPermission success32*/33import java.net.ServerSocket;34import java.net.Socket;35import java.net.URL;36import java.net.URLConnection;37import java.rmi.server.RMISocketFactory;38import java.security.AccessControlException;3940public class SetFactoryPermission {41static boolean success = false;4243interface Runner {44public void run() throws Exception;45}4647public static void main (String[] args) throws Exception {48if (args.length > 0) {49success = System.getSecurityManager() == null || args[0].equals("success");50}5152doTest(()->{53System.out.println("Verify URLConnection.setContentHandlerFactor()");54URLConnection.setContentHandlerFactory(null);55});56doTest(()->{57System.out.println("Verify URL.setURLStreamHandlerFactory()");58URL.setURLStreamHandlerFactory(null);59});60doTest(()->{61System.out.println("Verify ServerSocket.setSocketFactory()");62ServerSocket.setSocketFactory(null);63});64doTest(()->{65System.out.println("Verify Socket.setSocketImplFactory()");66Socket.setSocketImplFactory(null);67});68doTest(()->{69System.out.println("Verify RMISocketFactory.setSocketFactory()");70RMISocketFactory.setSocketFactory(null);71});72}7374static void doTest(Runner func) throws Exception {75try {76func.run();77if (!success) {78throw new RuntimeException("AccessControlException is not thrown. Test failed");79}80} catch (SecurityException e) {81if (success) {82e.printStackTrace();83throw new RuntimeException("AccessControlException is thrown unexpectedly. Test failed");84}85}86}87}888990