增加集成测试和部分超时测试

This commit is contained in:
LiYang 2016-08-23 14:45:13 +08:00
parent 055af3fab9
commit 9eaf64ffe2
8 changed files with 196 additions and 4 deletions

View File

@ -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);
}

View File

100
tests/integration/test.py Normal file
View File

@ -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)

View File

@ -0,0 +1,29 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/types.h>
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;
}

View File

@ -0,0 +1,29 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/types.h>
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;
}

View File

@ -1 +1,8 @@
#include <stdio.h>
#include <stdio.h>
#include <signal.h>
int main()
{
raise(SIGSEGV);
return 0;
}

View File

@ -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)

View File

@ -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