Commit ebb26247 by Ned Batchelder

Make jailpy tests more convenient and informative.

parent 7c498be6
...@@ -8,10 +8,17 @@ from codejail.jailpy import jailpy ...@@ -8,10 +8,17 @@ from codejail.jailpy import jailpy
dedent = textwrap.dedent dedent = textwrap.dedent
class TestFeatures(unittest.TestCase): class JailPyHelpers(object):
"""Assert helpers for jailpy tests."""
def assertResultOk(self, res):
self.assertEqual(res.stderr, "")
self.assertEqual(res.status, 0)
class TestFeatures(JailPyHelpers, unittest.TestCase):
def test_hello_world(self): def test_hello_world(self):
res = jailpy("print 'Hello, world!'") res = jailpy("print 'Hello, world!'")
self.assertEqual(res.status, 0) self.assertResultOk(res)
self.assertEqual(res.stdout, 'Hello, world!\n') self.assertEqual(res.stdout, 'Hello, world!\n')
def test_argv(self): def test_argv(self):
...@@ -19,7 +26,7 @@ class TestFeatures(unittest.TestCase): ...@@ -19,7 +26,7 @@ class TestFeatures(unittest.TestCase):
"import sys; print ':'.join(sys.argv[1:])", "import sys; print ':'.join(sys.argv[1:])",
argv=["Hello", "world", "-x"] argv=["Hello", "world", "-x"]
) )
self.assertEqual(res.status, 0) self.assertResultOk(res)
self.assertEqual(res.stdout, "Hello:world:-x\n") self.assertEqual(res.stdout, "Hello:world:-x\n")
def test_ends_with_exception(self): def test_ends_with_exception(self):
...@@ -38,10 +45,11 @@ class TestFeatures(unittest.TestCase): ...@@ -38,10 +45,11 @@ class TestFeatures(unittest.TestCase):
"import json,sys; print sum(json.load(sys.stdin))", "import json,sys; print sum(json.load(sys.stdin))",
stdin="[1, 2.5, 33]" stdin="[1, 2.5, 33]"
) )
self.assertResultOk(res)
self.assertEqual(res.stdout.strip(), "36.5") self.assertEqual(res.stdout.strip(), "36.5")
class TestLimits(unittest.TestCase): class TestLimits(JailPyHelpers, unittest.TestCase):
def test_cant_use_too_much_memory(self): def test_cant_use_too_much_memory(self):
res = jailpy("print sum(range(100000000))") res = jailpy("print sum(range(100000000))")
self.assertNotEqual(res.status, 0) self.assertNotEqual(res.status, 0)
...@@ -78,7 +86,7 @@ class TestLimits(unittest.TestCase): ...@@ -78,7 +86,7 @@ class TestLimits(unittest.TestCase):
# TODO: read network # TODO: read network
# TODO: fork # TODO: fork
class TestMalware(unittest.TestCase): class TestMalware(JailPyHelpers, unittest.TestCase):
def test_crash_cpython(self): def test_crash_cpython(self):
# http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html # http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
res = jailpy(dedent("""\ res = jailpy(dedent("""\
...@@ -119,5 +127,5 @@ class TestMalware(unittest.TestCase): ...@@ -119,5 +127,5 @@ class TestMalware(unittest.TestCase):
print "Files in %r: %r" % (place, files) print "Files in %r: %r" % (place, files)
print "Done." print "Done."
""")) """))
self.assertEqual(res.status, 0) self.assertResultOk(res)
self.assertEqual(res.stdout, "Done.\n") self.assertEqual(res.stdout, "Done.\n")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment