diff options
| author | 2022-11-16 19:18:02 -0500 | |
|---|---|---|
| committer | 2022-11-16 19:18:02 -0500 | |
| commit | 8dddd105182a42ee8e76b02f7abed1e19a299b2d (patch) | |
| tree | 506199998c03c2436f0a2d2bf4ca34c1a53172bf | |
| parent | Support `-m` calls to default to no file (diff) | |
Fixed unit tests
| -rw-r--r-- | tests/api/__init__.py | 4 | ||||
| -rw-r--r-- | tests/test_nsjail.py | 29 | 
2 files changed, 25 insertions, 8 deletions
| diff --git a/tests/api/__init__.py b/tests/api/__init__.py index 0e6e422..5f20faf 100644 --- a/tests/api/__init__.py +++ b/tests/api/__init__.py @@ -1,10 +1,10 @@  import logging -from subprocess import CompletedProcess  from unittest import mock  from falcon import testing  from snekbox.api import SnekAPI +from snekbox.process import EvalResult  class SnekAPITestCase(testing.TestCase): @@ -13,7 +13,7 @@ class SnekAPITestCase(testing.TestCase):          self.patcher = mock.patch("snekbox.api.snekapi.NsJail", autospec=True)          self.mock_nsjail = self.patcher.start() -        self.mock_nsjail.return_value.python3.return_value = CompletedProcess( +        self.mock_nsjail.return_value.python3.return_value = EvalResult(              args=[], returncode=0, stdout="output", stderr="error"          )          self.addCleanup(self.patcher.stop) diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 6f6e2a7..684e69a 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -41,9 +41,9 @@ class NsJailTests(unittest.TestCase):          # Add a kilobyte just to be safe.          code = f"x = ' ' * {self.nsjail.config.cgroup_mem_max + 1000}" -        result = self.nsjail.python3(code) -        self.assertEqual(result.returncode, 137) +        result = self.nsjail.python3(code, py_args=("-c",))          self.assertEqual(result.stdout, "") +        self.assertEqual(result.returncode, 137)          self.assertEqual(result.stderr, None)      def test_subprocess_resource_unavailable(self): @@ -99,7 +99,7 @@ class NsJailTests(unittest.TestCase):          self.assertEqual(result.stderr, None)      def test_read_only_file_system(self): -        for path in ("/", "/etc", "/lib", "/lib64", "/snekbox", "/usr"): +        for path in ("/etc", "/lib", "/lib64", "/snekbox", "/usr"):              with self.subTest(path=path):                  code = dedent(                      f""" @@ -113,6 +113,21 @@ class NsJailTests(unittest.TestCase):                  self.assertIn("Read-only file system", result.stdout)                  self.assertEqual(result.stderr, None) +    def test_write(self): +        code = dedent( +            """ +            from pathlib import Path +            with open('test.txt', 'w') as f: +                f.write('hello') +            print(Path('test.txt').read_text()) +            """ +        ).strip() + +        result = self.nsjail.python3(code) +        self.assertEqual(result.returncode, 0) +        self.assertEqual(result.stdout, "hello\n") +        self.assertEqual(result.stderr, None) +      def test_forkbomb_resource_unavailable(self):          code = dedent(              """ @@ -141,7 +156,9 @@ class NsJailTests(unittest.TestCase):          self.assertEqual(result.stderr, None)      def test_null_byte_value_error(self): -        result = self.nsjail.python3("\0") +        # This error does not occur without -c, where it +        # would be a normal SyntaxError. +        result = self.nsjail.python3("\0", py_args=("-c",))          self.assertEqual(result.returncode, None)          self.assertEqual(result.stdout, "ValueError: embedded null byte")          self.assertEqual(result.stderr, None) @@ -272,14 +289,14 @@ class NsJailTests(unittest.TestCase):          self.assertEqual(output, chunk * expected_chunks)      def test_nsjail_args(self): -        args = ("foo", "bar") +        args = ["foo", "bar"]          result = self.nsjail.python3("", nsjail_args=args)          end = result.args.index("--")          self.assertEqual(result.args[end - len(args) : end], args)      def test_py_args(self): -        args = ("-m", "timeit") +        args = ["-m", "timeit"]          result = self.nsjail.python3("", py_args=args)          self.assertEqual(result.returncode, 0) | 
