From e093f20bad446bb6023ef243fe867d5f2f7b4334 Mon Sep 17 00:00:00 2001 From: Mark <1515135+MarkKoz@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:26:21 -0700 Subject: Add config path & output size args to NsJail class --- tests/test_nsjail.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/test_nsjail.py') diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index a3632e6..dc30dfa 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -5,7 +5,7 @@ import unittest import unittest.mock from textwrap import dedent -from snekbox.nsjail import OUTPUT_MAX, READ_CHUNK_SIZE, NsJail +from snekbox.nsjail import NsJail class NsJailTests(unittest.TestCase): @@ -255,8 +255,8 @@ class NsJailTests(unittest.TestCase): self.assertEqual(result.returncode, 143) def test_large_output_is_truncated(self): - chunk = "a" * READ_CHUNK_SIZE - expected_chunks = OUTPUT_MAX // sys.getsizeof(chunk) + 1 + chunk = "a" * self.nsjail.read_chunk_size + expected_chunks = self.nsjail.max_output_size // sys.getsizeof(chunk) + 1 nsjail_subprocess = unittest.mock.MagicMock() -- cgit v1.2.3 From c7a2a24e4740bb248bd73f03d8d11067c48cde9e Mon Sep 17 00:00:00 2001 From: Mark <1515135+MarkKoz@users.noreply.github.com> Date: Mon, 6 Jun 2022 18:53:52 -0700 Subject: Test NsJail args are set and used when passed to __init__ --- tests/test_nsjail.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'tests/test_nsjail.py') diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index dc30dfa..745c44f 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -1,6 +1,8 @@ import io import logging +import shutil import sys +import tempfile import unittest import unittest.mock from textwrap import dedent @@ -280,3 +282,38 @@ class NsJailTests(unittest.TestCase): self.assertEqual(result.returncode, 0) self.assertEqual(result.args[-3:-1], args) + + +class NsJailArgsTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls._temp_dir = tempfile.TemporaryDirectory() + cls.addClassCleanup(cls._temp_dir.cleanup) + + cls.nsjail_path = shutil.copy2("/usr/sbin/nsjail", cls._temp_dir.name) + cls.config_path = shutil.copy2("./config/snekbox.cfg", cls._temp_dir.name) + cls.max_output_size = 1_234_567 + cls.read_chunk_size = 12_345 + + cls.nsjail = NsJail( + cls.nsjail_path, cls.config_path, cls.max_output_size, cls.read_chunk_size + ) + + logging.getLogger("snekbox.nsjail").setLevel(logging.WARNING) + + def test_nsjail_path(self): + result = self.nsjail.python3("") + + self.assertEqual(result.args[0], self.nsjail_path) + + def test_config_path(self): + result = self.nsjail.python3("") + + i = result.args.index("--config") + 1 + self.assertEqual(result.args[i], self.config_path) + + def test_init_args(self): + self.assertEqual(self.nsjail.nsjail_path, self.nsjail_path) + self.assertEqual(self.nsjail.config_path, self.config_path) + self.assertEqual(self.nsjail.max_output_size, self.max_output_size) + self.assertEqual(self.nsjail.read_chunk_size, self.read_chunk_size) -- cgit v1.2.3 From 308385c2b1348f7aaa271ef1785a9d8a3fb03715 Mon Sep 17 00:00:00 2001 From: Mark <1515135+MarkKoz@users.noreply.github.com> Date: Tue, 7 Jun 2022 10:20:31 -0700 Subject: Use new NsJail instance for each test in NsJailArgsTests --- tests/test_nsjail.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'tests/test_nsjail.py') diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 745c44f..492c2f9 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -285,18 +285,17 @@ class NsJailTests(unittest.TestCase): class NsJailArgsTests(unittest.TestCase): - @classmethod - def setUpClass(cls): - cls._temp_dir = tempfile.TemporaryDirectory() - cls.addClassCleanup(cls._temp_dir.cleanup) - - cls.nsjail_path = shutil.copy2("/usr/sbin/nsjail", cls._temp_dir.name) - cls.config_path = shutil.copy2("./config/snekbox.cfg", cls._temp_dir.name) - cls.max_output_size = 1_234_567 - cls.read_chunk_size = 12_345 - - cls.nsjail = NsJail( - cls.nsjail_path, cls.config_path, cls.max_output_size, cls.read_chunk_size + def setUp(self): + self._temp_dir = tempfile.TemporaryDirectory() + self.addClassCleanup(self._temp_dir.cleanup) + + self.nsjail_path = shutil.copy2("/usr/sbin/nsjail", self._temp_dir.name) + self.config_path = shutil.copy2("./config/snekbox.cfg", self._temp_dir.name) + self.max_output_size = 1_234_567 + self.read_chunk_size = 12_345 + + self.nsjail = NsJail( + self.nsjail_path, self.config_path, self.max_output_size, self.read_chunk_size ) logging.getLogger("snekbox.nsjail").setLevel(logging.WARNING) -- cgit v1.2.3