<commit_before>#!/usr/bin/env python
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of StraitJacket.
#
# StraitJacket is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# StraitJacket is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

import server, unittest, urllib, json, re

__author__ = "<NAME>"
__copyright__ = "Copyright 2011 Instructure, Inc."
__license__ = "AGPLv3"
__email__ = "<EMAIL>"

wrapper = server.straitjacket.StraitJacket(server.DEFAULT_CONFIG_DIR)
webapp = server.webapp(wrapper)

class TestCase(unittest.TestCase): pass

class WebServerTests(TestCase):

  def execute(self, data, expected_stdout, expected_stderr,
      expected_exitstatus, expected_error):
    response = webapp.request("/execute", "POST",
        urllib.urlencode(data))
    self.assertEquals(response.status, "200 OK")
    r = json.loads(response.data)
    try:
      if type(expected_stdout) in (str, unicode):
        if r["stdout"] != expected_stdout: raise Exception
      else:
        if not expected_stdout.search(r["stdout"]): raise Exception
      if type(expected_stderr) in (str, unicode):
        if r["stderr"] != expected_stderr: raise Exception
      else:
        if not expected_stderr.search(r["stderr"]): raise Exception
      if r["exitstatus"] != expected_exitstatus: raise Exception
    except:
      raise Exception, "unexpected output: %s" % r

  def testBadLanguage(self):
    self.assertEquals(webapp.request("/execute", "POST",
        urllib.urlencode({
      "language": "non-existent-language",
      "source": "",
      "stdin": ""})).status, "400 Bad Request")

  def testTooLongExecution(self):
    self.execute({
        "language": "ruby1.8",
        "source": "sleep 20\n",
        "stdin": ""},
      "",
      "",
      -9,
      "runtime_timelimit")

  def testOkayExecution(self):
    self.execute({
        "language": "ruby1.8",
        "source": "sleep 10\n",
        "stdin": ""},
      "",
      "",
      0,
      "")

  def testLimitedExecution(self):
    self.execute({
        "language": "ruby1.8",
        "source": "sleep 10\n",
        "stdin": "",
        "timelimit": "1.5"},
      "",
      "",
      -9,
      "runtime_timelimit")

  def testOkayLimitedExecution(self):
    self.execute({
        "language": "ruby1.8",
        "source": "sleep 1\n",
        "stdin": "",
        "timelimit": "2.5"},
      "",
      "",
      0,
      "")


class StraitJacketTests(TestCase):

  def sj_run(self, lang, source):
    stdout, stderr, exitstatus, runtime, error = wrapper.run(lang, source, "",
        custom_timelimit=1.0)
    return (exitstatus, error)

  def testCompilerProfileErrors(self):
    self.assertEquals(wrapper.enabled_languages["c"][
        "execution_profile"].__class__.__name__, "CompilerProfile")
    self.assertEquals(self.sj_run("c",
        "int main(int argc, char** argv) { return 0; }"), (0, ""))
    self.assertEquals(self.sj_run("c",
        "(int argc, char** argv) { }"), (1, "compilation_error"))
    self.assertEquals(self.sj_run("c",
        "int main(int argc, char** argv) { return 1; }"), (1, "runtime_error"))
    self.assertEquals(self.sj_run("c",
        "#include <time.h>\nint main(int argc, char** argv) { "
        "sleep(20); return 0; }"), (-9, "runtime_timelimit"))

  def testInterpreterProfileErrors(self):
    self.assertEquals(wrapper.enabled_languages["python"][
        "execution_profile"].__class__.__name__, "InterpreterProfile")
    self.assertEquals(self.sj_run("python",
        "print 'hi'\n"), (0, ""))
    self.assertEquals(self.sj_run("python",
        "raise 'error'\n"), (1, "runtime_error"))
    self.assertEquals(self.sj_run("python",
        "import time\ntime.sleep(20)"), (-9, "runtime_timelimit"))

  def testVMProfileErrors(self):
    self.assertEquals(wrapper.enabled_languages["scala"][
        "execution_profile"].__class__.__name__, "VMProfile")
    self.assertEquals(self.sj_run("scala",
        "object Main extends Application { 0 }"), (0, ""))
    self.assertEquals(self.sj_run("scala",
        "object extends { 0 }"), (1, "compilation_error"))
    self.assertEquals(self.sj_run("scala",
        "object Main extends Application { throw new Exception(\"whoops\")}"),
        (1, "runtime_error"))
    self.assertEquals(self.sj_run("scala",
        "object Main extends Application { Thread.sleep(20000) }"),
        (-9, "runtime_timelimit"))<commit_msg>scala return values started being weird, so switching to java for vm profile tests<commit_after>#!/usr/bin/env python
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of StraitJacket.
#
# StraitJacket is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# StraitJacket is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

import server, unittest, urllib, json, re

__author__ = "<NAME>"
__copyright__ = "Copyright 2011 Instructure, Inc."
__license__ = "AGPLv3"
__email__ = "<EMAIL>"

wrapper = server.straitjacket.StraitJacket(server.DEFAULT_CONFIG_DIR)
webapp = server.webapp(wrapper)

class TestCase(unittest.TestCase): pass

class WebServerTests(TestCase):

  def execute(self, data, expected_stdout, expected_stderr,
      expected_exitstatus, expected_error):
    response = webapp.request("/execute", "POST",
        urllib.urlencode(data))
    self.assertEquals(response.status, "200 OK")
    r = json.loads(response.data)
    try:
      if type(expected_stdout) in (str, unicode):
        if r["stdout"] != expected_stdout: raise Exception
      else:
        if not expected_stdout.search(r["stdout"]): raise Exception
      if type(expected_stderr) in (str, unicode):
        if r["stderr"] != expected_stderr: raise Exception
      else:
        if not expected_stderr.search(r["stderr"]): raise Exception
      if r["exitstatus"] != expected_exitstatus: raise Exception
    except:
      raise Exception, "unexpected output: %s" % r

  def testBadLanguage(self):
    self.assertEquals(webapp.request("/execute", "POST",
        urllib.urlencode({
      "language": "non-existent-language",
      "source": "",
      "stdin": ""})).status, "400 Bad Request")

  def testTooLongExecution(self):
    self.execute({
        "language": "ruby1.8",
        "source": "sleep 20\n",
        "stdin": ""},
      "",
      "",
      -9,
      "runtime_timelimit")

  def testOkayExecution(self):
    self.execute({
        "language": "ruby1.8",
        "source": "sleep 10\n",
        "stdin": ""},
      "",
      "",
      0,
      "")

  def testLimitedExecution(self):
    self.execute({
        "language": "ruby1.8",
        "source": "sleep 10\n",
        "stdin": "",
        "timelimit": "1.5"},
      "",
      "",
      -9,
      "runtime_timelimit")

  def testOkayLimitedExecution(self):
    self.execute({
        "language": "ruby1.8",
        "source": "sleep 1\n",
        "stdin": "",
        "timelimit": "2.5"},
      "",
      "",
      0,
      "")


class StraitJacketTests(TestCase):

  def sj_run(self, lang, source):
    stdout, stderr, exitstatus, runtime, error = wrapper.run(lang, source, "",
        custom_timelimit=1.0)
    return (exitstatus, error)

  def testCompilerProfileErrors(self):
    self.assertEquals(wrapper.enabled_languages["c"][
        "execution_profile"].__class__.__name__, "CompilerProfile")
    self.assertEquals(self.sj_run("c",
        "int main(int argc, char** argv) { return 0; }"), (0, ""))
    self.assertEquals(self.sj_run("c",
        "(int argc, char** argv) { }"), (1, "compilation_error"))
    self.assertEquals(self.sj_run("c",
        "int main(int argc, char** argv) { return 1; }"), (1, "runtime_error"))
    self.assertEquals(self.sj_run("c",
        "#include <time.h>\nint main(int argc, char** argv) { "
        "sleep(20); return 0; }"), (-9, "runtime_timelimit"))

  def testInterpreterProfileErrors(self):
    self.assertEquals(wrapper.enabled_languages["python"][
        "execution_profile"].__class__.__name__, "InterpreterProfile")
    self.assertEquals(self.sj_run("python",
        "print 'hi'\n"), (0, ""))
    self.assertEquals(self.sj_run("python",
        "raise 'error'\n"), (1, "runtime_error"))
    self.assertEquals(self.sj_run("python",
        "import time\ntime.sleep(20)"), (-9, "runtime_timelimit"))

  def testVMProfileErrors(self):
    self.assertEquals(wrapper.enabled_languages["java"][
        "execution_profile"].__class__.__name__, "VMProfile")
    self.assertEquals(self.sj_run("java",
        "class Main { public static void main(String[] args) { } }"), (0, ""))
    self.assertEquals(self.sj_run("java",
        "object extends { 0 }"), (1, "compilation_error"))
    self.assertEquals(self.sj_run("java",
        "class Main { public static void main(String[] args) throws Exception "
        "{ throw new Exception(); } }"), (1, "runtime_error"))
    self.assertEquals(self.sj_run("java",
        "class Main { public static void main(String[] args) throws Exception "
        "{ Thread.sleep(20000); } }"), (-9, "runtime_timelimit"))
{'fraction_non_alphanumeric': 0.1503584772291225, 'fraction_numerical': 0.013228314652125619, 'mean_word_length': 3.3042155584528468, 'pattern_counts': {'":': 34, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5167852', 'n_tokens_mistral': 3137, 'n_tokens_neox': 3053, 'n_words': 756}