Skip to content

Commit 30bd5aa

Browse files
committed
test(routes): test-file callers and handlers pair only within their file
Design decision 2026-10-18 (R1 + R2): a DATA_FLOWS caller in a test file pairs only with handlers in that same file, and a handler in a test file pairs only with callers in that same file. infrascan: test callers by each language rule (a tests/ directory, a Java *Test.java, a TS .spec.ts) get no flow to a production handler; a production caller gets no flow to a test mock server; a test reaches the mock server of its own file but not one in another test file. Controls: a production caller keeps its flow, and so does a same-file test pair. edge_types_probe: a Spring controller, a mock controller under src/test/, a production TS client and a TS .test.ts client meet on the same __route__ANY__ nodes. Exactly one flow (production client to production controller) on the sequential and on the parallel path. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 684dcf5 commit 30bd5aa

2 files changed

Lines changed: 202 additions & 0 deletions

File tree

‎tests/test_edge_types_probe.c‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,132 @@ static int et_handles_exact(const EtFile *files, int nfiles, const EtHandles *wa
10881088
return et_handles_exact_routes(files, nfiles, want, parallel, NULL);
10891089
}
10901090

1091+
/* The exact DATA_FLOWS set, caller -> handler by QN tail. route_qns (NULL-terminated) must
1092+
* exist, so a test that expects NO flow on a path cannot pass because the caller's or the
1093+
* handler's Route was never created; handles is the exact HANDLES count. */
1094+
typedef struct {
1095+
const char *caller_tail;
1096+
const char *handler_tail;
1097+
} EtFlow;
1098+
1099+
static int et_flow_matches(cbm_store_t *store, const cbm_edge_t *e, const EtFlow *want,
1100+
int wanted, int *found) {
1101+
cbm_node_t src = {0};
1102+
cbm_node_t tgt = {0};
1103+
int have_src = cbm_store_find_node_by_id(store, e->source_id, &src) == CBM_STORE_OK;
1104+
int have_tgt = cbm_store_find_node_by_id(store, e->target_id, &tgt) == CBM_STORE_OK;
1105+
int matched = 0;
1106+
for (int wi = 0; have_src && have_tgt && wi < wanted; wi++) {
1107+
if (!found[wi] && et_qn_has_tail(src.qualified_name, want[wi].caller_tail) &&
1108+
et_qn_has_tail(tgt.qualified_name, want[wi].handler_tail)) {
1109+
found[wi] = matched = 1;
1110+
break;
1111+
}
1112+
}
1113+
if (!matched) {
1114+
fprintf(stderr, " [ET-FLOWS] unexpected %s -> %s\n",
1115+
have_src && src.qualified_name ? src.qualified_name : "<?>",
1116+
have_tgt && tgt.qualified_name ? tgt.qualified_name : "<?>");
1117+
}
1118+
if (have_src) cbm_node_free_fields(&src);
1119+
if (have_tgt) cbm_node_free_fields(&tgt);
1120+
return matched;
1121+
}
1122+
1123+
static int et_data_flows_exact(const EtFile *files, int nfiles, const EtFlow *want, int parallel,
1124+
const char *const *route_qns, int handles) {
1125+
EtProj lp;
1126+
cbm_store_t *store =
1127+
parallel ? et_index_parallel(&lp, files, nfiles) : et_index_files(&lp, files, nfiles);
1128+
int wanted = 0;
1129+
int found[ET_ROUTE_ASSERT_MAX] = {0};
1130+
while (want[wanted].caller_tail && wanted < ET_ROUTE_ASSERT_MAX) {
1131+
wanted++;
1132+
}
1133+
cbm_edge_t *edges = NULL;
1134+
int n = 0;
1135+
int ok = store != NULL &&
1136+
cbm_store_find_edges_by_type(store, lp.project, "DATA_FLOWS", &edges, &n) ==
1137+
CBM_STORE_OK &&
1138+
n == wanted;
1139+
for (int i = 0; store && i < n; i++) {
1140+
if (!et_flow_matches(store, &edges[i], want, wanted, found)) {
1141+
ok = 0;
1142+
}
1143+
}
1144+
for (int wi = 0; wi < wanted; wi++) {
1145+
if (!found[wi]) {
1146+
ok = 0;
1147+
fprintf(stderr, " [ET-FLOWS] missing %s -> %s\n", want[wi].caller_tail,
1148+
want[wi].handler_tail);
1149+
}
1150+
}
1151+
for (int ri = 0; route_qns && route_qns[ri]; ri++) {
1152+
cbm_node_t route = {0};
1153+
if (!store ||
1154+
cbm_store_find_node_by_qn(store, lp.project, route_qns[ri], &route) != CBM_STORE_OK) {
1155+
ok = 0;
1156+
fprintf(stderr, " [ET-FLOWS] missing Route %s\n", route_qns[ri]);
1157+
} else {
1158+
cbm_node_free_fields(&route);
1159+
}
1160+
}
1161+
int got_handles = store ? cbm_store_count_edges_by_type(store, lp.project, "HANDLES") : -1;
1162+
if (got_handles != handles) {
1163+
ok = 0;
1164+
fprintf(stderr, " [ET-FLOWS] HANDLES=%d expected %d\n", got_handles, handles);
1165+
}
1166+
if (!ok) {
1167+
fprintf(stderr, " [ET-FLOWS] FAIL (%s path) expected=%d actual=%d\n",
1168+
parallel ? "parallel" : "sequential", wanted, n);
1169+
}
1170+
if (edges) cbm_store_free_edges(edges, n);
1171+
et_cleanup(&lp, store);
1172+
return ok;
1173+
}
1174+
1175+
/* R1 + R2 (user decision, 2026-10-18): DATA_FLOWS pair a test-file caller only with handlers in its
1176+
* own file, and a test-file handler only with callers in its own file. A Spring controller and a
1177+
* mock controller under src/test/ both answer `@RequestMapping` (any method). A production TS
1178+
* client and a TS test (.test.ts) call them with fetch, which names no method, so every caller and
1179+
* handler meets on the same __route__ANY__ node. Only the production client -> production
1180+
* controller pair is a data flow of the application, on the sequential and the parallel path. */
1181+
static const EtFile et_r12_files[] = {
1182+
{"server/OrderController.java",
1183+
"package com.example;\n\n"
1184+
"import org.springframework.web.bind.annotation.RequestMapping;\n"
1185+
"import org.springframework.web.bind.annotation.RestController;\n\n"
1186+
"@RestController\n@RequestMapping(\"/api\")\npublic class OrderController {\n"
1187+
" @RequestMapping(\"/orders\")\n"
1188+
" public String orders() {\n return \"orders\";\n }\n}\n"},
1189+
{"server/src/test/java/com/example/MockPaymentController.java",
1190+
"package com.example;\n\n"
1191+
"import org.springframework.web.bind.annotation.RequestMapping;\n"
1192+
"import org.springframework.web.bind.annotation.RestController;\n\n"
1193+
"@RestController\n@RequestMapping(\"/api\")\npublic class MockPaymentController {\n"
1194+
" @RequestMapping(\"/payments\")\n"
1195+
" public String pay() {\n return \"paid\";\n }\n}\n"},
1196+
{"web/src/api.ts",
1197+
"export async function loadOrders() {\n return fetch(\"/api/orders\");\n}\n\n"
1198+
"export async function loadPayments() {\n return fetch(\"/api/payments\");\n}\n"},
1199+
{"web/src/api.test.ts",
1200+
"export async function testLoadOrders() {\n return fetch(\"/api/orders\");\n}\n"}};
1201+
1202+
static const EtFlow et_r12_flows[] = {{"api.loadOrders", "OrderController.orders"}, {NULL, NULL}};
1203+
1204+
static const char *const et_r12_routes[] = {"__route__ANY__/api/orders",
1205+
"__route__ANY__/api/payments", NULL};
1206+
1207+
TEST(data_flows_test_files_pair_within_their_file_sequential) {
1208+
ASSERT_TRUE(et_data_flows_exact(et_r12_files, 4, et_r12_flows, 0, et_r12_routes, 2));
1209+
PASS();
1210+
}
1211+
1212+
TEST(data_flows_test_files_pair_within_their_file_parallel) {
1213+
ASSERT_TRUE(et_data_flows_exact(et_r12_files, 4, et_r12_flows, 1, et_r12_routes, 2));
1214+
PASS();
1215+
}
1216+
10911217
/* #1146: Laravel's two class-based handler forms —
10921218
* Route::get('/x', [UserController::class, 'show']) (controller method)
10931219
* Route::get('/x', GetCurrentUserController::class) (invokable: __invoke)
@@ -2589,6 +2715,8 @@ SUITE(edge_types_probe) {
25892715
RUN_TEST(handles_fiber_group_prefix_parallel_issue686);
25902716
RUN_TEST(handles_gin_group_prefix_issue686);
25912717
RUN_TEST(handles_spring_java);
2718+
RUN_TEST(data_flows_test_files_pair_within_their_file_sequential);
2719+
RUN_TEST(data_flows_test_files_pair_within_their_file_parallel);
25922720
RUN_TEST(handles_spring_java_path_attribute_fourth);
25932721
RUN_TEST(handles_spring_kotlin);
25942722
RUN_TEST(handles_jaxrs_java);

‎tests/test_infrascan.c‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,78 @@ TEST(infrascan_data_flows_reach_every_infra_handler) {
477477
PASS();
478478
}
479479

480+
/* R1 + R2 (user decision, 2026-10-18): a caller in a test file pairs only with handlers in that same
481+
* file, and a handler in a test file pairs only with callers in that same file. A test's request to
482+
* the application, a production caller matched to a test's mock server, and the private mock servers
483+
* of two tests pairing across files are not data flows of the application. */
484+
static int64_t r12_fn(cbm_gbuf_t *gb, const char *name, const char *file) {
485+
char qn[96];
486+
snprintf(qn, sizeof(qn), "test.%s", name);
487+
return cbm_gbuf_upsert_node(gb, "Function", name, qn, file, 1, 3, "{}");
488+
}
489+
490+
static int64_t r12_route(cbm_gbuf_t *gb, const char *path) {
491+
char qn[128];
492+
snprintf(qn, sizeof(qn), "__route__GET__%s", path);
493+
return cbm_gbuf_upsert_node(gb, "Route", path, qn, "", 0, 0, "{\"method\":\"GET\"}");
494+
}
495+
496+
static void r12_call(cbm_gbuf_t *gb, int64_t caller, int64_t route, const char *path) {
497+
char props[160];
498+
snprintf(props, sizeof(props),
499+
"{\"callee\":\"requests.get\",\"url_path\":\"%s\",\"method\":\"GET\"}", path);
500+
cbm_gbuf_insert_edge(gb, caller, route, "HTTP_CALLS", props);
501+
}
502+
503+
TEST(infrascan_data_flows_test_callers_pair_only_within_their_file) {
504+
cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp/cbm_infrascan_r12_callers");
505+
ASSERT_NOT_NULL(gb);
506+
int64_t route = r12_route(gb, "/api/orders");
507+
int64_t handler = r12_fn(gb, "list_orders", "app/views.py");
508+
cbm_gbuf_insert_edge(gb, handler, route, "HANDLES", "{}");
509+
/* Test files by each language's own rule: a tests/ directory, a Java *Test.java, a TS .spec.ts. */
510+
int64_t py_test = r12_fn(gb, "test_list_orders", "tests/test_orders.py");
511+
int64_t java_test = r12_fn(gb, "listsOrders", "app/OrderServiceTest.java");
512+
int64_t ts_spec = r12_fn(gb, "fetchesOrders", "web/orders.spec.ts");
513+
int64_t prod = r12_fn(gb, "load_orders", "app/client.py");
514+
r12_call(gb, py_test, route, "/api/orders");
515+
r12_call(gb, java_test, route, "/api/orders");
516+
r12_call(gb, ts_spec, route, "/api/orders");
517+
r12_call(gb, prod, route, "/api/orders");
518+
cbm_pipeline_create_route_nodes(gb);
519+
ASSERT_FALSE(has_data_flow(gb, py_test, handler));
520+
ASSERT_FALSE(has_data_flow(gb, java_test, handler));
521+
ASSERT_FALSE(has_data_flow(gb, ts_spec, handler));
522+
/* Control: the production caller keeps its flow. */
523+
ASSERT_TRUE(has_data_flow(gb, prod, handler));
524+
cbm_gbuf_free(gb);
525+
PASS();
526+
}
527+
528+
TEST(infrascan_data_flows_test_handlers_pair_only_within_their_file) {
529+
cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp/cbm_infrascan_r12_handlers");
530+
ASSERT_NOT_NULL(gb);
531+
int64_t route = r12_route(gb, "/api/s3");
532+
/* Two tests each run their own mock server on the same path. */
533+
int64_t mock_a = r12_fn(gb, "mock_s3_a", "tests/test_upload.py");
534+
int64_t mock_b = r12_fn(gb, "mock_s3_b", "tests/conftest.py");
535+
cbm_gbuf_insert_edge(gb, mock_a, route, "HANDLES", "{}");
536+
cbm_gbuf_insert_edge(gb, mock_b, route, "HANDLES", "{}");
537+
int64_t prod = r12_fn(gb, "upload", "app/storage.py");
538+
int64_t test_a = r12_fn(gb, "test_upload", "tests/test_upload.py");
539+
r12_call(gb, prod, route, "/api/s3");
540+
r12_call(gb, test_a, route, "/api/s3");
541+
cbm_pipeline_create_route_nodes(gb);
542+
/* A production caller never reaches a test's mock server. */
543+
ASSERT_FALSE(has_data_flow(gb, prod, mock_a));
544+
ASSERT_FALSE(has_data_flow(gb, prod, mock_b));
545+
/* A test reaches its own file's mock server, not another test file's. */
546+
ASSERT_TRUE(has_data_flow(gb, test_a, mock_a));
547+
ASSERT_FALSE(has_data_flow(gb, test_a, mock_b));
548+
cbm_gbuf_free(gb);
549+
PASS();
550+
}
551+
480552
SUITE(infrascan) {
481553
RUN_TEST(infrascan_http_route_literal_guard_rejects_filesystem_paths);
482554
RUN_TEST(infrascan_http_route_literal_guard_rejects_document_extensions);
@@ -488,4 +560,6 @@ SUITE(infrascan) {
488560
RUN_TEST(infrascan_data_flows_link_every_caller_of_a_route);
489561
RUN_TEST(infrascan_data_flows_do_not_depend_on_caller_insertion_order);
490562
RUN_TEST(infrascan_data_flows_reach_every_infra_handler);
563+
RUN_TEST(infrascan_data_flows_test_callers_pair_only_within_their_file);
564+
RUN_TEST(infrascan_data_flows_test_handlers_pair_only_within_their_file);
491565
}

0 commit comments

Comments
 (0)