multiprobe 0.5.0

Multi-protocol network probing with BGP-correlated divergence analysis, AS-Boundary Divergence Score (ABDS), Protocol Divergence Localization, Paris Traceroute, and path analytics
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
//! BGP-Correlated Protocol Divergence Analysis
//!
//! Novel contribution: Correlates active multi-protocol path measurements
//! with BGP routing state to classify divergence causes.

use std::collections::HashMap;
use std::net::IpAddr;
use std::time::Duration;

use crate::divergence::{
    DivergenceOptions, DivergenceProtocol, HopStatus, analyze_divergence,
};
use crate::bgp::asn_lookup::{AsnLookup, AsnInfo};
use crate::Error;

/// Classification of why protocol divergence occurred
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DivergenceCause {
    /// Divergence at AS boundary - likely routing policy
    AsBoundaryPolicy {
        from_asn: u32,
        to_asn: u32,
    },

    /// Divergence within single AS - likely internal firewall/ACL
    IntraAsPolicy {
        asn: u32,
    },

    /// Divergence at cloud provider edge
    CloudProviderEdge {
        provider_asn: u32,
        provider_name: String,
    },

    /// Divergence at transit provider
    TransitProvider {
        transit_asn: u32,
    },

    /// Cannot determine cause (private IPs, lookup failure)
    Unknown,
}

impl std::fmt::Display for DivergenceCause {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::AsBoundaryPolicy { from_asn, to_asn } => {
                write!(f, "AS boundary policy (AS{} -> AS{})", from_asn, to_asn)
            }
            Self::IntraAsPolicy { asn } => {
                write!(f, "Intra-AS policy (within AS{})", asn)
            }
            Self::CloudProviderEdge { provider_name, .. } => {
                write!(f, "Cloud provider edge ({})", provider_name)
            }
            Self::TransitProvider { transit_asn } => {
                write!(f, "Transit provider policy (AS{})", transit_asn)
            }
            Self::Unknown => write!(f, "Unknown cause"),
        }
    }
}

/// AS-Boundary Divergence Score (ABDS)
///
/// Novel metric quantifying correlation between protocol divergence
/// and AS boundaries. Higher score indicates divergence correlates
/// with AS transitions.
#[derive(Debug, Clone)]
pub struct AsBoundaryDivergenceScore {
    /// Total divergent hops
    pub total_divergent: usize,

    /// Divergent hops at AS boundaries
    pub at_boundary: usize,

    /// Divergent hops within same AS
    pub intra_as: usize,

    /// Divergent hops at unknown locations (private IPs)
    pub unknown: usize,

    /// ABDS score: at_boundary / total_divergent (0.0 to 1.0)
    /// High score = divergence correlates with AS boundaries (routing policy)
    /// Low score = divergence within ASes (internal firewall)
    pub score: f64,

    /// Interpretation
    pub interpretation: AbdsInterpretation,
}

/// Interpretation of ABDS score
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AbdsInterpretation {
    /// Score >= 0.8: Divergence strongly correlates with AS boundaries
    RoutingPolicyDriven,

    /// Score 0.4-0.8: Mixed causes
    Mixed,

    /// Score < 0.4: Divergence mostly within ASes
    InternalPolicyDriven,

    /// No divergence detected
    NoDivergence,
}

impl std::fmt::Display for AbdsInterpretation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::RoutingPolicyDriven => write!(f, "Routing policy driven"),
            Self::Mixed => write!(f, "Mixed causes"),
            Self::InternalPolicyDriven => write!(f, "Internal policy driven"),
            Self::NoDivergence => write!(f, "No divergence"),
        }
    }
}

/// A single hop with BGP correlation data
#[derive(Debug, Clone)]
pub struct BgpCorrelatedHop {
    /// TTL/hop number
    pub ttl: u8,

    /// IP address (if responded)
    pub addr: Option<IpAddr>,

    /// ASN information (if resolvable)
    pub asn_info: Option<AsnInfo>,

    /// Protocol results at this hop
    pub protocol_results: HashMap<DivergenceProtocol, HopStatus>,

    /// Whether divergence occurred at this hop
    pub has_divergence: bool,

    /// Whether this hop is at an AS boundary
    pub is_as_boundary: bool,

    /// Previous hop's ASN (for boundary detection)
    pub prev_asn: Option<u32>,

    /// Divergence cause classification
    pub divergence_cause: Option<DivergenceCause>,
}

impl BgpCorrelatedHop {
    /// Get ASN if available
    pub fn asn(&self) -> Option<u32> {
        self.asn_info.as_ref().map(|info| info.asn)
    }

    /// Check if at cloud provider edge
    pub fn is_cloud_edge(&self) -> bool {
        self.asn_info.as_ref().map(|i| i.is_cloud_provider()).unwrap_or(false)
            && self.is_as_boundary
    }

    /// Check if at transit provider
    pub fn is_transit(&self) -> bool {
        self.asn_info.as_ref().map(|i| i.is_transit()).unwrap_or(false)
    }
}

/// Complete BGP-correlated divergence result
#[derive(Debug)]
pub struct BgpCorrelatedResult {
    /// Target analyzed
    pub target: String,

    /// Target IP
    pub target_ip: IpAddr,

    /// All hops with BGP correlation
    pub hops: Vec<BgpCorrelatedHop>,

    /// First hop where divergence occurs
    pub first_divergence_hop: Option<u8>,

    /// AS path observed (ASN sequence)
    pub as_path: Vec<u32>,

    /// Number of AS transitions
    pub as_transitions: usize,

    /// AS-Boundary Divergence Score
    pub abds: AsBoundaryDivergenceScore,

    /// Primary divergence cause
    pub primary_cause: DivergenceCause,

    /// Total time for analysis
    pub total_time: Duration,
}

impl BgpCorrelatedResult {
    /// Get summary of findings
    pub fn summary(&self) -> String {
        if self.first_divergence_hop.is_none() {
            return "No protocol divergence detected".to_string();
        }

        format!(
            "Divergence at hop {}: {} (ABDS: {:.2}, {})",
            self.first_divergence_hop.unwrap(),
            self.primary_cause,
            self.abds.score,
            self.abds.interpretation
        )
    }

    /// Get AS path as string
    pub fn as_path_str(&self) -> String {
        self.as_path.iter()
            .map(|asn| format!("AS{}", asn))
            .collect::<Vec<_>>()
            .join(" -> ")
    }

    /// Check if divergence is at a specific AS
    pub fn divergence_at_as(&self, asn: u32) -> bool {
        self.hops.iter().any(|h| {
            h.has_divergence && h.asn() == Some(asn)
        })
    }
}

/// Options for BGP-correlated analysis
#[derive(Debug, Clone)]
pub struct CorrelationOptions {
    /// Divergence analysis options
    pub divergence: DivergenceOptions,

    /// Whether to look up AS names (slower)
    pub lookup_as_names: bool,

    /// Cache ASN lookups
    pub cache_asn: bool,
}

impl Default for CorrelationOptions {
    fn default() -> Self {
        Self {
            divergence: DivergenceOptions::default(),
            lookup_as_names: true,
            cache_asn: true,
        }
    }
}

/// Perform BGP-correlated protocol divergence analysis
pub async fn correlate_divergence(
    target: &str,
    options: &CorrelationOptions,
) -> Result<BgpCorrelatedResult, Error> {
    let start = std::time::Instant::now();

    // Step 1: Run Protocol Divergence Localization
    let divergence_result = analyze_divergence(target, &options.divergence).await?;

    // Step 2: Initialize ASN lookup
    let asn_lookup = AsnLookup::new().await
        .map_err(|e| Error::Dns(e.to_string()))?;

    // Step 3: Look up ASN for each hop
    let mut correlated_hops = Vec::new();
    let mut prev_asn: Option<u32> = None;
    let mut as_path = Vec::new();

    for hop in &divergence_result.hops {
        let addr = hop.results.values()
            .filter_map(|s| s.addr())
            .next();

        let asn_info = if let Some(ip) = addr {
            match asn_lookup.lookup(ip).await {
                Ok(info) => Some(info),
                Err(_) => None,
            }
        } else {
            None
        };

        // Detect AS boundary
        let current_asn = asn_info.as_ref().map(|i| i.asn);
        let is_as_boundary = match (prev_asn, current_asn) {
            (Some(prev), Some(curr)) => prev != curr,
            (None, Some(_)) => true, // First public AS
            _ => false,
        };

        // Track AS path
        if let Some(asn) = current_asn {
            if as_path.last() != Some(&asn) {
                as_path.push(asn);
            }
        }

        // Classify divergence cause
        let divergence_cause = if hop.has_divergence {
            Some(classify_divergence_cause(
                is_as_boundary,
                prev_asn,
                asn_info.as_ref(),
            ))
        } else {
            None
        };

        correlated_hops.push(BgpCorrelatedHop {
            ttl: hop.ttl,
            addr,
            asn_info: asn_info.clone(),
            protocol_results: hop.results.clone(),
            has_divergence: hop.has_divergence,
            is_as_boundary,
            prev_asn,
            divergence_cause,
        });

        if current_asn.is_some() {
            prev_asn = current_asn;
        }
    }

    // Step 4: Calculate ABDS
    let abds = calculate_abds(&correlated_hops);

    // Step 5: Determine primary cause
    let primary_cause = correlated_hops.iter()
        .find(|h| h.has_divergence)
        .and_then(|h| h.divergence_cause.clone())
        .unwrap_or(DivergenceCause::Unknown);

    let as_transitions = as_path.windows(2).count();

    Ok(BgpCorrelatedResult {
        target: divergence_result.target,
        target_ip: divergence_result.target_ip,
        hops: correlated_hops,
        first_divergence_hop: divergence_result.first_divergence_hop,
        as_path,
        as_transitions,
        abds,
        primary_cause,
        total_time: start.elapsed(),
    })
}

/// Classify the cause of divergence at a hop
fn classify_divergence_cause(
    is_as_boundary: bool,
    prev_asn: Option<u32>,
    asn_info: Option<&AsnInfo>,
) -> DivergenceCause {
    match (is_as_boundary, asn_info) {
        (true, Some(info)) if info.is_cloud_provider() => {
            DivergenceCause::CloudProviderEdge {
                provider_asn: info.asn,
                provider_name: info.as_name.clone().unwrap_or_else(|| format!("AS{}", info.asn)),
            }
        }
        (true, Some(info)) if info.is_transit() => {
            DivergenceCause::TransitProvider {
                transit_asn: info.asn,
            }
        }
        (true, Some(info)) => {
            DivergenceCause::AsBoundaryPolicy {
                from_asn: prev_asn.unwrap_or(0),
                to_asn: info.asn,
            }
        }
        (false, Some(info)) => {
            DivergenceCause::IntraAsPolicy {
                asn: info.asn,
            }
        }
        _ => DivergenceCause::Unknown,
    }
}

/// Calculate AS-Boundary Divergence Score
fn calculate_abds(hops: &[BgpCorrelatedHop]) -> AsBoundaryDivergenceScore {
    let divergent_hops: Vec<_> = hops.iter()
        .filter(|h| h.has_divergence)
        .collect();

    let total = divergent_hops.len();

    if total == 0 {
        return AsBoundaryDivergenceScore {
            total_divergent: 0,
            at_boundary: 0,
            intra_as: 0,
            unknown: 0,
            score: 0.0,
            interpretation: AbdsInterpretation::NoDivergence,
        };
    }

    let at_boundary = divergent_hops.iter()
        .filter(|h| h.is_as_boundary && h.asn_info.is_some())
        .count();

    let intra_as = divergent_hops.iter()
        .filter(|h| !h.is_as_boundary && h.asn_info.is_some())
        .count();

    let unknown = divergent_hops.iter()
        .filter(|h| h.asn_info.is_none())
        .count();

    let score = if total > unknown {
        at_boundary as f64 / (total - unknown) as f64
    } else {
        0.0
    };

    let interpretation = if total == 0 {
        AbdsInterpretation::NoDivergence
    } else if score >= 0.8 {
        AbdsInterpretation::RoutingPolicyDriven
    } else if score >= 0.4 {
        AbdsInterpretation::Mixed
    } else {
        AbdsInterpretation::InternalPolicyDriven
    };

    AsBoundaryDivergenceScore {
        total_divergent: total,
        at_boundary,
        intra_as,
        unknown,
        score,
        interpretation,
    }
}

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

    #[test]
    fn test_divergence_cause_display() {
        let cause = DivergenceCause::AsBoundaryPolicy {
            from_asn: 64500,
            to_asn: 64501,
        };
        assert!(cause.to_string().contains("AS64500"));
        assert!(cause.to_string().contains("AS64501"));

        let cause = DivergenceCause::CloudProviderEdge {
            provider_asn: 15169,
            provider_name: "GOOGLE".to_string(),
        };
        assert!(cause.to_string().contains("GOOGLE"));
    }

    #[test]
    fn test_abds_calculation_no_divergence() {
        let hops: Vec<BgpCorrelatedHop> = vec![];
        let abds = calculate_abds(&hops);

        assert_eq!(abds.total_divergent, 0);
        assert_eq!(abds.score, 0.0);
        assert_eq!(abds.interpretation, AbdsInterpretation::NoDivergence);
    }

    #[test]
    fn test_abds_interpretation() {
        // High score = routing policy
        let abds = AsBoundaryDivergenceScore {
            total_divergent: 5,
            at_boundary: 4,
            intra_as: 1,
            unknown: 0,
            score: 0.8,
            interpretation: AbdsInterpretation::RoutingPolicyDriven,
        };
        assert_eq!(abds.interpretation, AbdsInterpretation::RoutingPolicyDriven);

        // Low score = internal policy
        let abds = AsBoundaryDivergenceScore {
            total_divergent: 5,
            at_boundary: 1,
            intra_as: 4,
            unknown: 0,
            score: 0.2,
            interpretation: AbdsInterpretation::InternalPolicyDriven,
        };
        assert_eq!(abds.interpretation, AbdsInterpretation::InternalPolicyDriven);
    }

    #[test]
    fn test_classify_divergence_cause() {
        // At AS boundary with cloud provider
        let google_info = AsnInfo {
            asn: 15169,
            prefix: "8.8.8.0/24".to_string(),
            country: Some("US".to_string()),
            registry: None,
            allocated: None,
            as_name: Some("GOOGLE".to_string()),
        };

        let cause = classify_divergence_cause(true, Some(64500), Some(&google_info));
        assert!(matches!(cause, DivergenceCause::CloudProviderEdge { .. }));

        // Within AS
        let cause = classify_divergence_cause(false, Some(64500), Some(&google_info));
        assert!(matches!(cause, DivergenceCause::IntraAsPolicy { .. }));

        // Unknown
        let cause = classify_divergence_cause(false, None, None);
        assert!(matches!(cause, DivergenceCause::Unknown));
    }

    #[test]
    fn test_abds_realistic_routing_policy_scenario() {
        // Simulate: Home -> ISP (AS7922) -> Transit (AS174) -> Google (AS15169)
        // Divergence occurs at AS boundary (Cogent -> Google) = routing policy
        let hops = vec![
            // Hop 1: Private IP (home router)
            BgpCorrelatedHop {
                ttl: 1,
                addr: Some("192.168.1.1".parse().unwrap()),
                asn_info: None,
                protocol_results: HashMap::new(),
                has_divergence: false,
                is_as_boundary: false,
                prev_asn: None,
                divergence_cause: None,
            },
            // Hop 2: ISP (Comcast)
            BgpCorrelatedHop {
                ttl: 2,
                addr: Some("68.86.85.1".parse().unwrap()),
                asn_info: Some(AsnInfo {
                    asn: 7922,
                    prefix: "68.86.0.0/14".to_string(),
                    country: Some("US".to_string()),
                    registry: None,
                    allocated: None,
                    as_name: Some("COMCAST-7922".to_string()),
                }),
                protocol_results: HashMap::new(),
                has_divergence: false,
                is_as_boundary: true,
                prev_asn: None,
                divergence_cause: None,
            },
            // Hop 3: Transit (Cogent) - AS boundary, no divergence
            BgpCorrelatedHop {
                ttl: 3,
                addr: Some("154.54.30.1".parse().unwrap()),
                asn_info: Some(AsnInfo {
                    asn: 174,
                    prefix: "154.54.0.0/16".to_string(),
                    country: Some("US".to_string()),
                    registry: None,
                    allocated: None,
                    as_name: Some("COGENT-174".to_string()),
                }),
                protocol_results: HashMap::new(),
                has_divergence: false,
                is_as_boundary: true,
                prev_asn: Some(7922),
                divergence_cause: None,
            },
            // Hop 4: Google edge - DIVERGENCE at AS boundary
            BgpCorrelatedHop {
                ttl: 4,
                addr: Some("142.250.169.1".parse().unwrap()),
                asn_info: Some(AsnInfo {
                    asn: 15169,
                    prefix: "142.250.0.0/15".to_string(),
                    country: Some("US".to_string()),
                    registry: None,
                    allocated: None,
                    as_name: Some("GOOGLE".to_string()),
                }),
                protocol_results: HashMap::new(),
                has_divergence: true,
                is_as_boundary: true,
                prev_asn: Some(174),
                divergence_cause: Some(DivergenceCause::CloudProviderEdge {
                    provider_asn: 15169,
                    provider_name: "GOOGLE".to_string(),
                }),
            },
        ];

        let abds = calculate_abds(&hops);

        assert_eq!(abds.total_divergent, 1);
        assert_eq!(abds.at_boundary, 1);
        assert_eq!(abds.intra_as, 0);
        assert_eq!(abds.score, 1.0);
        assert_eq!(abds.interpretation, AbdsInterpretation::RoutingPolicyDriven);
    }

    #[test]
    fn test_abds_realistic_internal_firewall_scenario() {
        // Simulate: Corporate network with internal firewall blocking UDP
        // Divergence occurs WITHIN corporate AS = internal policy
        let corporate_asn = 64496; // Private use ASN
        let hops = vec![
            // Hop 1: Internal router
            BgpCorrelatedHop {
                ttl: 1,
                addr: Some("10.1.1.1".parse().unwrap()),
                asn_info: None,
                protocol_results: HashMap::new(),
                has_divergence: false,
                is_as_boundary: false,
                prev_asn: None,
                divergence_cause: None,
            },
            // Hop 2: Corporate edge (public IP)
            BgpCorrelatedHop {
                ttl: 2,
                addr: Some("203.0.113.1".parse().unwrap()),
                asn_info: Some(AsnInfo {
                    asn: corporate_asn,
                    prefix: "203.0.113.0/24".to_string(),
                    country: Some("US".to_string()),
                    registry: None,
                    allocated: None,
                    as_name: Some("ACME-CORP".to_string()),
                }),
                protocol_results: HashMap::new(),
                has_divergence: false,
                is_as_boundary: true,
                prev_asn: None,
                divergence_cause: None,
            },
            // Hop 3: Firewall - DIVERGENCE within corporate AS
            BgpCorrelatedHop {
                ttl: 3,
                addr: Some("203.0.113.10".parse().unwrap()),
                asn_info: Some(AsnInfo {
                    asn: corporate_asn,
                    prefix: "203.0.113.0/24".to_string(),
                    country: Some("US".to_string()),
                    registry: None,
                    allocated: None,
                    as_name: Some("ACME-CORP".to_string()),
                }),
                protocol_results: HashMap::new(),
                has_divergence: true,
                is_as_boundary: false,
                prev_asn: Some(corporate_asn),
                divergence_cause: Some(DivergenceCause::IntraAsPolicy { asn: corporate_asn }),
            },
            // Hop 4: ISP
            BgpCorrelatedHop {
                ttl: 4,
                addr: Some("198.51.100.1".parse().unwrap()),
                asn_info: Some(AsnInfo {
                    asn: 7922,
                    prefix: "198.51.100.0/24".to_string(),
                    country: Some("US".to_string()),
                    registry: None,
                    allocated: None,
                    as_name: Some("COMCAST".to_string()),
                }),
                protocol_results: HashMap::new(),
                has_divergence: false,
                is_as_boundary: true,
                prev_asn: Some(corporate_asn),
                divergence_cause: None,
            },
        ];

        let abds = calculate_abds(&hops);

        assert_eq!(abds.total_divergent, 1);
        assert_eq!(abds.at_boundary, 0);
        assert_eq!(abds.intra_as, 1);
        assert_eq!(abds.score, 0.0);
        assert_eq!(abds.interpretation, AbdsInterpretation::InternalPolicyDriven);
    }

    #[test]
    fn test_abds_mixed_divergence_scenario() {
        // Multiple divergence points: some at boundaries, some internal
        let hops = vec![
            // Hop 1: First AS - internal divergence
            BgpCorrelatedHop {
                ttl: 1,
                addr: Some("192.0.2.1".parse().unwrap()),
                asn_info: Some(AsnInfo {
                    asn: 64500,
                    prefix: "192.0.2.0/24".to_string(),
                    country: Some("US".to_string()),
                    registry: None,
                    allocated: None,
                    as_name: Some("AS64500".to_string()),
                }),
                protocol_results: HashMap::new(),
                has_divergence: true, // Internal divergence
                is_as_boundary: false,
                prev_asn: None,
                divergence_cause: Some(DivergenceCause::IntraAsPolicy { asn: 64500 }),
            },
            // Hop 2: AS boundary - divergence
            BgpCorrelatedHop {
                ttl: 2,
                addr: Some("198.51.100.1".parse().unwrap()),
                asn_info: Some(AsnInfo {
                    asn: 64501,
                    prefix: "198.51.100.0/24".to_string(),
                    country: Some("US".to_string()),
                    registry: None,
                    allocated: None,
                    as_name: Some("AS64501".to_string()),
                }),
                protocol_results: HashMap::new(),
                has_divergence: true, // Boundary divergence
                is_as_boundary: true,
                prev_asn: Some(64500),
                divergence_cause: Some(DivergenceCause::AsBoundaryPolicy {
                    from_asn: 64500,
                    to_asn: 64501,
                }),
            },
        ];

        let abds = calculate_abds(&hops);

        assert_eq!(abds.total_divergent, 2);
        assert_eq!(abds.at_boundary, 1);
        assert_eq!(abds.intra_as, 1);
        assert_eq!(abds.score, 0.5);
        assert_eq!(abds.interpretation, AbdsInterpretation::Mixed);
    }
}