1 /*
   2  * Copyright (c) 1998, 2014, 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 4109103
  26  * @summary rmid should annotate child process output
  27  *
  28  * @author Laird Dornin; code borrowed from Ann Wollrath
  29  *
  30  * @library ../../../testlibrary
  31  * @build TestLibrary RMID MyRMI CheckAnnotations_Stub
  32  * @run main/othervm/policy=security.policy/timeout=480 CheckAnnotations
  33  */
  34 
  35 import java.io.*;
  36 import java.rmi.*;
  37 import java.rmi.server.*;
  38 import java.rmi.activation.*;
  39 import java.security.CodeSource;
  40 import java.util.Properties;
  41 import java.util.StringTokenizer;
  42 
  43 
  44 public class CheckAnnotations
  45     extends Activatable implements MyRMI, Runnable
  46 {
  47 
  48     private static Object dummy = new Object();
  49     private static MyRMI myRMI = null;
  50 
  51     // buffers to store rmid output.
  52     private static ByteArrayOutputStream rmidOut = new ByteArrayOutputStream();
  53     private static ByteArrayOutputStream rmidErr = new ByteArrayOutputStream();
  54 
  55     public static void main(String args[]) {
  56         /*
  57          * The following line is required with the JDK 1.2 VM so that the
  58          * VM can exit gracefully when this test completes.  Otherwise, the
  59          * conservative garbage collector will find a handle to the server
  60          * object on the native stack and not clear the weak reference to
  61          * it in the RMI runtime's object table.
  62          */
  63         Object dummy1 = new Object();
  64         RMID rmid = null;
  65 
  66         System.err.println("\nRegression test for bug/rfe 4109103\n");
  67 
  68         try {
  69 
  70             // Set security manager according to the
  71             // testlibrary.
  72             TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);
  73 
  74             // start an rmid.
  75             RMID.removeLog();
  76             rmid = RMID.createRMID(rmidOut, rmidErr, false);
  77             rmid.start();
  78 
  79             /* Cause activation groups to have a security policy that will
  80              * allow security managers to be downloaded and installed
  81              */
  82             Properties p = new Properties();
  83             // this test must always set policies/managers in its
  84             // activation groups
  85             p.put("java.security.policy",
  86                   TestParams.defaultGroupPolicy);
  87             p.put("java.security.manager",
  88                   TestParams.defaultSecurityManager);
  89 
  90             /* new desc - we will reuse in order to get multiple vms.*/
  91             System.err.println("Create activation group in this VM");
  92             ActivationGroupDesc groupDesc =
  93                 new ActivationGroupDesc(p, null);
  94             ActivationSystem system = ActivationGroup.getSystem();
  95             ActivationGroupID groupID = system.registerGroup(groupDesc);
  96             ActivationGroup.createGroup(groupID, groupDesc, 0);
  97 
  98             ActivationDesc desc = new ActivationDesc
  99                 ("CheckAnnotations", null, null);
 100             myRMI = (MyRMI) Activatable.register(desc);
 101 
 102             /* The test-
 103              * Loop a bunch of times to force activator to
 104              * spawn VMs (groups)
 105              */
 106             for (int i = 0; i < 3; i++) {
 107 
 108                 // object activated in annotation check via method call
 109                 if(!checkAnnotations(i-1)) {
 110                     TestLibrary.bomb("Test failed: output improperly annotated.");
 111                 }
 112 
 113                 /*
 114                  * Clean up object too.
 115                  */
 116                 System.err.println
 117                     ("Deactivate object via method call");
 118                 myRMI.shutdown();
 119             }
 120             System.err.println
 121                 ("\nsuccess: CheckAnnotations test passed ");
 122 
 123         } catch (Exception e) {
 124             TestLibrary.bomb("\nfailure: unexpected exception ", e);
 125         } finally {
 126             try {
 127                 Thread.sleep(4000);
 128             } catch (InterruptedException e) {
 129             }
 130 
 131             myRMI = null;
 132             System.err.println("rmid shut down");
 133             rmid.cleanup();
 134         }
 135     }
 136 
 137     /**
 138      * check to make sure that the output from a spawned vm is
 139      * formatted/annotated properly.
 140      */
 141     public static boolean checkAnnotations(int iteration)
 142         throws IOException
 143     {
 144         try {
 145             Thread.sleep(5000);
 146         } catch(Exception e) {
 147             System.err.println(e.getMessage());
 148         }
 149 
 150         /**
 151          * cause the spawned vm to generate output that will
 152          * be checked for proper annotation.  printOut is
 153          * actually being called on an activated implementation.
 154          */
 155         myRMI.printOut("out" + iteration);
 156         myRMI.printErr("err" + iteration);
 157         myRMI.printOut("out" + iteration);
 158         myRMI.printErr("err" + iteration);
 159 
 160         /* we have to wait for output to filter down
 161          * from children so we can read it before we
 162          * kill rmid.
 163          */
 164 
 165         String outString = null;
 166         String errString = null;
 167 
 168         for (int i = 0 ; i < 5 ; i ++ ) {
 169             // have to give output from rmid time to trickle down to
 170             // this process
 171             try {
 172                 Thread.sleep(4000);
 173             } catch(InterruptedException e) {
 174             }
 175 
 176             outString = rmidOut.toString();
 177             errString = rmidErr.toString();
 178 
 179             if ((!outString.equals("")) &&
 180                 (!errString.equals("")))
 181             {
 182                 System.err.println("obtained annotations");
 183                 break;
 184             }
 185             System.err.println("rmid output not yet received, retrying...");
 186         }
 187 
 188         rmidOut.reset();
 189         rmidErr.reset();
 190 
 191         // only test when we are annotating..., first run does not annotate
 192         if (iteration >= 0) {
 193             System.err.println("Checking annotations...");
 194             System.err.println(outString);
 195             System.err.println(errString);
 196 
 197             StringTokenizer stOut = new StringTokenizer(outString, ":");
 198             StringTokenizer stErr = new StringTokenizer(errString, ":");
 199 
 200             String execErr = null;
 201             String execOut = null;
 202             String destOut = null;
 203             String destErr = null;
 204             String outTmp  = null;
 205             String errTmp  = null;
 206 
 207             while (stOut.hasMoreTokens()) {
 208                 execOut = outTmp;
 209                 outTmp  = destOut;
 210                 destOut = stOut.nextToken();
 211             }
 212             while (stErr.hasMoreTokens()) {
 213                 execErr = errTmp;
 214                 errTmp  = destErr;
 215                 destErr = stErr.nextToken();
 216             }
 217 
 218             if ((execErr == null)||(errTmp == null)||
 219                 (destErr == null)) {
 220                 return false;
 221             }
 222             if ((execOut == null)||(outTmp == null)||
 223                 (destOut == null)) {
 224                 return false;
 225             }
 226 
 227             // just make sure that last two strings are what we expect.
 228             if (execOut.equals("ExecGroup-" + iteration)
 229                 && (new String(destOut.substring(0,4)).equals("out" +
 230                                                               iteration))
 231                 && (execErr.equals("ExecGroup-"+iteration))
 232                 && (new String(destErr.substring(0,4)).equals("err" +
 233                                                               iteration)) ) {
 234                 return true;
 235             } else {
 236                 return false;
 237             }
 238         }
 239         return true;
 240     }
 241 
 242     // implementation of MyRMI, make this object activatable.
 243     public CheckAnnotations
 244         (ActivationID id, MarshalledObject mo)
 245      throws RemoteException {
 246 
 247         // register/export anonymously
 248         super(id,0);
 249     }
 250 
 251     public void printOut(String toPrint) {
 252         System.out.println(toPrint);
 253     }
 254 
 255     public void printErr(String toPrint) {
 256         System.err.println(toPrint);
 257     }
 258 
 259     /**
 260      * Spawns a thread to deactivate the object.
 261      */
 262     public void shutdown() throws Exception {
 263         (new Thread(this,"CheckAnnotations")).start();
 264     }
 265 
 266     /**
 267      * Thread to deactivate object. First attempts to make object
 268      * inactive (via the inactive method).  If that fails (the
 269      * object may still have pending/executing calls), then
 270      * unexport the object forcibly.
 271      */
 272     public void run() {
 273         ActivationLibrary.deactivate(this, getID());
 274     }
 275 }