public void test_exec() throws IOException {

    final ScriptProcess p = datastore.newProcess(TEST_USER, "exec.js", "", "owner");

    ScriptAction r = p.call();

    p.save();

    assertTrue("Exec correctly", r instanceof Exec);
    assertEquals("Exec good program", "returnArg.js", ((Exec) r).getScript());
    assertEquals("Exec good args", "arg1 arg2", ((Exec) r).getArgs());

    r.visit(
        new ScriptusFacade(datastore, c, m) {

          @Override
          public void execute(UUID pid) {

            ScriptProcess pp = datastore.getProcess(pid);

            assertEquals("good pid", p.getPid(), pid);
            assertEquals("good source", "returnArg.js", pp.getSourceName());
            assertEquals("good args", "arg1 arg2", pp.getArgs());

            ScriptAction aa = pp.call();

            assertEquals("good class result", NormalTermination.class, aa.getClass());
            assertEquals("goood result", "resultarg1 arg2", ((NormalTermination) aa).getResult());
          }
        },
        p);
  }
  public void test_forkNoPrefix() throws IOException {

    ScriptProcess p = datastore.newProcess(TEST_USER, "forkNoPrefix.js", "", "owner");

    ScriptAction r = p.call();

    assertTrue("Forked correctly", r instanceof Fork);

    r.visit(new ScriptusFacade(datastore, c, m), p);
  }
  public void test_breakSecurity5() throws IOException {

    ScriptProcess p = datastore.newProcess(TEST_USER, "breakSec5.js", "", "owner");

    ScriptAction r = p.call();

    assertEquals("Failed correctly", ErrorTermination.class, r.getClass());

    System.out.println(((ErrorTermination) r).getError());

    r.visit(new ScriptusFacade(datastore, c, m), p);
  }
  public void test_eval() throws IOException {

    ScriptProcess p = datastore.newProcess(TEST_USER, "eval.js", "", "owner");

    ScriptAction r = p.call();

    assertTrue("Listened correctly", r instanceof Listen);

    p.save();

    r.visit(new ScriptusFacade(datastore, c, m), p);
  }
  public void test_defaultListen() throws IOException {

    ScriptProcess p = datastore.newProcess(TEST_USER, "defaultListen.js", "", "owner");

    ScriptAction r = p.call();

    assertTrue("Listened correctly", r instanceof Listen);
    assertNull("Listened correctly to", ((Listen) r).getWho());

    p.save();

    r.visit(new ScriptusFacade(datastore, c, m), p);
  }
  public void test_askTimeout() throws IOException {

    ScriptProcess p = datastore.newProcess(TEST_USER, "askTimeout.js", "", "owner");

    ScriptAction r = p.call();

    assertTrue("Asked correctly", r instanceof Ask);
    assertTrue("Asked correctly owner", ((Ask) r).getWho().equals("foo"));

    p.save();

    r.visit(new ScriptusFacade(datastore, c, m), p);
  }
  public void test_say() throws IOException {

    ScriptProcess p = datastore.newProcess(TEST_USER, "say.js", "", "owner");

    ScriptAction r = p.call();

    assertTrue("Said correctly", r instanceof Say);
    assertTrue("Said correctly to user", ((Say) r).getWho().equals("foo"));
    assertTrue("Said correctly message", ((Say) r).getMsg().equals("message"));

    p.save();

    r.visit(new ScriptusFacade(datastore, c, m), p);
  }
  public void test_log() throws IOException {

    ScriptProcess p = datastore.newProcess(TEST_USER, "log.js", "", "owner");

    ScriptAction r = p.call();

    assertTrue("Correct result", r instanceof NormalTermination);

    NormalTermination n = (NormalTermination) r;

    r.visit(new ScriptusFacade(datastore, c, m), p); // sould say

    assertEquals("Correct result", "result", n.getResult());
  }
  /**
   * Illustrates the problems of scripts breaking because of prototypes.
   *
   * @throws IOException
   */
  public void test_prototypes() throws IOException {

    ScriptProcess p = datastore.newProcess(TEST_USER, "prototypes.js", "", "owner");

    ScriptAction r = p.call();

    assertTrue("Correct result", r instanceof Say);

    p.save();

    r.visit(
        new ScriptusFacade(datastore, c, m) {

          @Override
          public void execute(UUID pid) {
            ScriptProcess pp = datastore.getProcess(pid);
            ScriptAction rr = pp.call();
            assertEquals("final result", NormalTermination.class, rr.getClass());
            assertEquals("final result value OK", "foo", ((NormalTermination) rr).getResult());
          }
        },
        p);
  }
  public void test_kill() throws IOException {

    final ScriptProcess p = datastore.newProcess(TEST_USER, "kill.js", "", "owner");

    p.save();

    ScriptAction r = p.call();

    assertTrue("Forked correctly", r instanceof Fork);

    final ThreadLocal<Boolean> executedParentPostFork = new ThreadLocal<Boolean>();
    final ThreadLocal<Boolean> executedParentPostKill = new ThreadLocal<Boolean>();
    final ThreadLocal<Boolean> executedChild = new ThreadLocal<Boolean>();

    System.out.println("pid=" + p.getPid());

    ScriptusFacade testFacade =
        new ScriptusFacade(datastore, c, m) {

          private UUID childPid;

          @Override
          public void execute(UUID pid) {

            if (!pid.equals(p.getPid())) {
              // executing child

              executedChild.set(Boolean.TRUE);

              childPid = pid;

              super.execute(pid);

              return;
            }

            if (pid.equals(p.getPid())) {

              // executing parent

              if (Boolean.TRUE.equals(executedParentPostFork.get())) {

                // post-kill

                executedParentPostKill.set(Boolean.TRUE);

              } else {

                // post-fork, pre-kill, pid

                executedParentPostFork.set(Boolean.TRUE);

                ScriptProcess p2 = datastore.getProcess(pid);

                ScriptAction r2 = p2.call();

                p2.save();

                assertTrue("Killed correctly", r2 instanceof Kill);

                r2.visit(this, p2);

                boolean caughtNotFoundExcepton = false;

                try {
                  datastore.getProcess(childPid);
                } catch (ProcessNotFoundException sre) {
                  caughtNotFoundExcepton = true;
                }

                assertTrue("process doesn't exist", caughtNotFoundExcepton);
              }
            }
          }
        };

    assertTrue("forking correctly", r instanceof Fork);

    r.visit(testFacade, p);

    assertEquals("Executed child", Boolean.TRUE, executedChild.get());
    assertEquals("Executed parent (post-fork)", Boolean.TRUE, executedParentPostFork.get());
    assertEquals("Executed parent (post-kill)", Boolean.TRUE, executedParentPostKill.get());
  }
  /** tests for when child process finished after wait() is called */
  public void test_wait2() throws IOException {

    final ScriptProcess p = datastore.newProcess(TEST_USER, "wait2.js", "", "owner");

    p.save();

    ScriptAction r = p.call();

    assertTrue("Forked correctly", r instanceof Fork);

    final ThreadLocal<Boolean> executedParentPostFork = new ThreadLocal<Boolean>();
    final ThreadLocal<Boolean> executedParentPostWait = new ThreadLocal<Boolean>();
    final ThreadLocal<Boolean> executedChild = new ThreadLocal<Boolean>();
    final ThreadLocal<Boolean> executedChildPostSleep = new ThreadLocal<Boolean>();

    ScriptusFacade testFacade =
        new ScriptusFacade(datastore, c, m) {

          private UUID childPid;

          @Override
          public void execute(UUID pid) {

            if (!pid.equals(p.getPid())) {
              // executing child

              if (Boolean.TRUE.equals(executedChild.get())) {

                executedChildPostSleep.set(Boolean.TRUE);

                ScriptProcess p2 = datastore.getProcess(pid);

                ScriptAction r2 = p2.call();

                assertTrue("in child termination", r2 instanceof Termination);

                p2.save();

                r2.visit(this, p2);

              } else {

                executedChild.set(Boolean.TRUE);

                childPid = pid;

                ScriptProcess p2 = datastore.getProcess(pid);

                ScriptAction r2 = p2.call();

                p2.save();

                assertTrue("in child sleep", r2 instanceof Sleep);
              }

              return;
            }

            if (pid.equals(p.getPid())) {

              if (Boolean.TRUE.equals(executedParentPostFork.get())) {

                executedParentPostWait.set(Boolean.TRUE);

                ScriptAction enfin = datastore.getProcess(pid).call();

                assertTrue("script finished", enfin instanceof Termination);
                assertEquals(
                    "script result OK",
                    "waitedfooslept" + childPid,
                    ((Termination) enfin).getResult());

              } else {

                executedParentPostFork.set(Boolean.TRUE);

                ScriptProcess p2 = datastore.getProcess(pid);

                ScriptAction r2 = p2.call();

                p2.save();

                assertTrue("Waited correctly", r2 instanceof Wait);

                // pause thread until child has termination

                r2.visit(this, p2);

                // assert parent is still waiting
                // wake child and execute
                execute(childPid);
              }
            }
          }
        };

    r.visit(testFacade, p);

    assertEquals("Executed child", Boolean.TRUE, executedChild.get());
    assertEquals("Executed child post-sleep", Boolean.TRUE, executedChildPostSleep.get());

    assertEquals("Executed parent (post-fork)", Boolean.TRUE, executedParentPostFork.get());
    assertEquals("Executed parent (post-wait)", Boolean.TRUE, executedParentPostWait.get());
  }