Skip to content

Commit

Permalink
Allow to use the cstest integration tests with any cstest implementat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
Rot127 committed Aug 16, 2024
1 parent 9cb6912 commit f50a8d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion suite/cstest/src/test_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static bool parse_input_options(const TestInput *input, cs_arch *arch,
goto next_option;
}
}
fprintf(stderr, "[!] Option: %s not used\n", opt_str);
fprintf(stderr, "[!] Option: '%s' not used\n", opt_str);
next_option:
continue;
}
Expand Down
40 changes: 27 additions & 13 deletions suite/cstest/test/integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
# Copyright © 2024 Rot127 <unisono@quyllur.org>
# SPDX-License-Identifier: BSD-3

import sys
import subprocess as sp


def check(cmd: list[str], expected_stdout: str, expected_stderr: str, fail_msg: str):
print(f"Run: {' '.join(cmd)}")
result = sp.run(cmd, capture_output=True)
stderr = result.stderr.decode("utf8")
stdout = result.stdout.decode("utf8")
Expand All @@ -19,72 +21,84 @@ def check(cmd: list[str], expected_stdout: str, expected_stderr: str, fail_msg:
exit(1)
if expected_stdout and expected_stdout not in stdout:
print(f"STDOUT mismatch: '{expected_stdout}' not in stdout")
print("\n###################### STDout ######################\n")
print("\n###################### STDOUT ######################\n")
print(stdout)
print("####################################################\n")
print(fail_msg)
exit(1)


def run_tests():
def run_tests(cmd: str):
cmd = cmd.split(" ")
check(
["cstest", "empty_test_file.yaml"],
cmd + ["empty_test_file.yaml"],
expected_stderr="Failed to parse test file 'empty_test_file.yaml'",
expected_stdout="",
fail_msg="Failed the empty file test",
)

check(
["cstest", "missing_madatory_field.yaml"],
cmd + ["missing_madatory_field.yaml"],
expected_stderr="Error: 'Missing required mapping field'",
expected_stdout="",
fail_msg="Failed the mandatory field test",
)

check(
["cstest", "invalid_test_file.yaml"],
cmd + ["invalid_test_file.yaml"],
expected_stderr="Error: 'libyaml parser error'",
expected_stdout="",
fail_msg="Failed the invalid test file test",
)

check(
["cstest", "min_valid_test_file.yaml"],
cmd + ["min_valid_test_file.yaml"],
expected_stdout="All tests succeeded.",
expected_stderr="",
fail_msg="Failed the minimal valid parsing test",
)

check(
["cstest", "invalid_cs_input.yaml"],
cmd + ["invalid_cs_input.yaml"],
expected_stderr="'ar' is not mapped to a capstone architecture.",
expected_stdout="",
fail_msg="Test: Invalid CS option failed",
)

check(
["cstest", "invalid_cs_input.yaml"],
expected_stderr="[ ERROR ] --- 0 != 0x1",
cmd + ["invalid_cs_input.yaml"],
expected_stderr="0 != 0x1",
expected_stdout="",
fail_msg="Test: Wrong number of instruction disassembled failed",
)

check(
["cstest", "invalid_cs_input.yaml"],
expected_stderr="Option: thum not used",
cmd + ["invalid_cs_input.yaml"],
expected_stderr="Option: 'thum' not used",
expected_stdout="",
fail_msg="Test: Invalid disassembly due to wrong option failed",
)

check(
["cstest", "."],
cmd + ["."],
expected_stdout="Test files found: 6",
expected_stderr="",
fail_msg="Test: Detecting file in directory failed.",
)


def print_usage_exit():
print(f'{sys.argv[0]} "cstest_command"')
print('"cstest_command" examples:')
print('\t"python3 ../../bindings/python/cstest.py"')
print("\tcstest")
exit(1)


if __name__ == "__main__":
run_tests()
if len(sys.argv) != 2:
print_usage_exit()

run_tests(sys.argv[1])
print("All tests passed")
exit(0)

0 comments on commit f50a8d4

Please sign in to comment.