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_sleepBadDate() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("Forked correctly", r instanceof ErrorTermination);
  }
  public void test_sleepDateObject() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("slept correctly", r instanceof Sleep);
  }
  public void test_evalBroken() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("Broken", r instanceof ErrorTermination);
  }
  public void test_fiddleWithAPI2() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("error condition", r instanceof ErrorTermination);
  }
  public void test_fiddleWithAPI() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("Didn't fork correctly", !(r instanceof Fork));
  }
  public void test_syntaxError() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("Error termination", r instanceof ErrorTermination);
  }
  public void test_defaultSay() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("Said correctly", r instanceof Say);
    assertNull("Said to owner correctly", ((Say) r).getWho());
  }
  public void test_exit() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("Exited correctly", r instanceof NormalTermination);
    assertTrue("correct exit", ((NormalTermination) r).getResult().equals("result"));
  }
  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_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_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_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_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());
  }
  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_getHttps() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("slept correctly", r instanceof Get);

    p.save();

    Get g = (Get) r;

    g.visit(new ScriptusFacade(datastore, c, m), p);

    p = datastore.getProcess(p.getPid());

    assertTrue("got content", p.getState() instanceof String);

    String content = (String) p.getState();

    assertTrue("content ok", content.contains("User-agent: *"));
  }
  public void test_sleepDuration() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("slept correctly", r instanceof Sleep);

    Sleep s = (Sleep) r;

    Calendar target = Calendar.getInstance();
    target.add(Calendar.YEAR, 1);
    target.add(Calendar.MONTH, 2);
    target.add(Calendar.DATE, 3);
    target.add(Calendar.HOUR, 4);

    Calendar c = s.getUntil();

    assertEquals("good year", target.get(Calendar.YEAR), c.get(Calendar.YEAR));
    assertEquals("good month", target.get(Calendar.MONTH), c.get(Calendar.MONTH));
    assertEquals("good day", target.get(Calendar.DATE), c.get(Calendar.DATE));
    assertEquals("good hour", target.get(Calendar.HOUR), c.get(Calendar.HOUR));
  }
  /**
   * 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_evalGetBug() throws IOException {

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

    ScriptAction r = p.call();

    assertTrue("slept correctly", r instanceof Get);

    p.save();

    Get g = (Get) r;

    g.visit(new ScriptusFacade(datastore, c, m), p);

    p = datastore.getProcess(p.getPid());

    assertTrue("got content", p.getState() instanceof String);

    r = p.call();

    assertTrue("said correctly", r instanceof Say);

    assertTrue("contains fn def", ((Say) r).getMsg().contains("return new Date().clearTime();"));
  }
  /** 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());
  }
  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());
  }