Path: blob/master/test/jdk/java/rmi/dgc/dgcImplInsulation/DGCImplInsulation.java
41153 views
/*1* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 446531525* @summary The RMI runtime's server-side DGC implementation object should26* not be exported with the arbitrary access control context that was in27* effect when it gets lazily created. For example, calls to it should not28* fail due to the "accept" SocketPermission check simply because of the29* access control context that it was exported with.30* @author Peter Jones31*32* @library ../../testlibrary33* @modules java.rmi/sun.rmi.registry34* java.rmi/sun.rmi.server35* java.rmi/sun.rmi.transport36* java.rmi/sun.rmi.transport.tcp37* @build TestLibrary DGCImplInsulation_Stub38* @run main/othervm/policy=security.policy DGCImplInsulation39*/4041import java.lang.ref.Reference;42import java.lang.ref.ReferenceQueue;43import java.lang.ref.WeakReference;44import java.net.SocketPermission;45import java.rmi.MarshalledObject;46import java.rmi.Remote;47import java.rmi.server.UnicastRemoteObject;48import java.security.AccessControlContext;49import java.security.CodeSource;50import java.security.Permissions;51import java.security.PrivilegedExceptionAction;52import java.security.ProtectionDomain;53import java.security.cert.Certificate;5455public class DGCImplInsulation implements java.rmi.Remote {5657private static final long TIMEOUT = 5000;5859public static void main(String[] args) throws Exception {6061TestLibrary.suggestSecurityManager(null);6263Permissions perms = new Permissions();64perms.add(new SocketPermission("*:1024-", "listen"));65AccessControlContext acc =66new AccessControlContext(new ProtectionDomain[] {67new ProtectionDomain(68new CodeSource(null, (Certificate[]) null), perms) });6970Remote impl = new DGCImplInsulation();;7172try {73Remote stub = (Remote) java.security.AccessController.doPrivileged(74new ExportAction(impl));75System.err.println("exported remote object; local stub: " + stub);7677MarshalledObject mobj = new MarshalledObject(stub);78stub = (Remote) mobj.get();79System.err.println("marshalled/unmarshalled stub: " + stub);8081ReferenceQueue refQueue = new ReferenceQueue();82Reference weakRef = new WeakReference(impl, refQueue);83impl = null;84System.gc();85if (refQueue.remove(TIMEOUT) == weakRef) {86throw new RuntimeException(87"TEST FAILED: remote object garbage collected");88} else {89System.err.println("TEST PASSED");90stub = null;91System.gc();92Thread.sleep(2000);93System.gc();94}95} finally {96try {97UnicastRemoteObject.unexportObject(impl, true);98} catch (Exception e) {99}100}101}102103private static class ExportAction implements PrivilegedExceptionAction {104private final Remote impl;105ExportAction(Remote impl) {106this.impl = impl;107}108public Object run() throws Exception {109return UnicastRemoteObject.exportObject(impl);110}111}112}113114115