copy test args

This commit is contained in:
LiYang 2017-05-22 10:54:21 +08:00
parent 3f60f3bb00
commit 9fd357a5ed
3 changed files with 44 additions and 55 deletions

View File

@ -3,6 +3,7 @@ from __future__ import print_function
import os
import random
import shutil
import copy
from unittest import TestCase
@ -56,3 +57,23 @@ class BaseTestCase(TestCase):
def output_content(self, path):
with open(path, "r") as f:
return f.read()
@property
def base_config(self):
config = {"max_cpu_time": 1000,
"max_real_time": 3000,
"max_memory": 128 * 1024 * 1024,
"max_stack": 32 * 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_name": None,
"uid": 0,
"gid": 0}
return config

View File

@ -11,22 +11,6 @@ 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": 128 * 1024 * 1024,
"max_stack": 32 * 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_name": None,
"uid": 0,
"gid": 0}
self.workspace = self.init_workspace("integration")
def _compile_c(self, src_name, extra_flags=None):
@ -146,9 +130,9 @@ class IntegrationTest(base.BaseTestCase):
input_path="/dev/null", output_path="/dev/null", error_path="/dev/null",
args=["12344"], env=["a=b"], log_path="/dev/null",
seccomp_rule_name=None, uid=0, gid=0)
def test_normal(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("normal.c")
config["input_path"] = self.make_input("judger_test")
config["output_path"] = config["error_path"] = self.output_path()
@ -165,7 +149,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertEqual("abs 1024", self.output_content(config["output_path"]))
def test_args(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("args.c")
config["args"] = ["test", "hehe", "000"]
config["output_path"] = config["error_path"] = self.output_path()
@ -175,7 +159,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertEqual(output, self.output_content(config["output_path"]))
def test_env(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("env.c")
config["output_path"] = config["error_path"] = self.output_path()
result = _judger.run(**config)
@ -184,7 +168,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertEqual(output, self.output_content(config["output_path"]))
def test_real_time(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("sleep.c")
result = _judger.run(**config)
self.assertEqual(result["result"], _judger.RESULT_REAL_TIME_LIMIT_EXCEEDED)
@ -192,7 +176,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertTrue(result["real_time"] >= config["max_real_time"])
def test_cpu_time(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("while1.c")
result = _judger.run(**config)
self.assertEqual(result["result"], _judger.RESULT_CPU_TIME_LIMIT_EXCEEDED)
@ -200,7 +184,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertTrue(result["cpu_time"] >= config["max_cpu_time"])
def test_memory1(self):
config = self.config
config = self.base_config
config["max_memory"] = 64 * 1024 * 1024
config["exe_path"] = self._compile_c("memory1.c")
result = _judger.run(**config)
@ -209,7 +193,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertEqual(result["result"], _judger.RESULT_MEMORY_LIMIT_EXCEEDED)
def test_memory2(self):
config = self.config
config = self.base_config
config["max_memory"] = 64 * 1024 * 1024
config["exe_path"] = self._compile_c("memory2.c")
result = _judger.run(**config)
@ -220,7 +204,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
def test_memory3(self):
config = self.config
config = self.base_config
config["max_memory"] = 512 * 1024 * 1024
config["exe_path"] = self._compile_c("memory3.c")
result = _judger.run(**config)
@ -228,34 +212,34 @@ class IntegrationTest(base.BaseTestCase):
self.assertTrue(result["memory"] >= 102400000 * 4)
def test_re1(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("re1.c")
result = _judger.run(**config)
# re1.c return 25
self.assertEqual(result["exit_code"], 25)
def test_re2(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("re2.c")
result = _judger.run(**config)
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
self.assertEqual(result["signal"], signal.SIGSEGV)
def test_child_proc_cpu_time_limit(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("child_proc_cpu_time_limit.c")
result = _judger.run(**config)
self.assertEqual(result["result"], _judger.RESULT_CPU_TIME_LIMIT_EXCEEDED)
def test_child_proc_real_time_limit(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("child_proc_real_time_limit.c")
result = _judger.run(**config)
self.assertEqual(result["result"], _judger.RESULT_REAL_TIME_LIMIT_EXCEEDED)
self.assertEqual(result["signal"], signal.SIGKILL)
def test_stdout_and_stderr(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("stdout_stderr.c")
config["output_path"] = config["error_path"] = self.output_path()
result = _judger.run(**config)
@ -264,7 +248,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertEqual(output, self.output_content(config["output_path"]))
def test_uid_and_gid(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("uid_gid.c")
config["output_path"] = config["error_path"] = self.output_path()
config["uid"] = 65534
@ -275,7 +259,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertEqual(output, self.output_content(config["output_path"]))
def test_gcc_random(self):
config = self.config
config = self.base_config
config["exe_path"] = "/usr/bin/gcc"
config["args"] = ["../test_src/integration/gcc_random.c",
"-o", os.path.join(self.workspace, "gcc_random")]
@ -285,7 +269,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertTrue(result["real_time"] >= 1950)
def test_cpp_meta(self):
config = self.config
config = self.base_config
config["exe_path"] = "/usr/bin/g++"
config["max_memory"] = 1024 * 1024 * 1024
config["args"] = ["../test_src/integration/cpp_meta.cpp",
@ -296,14 +280,14 @@ class IntegrationTest(base.BaseTestCase):
self.assertTrue(result["real_time"] >= 1950)
def test_output_size(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_c("output_size.c")
config["max_output_size"] = 1000 * 10
result = _judger.run(**config)
self.assertEqual(result["exit_code"], 2)
def test_stack_size(self):
config = self.config
config = self.base_config
config["max_memory"] = 256 * 1024 * 1024
config["exe_path"] = self._compile_c("stack.c")
config["output_path"] = config["error_path"] = self.output_path()
@ -317,7 +301,7 @@ class IntegrationTest(base.BaseTestCase):
self.assertEqual("big stack", self.output_content(config["output_path"]))
def test_writev(self):
config = self.config
config = self.base_config
config["exe_path"] = self._compile_cpp("writev.cpp")
config["seccomp_rule_name"] = "c_cpp"
config["input_path"] = self.make_input("111" * 10000 + "\n")

View File

@ -10,29 +10,13 @@ from .. import base
class SeccompTest(base.BaseTestCase):
def setUp(self):
print("Running", self._testMethodName)
self.config = {"max_cpu_time": 1000,
"max_real_time": 3000,
"max_memory": 1024 * 1024 * 128,
"max_stack": 32 * 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_name": None,
"uid": 0,
"gid": 0}
self.workspace = self.init_workspace("integration")
def _compile_c(self, src_name, extra_flags=None):
return super(SeccompTest, self)._compile_c("../../test_src/seccomp/" + src_name, extra_flags)
def test_fork(self):
config = self.config
config = self.base_config
config["max_memory"] = 1024 * 1024 * 1024
config["exe_path"] = self._compile_c("fork.c")
config["output_path"] = config["error_path"] = self.output_path()
@ -54,7 +38,7 @@ class SeccompTest(base.BaseTestCase):
self.assertEqual(result["signal"], 31)
def test_execve(self):
config = self.config
config = self.base_config
config["max_memory"] = 1024 * 1024 * 1024
config["exe_path"] = self._compile_c("execve.c")
config["output_path"] = config["error_path"] = self.output_path()
@ -76,7 +60,7 @@ class SeccompTest(base.BaseTestCase):
self.assertEqual(result["signal"], 31)
def test_write_file(self):
config = self.config
config = self.base_config
config["max_memory"] = 1024 * 1024 * 1024
config["exe_path"] = self._compile_c("write_file.c")
config["output_path"] = config["error_path"] = self.output_path()