Test4080522.java revision 2362:00cd9dc3c2b5
1168404Spjd/*
2168404Spjd * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
3168404Spjd * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4168404Spjd *
5168404Spjd * This code is free software; you can redistribute it and/or modify it
6168404Spjd * under the terms of the GNU General Public License version 2 only, as
7168404Spjd * published by the Free Software Foundation.
8168404Spjd *
9168404Spjd * This code is distributed in the hope that it will be useful, but WITHOUT
10168404Spjd * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11168404Spjd * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12168404Spjd * version 2 for more details (a copy is included in the LICENSE file that
13168404Spjd * accompanied this code).
14168404Spjd *
15168404Spjd * You should have received a copy of the GNU General Public License version
16168404Spjd * 2 along with this work; if not, write to the Free Software Foundation,
17168404Spjd * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18168404Spjd *
19168404Spjd * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20168404Spjd * or visit www.oracle.com if you need additional information or have any
21168404Spjd * questions.
22185029Spjd */
23168404Spjd
24168404Spjd/*
25168404Spjd * @test
26168404Spjd * @bug 4080522
27168404Spjd * @summary Tests security checks for calls on:
28168404Spjd *          Beans.setDesignTime
29168404Spjd *          Beans.setGuiAvailable
30168404Spjd *          Introspector.setBeanInfoSearchPath
31168404Spjd *          PropertyEditorManager.setEditorSearchPath
32168404Spjd * @author Graham Hamilton
33168404Spjd */
34168404Spjd
35168404Spjdimport java.beans.Introspector;
36168404Spjdimport java.beans.Beans;
37168404Spjdimport java.beans.PropertyEditorManager;
38168404Spjd
39168404Spjdpublic class Test4080522 {
40168404Spjd    public static void main(String[] args) {
41168404Spjd        OurSecurityManager sm = new OurSecurityManager();
42168404Spjd        String[] path = {"a", "b"};
43168404Spjd        // with no security manager we shuld be able to do these calls OK
44168404Spjd        test(path);
45168404Spjd        // add our own security manager
46168404Spjd        System.setSecurityManager(sm);
47168404Spjd        // now each of the calls should raise an exception
48168404Spjd        try {
49168404Spjd            Beans.setDesignTime(true);
50168404Spjd            throw new Error("Beans.setDesignTime should throw SecurityException");
51168404Spjd        } catch (SecurityException exception) {
52168404Spjd            // expected exception
53168404Spjd        }
54168404Spjd        try {
55168404Spjd            Beans.setGuiAvailable(true);
56185029Spjd            throw new Error("Beans.setGuiAvailable should throw SecurityException");
57185029Spjd        } catch (SecurityException exception) {
58185029Spjd            // expected exception
59185029Spjd        }
60185029Spjd        try {
61185029Spjd            Introspector.setBeanInfoSearchPath(path);
62185029Spjd            throw new Error("Introspector.setBeanInfoSearchPath should throw SecurityException");
63185029Spjd        } catch (SecurityException exception) {
64185029Spjd            // expected exception
65185029Spjd        }
66185029Spjd        try {
67168404Spjd            PropertyEditorManager.setEditorSearchPath(path);
68168404Spjd            throw new Error("PropertyEditorManager.setEditorSearchPath should throw SecurityException");
69168404Spjd        } catch (SecurityException exception) {
70168404Spjd            // expected exception
71168404Spjd        }
72168404Spjd        // now set the security manager to be friendly
73168404Spjd        sm.friendly = true;
74168404Spjd        // now the calls should be OK again.
75168404Spjd        test(path);
76168404Spjd    }
77185029Spjd
78168404Spjd    private static void test(String[] path) {
79185029Spjd        try {
80168404Spjd            Beans.setDesignTime(true);
81185029Spjd            Beans.setGuiAvailable(true);
82185029Spjd            Introspector.setBeanInfoSearchPath(path);
83185029Spjd            PropertyEditorManager.setEditorSearchPath(path);
84185029Spjd        } catch (SecurityException exception) {
85185029Spjd            throw new Error("unexpected security exception", exception);
86185029Spjd        }
87185029Spjd    }
88185029Spjd
89185029Spjd    private static class OurSecurityManager extends SecurityManager {
90185029Spjd        boolean friendly;
91185029Spjd
92185029Spjd        public void checkPropertiesAccess() {
93185029Spjd            if (!friendly) {
94185029Spjd                throw new SecurityException("No way");
95168404Spjd            }
96168404Spjd        }
97168404Spjd    }
98168404Spjd}
99168404Spjd