jdk/test/java/net/SetFactoryPermission/SetFactoryPermission.java
author mikael
Fri, 04 Dec 2015 15:08:49 -0800
changeset 35090 1f5b6aa795d0
parent 27033 17712e486fb2
child 38883 d5de564f8089
permissions -rw-r--r--
8144748: Move assembler/macroAssembler inline function definitions to corresponding inline.hpp files Reviewed-by: kvn, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27033
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     1
/*
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     4
 *
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     8
 *
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    13
 * accompanied this code).
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    14
 *
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    18
 *
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    21
 * questions.
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    22
 */
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    23
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    24
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    25
/*
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    26
 * @test
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    27
 * @bug 8048052
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    28
 * @summary Test a series of methods which requires "setFactory" runtime permission
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    29
 * @run main SetFactoryPermission success
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    30
 * @run main/othervm/policy=policy.fail SetFactoryPermission fail
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    31
 * @run main/othervm/policy=policy.success SetFactoryPermission success
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    32
 */
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    33
import java.net.ServerSocket;
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    34
import java.net.Socket;
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    35
import java.net.URL;
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    36
import java.net.URLConnection;
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    37
import java.rmi.server.RMISocketFactory;
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    38
import java.security.AccessControlException;
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    39
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    40
public class SetFactoryPermission {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    41
    static boolean success = false;
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    42
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    43
    interface Runner {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    44
        public void run() throws Exception;
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    45
    }
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    46
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    47
    public static void main (String[] args) throws Exception {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    48
        if (args.length > 0) {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    49
            success = System.getSecurityManager() == null || args[0].equals("success");
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    50
        }
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    51
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    52
        doTest(()->{
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    53
            System.out.println("Verify URLConnection.setContentHandlerFactor()");
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    54
            URLConnection.setContentHandlerFactory(null);
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    55
        });
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    56
        doTest(()->{
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    57
            System.out.println("Verify URL.setURLStreamHandlerFactory()");
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    58
            URL.setURLStreamHandlerFactory(null);
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    59
        });
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    60
        doTest(()->{
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    61
            System.out.println("Verify ServerSocket.setSocketFactory()");
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    62
            ServerSocket.setSocketFactory(null);
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    63
        });
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    64
        doTest(()->{
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    65
            System.out.println("Verify Socket.setSocketImplFactory()");
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    66
            Socket.setSocketImplFactory(null);
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    67
        });
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    68
        doTest(()->{
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    69
            System.out.println("Verify RMISocketFactory.setSocketFactory()");
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    70
            RMISocketFactory.setSocketFactory(null);
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    71
        });
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    72
    }
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    73
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    74
    static void doTest(Runner func) throws Exception {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    75
        try {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    76
            func.run();
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    77
            if (!success) {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    78
                throw new RuntimeException("AccessControlException is not thrown. Test failed");
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    79
            }
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    80
        } catch (SecurityException e) {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    81
            if (success) {
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    82
                e.printStackTrace();
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    83
                throw new RuntimeException("AccessControlException is thrown unexpectedly. Test failed");
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    84
            }
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    85
        }
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    86
    }
17712e486fb2 8048052: Permission tests for setFactory
weijun
parents:
diff changeset
    87
}