J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001-2003 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 | /* @test |
| 25 | * @bug 4465315 |
| 26 | * @summary The RMI runtime's server-side DGC implementation object should |
| 27 | * not be exported with the arbitrary access control context that was in |
| 28 | * effect when it gets lazily created. For example, calls to it should not |
| 29 | * fail due to the "accept" SocketPermission check simply because of the |
| 30 | * access control context that it was exported with. |
| 31 | * @author Peter Jones |
| 32 | * |
| 33 | * @library ../../testlibrary |
| 34 | * @build TestLibrary |
| 35 | * @build DGCImplInsulation |
| 36 | * @build DGCImplInsulation_Stub |
| 37 | * @run main/othervm/policy=security.policy DGCImplInsulation |
| 38 | */ |
| 39 | |
| 40 | import java.lang.ref.Reference; |
| 41 | import java.lang.ref.ReferenceQueue; |
| 42 | import java.lang.ref.WeakReference; |
| 43 | import java.net.SocketPermission; |
| 44 | import java.rmi.MarshalledObject; |
| 45 | import java.rmi.Remote; |
| 46 | import java.rmi.server.UnicastRemoteObject; |
| 47 | import java.security.AccessControlContext; |
| 48 | import java.security.CodeSource; |
| 49 | import java.security.Permissions; |
| 50 | import java.security.PrivilegedExceptionAction; |
| 51 | import java.security.ProtectionDomain; |
| 52 | import java.security.cert.Certificate; |
| 53 | |
| 54 | public class DGCImplInsulation implements java.rmi.Remote { |
| 55 | |
| 56 | private static final long TIMEOUT = 5000; |
| 57 | |
| 58 | public static void main(String[] args) throws Exception { |
| 59 | |
| 60 | TestLibrary.suggestSecurityManager(null); |
| 61 | |
| 62 | Permissions perms = new Permissions(); |
| 63 | perms.add(new SocketPermission("*:1024-", "listen")); |
| 64 | AccessControlContext acc = |
| 65 | new AccessControlContext(new ProtectionDomain[] { |
| 66 | new ProtectionDomain( |
| 67 | new CodeSource(null, (Certificate[]) null), perms) }); |
| 68 | |
| 69 | Remote impl = new DGCImplInsulation();; |
| 70 | |
| 71 | try { |
| 72 | Remote stub = (Remote) java.security.AccessController.doPrivileged( |
| 73 | new ExportAction(impl)); |
| 74 | System.err.println("exported remote object; local stub: " + stub); |
| 75 | |
| 76 | MarshalledObject mobj = new MarshalledObject(stub); |
| 77 | stub = (Remote) mobj.get(); |
| 78 | System.err.println("marshalled/unmarshalled stub: " + stub); |
| 79 | |
| 80 | ReferenceQueue refQueue = new ReferenceQueue(); |
| 81 | Reference weakRef = new WeakReference(impl, refQueue); |
| 82 | impl = null; |
| 83 | System.gc(); |
| 84 | if (refQueue.remove(TIMEOUT) == weakRef) { |
| 85 | throw new RuntimeException( |
| 86 | "TEST FAILED: remote object garbage collected"); |
| 87 | } else { |
| 88 | System.err.println("TEST PASSED"); |
| 89 | stub = null; |
| 90 | System.gc(); |
| 91 | Thread.sleep(2000); |
| 92 | System.gc(); |
| 93 | } |
| 94 | } finally { |
| 95 | try { |
| 96 | UnicastRemoteObject.unexportObject(impl, true); |
| 97 | } catch (Exception e) { |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private static class ExportAction implements PrivilegedExceptionAction { |
| 103 | private final Remote impl; |
| 104 | ExportAction(Remote impl) { |
| 105 | this.impl = impl; |
| 106 | } |
| 107 | public Object run() throws Exception { |
| 108 | return UnicastRemoteObject.exportObject(impl); |
| 109 | } |
| 110 | } |
| 111 | } |