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