Example #1
0
 def _judger_memory_args_check(self):
     with self.assertRaisesRegexp(ValueError, "max_memory must > 16M or unlimited"):
         judger.run(path="/bin/true", in_file="/dev/null",
                    out_file="/dev/null", max_cpu_time=1000, max_memory=100,
                    env=["aaa=123"], use_sandbox=True, use_nobody=True)
     try:
         judger.run(path="/bin/true", in_file="/dev/null",
                    out_file="/dev/null", max_cpu_time=1000, max_memory=judger.MEMORY_UNLIMITED,
                    env=["aaa=123"], use_sandbox=True, use_nobody=True)
     except Exception as e:
         self.fail(e.message)
Example #2
0
 def _judger_cpu_time_args_check(self):
     with self.assertRaisesRegexp(ValueError, "max_cpu_time must > 1 ms or unlimited"):
         judger.run(path="/bin/true", in_file="/dev/null",
                    out_file="/dev/null", max_cpu_time=0, max_memory=200000000,
                    env=["aaa=123"], use_sandbox=False, use_nobody=False)
     try: 
         judger.run(path="/bin/true", in_file="/dev/null",
                    out_file="/dev/null", max_cpu_time=judger.CPU_TIME_UNLIMITED, max_memory=200000000,
                    env=["aaa=123"], use_sandbox=False, use_nobody=False)
     except Exception as e:
         self.fail(e.message)
    def _judge_one(self, test_case_id):
        execute_command = self._language["execute_command"].format(exe_path=self._exe_path).split(" ")

        run_result = judger.run(path=execute_command[0],
                                max_cpu_time=self._max_cpu_time,
                                max_memory=self._max_memory,
                                in_file=os.path.join(self._test_case_dir, str(test_case_id) + ".in"),
                                out_file=os.path.join(self._judge_base_path, str(test_case_id) + ".out"),
                                args=execute_command[1:],
                                env=["PATH=" + os.environ["PATH"]],
                                use_sandbox=self._language["use_sandbox"])
        if run_result["flag"] == 0:
            output_md5, r = self._compare_output(test_case_id)
            if r:
                run_result["result"] = result["accepted"]
            else:
                run_result["result"] = result["wrong_answer"]
            run_result["output_md5"] = output_md5
        elif run_result["flag"] in [1, 2]:
            run_result["result"] = result["time_limit_exceeded"]
        elif run_result["flag"] == 3:
            run_result["result"] = result["memory_limit_exceeded"]
        elif run_result["flag"] == 4:
            run_result["result"] = result["runtime_error"]
        elif run_result["flag"] == 5:
            run_result["result"] = result["system_error"]
        return run_result
Example #4
0
    def test_run(self):
        shutil.rmtree(self.tmp_path, ignore_errors=True)
        os.mkdir(self.tmp_path)
        for i in os.listdir("."):
            try:
                int(i)
            except Exception:
                continue
            print "\n\nRunning test: ", i
            test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), str(i))
            exe_path = os.path.join(self.tmp_path, str(i))
            config = json.loads(open(os.path.join(test_dir, "config")).read())
            self.assertEqual(self.compile_src(os.path.join(test_dir, "Main.c"), config.pop("language"), exe_path), 0)

            run_result = judger.run(path=exe_path,
                                    in_file=os.path.join(test_dir, "in"),
                                    out_file=os.path.join(self.tmp_path, str(i) + ".out"),
                                    **config)
            result = json.loads(open(os.path.join(test_dir, "result")).read())
            print run_result
            self.assertEqual(result["flag"], run_result["flag"])
            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._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_child_process_cpu_time_check()
        self._judger_user_args_check()
Example #5
0
    def _judge_one(self, test_case_id):
        run_result = judger.run(path=self.execute_command[0],
                                max_cpu_time=self._max_cpu_time,
                                max_memory=self._max_memory,
                                in_file=os.path.join(self._test_case_dir, str(test_case_id) + ".in"),
                                out_file=os.path.join(self._judge_base_path, str(test_case_id) + ".out"),
                                args=self.execute_command[1:],
                                env=["PATH=" + os.environ["PATH"]],
                                use_sandbox=self._language["use_sandbox"],
                                use_nobody=True)
        run_result["test_case"] = test_case_id
        
        # 对Java的特殊处理, 详见__init__函数中注释
        if self._language["name"] == "java" and run_result["memory"] > self._real_max_memory * 1.5:
            run_result["flag"] = 3

        # 将judger返回的结果标志转换为本系统中使用的
        if run_result["flag"] == 0:
            output_md5, r = self._compare_output(test_case_id)
            if r:
                run_result["result"] = result["accepted"]
            else:
                run_result["result"] = result["wrong_answer"]
            run_result["output_md5"] = output_md5
        elif run_result["flag"] in [1, 2]:
            run_result["result"] = result["time_limit_exceeded"]
        elif run_result["flag"] == 3:
            run_result["result"] = result["memory_limit_exceeded"]
        elif run_result["flag"] == 4:
            run_result["result"] = result["runtime_error"]
        elif run_result["flag"] == 5:
            run_result["result"] = result["system_error"]
        return run_result
Example #6
0
def compile_(language_item, src_path, exe_path, judge_base_path, compile_spj=False):
    command_item = "spj_compile_command" if compile_spj else "compile_command"
    compile_command = language_item[command_item].format(src_path=src_path, exe_path=exe_path).split(" ")
    compiler = compile_command[0]
    compile_args = compile_command[1:]
    compiler_output_file = os.path.join(judge_base_path, "compiler.out")

    compile_result = judger.run(path=compiler,
                                in_file="/dev/null",
                                out_file=compiler_output_file,
                                max_cpu_time=2000,
                                max_memory=2000000000,
                                args=compile_args,
                                env=["PATH=" + os.environ["PATH"]],
                                use_sandbox=False,
                                use_nobody=True)

    compile_output_handler = open(compiler_output_file)
    compile_output = compile_output_handler.read().strip()
    compile_output_handler.close()

    if compile_result["flag"] != 0:
        logger.error("Compiler error")
        logger.error(compile_output)
        logger.error(str(compile_result))
        if compile_output:
            raise CompileError(compile_output)
        else:
            raise CompileError("Compile error, info: " + str(compile_result))
    else:
        if "error" in compile_output:
            raise CompileError(compile_output)
        return exe_path
Example #7
0
def _compile():
    return judger.run(path="/usr/bin/gcc",
                      in_file=os.path.join(base_path, "in"),
                      out_file=os.path.join(base_path, "gcc_out"),
                      max_cpu_time=2000,
                      max_memory=200000000,
                      args=[os.path.join(base_path, "demo.c"), "-o", os.path.join(base_path, "demo")],
                      env=["PATH=" + os.environ["PATH"]],
                      use_sandbox=False)
Example #8
0
def _compile():
    return judger.run(path="/usr/bin/gcc",
                      in_file=os.path.join(base_path, "in"),
                      out_file=os.path.join(base_path, "gcc_out"),
                      max_cpu_time=judger.CPU_TIME_UNLIMITED,
                      max_memory=judger.MEMORY_UNLIMITED,
                      args=[os.path.join(base_path, "demo.c"), "-o", os.path.join(base_path, "demo")],
                      env=["PATH=" + os.environ["PATH"]],
                      use_sandbox=False, 
                      use_nobody=False)
def run(use_sandbox):
    path = os.path.join(base_path, "demo")
    return judger.run(path=path,
                      in_file=os.path.join(base_path, "1.in"),
                      out_file=os.path.join(base_path, "seccomp.out"),
                      # ms
                      max_cpu_time=2000000,
                      # Byte
                      max_memory=200000000,
                      # default is True
                      use_sandbox=use_sandbox)
Example #10
0
def spj(path, max_cpu_time, max_memory, in_path, user_out_path):
    if file_exists(in_path) and file_exists(user_out_path):
        result = judger.run(path=path, in_file=in_path, out_file="/tmp/spj.out",
                            max_cpu_time=max_cpu_time, max_memory=max_memory,
                            args=[in_path, user_out_path], env=["PATH=" + os.environ.get("PATH", "")],
                            use_sandbox=True, use_nobody=True)
        if result["signal"] == 0 and result["exit_status"] in [AC, WA, SPJ_ERROR]:
            result["spj_result"] = result["exit_status"]
        else:
            result["spj_result"] = SPJ_ERROR
        return result
    else:
        raise ValueError("in_path or user_out_path does not exist")
Example #11
0
 def _judger_child_process_cpu_time_check(self):
     try:
         max_cpu_time = 2000
         result = judger.run(path=os.path.join(self.tmp_path, "17"), in_file="/dev/null",
                             out_file="/dev/null", max_cpu_time=max_cpu_time, max_memory=200000000,
                             env=["aaa=123"], use_sandbox=False, use_nobody=True)
         expected_cpu_time = (max_cpu_time + 1000) / 1000 * 1000
         if result["cpu_time"] > expected_cpu_time * 1.1:
             self.fail("cpu time exceeded")
         if result["real_time"] >= max_cpu_time * 3:
             self.fail("real time exceeded")
     except Exception as e:
         self.fail(e.message)
Example #12
0
def run(use_sandbox):
    _compile()
    path = os.path.join(base_path, "demo")
    return judger.run(path=path,
                      in_file=os.path.join(base_path, "in"),
                      out_file=os.path.join(base_path, "out"),
                      # ms
                      max_cpu_time=2000,
                      # Byte
                      max_memory=200000000,
                      # args and env are optional
                      args=["1", "2", "####"],
                      env=["aaa=123"],
                      # default is True
                      use_sandbox=use_sandbox)
Example #13
0
def run(use_sandbox, use_nobody):
    print "compile result: ", _compile()
    path = os.path.join(base_path, "demo")
    return judger.run(path=path,
                      in_file=os.path.join(base_path, "in"),
                      out_file=os.path.join(base_path, "out"),
                      # ms
                      max_cpu_time=2000,
                      # Byte
                      max_memory=200000000,
                      # args env and log_path are optional
                      args=["1", "2", "####"],
                      env=["aaa=123"],
                      log_path="run.log",
                      # default is True
                      use_sandbox=use_sandbox,
                      use_nobody=use_nobody)
Example #14
0
    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)
Example #15
0
    def test_args_must_be_list(self):
        with self.assertRaisesRegex(ValueError, "args must be a list!"):
            config = self.base_config
            config["args"] = "test"
            judger.run(**config)

        with self.assertRaisesRegex(ValueError, "args must be a list!"):
            config = self.base_config
            config["args"] = ("test", 1)
            judger.run(**config)

        with self.assertRaisesRegex(ValueError, "args must be a list!"):
            config = self.base_config
            config["args"] = {"a": "v"}
            judger.run(**config)
Example #16
0
    def test_run(self):
        shutil.rmtree(self.tmp_path, ignore_errors=True)
        os.mkdir(self.tmp_path)
        for i in os.listdir("."):
            try:
                int(i)
            except Exception:
                continue
            test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), str(i))
            exe_path = os.path.join("/tmp/judger_test", str(i))
            config = json.loads(open(os.path.join(test_dir, "config")).read())
            self.assertEqual(self.compile_src(os.path.join(test_dir, "Main.c"), config.pop("language"), exe_path), 0)

            run_result = judger.run(path=exe_path,
                                    in_file=os.path.join(test_dir, "in"),
                                    out_file=os.path.join(self.tmp_path, str(i) + ".out"),
                                    **config)
            result = json.loads(open(os.path.join(test_dir, "result")).read())
            self.assertEqual(result["flag"], run_result["flag"])
            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())
Example #17
0
    def test_args_item_must_be_string(self):
        with self.assertRaisesRegex(ValueError, "args item must be a string!"):
            config = self.base_config
            config["args"] = ["test", 1]
            judger.run(**config)

        with self.assertRaisesRegex(ValueError, "args item must be a string!"):
            config = self.base_config
            config["args"] = [None, "1"]
            judger.run(**config)

        args = ["测试".encode("utf-8")]
        with self.assertRaisesRegex(ValueError, "args item must be a string!"):
            config = self.base_config
            config["args"] = args
            judger.run(**config)
Example #18
0
    def test_env_item_must_be_string(self):
        with self.assertRaisesRegex(ValueError, "env item must be a string!"):
            config = self.base_config
            config["env"] = ["test", 1]
            judger.run(**config)

        with self.assertRaisesRegex(ValueError, "env item must be a string!"):
            config = self.base_config
            config["env"] = [None, "1"]
            judger.run(**config)

        env = ["测试".encode("utf-8")]
        with self.assertRaisesRegex(ValueError, "env item must be a string!"):
            config = self.base_config
            config["env"] = env
            judger.run(**config)
Example #19
0
def compile_(language_item,
             src_path,
             exe_path,
             judge_base_path,
             compile_spj=False):
    command_item = "spj_compile_command" if compile_spj else "compile_command"
    compile_command = language_item[command_item].format(
        src_path=src_path, exe_path=exe_path).split(" ")
    compiler = compile_command[0]
    compile_args = compile_command[1:]
    compiler_output_file = os.path.join(judge_base_path, "compiler.out")

    compile_result = judger.run(
        path=compiler,
        in_file="/dev/null",
        out_file=compiler_output_file,
        max_cpu_time=language_item["compile_max_cpu_time"],
        max_memory=language_item["compile_max_memory"],
        args=compile_args,
        env=["PATH=" + os.environ["PATH"]],
        use_sandbox=False,
        use_nobody=True)

    compile_output_handler = open(compiler_output_file)
    compile_output = compile_output_handler.read().strip()
    compile_output_handler.close()

    if compile_result["flag"] != 0:
        logger.error("Compiler error")
        logger.error(compile_output)
        logger.error(str(compile_result))
        if compile_output:
            raise CompileError(compile_output)
        else:
            raise CompileError("Compile error, info: " + str(compile_result))
    else:
        if "error" in compile_output:
            raise CompileError(compile_output)
        return exe_path
Example #20
0
    def test_run(self):
        shutil.rmtree(self.tmp_path, ignore_errors=True)
        os.mkdir(self.tmp_path)
        for i in os.listdir("."):
            try:
                int(i)
            except Exception:
                continue
            print "\n\nRunning test: ", i
            test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    str(i))
            exe_path = os.path.join(self.tmp_path, str(i))
            config = json.loads(open(os.path.join(test_dir, "config")).read())
            self.assertEqual(
                self.compile_src(os.path.join(test_dir, "Main.c"),
                                 config.pop("language"), exe_path), 0)

            run_result = judger.run(path=exe_path,
                                    in_file=os.path.join(test_dir, "in"),
                                    out_file=os.path.join(
                                        self.tmp_path,
                                        str(i) + ".out"),
                                    **config)
            result = json.loads(open(os.path.join(test_dir, "result")).read())
            print run_result
            self.assertEqual(result["flag"], run_result["flag"])
            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._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_child_process_cpu_time_check()
        self._judger_user_args_check()
Example #21
0
 def test_child_real_time(self):
     config = self.base_config
     config["exe_path"] = self._compile_c("test_child_real_time.c")
     result = judger.run(**config)
     self.assertEqual(result["result"],
                      judger.RESULT_REAL_TIME_LIMIT_EXCEEDED)
Example #22
0
 def _judger_in_file_args_check(self):
     with self.assertRaisesRegexp(ValueError, "in_file does not exist"):
         judger.run(path="/bin/true", in_file="/dev/xxx",
                    out_file="/dev/null", max_cpu_time=1000, max_memory=200000000,
                    env=["aaa=123"], use_sandbox=True, use_nobody=True)
Example #23
0
File: demo.py Project: WPH95/Judger
# coding=utf-8
import os
import judger


base_path = os.path.dirname(os.path.abspath(__file__))


print judger.run(path=os.path.join(base_path, "test"),
                 in_file=os.path.join(base_path, "in"),
                 out_file=os.path.join(base_path, "out"),
                 max_cpu_time=2000,
                 max_memory=200000000,
                 args=["1", "2", "####"],
                 env=["LD_PRELOAD=" + os.path.join(base_path, "sandbox.so")])

Example #24
0
    def test_sysinfo(self):
        config = self.base_config
        config["exe_path"] = self._compile_c("test_sysinfo.c")

        result = judger.run(**config)
        self.assertEqual(result["result"], judger.RESULT_SUCCESS)
Example #25
0
 def _judger_user_args_check(self):
     os.setuid(pwd.getpwnam("nobody").pw_uid)
     with self.assertRaisesRegexp(ValueError, "Root user is required when use_nobody=True"):
         judger.run(path="/bin/ls", in_file="/dev/null",
                    out_file="/dev/null", max_cpu_time=2000, max_memory=200000000,
                    env=["aaa=123"], use_sandbox=True, use_nobody=True)