From 9eaf64ffe201e4efac29364143578fe702a32ec0 Mon Sep 17 00:00:00 2001 From: LiYang Date: Tue, 23 Aug 2016 14:45:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=9B=86=E6=88=90=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=92=8C=E9=83=A8=E5=88=86=E8=B6=85=E6=97=B6=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bindings/Python/_judger.c | 7 +- tests/integration/__init__.py | 0 tests/integration/test.py | 100 ++++++++++++++++++ .../c_cpp/child_proc_cpu_time_limit.c | 29 +++++ .../c_cpp/child_proc_real_time_limit.c | 29 +++++ tests/languages/c_cpp/re2.c | 9 +- tests/languages/c_cpp/test.py | 24 +++++ tests/test.py | 2 + 8 files changed, 196 insertions(+), 4 deletions(-) create mode 100644 tests/integration/__init__.py create mode 100644 tests/integration/test.py create mode 100644 tests/languages/c_cpp/child_proc_cpu_time_limit.c create mode 100644 tests/languages/c_cpp/child_proc_real_time_limit.c diff --git a/bindings/Python/_judger.c b/bindings/Python/_judger.c index 5d93a09..64cfed1 100644 --- a/bindings/Python/_judger.c +++ b/bindings/Python/_judger.c @@ -34,7 +34,6 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) { if (!PyList_Check(args_list)) { RaiseValueError("args must be a list"); } - _config.args[count++] = _config.exe_path; args_iter = PyObject_GetIter(args_list); while (1) { @@ -43,7 +42,7 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) { break; } if (!PyString_Check(next)) { - RaiseValueError("arg in args must be a string"); + RaiseValueError("arg item must be a string"); } _config.args[count] = PyString_AsString(next); count++; @@ -52,6 +51,7 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) { _config.args[count] = NULL; count = 0; + if (!PyList_Check(env_list)) { RaiseValueError("env must be a list"); } @@ -61,13 +61,14 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) { if (!next) { break; } - if (!PyString_Check(next) && !PyUnicode_Check(next)) { + if (!PyString_Check(next)) { RaiseValueError("env item must be a string"); } _config.env[count] = PyString_AsString(next); count++; } _config.env[count] = NULL; + if (PyString_Check(rule_path)) { _config.seccomp_rule_so_path = PyString_AsString(rule_path); } diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/test.py b/tests/integration/test.py new file mode 100644 index 0000000..1031c4f --- /dev/null +++ b/tests/integration/test.py @@ -0,0 +1,100 @@ +# coding=utf-8 +import _judger +from unittest import TestCase + + +class IntegrationTest(TestCase): + def setUp(self): + print "Running", self._testMethodName + + def test_args_validation(self): + with self.assertRaisesRegexp(ValueError, "Invalid args and kwargs"): + _judger.run() + _judger.run(a=1, c=3) + + def test_args_must_be_list(self): + with self.assertRaisesRegexp(ValueError, "args must be a list"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args="12344", env=["a=b"], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + with self.assertRaisesRegexp(ValueError, "args must be a list"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args={"k": "v"}, env=["a=b"], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + def test_args_item_must_be_string(self): + with self.assertRaisesRegexp(ValueError, "arg item must be a string"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["1234", 1234], env=["a=b"], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + with self.assertRaisesRegexp(ValueError, "arg item must be a string"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["1234", None], env=["a=b"], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + with self.assertRaisesRegexp(ValueError, "arg item must be a string"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=[u"哈哈哈"], env=["a=b"], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + def test_env_must_be_list(self): + with self.assertRaisesRegexp(ValueError, "env must be a list"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["1234"], env="1234", log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + with self.assertRaisesRegexp(ValueError, "env must be a list"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["1234"], env={"k": "v"}, log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + def test_env_item_must_be_string(self): + with self.assertRaisesRegexp(ValueError, "env item must be a string"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["1234"], env=["1234", 1234], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + with self.assertRaisesRegexp(ValueError, "env item must be a string"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["1234"], env=["a=b", None], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + with self.assertRaisesRegexp(ValueError, "env item must be a string"): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["1234"], env=[u"哈哈哈"], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + def test_seccomp_rule_can_be_none(self): + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["12344"], env=["a=b"], log_path="1.log", + seccomp_rule_so_path="1.so", uid=0, gid=0) + + _judger.run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000, + max_process_number=200, max_output_size=10000, exe_path="1.out", + input_path="1.in", output_path="1.out", error_path="1.out", + args=["12344"], env=["a=b"], log_path="1.log", + seccomp_rule_so_path=None, uid=0, gid=0) diff --git a/tests/languages/c_cpp/child_proc_cpu_time_limit.c b/tests/languages/c_cpp/child_proc_cpu_time_limit.c new file mode 100644 index 0000000..b1e6dbc --- /dev/null +++ b/tests/languages/c_cpp/child_proc_cpu_time_limit.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +int main() +{ + int status; + int pid; + + if ((pid = fork()) < 0) { + perror("fork error"); + return 0; + } + + if (pid == 0) { + while (1) {}; + } + else { + struct rusage resource_usage; + if (wait4(pid, &status, 0, &resource_usage) == -1) { + perror("wait4 error!"); + } + } + + return 0; +} \ No newline at end of file diff --git a/tests/languages/c_cpp/child_proc_real_time_limit.c b/tests/languages/c_cpp/child_proc_real_time_limit.c new file mode 100644 index 0000000..c31b22f --- /dev/null +++ b/tests/languages/c_cpp/child_proc_real_time_limit.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +int main() +{ + int status; + int pid; + + if ((pid = fork()) < 0) { + perror("fork error"); + return 0; + } + + if (pid == 0) { + sleep(10000); + } + else { + struct rusage resource_usage; + if (wait4(pid, &status, 0, &resource_usage) == -1) { + perror("wait4 error!"); + } + } + + return 0; +} \ No newline at end of file diff --git a/tests/languages/c_cpp/re2.c b/tests/languages/c_cpp/re2.c index 6724ae3..4f1468d 100644 --- a/tests/languages/c_cpp/re2.c +++ b/tests/languages/c_cpp/re2.c @@ -1 +1,8 @@ -#include \ No newline at end of file +#include +#include + +int main() +{ + raise(SIGSEGV); + return 0; +} \ No newline at end of file diff --git a/tests/languages/c_cpp/test.py b/tests/languages/c_cpp/test.py index 5b15413..338fafc 100644 --- a/tests/languages/c_cpp/test.py +++ b/tests/languages/c_cpp/test.py @@ -128,3 +128,27 @@ judger result = _judger.run(**config) # re1.c return 25 self.assertEqual(result["exit_code"], 25) + + def test_re2(self): + config = self.config + config["exe_path"] = self._compile("re2.c") + config["seccomp_rule_so_path"] = None + result = _judger.run(**config) + self.assertEqual(result["result"], _judger.RUNTIME_ERROR) + self.assertEqual(result["signal"], signal.SIGSEGV) + + def test_child_proc_cpu_time_limit(self): + config = self.config + config["exe_path"] = self._compile("child_proc_cpu_time_limit.c") + config["seccomp_rule_so_path"] = None + result = _judger.run(**config) + self.assertEqual(result["result"], _judger.CPU_TIME_LIMIT_EXCEEDED) + + def test_child_proc_real_time_limit(self): + config = self.config + config["exe_path"] = self._compile("child_proc_real_time_limit.c") + config["seccomp_rule_so_path"] = None + result = _judger.run(**config) + print result + self.assertEqual(result["result"], _judger.REAL_TIME_LIMIT_EXCEEDED) + self.assertEqual(result["signal"], signal.SIGKILL) diff --git a/tests/test.py b/tests/test.py index 1e55a27..6f444af 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,6 +1,8 @@ # coding=utf-8 from unittest import TestCase, main +from integration.test import IntegrationTest + from languages.c_cpp.test import C_CPPJudgeTestCase from languages.Python.test import PythonJudgeTestCase