diff options
author | 2023-05-09 22:08:53 +0100 | |
---|---|---|
committer | 2023-05-09 22:08:53 +0100 | |
commit | 9804a10a598b678225d299178113210f74a25392 (patch) | |
tree | 9192cd23a3ee78e96ac2083327324fa3d3f28223 /tests | |
parent | Update Sentry SDK to support Falcon 3 (diff) | |
parent | Merge branch 'main' into file-scan-recursion-fix (diff) |
Merge pull request #173 from python-discord/file-scan-recursion-fix
Fix recursion error during file attachment parsing of deep nested paths
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_nsjail.py | 23 | ||||
-rw-r--r-- | tests/test_timed.py | 30 |
2 files changed, 53 insertions, 0 deletions
diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 456046b..c701d3a 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -227,6 +227,29 @@ class NsJailTests(unittest.TestCase): ) self.assertEqual(result.stderr, None) + def test_file_parsing_depth_limit(self): + code = dedent( + """ + import os + + x = "" + for _ in range(1000): + x += "a/" + os.mkdir(x) + + open(f"{x}test.txt", "w").write("test") + """ + ).strip() + + nsjail = NsJail(memfs_instance_size=32 * Size.MiB, files_timeout=5) + result = nsjail.python3(["-c", code]) + self.assertEqual(result.returncode, None) + self.assertEqual( + result.stdout, + "FileParsingError: Exceeded directory depth limit while parsing attachments", + ) + self.assertEqual(result.stderr, None) + def test_file_write_error(self): """Test errors during file write.""" result = self.nsjail.python3( diff --git a/tests/test_timed.py b/tests/test_timed.py new file mode 100644 index 0000000..e46bd37 --- /dev/null +++ b/tests/test_timed.py @@ -0,0 +1,30 @@ +import math +import time +from unittest import TestCase + +from snekbox.utils.timed import time_limit + + +class TimedTests(TestCase): + def test_sleep(self): + """Test that a sleep can be interrupted.""" + _finished = False + start = time.perf_counter() + with self.assertRaises(TimeoutError): + with time_limit(1): + time.sleep(2) + _finished = True + end = time.perf_counter() + self.assertLess(end - start, 2) + self.assertFalse(_finished) + + def test_iter(self): + """Test that a long-running built-in function can be interrupted.""" + _result = 0 + start = time.perf_counter() + with self.assertRaises(TimeoutError): + with time_limit(1): + _result = math.factorial(2**30) + end = time.perf_counter() + self.assertEqual(_result, 0) + self.assertLess(end - start, 2) |