From efaf2675dfdcc9f50597846c2faa1f894e4511e3 Mon Sep 17 00:00:00 2001 From: LiYang Date: Thu, 25 Aug 2016 09:40:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=B5=8B=E8=AF=95=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/{integration => }/__init__.py | 0 tests/integration/test.py | 100 -------- tests/languages/base.py | 24 -- tests/languages/c_cpp/test.py | 154 ----------- tests/test.py | 6 +- .../Python/__init__.py | 0 tests/{languages => testcase}/Python/test.py | 0 tests/{languages => testcase}/__init__.py | 0 tests/testcase/base.py | 45 ++++ .../{languages => testcase}/c_cpp/__init__.py | 0 tests/testcase/c_cpp/test.py | 28 ++ tests/testcase/integration/__init__.py | 0 tests/testcase/integration/a.out | Bin 0 -> 8667 bytes .../c_cpp => testcase/integration}/args.c | 0 .../integration}/child_proc_cpu_time_limit.c | 0 .../integration}/child_proc_real_time_limit.c | 0 .../c_cpp => testcase/integration}/env.c | 0 .../c_cpp => testcase/integration}/memory1.c | 0 .../c_cpp => testcase/integration}/memory2.c | 0 .../c_cpp => testcase/integration}/normal.c | 0 .../c_cpp => testcase/integration}/re1.c | 0 .../c_cpp => testcase/integration}/re2.c | 0 .../c_cpp => testcase/integration}/sleep.c | 0 tests/testcase/integration/stdout_stderr.c | 11 + tests/testcase/integration/test.py | 242 ++++++++++++++++++ tests/testcase/integration/uid_gid.c | 10 + .../c_cpp => testcase/integration}/while1.c | 0 27 files changed, 338 insertions(+), 282 deletions(-) rename tests/{integration => }/__init__.py (100%) delete mode 100644 tests/integration/test.py delete mode 100644 tests/languages/base.py delete mode 100644 tests/languages/c_cpp/test.py rename tests/{languages => testcase}/Python/__init__.py (100%) rename tests/{languages => testcase}/Python/test.py (100%) rename tests/{languages => testcase}/__init__.py (100%) create mode 100644 tests/testcase/base.py rename tests/{languages => testcase}/c_cpp/__init__.py (100%) create mode 100644 tests/testcase/c_cpp/test.py create mode 100644 tests/testcase/integration/__init__.py create mode 100755 tests/testcase/integration/a.out rename tests/{languages/c_cpp => testcase/integration}/args.c (100%) rename tests/{languages/c_cpp => testcase/integration}/child_proc_cpu_time_limit.c (100%) rename tests/{languages/c_cpp => testcase/integration}/child_proc_real_time_limit.c (100%) rename tests/{languages/c_cpp => testcase/integration}/env.c (100%) rename tests/{languages/c_cpp => testcase/integration}/memory1.c (100%) rename tests/{languages/c_cpp => testcase/integration}/memory2.c (100%) rename tests/{languages/c_cpp => testcase/integration}/normal.c (100%) rename tests/{languages/c_cpp => testcase/integration}/re1.c (100%) rename tests/{languages/c_cpp => testcase/integration}/re2.c (100%) rename tests/{languages/c_cpp => testcase/integration}/sleep.c (100%) create mode 100644 tests/testcase/integration/stdout_stderr.c create mode 100644 tests/testcase/integration/test.py create mode 100644 tests/testcase/integration/uid_gid.c rename tests/{languages/c_cpp => testcase/integration}/while1.c (100%) diff --git a/tests/integration/__init__.py b/tests/__init__.py similarity index 100% rename from tests/integration/__init__.py rename to tests/__init__.py diff --git a/tests/integration/test.py b/tests/integration/test.py deleted file mode 100644 index 1031c4f..0000000 --- a/tests/integration/test.py +++ /dev/null @@ -1,100 +0,0 @@ -# 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/base.py b/tests/languages/base.py deleted file mode 100644 index 6eb5777..0000000 --- a/tests/languages/base.py +++ /dev/null @@ -1,24 +0,0 @@ -# coding=utf-8 -import os -import shutil -from unittest import TestCase - - -class RunResult(object): - cpu_time_limited = 1 - real_time_limit_exceeded = 2 - memory_limit_exceeded = 3 - runtime_error = 4 - system_error = 5 - - -class BaseTestCase(TestCase): - def init_workspace(self, language): - base_workspace = "/tmp" - workspace = os.path.join(base_workspace, language) - shutil.rmtree(workspace, ignore_errors=True) - os.mkdir(workspace) - return workspace - - def rand_str(self): - return ''.join(map(lambda xx:(hex(ord(xx))[2:]), os.urandom(16))) \ No newline at end of file diff --git a/tests/languages/c_cpp/test.py b/tests/languages/c_cpp/test.py deleted file mode 100644 index 338fafc..0000000 --- a/tests/languages/c_cpp/test.py +++ /dev/null @@ -1,154 +0,0 @@ -# coding=utf-8 -import os -import _judger -import signal - -from unittest import TestCase -from .. import base - - -class C_CPPJudgeTestCase(base.BaseTestCase): - def setUp(self): - self.workspace = self.init_workspace("c_cpp") - self.config = {"max_cpu_time": 1000, - "max_real_time": 3000, - "max_memory": 1024 * 1024 * 1024, - "max_process_number": 10, - "max_output_size": 1024 * 1024, - "exe_path": "/bin/ls", - "input_path": "/dev/null", - "output_path": "/dev/null", - "error_path": "/dev/null", - "args": [], - "env": ["env=judger_test", "test=judger"], - "log_path": "judger_test.log", - "seccomp_rule_so_path": "/usr/lib/judger/librule_c_cpp.so", - "uid": 0, - "gid": 0} - print "Running", self._testMethodName - - def _compile(self, src_name): - path = os.path.dirname(os.path.abspath(__file__)) - exe_path = os.path.join(self.workspace, src_name.split(".")[0]) - cmd = "gcc {0} -o {1}".format(os.path.join(path, src_name), exe_path) - if os.system(cmd): - raise AssertionError("compile error, cmd: {0}".format(cmd)) - return exe_path - - def make_input(self, content): - path = os.path.join(self.workspace, self.rand_str()) - with open(path, "w") as f: - f.write(content) - return path - - def output_path(self): - return os.path.join(self.workspace, self.rand_str()) - - def output_content(self, path): - with open(path, "r") as f: - return f.read() - - def test_normal(self): - config = self.config - config["exe_path"] = self._compile("normal.c") - config["input_path"] = self.make_input("judger_test") - config["output_path"] = config["error_path"] = self.output_path() - result = _judger.run(**config) - output = """judger_test -Hello world""" - self.assertEqual(result["result"], _judger.SUCCESS) - self.assertEqual(output, self.output_content(config["output_path"])) - - def test_args(self): - config = self.config - config["exe_path"] = self._compile("args.c") - config["args"] = ["test", "hehe", "000"] - config["output_path"] = config["error_path"] = self.output_path() - result = _judger.run(**config) - output = """argv[0]: /tmp/c_cpp/args -argv[1]: test -argv[2]: hehe -argv[3]: 000 -""" - self.assertEqual(result["result"], _judger.SUCCESS) - self.assertEqual(output, self.output_content(config["output_path"])) - - def test_env(self): - config = self.config - config["exe_path"] = self._compile("env.c") - config["output_path"] = config["error_path"] = self.output_path() - result = _judger.run(**config) - output = """judger_test -judger -""" - self.assertEqual(result["result"], _judger.SUCCESS) - self.assertEqual(output, self.output_content(config["output_path"])) - - def test_real_time(self): - config = self.config - config["exe_path"] = self._compile("sleep.c") - config["seccomp_rule_so_path"] = None - result = _judger.run(**config) - self.assertEqual(result["result"], _judger.REAL_TIME_LIMIT_EXCEEDED) - self.assertEqual(result["signal"], signal.SIGKILL) - self.assertTrue(result["real_time"] >= config["max_real_time"]) - - def test_cpu_time(self): - config = self.config - config["exe_path"] = self._compile("while1.c") - config["seccomp_rule_so_path"] = None - result = _judger.run(**config) - self.assertEqual(result["result"], _judger.CPU_TIME_LIMIT_EXCEEDED) - self.assertEqual(result["signal"], signal.SIGKILL) - self.assertTrue(result["cpu_time"] >= config["max_cpu_time"]) - - def test_memory(self): - config = self.config - config["max_memory"] = 64 * 1024 * 1024 - config["exe_path"] = self._compile("memory1.c") - result = _judger.run(**config) - # malloc succeeded - self.assertTrue(result["memory"] > 80 * 1024 * 1024) - self.assertEqual(result["result"], _judger.MEMORY_LIMIT_EXCEEDED) - - def test_memory2(self): - config = self.config - config["max_memory"] = 64 * 1024 * 1024 - config["exe_path"] = self._compile("memory2.c") - result = _judger.run(**config) - # malloc failed, return 1 - self.assertEqual(result["exit_code"], 1) - # malloc failed, so it should use a little memory - self.assertTrue(result["memory"] < 12 * 1024 * 1024) - self.assertEqual(result["result"], _judger.RUNTIME_ERROR) - - def test_re1(self): - config = self.config - config["exe_path"] = self._compile("re1.c") - 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 6f444af..30bacad 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,9 +1,7 @@ # 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 +from testcase.integration.test import IntegrationTest +from testcase.c_cpp.test import C_CPPJudgeTestCase main() \ No newline at end of file diff --git a/tests/languages/Python/__init__.py b/tests/testcase/Python/__init__.py similarity index 100% rename from tests/languages/Python/__init__.py rename to tests/testcase/Python/__init__.py diff --git a/tests/languages/Python/test.py b/tests/testcase/Python/test.py similarity index 100% rename from tests/languages/Python/test.py rename to tests/testcase/Python/test.py diff --git a/tests/languages/__init__.py b/tests/testcase/__init__.py similarity index 100% rename from tests/languages/__init__.py rename to tests/testcase/__init__.py diff --git a/tests/testcase/base.py b/tests/testcase/base.py new file mode 100644 index 0000000..a337f49 --- /dev/null +++ b/tests/testcase/base.py @@ -0,0 +1,45 @@ +# coding=utf-8 +import os +import shutil +from unittest import TestCase + + +class RunResult(object): + cpu_time_limited = 1 + real_time_limit_exceeded = 2 + memory_limit_exceeded = 3 + runtime_error = 4 + system_error = 5 + + +class BaseTestCase(TestCase): + def init_workspace(self, language): + base_workspace = "/tmp" + workspace = os.path.join(base_workspace, language) + shutil.rmtree(workspace, ignore_errors=True) + os.system("mkdir -p " + workspace) + return workspace + + def rand_str(self): + return ''.join(map(lambda xx:(hex(ord(xx))[2:]), os.urandom(16))) + + def _compile(self, src_name): + path = os.path.dirname(os.path.abspath(__file__)) + exe_path = os.path.join(self.workspace, src_name.split("/")[-1].split(".")[0]) + cmd = "gcc {0} -g -O0 -o {1}".format(os.path.join(path, src_name), exe_path) + if os.system(cmd): + raise AssertionError("compile error, cmd: {0}".format(cmd)) + return exe_path + + def make_input(self, content): + path = os.path.join(self.workspace, self.rand_str()) + with open(path, "w") as f: + f.write(content) + return path + + def output_path(self): + return os.path.join(self.workspace, self.rand_str()) + + def output_content(self, path): + with open(path, "r") as f: + return f.read() \ No newline at end of file diff --git a/tests/languages/c_cpp/__init__.py b/tests/testcase/c_cpp/__init__.py similarity index 100% rename from tests/languages/c_cpp/__init__.py rename to tests/testcase/c_cpp/__init__.py diff --git a/tests/testcase/c_cpp/test.py b/tests/testcase/c_cpp/test.py new file mode 100644 index 0000000..6cdf431 --- /dev/null +++ b/tests/testcase/c_cpp/test.py @@ -0,0 +1,28 @@ +# coding=utf-8 +import os +import _judger +import signal + +from unittest import TestCase +from .. import base + + +class C_CPPJudgeTestCase(base.BaseTestCase): + def setUp(self): + self.workspace = self.init_workspace("c_cpp") + self.config = {"max_cpu_time": 1000, + "max_real_time": 3000, + "max_memory": 1024 * 1024 * 1024, + "max_process_number": 10, + "max_output_size": 1024 * 1024, + "exe_path": "/bin/ls", + "input_path": "/dev/null", + "output_path": "/dev/null", + "error_path": "/dev/null", + "args": [], + "env": ["env=judger_test", "test=judger"], + "log_path": "judger_test.log", + "seccomp_rule_so_path": "/usr/lib/judger/librule_c_cpp.so", + "uid": 0, + "gid": 0} + print "Running", self._testMethodName diff --git a/tests/testcase/integration/__init__.py b/tests/testcase/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/testcase/integration/a.out b/tests/testcase/integration/a.out new file mode 100755 index 0000000000000000000000000000000000000000..8dafaa0b89e70a587675937d1ba564c1325d6261 GIT binary patch literal 8667 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 OQ%tyLI3gmWEdB%6 +#include + +int main() +{ + fprintf(stderr, "stderr\n"); + fprintf(stdout, "--------------\n"); + fprintf(stdout, "stdout\n"); + fprintf(stderr, "+++++++++++++++\n"); + return 0; +} \ No newline at end of file diff --git a/tests/testcase/integration/test.py b/tests/testcase/integration/test.py new file mode 100644 index 0000000..ab8508b --- /dev/null +++ b/tests/testcase/integration/test.py @@ -0,0 +1,242 @@ +# coding=utf-8 +import _judger +import signal +import pwd +import grp + +from .. import base + + +class IntegrationTest(base.BaseTestCase): + def setUp(self): + print "Running", self._testMethodName + self.config = {"max_cpu_time": 1000, + "max_real_time": 3000, + "max_memory": 1024 * 1024 * 1024, + "max_process_number": 10, + "max_output_size": 1024 * 1024, + "exe_path": "/bin/ls", + "input_path": "/dev/null", + "output_path": "/dev/null", + "error_path": "/dev/null", + "args": [], + "env": ["env=judger_test", "test=judger"], + "log_path": "judger_test.log", + "seccomp_rule_so_path": "/usr/lib/judger/librule_c_cpp.so", + "uid": 0, + "gid": 0} + self.workspace = self.init_workspace("integration") + + def _compile(self, src_name): + return super(IntegrationTest, self)._compile("integration/" + src_name) + + 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="/bin/ls", + input_path="/dev/null", output_path="/dev/null", error_path="/dev/null", + args=["12344"], env=["a=b"], log_path="/dev/null", + seccomp_rule_so_path="/usr/lib/judger/librule_c_cpp.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="/bin/ls", + input_path="/dev/null", output_path="/dev/null", error_path="/dev/null", + args=["12344"], env=["a=b"], log_path="/dev/null", + seccomp_rule_so_path=None, uid=0, gid=0) + + def test_normal(self): + config = self.config + config["exe_path"] = self._compile("normal.c") + config["input_path"] = self.make_input("judger_test") + config["output_path"] = config["error_path"] = self.output_path() + result = _judger.run(**config) + output = "judger_test\nHello world" + self.assertEqual(result["result"], _judger.SUCCESS) + self.assertEqual(output, self.output_content(config["output_path"])) + + def test_args(self): + config = self.config + config["exe_path"] = self._compile("args.c") + config["args"] = ["test", "hehe", "000"] + config["output_path"] = config["error_path"] = self.output_path() + result = _judger.run(**config) + output = "argv[0]: /tmp/integration/args\nargv[1]: test\nargv[2]: hehe\nargv[3]: 000\n" + self.assertEqual(result["result"], _judger.SUCCESS) + self.assertEqual(output, self.output_content(config["output_path"])) + + def test_env(self): + config = self.config + config["exe_path"] = self._compile("env.c") + config["output_path"] = config["error_path"] = self.output_path() + result = _judger.run(**config) + output = "judger_test\njudger\n" + self.assertEqual(result["result"], _judger.SUCCESS) + self.assertEqual(output, self.output_content(config["output_path"])) + + def test_real_time(self): + config = self.config + config["exe_path"] = self._compile("sleep.c") + config["seccomp_rule_so_path"] = None + result = _judger.run(**config) + self.assertEqual(result["result"], _judger.REAL_TIME_LIMIT_EXCEEDED) + self.assertEqual(result["signal"], signal.SIGKILL) + self.assertTrue(result["real_time"] >= config["max_real_time"]) + + def test_cpu_time(self): + config = self.config + config["exe_path"] = self._compile("while1.c") + config["seccomp_rule_so_path"] = None + result = _judger.run(**config) + self.assertEqual(result["result"], _judger.CPU_TIME_LIMIT_EXCEEDED) + self.assertEqual(result["signal"], signal.SIGKILL) + self.assertTrue(result["cpu_time"] >= config["max_cpu_time"]) + + def test_memory(self): + config = self.config + config["max_memory"] = 64 * 1024 * 1024 + config["exe_path"] = self._compile("memory1.c") + result = _judger.run(**config) + # malloc succeeded + self.assertTrue(result["memory"] > 80 * 1024 * 1024) + self.assertEqual(result["result"], _judger.MEMORY_LIMIT_EXCEEDED) + + def test_memory2(self): + config = self.config + config["max_memory"] = 64 * 1024 * 1024 + config["exe_path"] = self._compile("memory2.c") + result = _judger.run(**config) + # malloc failed, return 1 + self.assertEqual(result["exit_code"], 1) + # malloc failed, so it should use a little memory + self.assertTrue(result["memory"] < 12 * 1024 * 1024) + self.assertEqual(result["result"], _judger.RUNTIME_ERROR) + + def test_re1(self): + config = self.config + config["exe_path"] = self._compile("re1.c") + 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) + self.assertEqual(result["result"], _judger.REAL_TIME_LIMIT_EXCEEDED) + self.assertEqual(result["signal"], signal.SIGKILL) + + def test_stdout_and_stderr(self): + config = self.config + config["exe_path"] = self._compile("stdout_stderr.c") + config["output_path"] = config["error_path"] = self.output_path() + result = _judger.run(**config) + self.assertEqual(result["result"], _judger.SUCCESS) + output = "stderr\n+++++++++++++++\n--------------\nstdout\n" + self.assertEqual(output, self.output_content(config["output_path"])) + + def test_uid_and_gid(self): + config = self.config + config["exe_path"] = self._compile("uid_gid.c") + config["output_path"] = config["error_path"] = self.output_path() + config["seccomp_rule_so_path"] = None + config["uid"] = 65534 + config["gid"] = 65534 + result = _judger.run(**config) + print result + self.assertEqual(result["result"], _judger.SUCCESS) + output = "uid=65534(nobody) gid=65534(nogroup) groups=65534(nogroup)\nuid 65534\ngid 65534\n" + self.assertEqual(output, self.output_content(config["output_path"])) diff --git a/tests/testcase/integration/uid_gid.c b/tests/testcase/integration/uid_gid.c new file mode 100644 index 0000000..e352f98 --- /dev/null +++ b/tests/testcase/integration/uid_gid.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int main() +{ + printf("uid %d\ngid %d\n", getuid(), getgid()); + system("/usr/bin/id"); + return 0; +} \ No newline at end of file diff --git a/tests/languages/c_cpp/while1.c b/tests/testcase/integration/while1.c similarity index 100% rename from tests/languages/c_cpp/while1.c rename to tests/testcase/integration/while1.c