1/*
2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * 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 * @run main/othervm Test4080522
33 * @author Graham Hamilton
34 */
35
36import java.beans.Introspector;
37import java.beans.Beans;
38import java.beans.PropertyEditorManager;
39
40public class Test4080522 {
41    public static void main(String[] args) {
42        OurSecurityManager sm = new OurSecurityManager();
43        String[] path = {"a", "b"};
44        // with no security manager we shuld be able to do these calls OK
45        test(path);
46        // add our own security manager
47        System.setSecurityManager(sm);
48        // now each of the calls should raise an exception
49        try {
50            Beans.setDesignTime(true);
51            throw new Error("Beans.setDesignTime should throw SecurityException");
52        } catch (SecurityException exception) {
53            // expected exception
54        }
55        try {
56            Beans.setGuiAvailable(true);
57            throw new Error("Beans.setGuiAvailable should throw SecurityException");
58        } catch (SecurityException exception) {
59            // expected exception
60        }
61        try {
62            Introspector.setBeanInfoSearchPath(path);
63            throw new Error("Introspector.setBeanInfoSearchPath should throw SecurityException");
64        } catch (SecurityException exception) {
65            // expected exception
66        }
67        try {
68            PropertyEditorManager.setEditorSearchPath(path);
69            throw new Error("PropertyEditorManager.setEditorSearchPath should throw SecurityException");
70        } catch (SecurityException exception) {
71            // expected exception
72        }
73        // now set the security manager to be friendly
74        sm.friendly = true;
75        // now the calls should be OK again.
76        test(path);
77    }
78
79    private static void test(String[] path) {
80        try {
81            Beans.setDesignTime(true);
82            Beans.setGuiAvailable(true);
83            Introspector.setBeanInfoSearchPath(path);
84            PropertyEditorManager.setEditorSearchPath(path);
85        } catch (SecurityException exception) {
86            throw new Error("unexpected security exception", exception);
87        }
88    }
89
90    private static class OurSecurityManager extends SecurityManager {
91        boolean friendly;
92
93        public void checkPropertiesAccess() {
94            if (!friendly) {
95                throw new SecurityException("No way");
96            }
97        }
98    }
99}
100