Initial Commit

This commit is contained in:
Sajid
2024-09-07 18:00:09 +06:00
commit 0f9a53f75a
3352 changed files with 1563708 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.15)
set(CSTEST_TEST_SRC_DIR ${CSTEST_TEST_DIR}/src)
set(CSTEST_TEST_INC_DIR ${CSTEST_TEST_DIR}/include)
include_directories(${CSTEST_TEST_INC_DIR}
${CSTEST_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include)
file(GLOB CSTEST_TEST_SRC ${CSTEST_TEST_SRC_DIR}/*.c)
add_executable(unit_test ${CSTEST_TEST_SRC})
add_dependencies(unit_test libcstest)
target_link_libraries(unit_test PUBLIC libcstest)
add_test(NAME UnitCSTest
COMMAND unit_test
WORKING_DIRECTORY ${CSTEST_TEST_DIR}
)
add_test(NAME IntegrationCSTest
COMMAND python3 ${CSTEST_TEST_DIR}/integration_tests.py cstest
WORKING_DIRECTORY ${CSTEST_TEST_DIR}
)

View File

@@ -0,0 +1 @@
Integration tests of cstest

View File

@@ -0,0 +1,134 @@
#!/usr/bin/env python3
# Copyright © 2024 Rot127 <unisono@quyllur.org>
# SPDX-License-Identifier: BSD-3
# Typing for Python3.8
from __future__ import annotations
import sys
import subprocess as sp
from pathlib import Path
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")
if expected_stderr and expected_stderr not in stderr:
print(f"STDERR mismatch: '{expected_stderr}' not in stderr")
print("\n###################### STDERR ######################\n")
print(stderr)
print("####################################################\n")
print(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(stdout)
print("####################################################\n")
print(fail_msg)
exit(1)
def run_tests(cmd: str):
p = (
sp.run(["git", "rev-parse", "--show-toplevel"], check=True, capture_output=True)
.stdout.decode("utf8")
.strip()
)
path = Path(p).joinpath("suite").joinpath("cstest").joinpath("test")
cmd = cmd.split(" ")
check(
cmd + [f"{path.joinpath('empty_test_file.yaml')}"],
expected_stderr="Failed to parse test file ",
expected_stdout="",
fail_msg="Failed the empty file test",
)
check(
cmd + [f"{path.joinpath('missing_madatory_field.yaml')}"],
expected_stderr="Error: 'Missing required mapping field'",
expected_stdout="",
fail_msg="Failed the mandatory field test",
)
check(
cmd + [f"{path.joinpath('invalid_test_file.yaml')}"],
expected_stderr="Error: 'libyaml parser error'",
expected_stdout="",
fail_msg="Failed the invalid test file test",
)
check(
cmd + [f"{path.joinpath('min_valid_test_file.yaml')}"],
expected_stdout="All tests succeeded.",
expected_stderr="",
fail_msg="Failed the minimal valid parsing test",
)
check(
cmd + [f"{path.joinpath('invalid_cs_input.yaml')}"],
expected_stderr="'ar' is not mapped to a capstone architecture.",
expected_stdout="",
fail_msg="Test: Invalid CS option failed",
)
check(
cmd + [f"{path.joinpath('invalid_cs_input.yaml')}"],
expected_stderr="0 != 0x1",
expected_stdout="",
fail_msg="Test: Wrong number of instruction disassembled failed",
)
check(
cmd + [f"{path.joinpath('invalid_cs_input.yaml')}"],
expected_stderr="Option: 'thum' not used",
expected_stdout="",
fail_msg="Test: Invalid disassembly due to wrong option failed",
)
check(
cmd + [f"{path}"],
expected_stdout="Test files found: 6",
expected_stderr="",
fail_msg="Test: Detecting file in directory failed.",
)
if "cstest_py" in cmd:
check(
cmd
+ [
f"{path}",
"-e",
"invalid_cs_input.yaml",
"-i",
"invalid_cs_input.yaml",
"min_valid_test_file.yaml",
"-v",
"debug",
],
expected_stdout="Test files found: 2",
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__":
if len(sys.argv) != 2:
print_usage_exit()
run_tests(sys.argv[1])
print("All tests passed")
exit(0)

View File

@@ -0,0 +1,29 @@
test_cases:
-
input:
bytes: [ 0x05, 0xb0, 0xa0, 0xe1 ]
arch: "ar" # Wrong arch
options: ["arm"]
expected:
insns:
-
asm_text: "mov r11, r5"
-
input:
bytes: [ 0x06 ] # Wrong number of bytes.
arch: "aarch64"
options: []
expected:
insns:
-
asm_text: "mov r1, r6"
-
input:
bytes: [ 0xc2, 0xf3, 0x00, 0x8f ]
arch: "arm"
options: ["thum"] # Wrong mode
expected:
insns:
-
asm_text: "bxj r2"

View File

@@ -0,0 +1,11 @@
test_cases:
-
input:
bytes: [ 0x05, 0xb0, 0xa0, 0xe1 ]
arch: "arm"
options: ["arm"]
expected:
insns:
-
op_str: "mov r11, r5"
# Invisble tab

View File

@@ -0,0 +1,31 @@
test_cases:
-
input:
bytes: [ 0x05, 0xb0, 0xa0, 0xe1 ]
arch: "arm"
options: ["arm"]
expected:
insns:
-
asm_text: "mov r11, r5"
-
input:
bytes: [ 0x06, 0x10, 0xa0, 0xe1 ]
arch: "arm"
options: ["arm"]
expected:
insns:
-
asm_text: "mov r1, r6"
-
input:
bytes: [ 0x06, 0x10, 0xa0, 0xe1, 0x05, 0xb0, 0xa0, 0xe1 ]
arch: "arm"
options: ["arm"]
expected:
insns:
-
asm_text: "mov r1, r6"
-
asm_text: "mov r11, r5"

View File

@@ -0,0 +1,8 @@
test_cases:
-
input:
arch: "arm"
expected:
insns:
-
op_str: "mov r11, r5"

View File

@@ -0,0 +1,31 @@
test_cases:
-
input:
bytes: [ 0x05, 0xb0, 0xa0, 0xe1 ]
arch: "arm"
options: ["arm"]
expected:
insns:
-
asm_text: "mov r11, r5"
-
input:
bytes: [ 0x06, 0x10, 0xa0, 0xe1 ]
arch: "arm"
options: ["arm"]
expected:
insns:
-
asm_text: "mov r1, r6"
-
input:
bytes: [ 0x06, 0x10, 0xa0, 0xe1, 0x05, 0xb0, 0xa0, 0xe1 ]
arch: "arm"
options: ["arm"]
expected:
insns:
-
asm_text: "mov r1, r6"
-
asm_text: "mov r11, r5"

View File

@@ -0,0 +1,74 @@
// Copyright © 2024 Rot127 <unisono@quyllur.org>
// SPDX-License-Identifier: BSD-3
#include "../../../utils.h"
#include "../../../Mapping.h"
#include "test_mapping.h"
#include <stdint.h>
bool test_cs_enum_get_val()
{
bool found = false;
// Get first value
uint32_t val = enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map),
"AAAAAAAAAAAAAAAAAAAAAAAAAA",
&found);
if (!found || val != 0xffffff) {
fprintf(stderr,
"enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), AAAAAAAAAAAAAAAAAAAAAAAAAA) failed is %d.\n",
val);
return false;
}
// Get last value
val = enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map),
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", &found);
if (!found || val != 0xffffff) {
fprintf(stderr,
"enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz) failed is %d.\n",
val);
return false;
}
// Some values
val = enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map),
"AArch64CC_EQ", &found);
if (!found || val != AArch64CC_EQ) {
fprintf(stderr,
"enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), AArch64CC_EQ) failed is %d.\n",
val);
return false;
}
val = enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map),
"AArch64CC_Invalid", &found);
if (!found || val != AArch64CC_Invalid) {
fprintf(stderr,
"enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), AArch64CC_In) failed is %d.\n",
val);
return false;
}
enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), "\0", &found);
if (found) {
fprintf(stderr, "Out of bounds failed.\n");
return false;
}
enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map),
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",
&found);
if (found) {
fprintf(stderr, "Out of bounds failed.\n");
return false;
}
return true;
}
int main()
{
bool success = true;
success &= test_cs_enum_get_val();
printf("test_cs_enum_get_val: %s\n", success ? "ok" : "fail");
return success ? 0 : 1;
}