完善测试;修改部分异常原因的写法

This commit is contained in:
virusdefender 2016-03-18 13:12:20 +08:00
parent e54f95b3de
commit 62619ae9da
3 changed files with 86 additions and 14 deletions

View File

@ -21,11 +21,11 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
return NULL;
}
if (config.max_cpu_time <= 1) {
PyErr_SetString(PyExc_ValueError, "Max cpu time can not less than 1 ms");
PyErr_SetString(PyExc_ValueError, "max_cpu_time must > 1 ms");
return NULL;
}
if (config.max_memory < 16 * 1024 * 1024) {
PyErr_SetString(PyExc_ValueError, "Max memory can not be less than 16M");
PyErr_SetString(PyExc_ValueError, "max_memory must > 16M");
return NULL;
}
if (access(config.path, F_OK) == -1) {
@ -33,7 +33,7 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
return NULL;
}
if (access(config.in_file, F_OK) == -1) {
PyErr_SetString(PyExc_ValueError, "Input file does not exist");
PyErr_SetString(PyExc_ValueError, "in_file does not exist");
return NULL;
}
config.args[count++] = config.path;
@ -50,13 +50,13 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
break;
}
if (!PyString_Check(next) && !PyUnicode_Check(next)) {
PyErr_SetString(PyExc_ValueError, "arg must be string");
PyErr_SetString(PyExc_ValueError, "arg in args must be a string");
return NULL;
}
config.args[count] = PyString_AsString(next);
count++;
if(count > 95) {
PyErr_SetString(PyExc_ValueError, "max number of args is 95");
PyErr_SetString(PyExc_ValueError, "Number of args must < 95");
return NULL;
}
}
@ -76,13 +76,13 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
break;
}
if (!PyString_Check(next) && !PyUnicode_Check(next)) {
PyErr_SetString(PyExc_ValueError, "env must be string");
PyErr_SetString(PyExc_ValueError, "env item must be a string");
return NULL;
}
config.env[count] = PyString_AsString(next);
count++;
if(count > 95) {
PyErr_SetString(PyExc_ValueError, "max number of env is 95");
PyErr_SetString(PyExc_ValueError, "Number of env must < 95");
return NULL;
}
}
@ -91,7 +91,7 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
if (use_sandbox != NULL) {
if (!PyBool_Check(use_sandbox)) {
PyErr_SetString(PyExc_ValueError, "use sandbox must ba a bool");
PyErr_SetString(PyExc_ValueError, "use_sandbox must ba a bool");
return NULL;
}
config.use_sandbox = PyObject_IsTrue(use_sandbox);
@ -102,7 +102,7 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
if (use_nobody != NULL) {
if (!PyBool_Check(use_nobody)) {
PyErr_SetString(PyExc_ValueError, "use nobody must be a bool");
PyErr_SetString(PyExc_ValueError, "use_nobody must be a bool");
return NULL;
}
config.use_nobody = PyObject_IsTrue(use_nobody);
@ -112,7 +112,7 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
}
if(config.use_nobody && getuid() != 0) {
PyErr_SetString(PyExc_ValueError, "root user is required when using nobody");
PyErr_SetString(PyExc_ValueError, "Root user is required when use_nobody=True");
return NULL;
}

View File

@ -1 +1 @@
{"language": "c", "max_cpu_time": 300, "max_memory": 50000000}
{"language": "c", "max_cpu_time": 3000, "max_memory": 50000000}

View File

@ -45,11 +45,83 @@ class JudgerTest(TestCase):
self.assertEqual(result["signal"], run_result["signal"])
self.assertEqual(open(os.path.join(test_dir, "out")).read(),
open(os.path.join(self.tmp_path, str(i) + ".out")).read())
self._user_args_check()
self._judger_cpu_time_args_check()
self._judger_memory_args_check()
self._judger_exec_file_args_check()
self._judger_in_file_args_check()
self._judger_args_args_check()
self._judger_env_args_check()
self._judger_user_args_check()
def _user_args_check(self):
def _judger_cpu_time_args_check(self):
with self.assertRaisesRegexp(ValueError, "max_cpu_time must > 1 ms"):
judger.run(path="/bin/ls",
in_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), "/dev/null"),
out_file="/dev/null", max_cpu_time=-1, max_memory=200000000,
env=["aaa=123"], use_sandbox=False, use_nobody=False)
def _judger_memory_args_check(self):
with self.assertRaisesRegexp(ValueError, "max_memory must > 16M"):
judger.run(path="/bin/ls",
in_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), "/dev/null"),
out_file="/dev/null", max_cpu_time=1000, max_memory=100,
env=["aaa=123"], use_sandbox=True, use_nobody=True)
def _judger_exec_file_args_check(self):
with self.assertRaisesRegexp(ValueError, "in_file does not exist"):
judger.run(path="/bin/xxx",
in_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), "/dev/null"),
out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
env=["aaa=123"], use_sandbox=True, use_nobody=True)
def _judger_in_file_args_check(self):
with self.assertRaisesRegexp(ValueError, "in_file does not exist"):
judger.run(path="/bin/xxx",
in_file="/dev/xxx",
out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
env=["aaa=123"], use_sandbox=True, use_nobody=True)
def _judger_args_args_check(self):
with self.assertRaisesRegexp(ValueError, "args must be a list"):
judger.run(path="/bin/ls",
in_file="/dev/null",
out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
args=123, use_sandbox=True, use_nobody=True)
with self.assertRaisesRegexp(ValueError, "arg in args must be a string"):
judger.run(path="/bin/ls",
in_file="/dev/null",
out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
args=["123", {"a": "b"}], use_sandbox=True, use_nobody=True)
with self.assertRaisesRegexp(ValueError, "Number of args must < 95"):
judger.run(path="/bin/ls",
in_file="/dev/null",
out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
args=["123"] * 200, use_sandbox=True, use_nobody=True)
def _judger_env_args_check(self):
with self.assertRaisesRegexp(ValueError, "env must be a list"):
judger.run(path="/bin/ls",
in_file="/dev/null",
out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
env=123, use_sandbox=True, use_nobody=True)
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
judger.run(path="/bin/ls",
in_file="/dev/null",
out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
env=["123", {"a": "b"}], use_sandbox=True, use_nobody=True)
with self.assertRaisesRegexp(ValueError, "Number of env must < 95"):
judger.run(path="/bin/ls",
in_file="/dev/null",
out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
env=["123=345"] * 200, use_sandbox=True, use_nobody=True)
def _judger_user_args_check(self):
os.setuid(pwd.getpwnam("nobody").pw_uid)
with self.assertRaisesRegexp(ValueError, "root user is required when using nobody"):
with self.assertRaisesRegexp(ValueError, "Root user is required when use_nobody=True"):
judger.run(path="/bin/ls",
in_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), "/dev/null"),
out_file="/dev/null", max_cpu_time=2000, max_memory=200000000,