1/*
2 * Copyright (c) 1998, 2012, 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/* @test
25 * @bug 4140511
26 * @summary RMIClassLoader's loadClass() methods, when there is no security
27 * manager installed, should not create or use RMI class loader instances for
28 * the requested codebases, but they should still succeed to load classes
29 * that can be found by delegation to parent class loader that would have
30 * been used for the RMI class loader instance.
31 * @author Peter Jones
32 *
33 * @build Dummy LocalDummy
34 * @run main/othervm/timeout=120 NoSecurityManager
35 */
36
37import java.io.*;
38import java.net.*;
39import java.security.*;
40import java.rmi.server.RMIClassLoader;
41
42/**
43 * NoSecurityManager verifies the behavior of the RMIClassLoader.loadClass()
44 * methods when there is no security manager installed in JDK1.2 as prescribed
45 * by RFE 4140511.
46 *
47 * First, a class installed in a codebase (and not in the test's class path)
48 * is loaded using RMIClassLoader.loadClass(); this should fail with a
49 * ClassNotFoundException (it failed with a SecurityException in JDK 1.1).
50 *
51 * Second, a class installed in the test's class path is loaded using
52 * RMIClassLoader.loadClass(), which should succeed (again, this used
53 * to fail with a SecurityException in JDK 1.1).
54 */
55public class NoSecurityManager {
56
57    /** name of target class to attempt to load */
58    private static final String className = "Dummy";
59
60    /** name of file containing definition for target class */
61    private static final String classFileName = className + ".class";
62
63    public static void main(String[] args) throws Exception {
64        /*
65         * Specify the file to contain the class definition.
66         * Make sure that there is a "classes" subdirectory underneath
67         * the working directory to be used as the codebase for the
68         * class definition to be located.
69         */
70        File dstDir = new File(System.getProperty("user.dir"), "codebase");
71        if (!dstDir.exists()) {
72            if (!dstDir.mkdir()) {
73                throw new RuntimeException(
74                    "could not create codebase directory");
75            }
76        }
77        File dstFile = new File(dstDir, classFileName);
78
79        /*
80         * Specify where we will copy the class definition from, if
81         * necessary.  After the test is built, the class file can be
82         * found in the "test.classes" directory.
83         */
84        File srcDir = new File(System.getProperty("test.classes", "."));
85        File srcFile = new File(srcDir, classFileName);
86
87        /*
88         * If the class definition is not already located at the codebase,
89         * copy it there from the test build area.
90         */
91        if (!dstFile.exists()) {
92            if (!srcFile.exists()) {
93                throw new RuntimeException(
94                    "could not find class file to install in codebase " +
95                    "(try rebuilding the test)");
96            }
97            if (!srcFile.renameTo(dstFile)) {
98                throw new RuntimeException(
99                    "could not install class file in codebase");
100            }
101        }
102
103        /*
104         * After the class definition is successfully installed at the
105         * codebase, delete it from the test's CLASSPATH, so that it will
106         * not be found there first before the codebase is searched.
107         */
108        if (srcFile.exists()) {
109            if (!srcFile.delete()) {
110                throw new RuntimeException(
111                    "could not delete duplicate class file in CLASSPATH");
112            }
113        }
114
115        /*
116         * Obtain the URL for the codebase.
117         */
118        URL codebaseURL = new URL("file", "",
119            dstDir.getAbsolutePath().replace(File.separatorChar, '/') + "/");
120
121        /*
122         * No security manager has been set: verify that we cannot load
123         * a class from a specified codebase (that is not in the class path).
124         */
125        try {
126            RMIClassLoader.loadClass(codebaseURL, className);
127            throw new RuntimeException(
128                "TEST FAILED: class loaded successfully from codebase");
129        } catch (ClassNotFoundException e) {
130            System.err.println(e.toString());
131        }
132
133        /*
134         * No security manager has been set: verify that we can still
135         * load a class available in the context class loader (class path).
136         */
137        RMIClassLoader.loadClass(codebaseURL, "LocalDummy");
138        System.err.println("TEST PASSED: local class loaded successfully");
139
140        /*
141         * Verify that getClassLoader returns context class loader
142         * if no security manager is set.
143         */
144        System.err.println("/nTest getClassLoader with no security manager set");
145        ClassLoader loader = RMIClassLoader.getClassLoader("http://codebase");
146        if (loader == Thread.currentThread().getContextClassLoader()) {
147            System.err.println("TEST PASSED: returned context class loader");
148        } else {
149            throw new RuntimeException(
150                "TEST FAILED: returned RMI-created class loader");
151        }
152    }
153}
154