1 /*
   2  * Copyright (c) 2001, 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 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 DGCImplInsulation_Stub
  35  * @run main/othervm/policy=security.policy DGCImplInsulation
  36  */
  37 
  38 import java.lang.ref.Reference;
  39 import java.lang.ref.ReferenceQueue;
  40 import java.lang.ref.WeakReference;
  41 import java.net.SocketPermission;
  42 import java.rmi.MarshalledObject;
  43 import java.rmi.Remote;
  44 import java.rmi.server.UnicastRemoteObject;
  45 import java.security.AccessControlContext;
  46 import java.security.CodeSource;
  47 import java.security.Permissions;
  48 import java.security.PrivilegedExceptionAction;
  49 import java.security.ProtectionDomain;
  50 import java.security.cert.Certificate;
  51 
  52 public class DGCImplInsulation implements java.rmi.Remote {
  53 
  54     private static final long TIMEOUT = 5000;
  55 
  56     public static void main(String[] args) throws Exception {
  57 
  58         TestLibrary.suggestSecurityManager(null);
  59 
  60         Permissions perms = new Permissions();
  61         perms.add(new SocketPermission("*:1024-", "listen"));
  62         AccessControlContext acc =
  63             new AccessControlContext(new ProtectionDomain[] {
  64                 new ProtectionDomain(
  65                     new CodeSource(null, (Certificate[]) null), perms) });
  66 
  67         Remote impl = new DGCImplInsulation();;
  68 
  69         try {
  70             Remote stub = (Remote) java.security.AccessController.doPrivileged(
  71                 new ExportAction(impl));
  72             System.err.println("exported remote object; local stub: " + stub);
  73 
  74             MarshalledObject mobj = new MarshalledObject(stub);
  75             stub = (Remote) mobj.get();
  76             System.err.println("marshalled/unmarshalled stub: " + stub);
  77 
  78             ReferenceQueue refQueue = new ReferenceQueue();
  79             Reference weakRef = new WeakReference(impl, refQueue);
  80             impl = null;
  81             System.gc();
  82             if (refQueue.remove(TIMEOUT) == weakRef) {
  83                 throw new RuntimeException(
  84                     "TEST FAILED: remote object garbage collected");
  85             } else {
  86                 System.err.println("TEST PASSED");
  87                 stub = null;
  88                 System.gc();
  89                 Thread.sleep(2000);
  90                 System.gc();
  91             }
  92         } finally {
  93             try {
  94                 UnicastRemoteObject.unexportObject(impl, true);
  95             } catch (Exception e) {
  96             }
  97         }
  98     }
  99 
 100     private static class ExportAction implements PrivilegedExceptionAction {
 101         private final Remote impl;
 102         ExportAction(Remote impl) {
 103             this.impl = impl;
 104         }
 105         public Object run() throws Exception {
 106             return UnicastRemoteObject.exportObject(impl);
 107         }
 108     }
 109 }