Skip to content

Commit

Permalink
Add a mandatory asm_text line for mnemonic + op_str check
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Jun 26, 2024
1 parent a82ddf3 commit da8629a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
7 changes: 5 additions & 2 deletions suite/cstest/include/test_case.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ static const cyaml_schema_field_t test_input_mapping_schema[] = {
/// Data compared to the produced cs_insn.
typedef struct {
uint32_t id;
char *op_str; // mandatory
char *asm_text; // mandatory
char *op_str;
bool is_alias;
uint64_t alias_id;
char *mnemonic;
Expand All @@ -69,7 +70,9 @@ TestInsnData *test_insn_data_clone(TestInsnData *test_insn_data);
static const cyaml_schema_field_t test_insn_data_mapping_schema[] = {
CYAML_FIELD_UINT("id", CYAML_FLAG_SCALAR_PLAIN | CYAML_FLAG_OPTIONAL,
TestInsnData, id),
CYAML_FIELD_STRING_PTR("op_str", CYAML_FLAG_POINTER, TestInsnData,
CYAML_FIELD_STRING_PTR("asm_text", CYAML_FLAG_POINTER, TestInsnData,
asm_text, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("op_str", CYAML_FLAG_POINTER_NULL_STR | CYAML_FLAG_OPTIONAL, TestInsnData,
op_str, 0, CYAML_UNLIMITED),
CYAML_FIELD_BOOL("is_alias", CYAML_FLAG_OPTIONAL, TestInsnData,
is_alias),
Expand Down
18 changes: 17 additions & 1 deletion suite/cstest/src/test_case.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void test_insn_data_free(TestInsnData *test_insn_data)
if (!test_insn_data) {
return;
}
cs_mem_free(test_insn_data->asm_text);
cs_mem_free(test_insn_data->op_str);
cs_mem_free(test_insn_data->mnemonic);
cs_mem_free(test_insn_data);
Expand All @@ -102,6 +103,8 @@ TestInsnData *test_insn_data_clone(TestInsnData *test_insn_data)
NULL;
tid->op_str = test_insn_data->op_str ? cs_strdup(test_insn_data->op_str) :
NULL;
tid->asm_text = test_insn_data->asm_text ? cs_strdup(test_insn_data->asm_text) :
NULL;
return tid;
}

Expand Down Expand Up @@ -145,7 +148,17 @@ void test_expected_compare(TestExpected *expected, cs_insn *insns,
for (size_t i = 0; i < insns_count; ++i) {
TestInsnData *expec_data = expected->insns[i];
// Test mandatory fields first
assert_string_equal(expec_data->op_str, insns[i].op_str);
// The asm text is saved differently for different architectures.
// Either all in op_str or split in mnemonic and op_str
char asm_text[256] = { 0 };
if (insns[i].mnemonic[0] != '\0') {
append_to_str(asm_text, sizeof(asm_text), insns[i].mnemonic);
append_to_str(asm_text, sizeof(asm_text), " ");
}
if (insns[i].op_str[0] != '\0') {
append_to_str(asm_text, sizeof(asm_text), insns[i].op_str);
}
assert_string_equal(expec_data->asm_text, asm_text);

// Not mandatory fields. If not initialized they should still match.
if (expec_data->id != 0) {
Expand All @@ -157,6 +170,9 @@ void test_expected_compare(TestExpected *expected, cs_insn *insns,
assert_string_equal(expec_data->mnemonic,
insns[i].mnemonic);
}
if (expec_data->op_str) {
assert_string_equal(expec_data->op_str, insns[i].op_str);
}
// TODO: details
}
}
Expand Down
8 changes: 4 additions & 4 deletions suite/cstest/test/min_valid_test_file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_cases:
expected:
insns:
-
op_str: "mov r11, r5"
asm_text: "mov r11, r5"
-
input:
bytes: [ 0x06, 0x10, 0xa0, 0xe1 ]
Expand All @@ -16,7 +16,7 @@ test_cases:
expected:
insns:
-
op_str: "mov r1, r6"
asm_text: "mov r1, r6"
-
input:
bytes: [ 0x06, 0x10, 0xa0, 0xe1, 0x05, 0xb0, 0xa0, 0xe1 ]
Expand All @@ -25,7 +25,7 @@ test_cases:
expected:
insns:
-
op_str: "mov r1, r6"
asm_text: "mov r1, r6"
-
op_str: "mov r11, r5"
asm_text: "mov r11, r5"

0 comments on commit da8629a

Please sign in to comment.