testtrim 0.14.8

Intelligently select automated tests to run via code coverage analysis
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
// SPDX-FileCopyrightText: 2024 Mathieu Fenniak <mathieu@fenniak.net>
//
// SPDX-License-Identifier: GPL-3.0-or-later

use std::{
    collections::{HashMap, HashSet},
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
    ops::RangeInclusive,
    path::PathBuf,
};

use anyhow::Result;
#[cfg(target_family = "unix")]
use anyhow::anyhow;
use ipnet::IpNet;
use log::{debug, trace, warn};
use regex::Regex;
use serde::Deserialize;

#[cfg(target_family = "unix")]
use crate::nsncd::parse_nscd_interchange;
use crate::{
    coverage::full_coverage_data::FullCoverageData,
    platform::{TestPlatform, TestReason},
    sys_trace::trace::{
        ResolvedSocketAddr, SocketCapture, SocketCaptureState, SocketOperation, UnifiedSocketAddr,
    },
    util::inline_range,
};

#[derive(Debug)]
pub struct NetworkDependency {
    pub socket: ResolvedSocketAddr,
}

/// Network policies; after matching a test based upon network access that it performed, different rules can be defined
/// for when the test is rerun in the future.
#[derive(Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct Policy {
    name: String,
    #[serde(rename = "match")]
    match_rules: Vec<PolicyMatch>,
    #[serde(rename = "apply")]
    apply_rules: PolicyApply,
}

/// One or more rule to define when a test policy is applied based upon network access specifics.
#[derive(Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
enum PolicyMatch {
    /// Matches any access to a Unix socket; can contain glob wildcards.
    UnixSocket(String),
    /// Matches network access on a specific port, regardless of the IP address used.
    Port(u16),
    /// Matches network access on a range of ports (eg. 1000-2000), inclusive, regardless of the IP address used.
    PortRange(#[serde(deserialize_with = "inline_range")] RangeInclusive<u16>),
    /// Matches network access on subnet (eg. 127.0.0.1/32, 10.0.0.0/8), regardless of the port used.
    Address(IpNet),
    /// Matches network access on subnet (eg. 127.0.0.1/32, 10.0.0.0/8) and specific network port.
    AddressPort(IpNet, u16),
    /// Matches network access on subnet (eg. 127.0.0.1/32, 10.0.0.0/8) and range of ports (eg. 1000-2000), inclusive.
    AddressPortRange(
        IpNet,
        #[serde(deserialize_with = "inline_range")] RangeInclusive<u16>,
    ),
    /// Matches network access on hostname (eg. localhost).
    Host(String),
    /// Matches network access on hostname (eg. localhost) and port.
    HostPort(String, u16),
    /// Matches network access on hostname (eg. localhost) and port range (eg. 1000-2000) inclusive.
    HostPortRange(
        String,
        #[serde(deserialize_with = "inline_range")] RangeInclusive<u16>,
    ),
}

#[derive(Deserialize, PartialEq, Eq, Debug)]
#[serde(rename_all = "kebab-case")]
enum PolicyApply {
    /// Always rerun the test because of the matched network access.  If network access matches no rules, the default is
    /// to rerun the test always, so this apply rule is rarely used.  However it can be used to force a test to rerun
    /// based upon one network match, even if other network matches end up omitting it.
    RunAlways,
    /// Ignore this network access; never rerun the test because of it.
    Ignore,
    /// For a test that matched this network access, run the test if other repository files are changed.  Each path can
    /// contain glob wildcards, and are interpreted relative to the repository root.  eg. `db/postgres/*.sql`
    RunIfFilesChanged(Vec<String>),
}

#[derive(PartialEq, Eq, Debug)]
enum Outcome {
    DefaultRun,
    ForceRun(String), // w/ at least one policy name that forced it (multiple are ignored)
    RunFromFileChange(String, PathBuf), // policy name and changed file that caused run
    Ignore(String),   // w/ at least one policy name that ignored it (multiple are ignored)
}

pub fn compute_tests_from_network_accesses<TP>(
    coverage_data: &FullCoverageData<TP::TI, TP::CI>,
    policies: &[Policy],
    eval_target_changed_files: &HashSet<PathBuf>,
) -> HashMap<TP::TI, HashSet<TestReason<TP::CI>>>
where
    TP: TestPlatform,
{
    let mut test_cases: HashMap<TP::TI, HashSet<TestReason<TP::CI>>> = HashMap::new();

    for (ci, tests) in coverage_data.coverage_identifier_to_test_map() {
        if let Ok(network_dependency) = TryInto::<NetworkDependency>::try_into(ci.clone()) {
            let test_reason = match evaluate_policy(
                policies,
                &network_dependency,
                eval_target_changed_files,
            ) {
                Outcome::DefaultRun => TestReason::CoverageIdentifier(ci.clone()),
                Outcome::ForceRun(ref policy_name) => {
                    debug!("network to {network_dependency:?} hit force-run policy {policy_name}");
                    TestReason::SideEffect(
                        Box::new(TestReason::CoverageIdentifier(ci.clone())),
                        Box::new(TestReason::NetworkPolicy(policy_name.clone())),
                    )
                }
                Outcome::RunFromFileChange(ref policy_name, ref file_changed) => {
                    debug!(
                        "network to {network_dependency:?} + file {} hit run policy {policy_name}",
                        file_changed.display(),
                    );
                    TestReason::SideEffect(
                        Box::new(TestReason::CoverageIdentifier(ci.clone())),
                        Box::new(TestReason::NetworkPolicy(format!(
                            "{policy_name} ({})",
                            file_changed.display()
                        ))),
                    )
                }
                Outcome::Ignore(ref policy_name) => {
                    debug!(
                        "ignoring network to {network_dependency:?} due to policy {policy_name}"
                    );
                    continue;
                }
            };

            for test in tests {
                test_cases
                    .entry(test.clone())
                    .or_default()
                    .insert(test_reason.clone());
            }
        }
    }

    test_cases
}

fn check_policy_match(network_dependency: &NetworkDependency, policy: &PolicyMatch) -> bool {
    match &network_dependency.socket.address {
        UnifiedSocketAddr::Unix(nd) => match policy {
            PolicyMatch::UnixSocket(mtch) => {
                evaluate_glob(mtch, &HashSet::from([nd.clone()])).is_some()
            }
            _ => false,
        },
        &UnifiedSocketAddr::Inet(SocketAddr::V4(ref v4)) => match policy {
            PolicyMatch::Port(port) => v4.port() == *port,
            PolicyMatch::PortRange(port_range) => port_range.contains(&v4.port()),
            &PolicyMatch::Address(IpNet::V4(ref v4_subnet)) => v4_subnet.contains(v4.ip()),
            &PolicyMatch::AddressPort(IpNet::V4(ref v4_subnet), ref port) => {
                v4_subnet.contains(v4.ip()) && v4.port() == *port
            }
            &PolicyMatch::AddressPortRange(IpNet::V4(ref v4_subnet), ref port_range) => {
                v4_subnet.contains(v4.ip()) && port_range.contains(&v4.port())
            }
            PolicyMatch::Host(hostname) => network_dependency.socket.hostnames.contains(hostname),
            PolicyMatch::HostPort(hostname, port) => {
                network_dependency.socket.hostnames.contains(hostname) && v4.port() == *port
            }
            PolicyMatch::HostPortRange(hostname, port_range) => {
                network_dependency.socket.hostnames.contains(hostname)
                    && port_range.contains(&v4.port())
            }
            &PolicyMatch::Address(IpNet::V6(_))
            | &PolicyMatch::AddressPort(IpNet::V6(_), _)
            | &PolicyMatch::AddressPortRange(IpNet::V6(_), _)
            | &PolicyMatch::UnixSocket(_) => false,
        },
        &UnifiedSocketAddr::Inet(SocketAddr::V6(ref v6)) => match policy {
            PolicyMatch::Port(port) => v6.port() == *port,
            PolicyMatch::PortRange(port_range) => port_range.contains(&v6.port()),
            &PolicyMatch::Address(IpNet::V6(ref v6_subnet)) => v6_subnet.contains(v6.ip()),
            &PolicyMatch::AddressPort(IpNet::V6(ref v6_subnet), ref port) => {
                v6_subnet.contains(v6.ip()) && v6.port() == *port
            }
            &PolicyMatch::AddressPortRange(IpNet::V6(ref v6_subnet), ref port_range) => {
                v6_subnet.contains(v6.ip()) && port_range.contains(&v6.port())
            }
            PolicyMatch::Host(hostname) => network_dependency.socket.hostnames.contains(hostname),
            PolicyMatch::HostPort(hostname, port) => {
                network_dependency.socket.hostnames.contains(hostname) && v6.port() == *port
            }
            PolicyMatch::HostPortRange(hostname, port_range) => {
                network_dependency.socket.hostnames.contains(hostname)
                    && port_range.contains(&v6.port())
            }
            // v6 could be an IPv4 Mapped address, and if so we match it to IPv4 policies as well:
            &PolicyMatch::Address(IpNet::V4(ref v4_subnet)) => v6
                .ip()
                .to_ipv4_mapped()
                .is_some_and(|v4| v4_subnet.contains(&v4)),
            &PolicyMatch::AddressPort(IpNet::V4(ref v4_subnet), ref port) => {
                v6.ip()
                    .to_ipv4_mapped()
                    .is_some_and(|v4| v4_subnet.contains(&v4))
                    && v6.port() == *port
            }
            &PolicyMatch::AddressPortRange(IpNet::V4(ref v4_subnet), ref port_range) => {
                v6.ip()
                    .to_ipv4_mapped()
                    .is_some_and(|v4| v4_subnet.contains(&v4))
                    && port_range.contains(&v6.port())
            }
            &PolicyMatch::UnixSocket(_) => false,
        },
    }
}

fn evaluate_policy(
    policies: &[Policy],
    network_dependency: &NetworkDependency,
    eval_target_changed_files: &HashSet<PathBuf>,
) -> Outcome {
    let mut outcome = Outcome::DefaultRun;

    for policy in policies {
        for match_rule in &policy.match_rules {
            if check_policy_match(network_dependency, match_rule) {
                match policy.apply_rules {
                    PolicyApply::RunAlways => {
                        // stop evaluating at this point as we've decided to force it already
                        return Outcome::ForceRun(policy.name.clone());
                    }

                    // if we've already moved on from DefaultRun, don't overwrite outcome
                    PolicyApply::Ignore if outcome == Outcome::DefaultRun => {
                        // don't return; allow all policies to be matched in case one forces it to run
                        outcome = Outcome::Ignore(policy.name.clone());
                    }
                    PolicyApply::Ignore => {}

                    PolicyApply::RunIfFilesChanged(ref globs) => {
                        // Ignore, unless we find a matching file...
                        outcome = Outcome::Ignore(policy.name.clone());

                        for glob in globs {
                            if let Some(path) = evaluate_glob(glob, eval_target_changed_files) {
                                // stop evaluating as this is treated like a force; can't be ignored after this
                                return Outcome::RunFromFileChange(
                                    policy.name.clone(),
                                    path.clone(),
                                );
                            }
                        }
                    }
                }
                break; // no need to check any other match rules on this policy
            }
        }
    }

    outcome
}

fn evaluate_glob<'a>(glob: &str, paths: &'a HashSet<PathBuf>) -> Option<&'a PathBuf> {
    // The glob library seems like the right thing to use here, but it seems to require that it works against a live
    // filesystem only... we're going to be using it against a set of "changed files", and in unit tests with fake
    // files, so it seems more straightforward to do it with a hacky little regex creation.

    // FIXME: probably need revision to support Windows file path separators
    let regex_txt = regex::escape(glob)
        // note, none of these replaces can use a RHS that could be matched by the next replace. :-p
        // path wildcard
        .replace("\\*\\*", ".{0,}")
        // non-path wildcard
        .replace("\\*", "[^/]{0,}");

    // anchor to ensure we're doing a full path match:
    let mut full_regex = String::with_capacity(regex_txt.len() + 2);
    full_regex.push('^');
    full_regex.push_str(&regex_txt);
    full_regex.push('$');

    // FIXME: cache the regex by glob?  Could be constructed quite a few times, if there are a lot of changed files.
    let regex = Regex::new(&full_regex).unwrap();

    paths
        .iter()
        .find(|&path| regex.is_match(&path.to_string_lossy()))
}

/// Analyze a set of `SocketCapture` records and attempt to identify if any DNS resolution occurred within the capture.
/// Returns a maps of the resolved IP address to the one-or-more hostnames which resolved to that address.
pub fn analyze_socket_captures(
    socket_captures: &[SocketCapture],
) -> Result<HashMap<IpAddr, HashSet<String>>> {
    let mut retval = HashMap::new();

    for capture in socket_captures {
        #[cfg(target_family = "unix")]
        {
            let result = analyze_nscd(capture, &mut retval)?;
            if let DnsCaptureAnalysisResult::IncompleteCapture = result {
                warn!(
                    "network stream to {:?} was not fully captured, preventing nscd protocol decode for DNS analysis",
                    capture.socket_addr
                );
            }
        }

        let result = analyze_dns(capture, &mut retval)?;
        if let DnsCaptureAnalysisResult::IncompleteCapture = result {
            warn!(
                "network stream to {:?} was not fully captured, preventing DNS protocol decode for DNS analysis",
                capture.socket_addr
            );
        }
    }

    Ok(retval)
}

#[derive(Debug, PartialEq)]
enum DnsCaptureAnalysisResult {
    NotApplicable,
    IncompleteCapture,
    Data,
}

#[derive(Debug, PartialEq)]
enum LastOperation {
    Unknown,
    Send,
    Read,
}

// Consolidate sequential Read and Sent chunks into larger Read and Sent chunks; stop each time the operation changes.
fn consolidate_chunks(operations: &[SocketOperation]) -> Vec<SocketOperation> {
    let mut retval = vec![];

    let mut mode = LastOperation::Unknown;
    let mut buf: Vec<u8> = Vec::with_capacity(256);

    for op in operations {
        match (op, &mode) {
            (&SocketOperation::Sent(ref new_data), LastOperation::Send)
            | (&SocketOperation::Read(ref new_data), LastOperation::Read) => {
                buf.extend(new_data);
            }
            (SocketOperation::Sent(new_data), LastOperation::Read) => {
                retval.push(SocketOperation::Read(buf));
                buf = Vec::with_capacity(256);
                buf.extend(new_data);
                mode = LastOperation::Send;
            }
            (SocketOperation::Sent(new_data), LastOperation::Unknown) => {
                buf.extend(new_data);
                mode = LastOperation::Send;
            }
            (SocketOperation::Read(new_data), LastOperation::Send) => {
                retval.push(SocketOperation::Sent(buf));
                buf = Vec::with_capacity(256);
                buf.extend(new_data);
                mode = LastOperation::Read;
            }
            (SocketOperation::Read(new_data), LastOperation::Unknown) => {
                buf.extend(new_data);
                mode = LastOperation::Read;
            }
        }
    }

    match mode {
        LastOperation::Unknown => {}
        LastOperation::Send => retval.push(SocketOperation::Sent(buf)),
        LastOperation::Read => retval.push(SocketOperation::Read(buf)),
    }

    retval
}

#[cfg(target_family = "unix")]
fn analyze_nscd(
    socket_capture: &SocketCapture,
    dns_resolutions: &mut HashMap<IpAddr, HashSet<String>>,
) -> Result<DnsCaptureAnalysisResult> {
    if let UnifiedSocketAddr::Unix(ref path) = socket_capture.socket_addr {
        if path != &PathBuf::from("/var/run/nscd/socket") {
            return Ok(DnsCaptureAnalysisResult::NotApplicable);
        }
    } else {
        return Ok(DnsCaptureAnalysisResult::NotApplicable);
    }

    let SocketCaptureState::Complete(ref operations) = socket_capture.state else {
        return Ok(DnsCaptureAnalysisResult::IncompleteCapture);
    };

    let operations = consolidate_chunks(operations);

    // We're expecting [Send, Recv] pairs.  Recv may be absent in the last operation.  The type of the send packet will
    // need to be understood in order to determine how to parse the receive packet.
    for send_recv in operations.chunks(2) {
        // Check if recv absent.
        if send_recv.len() != 2 {
            continue;
        }

        let send = &send_recv[0];
        let recv = &send_recv[1];

        if let SocketOperation::Sent(send_data) = send {
            if let SocketOperation::Read(recv_data) = recv {
                parse_nscd_interchange(send_data, recv_data, dns_resolutions)?;
            } else {
                return Err(anyhow!("chunk was expected to be Read, but was Sent"));
            }
        } else {
            return Err(anyhow!("chunk was expected to be Sent, but was Read"));
        }
    }

    Ok(DnsCaptureAnalysisResult::Data)
}

fn analyze_dns(
    socket_capture: &SocketCapture,
    dns_resolutions: &mut HashMap<IpAddr, HashSet<String>>,
) -> Result<DnsCaptureAnalysisResult> {
    if let UnifiedSocketAddr::Inet(ref socket_addr) = socket_capture.socket_addr {
        let dns = match socket_addr {
            SocketAddr::V4(v4) => v4.port() == 53,
            SocketAddr::V6(v6) => v6.port() == 53,
        };
        if !dns {
            return Ok(DnsCaptureAnalysisResult::NotApplicable);
        }
    } else {
        return Ok(DnsCaptureAnalysisResult::NotApplicable);
    }

    let SocketCaptureState::Complete(ref operations) = socket_capture.state else {
        return Ok(DnsCaptureAnalysisResult::IncompleteCapture);
    };

    for msg in operations {
        let SocketOperation::Read(buffer) = msg else {
            continue;
        };

        // Experimentally the largest attempted write I've seen is for 12 Answer responses (NotEnoughWriteSpace {
        // tried_to_write: 12, available: 10, buffer_type: "Answer" }) -- this impl current allocates 12 of each
        // response type to write into to see whether that's enough.
        let mut questions = [dns_protocol::Question::default(); 1];
        let mut answers = [dns_protocol::ResourceRecord::default(); 12];
        let mut authorities = [dns_protocol::ResourceRecord::default(); 12];
        let mut additional = [dns_protocol::ResourceRecord::default(); 12];
        match dns_protocol::Message::read(
            buffer,
            &mut questions,
            &mut answers,
            &mut authorities,
            &mut additional,
        ) {
            Ok(message) => {
                // It will be common when looking at DNS responses to get a few different pieces of information back;
                // for example when fetching a record with a CNAME response:
                //
                // ```
                // ;; QUESTION SECTION:
                // ;api.nuget.org.                 IN      A
                //
                // ;; ANSWER SECTION:
                // api.nuget.org.          300     IN      CNAME   nugetapiprod.trafficmanager.net.
                // nugetapiprod.trafficmanager.net. 180 IN CNAME   az320820.vo.msecnd.net.
                // az320820.vo.msecnd.net. 3600    IN      CNAME   cs2.wpc.gammacdn.net.
                // cs2.wpc.gammacdn.net.   3600    IN      A       152.199.4.184
                // ```
                //
                // So we really want to say that "api.nuget.org" is "152.199.4.184" in a response like this, because the
                // program that was traced is coded against "api.nuget.org", and that's what we'd want people to write
                // network rules against.
                //
                // FIXME: There's likely an unfinished gap here -- if the question is "A", the response is "CNAME", and
                // the response doesn't also include the supplementary data to follow that CNAME to the "A", then I
                // think a proper DNS client would make a follow-up request for the CNAME address.  With the current
                // logic here, we'd end up mapping the CNAME to the A, rather than the original DNS request.  But I
                // think that as long as the host is using a recursive DNS resolver, it will follow the CNAMEs all the
                // way to a response, so maybe this isn't a big deal.
                let question = message.questions()[0];
                let hostname = match question.ty() {
                    dns_protocol::ResourceType::A | dns_protocol::ResourceType::AAAA => {
                        format!("{}", question.name())
                    }
                    other => {
                        // "warn" is probably higher level logging than needed, but I want to see if anything else
                        // interesting happens here that needs attention.
                        warn!(
                            "DNS response had skipped question {other:?} that is not understood by analyze_dns; {question:?}"
                        );
                        continue;
                    }
                };

                for answer in message.answers() {
                    match answer.ty() {
                        dns_protocol::ResourceType::A => {
                            let addr = answer.data().try_into().map(u32::from_be_bytes)?;
                            debug!(
                                "DNS {} (via label {}) -> {}",
                                hostname,
                                answer.name(),
                                IpAddr::V4(Ipv4Addr::from_bits(addr))
                            );
                            dns_resolutions
                                .entry(IpAddr::V4(Ipv4Addr::from_bits(addr)))
                                .or_default()
                                .insert(hostname.clone());
                        }
                        dns_protocol::ResourceType::AAAA => {
                            let addr = answer.data().try_into().map(u128::from_be_bytes)?;
                            debug!(
                                "DNS {} (via label {}) -> {}",
                                hostname,
                                answer.name(),
                                IpAddr::V6(Ipv6Addr::from_bits(addr))
                            );
                            dns_resolutions
                                .entry(IpAddr::V6(Ipv6Addr::from_bits(addr)))
                                .or_default()
                                .insert(hostname.clone());
                        }
                        dns_protocol::ResourceType::CName => {
                            trace!("DNS response CNAME {} skipped", answer.name());
                        }
                        other => {
                            debug!("DNS response type {other:?} is not understood by analyze_dns");
                        }
                    }
                }
            }
            Err(e) => {
                warn!(
                    "Error occurred parsing network response on port 53 as DNS protocol; some DNS resolutions may be lost.  Error was: {e:?}"
                );
            }
        }
    }

    Ok(DnsCaptureAnalysisResult::Data)
}

#[cfg(test)]
mod tests {
    use std::{
        collections::{BTreeSet, HashMap, HashSet},
        net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4},
        path::PathBuf,
        str::FromStr as _,
        sync::LazyLock,
    };

    use anyhow::Result;
    use ipnet::{IpNet, Ipv4Net, Ipv6Net};
    use serde::Deserialize;

    use crate::{
        coverage::full_coverage_data::FullCoverageData,
        network::{
            DnsCaptureAnalysisResult, NetworkDependency, Outcome, PolicyApply, PolicyMatch,
            check_policy_match, evaluate_glob, evaluate_policy,
        },
        platform::{
            TestReason,
            rust::{RustCoverageIdentifier, RustTestIdentifier, RustTestPlatform},
        },
        sys_trace::trace::{
            ResolvedSocketAddr, SocketCapture, SocketCaptureState, SocketOperation,
            UnifiedSocketAddr,
        },
    };

    use super::{Policy, analyze_dns, analyze_nscd, compute_tests_from_network_accesses};

    #[derive(Deserialize, Debug)]
    #[serde(rename_all = "kebab-case")]
    struct Config {
        network_policy: Vec<Policy>,
    }

    #[test]
    fn parse_config() {
        let config: Config = toml::from_str(
            r#"
                [[network-policy]]
                name = "DNS access" # used to report test reasons in get-test-identifiers
                apply = 'run-always'
                [[network-policy.match]]
                unix-socket = "/var/run/nscd/socket"
                [[network-policy.match]]
                port = 53

                [[network-policy]]
                name = "internal test servers"
                apply = 'ignore'
                [[network-policy.match]]
                port-range = "16384-32768"
                [[network-policy.match]]
                address = "10.0.0.0/8"
                [[network-policy.match]]
                address = "::1/128"
                [[network-policy.match]]
                address-port = ["127.0.0.1/32", 8080]
                [[network-policy.match]]
                address-port-range = ["127.0.0.1/32", "8085-8086"]
                [[network-policy.match]]
                port-range = "1024-65535"
                [[network-policy.match]]
                host = "testtrim.org"
                [[network-policy.match]]
                host-port = ["testtrim.org", 8080]
                [[network-policy.match]]
                host-port-range = ["testtrim.org", "8085-8086"]

                [[network-policy]]
                name = "PostgreSQL server"
                apply.run-if-files-changed = [
                    "db/postgres/*.sql",
                ]
                [[network-policy.match]]
                port = 5432
            "#,
        )
        .unwrap();

        let policy = &config.network_policy[0];
        assert_eq!(policy.name, "DNS access");
        assert_eq!(policy.apply_rules, PolicyApply::RunAlways);

        let PolicyMatch::UnixSocket(unix_socket) = &policy.match_rules[0] else {
            panic!("expected match_rules[0] to be UnixSocket");
        };
        assert_eq!(unix_socket, "/var/run/nscd/socket");

        let PolicyMatch::Port(port) = &policy.match_rules[1] else {
            panic!("expected match_rules[0] to be Port");
        };
        assert_eq!(port, &53);

        let policy = &config.network_policy[1];
        assert_eq!(policy.name, "internal test servers");
        assert_eq!(policy.apply_rules, PolicyApply::Ignore);

        let PolicyMatch::PortRange(port_range) = &policy.match_rules[0] else {
            panic!("expected match_rules[0] to be PortRange");
        };
        assert_eq!(*port_range.start(), 16384);
        assert_eq!(*port_range.end(), 32768);

        let PolicyMatch::Address(address) = &policy.match_rules[1] else {
            panic!("expected match_rules[1] to be Address");
        };
        let IpNet::V4(address_ip4) = address else {
            panic!("expected match_rules[1] to be Ipv4Net");
        };
        assert_eq!(
            address_ip4,
            &Ipv4Net::new(Ipv4Addr::new(10, 0, 0, 0), 8).unwrap()
        );

        let PolicyMatch::Address(address) = &policy.match_rules[2] else {
            panic!("expected match_rules[2] to be Address");
        };
        let IpNet::V6(address_ip6) = address else {
            panic!("expected match_rules[2] to be Ipv6Net");
        };
        assert_eq!(
            address_ip6,
            &Ipv6Net::new(Ipv6Addr::LOCALHOST, 128).unwrap()
        );

        let PolicyMatch::AddressPort(address, port) = &policy.match_rules[3] else {
            panic!("expected match_rules[3] to be AddressPort");
        };
        let IpNet::V4(address_ip4) = address else {
            panic!("expected match_rules[3] to be Ipv4Net");
        };
        assert_eq!(address_ip4, &Ipv4Net::new(Ipv4Addr::LOCALHOST, 32).unwrap());
        assert_eq!(port, &8080);

        let PolicyMatch::AddressPortRange(address, port_range) = &policy.match_rules[4] else {
            panic!("expected match_rules[4] to be AddressPortRange");
        };
        let IpNet::V4(address_ip4) = address else {
            panic!("expected match_rules[4] to be Ipv4Net");
        };
        assert_eq!(address_ip4, &Ipv4Net::new(Ipv4Addr::LOCALHOST, 32).unwrap());
        assert_eq!(*port_range.start(), 8085);
        assert_eq!(*port_range.end(), 8086);

        let PolicyMatch::PortRange(port_range) = &policy.match_rules[5] else {
            panic!("expected match_rules[5] to be PortRange");
        };
        assert_eq!(*port_range.start(), 1024);
        assert_eq!(*port_range.end(), 65535);

        let PolicyMatch::Host(hostname) = &policy.match_rules[6] else {
            panic!("expected match_rules[6] to be Host");
        };
        assert_eq!(*hostname, "testtrim.org");

        let PolicyMatch::HostPort(hostname, port) = &policy.match_rules[7] else {
            panic!("expected match_rules[7] to be HostPort");
        };
        assert_eq!(*hostname, "testtrim.org");
        assert_eq!(*port, 8080);

        let PolicyMatch::HostPortRange(hostname, port_range) = &policy.match_rules[8] else {
            panic!("expected match_rules[8] to be HostPortRange");
        };
        assert_eq!(*hostname, "testtrim.org");
        assert_eq!(*port_range.start(), 8085);
        assert_eq!(*port_range.end(), 8086);

        let policy = &config.network_policy[2];
        assert_eq!(policy.name, "PostgreSQL server");
        assert_eq!(
            policy.apply_rules,
            PolicyApply::RunIfFilesChanged(vec!["db/postgres/*.sql".to_string()])
        );
    }

    /// Helper function to create a `SocketAddr` for testing.
    fn create_socket_addr(ip: &str, port: u16) -> SocketAddr {
        SocketAddr::new(IpAddr::from_str(ip).unwrap(), port)
    }

    #[test]
    fn test_match_port() {
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("127.0.0.1", 8080)).into(),
        };
        let pm = PolicyMatch::Port(8080);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Port(8081);
        assert!(!check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("::1", 8080)).into(),
        };
        let pm = PolicyMatch::Port(8080);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Port(8081);
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_match_port_range() {
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("127.0.0.1", 1500)).into(),
        };
        let pm = PolicyMatch::PortRange(1000..=2000);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::PortRange(1500..=2000);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::PortRange(1000..=1501);
        assert!(check_policy_match(&nd, &pm));

        let pm = PolicyMatch::PortRange(0..=1024);
        assert!(!check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("::1", 1500)).into(),
        };
        let pm = PolicyMatch::PortRange(1000..=2000);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::PortRange(1500..=2000);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::PortRange(1000..=1501);
        assert!(check_policy_match(&nd, &pm));

        let pm = PolicyMatch::PortRange(0..=1024);
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_match_unix_socket() {
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Unix(PathBuf::from("/tmp/socket")).into(),
        };
        let pm = PolicyMatch::UnixSocket(String::from("/tmp/socket"));
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::UnixSocket(String::from("/tmp/socket2"));
        assert!(!check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Unix(PathBuf::from("/tmp/dir/socket")).into(),
        };
        let pm = PolicyMatch::UnixSocket(String::from("/**/*"));
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::UnixSocket(String::from("/tmp/dir/sock*"));
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::UnixSocket(String::from("/var/run/*"));
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_match_address() {
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("10.0.0.1", 8080)).into(),
        };
        let pm = PolicyMatch::Address(IpNet::from_str("10.0.0.0/8").unwrap());
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Address(IpNet::from_str("11.0.0.0/8").unwrap());
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Address(IpNet::from_str("::/0").unwrap());
        assert!(!check_policy_match(&nd, &pm));

        // If policy is an ipv4 addr, but tracing shows an IPv4 Mapped address, we should still match:
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("::ffff:10.0.0.1", 8080)).into(),
        };
        let pm = PolicyMatch::Address(IpNet::from_str("10.0.0.0/8").unwrap());
        assert!(check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("2400:4000::1234", 8080)).into(),
        };
        let pm = PolicyMatch::Address(IpNet::from_str("2400:4000::/21").unwrap());
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Address(IpNet::from_str("0000:4000::/21").unwrap());
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Address(IpNet::from_str("0.0.0.0/0").unwrap());
        assert!(!check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("::1", 8080)).into(),
        };
        let pm = PolicyMatch::Address(IpNet::from_str("0.0.0.0/0").unwrap());
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_match_address_port() {
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("10.0.0.1", 8080)).into(),
        };
        let pm = PolicyMatch::AddressPort(IpNet::from_str("10.0.0.0/8").unwrap(), 8080);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPort(IpNet::from_str("127.0.0.0/8").unwrap(), 8080);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPort(IpNet::from_str("10.0.0.0/8").unwrap(), 8081);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPort(IpNet::from_str("::/0").unwrap(), 8080);
        assert!(!check_policy_match(&nd, &pm));

        // If policy is an ipv4 addr, but tracing shows an IPv4 Mapped address, we should still match:
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("::ffff:10.0.0.1", 8080)).into(),
        };
        let pm = PolicyMatch::AddressPort(IpNet::from_str("10.0.0.0/8").unwrap(), 8080);
        assert!(check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("::1", 8080)).into(),
        };
        let pm = PolicyMatch::AddressPort(IpNet::from_str("::/0").unwrap(), 8080);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPort(IpNet::from_str("2400:4000::/21").unwrap(), 8080);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPort(IpNet::from_str("::/0").unwrap(), 8081);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPort(IpNet::from_str("0.0.0.0/0").unwrap(), 8080);
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_match_address_port_range() {
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("10.0.0.1", 1500)).into(),
        };
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("10.0.0.0/8").unwrap(), 1000..=2000);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("10.0.0.0/8").unwrap(), 1500..=2000);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("10.0.0.0/8").unwrap(), 1000..=1501);
        assert!(check_policy_match(&nd, &pm));

        let pm =
            PolicyMatch::AddressPortRange(IpNet::from_str("127.0.0.1/32").unwrap(), 1000..=2000);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("10.0.0.0/8").unwrap(), 1501..=1502);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("::/0").unwrap(), 1000..=2000);
        assert!(!check_policy_match(&nd, &pm));

        // If policy is an ipv4 addr, but tracing shows an IPv4 Mapped address, we should still match:
        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("::ffff:10.0.0.1", 8080)).into(),
        };
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("10.0.0.0/8").unwrap(), 8000..=9000);
        assert!(check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: UnifiedSocketAddr::Inet(create_socket_addr("::1", 1500)).into(),
        };
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("::/0").unwrap(), 1000..=2000);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("::/0").unwrap(), 1500..=2000);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("::/0").unwrap(), 1000..=1501);
        assert!(check_policy_match(&nd, &pm));

        let pm =
            PolicyMatch::AddressPortRange(IpNet::from_str("2400:4000::/21").unwrap(), 1000..=2000);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("10.0.0.0/8").unwrap(), 1501..=1502);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::AddressPortRange(IpNet::from_str("0.0.0.0/0").unwrap(), 1000..=2000);
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_match_host() {
        let nd = NetworkDependency {
            socket: ResolvedSocketAddr {
                address: UnifiedSocketAddr::Inet(create_socket_addr("10.0.0.1", 1500)),
                hostnames: BTreeSet::from([
                    String::from("testtrim.org"),
                    String::from("static.testtrim.org"),
                ]),
            },
        };
        let pm = PolicyMatch::Host(String::from("testtrim.org"));
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Host(String::from("static.testtrim.org"));
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Host(String::from("localhost"));
        assert!(!check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: ResolvedSocketAddr {
                address: UnifiedSocketAddr::Inet(create_socket_addr("2400:4000::dead:beef", 1500)),
                hostnames: BTreeSet::from([
                    String::from("testtrim.org"),
                    String::from("static.testtrim.org"),
                ]),
            },
        };
        let pm = PolicyMatch::Host(String::from("testtrim.org"));
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Host(String::from("static.testtrim.org"));
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::Host(String::from("localhost"));
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_match_host_port() {
        let nd = NetworkDependency {
            socket: ResolvedSocketAddr {
                address: UnifiedSocketAddr::Inet(create_socket_addr("10.0.0.1", 1500)),
                hostnames: BTreeSet::from([
                    String::from("testtrim.org"),
                    String::from("static.testtrim.org"),
                ]),
            },
        };
        let pm = PolicyMatch::HostPort(String::from("testtrim.org"), 1500);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPort(String::from("static.testtrim.org"), 1500);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPort(String::from("static.testtrim.org"), 1501);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPort(String::from("localhost"), 1500);
        assert!(!check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: ResolvedSocketAddr {
                address: UnifiedSocketAddr::Inet(create_socket_addr("2400:4000::dead:beef", 1500)),
                hostnames: BTreeSet::from([
                    String::from("testtrim.org"),
                    String::from("static.testtrim.org"),
                ]),
            },
        };
        let pm = PolicyMatch::HostPort(String::from("testtrim.org"), 1500);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPort(String::from("static.testtrim.org"), 1500);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPort(String::from("static.testtrim.org"), 1501);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPort(String::from("localhost"), 1500);
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_match_host_portrange() {
        let nd = NetworkDependency {
            socket: ResolvedSocketAddr {
                address: UnifiedSocketAddr::Inet(create_socket_addr("10.0.0.1", 1500)),
                hostnames: BTreeSet::from([
                    String::from("testtrim.org"),
                    String::from("static.testtrim.org"),
                ]),
            },
        };
        let pm = PolicyMatch::HostPortRange(String::from("testtrim.org"), 1490..=1550);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPortRange(String::from("static.testtrim.org"), 1490..=1550);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPortRange(String::from("static.testtrim.org"), 1501..=1550);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPortRange(String::from("localhost"), 1490..=1550);
        assert!(!check_policy_match(&nd, &pm));

        let nd = NetworkDependency {
            socket: ResolvedSocketAddr {
                address: UnifiedSocketAddr::Inet(create_socket_addr("2400:4000::dead:beef", 1500)),
                hostnames: BTreeSet::from([
                    String::from("testtrim.org"),
                    String::from("static.testtrim.org"),
                ]),
            },
        };
        let pm = PolicyMatch::HostPortRange(String::from("testtrim.org"), 1490..=1550);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPortRange(String::from("static.testtrim.org"), 1490..=1550);
        assert!(check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPortRange(String::from("static.testtrim.org"), 1501..=1550);
        assert!(!check_policy_match(&nd, &pm));
        let pm = PolicyMatch::HostPortRange(String::from("localhost"), 1490..=1550);
        assert!(!check_policy_match(&nd, &pm));
    }

    #[test]
    fn test_evaluate_policy() {
        let config: Config = toml::from_str(
            r#"
                [[network-policy]]
                name = "local router access"
                apply = "run-always"
                [[network-policy.match]]
                address = "192.168.0.0/16"

                [[network-policy]]
                name = "DNS access"
                apply = "ignore"
                [[network-policy.match]]
                port = 53

                [[network-policy]]
                # This is the same policy as above, but just shows what happens if we match multiple ignores.
                name = "DNS access #2"
                apply = "ignore"
                [[network-policy.match]]
                port = 53

                [[network-policy]]
                name = "PostgreSQL server"
                apply.run-if-files-changed = [
                    "db/postgres/*.sql",
                ]
                [[network-policy.match]]
                port = 5432
            "#,
        )
        .unwrap();

        // Case that doesn't match any policy
        let outcome = evaluate_policy(
            &config.network_policy,
            &NetworkDependency {
                socket: UnifiedSocketAddr::Inet(create_socket_addr("10.1.1.1", 8080)).into(),
            },
            &HashSet::new(),
        );
        assert_eq!(outcome, Outcome::DefaultRun);

        // Matches two ignore policies; expect the first one's name to be returned:
        let outcome = evaluate_policy(
            &config.network_policy,
            &NetworkDependency {
                socket: UnifiedSocketAddr::Inet(create_socket_addr("10.1.1.1", 53)).into(),
            },
            &HashSet::new(),
        );
        assert_eq!(outcome, Outcome::Ignore("DNS access".to_string()));

        // Matches the *both* the ignore and always-run policy; always-run overrides ignore:
        let outcome = evaluate_policy(
            &config.network_policy,
            &NetworkDependency {
                socket: UnifiedSocketAddr::Inet(create_socket_addr("192.168.1.1", 53)).into(),
            },
            &HashSet::new(),
        );
        assert_eq!(
            outcome,
            Outcome::ForceRun("local router access".to_string())
        );

        // Matches the run-if-files-changed but doesn't have any matching files:
        let outcome = evaluate_policy(
            &config.network_policy,
            &NetworkDependency {
                socket: UnifiedSocketAddr::Inet(create_socket_addr("10.1.1.1", 5432)).into(),
            },
            &HashSet::new(),
        );
        assert_eq!(outcome, Outcome::Ignore("PostgreSQL server".to_string()));

        // Matches the run-if-files-changed and also matches files changed:
        let outcome = evaluate_policy(
            &config.network_policy,
            &NetworkDependency {
                socket: UnifiedSocketAddr::Inet(create_socket_addr("10.1.1.1", 5432)).into(),
            },
            &HashSet::from([PathBuf::from("db/postgres/2024_schema.sql")]),
        );
        assert_eq!(
            outcome,
            Outcome::RunFromFileChange(
                "PostgreSQL server".to_string(),
                PathBuf::from("db/postgres/2024_schema.sql")
            )
        );
    }

    #[test]
    fn test_evaluate_glob() {
        assert_eq!(
            evaluate_glob(
                "CHANGELOG.md",
                &HashSet::from([PathBuf::from("CHANGELOG.md")])
            ),
            Some(&PathBuf::from("CHANGELOG.md"))
        );
        assert_eq!(
            evaluate_glob(
                "CHANGELOG.md",
                &HashSet::from([PathBuf::from("CHANGELOG-md")])
            ),
            None
        );
        assert_eq!(
            evaluate_glob("*.md", &HashSet::from([PathBuf::from("CHANGELOG.md")])),
            Some(&PathBuf::from("CHANGELOG.md"))
        );
        assert_eq!(
            evaluate_glob("*.md", &HashSet::from([PathBuf::from("docs/CHANGELOG.md")])),
            None
        );
        assert_eq!(
            evaluate_glob(
                "**/*.md",
                &HashSet::from([PathBuf::from("docs/CHANGELOG.md")])
            ),
            Some(&PathBuf::from("docs/CHANGELOG.md"))
        );
        assert_eq!(
            evaluate_glob(
                "**/*.md",
                &HashSet::from([PathBuf::from("docs/CHANGELOG.txt")])
            ),
            None
        );
    }

    static TEST1: LazyLock<RustTestIdentifier> = LazyLock::new(|| RustTestIdentifier {
        test_src_path: PathBuf::from("src/lib.rs"),
        test_name: "test1".to_string(),
    });
    static TEST2: LazyLock<RustTestIdentifier> = LazyLock::new(|| RustTestIdentifier {
        test_src_path: PathBuf::from("src/lib.rs"),
        test_name: "test2".to_string(),
    });
    static TEST3: LazyLock<RustTestIdentifier> = LazyLock::new(|| RustTestIdentifier {
        test_src_path: PathBuf::from("sub_module/src/lib.rs"),
        test_name: "test1".to_string(),
    });
    static TEST4: LazyLock<RustTestIdentifier> = LazyLock::new(|| RustTestIdentifier {
        test_src_path: PathBuf::from("sub_module/src/lib.rs"),
        test_name: "test4".to_string(),
    });

    #[test]
    fn test_compute_tests() {
        let config: Config = toml::from_str(
            r#"
                [[network-policy]]
                name = "local router access"
                apply = "run-always"
                [[network-policy.match]]
                address = "192.168.0.0/16"

                [[network-policy]]
                name = "DNS access"
                apply = "ignore"
                [[network-policy.match]]
                port = 53

                [[network-policy]]
                name = "PostgreSQL server"
                apply.run-if-files-changed = [
                    "db/postgres/*.sql",
                ]
                [[network-policy.match]]
                port = 5432
            "#,
        )
        .unwrap();

        let mut coverage_data =
            FullCoverageData::<RustTestIdentifier, RustCoverageIdentifier>::new();
        let policies = &config.network_policy;

        // default case; a network access that isn't part of the policy
        let test1_network_ci = RustCoverageIdentifier::NetworkDependency(
            UnifiedSocketAddr::Unix(PathBuf::from("/tmp/socket")).into(),
        );
        coverage_data.add_heuristic_coverage_to_test(TEST1.clone(), test1_network_ci.clone());

        // ignore case; network access was present but policy says nevermind it
        let test2_network_ci = RustCoverageIdentifier::NetworkDependency(
            UnifiedSocketAddr::Inet(create_socket_addr("127.0.0.1", 53)).into(),
        );
        coverage_data.add_heuristic_coverage_to_test(TEST2.clone(), test2_network_ci.clone());

        // force-run case; network access was both "ignored" and "run-always"'d
        let test3_network_ci = RustCoverageIdentifier::NetworkDependency(
            UnifiedSocketAddr::Inet(create_socket_addr("192.168.1.1", 53)).into(),
        );
        coverage_data.add_heuristic_coverage_to_test(TEST3.clone(), test3_network_ci.clone());

        // run-if-files-changed case
        let test4_network_ci = RustCoverageIdentifier::NetworkDependency(
            UnifiedSocketAddr::Inet(create_socket_addr("10.1.1.1", 5432)).into(),
        );
        coverage_data.add_heuristic_coverage_to_test(TEST4.clone(), test4_network_ci.clone());

        let test_result = compute_tests_from_network_accesses::<RustTestPlatform>(
            &coverage_data,
            policies,
            &HashSet::from([PathBuf::from("db/postgres/2024_schema.sql")]),
        );

        let test1_reasons = test_result.get(&TEST1);
        assert!(test1_reasons.is_some());
        let test1_reasons = test1_reasons.unwrap();
        assert!(test1_reasons.contains(&TestReason::CoverageIdentifier(test1_network_ci)));

        let test2_reasons = test_result.get(&TEST2);
        assert!(test2_reasons.is_none());

        let test3_reasons = test_result.get(&TEST3);
        assert!(test3_reasons.is_some());
        let test3_reasons = test3_reasons.unwrap();
        assert!(test3_reasons.contains(&TestReason::SideEffect(
            Box::new(TestReason::CoverageIdentifier(test3_network_ci)),
            Box::new(TestReason::NetworkPolicy("local router access".to_string())),
        )));

        let test4_reasons = test_result.get(&TEST4);
        assert!(test4_reasons.is_some());
        let test4_reasons = test4_reasons.unwrap();
        assert!(
            test4_reasons.contains(&TestReason::SideEffect(
                Box::new(TestReason::CoverageIdentifier(test4_network_ci)),
                Box::new(TestReason::NetworkPolicy(
                    "PostgreSQL server (db/postgres/2024_schema.sql)".to_string()
                )),
            )),
            "test4_reasons missing expected contents, was: {test4_reasons:?}"
        );
    }

    #[test]
    fn test_analyze_nscd() -> Result<()> {
        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Unix(PathBuf::from("/var/run/.pgsql.5432")),
            state: SocketCaptureState::Complete(vec![]),
        };
        let mut hashmap = HashMap::new();
        let result = analyze_nscd(&cap, &mut hashmap)?;
        if let DnsCaptureAnalysisResult::NotApplicable = result {
            assert_eq!(hashmap.len(), 0);
        } else {
            panic!("expected DnsCaptureAnalysisResult::NotApplicable, but was {result:?}");
        }
        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Unix(PathBuf::from("/var/run/.pgsql.5432")),
            state: SocketCaptureState::Incomplete,
        };
        let mut hashmap = HashMap::new();
        let result = analyze_nscd(&cap, &mut hashmap)?;
        // It's important that Incomplete & wrong-socket gives us the NotApplicable result -- because InComplete will
        // cause a warning/error/something when it is on an applicable socket.
        if let DnsCaptureAnalysisResult::NotApplicable = result {
            assert_eq!(hashmap.len(), 0);
        } else {
            panic!("expected DnsCaptureAnalysisResult::NotApplicable, but was {result:?}");
        }

        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Unix(PathBuf::from("/var/run/nscd/socket")),
            state: SocketCaptureState::Incomplete,
        };
        let mut hashmap = HashMap::new();
        let result = analyze_nscd(&cap, &mut hashmap)?;
        if let DnsCaptureAnalysisResult::IncompleteCapture = result {
            assert_eq!(hashmap.len(), 0);
        } else {
            panic!("expected DnsCaptureAnalysisResult::IncompleteCapture, but was {result:?}");
        }

        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Unix(PathBuf::from("/var/run/nscd/socket")),
            state: SocketCaptureState::Complete(vec![
                // A single request, but we'll split it into multiple send operations since this is common:
                SocketOperation::Sent(Vec::from(b"\x02\x00\x00\x00\x0e\x00\x00\x00\x0b")),
                SocketOperation::Sent(Vec::from(
                    b"\x00\x00\x00\x67\x6f\x6f\x67\x6c\x65\x2e\x63\x6f\x6d\x00",
                )),
                // Similar, single response, split up:
                SocketOperation::Read(Vec::from(b"\x02\x00\x00\x00\x01\x00\x00\x00\x02")),
                SocketOperation::Read(Vec::from(b"\x00\x00\x00\x14\x00\x00\x00\n\x00\x00\x00\x00")),
                SocketOperation::Read(Vec::from(b"\x00\x00\x00&\x07\xf8\xb0@\n\x08\x01")),
                SocketOperation::Read(Vec::from(b"\x00\x00\x00\x00\x00\x00 \x03")),
                SocketOperation::Read(Vec::from(b"\xac\xd9\x0e\xc3\n\x02google.ca\x00")),
            ]),
        };
        let mut hashmap = HashMap::new();
        let result = analyze_nscd(&cap, &mut hashmap)?;
        if let DnsCaptureAnalysisResult::Data = result {
            let ip6 = IpAddr::from_str("2607:f8b0:400a:801::2003")?;
            assert_eq!(
                hashmap.get(&ip6),
                Some(&HashSet::from([
                    String::from("google.com"),
                    String::from("google.ca")
                ]))
            );

            let ip4 = IpAddr::from_str("172.217.14.195")?;
            assert_eq!(
                hashmap.get(&ip4),
                Some(&HashSet::from([
                    String::from("google.com"),
                    String::from("google.ca")
                ]))
            );
        } else {
            panic!("expected DnsCaptureAnalysisResult::Data, but was {result:?}");
        }

        Ok(())
    }

    #[test]
    fn test_analyze_dns() -> Result<()> {
        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Inet(SocketAddr::V4(SocketAddrV4::new(
                Ipv4Addr::new(100, 100, 100, 100),
                443,
            ))),
            state: SocketCaptureState::Complete(vec![]),
        };
        let mut hashmap = HashMap::new();
        let result = analyze_dns(&cap, &mut hashmap)?;
        if let DnsCaptureAnalysisResult::NotApplicable = result {
            assert_eq!(hashmap.len(), 0);
        } else {
            panic!("expected DnsCaptureAnalysisResult::NotApplicable, but was {result:?}");
        }
        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Inet(SocketAddr::V4(SocketAddrV4::new(
                Ipv4Addr::new(100, 100, 100, 100),
                443,
            ))),
            state: SocketCaptureState::Incomplete,
        };
        let mut hashmap = HashMap::new();
        let result = analyze_dns(&cap, &mut hashmap)?;
        // It's important that Incomplete & wrong-socket gives us the NotApplicable result -- because InComplete will
        // cause a warning/error/something when it is on an applicable socket.
        if let DnsCaptureAnalysisResult::NotApplicable = result {
            assert_eq!(hashmap.len(), 0);
        } else {
            panic!("expected DnsCaptureAnalysisResult::NotApplicable, but was {result:?}");
        }

        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Inet(SocketAddr::V4(SocketAddrV4::new(
                Ipv4Addr::new(100, 100, 100, 100),
                53,
            ))),
            state: SocketCaptureState::Incomplete,
        };
        let mut hashmap = HashMap::new();
        let result = analyze_dns(&cap, &mut hashmap)?;
        if let DnsCaptureAnalysisResult::IncompleteCapture = result {
            assert_eq!(hashmap.len(), 0);
        } else {
            panic!("expected DnsCaptureAnalysisResult::IncompleteCapture, but was {result:?}");
        }

        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Inet(SocketAddr::V4(SocketAddrV4::new(
                Ipv4Addr::new(100, 100, 100, 100),
                53
            ))),
            state: SocketCaptureState::Complete(vec![
                // Note: analyze_dns doesn't currently use the sent data, so we don't reproduce it here for this test.
                SocketOperation::Read(Vec::from(b"\x07a\x81\x80\x00\x01\x00\x01\x00\x00\x00\x01\x08codeberg\x03org\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x0b\x1b\x00\x04\xd9\xc5[\x91\x00\x00)\x04\xd0\x00\x00\x00\x00\x00\x00")),
                SocketOperation::Read(Vec::from(b"\xccd\x81\x80\x00\x01\x00\x01\x00\x00\x00\x01\x08codeberg\x03org\x00\x00\x1c\x00\x01\xc0\x0c\x00\x1c\x00\x01\x00\x00\x0b\x1b\x00\x10 \x01\x06|\x14\x01 \xf0\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00)\x04\xd0\x00\x00\x00\x00\x00\x00")),
            ]),
        };
        let mut hashmap = HashMap::new();
        let result = analyze_dns(&cap, &mut hashmap)?;
        println!("hashmap: {hashmap:?}");
        if let DnsCaptureAnalysisResult::Data = result {
            let ip6 = IpAddr::from_str("2001:67c:1401:20f0::1")?;
            assert_eq!(
                hashmap.get(&ip6),
                Some(&HashSet::from([String::from("codeberg.org")]))
            );

            let ip4 = IpAddr::from_str("217.197.91.145")?;
            assert_eq!(
                hashmap.get(&ip4),
                Some(&HashSet::from([String::from("codeberg.org")]))
            );
        } else {
            panic!("expected DnsCaptureAnalysisResult::Data, but was {result:?}");
        }

        let cap = SocketCapture {
            socket_addr: UnifiedSocketAddr::Inet(SocketAddr::V4(SocketAddrV4::new(
                Ipv4Addr::new(100, 100, 100, 100),
                53
            ))),
            state: SocketCaptureState::Complete(vec![
                // Note: analyze_dns doesn't currently use the sent data, so we don't reproduce it here for this test.
                SocketOperation::Read(Vec::from(b"\xfc\x1f\x81\x80\x00\x01\x00\x04\x00\x00\x00\x01\x03api\x05nuget\x03org\x00\x00\x01\x00\x01\xc0\x0c\x00\x05\x00\x01\x00\x00\x01,\x00!\x0cnugetapiprod\x0etrafficmanager\x03net\x00\xc0+\x00\x05\x00\x01\x00\x00\x00\xb4\x00\x15\x08az320820\x02vo\x06msecnd\xc0G\xc0X\x00\x05\x00\x01\x00\x00\x0e\x10\x00\x13\x03cs2\x03wpc\x08gammacdn\xc0G\xc0y\x00\x01\x00\x01\x00\x00\x0e\x10\x00\x04\x98\xc7\x04\xb8\x00\x00)\x04\xd0\x00\x00\x00\x00\x00\x00")),
                SocketOperation::Read(Vec::from(b"\xd5\x1e\x81\x80\x00\x01\x00\x03\x00\x01\x00\x01\x03api\x05nuget\x03org\x00\x00\x1c\x00\x01\xc0\x0c\x00\x05\x00\x01\x00\x00\x01,\x00!\x0cnugetapiprod\x0etrafficmanager\x03net\x00\xc0+\x00\x05\x00\x01\x00\x00\x00\xb4\x00\x15\x08az320820\x02vo\x06msecnd\xc0G\xc0X\x00\x05\x00\x01\x00\x00\x0e\x10\x00\x13\x03cs2\x03wpc\x08gammacdn\xc0G\xc0}\x00\x06\x00\x01\x00\x00\x02X\x00,\x03ns1\xc0\x81\x03noc\x08edgecast\x03com\x00ga}\xaa\x00\x00\x0e\x10\x00\x00\x02X\x00\t:\x80\x00\x00\x02X\x00\x00)\x04\xd0\x00\x00\x00\x00\x00\x00")),
            ]),
        };
        let mut hashmap = HashMap::new();
        let result = analyze_dns(&cap, &mut hashmap)?;
        println!("hashmap: {hashmap:?}");
        if let DnsCaptureAnalysisResult::Data = result {
            let ip4 = IpAddr::from_str("152.199.4.184")?;
            assert_eq!(
                hashmap.get(&ip4),
                Some(&HashSet::from([String::from("api.nuget.org")]))
            );
        } else {
            panic!("expected DnsCaptureAnalysisResult::Data, but was {result:?}");
        }

        Ok(())
    }
}