<commit_before>import textwrap
import unittest
from nose.plugins.skip import SkipTest

from codejail.jailpy import jailpy

dedent = textwrap.dedent

class TestFeatures(unittest.TestCase):
    def test_hello_world(self):
        res = jailpy("print 'Hello, world!'")
        self.assertEqual(res.status, 0)
        self.assertEqual(res.stdout, 'Hello, world!\n')

    def test_argv(self):
        res = jailpy(
                "import sys; print ':'.join(sys.argv[1:])",
                argv=["Hello", "world", "-x"]
                )
        self.assertEqual(res.status, 0)
        self.assertEqual(res.stdout, "Hello:world:-x\n")

    def test_ends_with_exception(self):
        res = jailpy("""raise Exception('FAIL')""")
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "")
        self.assertEqual(res.stderr, dedent("""\
            Traceback (most recent call last):
              File "jailed_code.py", line 1, in <module>
                raise Exception('FAIL')
            Exception: FAIL
            """))


class TestLimits(unittest.TestCase):
    def test_cant_use_too_much_memory(self):
        res = jailpy("print sum(range(100000000))")
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "")

    def test_cant_use_too_much_cpu(self):
        res = jailpy("print sum(xrange(100000000))")
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "")

    def test_cant_use_too_much_time(self):
        raise SkipTest  # TODO: test this once we can kill sleeping processes.
        res = jailpy(dedent("""\
                import time
                time.sleep(5)
                print 'Done!'
                """))
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "")

    # TODO: write files
    # TODO: read network
    # TODO: fork

class TestMalware(unittest.TestCase):
    def test_crash_cpython(self):<commit_msg>Test that we can't write files<commit_after>"""Test jailpy.py"""

import textwrap
import unittest
from nose.plugins.skip import SkipTest

from codejail.jailpy import jailpy

dedent = textwrap.dedent

class TestFeatures(unittest.TestCase):
    def test_hello_world(self):
        res = jailpy("print 'Hello, world!'")
        self.assertEqual(res.status, 0)
        self.assertEqual(res.stdout, 'Hello, world!\n')

    def test_argv(self):
        res = jailpy(
                "import sys; print ':'.join(sys.argv[1:])",
                argv=["Hello", "world", "-x"]
                )
        self.assertEqual(res.status, 0)
        self.assertEqual(res.stdout, "Hello:world:-x\n")

    def test_ends_with_exception(self):
        res = jailpy("""raise Exception('FAIL')""")
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "")
        self.assertEqual(res.stderr, dedent("""\
            Traceback (most recent call last):
              File "jailed_code.py", line 1, in <module>
                raise Exception('FAIL')
            Exception: FAIL
            """))


class TestLimits(unittest.TestCase):
    def test_cant_use_too_much_memory(self):
        res = jailpy("print sum(range(100000000))")
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "")

    def test_cant_use_too_much_cpu(self):
        res = jailpy("print sum(xrange(100000000))")
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "")

    def test_cant_use_too_much_time(self):
        raise SkipTest  # TODO: test this once we can kill sleeping processes.
        res = jailpy(dedent("""\
                import time
                time.sleep(5)
                print 'Done!'
                """))
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "")

    def test_cant_write_files(self):
        res = jailpy(dedent("""\
                print "Trying"
                with open("mydata.txt", "w") as f:
                    f.write("hello")
                with open("mydata.txt") as f2:
                    print "Got this:", f2.read()
                """))
        self.assertNotEqual(res.status, 0)
        self.assertEqual(res.stdout, "Trying\n")
        self.assertIn("ermission denied", res.stderr)

    # TODO: write files
    # TODO: read network
    # TODO: fork

class TestMalware(unittest.TestCase):
    def test_crash_cpython(self):
{'fraction_non_alphanumeric': 0.12804739120528594, 'fraction_numerical': 0.012987012987012988, 'mean_word_length': 2.290854572713643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '6117428', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1303, 'n_words': 273}