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 0000000..8dafaa0 Binary files /dev/null and b/tests/testcase/integration/a.out differ diff --git a/tests/languages/c_cpp/args.c b/tests/testcase/integration/args.c similarity index 100% rename from tests/languages/c_cpp/args.c rename to tests/testcase/integration/args.c diff --git a/tests/languages/c_cpp/child_proc_cpu_time_limit.c b/tests/testcase/integration/child_proc_cpu_time_limit.c similarity index 100% rename from tests/languages/c_cpp/child_proc_cpu_time_limit.c rename to tests/testcase/integration/child_proc_cpu_time_limit.c diff --git a/tests/languages/c_cpp/child_proc_real_time_limit.c b/tests/testcase/integration/child_proc_real_time_limit.c similarity index 100% rename from tests/languages/c_cpp/child_proc_real_time_limit.c rename to tests/testcase/integration/child_proc_real_time_limit.c diff --git a/tests/languages/c_cpp/env.c b/tests/testcase/integration/env.c similarity index 100% rename from tests/languages/c_cpp/env.c rename to tests/testcase/integration/env.c diff --git a/tests/languages/c_cpp/memory1.c b/tests/testcase/integration/memory1.c similarity index 100% rename from tests/languages/c_cpp/memory1.c rename to tests/testcase/integration/memory1.c diff --git a/tests/languages/c_cpp/memory2.c b/tests/testcase/integration/memory2.c similarity index 100% rename from tests/languages/c_cpp/memory2.c rename to tests/testcase/integration/memory2.c diff --git a/tests/languages/c_cpp/normal.c b/tests/testcase/integration/normal.c similarity index 100% rename from tests/languages/c_cpp/normal.c rename to tests/testcase/integration/normal.c diff --git a/tests/languages/c_cpp/re1.c b/tests/testcase/integration/re1.c similarity index 100% rename from tests/languages/c_cpp/re1.c rename to tests/testcase/integration/re1.c diff --git a/tests/languages/c_cpp/re2.c b/tests/testcase/integration/re2.c similarity index 100% rename from tests/languages/c_cpp/re2.c rename to tests/testcase/integration/re2.c diff --git a/tests/languages/c_cpp/sleep.c b/tests/testcase/integration/sleep.c similarity index 100% rename from tests/languages/c_cpp/sleep.c rename to tests/testcase/integration/sleep.c diff --git a/tests/testcase/integration/stdout_stderr.c b/tests/testcase/integration/stdout_stderr.c new file mode 100644 index 0000000..5594d8f --- /dev/null +++ b/tests/testcase/integration/stdout_stderr.c @@ -0,0 +1,11 @@ +#include +#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