/** check to make sure that the output from a spawned vm is formatted/annotated properly. */
  public static boolean checkAnnotations(int iteration) throws IOException {
    try {
      Thread.sleep(5000);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }

    /**
     * cause the spawned vm to generate output that will be checked for proper annotation. printOut
     * is actually being called on an activated implementation.
     */
    myRMI.printOut("out" + iteration);
    myRMI.printErr("err" + iteration);
    myRMI.printOut("out" + iteration);
    myRMI.printErr("err" + iteration);

    /* we have to wait for output to filter down
     * from children so we can read it before we
     * kill rmid.
     */

    String outString = null;
    String errString = null;

    for (int i = 0; i < 5; i++) {
      // have to give output from rmid time to trickle down to
      // this process
      try {
        Thread.sleep(4000);
      } catch (InterruptedException e) {
      }

      outString = rmidOut.toString();
      errString = rmidErr.toString();

      if ((!outString.equals("")) && (!errString.equals(""))) {
        System.err.println("obtained annotations");
        break;
      }
      System.err.println("rmid output not yet received, retrying...");
    }

    rmidOut.reset();
    rmidErr.reset();

    // only test when we are annotating..., first run does not annotate
    if (iteration >= 0) {
      System.err.println("Checking annotations...");
      System.err.println(outString);
      System.err.println(errString);

      StringTokenizer stOut = new StringTokenizer(outString, ":");
      StringTokenizer stErr = new StringTokenizer(errString, ":");

      String execErr = null;
      String execOut = null;
      String destOut = null;
      String destErr = null;
      String outTmp = null;
      String errTmp = null;

      while (stOut.hasMoreTokens()) {
        execOut = outTmp;
        outTmp = destOut;
        destOut = stOut.nextToken();
      }
      while (stErr.hasMoreTokens()) {
        execErr = errTmp;
        errTmp = destErr;
        destErr = stErr.nextToken();
      }

      if ((execErr == null) || (errTmp == null) || (destErr == null)) {
        return false;
      }
      if ((execOut == null) || (outTmp == null) || (destOut == null)) {
        return false;
      }

      // just make sure that last two strings are what we expect.
      if (execOut.equals("ExecGroup-" + iteration)
          && (new String(destOut.substring(0, 4)).equals("out" + iteration))
          && (execErr.equals("ExecGroup-" + iteration))
          && (new String(destErr.substring(0, 4)).equals("err" + iteration))) {
        return true;
      } else {
        return false;
      }
    }
    return true;
  }
Exemplo n.º 2
0
  public static void main(String args[]) {

    sameGroup = true;

    RMID rmid = null;

    System.err.println("\nRegression test for bug/rfe 4179055\n");

    try {
      TestLibrary.suggestSecurityManager("java.lang.SecurityManager");

      registry = java.rmi.registry.LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);

      // must run with java.lang.SecurityManager or the test
      // result will be nullified if running with a build where
      // 4180392 has not been fixed.
      String smClassName = System.getSecurityManager().getClass().getName();
      if (!smClassName.equals("java.lang.SecurityManager")) {
        TestLibrary.bomb("Test must run with java.lang.SecurityManager");
      }

      // start an rmid.
      RMID.removeLog();
      rmid = RMID.createRMID();
      rmid.start();

      // rmid.addOptions(new String[] {"-C-Djava.rmi.server.logCalls=true"});

      // Ensure that activation groups run with the correct
      // security manager.
      //
      Properties p = new Properties();
      p.put("java.security.policy", TestParams.defaultGroupPolicy);
      p.put("java.security.manager", "java.lang.SecurityManager");

      // This action causes the following classes to be created
      // in this VM (RMI must permit the creation of these classes):
      //
      // sun.rmi.server.Activation$ActivationSystemImpl_Stub
      // sun.rmi.server.Activation$ActivationMonitorImpl_Stub
      //
      System.err.println("Create activation group, in a new VM");
      ActivationGroupDesc groupDesc = new ActivationGroupDesc(p, null);
      ActivationSystem system = ActivationGroup.getSystem();
      ActivationGroupID groupID = system.registerGroup(groupDesc);

      System.err.println("register activatable");
      // Fix for: 4271615: make sure activation group runs in a new VM
      ActivationDesc desc = new ActivationDesc(groupID, "StubClassesPermitted", null, null);
      canCreateStubs = (CanCreateStubs) Activatable.register(desc);

      // ensure registry stub can be passed in a remote call
      System.err.println("getting the registry");
      registry = canCreateStubs.getRegistry();

      // make sure a client cant load just any sun.* class, just
      // as a sanity check, try to create a class we are not
      // allowed to access but which was passed in a remote call
      try {
        System.err.println("accessing forbidden class");
        Object secureRandom = canCreateStubs.getForbiddenClass();

        TestLibrary.bomb(
            "test allowed to access forbidden class," + " sun.security.provider.SecureRandom");
      } catch (java.security.AccessControlException e) {

        // Make sure we received a *local* AccessControlException
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(bout);
        e.printStackTrace(ps);
        ps.flush();
        String trace = new String(bout.toByteArray());
        if ((trace.indexOf("exceptionReceivedFromServer") >= 0) || trace.equals("")) {
          throw e;
        }
        System.err.println("received expected local access control exception");
      }

      // make sure that an ActivationGroupID can be passed in a
      // remote call; this is slightly more inclusive than
      // just passing a reference to the activation system
      System.err.println("returning group desc");
      canCreateStubs.returnGroupID();

      // Clean up object
      System.err.println("Deactivate object via method call");
      canCreateStubs.shutdown();

      System.err.println("\nsuccess: StubClassesPermitted test passed ");

    } catch (Exception e) {
      TestLibrary.bomb("\nfailure: unexpected exception ", e);
    } finally {
      try {
        Thread.sleep(4000);
      } catch (InterruptedException e) {
      }

      canCreateStubs = null;
      ActivationLibrary.rmidCleanup(rmid);
      System.err.println("rmid shut down");
    }
  }