Commit 29d891a3 authored by Andreas Fried's avatar Andreas Fried

Read test input as binary data.

parent 7b4adf13
......@@ -180,13 +180,13 @@ class Environment:
cmd.extend(list(args))
return execute(cmd, timeout=timeout or self.timeout)
def run_command(self, cmd: str, *args: Tuple[str], timeout: float = None, input_str: Optional[str] = None) -> Tuple[bytes, bytes, int]:
def run_command(self, cmd: str, *args: Tuple[str], timeout: float = None, input_bytes: Optional[bytes] = None) -> Tuple[bytes, bytes, int]:
"""
Execute the passend command with its arguments
:return: (out, err, return code)
"""
return execute([cmd] + list(args), timeout=timeout or self.timeout, input_str=input_str)
return execute([cmd] + list(args), timeout=timeout or self.timeout, input_bytes=input_bytes)
def has_to_preprocess(self, file: str) -> bool:
return os.path.relpath(file, self.test_dir).startswith("exec")
......
......@@ -59,24 +59,23 @@ class JavaExecTest(BasicSyntaxTest):
cwd = os.getcwd()
os.chdir(tmp_dir)
exp_out = None
input_str = None
input_bytes = None
#print(base_filename, get_main_class_name(base_filename + ".java"))
test_result = ExtensibleTestResult(self)
if self._has_input_file:
if self._has_character_input:
with open(self._input_file, "r", errors="backslashreplace") as f:
input_str = f.read()
with open(self._input_file, "rb") as f:
input_bytes = f.read()
else:
with open(self._input_file, "r", errors="backslashreplace") as f:
chars = bytearray('ascii', 'ignore') # type: bytearray
input_bytes = bytearray(encoding='ascii', errors='ignore') # type: bytearray
for line in f.readlines():
for part in line.split(" "):
part = part.strip()
if len(part) > 0:
chars.append(int(part))
input_str = decode(chars)
if not self._has_expected_output_file and not self._only_compile:
_, err, javac_rtcode = \
......@@ -95,7 +94,7 @@ class JavaExecTest(BasicSyntaxTest):
main_class = base_filename
if not self._only_compile:
exp_out, err, java_rtcode = \
self.env.run_command("java", get_main_class_name(base_filename + ".java"), timeout=timeout, input_str=input_str)
self.env.run_command("java", get_main_class_name(base_filename + ".java"), timeout=timeout, input_bytes=input_bytes)
if javac_rtcode != 0:
test_result.add_long_text("Java output: ", decode(exp_out))
test_result.incorrect_msg = "java runtime error"
......@@ -146,7 +145,7 @@ class JavaExecTest(BasicSyntaxTest):
test_result.add_file("Source file", self.preprocessed_file)
return test_result
try:
out, err, rtcode = self.env.run_command("./a.out", timeout=timeout, input_str=input_str)
out, err, rtcode = self.env.run_command("./a.out", timeout=timeout, input_bytes=input_bytes)
if rtcode != 0:
test_result.incorrect_msg = "binary can't be run, non zero error code"
test_result.set_error_code(rtcode)
......
......@@ -46,14 +46,14 @@ def _lower_rlimit(res, limit):
class _Execute(object):
def __init__(self, cmd, timeout, env, rlimit, input_str):
def __init__(self, cmd, timeout, env, rlimit, input_bytes):
self.cmd = cmd
self.timeout = timeout
self.env = env
self.proc = None
self.exception = None
self.rlimit = rlimit
self.input_str = input_str
self.input_bytes = input_bytes
MB = 1024 * 1024
if not 'RLIMIT_CORE' in rlimit:
rlimit['RLIMIT_CORE'] = 0
......@@ -80,14 +80,13 @@ class _Execute(object):
self.proc = subprocess.Popen(self.cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE if self.input_str else None,
stdin=subprocess.PIPE if self.input_bytes else None,
env=self.env,
shell=True,
**prexec_args)
#if self.input_str:
# self.proc.stdin.write(self.input_str.decode)
input_bytes = self.input_str.encode() if self.input_str else None
x = self.proc.communicate(input=input_bytes, timeout=self.timeout if self.timeout > 0.0 else None)
#if self.input_bytes:
# self.proc.stdin.write(self.input_bytes.decode)
x = self.proc.communicate(input=self.input_bytes, timeout=self.timeout if self.timeout > 0.0 else None)
self.out, self.err = x
self.returncode = self.proc.returncode
except subprocess.TimeoutExpired as t:
......@@ -110,7 +109,7 @@ class _Execute(object):
raise SigKill(-self.returncode, _EXIT_CODES[self.returncode] + ": " + os.strerror(-self.returncode))
return (self.out, self.err, self.returncode)
def execute(cmd, env=None, timeout=0, rlimit=None, propagate_sigint=True, input_str = None):
def execute(cmd, env=None, timeout=0, rlimit=None, propagate_sigint=True, input_bytes = None):
"""Execute a command and return stderr and stdout data"""
if not rlimit:
rlimit = dict()
......@@ -120,7 +119,7 @@ def execute(cmd, env=None, timeout=0, rlimit=None, propagate_sigint=True, input_
else:
#cmd = shlex.split(cmd[0]) + cmd[1:]
cmd = " ".join(shlex.quote(c) for c in cmd)
exc = _Execute(cmd, timeout, env, rlimit, input_str)
exc = _Execute(cmd, timeout, env, rlimit, input_bytes)
(out, err, returncode) = exc.run()
if returncode == -signal.SIGINT:
raise KeyboardInterrupt
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment