|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import pytest |
| 5 | + |
| 6 | +# TODO: only run on Windows |
| 7 | + |
| 8 | +def _lookup_cppcheck_exe(exe_name): |
| 9 | + # path the script is located in |
| 10 | + script_path = os.path.dirname(os.path.realpath(__file__)) |
| 11 | + |
| 12 | + if sys.platform == "win32": |
| 13 | + exe_name += ".exe" |
| 14 | + |
| 15 | + for base in (script_path + '/../../', './'): |
| 16 | + for path in ('', 'bin/', 'bin/debug/'): |
| 17 | + exe_path = base + path + exe_name |
| 18 | + if os.path.isfile(exe_path): |
| 19 | + print("using '{}'".format(exe_path)) |
| 20 | + return exe_path |
| 21 | + |
| 22 | + return None |
| 23 | + |
| 24 | +def _call_process(arg): |
| 25 | + exe = _lookup_cppcheck_exe('test-sehwrapper') |
| 26 | + if exe is None: |
| 27 | + raise Exception('executable not found') |
| 28 | + p = subprocess.Popen([exe, arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 29 | + comm = p.communicate() |
| 30 | + stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') |
| 31 | + stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') |
| 32 | + return p.returncode, stdout, stderr |
| 33 | + |
| 34 | + |
| 35 | +def test_assert(): |
| 36 | + exitcode, stdout, stderr = _call_process('assert') |
| 37 | + assert stdout == '' |
| 38 | + # Assertion failed: false, file S:\GitHub\cppcheck-fw\test\seh\test-sehwrapper.cpp, line 33 |
| 39 | + assert stderr.startswith("Assertion failed: false, file "), stderr |
| 40 | + assert stderr.endswith("test-sehwrapper.cpp, line 33\n"), stderr |
| 41 | + assert exitcode == 3 |
| 42 | + |
| 43 | + |
| 44 | +def test_abort(): |
| 45 | + exitcode, stdout, stderr = _call_process('abort') |
| 46 | + # nothing is written in case of abort() |
| 47 | + # it will show the "Debug Error!" MessageBox though which we suppress in the test |
| 48 | + assert stdout == '' |
| 49 | + assert stderr == '' |
| 50 | + assert exitcode == 3 |
| 51 | + |
| 52 | + |
| 53 | +def test_segv(): |
| 54 | + exitcode, stdout, stderr = _call_process('segv') |
| 55 | + assert stderr == '' |
| 56 | + lines = stdout.splitlines() |
| 57 | + assert lines[0].startswith('Internal error: Access violation (instruction: 0x'), lines[0] |
| 58 | + assert lines[0].endswith(') reading from 0x0000000000000000'), lines[0] |
| 59 | + assert lines[1].startswith('0. 0x'), lines[1] |
| 60 | + assert lines[1].endswith(' in my_segv'), lines[1] |
| 61 | + assert lines[2].startswith('1. 0x'), lines[2] |
| 62 | + assert lines[2].endswith(' in main'), lines[2] |
| 63 | + assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!' |
| 64 | + assert exitcode == 4294967295 # returns -1 |
| 65 | + |
| 66 | + |
| 67 | +# TODO: make this work |
| 68 | +@pytest.mark.skip |
| 69 | +def test_fpe(): |
| 70 | + exitcode, stdout, stderr = _call_process('fpe') |
| 71 | + assert stderr == '' |
| 72 | + lines = stdout.splitlines() |
| 73 | + assert lines[0].startswith('Internal error: cppcheck received signal SIGFPE - FPE_FLTDIV (at 0x7f'), lines[0] |
| 74 | + assert lines[0].endswith(').'), lines[0] |
| 75 | + assert lines[1] == 'Callstack:' |
| 76 | + assert lines[2].endswith('my_fpe()'), lines[2] |
| 77 | + assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!' |
| 78 | + assert exitcode == 4294967295 # returns -1 |
0 commit comments