/** 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) {
    Ping obj = null;
    Registry registry = null;

    try {
      /*
       * create registry
       */
      TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");

      System.err.println("creating Registry...");

      registry = TestLibrary.createRegistryOnUnusedPort();
      int port = TestLibrary.getRegistryPort(registry);
      /*
       * create object with custom ref and bind in registry
       */
      System.err.println("creating UseCustomRef...");
      UseCustomRef cr = new UseCustomRef();
      RemoteRef ref = cr.getRef();
      if (!(ref instanceof CustomServerRef)) {
        TestLibrary.bomb("test failed: reference not " + "instanceof CustomServerRef");
      }

      String name = "//:" + port + "/UseCustomRef";
      //      String name = "UseCustomRef";
      System.err.println("binding object in registry...");
      Naming.rebind(name, cr);

      /*
       * look up object and invoke its ping method
       */
      System.err.println("ping object...");
      obj = (Ping) Naming.lookup(name);
      obj.ping();

      /*
       * pass object with custom ref in remote call
       */
      System.err.println("pass object in remote call...");
      obj.receiveAndPing(cr);

      /*
       * write remote object with custom ref to output stream
       */
      System.err.println("writing remote object to stream...");
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(bout);
      out.writeObject(cr);
      out.flush();
      out.close();

      /*
       * read back remote object from output stream
       */
      System.err.println("reading remote object from stream...");
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
      cr = (UseCustomRef) in.readObject();

      /*
       * re-export object and ping
       */
      System.err.println("re-export object read...");
      cr.exportObject();
      System.err.println("look up object again...");
      Naming.rebind(name, cr);
      System.err.println("ping object read...");
      obj = (Ping) Naming.lookup(name);
      obj.ping();
      System.err.println("TEST PASSED");
      Naming.unbind(name);
      cr = null;

    } catch (Exception e) {
      TestLibrary.bomb("test failed with exception: ", e);
    } finally {
      TestLibrary.unexport(obj);
      TestLibrary.unexport(registry);

      registry = null;
      obj = null;
    }
  }
Exemplo n.º 3
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");
    }
  }