Skip to content

Commit d211526

Browse files
authored
Merge pull request DeusData#2707 from DeusData/fix/data-flows-test-file-scope
fix(routes): scope DATA_FLOWS pairing at the test-file boundary (R1 + R2)
2 parents 32445b2 + 2cda42e commit d211526

3 files changed

Lines changed: 266 additions & 13 deletions

File tree

‎src/pipeline/pass_route_nodes.c‎

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ enum {
3333
#include <stdint.h>
3434
#include "graph_buffer/graph_buffer.h"
3535
#include "foundation/log.h"
36-
#include "service_patterns.h" /* cbm_service_pattern_is_http_route_literal */
36+
#include "service_patterns.h" /* cbm_service_pattern_is_http_route_literal */
37+
#include "helpers.h" /* cbm_is_test_file */
38+
#include "discover/discover.h" /* cbm_language_for_filename */
3739

3840
#include <stdio.h>
3941
#include <string.h>
@@ -707,14 +709,51 @@ static bool http_call_edge_has_valid_route(const cbm_gbuf_edge_t *edge) {
707709
return cbm_service_pattern_is_http_route_literal(url, callee);
708710
}
709711

712+
/* try_create_data_flow's refusal for a pair across the test-file boundary. */
713+
enum { RN_FLOW_TEST_SCOPED = -2 };
714+
715+
/* Is this node defined in a test file, by the extraction's own per-language rule? */
716+
static bool node_in_test_file(const cbm_gbuf_node_t *n) {
717+
if (!n || !n->file_path || !n->file_path[0]) {
718+
return false;
719+
}
720+
const char *slash = strrchr(n->file_path, '/');
721+
const char *base = slash ? slash + SKIP_ONE : n->file_path;
722+
return cbm_is_test_file(n->file_path, cbm_language_for_filename(base));
723+
}
724+
725+
/* R1 + R2 (user decision, 2026-10-18): a handler defined in a test file pairs only with
726+
* callers in that same file, and a caller in a test file pairs only with handlers in that same
727+
* file. A test's request to the application, a production caller matched to a test's mock
728+
* server, and two tests' private mock servers pairing across files are not data flows of the
729+
* application. The HTTP_CALLS / HANDLES edges themselves stay; only the pairing is scoped. */
730+
static bool data_flow_crosses_test_scope(const cbm_gbuf_t *gb, int64_t caller_id,
731+
int64_t handler_id) {
732+
const cbm_gbuf_node_t *caller = cbm_gbuf_find_by_id(gb, caller_id);
733+
const cbm_gbuf_node_t *handler = cbm_gbuf_find_by_id(gb, handler_id);
734+
if (!caller || !handler) {
735+
return false;
736+
}
737+
const char *cf = caller->file_path ? caller->file_path : "";
738+
const char *hf = handler->file_path ? handler->file_path : "";
739+
if (strcmp(cf, hf) == 0) {
740+
return false;
741+
}
742+
return node_in_test_file(caller) || node_in_test_file(handler);
743+
}
744+
710745
/* Try to create a DATA_FLOWS edge between caller and handler via a route.
711-
* Returns: 1=created, 0=skipped (self/duplicate), -1=skipped (has direct call). */
746+
* Returns: 1=created, 0=skipped (self/duplicate), -1=skipped (has direct call),
747+
* RN_FLOW_TEST_SCOPED=skipped (the pair crosses the test-file boundary, R1 + R2). */
712748
static int try_create_data_flow(cbm_gbuf_t *gb, int64_t caller_id, int64_t handler_id,
713749
const cbm_gbuf_node_t *route, const char *edge_type,
714750
const char *caller_props, bool via_infra) {
715751
if (caller_id == handler_id) {
716752
return 0;
717753
}
754+
if (data_flow_crosses_test_scope(gb, caller_id, handler_id)) {
755+
return RN_FLOW_TEST_SCOPED;
756+
}
718757
if (has_direct_call(gb, caller_id, handler_id)) {
719758
return CBM_NOT_FOUND;
720759
}
@@ -776,14 +815,23 @@ typedef struct {
776815
int infra_count;
777816
int flows;
778817
int skipped;
818+
int test_scoped;
779819
} route_flows_t;
780820

821+
typedef struct {
822+
int flows;
823+
int skipped; /* the caller already CALLS the handler */
824+
int test_scoped; /* the pair crosses the test-file boundary (R1 + R2) */
825+
} data_flow_stats_t;
826+
781827
static void route_flow_add(route_flows_t *rf, const cbm_gbuf_edge_t *caller, int64_t handler_id,
782828
const char *edge_type, bool via_infra) {
783829
int rc = try_create_data_flow(rf->gb, caller->source_id, handler_id, rf->route, edge_type,
784830
caller->properties_json, via_infra);
785831
if (rc > 0) {
786832
rf->flows++;
833+
} else if (rc == RN_FLOW_TEST_SCOPED) {
834+
rf->test_scoped++;
787835
} else if (rc < 0) {
788836
rf->skipped++;
789837
}
@@ -816,8 +864,8 @@ static void caller_data_flows(route_flows_t *rf, const cbm_gbuf_edge_t *caller,
816864
* is now the full caller x handler product. A (caller, handler) pair reached twice -- through two
817865
* routes, or directly and via infra -- keeps the order-free property winner of
818866
* cbm_gbuf_insert_edge. */
819-
static void create_route_data_flows(cbm_gbuf_t *gb, const cbm_gbuf_node_t *route, int *flows,
820-
int *skipped) {
867+
static void create_route_data_flows(cbm_gbuf_t *gb, const cbm_gbuf_node_t *route,
868+
data_flow_stats_t *st) {
821869
route_flows_t rf = {.gb = gb, .route = route};
822870
cbm_gbuf_find_edges_by_target_type(gb, route->id, "HANDLES", &rf.handles, &rf.handles_count);
823871
cbm_gbuf_find_edges_by_source_type(gb, route->id, "INFRA_MAPS", &rf.infra, &rf.infra_count);
@@ -836,8 +884,9 @@ static void create_route_data_flows(cbm_gbuf_t *gb, const cbm_gbuf_node_t *route
836884
for (int i = 0; i < async_count; i++) {
837885
caller_data_flows(&rf, async_edges[i], "ASYNC_CALLS");
838886
}
839-
*flows += rf.flows;
840-
*skipped += rf.skipped;
887+
st->flows += rf.flows;
888+
st->skipped += rf.skipped;
889+
st->test_scoped += rf.test_scoped;
841890
}
842891

843892
/* Find the enclosing proto service for a function by file + line range. */
@@ -940,19 +989,21 @@ static void create_data_flows(cbm_gbuf_t *gb) {
940989
return;
941990
}
942991

943-
int flows = 0;
944-
int skipped = 0;
992+
data_flow_stats_t st = {0};
945993

946994
for (int ri = 0; ri < route_count; ri++) {
947-
create_route_data_flows(gb, routes[ri], &flows, &skipped);
995+
create_route_data_flows(gb, routes[ri], &st);
948996
}
949997

950-
if (flows > 0 || skipped > 0) {
998+
if (st.flows > 0 || st.skipped > 0 || st.test_scoped > 0) {
951999
char buf1[CBM_SZ_16];
9521000
char buf2[CBM_SZ_16];
953-
snprintf(buf1, sizeof(buf1), "%d", flows);
954-
snprintf(buf2, sizeof(buf2), "%d", skipped);
955-
cbm_log_info("pass.data_flows", "created", buf1, "skipped_has_call", buf2);
1001+
char buf3[CBM_SZ_16];
1002+
snprintf(buf1, sizeof(buf1), "%d", st.flows);
1003+
snprintf(buf2, sizeof(buf2), "%d", st.skipped);
1004+
snprintf(buf3, sizeof(buf3), "%d", st.test_scoped);
1005+
cbm_log_info("pass.data_flows", "created", buf1, "skipped_has_call", buf2,
1006+
"skipped_test_scope", buf3);
9561007
}
9571008
}
9581009

‎tests/test_edge_types_probe.c‎

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

1211+
/* The exact DATA_FLOWS set, caller -> handler by QN tail. route_qns (NULL-terminated) must
1212+
* exist, so a test that expects NO flow on a path cannot pass because the caller's or the
1213+
* handler's Route was never created; handles is the exact HANDLES count. */
1214+
typedef struct {
1215+
const char *caller_tail;
1216+
const char *handler_tail;
1217+
} EtFlow;
1218+
1219+
static int et_flow_matches(cbm_store_t *store, const cbm_edge_t *e, const EtFlow *want,
1220+
int wanted, int *found) {
1221+
cbm_node_t src = {0};
1222+
cbm_node_t tgt = {0};
1223+
int have_src = cbm_store_find_node_by_id(store, e->source_id, &src) == CBM_STORE_OK;
1224+
int have_tgt = cbm_store_find_node_by_id(store, e->target_id, &tgt) == CBM_STORE_OK;
1225+
int matched = 0;
1226+
for (int wi = 0; have_src && have_tgt && wi < wanted; wi++) {
1227+
if (!found[wi] && et_qn_has_tail(src.qualified_name, want[wi].caller_tail) &&
1228+
et_qn_has_tail(tgt.qualified_name, want[wi].handler_tail)) {
1229+
found[wi] = matched = 1;
1230+
break;
1231+
}
1232+
}
1233+
if (!matched) {
1234+
fprintf(stderr, " [ET-FLOWS] unexpected %s -> %s\n",
1235+
have_src && src.qualified_name ? src.qualified_name : "<?>",
1236+
have_tgt && tgt.qualified_name ? tgt.qualified_name : "<?>");
1237+
}
1238+
if (have_src) cbm_node_free_fields(&src);
1239+
if (have_tgt) cbm_node_free_fields(&tgt);
1240+
return matched;
1241+
}
1242+
1243+
static int et_data_flows_exact(const EtFile *files, int nfiles, const EtFlow *want, int parallel,
1244+
const char *const *route_qns, int handles) {
1245+
EtProj lp;
1246+
cbm_store_t *store =
1247+
parallel ? et_index_parallel(&lp, files, nfiles) : et_index_files(&lp, files, nfiles);
1248+
int wanted = 0;
1249+
int found[ET_ROUTE_ASSERT_MAX] = {0};
1250+
while (want[wanted].caller_tail && wanted < ET_ROUTE_ASSERT_MAX) {
1251+
wanted++;
1252+
}
1253+
cbm_edge_t *edges = NULL;
1254+
int n = 0;
1255+
int ok = store != NULL &&
1256+
cbm_store_find_edges_by_type(store, lp.project, "DATA_FLOWS", &edges, &n) ==
1257+
CBM_STORE_OK &&
1258+
n == wanted;
1259+
for (int i = 0; store && i < n; i++) {
1260+
if (!et_flow_matches(store, &edges[i], want, wanted, found)) {
1261+
ok = 0;
1262+
}
1263+
}
1264+
for (int wi = 0; wi < wanted; wi++) {
1265+
if (!found[wi]) {
1266+
ok = 0;
1267+
fprintf(stderr, " [ET-FLOWS] missing %s -> %s\n", want[wi].caller_tail,
1268+
want[wi].handler_tail);
1269+
}
1270+
}
1271+
for (int ri = 0; route_qns && route_qns[ri]; ri++) {
1272+
cbm_node_t route = {0};
1273+
if (!store ||
1274+
cbm_store_find_node_by_qn(store, lp.project, route_qns[ri], &route) != CBM_STORE_OK) {
1275+
ok = 0;
1276+
fprintf(stderr, " [ET-FLOWS] missing Route %s\n", route_qns[ri]);
1277+
} else {
1278+
cbm_node_free_fields(&route);
1279+
}
1280+
}
1281+
int got_handles = store ? cbm_store_count_edges_by_type(store, lp.project, "HANDLES") : -1;
1282+
if (got_handles != handles) {
1283+
ok = 0;
1284+
fprintf(stderr, " [ET-FLOWS] HANDLES=%d expected %d\n", got_handles, handles);
1285+
}
1286+
if (!ok) {
1287+
fprintf(stderr, " [ET-FLOWS] FAIL (%s path) expected=%d actual=%d\n",
1288+
parallel ? "parallel" : "sequential", wanted, n);
1289+
}
1290+
if (edges) cbm_store_free_edges(edges, n);
1291+
et_cleanup(&lp, store);
1292+
return ok;
1293+
}
1294+
1295+
/* R1 + R2 (user decision, 2026-10-18): DATA_FLOWS pair a test-file caller only with handlers in its
1296+
* own file, and a test-file handler only with callers in its own file. A Spring controller and a
1297+
* mock controller under src/test/ both answer `@RequestMapping` (any method). A production TS
1298+
* client and a TS test (.test.ts) call them with fetch, which names no method, so every caller and
1299+
* handler meets on the same __route__ANY__ node. Only the production client -> production
1300+
* controller pair is a data flow of the application, on the sequential and the parallel path. */
1301+
static const EtFile et_r12_files[] = {
1302+
{"server/OrderController.java",
1303+
"package com.example;\n\n"
1304+
"import org.springframework.web.bind.annotation.RequestMapping;\n"
1305+
"import org.springframework.web.bind.annotation.RestController;\n\n"
1306+
"@RestController\n@RequestMapping(\"/api\")\npublic class OrderController {\n"
1307+
" @RequestMapping(\"/orders\")\n"
1308+
" public String orders() {\n return \"orders\";\n }\n}\n"},
1309+
{"server/src/test/java/com/example/MockPaymentController.java",
1310+
"package com.example;\n\n"
1311+
"import org.springframework.web.bind.annotation.RequestMapping;\n"
1312+
"import org.springframework.web.bind.annotation.RestController;\n\n"
1313+
"@RestController\n@RequestMapping(\"/api\")\npublic class MockPaymentController {\n"
1314+
" @RequestMapping(\"/payments\")\n"
1315+
" public String pay() {\n return \"paid\";\n }\n}\n"},
1316+
{"web/src/api.ts",
1317+
"export async function loadOrders() {\n return fetch(\"/api/orders\");\n}\n\n"
1318+
"export async function loadPayments() {\n return fetch(\"/api/payments\");\n}\n"},
1319+
{"web/src/api.test.ts",
1320+
"export async function testLoadOrders() {\n return fetch(\"/api/orders\");\n}\n"}};
1321+
1322+
static const EtFlow et_r12_flows[] = {{"api.loadOrders", "OrderController.orders"}, {NULL, NULL}};
1323+
1324+
static const char *const et_r12_routes[] = {"__route__ANY__/api/orders",
1325+
"__route__ANY__/api/payments", NULL};
1326+
1327+
TEST(data_flows_test_files_pair_within_their_file_sequential) {
1328+
ASSERT_TRUE(et_data_flows_exact(et_r12_files, 4, et_r12_flows, 0, et_r12_routes, 2));
1329+
PASS();
1330+
}
1331+
1332+
TEST(data_flows_test_files_pair_within_their_file_parallel) {
1333+
ASSERT_TRUE(et_data_flows_exact(et_r12_files, 4, et_r12_flows, 1, et_r12_routes, 2));
1334+
PASS();
1335+
}
1336+
12111337
/* ASP.NET Core MVC (#1692): a GET form action and its POST submit share one
12121338
* method name, so both overloads collapse into one Method node whose merged
12131339
* properties keep only one route. The parallel resolver emitted each
@@ -2737,6 +2863,8 @@ SUITE(edge_types_probe) {
27372863
RUN_TEST(handles_fiber_group_prefix_parallel_issue686);
27382864
RUN_TEST(handles_gin_group_prefix_issue686);
27392865
RUN_TEST(handles_spring_java);
2866+
RUN_TEST(data_flows_test_files_pair_within_their_file_sequential);
2867+
RUN_TEST(data_flows_test_files_pair_within_their_file_parallel);
27402868
RUN_TEST(handles_spring_java_path_attribute_fourth);
27412869
RUN_TEST(handles_spring_kotlin);
27422870
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)