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

Read test input as binary data.

parent 7b4adf13
...@@ -180,13 +180,13 @@ class Environment: ...@@ -180,13 +180,13 @@ class Environment:
cmd.extend(list(args)) cmd.extend(list(args))
return execute(cmd, timeout=timeout or self.timeout) 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 Execute the passend command with its arguments
:return: (out, err, return code) :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: def has_to_preprocess(self, file: str) -> bool:
return os.path.relpath(file, self.test_dir).startswith("exec") return os.path.relpath(file, self.test_dir).startswith("exec")
...@@ -249,4 +249,4 @@ class Environment: ...@@ -249,4 +249,4 @@ class Environment:
timer.cancel() timer.cancel()
else: else:
stdout, stderr = proc.communicate() stdout, stderr = proc.communicate()
return stdout, stderr, proc.returncode, timeouted return stdout, stderr, proc.returncode, timeouted
\ No newline at end of file
...@@ -59,24 +59,23 @@ class JavaExecTest(BasicSyntaxTest): ...@@ -59,24 +59,23 @@ class JavaExecTest(BasicSyntaxTest):
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(tmp_dir) os.chdir(tmp_dir)
exp_out = None exp_out = None
input_str = None input_bytes = None
#print(base_filename, get_main_class_name(base_filename + ".java")) #print(base_filename, get_main_class_name(base_filename + ".java"))
test_result = ExtensibleTestResult(self) test_result = ExtensibleTestResult(self)
if self._has_input_file: if self._has_input_file:
if self._has_character_input: if self._has_character_input:
with open(self._input_file, "r", errors="backslashreplace") as f: with open(self._input_file, "rb") as f:
input_str = f.read() input_bytes = f.read()
else: else:
with open(self._input_file, "r", errors="backslashreplace") as f: 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 line in f.readlines():
for part in line.split(" "): for part in line.split(" "):
part = part.strip() part = part.strip()
if len(part) > 0: if len(part) > 0:
chars.append(int(part)) chars.append(int(part))
input_str = decode(chars)
if not self._has_expected_output_file and not self._only_compile: if not self._has_expected_output_file and not self._only_compile:
_, err, javac_rtcode = \ _, err, javac_rtcode = \
...@@ -95,7 +94,7 @@ class JavaExecTest(BasicSyntaxTest): ...@@ -95,7 +94,7 @@ class JavaExecTest(BasicSyntaxTest):
main_class = base_filename main_class = base_filename
if not self._only_compile: if not self._only_compile:
exp_out, err, java_rtcode = \ 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: if javac_rtcode != 0:
test_result.add_long_text("Java output: ", decode(exp_out)) test_result.add_long_text("Java output: ", decode(exp_out))
test_result.incorrect_msg = "java runtime error" test_result.incorrect_msg = "java runtime error"
...@@ -146,7 +145,7 @@ class JavaExecTest(BasicSyntaxTest): ...@@ -146,7 +145,7 @@ class JavaExecTest(BasicSyntaxTest):
test_result.add_file("Source file", self.preprocessed_file) test_result.add_file("Source file", self.preprocessed_file)
return test_result return test_result
try: 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: if rtcode != 0:
test_result.incorrect_msg = "binary can't be run, non zero error code" test_result.incorrect_msg = "binary can't be run, non zero error code"
test_result.set_error_code(rtcode) test_result.set_error_code(rtcode)
......
...@@ -582,4 +582,4 @@ import mjtest.test.syntax_tests ...@@ -582,4 +582,4 @@ import mjtest.test.syntax_tests
import mjtest.test.ast_tests import mjtest.test.ast_tests
import mjtest.test.semantic_tests import mjtest.test.semantic_tests
import mjtest.test.exec_tests import mjtest.test.exec_tests
import mjtest.test.bench import mjtest.test.bench
\ No newline at end of file
...@@ -16,7 +16,7 @@ try: ...@@ -16,7 +16,7 @@ try:
except ImportError: except ImportError:
has_resource_module = False has_resource_module = False
pass pass
import sys import sys
import signal import signal
import threading import threading
...@@ -46,14 +46,14 @@ def _lower_rlimit(res, limit): ...@@ -46,14 +46,14 @@ def _lower_rlimit(res, limit):
class _Execute(object): 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.cmd = cmd
self.timeout = timeout self.timeout = timeout
self.env = env self.env = env
self.proc = None self.proc = None
self.exception = None self.exception = None
self.rlimit = rlimit self.rlimit = rlimit
self.input_str = input_str self.input_bytes = input_bytes
MB = 1024 * 1024 MB = 1024 * 1024
if not 'RLIMIT_CORE' in rlimit: if not 'RLIMIT_CORE' in rlimit:
rlimit['RLIMIT_CORE'] = 0 rlimit['RLIMIT_CORE'] = 0
...@@ -80,14 +80,13 @@ class _Execute(object): ...@@ -80,14 +80,13 @@ class _Execute(object):
self.proc = subprocess.Popen(self.cmd, self.proc = subprocess.Popen(self.cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=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, env=self.env,
shell=True, shell=True,
**prexec_args) **prexec_args)
#if self.input_str: #if self.input_bytes:
# self.proc.stdin.write(self.input_str.decode) # self.proc.stdin.write(self.input_bytes.decode)
input_bytes = self.input_str.encode() if self.input_str else None x = self.proc.communicate(input=self.input_bytes, timeout=self.timeout if self.timeout > 0.0 else None)
x = self.proc.communicate(input=input_bytes, timeout=self.timeout if self.timeout > 0.0 else None)
self.out, self.err = x self.out, self.err = x
self.returncode = self.proc.returncode self.returncode = self.proc.returncode
except subprocess.TimeoutExpired as t: except subprocess.TimeoutExpired as t:
...@@ -110,7 +109,7 @@ class _Execute(object): ...@@ -110,7 +109,7 @@ class _Execute(object):
raise SigKill(-self.returncode, _EXIT_CODES[self.returncode] + ": " + os.strerror(-self.returncode)) raise SigKill(-self.returncode, _EXIT_CODES[self.returncode] + ": " + os.strerror(-self.returncode))
return (self.out, self.err, 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""" """Execute a command and return stderr and stdout data"""
if not rlimit: if not rlimit:
rlimit = dict() rlimit = dict()
...@@ -120,7 +119,7 @@ def execute(cmd, env=None, timeout=0, rlimit=None, propagate_sigint=True, input_ ...@@ -120,7 +119,7 @@ def execute(cmd, env=None, timeout=0, rlimit=None, propagate_sigint=True, input_
else: else:
#cmd = shlex.split(cmd[0]) + cmd[1:] #cmd = shlex.split(cmd[0]) + cmd[1:]
cmd = " ".join(shlex.quote(c) for c in cmd) 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() (out, err, returncode) = exc.run()
if returncode == -signal.SIGINT: if returncode == -signal.SIGINT:
raise KeyboardInterrupt 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