jdk/test/java/rmi/server/RemoteServer/setLogPermission/SetLogPermission.java
changeset 309 bda219d843f6
parent 2 90ce3da70b43
child 715 f16baef3a20e
equal deleted inserted replaced
308:33a1639d64a5 309:bda219d843f6
     1 /* 
     1 /*
     2  * Copyright 2002-2003 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2002-2003 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4533390
    25  * @bug 4533390
    26  * @summary SecurityException can be obtained but is not specified.
    26  * @summary SecurityException can be obtained but is not specified.
    27  * The RemoteServer.setLog method requires
    27  * The RemoteServer.setLog method requires
    28  * java.util.log.LoggingPermission("control"). 
    28  * java.util.log.LoggingPermission("control").
    29  * @author Ann Wollrath
    29  * @author Ann Wollrath
    30  * @run main/othervm/policy=java.policy SetLogPermission
    30  * @run main/othervm/policy=java.policy SetLogPermission
    31  */
    31  */
    32 
    32 
    33 import java.rmi.server.RemoteServer;
    33 import java.rmi.server.RemoteServer;
    38 
    38 
    39 public class SetLogPermission {
    39 public class SetLogPermission {
    40 
    40 
    41     public static void main(String[] args) throws Exception {
    41     public static void main(String[] args) throws Exception {
    42 
    42 
    43 	System.err.println("\nRegression test for bug 4533390\n");
    43         System.err.println("\nRegression test for bug 4533390\n");
    44 
    44 
    45 	if (System.getSecurityManager() == null) {
    45         if (System.getSecurityManager() == null) {
    46 	    System.setSecurityManager(new SecurityManager());
    46             System.setSecurityManager(new SecurityManager());
    47 	}
    47         }
    48 	
       
    49 	CodeSource codesource = new CodeSource(null, (Certificate[]) null);
       
    50 	Permissions perms = null;
       
    51 	ProtectionDomain pd = new ProtectionDomain(codesource, perms);
       
    52 	AccessControlContext acc =
       
    53 	    new AccessControlContext(new ProtectionDomain[] { pd });
       
    54 	
       
    55 	java.security.AccessController.doPrivileged(
       
    56 	    new java.security.PrivilegedAction() {
       
    57 	    public Object run() {
       
    58 		try {
       
    59 		    System.err.println(
       
    60 			"Attempt to set log without permission");
       
    61 		    RemoteServer.setLog(new ByteArrayOutputStream());
       
    62 		    throw new RuntimeException(
       
    63 			"TEST FAILED: set log without permission");
       
    64 		} catch (SecurityException e) {
       
    65 		    System.err.println(
       
    66 			"TEST PASSED: unable to set log without permission");
       
    67 		}
       
    68 		return null;
       
    69 	    }}, acc);
       
    70 
    48 
    71 	try {
    49         CodeSource codesource = new CodeSource(null, (Certificate[]) null);
    72 	    System.err.println("Attempt to set log with permission");
    50         Permissions perms = null;
    73 	    RemoteServer.setLog(new ByteArrayOutputStream());
    51         ProtectionDomain pd = new ProtectionDomain(codesource, perms);
    74 	    System.err.println(
    52         AccessControlContext acc =
    75 		"TEST PASSED: sufficient permission to set log");
    53             new AccessControlContext(new ProtectionDomain[] { pd });
    76 	} catch (SecurityException e) {
    54 
    77 	    System.err.println("TEST FAILED: unable to set log");
    55         java.security.AccessController.doPrivileged(
    78 	    throw e;
    56             new java.security.PrivilegedAction() {
    79 	}
    57             public Object run() {
       
    58                 try {
       
    59                     System.err.println(
       
    60                         "Attempt to set log without permission");
       
    61                     RemoteServer.setLog(new ByteArrayOutputStream());
       
    62                     throw new RuntimeException(
       
    63                         "TEST FAILED: set log without permission");
       
    64                 } catch (SecurityException e) {
       
    65                     System.err.println(
       
    66                         "TEST PASSED: unable to set log without permission");
       
    67                 }
       
    68                 return null;
       
    69             }}, acc);
       
    70 
       
    71         try {
       
    72             System.err.println("Attempt to set log with permission");
       
    73             RemoteServer.setLog(new ByteArrayOutputStream());
       
    74             System.err.println(
       
    75                 "TEST PASSED: sufficient permission to set log");
       
    76         } catch (SecurityException e) {
       
    77             System.err.println("TEST FAILED: unable to set log");
       
    78             throw e;
       
    79         }
    80     }
    80     }
    81 }
    81 }