exec_tests.py 18.8 KB
Newer Older
1
import hashlib
2 3 4
import logging
import os
import shutil
5
import signal
6
from os import path
7 8 9 10

import re
from typing import Optional

11 12
import errno

13 14
from mjtest.environment import TestMode, Environment
from mjtest.test.syntax_tests import BasicSyntaxTest
15
from mjtest.test.tests import TestCase, BasicDiffTestResult, BasicTestResult, ExtensibleTestResult
16
from mjtest.util.shell import SigKill
17
from mjtest.util.utils import get_main_class_name, InsertionTimeOrderedDict, decode
18 19 20 21 22 23 24 25

_LOG = logging.getLogger("exec_tests")

class JavaExecTest(BasicSyntaxTest):
    """
    The MiniJava compiler should behave the same as javac
    """

26
    FILE_ENDINGS = [".java", ".mj"]
27
    INVALID_FILE_ENDINGS = [".inf.java", ".inf.mj", ".input.mj", ".input.java", ".invalid.java", ".invalid.mj"]
28
    OUTPUT_FILE_ENDING = ".out"
29
    MODE = TestMode.compile_firm
30
    INPUT_FILE_REGEXP = r'(\.[0-9]+)\.input(c?)$'
31

32 33
    def __init__(self, env: Environment, type: str, file: str, preprocessed_file: str, log_file_mode: str):
        super().__init__(env, type, file, preprocessed_file, log_file_mode)
34 35 36
        self._has_input_file = bool(re.search(self.INPUT_FILE_REGEXP, file))
        self._input_file = file if self._has_input_file else None
        self._has_character_input = file.endswith("c") if self._has_input_file else False
37
        self._expected_output_file = file + self.OUTPUT_FILE_ENDING
38 39 40 41
        prev_out_dir = path.join(path.dirname(file), ".java_output")
        if not path.exists(prev_out_dir):
            os.mkdir(prev_out_dir)
        self._prev_out_file = path.join(prev_out_dir, path.basename(self._expected_output_file))
42
        self._prev_out_hash_file = self._prev_out_file + "_hash"
43
        self._has_expected_output_file = path.exists(self._expected_output_file)
44
        if not self._has_expected_output_file:
45 46
            if path.exists(self._prev_out_file) and path.exists(self._prev_out_hash_file) \
                    and self._check_hash_sum(self.preprocessed_file, self._prev_out_hash_file):
47 48 49
                self._has_expected_output_file = True
                self._expected_output_file = self._prev_out_file
                _LOG.info("Reuse old java output file \"{}\"".format(path.relpath(self._prev_out_file)))
50
        self._should_succeed = True
51
        self._only_compile = self.MODE.endswith("-only")
52 53

    def run(self) -> BasicDiffTestResult:
54 55
        is_big_testcase = "big" in self.file
        timeout = self.env.big_timeout if is_big_testcase else self.env.timeout
56
        base_filename = path.basename(self.file).split(".")[0]
Johannes Bechberger's avatar
Johannes Bechberger committed
57
        tmp_dir = self.env.create_pid_local_tmpdir()
58 59 60 61
        shutil.copy(self.preprocessed_file, path.join(tmp_dir, base_filename + ".java"))
        cwd = os.getcwd()
        os.chdir(tmp_dir)
        exp_out = None
62
        input_bytes = None
63
        #print(base_filename, get_main_class_name(base_filename + ".java"))
64 65 66

        test_result = ExtensibleTestResult(self)

67 68
        if self._has_input_file:
            if self._has_character_input:
69 70
                with open(self._input_file, "rb") as f:
                    input_bytes = f.read()
71
            else:
72
                with open(self._input_file, "r", errors="backslashreplace") as f:
73
                    input_bytes = bytearray(encoding='ascii', errors='ignore')  # type: bytearray
74 75 76 77 78 79
                    for line in f.readlines():
                        for part in line.split(" "):
                            part = part.strip()
                            if len(part) > 0:
                                chars.append(int(part))

80
        if not self._has_expected_output_file and not self._only_compile:
81
            _, err, javac_rtcode = \
82
                self.env.run_command("javac", base_filename + ".java", timeout=timeout)
83 84
            if javac_rtcode != 0:
                _LOG.error("File \"{}\" isn't valid Java".format(self.preprocessed_file))
85
                test_result.incorrect_msg = "invalid java code, but output file missing"
86
                test_result.set_error_code(javac_rtcode)
87
                test_result.add_long_text("Javac error message", decode(err))
88
                test_result.add_file("Source file", self.preprocessed_file)
89
                os.chdir(cwd)
90
                return test_result
91
            main_class = get_main_class_name(base_filename + ".java")
92 93 94
            if not main_class:
                _LOG.debug("Can't find a main class, using the file name instead")
                main_class = base_filename
95 96
            if not self._only_compile:
                exp_out, err, java_rtcode = \
97
                    self.env.run_command("java", get_main_class_name(base_filename + ".java"), timeout=timeout, input_bytes=input_bytes)
98
                if javac_rtcode != 0:
99
                    test_result.add_long_text("Java output: ", decode(exp_out))
100 101
                    test_result.incorrect_msg = "java runtime error"
                    test_result.set_error_code(java_rtcode)
102
                    test_result.add_long_text("Java error message", decode(err))
103 104 105 106 107
                    test_result.add_file("Source file", self.preprocessed_file)
                    if self._input_file:
                        test_result.add_file("Input file", self.file)
                    os.chdir(cwd)
                    return test_result
108
                exp_out = decode(exp_out)
109

110
                with open(self._prev_out_file, "w", errors="backslashreplace") as f:
111 112
                    f.write(exp_out)
                    f.flush()
113
                with open(self._prev_out_hash_file, "w", errors="backslashreplace") as f:
114 115 116 117
                    f.write(self._hash_sum_for_file(base_filename + ".java"))
                    f.flush()
        if self._has_expected_output_file and self.type == self.MODE and self.env.mode == self.MODE \
                and not self._only_compile:
118
            with open(self._expected_output_file, "r", errors="backslashreplace") as f:
119
                exp_out = f.read()
120
        try:
121 122
            out, err, rtcode = None, None, None
            try:
123
                out, err, rtcode = self.env.run_mj_command(self.MODE, base_filename + ".java", timeout=-1)
124 125
                if rtcode != 0:
                    test_result.incorrect_msg = "file can't be compiled"
126
                    test_result.set_error_code(rtcode)
127 128
                    test_result.add_long_text("Error output", decode(err))
                    test_result.add_long_text("Output", decode(out))
129 130 131 132 133
                    test_result.add_file("Source file", self.preprocessed_file)
                    os.chdir(cwd)
                    return test_result
            except SigKill as sig:
                test_result.incorrect_msg = "file can't be compiled: " + sig.name
134
                test_result.set_error_code(sig.retcode)
135 136 137 138 139 140
                test_result.add_file("Source file", self.preprocessed_file)
                os.chdir(cwd)
                return test_result
            except:
                os.chdir(cwd)
                raise
141 142 143 144 145 146
            if self._only_compile:
                if not os.path.exists("a.out"):
                    test_result.incorrect_msg = "binary cannot be found"
                    test_result.set_error_code(1)
                    test_result.add_file("Source file", self.preprocessed_file)
                return test_result
147
            try:
148
                out, err, rtcode = self.env.run_command("./a.out", timeout=timeout, input_bytes=input_bytes)
149 150 151
                if rtcode != 0:
                    test_result.incorrect_msg = "binary can't be run, non zero error code"
                    test_result.set_error_code(rtcode)
152 153
                    test_result.add_long_text("Error output", decode(err))
                    test_result.add_long_text("Output", decode(out))
154
                    test_result.add_file("Source file", self.preprocessed_file)
155 156
                    if self._input_file:
                        test_result.add_file("Input file", self.file)
157 158
                    os.chdir(cwd)
                    return test_result
159
            except SigKill as sig:
160 161
                test_result.incorrect_msg = "binary can't be run: " + sig.name.strip()
                test_result.set_error_code(sig.retcode)
162
                test_result.add_file("Source file", self.preprocessed_file)
163 164
                if self._input_file:
                    test_result.add_file("Input file", self.file)
165 166 167 168 169
                os.chdir(cwd)
                return test_result
            except:
                os.chdir(cwd)
                raise
170
            out = decode(out)
171
            if self.type == self.MODE and self.env.mode == self.MODE:
172 173
                if exp_out.strip() != out.strip():
                    test_result.incorrect_msg = "incorrect output"
174
                    test_result.has_succeeded = False
175 176 177
                    test_result.add_short_text("Expected output file", self._expected_output_file)
                    test_result.add_long_text("Expected output", exp_out)
                    test_result.add_long_text("Actual output", out)
178
                    test_result.add_diff("Output diff [expected <-> actual]", exp_out, out, with_line_numbers=True)
179
                    test_result.add_file("Source file", self.preprocessed_file)
180 181
                    if self._input_file:
                        test_result.add_file("Input file", self.file)
Johannes Bechberger's avatar
Fix #72  
Johannes Bechberger committed
182 183
                else:
                    test_result.add_long_text("Output", out)
184 185
                os.chdir(cwd)
                return test_result
186
            return BasicTestResult(self, rtcode, out, decode(err))
187 188
        except SigKill as sig:
            os.chdir(cwd)
189
            assert False
190
        except BaseException:
191
            os.chdir(cwd)
Johannes Bechberger's avatar
Johannes Bechberger committed
192
            raise
193

194 195
    def _check_hash_sum(self, file: str, hash_sum_file: str) -> bool:
        old_hash = ""
196
        with open(hash_sum_file, "r", errors="backslashreplace") as f:
197 198 199 200
            try:
                old_hash = f.readline().strip()
            except UnicodeDecodeError:
                _LOG.exception("Decoding a hash sum for java output caching failed")
201
        return self._hash_sum_for_file(file) == old_hash
202

203
    @classmethod
204
    def _hash_sum_for_file(self, file: str) -> str:
205
        with open(file, "r", errors="backslashreplace") as f:
206
            return hashlib.sha256(f.read().encode()).hexdigest()
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    @classmethod
    def _test_case_file_for_input_file(cls, input_file: str) -> Optional[str]:
        base = re.sub(cls.INPUT_FILE_REGEXP, "", input_file)
        if path.exists(base + ".input.mj"):
            return base + ".input.mj"
        if path.exists(base + ".input.java"):
            return base + ".input.java"
        return None

    @staticmethod
    def is_file_ending_valid(cls, file: str) -> bool:
        if re.search(cls.INPUT_FILE_REGEXP, file) and ".java_output" not in file and "precprocessor" not in file:
            if not JavaExecTest._test_case_file_for_input_file(file):
                _LOG.error("Skip {} that hasn't a corresponding test case file".format(file))
            return True
        return super().is_file_ending_valid(cls, file)

    @staticmethod
    def _get_test_case_file(cls, file: str) -> Optional[str]:
        if re.search(JavaExecTest.INPUT_FILE_REGEXP, file):
            return JavaExecTest._test_case_file_for_input_file(file)
        return file

231 232
TestCase.TEST_CASE_CLASSES[TestMode.compile_firm].append(JavaExecTest)

Johannes Bechberger's avatar
Johannes Bechberger committed
233 234 235 236
class JavaCompileExecTest(JavaExecTest):
    MODE = TestMode.compile

TestCase.TEST_CASE_CLASSES[TestMode.compile].append(JavaCompileExecTest)
237

238 239 240 241 242 243 244 245 246 247
class JavaCompileOnlyTest(JavaExecTest):
    MODE = TestMode.compile_only

TestCase.TEST_CASE_CLASSES[TestMode.compile_only].append(JavaCompileOnlyTest)

class JavaCompileFirmOnlyTest(JavaExecTest):
    MODE = TestMode.compile_firm_only

TestCase.TEST_CASE_CLASSES[TestMode.compile_firm_only].append(JavaCompileFirmOnlyTest)

248 249
class JavaInfiniteLoopTest(BasicSyntaxTest):

250 251
    FILE_ENDINGS = [".inf.java", ".inf.mj"]
    OUTPUT_FILE_ENDING = ".out"
252 253
    MODE = TestMode.compile_firm

254 255
    def __init__(self, env: Environment, type: str, file: str, preprocessed_file: str, log_file_mode: str):
        super().__init__(env, type, file, preprocessed_file, log_file_mode)
256 257
        self._output_file = self.file + self.OUTPUT_FILE_ENDING
        self._has_output_file = path.exists(self._output_file)
258 259 260 261 262 263 264 265

    def run(self) -> BasicTestResult:
        base_filename = path.basename(self.file).split(".")[0]
        tmp_dir = self.env.create_pid_local_tmpdir()
        shutil.copy(self.preprocessed_file, path.join(tmp_dir, base_filename + ".java"))
        cwd = os.getcwd()
        os.chdir(tmp_dir)
        timeout = 1
266 267 268 269
        err = None
        out = None
        rtcode = None
        test_result = ExtensibleTestResult(self)
270
        try:
271 272 273
            out, err, rtcode = self.env.run_mj_command(self.MODE, base_filename + ".java")
            if rtcode != 0:
                test_result.incorrect_msg = "file can't be compiled"
274
                test_result.set_error_code(rtcode)
275 276
                test_result.add_long_text("Error output", decode(err))
                test_result.add_long_text("Output", decode(out))
277 278 279
                test_result.add_file("Source file", self.preprocessed_file)
                os.chdir(cwd)
                return test_result
Johannes Bechberger's avatar
Johannes Bechberger committed
280
            out, err, rtcode = self.env.run_command("./a.out", timeout=timeout)
281 282 283
            if rtcode != 0:
                test_result.incorrect_msg = "binary can't be run, non zero error code"
                test_result.set_error_code(rtcode)
284 285
                test_result.add_long_text("Error output", decode(err))
                test_result.add_long_text("Output", decode(out))
286 287 288
                test_result.add_file("Source file", self.preprocessed_file)
                os.chdir(cwd)
                return test_result
289 290
        except SigKill as sig:
            if sig.retcode == signal.SIGXCPU:
291
                out, _, _, _ = self.env.run_command_till_timeout("./a.out", timeout=1)
292
                test_result.add_long_text("Output", decode(out))
293
                if self._has_output_file:
294
                    out = decode(out).strip()
295
                    exp_out = ""
296
                    with open(self._output_file, "r", errors="backslashreplace") as f:
297
                        exp_out = f.read().strip()
298
                    test_result.add_long_text("Expected output start", exp_out)
299
                    if not out.startswith(exp_out):
300 301 302 303 304 305 306 307 308
                        test_result.incorrect_msg = "incorrect output start"
                        test_result.has_succeeded = False
                    test_result.add_file("Source file", self.preprocessed_file)
                    os.chdir(cwd)
                    return test_result
                else:
                    test_result.add_file("Source file", self.preprocessed_file)
                    os.chdir(cwd)
                    return test_result
309
            else:
310
                test_result.incorrect_msg = "binary can't be run: " + sig.name
311
                test_result.set_error_code(sig.retcode)
312 313 314
                test_result.add_file("Source file", self.preprocessed_file)
                os.chdir(cwd)
                return test_result
315
        except BaseException as e:
316
            os.chdir(cwd)
317
            print(e)
318 319
            raise
        os.chdir(cwd)
320 321 322 323 324 325
        test_result.incorrect_msg = "run shorter than one second"
        test_result.add_long_text("Output", out)
        test_result.has_succeeded = False
        test_result.add_file("Source file", self.preprocessed_file)
        os.chdir(cwd)
        return test_result
326 327


328
TestCase.TEST_CASE_CLASSES[TestMode.compile_firm].append(JavaInfiniteLoopTest)
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361


class InvalidJavaExecTest(BasicSyntaxTest):
    """
    The compiled binary should fail to execute properly
    """

    FILE_ENDINGS = [".invalid.java", ".invalid.mj"]
    MODE = TestMode.compile_firm

    def __init__(self, env: Environment, type: str, file: str, preprocessed_file: str, log_file_mode: str):
        super().__init__(env, type, file, preprocessed_file, log_file_mode)
        self._should_succeed = False

    def run(self) -> BasicDiffTestResult:
        is_big_testcase = "big" in self.file
        timeout = self.env.big_timeout if is_big_testcase else self.env.timeout
        base_filename = path.basename(self.file).split(".")[0]
        tmp_dir = self.env.create_pid_local_tmpdir()
        shutil.copy(self.preprocessed_file, path.join(tmp_dir, base_filename + ".java"))
        cwd = os.getcwd()
        os.chdir(tmp_dir)
        #print(base_filename, get_main_class_name(base_filename + ".java"))

        test_result = ExtensibleTestResult(self)
        test_result.require_error_string_in_error_case = False
        try:
            out, err, rtcode = None, None, None
            try:
                out, err, rtcode = self.env.run_mj_command(self.MODE, base_filename + ".java", timeout=-1)
                if rtcode != 0:
                    test_result.incorrect_msg = "file can't be compiled"
                    test_result.set_error_code(-rtcode)
362 363
                    test_result.add_long_text("Error output", decode(err))
                    test_result.add_long_text("Output", decode(out))
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
                    test_result.add_file("Source file", self.preprocessed_file)
                    os.chdir(cwd)
                    return test_result
            except SigKill as sig:
                test_result.incorrect_msg = "file can't be compiled: " + sig.name
                test_result.set_error_code(sig.retcode)
                test_result.add_file("Source file", self.preprocessed_file)
                os.chdir(cwd)
                return test_result
            except:
                os.chdir(cwd)
                raise
            try:
                out, err, rtcode = self.env.run_command("./a.out", timeout=timeout)
                if rtcode != 0:
                    test_result.incorrect_msg = "binary can't be run, non zero error code"
                    test_result.set_error_code(rtcode)
381 382
                    test_result.add_long_text("Error output", decode(err))
                    test_result.add_long_text("Output", decode(out))
383 384 385 386 387 388 389 390 391 392 393 394
                    test_result.add_file("Source file", self.preprocessed_file)
                    os.chdir(cwd)
                    return test_result
            except SigKill as sig:
                test_result.incorrect_msg = "binary can't be run: " + sig.name.strip()
                test_result.set_error_code(sig.retcode)
                test_result.add_file("Source file", self.preprocessed_file)
                os.chdir(cwd)
                return test_result
            except:
                os.chdir(cwd)
                raise
395
            test_result = BasicTestResult(self, rtcode, decode(out), decode(err))
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
            test_result.require_error_string_in_error_case = False
            return test_result
        except SigKill as sig:
            os.chdir(cwd)
            assert False
        except BaseException:
            os.chdir(cwd)
            raise


TestCase.TEST_CASE_CLASSES[TestMode.compile_firm].append(InvalidJavaExecTest)

class InvalidJavaCompileExecTest(InvalidJavaExecTest):
    MODE = TestMode.compile

TestCase.TEST_CASE_CLASSES[TestMode.compile].append(InvalidJavaExecTest)