Add more features to CSTEST (#1376)

* fix crash

* MC: alias registers for Mips

* MC: alias registers for Mips - v1

* MC: alias registers for Mips

* MC: alias registers for Mips

* MC: more fix for Mips .cs

* MC: more fix for Mips .cs

* add comment feature

* free mem

* Done fixing

* Done

* rename report

* fix README
remotes/origin/master
Đỗ Minh Tuấn 6 years ago committed by Nguyen Anh Quynh
parent 3a5a27f3cf
commit e25a8a4cbf
  1. 8
      suite/MC/X86/x86_64-imm-widths.s.cs
  2. 8
      suite/cstest/README.md
  3. 30
      suite/cstest/cstest_report.py
  4. 2
      suite/cstest/include/capstone_test.h
  5. 12
      suite/cstest/issues.cs
  6. 115
      suite/cstest/src/capstone_test.c
  7. 2
      suite/cstest/src/helper.c
  8. 177
      suite/cstest/src/main.c

@ -5,14 +5,14 @@
0x04,0xff = addb $0xFF, %al
0x66,0x83,0xc0,0x00 = addw $0x0000, %ax
0x66,0x83,0xc0,0x7f = addw $0x007F, %ax
0x66,0x83,0xc0,0x80 = addw $0x80, %ax
0x66,0x83,0xc0,0xff = addw $0xFFFF, %ax
// 0x66,0x83,0xc0,0x80 = addw $0x80, %ax
// 0x66,0x83,0xc0,0xff = addw $0xFFFF, %ax
0x83,0xc0,0x00 = addl $0x00000000, %eax
0x83,0xc0,0x7f = addl $0x0000007F, %eax
0x05,0x80,0xff,0x00,0x00 = addl $0xFF80, %eax
0x05,0xff,0xff,0x00,0x00 = addl $0xFFFF, %eax
0x83,0xc0,0x80 = addl $0xFFFFFF80, %eax
0x83,0xc0,0xff = addl $0xFFFFFFFF, %eax
// 0x83,0xc0,0x80 = addl $0xFFFFFF80, %eax
// 0x83,0xc0,0xff = addl $0xFFFFFFFF, %eax
0x48,0x83,0xc0,0x00 = addq $0x0000000000000000, %rax
0x48,0x83,0xc0,0x7f = addq $0x000000000000007F, %rax
0x48,0x83,0xc0,0x80 = addq $0xFFFFFFFFFFFFFF80, %rax

@ -33,6 +33,9 @@ make
## Usage
- Usage: `cstest [-e] [-f <file_name.cs>] [-d <directory>]`
- `-e` : test all commented test
- Test for all closed issues
```
@ -63,8 +66,9 @@ make cstest
## Using report tool
- Usage `python report.py -t <cstest_path> [-f <file_name.cs>] [-d <directory>]`
- Usage `python report.py [-Dc] -t <cstest_path> [-f <file_name.cs>] [-d <directory>]`
- `-D` : print details
- `-c` : auto comment out failed test
- Example:
```

@ -12,7 +12,7 @@ def Usage(s):
print 'Usage: {} -t <cstest_path> [-f <file_name.cs>] [-d <directory>]'.format(s)
sys.exit(-1)
def get_report_file(toolpath, filepath, getDetails):
def get_report_file(toolpath, filepath, getDetails, cmt_out):
cmd = [toolpath, '-f', filepath]
process = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
@ -46,33 +46,51 @@ def get_report_file(toolpath, filepath, getDetails):
print '[-] Detailed report for {}:\n'.format(filepath)
for c, f, d in details:
print '\t[+] {}: {}\n\t\t{}\n'.format(f, c, d)
if len(f) > 0 and cmt_out is True:
tmp_cmd = ['sed', '-E', '-i.bak', 's/({})(.*)/\/\/ \\1\\2/g'.format(c), filepath]
sed_proc = Popen(tmp_cmd, stdout=PIPE, stderr=PIPE)
sed_proc.communicate()
tmp_cmd2 = ['rm', '-f', filepath + '.bak']
rm_proc = Popen(tmp_cmd2, stdout=PIPE, stderr=PIPE)
rm_proc.communicate()
print '\n'
return 0
elif len(details) > 0:
return 0;
return 1
def get_report_folder(toolpath, folderpath, details):
def get_report_folder(toolpath, folderpath, details, cmt_out):
result = 1
for root, dirs, files in os.walk(folderpath):
path = root.split(os.sep)
for f in files:
if f.split('.')[-1] == 'cs':
print '[-] Target:', f,
get_report_file(toolpath, os.sep.join(x for x in path) + os.sep + f, details)
result *= get_report_file(toolpath, os.sep.join(x for x in path) + os.sep + f, details, cmt_out)
sys.exit(result ^ 1)
if __name__ == '__main__':
Done = False
details = False
toolpath = ''
cmt_out = False
try:
opts, args = getopt.getopt(sys.argv[1:], "t:f:d:D")
opts, args = getopt.getopt(sys.argv[1:], "ct:f:d:D")
for opt, arg in opts:
if opt == '-f':
get_report_file(toolpath, arg, details)
get_report_file(toolpath, arg, details, cmt_out)
Done = True
elif opt == '-d':
get_report_folder(toolpath, arg, details)
get_report_folder(toolpath, arg, details, cmt_out)
Done = True
elif opt == '-t':
toolpath = arg
elif opt == '-D':
details = True
elif opt == '-c':
cmt_out = True
except getopt.GetoptError:
Usage(sys.argv[0])

@ -32,7 +32,7 @@
} while (0)
#define NUMARCH 9
#define NUMMODE 24
#define NUMMODE 33
#define NUMOPTION 40
#define MAXMEM 1024

@ -1,10 +1,10 @@
//!# issue 0
//!#CS_ARCH_X86, CS_MODE_64, CS_OPT_UNSIGNED
//0x66,0x83,0xc0,0x80 == add ax, 0xff80
// !# issue 0
// !#CS_ARCH_X86, CS_MODE_64, CS_OPT_UNSIGNED
// 0x66,0x83,0xc0,0x80 == add ax, 0xff80
//!# issue 0
//!#CS_ARCH_X86, CS_MODE_64, CS_OPT_SYNTAX_ATT | CS_OPT_UNSIGNED
//0x66,0x83,0xc0,0x80 == addw $0xff80, %ax
// !# issue 0
// !#CS_ARCH_X86, CS_MODE_64, CS_OPT_SYNTAX_ATT | CS_OPT_UNSIGNED
// 0x66,0x83,0xc0,0x80 == addw $0xff80, %ax
!# issue 1323
!#CS_ARCH_ARM, CS_MODE_THUMB, CS_OPT_DETAIL

@ -12,6 +12,7 @@ single_dict arches[] = {
{"CS_ARCH_M68K", CS_ARCH_M68K}
};
/*
single_dict modes[] = {
{"CS_MODE_16", CS_MODE_16},
{"CS_MODE_32", CS_MODE_32},
@ -38,6 +39,43 @@ single_dict modes[] = {
{"CS_MODE_MIPS64+CS_MODE_BIG_ENDIAN", CS_MODE_MIPS64+CS_MODE_BIG_ENDIAN},
{"CS_MODE_THUMB | CS_MODE_BIG_ENDIAN", CS_MODE_THUMB | CS_MODE_BIG_ENDIAN}
};
*/
single_dict modes[] = {
{"CS_MODE_LITTLE_ENDIAN", CS_MODE_LITTLE_ENDIAN},
{"CS_MODE_ARM", CS_MODE_ARM},
{"CS_MODE_16", CS_MODE_16},
{"CS_MODE_32", CS_MODE_32},
{"CS_MODE_64", CS_MODE_64},
{"CS_MODE_THUMB", CS_MODE_THUMB},
{"CS_MODE_MCLASS", CS_MODE_MCLASS},
{"CS_MODE_V8", CS_MODE_V8},
{"CS_MODE_MICRO", CS_MODE_MICRO},
{"CS_MODE_MIPS3", CS_MODE_MIPS3},
{"CS_MODE_MIPS32R6", CS_MODE_MIPS32R6},
{"CS_MODE_MIPS2", CS_MODE_MIPS2},
{"CS_MODE_V9", CS_MODE_V9},
{"CS_MODE_QPX", CS_MODE_QPX},
{"CS_MODE_M68K_000", CS_MODE_M68K_000},
{"CS_MODE_M68K_010", CS_MODE_M68K_010},
{"CS_MODE_M68K_020", CS_MODE_M68K_020},
{"CS_MODE_M68K_030", CS_MODE_M68K_030},
{"CS_MODE_M68K_040", CS_MODE_M68K_040},
{"CS_MODE_M68K_060", CS_MODE_M68K_060},
{"CS_MODE_BIG_ENDIAN", CS_MODE_BIG_ENDIAN},
{"CS_MODE_MIPS32", CS_MODE_MIPS32},
{"CS_MODE_MIPS64", CS_MODE_MIPS64},
{"CS_MODE_M680X_6301", CS_MODE_M680X_6301},
{"CS_MODE_M680X_6309", CS_MODE_M680X_6309},
{"CS_MODE_M680X_6800", CS_MODE_M680X_6800},
{"CS_MODE_M680X_6801", CS_MODE_M680X_6801},
{"CS_MODE_M680X_6805", CS_MODE_M680X_6805},
{"CS_MODE_M680X_6808", CS_MODE_M680X_6808},
{"CS_MODE_M680X_6809", CS_MODE_M680X_6809},
{"CS_MODE_M680X_6811", CS_MODE_M680X_6811},
{"CS_MODE_M680X_CPU12", CS_MODE_M680X_CPU12},
{"CS_MODE_M680X_HCS08", CS_MODE_M680X_HCS08}
};
double_dict options[] = {
{"CS_OPT_DETAIL", CS_OPT_DETAIL, CS_OPT_ON},
@ -79,7 +117,8 @@ double_dict options[] = {
{"CS_MODE_M680X_6809", CS_OPT_MODE, CS_MODE_M680X_6809},
{"CS_MODE_M680X_6811", CS_OPT_MODE, CS_MODE_M680X_6811},
{"CS_MODE_M680X_CPU12", CS_OPT_MODE, CS_MODE_M680X_CPU12},
{"CS_MODE_M680X_HCS08", CS_OPT_MODE, CS_MODE_M680X_HCS08}
{"CS_MODE_M680X_HCS08", CS_OPT_MODE, CS_MODE_M680X_HCS08},
{"CS_OPT_UNSIGNED", CS_OPT_ON, CS_OPT_UNSIGNED}
};
char *(*function)(csh *, cs_mode, cs_insn*) = NULL;
@ -106,7 +145,7 @@ void test_single_MC(csh *handle, int mc_mode, char *line)
int i, count, count_noreg;
unsigned char *code;
cs_insn *insn;
char tmp[MAXMEM], cs_hex[MAXMEM], mc_hex[MAXMEM], mc_dec[MAXMEM];
char tmp[MAXMEM], cs_hex[MAXMEM], mc_hex[MAXMEM], mc_dec[MAXMEM], tmp_mc[MAXMEM];
char tmp_noreg[MAXMEM], cs_hex_noreg[MAXMEM], mc_hex_noreg[MAXMEM], mc_dec_noreg[MAXMEM];
char **offset_opcode;
int size_offset_opcode;
@ -126,34 +165,34 @@ void test_single_MC(csh *handle, int mc_mode, char *line)
code = (unsigned char *)malloc(size_byte * sizeof(char));
for (i=0; i<size_byte; ++i) {
code[i] = (unsigned char)strtol(list_byte[i], NULL, 16);
// printf("Byte: 0x%.2x\n", (int)code[i]);
// printf("Byte: 0x%.2x\n", (int)code[i]);
}
cs_option(*handle, CS_OPT_UNSIGNED, CS_OPT_ON);
// list_data = split(list_part[1], ";", &size_data);
count = cs_disasm(*handle, code, size_byte, offset, 0, &insn);
if (count == 0) {
fprintf(stderr, "[ ERROR ] --- %s --- Failed to disassemble given code!\n", list_part[0]);
free(list_part);
free(offset_opcode);
free(list_byte);
free_strs(list_part, size_part);
free_strs(offset_opcode, size_offset_opcode);
free_strs(list_byte, size_byte);
free(code);
// free(list_data);
// free(list_data);
_fail(__FILE__, __LINE__);
}
if (count > 1) {
fprintf(stderr, "[ ERROR ] --- %s --- Multiple instructions(%d) disassembling doesn't support!\n", list_part[0], count);
free(list_part);
free(offset_opcode);
free(list_byte);
free_strs(list_part, size_part);
free_strs(offset_opcode, size_offset_opcode);
free_strs(list_byte, size_byte);
free(code);
// free(list_data);
// free(list_data);
_fail(__FILE__, __LINE__);
}
for (p=list_part[1]; *p; ++p) *p = tolower(*p);
trim_str(list_part[1]);
// replace_negative(list_part[1], mc_mode);
strcpy(tmp_mc, list_part[1]);
replace_negative(tmp_mc, mc_mode);
// tmp = (char *)malloc(strlen(insn[0].mnemonic) + strlen(insn[0].op_str) + 100);
strcpy(tmp, insn[0].mnemonic);
@ -166,10 +205,11 @@ void test_single_MC(csh *handle, int mc_mode, char *line)
// cs_hex = strdup(tmp);
strcpy(cs_hex, tmp);
replace_hex(tmp);
// replace_negative(tmp, mc_mode);
// mc_hex = strdup(list_data[0]);
// mc_dec = strdup(list_data[0]);
strcpy(mc_hex, list_part[1]);
strcpy(mc_dec, list_part[1]);
strcpy(mc_hex, tmp_mc);
strcpy(mc_dec, tmp_mc);
replace_hex(mc_dec);
if (cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME) == CS_ERR_OK) {
@ -186,16 +226,17 @@ void test_single_MC(csh *handle, int mc_mode, char *line)
strcpy(cs_hex_noreg, tmp_noreg);
replace_hex(tmp_noreg);
// mc_dec_noreg = strdup(list_data[0]);
strcpy(mc_hex_noreg, list_part[1]);
strcpy(mc_dec_noreg, list_part[1]);
strcpy(mc_hex_noreg, tmp_mc);
strcpy(mc_dec_noreg, tmp_mc);
replace_hex(mc_dec_noreg);
// replace_negative(mc_dec_noreg, mc_mode);
if (strcmp(tmp, mc_hex) && strcmp(cs_hex, mc_hex) && strcmp(tmp, mc_dec) && strcmp(tmp, mc_hex)
&& strcmp(tmp_noreg, mc_hex_noreg) && strcmp(cs_hex_noreg, mc_hex_noreg) && strcmp(tmp_noreg, mc_dec_noreg) && strcmp(tmp_noreg, mc_hex_noreg)) {
fprintf(stderr, "[ ERROR ] --- %s --- \"%s\" != \"%s\"\n", list_part[0], cs_hex, list_part[1]);
free(list_part);
free(offset_opcode);
free(list_byte);
free_strs(list_part, size_part);
free_strs(offset_opcode, size_offset_opcode);
free_strs(list_byte, size_byte);
free(code);
// free(list_data);
cs_free(insn, count);
@ -204,18 +245,18 @@ void test_single_MC(csh *handle, int mc_mode, char *line)
cs_option(*handle, CS_OPT_SYNTAX, 0);
} else if (!quadruple_compare(tmp, cs_hex, mc_dec, mc_hex, list_part[0])) {
free(list_part);
free(offset_opcode);
free(list_byte);
free_strs(list_part, size_part);
free_strs(offset_opcode, size_offset_opcode);
free_strs(list_byte, size_byte);
free(code);
// free(list_data);
cs_free(insn, count);
_fail(__FILE__, __LINE__);
}
free(list_part);
free(offset_opcode);
free(list_byte);
free_strs(list_part, size_part);
free_strs(offset_opcode, size_offset_opcode);
free_strs(list_byte, size_byte);
free(code);
// free(list_data);
cs_free(insn, count);
@ -352,6 +393,12 @@ void test_single_issue(csh *handle, cs_mode mode, char *line, int detail)
if (size_part_cs_result != size_part_issue_result) {
fprintf(stderr, "[ ERROR ] --- %s --- Number of details( Capstone: %d --- Issue: %d ) doesn't match\n", list_part[0], size_part_cs_result, size_part_issue_result);
cs_free(insn, count);
free_strs(list_part, size_part);
free_strs(list_byte, size_byte);
free(cs_result);
free_strs(list_part_cs_result, size_part_cs_result);
free_strs(list_part_issue_result, size_part_issue_result);
_fail(__FILE__, __LINE__);
}
@ -362,20 +409,20 @@ void test_single_issue(csh *handle, cs_mode mode, char *line, int detail)
if (strcmp(list_part_cs_result[i], list_part_issue_result[i])) {
fprintf(stderr, "[ ERROR ] --- %s --- \"%s\" != \"%s\"\n", list_part[0], list_part_cs_result[i], list_part_issue_result[i]);
cs_free(insn, count);
free(list_part);
free(list_byte);
free_strs(list_part, size_part);
free_strs(list_byte, size_byte);
free(cs_result);
free(list_part_cs_result);
free(list_part_issue_result);
free_strs(list_part_cs_result, size_part_cs_result);
free_strs(list_part_issue_result, size_part_issue_result);
_fail(__FILE__, __LINE__);
}
}
cs_free(insn, count);
free(list_part);
free(list_byte);
free_strs(list_part, size_part);
free_strs(list_byte, size_byte);
free(cs_result);
free(list_part_cs_result);
free(list_part_issue_result);
free_strs(list_part_cs_result, size_part_cs_result);
free_strs(list_part_issue_result, size_part_issue_result);
}

@ -124,7 +124,7 @@ void replace_hex(char *src)
value = value*0x10 + (*found - 'a' + 10);
else
value = value*0x10 + (*found - '0');
// printf("====> %d -- %llu\n", *found, value);
// printf("====> %d -- %llu\n", *found, value);
found++;
}

@ -9,6 +9,7 @@ static int size_lines;
static cs_mode issue_mode;
static int getDetail;
static int mc_mode;
static int e_flag;
static int setup_MC(void **state)
{
@ -24,47 +25,85 @@ static int setup_MC(void **state)
}
tmp_counter = 0;
while (tmp_counter < size_lines && list_lines[tmp_counter][0] != '#') tmp_counter++; // get issue line
while (tmp_counter < size_lines && list_lines[tmp_counter][0] != '#') tmp_counter++;
list_params = split(list_lines[tmp_counter] + 2, ", ", &size_params);
if (size_params != 3) {
fprintf(stderr, "[ ERROR ] --- Invalid options ( arch, mode, option )\n");
failed_setup = 1;
return -1;
}
arch = get_value(arches, NUMARCH, list_params[0]);
mode = get_value(modes, NUMMODE, list_params[1]);
if (!strcmp(list_params[0], "CS_ARCH_ARM64")) mc_mode = 2;
else mc_mode = 1;
// mode = get_value(modes, NUMMODE, list_params[1]);
mode = 0;
for (i=0; i<NUMMODE; ++i) {
if (strstr(list_params[1], modes[i].str)) {
mode += modes[i].value;
switch (modes[i].value) {
case CS_MODE_16:
mc_mode = 0;
break;
case CS_MODE_64:
mc_mode = 2;
break;
case CS_MODE_THUMB:
mc_mode = 0;
break;
default:
break;
}
}
}
if (arch == -1 || mode == -1) {
fprintf(stderr, "[ ERROR ] --- Arch and/or Mode are not supported!\n");
if (arch == -1) {
fprintf(stderr, "[ ERROR ] --- Arch is not supported!\n");
failed_setup = 1;
return -1;
}
handle = (csh *)malloc(sizeof(csh));
cs_open(arch, mode, handle);
for (i=2; i < size_params; ++i)
if (strcmp(list_params[i], "None")) {
index = get_index(options, NUMOPTION, list_params[i]);
if (index == -1) {
fprintf(stderr, "[ ERROR ] --- Option is not supported!\n");
if(cs_open(arch, mode, handle) != CS_ERR_OK) {
fprintf(stderr, "[ ERROR ] --- Cannot initialize capstone\n");
failed_setup = 1;
return -1;
}
for (i=0; i<NUMOPTION; ++i) {
if (strstr(list_params[2], options[i].str)) {
if (cs_option(*handle, options[i].first_value, options[i].second_value) != CS_ERR_OK) {
fprintf(stderr, "[ ERROR ] --- Option is not supported for this arch/mode\n");
failed_setup = 1;
return -1;
}
cs_option(*handle, options[index].first_value, options[index].second_value);
}
}
*state = (void *)handle;
counter++;
while (counter < size_lines && list_lines[counter][0] != '0') counter++;
free_strs(list_params, size_params);
if (e_flag == 0)
while (counter < size_lines && strncmp(list_lines[counter], "0x", 2)) counter++;
else
while (counter < size_lines && strncmp(list_lines[counter], "// 0x", 5)) counter++;
free_strs(list_params, size_params);
return 0;
}
static void test_MC(void **state)
{
test_single_MC((csh *)*state, mc_mode, list_lines[counter]);
if (e_flag == 1)
test_single_MC((csh *)*state, mc_mode, list_lines[counter] + 3);
else
test_single_MC((csh *)*state, mc_mode, list_lines[counter]);
}
static int teardown_MC(void **state)
{
cs_close(*state);
cs_close(*state);
free(*state);
return 0;
}
@ -81,32 +120,72 @@ static int setup_issue(void **state)
getDetail = 0;
failed_setup = 0;
while (counter < size_lines && list_lines[counter][0] != '!') counter++; // get issue line
if (e_flag == 0)
while (counter < size_lines && strncmp(list_lines[counter], "!# ", 3)) counter++; // get issue line
else
while (counter < size_lines && strncmp(list_lines[counter], "// !#", 5)) counter++;
counter++;
while (counter < size_lines && list_lines[counter][0] != '!') counter++; // get arch/mode line
list_params = split(list_lines[counter] + 2, ", ", &size_params);
if (e_flag == 0)
while (counter < size_lines && strncmp(list_lines[counter], "!#", 2)) counter++; // get arch line
else
while (counter < size_lines && strncmp(list_lines[counter], "// !#", 5)) counter++;
if (e_flag == 0)
list_params = split(list_lines[counter] + 2, ", ", &size_params);
else
list_params = split(list_lines[counter] + 5, ", ", &size_params);
// print_strs(list_params, size_params);
arch = get_value(arches, NUMARCH, list_params[0]);
mode = get_value(modes, NUMMODE, list_params[1]);
if (arch == -1 || mode == -1) {
fprintf(stderr, "[ ERROR ] --- Arch and/or Mode are not supported!\n");
if (!strcmp(list_params[0], "CS_ARCH_ARM64")) mc_mode = 2;
else mc_mode = 1;
// mode = get_value(modes, NUMMODE, list_params[1]);
mode = 0;
for (i=0; i<NUMMODE; ++i) {
if (strstr(list_params[1], modes[i].str)) {
mode += modes[i].value;
switch (modes[i].value) {
case CS_MODE_16:
mc_mode = 0;
break;
case CS_MODE_64:
mc_mode = 2;
break;
case CS_MODE_THUMB:
mc_mode = 0;
break;
default:
break;
}
}
}
if (arch == -1) {
fprintf(stderr, "[ ERROR ] --- Arch is not supported!\n");
failed_setup = 1;
return -1;
}
handle = (csh *)calloc(1, sizeof(csh));
cs_open(arch, mode, handle);
for (i=2; i < size_params; ++i) {
if (strcmp(list_params[i], "None")) {
index = get_index(options, NUMOPTION, list_params[i]);
if (index == -1) {
fprintf(stderr, "[ ERROR ] --- Option is not supported!\n");
if(cs_open(arch, mode, handle) != CS_ERR_OK) {
fprintf(stderr, "[ ERROR ] --- Cannot initialize capstone\n");
failed_setup = 1;
return -1;
}
for (i=0; i<NUMOPTION; ++i) {
if (strstr(list_params[2], options[i].str)) {
if (cs_option(*handle, options[i].first_value, options[i].second_value) != CS_ERR_OK) {
fprintf(stderr, "[ ERROR ] --- Option is not supported for this arch/mode\n");
failed_setup = 1;
return -1;
}
if (index == 0) {
if (i == 0) {
result = set_function(arch);
if (result == -1) {
fprintf(stderr, "[ ERROR ] --- Cannot get details\n");
@ -115,14 +194,17 @@ static int setup_issue(void **state)
}
getDetail = 1;
}
cs_option(*handle, options[index].first_value, options[index].second_value);
}
}
*state = (void *)handle;
issue_mode = mode;
while (counter < size_lines && list_lines[counter][0] != '0') counter ++;
if (e_flag == 0)
while (counter < size_lines && strncmp(list_lines[counter], "0x", 2)) counter++;
else
while (counter < size_lines && strncmp(list_lines[counter], "// 0x", 5)) counter++;
free_strs(list_params, size_params);
return 0;
@ -130,14 +212,20 @@ static int setup_issue(void **state)
static void test_issue(void **state)
{
test_single_issue((csh *)*state, issue_mode, list_lines[counter], getDetail);
if (e_flag == 0)
test_single_issue((csh *)*state, issue_mode, list_lines[counter], getDetail);
else
test_single_issue((csh *)*state, issue_mode, list_lines[counter] + 3, getDetail);
// counter ++;
return;
}
static int teardown_issue(void **state)
{
while (counter < size_lines && list_lines[counter][0] != '!') counter++; // get next issue
if (e_flag == 0)
while (counter < size_lines && strncmp(list_lines[counter], "!# ", 3)) counter++;
else
while (counter < size_lines && strncmp(list_lines[counter], "// !#", 5)) counter++;
cs_close(*state);
free(*state);
function = NULL;
@ -164,13 +252,15 @@ static void test_file(const char *filename)
// tests = (struct CMUnitTest *)malloc(sizeof(struct CMUnitTest) * size_lines / 3);
tests = NULL;
for (i=0; i < size_lines; ++i) {
if (strstr(list_lines[i], "!# issue")) {
tmp = (char *)malloc(sizeof(char) * 100);
sscanf(list_lines[i], "!# issue %d\n", &issue_num);
sprintf(tmp, "Issue #%d", issue_num);
if ((!strncmp(list_lines[i], "// !# issue", 11) && e_flag == 1) ||
(!strncmp(list_lines[i], "!# issue", 8) && e_flag == 0)) {
// tmp = (char *)malloc(sizeof(char) * 100);
// sscanf(list_lines[i], "!# issue %d\n", &issue_num);
// sprintf(tmp, "Issue #%d", issue_num);
tests = (struct CMUnitTest *)realloc(tests, sizeof(struct CMUnitTest) * (number_of_tests + 1));
tests[number_of_tests] = (struct CMUnitTest)cmocka_unit_test_setup_teardown(test_issue, setup_issue, teardown_issue);
tests[number_of_tests].name = tmp;
// tests[number_of_tests].name = tmp;
tests[number_of_tests].name = strdup(list_lines[i]);
number_of_tests ++;
}
}
@ -183,7 +273,7 @@ static void test_file(const char *filename)
tests = NULL;
// tests = (struct CMUnitTest *)malloc(sizeof(struct CMUnitTest) * (size_lines - 1));
for (i = 1; i < size_lines; ++i) {
if (list_lines[i][0] == '0') {
if ((!strncmp(list_lines[i], "// 0x", 5) && e_flag == 1) || (!strncmp(list_lines[i], "0x", 2) && e_flag == 0)) {
tmp = (char *)malloc(sizeof(char) * 100);
sprintf(tmp, "Line %d", i+1);
tests = (struct CMUnitTest *)realloc(tests, sizeof(struct CMUnitTest) * (number_of_tests + 1));
@ -221,7 +311,9 @@ int main(int argc, char *argv[])
int opt, flag;
flag = 0;
while ((opt = getopt(argc, argv, "f:d:")) > 0) {
e_flag = 0;
while ((opt = getopt(argc, argv, "ef:d:")) > 0) {
switch (opt) {
case 'f':
test_file(optarg);
@ -231,14 +323,17 @@ int main(int argc, char *argv[])
test_folder(optarg);
flag = 1;
break;
case 'e':
e_flag = 1;
break;
default:
printf("Usage: %s [-f <file_name.cs>] [-d <directory>]\n", argv[0]);
printf("Usage: %s [-e] [-f <file_name.cs>] [-d <directory>]\n", argv[0]);
exit(-1);
}
}
if (flag == 0) {
printf("Usage: %s [-f <file_name.cs>] [-d <directory>]\n", argv[0]);
printf("Usage: %s [-e] [-f <file_name.cs>] [-d <directory>]\n", argv[0]);
exit(-1);
}

Loading…
Cancel
Save