Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement automatic test directory for r2r #16365

Merged
merged 7 commits into from
Mar 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 89 additions & 8 deletions binr/r2r/r2r.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* radare - LGPL - Copyright 2020 - thestr4ng3r */

#include "r2r.h"

#include <r_cons.h>

#define WORKERS_DEFAULT 8
Expand Down Expand Up @@ -41,23 +40,87 @@ static int help(bool verbose) {
" -r [radare2] path to radare2 executable (default is "RADARE2_CMD_DEFAULT")\n"
" -m [rasm2] path to rasm2 executable (default is "RASM2_CMD_DEFAULT")\n"
" -f [file] file to use for json tests (default is "JSON_TEST_FILE_DEFAULT")\n"
" -C [dir] chdir before running r2r (default follows executable symlink + test/new\n"
"\n"
"OS/Arch for archos tests: "R2R_ARCH_OS"\n");
}
return 1;
}

static bool r2r_chdir(const char *argv0) {
#if __UNIX__
if (r_file_is_directory ("db")) {
return true;
}
char src_path[PATH_MAX];
char *r2r_path = r_file_path (argv0);
bool found = false;
if (readlink (r2r_path, src_path, sizeof (src_path)) != -1) {
char *p = strstr (src_path, R_SYS_DIR "binr"R_SYS_DIR"r2r"R_SYS_DIR"r2r");
if (p) {
*p = 0;
strcat (src_path, R_SYS_DIR"test"R_SYS_DIR"new");
if (r_file_is_directory (src_path)) {
(void)chdir (src_path);
eprintf ("Running from %s\n", src_path);
thestr4ng3r marked this conversation as resolved.
Show resolved Hide resolved
found = true;
}
}
}
free (r2r_path);
return found;
#else
return false;
#endif
}

static bool r2r_chdir_fromtest(const char *test_path) {
char *abs_test_path = r_file_abspath (test_path);
if (!r_file_is_directory (abs_test_path)) {
char *last_slash = (char *)r_str_lchr (abs_test_path, R_SYS_DIR[0]);
if (last_slash) {
*last_slash = 0;
}
}
if (chdir (abs_test_path) == -1) {
return false;
}
bool found = false;
char *cwd = NULL;
char *old_cwd = NULL;
while (true) {
cwd = r_sys_getdir ();
if (old_cwd && !strcmp (old_cwd, cwd)) {
break;
}
if (r_file_is_directory ("db")) {
found = true;
eprintf ("Running from %s\n", cwd);
break;
}
free (old_cwd);
old_cwd = cwd;
cwd = NULL;
if (chdir ("..") == -1) {
break;
}
}
free (old_cwd);
free (cwd);
return found;
}

int main(int argc, char **argv) {
int workers_count = WORKERS_DEFAULT;
bool verbose = false;
char *radare2_cmd = NULL;
char *rasm2_cmd = NULL;
char *json_test_file = NULL;

const char *r2r_dir = NULL;
int ret = 0;

RGetopt opt;
r_getopt_init (&opt, argc, (const char **)argv, "hvj:r:m:f:");
r_getopt_init (&opt, argc, (const char **)argv, "hvj:r:m:f:C:");
int c;
while ((c = r_getopt_next (&opt)) != -1) {
switch (c) {
Expand All @@ -67,7 +130,7 @@ int main(int argc, char **argv) {
case 'v':
verbose = true;
break;
case 'j': {
case 'j':
workers_count = atoi (opt.arg);
if (workers_count <= 0) {
eprintf ("Invalid thread count\n");
Expand All @@ -79,6 +142,9 @@ int main(int argc, char **argv) {
free (radare2_cmd);
radare2_cmd = strdup (opt.arg);
break;
case 'C':
r2r_dir = opt.arg;
break;
case 'm':
free (rasm2_cmd);
rasm2_cmd = strdup (opt.arg);
Expand All @@ -87,13 +153,25 @@ int main(int argc, char **argv) {
free (json_test_file);
json_test_file = strdup (opt.arg);
break;
}
default:
ret = help (false);
goto beach;
}
}

char *cwd = r_sys_getdir ();
if (r2r_dir) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to fix all paths given as args because if they are relative, they will be broken after cd'ing, making them absolute will probably be enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then we need to chdir multiple times and chdir-back to the original one after each path assuming the user can pass multiple test files from different working directories

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one run-path for all given paths is enough. But as it is right now, you will be unable to load the tests at all.

chdir (r2r_dir);
} else {
bool dir_found = (opt.ind < argc)
? r2r_chdir_fromtest (argv[opt.ind])
: r2r_chdir (argv[0]);
if (!dir_found) {
eprintf ("Cannot find db/ directory related to the given test.\n");
return -1;
}
}

if (!r2r_subprocess_init ()) {
eprintf ("Subprocess init failed\n");
return -1;
Expand Down Expand Up @@ -125,11 +203,14 @@ int main(int argc, char **argv) {
// Manually specified path(s)
int i;
for (i = opt.ind; i < argc; i++) {
if (!r2r_test_database_load (state.db, argv[i])) {
eprintf ("Failed to load tests from \"%s\"\n", argv[i]);
char *tf = (argv[i][0] == '/')? strdup (argv[i]): r_str_newf ("%s"R_SYS_DIR"%s", cwd, argv[i]);
if (!r2r_test_database_load (state.db, tf)) {
eprintf ("Failed to load tests from \"%s\"\n", tf);
r2r_test_database_free (state.db);
free (tf);
return -1;
}
free (tf);
}
} else {
// Default db path
Expand All @@ -139,7 +220,7 @@ int main(int argc, char **argv) {
return -1;
}
}

R_FREE (cwd);
r_pvector_insert_range (&state.queue, 0, state.db->tests.v.a, r_pvector_len (&state.db->tests));

bool jq_available = r2r_check_jq_available ();
Expand Down