sqlitegraph 2.2.2

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
//! Taint analysis for security and compliance vulnerability detection.
//!
//! This module provides taint propagation algorithms for tracking untrusted data
//! from sources to sinks in directed graphs. Taint analysis is a fundamental technique
//! for security vulnerability detection, enabling identification of:
//!
//! - **SQL injection**: User input reaching SQL query construction
//! - **Cross-site scripting (XSS)**: Untrusted data flowing to HTML output
//! - **Command injection**: User input flowing to shell command execution
//! - **Path traversal**: File names from user input reaching file operations
//! - **LDAP injection**: User input reaching LDAP queries
//!
//! # What is Taint Analysis?
//!
//! Taint analysis marks data from untrusted sources as "tainted" and tracks
//! its flow through the program. If tainted data reaches a security-sensitive
//! sink without proper sanitization, a potential vulnerability is reported.
//!
//! ## Core Concepts
//!
//! - **Source**: Untrusted data entry point (user input, environment variables, network data)
//! - **Sink**: Security-sensitive operation (SQL query, HTML output, command execution)
//! - **Taint propagation**: Tracking flow from sources to sinks via graph reachability
//! - **Vulnerability**: Path exists from source to sink (taint reaches sink)
//!
//! # Algorithm
//!
//! Taint propagation is fundamentally a **reachability problem**:
//!
//! ## Forward Taint Analysis
//! 1. Start from taint source nodes
//! 2. Compute forward reachability using BFS (follow data flow)
//! 3. Check if any sink nodes are in the reachable set
//! 4. Report source-sink paths as vulnerabilities
//!
//! ## Backward Taint Analysis
//! 1. Start from sink node
//! 2. Compute backward reachability using reverse BFS (trace data sources)
//! 3. Find which taint sources can reach the sink
//! 4. Report affecting sources as vulnerabilities
//!
//! Both directions reuse the reachability algorithms from [`reachability`],
//! treating taint analysis as annotated reachability with source/sink semantics.
//!
//! # Complexity
//!
//! - **Time**: O(|V| + |E|) - BFS traversal for reachability
//! - **Space**: O(|V|) - for tainted nodes set and BFS queue
//!
//! Where:
//! - V = number of vertices
//! - E = number of edges
//!
//! # Example
//!
//! ```rust,ignore
//! use sqlitegraph::{
//!     SqliteGraph,
//!     algo::{
//!         propagate_taint_forward,
//!         discover_sources_and_sinks_default,
//!     },
//! };
//!
//! let graph = SqliteGraph::open_in_memory()?;
//! // ... build graph with source/sink annotations ...
//!
//! // Discover sources and sinks automatically
//! let (sources, sinks) = discover_sources_and_sinks_default(&graph)?;
//!
//! // Forward analysis: Find all vulnerabilities
//! let result = propagate_taint_forward(&graph, &sources, &sinks)?;
//!
//! if result.has_vulnerability() {
//!     println!("SECURITY ISSUE: {} tainted nodes reach {} sinks",
//!              result.tainted_nodes.len(),
//!              result.sinks_reached.len());
//!
//!     for (source, sink) in result.sorted_vulnerabilities() {
//!         println!("  Vulnerability: source {} reaches sink {}", source, sink);
//!     }
//! }
//! ```
//!
//! # Source and Sink Detection
//!
//! Sources and sinks can be detected via:
//!
//! 1. **Metadata-based**: Using [`MetadataSourceDetector`] and [`MetadataSinkDetector`]
//!    which check entity `data` field for `"kind": "source"` or `"kind": "sink"`
//! 2. **Custom callbacks**: Implement [`SourceCallback`] and [`SinkCallback`] traits
//!    for domain-specific detection logic
//!
//! # References
//!
//! - Livshits, B., & Lam, M. S. "Finding security vulnerabilities in Java applications
//!   with static analysis." USENIX Security, 2005.
//! - Tripp, O., et al. "Taint propagation with demand-driven flow analysis." ACM TOSEM, 2013.
//!
//! [`reachability`]: crate::algo::reachability

use ahash::{AHashMap, AHashSet};

#[cfg(test)]
use crate::GraphEdge;
use crate::progress::ProgressCallback;
use crate::{GraphEntity, errors::SqliteGraphError, graph::SqliteGraph};

use super::reachability::{can_reach, reachable_from, reverse_reachable_from};

/// Result of taint analysis operation.
///
/// Contains the complete taint propagation result with sources, sinks reached,
/// tainted nodes, and source-sink vulnerability paths.
///
/// # Fields
///
/// - `sources`: Source nodes that were analyzed (taint origins)
/// - `sinks_reached`: Sink nodes that are reachable from sources (vulnerabilities)
/// - `tainted_nodes`: All nodes tainted by source data (forward reachability from sources)
/// - `source_sink_paths`: Vulnerability paths as (source, sink) pairs
/// - `size`: Number of tainted nodes (len of tainted_nodes)
///
/// # Example
///
/// ```rust,ignore
/// let result = propagate_taint_forward(&graph, &sources, &sinks)?;
///
/// println!("Tainted {} nodes from {} sources",
///          result.size, result.sources.len());
///
/// // Check if specific node is tainted
/// if result.is_tainted(node_id) {
///     println!("Node {} is tainted", node_id);
/// }
///
/// // Check for vulnerabilities
/// if result.has_vulnerability() {
///     println!("Found {} source-sink vulnerabilities",
///              result.source_sink_paths.len());
///
///     for (source, sink) in result.sorted_vulnerabilities() {
///         println!("  VULN: source {} -> sink {}", source, sink);
///     }
/// }
/// ```
#[derive(Debug, Clone)]
pub struct TaintResult {
    /// Source nodes that taint originated from
    pub sources: AHashSet<i64>,

    /// Sink nodes that were reached by tainted data (vulnerabilities)
    pub sinks_reached: AHashSet<i64>,

    /// All nodes that are tainted (reachable from any source)
    pub tainted_nodes: AHashSet<i64>,

    /// Source-sink paths that represent vulnerabilities
    pub source_sink_paths: Vec<(i64, i64)>,

    /// Number of tainted nodes
    pub size: usize,
}

impl TaintResult {
    /// Creates a new empty taint result.
    pub fn new() -> Self {
        Self {
            sources: AHashSet::new(),
            sinks_reached: AHashSet::new(),
            tainted_nodes: AHashSet::new(),
            source_sink_paths: Vec::new(),
            size: 0,
        }
    }

    /// Checks if a node is tainted.
    ///
    /// Returns true if the node is in the tainted_nodes set.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if result.is_tainted(node_id) {
    ///     println!("Node {} is tainted", node_id);
    /// }
    /// ```
    pub fn is_tainted(&self, node: i64) -> bool {
        self.tainted_nodes.contains(&node)
    }

    /// Checks if any vulnerabilities were found.
    ///
    /// Returns true if any sink is reachable from any source.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// if result.has_vulnerability() {
    ///     println!("SECURITY ISSUE: tainted data reaches sensitive sink!");
    /// }
    /// ```
    pub fn has_vulnerability(&self) -> bool {
        // A vulnerability exists only if there's at least one source-sink path
        !self.source_sink_paths.is_empty()
    }

    /// Returns sorted list of tainted node IDs.
    ///
    /// Provides deterministic output for testing and reporting.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// for node in result.sorted_tainted_nodes() {
    ///     println!("Tainted node: {}", node);
    /// }
    /// ```
    pub fn sorted_tainted_nodes(&self) -> Vec<i64> {
        let mut nodes: Vec<i64> = self.tainted_nodes.iter().copied().collect();
        nodes.sort();
        nodes
    }

    /// Returns sorted list of vulnerability paths.
    ///
    /// Each vulnerability is a (source, sink) pair.
    /// Sorted by source then sink for deterministic output.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// for (source, sink) in result.sorted_vulnerabilities() {
    ///     println!("VULN: source {} -> sink {}", source, sink);
    /// }
    /// ```
    pub fn sorted_vulnerabilities(&self) -> Vec<(i64, i64)> {
        let mut paths = self.source_sink_paths.clone();
        paths.sort();
        paths
    }
}

impl Default for TaintResult {
    fn default() -> Self {
        Self::new()
    }
}

/// Callback trait for detecting taint sources.
///
/// Implement this trait to define custom source detection logic.
/// The callback receives a node ID and its entity data, returning true
/// if the node is a taint source.
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::algo::{SourceCallback, TaintResult};
/// use sqlitegraph::graph::types::GraphEntity;
///
/// struct HttpParamDetector;
///
/// impl SourceCallback for HttpParamDetector {
///     fn is_source(&self, node: i64, entity: &GraphEntity) -> bool {
///         // Detect HTTP parameter nodes
///         entity.kind == "http_param" ||
///         entity.data["taint"].as_str() == Some("untrusted")
///     }
/// }
/// ```
pub trait SourceCallback {
    /// Checks if a node is a taint source.
    ///
    /// # Arguments
    /// * `node` - Node ID to check
    /// * `entity` - Graph entity containing metadata
    ///
    /// # Returns
    /// true if the node is a taint source, false otherwise
    fn is_source(&self, node: i64, entity: &GraphEntity) -> bool;
}

/// Callback trait for detecting security-sensitive sinks.
///
/// Implement this trait to define custom sink detection logic.
/// The callback receives a node ID and its entity data, returning true
/// if the node is a security-sensitive sink.
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::algo::{SinkCallback, TaintResult};
/// use sqlitegraph::graph::types::GraphEntity;
///
/// struct SqlQueryDetector;
///
/// impl SinkCallback for SqlQueryDetector {
///     fn is_sink(&self, node: i64, entity: &GraphEntity) -> bool {
///         // Detect SQL query execution nodes
///         entity.kind == "sql_execute" ||
///         entity.data["operation"].as_str() == Some("query")
///     }
/// }
/// ```
pub trait SinkCallback {
    /// Checks if a node is a security-sensitive sink.
    ///
    /// # Arguments
    /// * `node` - Node ID to check
    /// * `entity` - Graph entity containing metadata
    ///
    /// # Returns
    /// true if the node is a sink, false otherwise
    fn is_sink(&self, node: i64, entity: &GraphEntity) -> bool;
}

/// Default source detector using entity metadata.
///
/// Checks for common source indicators in entity data field:
/// - `"kind": "source"`
/// - `"kind": "untrusted"`
/// - `"kind": "user_input"`
/// - `"taint": "source"`
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::algo::{discover_sources_and_sinks, MetadataSourceDetector};
///
/// let (sources, sinks) = discover_sources_and_sinks(
///     &graph,
///     &MetadataSourceDetector,
///     &MetadataSinkDetector,
/// )?;
/// ```
pub struct MetadataSourceDetector;

impl SourceCallback for MetadataSourceDetector {
    fn is_source(&self, _node: i64, entity: &GraphEntity) -> bool {
        // Check data.kind field
        if let Some(kind) = entity.data.get("kind").and_then(|k| k.as_str()) {
            if matches!(kind, "source" | "untrusted" | "user_input") {
                return true;
            }
        }

        // Check data.taint field
        if let Some(taint) = entity.data.get("taint").and_then(|t| t.as_str()) {
            if taint == "source" {
                return true;
            }
        }

        false
    }
}

/// Default sink detector using entity metadata.
///
/// Checks for common sink indicators in entity data field:
/// - `"kind": "sink"`
/// - `"kind": "sql_query"`
/// - `"kind": "html_output"`
/// - `"kind": "command"`
/// - `"operation": "execute"`
/// - `"operation": "query"`
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::algo::{discover_sources_and_sinks, MetadataSinkDetector};
///
/// let (sources, sinks) = discover_sources_and_sinks(
///     &graph,
///     &MetadataSourceDetector,
///     &MetadataSinkDetector,
/// )?;
/// ```
pub struct MetadataSinkDetector;

impl SinkCallback for MetadataSinkDetector {
    fn is_sink(&self, _node: i64, entity: &GraphEntity) -> bool {
        // Check data.kind field
        if let Some(kind) = entity.data.get("kind").and_then(|k| k.as_str()) {
            if matches!(
                kind,
                "sink" | "sql_query" | "html_output" | "command" | "file_operation"
            ) {
                return true;
            }
        }

        // Check data.operation field
        if let Some(operation) = entity.data.get("operation").and_then(|o| o.as_str()) {
            if matches!(operation, "execute" | "query" | "render" | "write") {
                return true;
            }
        }

        false
    }
}

/// Propagates taint forward from sources to find reachable sinks.
///
/// Computes all nodes reachable from taint sources and identifies which
/// security-sensitive sinks are reachable (vulnerabilities).
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `sources` - Source node IDs where taint originates
/// * `sinks` - Sink node IDs to check for taint reachability
///
/// # Returns
/// TaintResult containing:
/// - All nodes tainted by sources (forward reachable)
/// - Which sinks are reachable (vulnerabilities)
/// - Source-sink paths that represent vulnerabilities
///
/// # Complexity
/// - **Time**: O(S × (V + E) + S × Sinks × (V + E)) where S = sources count
///   - O(V + E) per source for forward reachability
///   - O(V + E) per source-sink pair for path validation
/// - **Space**: O(V) for tainted nodes set
///
/// # Algorithm
/// 1. Initialize tainted_nodes = empty set
/// 2. For each source:
///    - Compute forward reachability using reachable_from()
///    - Extend tainted_nodes with reachable nodes
/// 3. Compute sinks_reached = sinks ∩ tainted_nodes
/// 4. Build source_sink_paths:
///    - For each source and sink pair, check can_reach(source, sink)
///    - If true, add (source, sink) to paths
/// 5. Return TaintResult with size = tainted_nodes.len()
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{SqliteGraph, algo::propagate_taint_forward};
///
/// let graph = SqliteGraph::open_in_memory()?;
/// // ... build graph with sources and sinks ...
///
/// let sources = vec![1, 2];   // User input nodes
/// let sinks = vec![10, 20];   // SQL query nodes
///
/// let result = propagate_taint_forward(&graph, &sources, &sinks)?;
///
/// if result.has_vulnerability() {
///     println!("Found {} vulnerabilities", result.source_sink_paths.len());
///     for (source, sink) in result.sorted_vulnerabilities() {
///         println!("  Source {} can reach sink {}", source, sink);
///     }
/// }
/// ```
pub fn propagate_taint_forward(
    graph: &SqliteGraph,
    sources: &[i64],
    sinks: &[i64],
) -> Result<TaintResult, SqliteGraphError> {
    let mut tainted_nodes: AHashSet<i64> = AHashSet::new();
    let sources_set: AHashSet<i64> = sources.iter().copied().collect();
    let sinks_set: AHashSet<i64> = sinks.iter().copied().collect();

    // Step 1: Propagate taint from each source
    for &source in sources {
        let reachable = reachable_from(graph, source)?;
        tainted_nodes.extend(reachable);
    }

    // Step 2: Find which sinks are reachable (vulnerabilities)
    let sinks_reached: AHashSet<i64> = sinks_set.intersection(&tainted_nodes).copied().collect();

    // Step 3: Build source-sink paths
    let mut source_sink_paths = Vec::new();
    for &source in sources {
        for &sink in &sinks_reached {
            if can_reach(graph, source, sink)? {
                source_sink_paths.push((source, sink));
            }
        }
    }

    let size = tainted_nodes.len();

    Ok(TaintResult {
        sources: sources_set,
        sinks_reached,
        tainted_nodes,
        source_sink_paths,
        size,
    })
}

/// Propagates taint forward with progress tracking.
///
/// Same algorithm as [`propagate_taint_forward`] but reports progress
/// during execution. Useful for long-running operations on large graphs.
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `sources` - Source node IDs where taint originates
/// * `sinks` - Sink node IDs to check for taint reachability
/// * `progress` - Progress callback for reporting execution status
///
/// # Returns
/// TaintResult with tainted nodes and vulnerability paths.
///
/// # Progress Reporting
///
/// The callback receives:
/// - `current`: Current number of sources processed
/// - `total`: Total number of sources (Some(total))
/// - `message`: "Taint propagation: {current}/{total} sources processed, {tainted} tainted nodes"
///
/// Progress is reported for each source processed.
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{
///     algo::propagate_taint_forward_with_progress,
///     progress::ConsoleProgress
/// };
///
/// let progress = ConsoleProgress::new();
/// let result = propagate_taint_forward_with_progress(&graph, &sources, &sinks, &progress)?;
/// // Output: Taint propagation: 1/5 sources processed, 10 tainted nodes
/// ```
pub fn propagate_taint_forward_with_progress<F>(
    graph: &SqliteGraph,
    sources: &[i64],
    sinks: &[i64],
    progress: &F,
) -> Result<TaintResult, SqliteGraphError>
where
    F: ProgressCallback,
{
    let mut tainted_nodes: AHashSet<i64> = AHashSet::new();
    let sources_set: AHashSet<i64> = sources.iter().copied().collect();
    let sinks_set: AHashSet<i64> = sinks.iter().copied().collect();
    let total = sources.len();

    // Step 1: Propagate taint from each source with progress
    for (idx, &source) in sources.iter().enumerate() {
        let reachable = reachable_from(graph, source)?;
        tainted_nodes.extend(reachable);

        // Report progress
        progress.on_progress(
            idx + 1,
            Some(total),
            &format!(
                "Taint propagation: {}/{} sources processed, {} tainted nodes",
                idx + 1,
                total,
                tainted_nodes.len()
            ),
        );
    }

    // Step 2: Find which sinks are reachable (vulnerabilities)
    let sinks_reached: AHashSet<i64> = sinks_set.intersection(&tainted_nodes).copied().collect();

    // Step 3: Build source-sink paths
    let mut source_sink_paths = Vec::new();
    for &source in sources {
        for &sink in &sinks_reached {
            if can_reach(graph, source, sink)? {
                source_sink_paths.push((source, sink));
            }
        }
    }

    // Report completion
    progress.on_complete();

    let size = tainted_nodes.len();

    Ok(TaintResult {
        sources: sources_set,
        sinks_reached,
        tainted_nodes,
        source_sink_paths,
        size,
    })
}

/// Propagates taint backward from a sink to find affecting sources.
///
/// Computes all nodes that can reach the sink (ancestors) and identifies
/// which taint sources can influence the sink.
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `sink` - The sink node ID to trace back from
/// * `sources` - Source node IDs to check for influence
///
/// # Returns
/// TaintResult containing:
/// - sources: Sources that can reach the sink (affecting sources)
/// - sinks_reached: Contains only the input sink
/// - tainted_nodes: All ancestors that can reach the sink
/// - source_sink_paths: (affecting_source, sink) pairs
///
/// # Complexity
/// - **Time**: O(V + E) for backward reachability + O(S) for source intersection
///   where S = sources count
/// - **Space**: O(V) for ancestors set
///
/// # Algorithm
/// 1. Call reverse_reachable_from(graph, sink) to get ancestors
/// 2. Find affecting_sources = sources ∩ ancestors
/// 3. Build source_sink_paths: (source, sink) for each affecting source
/// 4. Return TaintResult with:
///    - sources = affecting_sources
///    - sinks_reached = {sink}
///    - tainted_nodes = ancestors
///    - source_sink_paths = (affecting_source, sink) for each
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{SqliteGraph, algo::propagate_taint_backward};
///
/// let graph = SqliteGraph::open_in_memory()?;
/// // ... build graph with sources and sinks ...
///
/// let sources = vec![1, 2, 3];   // User input nodes
/// let sink = 10;                 // SQL query node
///
/// let result = propagate_taint_backward(&graph, sink, &sources)?;
///
/// println!("Sink {} is affected by {} sources",
///          sink, result.sources.len());
/// for &source in &result.sources {
///     println!("  Source {} can reach sink {}", source, sink);
/// }
/// ```
pub fn propagate_taint_backward(
    graph: &SqliteGraph,
    sink: i64,
    sources: &[i64],
) -> Result<TaintResult, SqliteGraphError> {
    let sources_set: AHashSet<i64> = sources.iter().copied().collect();

    // Step 1: Find all ancestors that can reach the sink
    let ancestors = reverse_reachable_from(graph, sink)?;

    // Step 2: Find which sources can reach the sink
    let affecting_sources: AHashSet<i64> = sources_set.intersection(&ancestors).copied().collect();

    // Step 3: Build source-sink paths
    let source_sink_paths: Vec<(i64, i64)> = affecting_sources
        .iter()
        .map(|&source| (source, sink))
        .collect();

    // Step 4: Build result
    let mut sinks_reached = AHashSet::new();
    sinks_reached.insert(sink);

    let size = ancestors.len();

    Ok(TaintResult {
        sources: affecting_sources,
        sinks_reached,
        tainted_nodes: ancestors,
        source_sink_paths,
        size,
    })
}

/// Propagates taint backward with progress tracking.
///
/// Same algorithm as [`propagate_taint_backward`] but reports progress
/// during execution.
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `sink` - The sink node ID to trace back from
/// * `sources` - Source node IDs to check for influence
/// * `progress` - Progress callback for reporting execution status
///
/// # Returns
/// TaintResult with affecting sources and ancestors.
///
/// # Progress Reporting
///
/// The callback receives:
/// - `current`: Always 1 (single operation)
/// - `total`: Always 1
/// - `message`: "Backward taint propagation: from sink {sink}, {affecting} sources found"
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{
///     algo::propagate_taint_backward_with_progress,
///     progress::ConsoleProgress
/// };
///
/// let progress = ConsoleProgress::new();
/// let result = propagate_taint_backward_with_progress(&graph, sink, &sources, &progress)?;
/// // Output: Backward taint propagation: from sink 10, 2 sources found
/// ```
pub fn propagate_taint_backward_with_progress<F>(
    graph: &SqliteGraph,
    sink: i64,
    sources: &[i64],
    progress: &F,
) -> Result<TaintResult, SqliteGraphError>
where
    F: ProgressCallback,
{
    let sources_set: AHashSet<i64> = sources.iter().copied().collect();

    // Step 1: Find all ancestors that can reach the sink
    let ancestors = reverse_reachable_from(graph, sink)?;

    // Step 2: Find which sources can reach the sink
    let affecting_sources: AHashSet<i64> = sources_set.intersection(&ancestors).copied().collect();

    // Report progress
    progress.on_progress(
        1,
        Some(1),
        &format!(
            "Backward taint propagation: from sink {}, {} sources found",
            sink,
            affecting_sources.len()
        ),
    );

    // Step 3: Build source-sink paths
    let source_sink_paths: Vec<(i64, i64)> = affecting_sources
        .iter()
        .map(|&source| (source, sink))
        .collect();

    // Step 4: Build result
    let mut sinks_reached = AHashSet::new();
    sinks_reached.insert(sink);

    // Report completion
    progress.on_complete();

    let size = ancestors.len();

    Ok(TaintResult {
        sources: affecting_sources,
        sinks_reached,
        tainted_nodes: ancestors,
        source_sink_paths,
        size,
    })
}

/// Performs sink reachability analysis for all sinks.
///
/// Analyzes each sink to determine which taint sources can reach it.
/// Returns a mapping of vulnerable sinks to their affecting sources.
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `sources` - Source node IDs where taint originates
/// * `sinks` - Sink node IDs to analyze
///
/// # Returns
/// HashMap mapping vulnerable sink -> Vec<affecting_sources>
/// Only includes sinks that have at least one affecting source.
///
/// # Complexity
/// - **Time**: O(Sinks × (V + E)) - one backward BFS per sink
/// - **Space**: O(V) for ancestors set (per sink)
///
/// # Algorithm
/// 1. Initialize result = empty HashMap
/// 2. For each sink:
///    - Call propagate_taint_backward(graph, sink, sources)
///    - If result.sources is non-empty (vulnerable):
///      - Add entry: sink -> Vec<affecting_sources>
/// 3. Return result mapping
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{SqliteGraph, algo::sink_reachability_analysis};
///
/// let graph = SqliteGraph::open_in_memory()?;
/// // ... build graph with sources and sinks ...
///
/// let sources = vec![1, 2, 3];   // User input nodes
/// let sinks = vec![10, 20, 30];  // SQL query nodes
///
/// let vulnerabilities = sink_reachability_analysis(&graph, &sources, &sinks)?;
///
/// if vulnerabilities.is_empty() {
///     println!("No vulnerabilities found!");
/// } else {
///     println!("Found {} vulnerable sinks:", vulnerabilities.len());
///     for (sink, affecting_sources) in vulnerabilities {
///         println!("  Sink {} is reachable from sources: {:?}", sink, affecting_sources);
///     }
/// }
/// ```
pub fn sink_reachability_analysis(
    graph: &SqliteGraph,
    sources: &[i64],
    sinks: &[i64],
) -> Result<AHashMap<i64, Vec<i64>>, SqliteGraphError> {
    let mut result: AHashMap<i64, Vec<i64>> = AHashMap::new();

    for &sink in sinks {
        let taint_result = propagate_taint_backward(graph, sink, sources)?;

        // Only include sinks that have affecting sources (vulnerabilities)
        if !taint_result.sources.is_empty() {
            let affecting_sources: Vec<i64> = taint_result.sources.iter().copied().collect();
            result.insert(sink, affecting_sources);
        }
    }

    Ok(result)
}

/// Performs sink reachability analysis with progress tracking.
///
/// Same algorithm as [`sink_reachability_analysis`] but reports progress
/// during execution. Useful for analyzing many sinks.
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `sources` - Source node IDs where taint originates
/// * `sinks` - Sink node IDs to analyze
/// * `progress` - Progress callback for reporting execution status
///
/// # Returns
/// HashMap mapping vulnerable sink -> Vec<affecting_sources>
///
/// # Progress Reporting
///
/// The callback receives:
/// - `current`: Current sink being analyzed
/// - `total`: Total number of sinks (Some(total))
/// - `message`: "Sink reachability: {current}/{total} sinks analyzed, {vulns} vulnerabilities found"
///
/// Progress is reported for each sink analyzed.
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{
///     algo::sink_reachability_analysis_with_progress,
///     progress::ConsoleProgress
/// };
///
/// let progress = ConsoleProgress::new();
/// let vulnerabilities = sink_reachability_analysis_with_progress(
///     &graph, &sources, &sinks, &progress
/// )?;
/// // Output: Sink reachability: 1/10 sinks analyzed, 1 vulnerabilities found
/// ```
pub fn sink_reachability_analysis_with_progress<F>(
    graph: &SqliteGraph,
    sources: &[i64],
    sinks: &[i64],
    progress: &F,
) -> Result<AHashMap<i64, Vec<i64>>, SqliteGraphError>
where
    F: ProgressCallback,
{
    let mut result: AHashMap<i64, Vec<i64>> = AHashMap::new();
    let total = sinks.len();

    for (idx, &sink) in sinks.iter().enumerate() {
        let taint_result = propagate_taint_backward(graph, sink, sources)?;

        // Only include sinks that have affecting sources (vulnerabilities)
        if !taint_result.sources.is_empty() {
            let affecting_sources: Vec<i64> = taint_result.sources.iter().copied().collect();
            result.insert(sink, affecting_sources);
        }

        // Report progress
        progress.on_progress(
            idx + 1,
            Some(total),
            &format!(
                "Sink reachability: {}/{} sinks analyzed, {} vulnerabilities found",
                idx + 1,
                total,
                result.len()
            ),
        );
    }

    // Report completion
    progress.on_complete();

    Ok(result)
}

/// Discovers all sources and sinks in the graph using custom callbacks.
///
/// Iterates through all nodes in the graph and applies the provided
/// callbacks to identify taint sources and security-sensitive sinks.
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `source_detector` - Callback for detecting sources
/// * `sink_detector` - Callback for detecting sinks
///
/// # Returns
/// Tuple of (sources, sinks) where each is Vec<i64> of node IDs.
///
/// # Complexity
/// - **Time**: O(V) - visits each node once
/// - **Space**: O(V) for storing sources and sinks lists
///
/// # Algorithm
/// 1. Get all nodes via graph.all_entity_ids()
/// 2. For each node:
///    - Fetch entity from graph
///    - Check source_detector.is_source(), append to sources if true
///    - Check sink_detector.is_sink(), append to sinks if true
/// 3. Return (sources, sinks) as Vec<i64>
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{
///     SqliteGraph,
///     algo::{discover_sources_and_sinks, MetadataSourceDetector, MetadataSinkDetector}
/// };
///
/// let graph = SqliteGraph::open_in_memory()?;
/// // ... build graph with metadata annotations ...
///
/// // Use default metadata-based detectors
/// let (sources, sinks) = discover_sources_and_sinks(
///     &graph,
///     &MetadataSourceDetector,
///     &MetadataSinkDetector,
/// )?;
///
/// println!("Found {} sources and {} sinks", sources.len(), sinks.len());
/// ```
pub fn discover_sources_and_sinks(
    graph: &SqliteGraph,
    source_detector: &impl SourceCallback,
    sink_detector: &impl SinkCallback,
) -> Result<(Vec<i64>, Vec<i64>), SqliteGraphError> {
    let mut sources = Vec::new();
    let mut sinks = Vec::new();

    // Get all nodes in the graph
    let all_ids = graph.all_entity_ids()?;

    for node_id in all_ids {
        // Fetch entity for this node
        let entity = graph.get_entity(node_id)?;

        // Check if this is a source
        if source_detector.is_source(node_id, &entity) {
            sources.push(node_id);
        }

        // Check if this is a sink
        if sink_detector.is_sink(node_id, &entity) {
            sinks.push(node_id);
        }
    }

    Ok((sources, sinks))
}

/// Discovers sources and sinks using default metadata-based detectors.
///
/// Convenience function that uses [`MetadataSourceDetector`] and
/// [`MetadataSinkDetector`] to find sources and sinks based on
/// entity metadata annotations.
///
/// # Arguments
/// * `graph` - The graph to analyze
///
/// # Returns
/// Tuple of (sources, sinks) where each is Vec<i64> of node IDs.
///
/// # Metadata Format
///
/// Sources are detected by:
/// - `"kind": "source"` or `"kind": "untrusted"` or `"kind": "user_input"`
/// - `"taint": "source"`
///
/// Sinks are detected by:
/// - `"kind": "sink"` or `"kind": "sql_query"` or `"kind": "html_output"` or `"kind": "command"`
/// - `"operation": "execute"` or `"operation": "query"` or `"operation": "render"` or `"operation": "write"`
///
/// # Complexity
/// - **Time**: O(V) - visits each node once
/// - **Space**: O(V) for storing sources and sinks lists
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{SqliteGraph, algo::discover_sources_and_sinks_default};
///
/// let graph = SqliteGraph::open_in_memory()?;
///
/// // Add source with metadata
/// graph.insert_entity(&GraphEntity {
///     id: 1,
///     kind: "variable".to_string(),
///     name: "user_input".to_string(),
///     file_path: None,
///     data: json!({"kind": "source", "taint": "untrusted"}),
/// })?;
///
/// // Add sink with metadata
/// graph.insert_entity(&GraphEntity {
///     id: 2,
///     kind: "operation".to_string(),
///     name: "sql_execute".to_string(),
///     file_path: None,
///     data: json!({"kind": "sql_query", "operation": "execute"}),
/// })?;
///
/// // Auto-discover sources and sinks
/// let (sources, sinks) = discover_sources_and_sinks_default(&graph)?;
///
/// assert_eq!(sources, vec![1]);
/// assert_eq!(sinks, vec![2]);
/// ```
pub fn discover_sources_and_sinks_default(
    graph: &SqliteGraph,
) -> Result<(Vec<i64>, Vec<i64>), SqliteGraphError> {
    discover_sources_and_sinks(graph, &MetadataSourceDetector, &MetadataSinkDetector)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    use crate::GraphEntity;

    // Test helper: create a simple entity with metadata
    fn create_test_entity(id: i64, kind: &str, data: serde_json::Value) -> GraphEntity {
        GraphEntity {
            id,
            kind: kind.to_string(),
            name: format!("node_{}", id),
            file_path: None,
            data,
        }
    }

    #[test]
    fn test_metadata_source_detector_kind_source() {
        let detector = MetadataSourceDetector;
        let entity = create_test_entity(1, "variable", json!({"kind": "source"}));

        assert!(detector.is_source(1, &entity));
    }

    #[test]
    fn test_metadata_source_detector_kind_untrusted() {
        let detector = MetadataSourceDetector;
        let entity = create_test_entity(1, "variable", json!({"kind": "untrusted"}));

        assert!(detector.is_source(1, &entity));
    }

    #[test]
    fn test_metadata_source_detector_kind_user_input() {
        let detector = MetadataSourceDetector;
        let entity = create_test_entity(1, "variable", json!({"kind": "user_input"}));

        assert!(detector.is_source(1, &entity));
    }

    #[test]
    fn test_metadata_source_detector_taint_field() {
        let detector = MetadataSourceDetector;
        let entity = create_test_entity(1, "variable", json!({"taint": "source"}));

        assert!(detector.is_source(1, &entity));
    }

    #[test]
    fn test_metadata_source_detector_not_a_source() {
        let detector = MetadataSourceDetector;
        let entity = create_test_entity(1, "variable", json!({"kind": "sanitized"}));

        assert!(!detector.is_source(1, &entity));
    }

    #[test]
    fn test_metadata_sink_detector_kind_sink() {
        let detector = MetadataSinkDetector;
        let entity = create_test_entity(1, "operation", json!({"kind": "sink"}));

        assert!(detector.is_sink(1, &entity));
    }

    #[test]
    fn test_metadata_sink_detector_kind_sql_query() {
        let detector = MetadataSinkDetector;
        let entity = create_test_entity(1, "operation", json!({"kind": "sql_query"}));

        assert!(detector.is_sink(1, &entity));
    }

    #[test]
    fn test_metadata_sink_detector_kind_html_output() {
        let detector = MetadataSinkDetector;
        let entity = create_test_entity(1, "operation", json!({"kind": "html_output"}));

        assert!(detector.is_sink(1, &entity));
    }

    #[test]
    fn test_metadata_sink_detector_kind_command() {
        let detector = MetadataSinkDetector;
        let entity = create_test_entity(1, "operation", json!({"kind": "command"}));

        assert!(detector.is_sink(1, &entity));
    }

    #[test]
    fn test_metadata_sink_detector_operation_execute() {
        let detector = MetadataSinkDetector;
        let entity = create_test_entity(1, "operation", json!({"operation": "execute"}));

        assert!(detector.is_sink(1, &entity));
    }

    #[test]
    fn test_metadata_sink_detector_operation_query() {
        let detector = MetadataSinkDetector;
        let entity = create_test_entity(1, "operation", json!({"operation": "query"}));

        assert!(detector.is_sink(1, &entity));
    }

    #[test]
    fn test_metadata_sink_detector_operation_render() {
        let detector = MetadataSinkDetector;
        let entity = create_test_entity(1, "operation", json!({"operation": "render"}));

        assert!(detector.is_sink(1, &entity));
    }

    #[test]
    fn test_metadata_sink_detector_not_a_sink() {
        let detector = MetadataSinkDetector;
        let entity = create_test_entity(1, "operation", json!({"operation": "validate"}));

        assert!(!detector.is_sink(1, &entity));
    }

    #[test]
    fn test_taint_result_new() {
        let result = TaintResult::new();

        assert!(result.sources.is_empty());
        assert!(result.sinks_reached.is_empty());
        assert!(result.tainted_nodes.is_empty());
        assert!(result.source_sink_paths.is_empty());
        assert_eq!(result.size, 0);
    }

    #[test]
    fn test_taint_result_default() {
        let result = TaintResult::default();

        assert!(result.sources.is_empty());
        assert!(result.sinks_reached.is_empty());
        assert!(result.tainted_nodes.is_empty());
        assert!(result.source_sink_paths.is_empty());
        assert_eq!(result.size, 0);
    }

    #[test]
    fn test_taint_result_is_tainted() {
        let mut result = TaintResult::new();
        result.tainted_nodes.insert(1);
        result.tainted_nodes.insert(5);
        result.tainted_nodes.insert(10);

        assert!(result.is_tainted(1));
        assert!(result.is_tainted(5));
        assert!(result.is_tainted(10));
        assert!(!result.is_tainted(99));
    }

    #[test]
    fn test_taint_result_has_vulnerability() {
        let mut result = TaintResult::new();

        // No vulnerability initially (no source-sink paths)
        assert!(!result.has_vulnerability());

        // Add a source-sink path (represents a vulnerability)
        result.source_sink_paths.push((1, 5));
        assert!(result.has_vulnerability());
    }

    #[test]
    fn test_taint_result_sorted_tainted_nodes() {
        let mut result = TaintResult::new();
        result.tainted_nodes.insert(10);
        result.tainted_nodes.insert(1);
        result.tainted_nodes.insert(5);
        result.tainted_nodes.insert(3);

        let sorted = result.sorted_tainted_nodes();
        assert_eq!(sorted, vec![1, 3, 5, 10]);
    }

    #[test]
    fn test_taint_result_sorted_vulnerabilities() {
        let mut result = TaintResult::new();
        result.source_sink_paths = vec![(5, 10), (1, 3), (3, 5), (1, 10)];

        let sorted = result.sorted_vulnerabilities();
        assert_eq!(sorted, vec![(1, 3), (1, 10), (3, 5), (5, 10)]);
    }

    // Test helpers for creating graphs with taint flow
    fn create_linear_flow_graph() -> SqliteGraph {
        // Create: source -> 2 -> 3 -> 4 -> sink
        let graph = SqliteGraph::open_in_memory().unwrap();

        // Add entities
        graph
            .insert_entity(&GraphEntity {
                id: 1,
                kind: "variable".to_string(),
                name: "source".to_string(),
                file_path: None,
                data: json!({"kind": "source"}),
            })
            .unwrap();

        for i in 2..=4 {
            graph
                .insert_entity(&GraphEntity {
                    id: i,
                    kind: "variable".to_string(),
                    name: format!("node_{}", i),
                    file_path: None,
                    data: json!({}),
                })
                .unwrap();
        }

        graph
            .insert_entity(&GraphEntity {
                id: 5,
                kind: "operation".to_string(),
                name: "sink".to_string(),
                file_path: None,
                data: json!({"kind": "sink"}),
            })
            .unwrap();

        // Add edges: 1 -> 2 -> 3 -> 4 -> 5
        for i in 1..5 {
            graph
                .insert_edge(&GraphEdge {
                    id: 0,
                    from_id: i,
                    to_id: i + 1,
                    edge_type: "data_flow".to_string(),
                    data: json!({}),
                })
                .unwrap();
        }

        graph
    }

    fn create_vulnerable_flow_graph() -> SqliteGraph {
        // Source -> intermediate -> sink (vulnerability exists)
        let graph = SqliteGraph::open_in_memory().unwrap();

        graph
            .insert_entity(&GraphEntity {
                id: 1,
                kind: "variable".to_string(),
                name: "user_input".to_string(),
                file_path: None,
                data: json!({"kind": "source"}),
            })
            .unwrap();

        graph
            .insert_entity(&GraphEntity {
                id: 2,
                kind: "variable".to_string(),
                name: "intermediate".to_string(),
                file_path: None,
                data: json!({}),
            })
            .unwrap();

        graph
            .insert_entity(&GraphEntity {
                id: 3,
                kind: "operation".to_string(),
                name: "sql_execute".to_string(),
                file_path: None,
                data: json!({"kind": "sql_query", "operation": "execute"}),
            })
            .unwrap();

        // Add edges: 1 -> 2 -> 3
        graph
            .insert_edge(&GraphEdge {
                id: 0,
                from_id: 1,
                to_id: 2,
                edge_type: "data_flow".to_string(),
                data: json!({}),
            })
            .unwrap();
        graph
            .insert_edge(&GraphEdge {
                id: 0,
                from_id: 2,
                to_id: 3,
                edge_type: "data_flow".to_string(),
                data: json!({}),
            })
            .unwrap();

        graph
    }

    fn create_safe_flow_graph() -> SqliteGraph {
        // Source -> sanitize (not a sink), separate sink (no vulnerability)
        let graph = SqliteGraph::open_in_memory().unwrap();

        graph
            .insert_entity(&GraphEntity {
                id: 1,
                kind: "variable".to_string(),
                name: "user_input".to_string(),
                file_path: None,
                data: json!({"kind": "source"}),
            })
            .unwrap();

        graph
            .insert_entity(&GraphEntity {
                id: 2,
                kind: "operation".to_string(),
                name: "sanitize".to_string(),
                file_path: None,
                data: json!({"operation": "sanitize"}), // Not a sink
            })
            .unwrap();

        graph
            .insert_entity(&GraphEntity {
                id: 3,
                kind: "operation".to_string(),
                name: "sql_execute".to_string(),
                file_path: None,
                data: json!({"kind": "sql_query"}),
            })
            .unwrap();

        // Add edges: 1 -> 2 (sanitize), but 2 doesn't reach 3
        graph
            .insert_edge(&GraphEdge {
                id: 0,
                from_id: 1,
                to_id: 2,
                edge_type: "data_flow".to_string(),
                data: json!({}),
            })
            .unwrap();

        graph
    }

    fn create_multi_source_sink_graph() -> SqliteGraph {
        // source1 -> \
        //              -> intermediate -> sink
        // source2 -> /
        let graph = SqliteGraph::open_in_memory().unwrap();

        // Two sources
        graph
            .insert_entity(&GraphEntity {
                id: 1,
                kind: "variable".to_string(),
                name: "source1".to_string(),
                file_path: None,
                data: json!({"kind": "source"}),
            })
            .unwrap();

        graph
            .insert_entity(&GraphEntity {
                id: 2,
                kind: "variable".to_string(),
                name: "source2".to_string(),
                file_path: None,
                data: json!({"kind": "untrusted"}),
            })
            .unwrap();

        // Intermediate node
        graph
            .insert_entity(&GraphEntity {
                id: 3,
                kind: "variable".to_string(),
                name: "intermediate".to_string(),
                file_path: None,
                data: json!({}),
            })
            .unwrap();

        // Sink
        graph
            .insert_entity(&GraphEntity {
                id: 4,
                kind: "operation".to_string(),
                name: "sink".to_string(),
                file_path: None,
                data: json!({"kind": "sink"}),
            })
            .unwrap();

        // Edges: both sources reach intermediate, intermediate reaches sink
        graph
            .insert_edge(&GraphEdge {
                id: 0,
                from_id: 1,
                to_id: 3,
                edge_type: "data_flow".to_string(),
                data: json!({}),
            })
            .unwrap();
        graph
            .insert_edge(&GraphEdge {
                id: 0,
                from_id: 2,
                to_id: 3,
                edge_type: "data_flow".to_string(),
                data: json!({}),
            })
            .unwrap();
        graph
            .insert_edge(&GraphEdge {
                id: 0,
                from_id: 3,
                to_id: 4,
                edge_type: "data_flow".to_string(),
                data: json!({}),
            })
            .unwrap();

        graph
    }

    // Tests for forward propagation

    #[test]
    fn test_propagate_taint_forward_vulnerable() {
        let graph = create_vulnerable_flow_graph();
        let sources = vec![1];
        let sinks = vec![3];

        let result = propagate_taint_forward(&graph, &sources, &sinks).unwrap();

        // Source 1 reaches sink 3 - vulnerability!
        assert!(result.has_vulnerability());
        assert_eq!(result.sinks_reached.len(), 1);
        assert!(result.sinks_reached.contains(&3));
        assert_eq!(result.source_sink_paths.len(), 1);
        assert_eq!(result.source_sink_paths[0], (1, 3));
        assert!(result.is_tainted(1));
        assert!(result.is_tainted(2));
        assert!(result.is_tainted(3));
    }

    #[test]
    fn test_propagate_taint_forward_safe() {
        let graph = create_safe_flow_graph();
        let sources = vec![1];
        let sinks = vec![3];

        let result = propagate_taint_forward(&graph, &sources, &sinks).unwrap();

        // Source 1 does NOT reach sink 3 - no vulnerability
        assert!(!result.has_vulnerability());
        assert_eq!(result.sinks_reached.len(), 0);
        assert_eq!(result.source_sink_paths.len(), 0);
        assert!(result.is_tainted(1));
        assert!(result.is_tainted(2));
        assert!(!result.is_tainted(3)); // Sink not tainted
    }

    #[test]
    fn test_propagate_taint_forward_multi_source() {
        let graph = create_multi_source_sink_graph();
        let sources = vec![1, 2];
        let sinks = vec![4];

        let result = propagate_taint_forward(&graph, &sources, &sinks).unwrap();

        // Both sources reach sink
        assert!(result.has_vulnerability());
        assert_eq!(result.sinks_reached.len(), 1);
        assert!(result.sinks_reached.contains(&4));
        assert_eq!(result.source_sink_paths.len(), 2);
        // Paths should be sorted
        assert_eq!(result.source_sink_paths, vec![(1, 4), (2, 4)]);
    }

    #[test]
    fn test_propagate_taint_forward_multi_sink() {
        let graph = create_linear_flow_graph();
        let sources = vec![1];
        let sinks = vec![3, 5]; // Two potential sinks

        let result = propagate_taint_forward(&graph, &sources, &sinks).unwrap();

        // Source reaches both sinks
        assert!(result.has_vulnerability());
        assert_eq!(result.sinks_reached.len(), 2);
        assert_eq!(result.source_sink_paths.len(), 2);
        let mut paths = result.source_sink_paths.clone();
        paths.sort();
        assert_eq!(paths, vec![(1, 3), (1, 5)]);
    }

    #[test]
    fn test_propagate_taint_forward_empty_sources() {
        let graph = create_vulnerable_flow_graph();
        let sources = vec![];
        let sinks = vec![3];

        let result = propagate_taint_forward(&graph, &sources, &sinks).unwrap();

        assert!(!result.has_vulnerability());
        assert_eq!(result.tainted_nodes.len(), 0);
        assert_eq!(result.size, 0);
    }

    #[test]
    fn test_propagate_taint_forward_empty_sinks() {
        let graph = create_vulnerable_flow_graph();
        let sources = vec![1];
        let sinks = vec![];

        let result = propagate_taint_forward(&graph, &sources, &sinks).unwrap();

        // No sinks to check, so no vulnerabilities reported
        assert!(!result.has_vulnerability());
        assert!(result.is_tainted(1)); // But taint still propagates
        assert!(result.is_tainted(2));
        assert!(result.is_tainted(3));
    }

    // Tests for backward propagation

    #[test]
    fn test_propagate_taint_backward_vulnerable() {
        let graph = create_vulnerable_flow_graph();
        let sources = vec![1];
        let sink = 3;

        let result = propagate_taint_backward(&graph, sink, &sources).unwrap();

        // Sink 3 is reachable from source 1
        assert_eq!(result.sources.len(), 1);
        assert!(result.sources.contains(&1));
        assert!(result.sinks_reached.contains(&3));
        assert_eq!(result.source_sink_paths.len(), 1);
        assert_eq!(result.source_sink_paths[0], (1, 3));
        assert!(result.is_tainted(1));
        assert!(result.is_tainted(2));
        assert!(result.is_tainted(3));
    }

    #[test]
    fn test_propagate_taint_backward_safe() {
        let graph = create_safe_flow_graph();
        let sources = vec![1];
        let sink = 3;

        let result = propagate_taint_backward(&graph, sink, &sources).unwrap();

        // Sink 3 is NOT reachable from source 1
        assert_eq!(result.sources.len(), 0);
        assert!(!result.has_vulnerability());
    }

    #[test]
    fn test_propagate_taint_backward_multi_source() {
        let graph = create_multi_source_sink_graph();
        let sources = vec![1, 2];
        let sink = 4;

        let result = propagate_taint_backward(&graph, sink, &sources).unwrap();

        // Both sources can reach the sink
        assert_eq!(result.sources.len(), 2);
        assert!(result.sources.contains(&1));
        assert!(result.sources.contains(&2));
        assert_eq!(result.source_sink_paths.len(), 2);
    }

    #[test]
    fn test_propagate_taint_backward_self() {
        let graph = create_vulnerable_flow_graph();
        let sources = vec![1];
        let sink = 1; // Source is also the sink

        let result = propagate_taint_backward(&graph, sink, &sources).unwrap();

        // Node can reach itself
        assert_eq!(result.sources.len(), 1);
        assert!(result.sources.contains(&1));
    }

    // Tests for sink reachability analysis

    #[test]
    fn test_sink_reachability_vulnerability_found() {
        let graph = create_vulnerable_flow_graph();
        let sources = vec![1];
        let sinks = vec![3];

        let vulnerabilities = sink_reachability_analysis(&graph, &sources, &sinks).unwrap();

        assert_eq!(vulnerabilities.len(), 1);
        assert!(vulnerabilities.contains_key(&3));
        let affecting_sources = vulnerabilities.get(&3).unwrap();
        assert_eq!(affecting_sources.len(), 1);
        assert!(affecting_sources.contains(&1));
    }

    #[test]
    fn test_sink_reachability_no_vulnerability() {
        let graph = create_safe_flow_graph();
        let sources = vec![1];
        let sinks = vec![3];

        let vulnerabilities = sink_reachability_analysis(&graph, &sources, &sinks).unwrap();

        assert_eq!(vulnerabilities.len(), 0);
    }

    #[test]
    fn test_sink_reachability_multi_vulnerabilities() {
        let graph = create_multi_source_sink_graph();
        let sources = vec![1, 2];
        let sinks = vec![4];

        let vulnerabilities = sink_reachability_analysis(&graph, &sources, &sinks).unwrap();

        assert_eq!(vulnerabilities.len(), 1);
        let affecting_sources = vulnerabilities.get(&4).unwrap();
        assert_eq!(affecting_sources.len(), 2);
        assert!(affecting_sources.contains(&1));
        assert!(affecting_sources.contains(&2));
    }

    // Tests for source/sink discovery

    #[test]
    fn test_discover_sources_and_sinks_metadata() {
        let graph = create_vulnerable_flow_graph();

        let (sources, sinks) = discover_sources_and_sinks_default(&graph).unwrap();

        assert_eq!(sources, vec![1]); // user_input
        assert_eq!(sinks, vec![3]); // sql_execute
    }

    #[test]
    fn test_discover_sources_and_sinks_custom() {
        // Custom detectors: even nodes are sources, odd nodes are sinks
        struct EvenSourceDetector;
        struct OddSinkDetector;

        impl SourceCallback for EvenSourceDetector {
            fn is_source(&self, node: i64, _entity: &GraphEntity) -> bool {
                node % 2 == 0
            }
        }

        impl SinkCallback for OddSinkDetector {
            fn is_sink(&self, node: i64, _entity: &GraphEntity) -> bool {
                node % 2 == 1
            }
        }

        let graph = create_linear_flow_graph();

        let (sources, sinks) =
            discover_sources_and_sinks(&graph, &EvenSourceDetector, &OddSinkDetector).unwrap();

        // Nodes 2, 4 are even (sources)
        assert_eq!(sources.len(), 2);
        assert!(sources.contains(&2));
        assert!(sources.contains(&4));

        // Nodes 1, 3, 5 are odd (sinks)
        assert_eq!(sinks.len(), 3);
        assert!(sinks.contains(&1));
        assert!(sinks.contains(&3));
        assert!(sinks.contains(&5));
    }

    #[test]
    fn test_discover_empty_graph() {
        let graph = SqliteGraph::open_in_memory().unwrap();

        let (sources, sinks) = discover_sources_and_sinks_default(&graph).unwrap();

        assert_eq!(sources.len(), 0);
        assert_eq!(sinks.len(), 0);
    }

    // Tests for progress variants (validate they match base functions)

    #[test]
    fn test_propagate_taint_forward_with_progress_matches() {
        use crate::progress::NoProgress;

        let graph = create_vulnerable_flow_graph();
        let sources = vec![1];
        let sinks = vec![3];

        let base_result = propagate_taint_forward(&graph, &sources, &sinks).unwrap();
        let progress_result =
            propagate_taint_forward_with_progress(&graph, &sources, &sinks, &NoProgress).unwrap();

        assert_eq!(base_result.sources, progress_result.sources);
        assert_eq!(base_result.sinks_reached, progress_result.sinks_reached);
        assert_eq!(base_result.tainted_nodes, progress_result.tainted_nodes);
        assert_eq!(
            base_result.source_sink_paths,
            progress_result.source_sink_paths
        );
        assert_eq!(base_result.size, progress_result.size);
    }

    #[test]
    fn test_propagate_taint_backward_with_progress_matches() {
        use crate::progress::NoProgress;

        let graph = create_vulnerable_flow_graph();
        let sources = vec![1];
        let sink = 3;

        let base_result = propagate_taint_backward(&graph, sink, &sources).unwrap();
        let progress_result =
            propagate_taint_backward_with_progress(&graph, sink, &sources, &NoProgress).unwrap();

        assert_eq!(base_result.sources, progress_result.sources);
        assert_eq!(base_result.sinks_reached, progress_result.sinks_reached);
        assert_eq!(base_result.tainted_nodes, progress_result.tainted_nodes);
        assert_eq!(
            base_result.source_sink_paths,
            progress_result.source_sink_paths
        );
        assert_eq!(base_result.size, progress_result.size);
    }

    #[test]
    fn test_sink_reachability_with_progress_matches() {
        use crate::progress::NoProgress;

        let graph = create_multi_source_sink_graph();
        let sources = vec![1, 2];
        let sinks = vec![4];

        let base_result = sink_reachability_analysis(&graph, &sources, &sinks).unwrap();
        let progress_result =
            sink_reachability_analysis_with_progress(&graph, &sources, &sinks, &NoProgress)
                .unwrap();

        assert_eq!(base_result.len(), progress_result.len());
        for (sink, sources) in base_result {
            assert!(progress_result.contains_key(&sink));
            // Compare as sorted vectors (order may vary)
            let mut expected: Vec<i64> = sources.clone();
            let mut actual: Vec<i64> = progress_result.get(&sink).unwrap().clone();
            expected.sort();
            actual.sort();
            assert_eq!(expected, actual, "Sources for sink {} don't match", sink);
        }
    }
}