Skip to content

Commit c38bfff

Browse files
committed
test(mcp): isolate search scratch cleanup fixtures
Let each test server use a private scratch parent so cleanup checks cannot count other processes' temporary files. Observe live scratch in the hook-rejection fixture, preserve deadline assertions, and cover scratch creation failure with a valid path filter for leak-sanitizer validation. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent fe8dd9d commit c38bfff

3 files changed

Lines changed: 82 additions & 15 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,7 @@ struct cbm_mcp_server {
17761776
#endif
17771777
size_t search_output_limit_override;
17781778
const char *search_scan_command_override;
1779+
const char *search_scratch_dir_override; /* NULL = cbm_tmpdir() */
17791780
uint64_t search_scan_timeout_override_ms;
17801781
bool search_scan_timeout_override_set;
17811782
cbm_thread_t autoindex_tid;
@@ -2114,6 +2115,12 @@ void cbm_mcp_server_set_search_scan_timeout_for_test(cbm_mcp_server_t *srv, uint
21142115
srv->search_scan_timeout_override_set = override_set;
21152116
}
21162117
}
2118+
2119+
void cbm_mcp_server_set_search_scratch_dir_for_test(cbm_mcp_server_t *srv, const char *dir) {
2120+
if (srv) {
2121+
srv->search_scratch_dir_override = dir;
2122+
}
2123+
}
21172124
#ifdef CBM_ENABLE_TEST_SEAMS
21182125
void cbm_mcp_server_set_snapshot_read_test_hook(cbm_mcp_server_t *srv,
21192126
cbm_mcp_snapshot_read_test_hook_fn hook,
@@ -14786,14 +14793,14 @@ static void search_scratch_close(search_scratch_t *scratch) {
1478614793
/* Open the scratch directory, write `pattern` to the grep -f file, and leave the
1478714794
* file list open for write_scoped_filelist. Returns true on success; on failure
1478814795
* everything already created is removed before returning. */
14789-
static bool search_scratch_open(search_scratch_t *scratch, const char *pattern) {
14796+
static bool search_scratch_open(search_scratch_t *scratch, const char *parent,
14797+
const char *pattern) {
1479014798
scratch->dir[0] = '\0';
1479114799
scratch->pattern_path[0] = '\0';
1479214800
scratch->filelist_path[0] = '\0';
1479314801
scratch->filelist = NULL;
1479414802

14795-
int written =
14796-
snprintf(scratch->dir, sizeof(scratch->dir), "%s/cbm-search-XXXXXX", cbm_tmpdir());
14803+
int written = snprintf(scratch->dir, sizeof(scratch->dir), "%s/cbm-search-XXXXXX", parent);
1479714804
if (written <= 0 || (size_t)written >= sizeof(scratch->dir) || !cbm_mkdtemp(scratch->dir)) {
1479814805
scratch->dir[0] = '\0';
1479914806
return false;
@@ -15097,7 +15104,9 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1509715104
: scan_started_ms + scan_budget_ms;
1509815105
bool scan_deadline_latched = false;
1509915106
search_scratch_t scratch;
15100-
if (!search_scratch_open(&scratch, pattern)) {
15107+
const char *scratch_parent =
15108+
srv->search_scratch_dir_override ? srv->search_scratch_dir_override : cbm_tmpdir();
15109+
if (!search_scratch_open(&scratch, scratch_parent, pattern)) {
1510115110
bool scan_cancelled = mcp_request_cancelled(srv);
1510215111
bool scan_timed_out = cbm_now_ms() >= scan_deadline_ms;
1510315112
char errmsg[CBM_SZ_256];

‎src/mcp/mcp_internal.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ void cbm_mcp_server_set_auto_index_count_test_hook(cbm_mcp_server_t *srv,
2929
void cbm_mcp_server_set_search_scan_command_for_test(cbm_mcp_server_t *srv, const char *command);
3030
void cbm_mcp_server_set_search_scan_timeout_for_test(cbm_mcp_server_t *srv, uint64_t timeout_ms,
3131
bool override_set);
32+
/* Parent directory for search_code's private cbm-search-XXXXXX scratch
33+
* (NULL restores cbm_tmpdir()). Tests point it at a private directory so a
34+
* scratch count cannot see other processes' entries in the shared temp dir.
35+
* The server borrows dir until it is reset or the server is freed. */
36+
void cbm_mcp_server_set_search_scratch_dir_for_test(cbm_mcp_server_t *srv, const char *dir);
3237

3338
/* Release only the constructor-created pristine in-memory store. Public
3439
* cbm_mcp_server_new(NULL) semantics remain unchanged; daemon sessions use

‎tests/test_mcp.c‎

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,14 @@ typedef struct {
174174
bool output_filled;
175175
int calls;
176176
char command[CBM_SZ_4K];
177+
/* When set, the hook counts cbm-search-* entries here at spawn time, which
178+
* proves the scan's scratch really lives in this private directory. */
179+
const char *scratch_directory;
180+
int scratch_seen;
177181
} mcp_search_command_probe_t;
178182

183+
static int mcp_count_directory_entries_with_prefix(const char *directory, const char *prefix);
184+
179185
typedef struct {
180186
char path[512];
181187
char *saved_cache;
@@ -213,6 +219,10 @@ static bool mcp_search_command_hook_probe(void *context, const char *command) {
213219
}
214220
probe->calls++;
215221
snprintf(probe->command, sizeof(probe->command), "%s", command);
222+
if (probe->scratch_directory) {
223+
probe->scratch_seen =
224+
mcp_count_directory_entries_with_prefix(probe->scratch_directory, "cbm-search-");
225+
}
216226
if (probe->delay_ms > 0) {
217227
cbm_usleep(probe->delay_ms * 1000U);
218228
}
@@ -11145,13 +11155,14 @@ TEST(search_code_output_limit_fails_closed_and_cleans_scan) {
1114511155
TEST(search_code_scan_deadline_fails_closed_and_resets) {
1114611156
mcp_search_cache_t cache;
1114711157
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-deadline"));
11148-
int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11149-
ASSERT_TRUE(scratch_before >= 0);
1115011158

1115111159
char tmp[512], src_path[768], vendor_path[768];
1115211160
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1115311161
vendor_path, sizeof(vendor_path));
1115411162
ASSERT_NOT_NULL(srv);
11163+
/* Scratch goes to the private cache dir: the shared temp dir is visible to
11164+
* every other process on the machine, so a count there is not exact. */
11165+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
1115511166
cbm_mcp_server_set_search_scan_timeout_for_test(srv, 0, true);
1115611167

1115711168
char *response =
@@ -11175,7 +11186,7 @@ TEST(search_code_scan_deadline_fails_closed_and_resets) {
1117511186
/* An immediate deadline can return before the log directory is created;
1117611187
* both a missing directory (-1) and an empty one (0) prove no artifact. */
1117711188
ASSERT_TRUE(mcp_count_directory_entries_with_prefix(logs, ".mcp-command-") <= 0);
11178-
ASSERT_EQ(mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-"), scratch_before);
11189+
ASSERT_EQ(mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-"), 0);
1117911190
free(response);
1118011191

1118111192
cbm_mcp_server_set_search_scan_timeout_for_test(srv, 0, false);
@@ -11276,13 +11287,14 @@ TEST(search_code_scan_setup_failures_respect_cause_precedence) {
1127611287
TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1127711288
mcp_search_cache_t cache;
1127811289
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-live-deadline"));
11279-
int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11280-
ASSERT_TRUE(scratch_before >= 0);
1128111290

1128211291
char tmp[512], src_path[768], vendor_path[768];
1128311292
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1128411293
vendor_path, sizeof(vendor_path));
1128511294
ASSERT_NOT_NULL(srv);
11295+
/* Count only this server's scratch. The hook-rejection test below observes
11296+
* creation directly, without requiring this short deadline to reach spawn. */
11297+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
1128611298
#ifdef _WIN32
1128711299
const char *slow_command =
1128811300
"echo deadline-partial-output & powershell.exe -NoProfile -Command \"Start-Sleep -Seconds "
@@ -11305,7 +11317,7 @@ TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1130511317
char logs[640];
1130611318
snprintf(logs, sizeof(logs), "%s/logs", cache.path);
1130711319
int command_artifacts = mcp_count_directory_entries_with_prefix(logs, ".mcp-command-");
11308-
int scratch_after = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11320+
int scratch_after = mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-");
1130911321

1131011322
free(response);
1131111323
cbm_mcp_server_set_search_scan_command_for_test(srv, NULL);
@@ -11317,7 +11329,7 @@ TEST(search_code_scan_live_child_deadline_is_bounded_and_fails_closed) {
1131711329
ASSERT_TRUE(timed_out);
1131811330
ASSERT_TRUE(partial_hidden);
1131911331
ASSERT_EQ(command_artifacts, 0);
11320-
ASSERT_EQ(scratch_after, scratch_before);
11332+
ASSERT_EQ(scratch_after, 0);
1132111333
PASS();
1132211334
}
1132311335

@@ -11380,14 +11392,13 @@ TEST(search_code_scan_deadline_precedes_output_limit) {
1138011392
TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up) {
1138111393
mcp_search_cache_t cache;
1138211394
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-reject"));
11383-
int scratch_before = mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-");
11384-
ASSERT_TRUE(scratch_before >= 0);
1138511395

1138611396
char tmp[512], src_path[768], vendor_path[768];
1138711397
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
1138811398
vendor_path, sizeof(vendor_path));
1138911399
ASSERT_NOT_NULL(srv);
11390-
mcp_search_command_probe_t probe = {.reject = true};
11400+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, cache.path);
11401+
mcp_search_command_probe_t probe = {.reject = true, .scratch_directory = cache.path};
1139111402
cbm_mcp_server_set_command_test_hook(srv, mcp_search_command_hook_probe, &probe);
1139211403

1139311404
char *response = cbm_mcp_handle_tool(srv, "search_code",
@@ -11402,14 +11413,55 @@ TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up) {
1140211413
char logs[640];
1140311414
snprintf(logs, sizeof(logs), "%s/logs", cache.path);
1140411415
ASSERT_EQ(mcp_count_directory_entries_with_prefix(logs, ".mcp-command-"), 0);
11405-
ASSERT_EQ(mcp_count_directory_entries_with_prefix(cbm_tmpdir(), "cbm-search-"), scratch_before);
11416+
ASSERT_EQ(probe.scratch_seen, 1);
11417+
ASSERT_EQ(mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-"), 0);
1140611418
free(response);
1140711419
cbm_mcp_server_free(srv);
1140811420
cleanup_prefilter_dir(tmp, src_path, vendor_path);
1140911421
ASSERT_TRUE(mcp_search_cache_close(&cache));
1141011422
PASS();
1141111423
}
1141211424

11425+
/* The private parent seam also pins scratch creation failure without changing
11426+
* process-wide temp settings. A valid path filter exercises its early cleanup
11427+
* under leak sanitizers; no command should be launched. */
11428+
TEST(search_code_scratch_creation_error_releases_path_filter) {
11429+
mcp_search_cache_t cache;
11430+
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-scratch-error"));
11431+
char blocker_path[640];
11432+
snprintf(blocker_path, sizeof(blocker_path), "%s/not-a-directory", cache.path);
11433+
FILE *blocker = cbm_fopen(blocker_path, "wb");
11434+
ASSERT_NOT_NULL(blocker);
11435+
ASSERT_EQ(fclose(blocker), 0);
11436+
11437+
char tmp[512], src_path[768], vendor_path[768];
11438+
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
11439+
vendor_path, sizeof(vendor_path));
11440+
ASSERT_NOT_NULL(srv);
11441+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, blocker_path);
11442+
mcp_search_command_probe_t probe = {0};
11443+
cbm_mcp_server_set_command_test_hook(srv, mcp_search_command_hook_probe, &probe);
11444+
char *response =
11445+
cbm_mcp_handle_tool(srv, "search_code",
11446+
"{\"pattern\":\"HandleRequest\",\"project\":\"prefilter-search\","
11447+
"\"path_filter\":\"^cbm_leak_probe/\"}");
11448+
bool setup_error = response && strstr(response, "\"isError\":true") != NULL &&
11449+
strstr(response, "cannot create temp file") != NULL;
11450+
int scratch_after = mcp_count_directory_entries_with_prefix(cache.path, "cbm-search-");
11451+
free(response);
11452+
cbm_mcp_server_set_search_scratch_dir_for_test(srv, NULL);
11453+
cbm_mcp_server_free(srv);
11454+
cleanup_prefilter_dir(tmp, src_path, vendor_path);
11455+
int removed = cbm_unlink(blocker_path);
11456+
bool cache_closed = mcp_search_cache_close(&cache);
11457+
ASSERT_TRUE(setup_error);
11458+
ASSERT_EQ(probe.calls, 0);
11459+
ASSERT_EQ(scratch_after, 0);
11460+
ASSERT_EQ(removed, 0);
11461+
ASSERT_TRUE(cache_closed);
11462+
PASS();
11463+
}
11464+
1141311465
TEST(search_code_scoped_exit_one_is_not_no_match) {
1141411466
char tmp[512], src_path[768], vendor_path[768];
1141511467
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
@@ -21277,6 +21329,7 @@ SUITE(mcp) {
2127721329
RUN_TEST(search_code_scan_cancellation_precedes_zero_deadline);
2127821330
RUN_TEST(search_code_scan_deadline_precedes_output_limit);
2127921331
RUN_TEST(search_code_scan_hook_rejection_is_contained_and_cleans_up);
21332+
RUN_TEST(search_code_scratch_creation_error_releases_path_filter);
2128021333
RUN_TEST(search_code_scoped_exit_one_is_not_no_match);
2128121334
RUN_TEST(search_code_no_match_is_empty_for_direct_and_scoped_routes);
2128221335
RUN_TEST(search_code_scoped_scan_skips_non_regular_indexed_paths);

0 commit comments

Comments
 (0)