jdk/test/java/beans/Beans/Test4080522.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 1998-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4080522
       
    27  * @summary Tests security checks for calls on:
       
    28  *          Beans.setDesignTime
       
    29  *          Beans.setGuiAvailable
       
    30  *          Introspector.setBeanInfoSearchPath
       
    31  *          PropertyEditorManager.setEditorSearchPath
       
    32  * @author Graham Hamilton
       
    33  */
       
    34 
       
    35 import java.beans.Introspector;
       
    36 import java.beans.Beans;
       
    37 import java.beans.PropertyEditorManager;
       
    38 
       
    39 public class Test4080522 {
       
    40     public static void main(String[] args) {
       
    41         OurSecurityManager sm = new OurSecurityManager();
       
    42         String[] path = {"a", "b"};
       
    43         // with no security manager we shuld be able to do these calls OK
       
    44         test(path);
       
    45         // add our own security manager
       
    46         System.setSecurityManager(sm);
       
    47         // now each of the calls should raise an exception
       
    48         try {
       
    49             Beans.setDesignTime(true);
       
    50             throw new Error("Beans.setDesignTime should throw SecurityException");
       
    51         } catch (SecurityException exception) {
       
    52             // expected exception
       
    53         }
       
    54         try {
       
    55             Beans.setGuiAvailable(true);
       
    56             throw new Error("Beans.setGuiAvailable should throw SecurityException");
       
    57         } catch (SecurityException exception) {
       
    58             // expected exception
       
    59         }
       
    60         try {
       
    61             Introspector.setBeanInfoSearchPath(path);
       
    62             throw new Error("Introspector.setBeanInfoSearchPath should throw SecurityException");
       
    63         } catch (SecurityException exception) {
       
    64             // expected exception
       
    65         }
       
    66         try {
       
    67             PropertyEditorManager.setEditorSearchPath(path);
       
    68             throw new Error("PropertyEditorManager.setEditorSearchPath should throw SecurityException");
       
    69         } catch (SecurityException exception) {
       
    70             // expected exception
       
    71         }
       
    72         // now set the security manager to be friendly
       
    73         sm.friendly = true;
       
    74         // now the calls should be OK again.
       
    75         test(path);
       
    76     }
       
    77 
       
    78     private static void test(String[] path) {
       
    79         try {
       
    80             Beans.setDesignTime(true);
       
    81             Beans.setGuiAvailable(true);
       
    82             Introspector.setBeanInfoSearchPath(path);
       
    83             PropertyEditorManager.setEditorSearchPath(path);
       
    84         } catch (SecurityException exception) {
       
    85             throw new Error("unexpected security exception", exception);
       
    86         }
       
    87     }
       
    88 
       
    89     private static class OurSecurityManager extends SecurityManager {
       
    90         boolean friendly;
       
    91 
       
    92         public void checkPropertiesAccess() {
       
    93             if (!friendly) {
       
    94                 throw new SecurityException("No way");
       
    95             }
       
    96         }
       
    97     }
       
    98 }