Empty strings cant be considered valid json ##tests

This commit is contained in:
pancake 2024-06-12 13:32:41 +02:00 committed by pancake
parent 71603c1a57
commit 79283d8f4c
9 changed files with 111 additions and 66 deletions

View File

@ -1,7 +1,7 @@
BIN=r2r
BINDEPS=r_util r_cons
# OBJ=load.o run.o
CFLAGS+=-DALLINC=1
OBJ=load.o run.o
#CFLAGS+=-DALLINC=1
include ../rules.mk
include ../../libr/util/deps.mk

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2020-2023 - pancake, thestr4ng3r */
/* radare - LGPL - Copyright 2020-2024 - pancake, thestr4ng3r */
#undef R_LOG_ORIGIN
#define R_LOG_ORIGIN "r2r.load"
@ -89,9 +89,8 @@ static char *read_string_val(char **nextline, const char *val, ut64 *linenum) {
r_strbuf_append (buf, line);
if (end) {
return r_strbuf_drain (buf);
} else {
r_strbuf_append (buf, "\n");
}
r_strbuf_append (buf, "\n");
} while ((line = *nextline));
R_LOG_ERROR ("Missing closing end token %s", endtoken);
r_strbuf_free (buf);
@ -265,12 +264,11 @@ R_API R2RAsmTest *r2r_asm_test_new(void) {
}
R_API void r2r_asm_test_free(R2RAsmTest *test) {
if (!test) {
return;
if (test != NULL) {
free (test->disasm);
free (test->bytes);
free (test);
}
free (test->disasm);
free (test->bytes);
free (test);
}
static bool parse_asm_path(const char *path, RStrConstPool *strpool, const char **arch_out, const char **cpuout, int *bitsout) {

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2020-2023 - pancake, thestr4ng3r */
/* radare - LGPL - Copyright 2020-2024 - pancake, thestr4ng3r */
#include "r2r.h"
@ -368,24 +368,22 @@ R_API void r2r_subprocess_stdin_write(R2RSubprocess *proc, const ut8 *buf, size_
R_API R2RProcessOutput *r2r_subprocess_drain(R2RSubprocess *proc) {
R2RProcessOutput *out = R_NEW (R2RProcessOutput);
if (!out) {
return NULL;
if (R_LIKELY (out)) {
out->out = r_strbuf_drain_nofree (&proc->out);
out->err = r_strbuf_drain_nofree (&proc->err);
out->ret = proc->ret;
}
out->out = r_strbuf_drain_nofree (&proc->out);
out->err = r_strbuf_drain_nofree (&proc->err);
out->ret = proc->ret;
return out;
}
R_API void r2r_subprocess_free(R2RSubprocess *proc) {
if (!proc) {
return;
if (R_LIKELY (proc)) {
CloseHandle (proc->stdin_write);
CloseHandle (proc->stdout_read);
CloseHandle (proc->stderr_read);
CloseHandle (proc->proc);
free (proc);
}
CloseHandle (proc->stdin_write);
CloseHandle (proc->stdout_read);
CloseHandle (proc->stderr_read);
CloseHandle (proc->proc);
free (proc);
}
#else
@ -928,11 +926,9 @@ static R2RProcessOutput *run_r2_test(R2RRunConfig *config, ut64 timeout_ms, int
r_pvector_push (&envvals, "1");
}
if (extra_env)
{
RListIter * eit;
char * kv;
if (extra_env) {
RListIter *eit;
char *kv;
r_list_foreach (extra_env, eit, kv) {
char * equal = strstr (kv, "=");
if (!equal) {
@ -943,6 +939,14 @@ static R2RProcessOutput *run_r2_test(R2RRunConfig *config, ut64 timeout_ms, int
r_pvector_push (&envvals, equal + 1);
}
}
#if 0
void **at;
eprintf ("->{\n");
r_pvector_foreach (&args, at) {
eprintf ("--> %s\n", *at);
}
eprintf ("->}\n");
#endif
size_t env_size = r_pvector_length (&envvars);
@ -1075,16 +1079,38 @@ R_API R2RProcessOutput *r2r_run_json_test(R2RRunConfig *config, R2RJsonTest *tes
return ret;
}
R_API R2RProcessOutput *r2r_run_json_test_nofile(R2RRunConfig *config, R2RJsonTest *test, R2RCmdRunner runner, void *user) {
RList *files = r_list_new ();
r_list_push (files, "--");
// TODO: config->timeout_ms is already inside config, no need to pass it twice! chk other calls
R2RProcessOutput *ret = run_r2_test (config, config->timeout_ms, 1, test->cmd, files, NULL, NULL, test->load_plugins, runner, user);
r_list_free (files);
return ret;
}
static bool r2r_empty_json_check(R2RProcessOutput *out) {
char *s = r_str_trim_dup (out->out);
const bool is_not_empty = (R_STR_ISNOTEMPTY (s));
free (s);
return is_not_empty;
}
R_API bool r2r_check_json_test(R2RProcessOutput *out, R2RJsonTest *test) {
if (!out || out->ret != 0 || !out->out || !out->err || out->timeout) {
return false;
}
const char *args[] = { "." };
R2RSubprocess *proc = r2r_subprocess_start (JQ_CMD, args, 1, NULL, NULL, 0);
r2r_subprocess_stdin_write (proc, (const ut8 *)out->out, strlen (out->out));
r2r_subprocess_wait (proc, UT64_MAX);
bool ret = proc->ret == 0;
r2r_subprocess_free (proc);
bool ret = false;
if (r2r_empty_json_check (out)) {
R2RSubprocess *proc = r2r_subprocess_start (JQ_CMD, args, 1, NULL, NULL, 0);
r2r_subprocess_stdin_write (proc, (const ut8 *)out->out, strlen (out->out));
r2r_subprocess_wait (proc, UT64_MAX);
ret = proc->ret == 0;
r2r_subprocess_free (proc);
} else {
eprintf ("\n");
R_LOG_ERROR ("[XX] Empty json for %s", test->cmd);
}
return ret;
}
@ -1377,6 +1403,19 @@ R_API R2RTestResultInfo *r2r_run_test(R2RRunConfig *config, R2RTest *test) {
R2RJsonTest *json_test = test->json_test;
R2RProcessOutput *out = r2r_run_json_test (config, json_test, subprocess_runner, NULL);
success = r2r_check_json_test (out, json_test);
#if TEST_JSON_WITH_NO_FILES
// R2_590 - enable these tests
if (strchr (json_test->cmd, '@')) {
// ignore json tests with @ when running r2 with no files
} else {
// test output of commands when no file is provided
r2r_process_output_free (out);
out = r2r_run_json_test_nofile (config, json_test, subprocess_runner, NULL);
if (!r2r_check_json_test (out, json_test)) {
success = false;
}
}
#endif
ret->proc_out = out;
ret->timeout = out->timeout;
ret->run_failed = !out;

View File

@ -1072,9 +1072,9 @@ static RCoreHelpMessage help_msg_ax = {
"axF", " [flg-glob]", "find data/code references of flags",
"axf", "[?] [addr]", "find data/code references from this address",
"axff[j]", " [addr]", "find data/code references from this function",
"axg", " [addr]", "show xrefs graph to reach current function",
"axg*", " [addr]", "show xrefs graph to given address, use .axg*;aggv",
"axgj", " [addr]", "show xrefs graph to reach current function in json format",
"axg", "[j*] [addr]", "show xrefs graph to reach current function",
// "axg*", " [addr]", "show xrefs graph to given address, use .axg*;aggv",
// "axgj", " [addr]", "show xrefs graph to reach current function in json format",
"axi", " addr [at]", "add indirect code reference (see ax?)",
"axj", "", "add jmp reference", // list refs in json format",
"axl", "[jcq]", "list xrefs (axlc = count, axlq = quiet, axlj = json)",
@ -10259,7 +10259,8 @@ static bool cmd_anal_refs(RCore *core, const char *input) {
} else if (input[1] == 'j') {
PJ *pj = pj_new ();
anal_axg (core, input + 2, 0, db, R_CORE_ANAL_JSON, pj);
r_cons_printf("%s\n", pj_string (pj));
const char *pjs = pj_string (pj);
r_cons_printf ("%s\n", *pjs? pjs: "{}");
pj_free (pj);
} else {
anal_axg (core, input[1] ? input + 2 : NULL, 0, db, 0, NULL);

View File

@ -1484,6 +1484,9 @@ static bool cmd_dmh(RCore *core, const char *input) {
return false;
#endif
}
if (input[1] == 'j') {
r_cons_println ("{}");
}
return true;
}
@ -1996,7 +1999,10 @@ static int cmd_debug_map(RCore *core, const char *input) {
r_debug_map_sync (core->dbg); // update process memory maps
r_debug_map_list (core->dbg, core->offset, input);
} else {
R_LOG_WARN ("Memory Maps require to be (cfg.debug/-d) in debugger mode. Otherwise use 'om'");
R_LOG_INFO ("dm requires the debugger or use `om` instead");
if (*input == 'j') {
r_cons_println ("{}");
}
}
break;
case '=': // "dm="

View File

@ -1,38 +1,39 @@
iSj
&j
/ad/j push rbp
/adj push rbp
/j l
/Oj
/oj
/Rj
/wj
# /Oj
# /oj
# /Rj
# /wj
?j 0x10
?Vj
aaij
aarj
abj
abpj
abtj
aeafj
# abj
# abpj
# abtj
# aeafj
aeaj
aerj
afbij
# afbij
afbj
afcrj
# afcrj
afij
afisaj
aflj
aflqj
aflmj
afnsj @ sym.main
afvbj @ sym.main
afvj @ sym.main
afvrj @ sym.main
afvsj @ sym.main
agcj @ sym.main
# aflqj
# aflmj
# afnsj @ sym.main
# afvbj @ sym.main
# afvj @ sym.main
# afvrj @ sym.main
# afvsj @ sym.main
# agcj @ sym.main
aggj
agJ
agj
# agJ
# agj
aij
anj
aOj
# aOj

View File

@ -5,7 +5,7 @@ asj
avj
axfj
axfj @ sym.main
axgj @ str.Invalid_phase_s
# axgj @ str.Invalid_phase_s
axlj @ sym.main
axtj
axtj @ sym.main

View File

@ -24,7 +24,7 @@ izj
izzj
iZj
Lcj
Lsj
# Lsj
obj
oj
oLj

View File

@ -1,18 +1,18 @@
rabin2 -gj /bin/ls
r2 -jV
pcj
pdbj
pdfj
# pdbj
# pdfj
pDj
pdaj
pdj
pDJ
pdJ
pdrj
# pdrj
pIj
pij
pifj
pifcj
# pifj
# pifcj
Pj
psj
pxhj