Skip to content

Commit 000c656

Browse files
committed
move test_rop_cache to integration test suite
1 parent e422516 commit 000c656

File tree

3 files changed

+79
-63
lines changed

3 files changed

+79
-63
lines changed

test/integration/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ if get_option('enable_tests') and cli_enabled
2727
'open_analyse_save_load_project',
2828
'pdb',
2929
'project_migrate',
30+
'rop_cache',
3031
'rzpipe',
3132
'str_search',
3233
]

test/integration/test_rop_cache.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-FileCopyrightText: 2024 z3phyr <giridh1337@gmail.com>
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
#include <rz_core.h>
5+
#include <rz_rop.h>
6+
#include "../unit/minunit.h"
7+
8+
bool test_rop_cache(void) {
9+
RzCore *core = rz_core_new();
10+
mu_assert_notnull(core, "core");
11+
12+
#if __WINDOWS__
13+
const char *test_bin = "bins/pe/standard.exe";
14+
#else
15+
const char *test_bin = "bins/elf/analysis/hello-linux-x86_64";
16+
#endif
17+
18+
RzCoreFile *cf = rz_core_file_open(core, test_bin, RZ_PERM_RX, 0);
19+
mu_assert_notnull(cf, "open file");
20+
rz_core_bin_load(core, NULL, 0);
21+
rz_config_set_b(core->config, "rop.cache", true);
22+
23+
RzCmdStateOutput state;
24+
rz_cmd_state_output_init(&state, RZ_OUTPUT_MODE_QUIET, core);
25+
26+
RzRopSearchContext *ctx = rz_core_rop_search_context_new(
27+
core, "", false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state);
28+
rz_core_rop_search(core, ctx);
29+
rz_core_rop_search_context_free(ctx);
30+
31+
if (core->analysis->ht_rop) {
32+
mu_assert_eq(core->analysis->ht_rop->count, 0, "empty filter not cached");
33+
}
34+
35+
const char *filter = "ret";
36+
ut64 key = rz_str_djb2_hash(filter);
37+
38+
ctx = rz_core_rop_search_context_new(
39+
core, filter, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state);
40+
rz_core_rop_search(core, ctx);
41+
rz_core_rop_search_context_free(ctx);
42+
43+
mu_assert_notnull(core->analysis->ht_rop, "ht_rop");
44+
char *result = ht_up_find(core->analysis->ht_rop, key, NULL);
45+
mu_assert_notnull(result, "cached result");
46+
47+
ctx = rz_core_rop_search_context_new(
48+
core, filter, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state);
49+
rz_core_rop_search(core, ctx);
50+
rz_core_rop_search_context_free(ctx);
51+
52+
char *result2 = ht_up_find(core->analysis->ht_rop, key, NULL);
53+
mu_assert_ptreq(result2, result, "cache hit");
54+
55+
const char *filter2 = "pop";
56+
ut64 key2 = rz_str_djb2_hash(filter2);
57+
58+
ctx = rz_core_rop_search_context_new(
59+
core, filter2, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state);
60+
rz_core_rop_search(core, ctx);
61+
rz_core_rop_search_context_free(ctx);
62+
63+
char *result3 = ht_up_find(core->analysis->ht_rop, key2, NULL);
64+
mu_assert_notnull(result3, "pop cached");
65+
mu_assert_ptrneq(result3, result, "different cache entries");
66+
mu_assert_eq(core->analysis->ht_rop->count, 2, "cache count");
67+
68+
rz_cmd_state_output_fini(&state);
69+
rz_core_free(core);
70+
mu_end;
71+
}
72+
73+
bool all_tests() {
74+
mu_run_test(test_rop_cache);
75+
return tests_passed != tests_run;
76+
}
77+
78+
mu_main(all_tests)

test/unit/test_rop.c

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -141,70 +141,7 @@ bool test_rz_direct_solver() {
141141
mu_end;
142142
}
143143

144-
bool test_rop_cache(void) {
145-
RzCore *core = rz_core_new();
146-
mu_assert_notnull(core, "core");
147-
148-
#if __WINDOWS__
149-
const char *test_bin_name = "bins/pe/standard.exe";
150-
#else
151-
const char *test_bin_name = "bins/elf/analysis/hello-linux-x86_64";
152-
#endif
153-
char *test_bin = NULL;
154-
if (rz_file_exists(test_bin_name)) {
155-
test_bin = strdup(test_bin_name);
156-
} else {
157-
// Fallback for environments where bins/ is missing but rizin-testbins exists
158-
// Try: rizin-testbins/elf/... (CWD is root)
159-
test_bin = rz_str_newf("rizin-testbins/%s", test_bin_name + 5);
160-
if (!rz_file_exists(test_bin)) {
161-
free(test_bin);
162-
// Try: ../rizin-testbins/elf/... (CWD is test/)
163-
test_bin = rz_str_newf("../rizin-testbins/%s", test_bin_name + 5);
164-
if (!rz_file_exists(test_bin)) {
165-
free(test_bin);
166-
// Try: ../../../rizin-testbins/elf/... (CWD is build/test/unit)
167-
test_bin = rz_str_newf("../../../rizin-testbins/%s", test_bin_name + 5);
168-
}
169-
}
170-
}
171-
172-
RzCoreFile *cf = rz_core_file_open(core, test_bin, RZ_PERM_RX, 0);
173-
mu_assert_notnull(cf, "open file");
174-
rz_core_bin_load(core, NULL, 0);
175-
rz_config_set_b(core->config, "rop.cache", true);
176-
177-
const char *filter = "ret";
178-
ut64 key = rz_str_djb2_hash(filter);
179-
180-
RzCmdStateOutput state;
181-
rz_cmd_state_output_init(&state, RZ_OUTPUT_MODE_QUIET, core);
182-
183-
RzRopSearchContext *ctx = rz_core_rop_search_context_new(
184-
core, filter, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state);
185-
rz_core_rop_search(core, ctx);
186-
rz_core_rop_search_context_free(ctx);
187-
188-
mu_assert_notnull(core->analysis->ht_rop, "ht_rop");
189-
char *result = ht_up_find(core->analysis->ht_rop, key, NULL);
190-
mu_assert_notnull(result, "cached result");
191-
192-
ctx = rz_core_rop_search_context_new(
193-
core, filter, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state);
194-
rz_core_rop_search(core, ctx);
195-
rz_core_rop_search_context_free(ctx);
196-
197-
char *result2 = ht_up_find(core->analysis->ht_rop, key, NULL);
198-
mu_assert_ptreq(result2, result, "cache hit");
199-
200-
free(test_bin);
201-
rz_cmd_state_output_fini(&state);
202-
rz_core_free(core);
203-
mu_end;
204-
}
205-
206144
bool all_tests() {
207-
mu_run_test(test_rop_cache);
208145
mu_run_test(test_rz_direct_solver);
209146
return tests_passed != tests_run;
210147
}

0 commit comments

Comments
 (0)