sciparse 0.6.1

Zero-copy SCION packet parsing, serialization and control plane components
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
// Copyright 2026 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! This module exports functions that allow combining SCION Path Segments into
//! end-to-end Paths. The main exported function is [combine] which serves the
//! same purpose as the `Combine`-method [1] of the SCION reference
//! implementation. The former also served as inspiration for this
//! implementation. Note, however, that we do not commit to maintaining a strict
//! equivalence between this implementation and the `Combine`-function [1].
//!
//! [1]: <https://github.com/scionproto/scion/blob/38e79c43faf38ebec719ac71fb65e1f814050e3b/private/path/combinator/combinator.go#L60>

mod graph;

#[cfg(test)]
mod test_graph;

use std::collections::HashMap;

use graph::number_of_hops;

use crate::{
    identifier::isd_asn::IsdAsn,
    path::{ScionPath, fingerprint::data_plane::DpPathFingerprint},
    segment::{Entry, PathSegment},
};

/// Finds all valid paths from `src` to `dst` that can be combined using the given core and
/// non-core segments using the number of hops as cost.
///
/// Core segments are segments between core ASes, non-core segments are all up and down segments,
/// the algorithm treats up and down segments symmetrically.
///
/// The resulting paths are sorted by cost, number of hops and filtered to remove paths with loops
/// and duplicates.
///
/// The algorithm is based on the scionproto go implementation.
#[inline]
pub fn combine<EntryType: Entry>(
    src: IsdAsn,
    dst: IsdAsn,
    cores: Vec<PathSegment<EntryType>>,
    non_cores: Vec<PathSegment<EntryType>>,
) -> Vec<ScionPath> {
    combine_with_weight_fn(src, dst, cores, non_cores, number_of_hops)
}

/// Finds all valid paths from `src` to `dst` that can be combined using the given core and non-core
/// segments.
///
/// Core segments are segments between core ASes, non-core segments are all up and down segments,
/// the algorithm treats up and down segments symmetrically.
///
/// The weight function is used to calculate the cost using a segment given
/// the segment, the shortcut index and a boolean indicating if the segment is used towards a peer.
///
/// The resulting paths are sorted by cost, number of hops and filtered to remove paths with loops
/// and duplicates.
///
/// The algorithm is based on the scionproto go implementation.
#[inline]
pub fn combine_with_weight_fn<EntryType: Entry>(
    src: IsdAsn,
    dst: IsdAsn,
    cores: Vec<PathSegment<EntryType>>,
    non_cores: Vec<PathSegment<EntryType>>,
    weight_fn: impl Fn(&graph::InputSegment<EntryType>, u64, bool) -> u64,
) -> Vec<ScionPath> {
    if src == dst {
        return vec![];
    }
    let mut graph = graph::MultiGraph::new(weight_fn);
    let segments = cores
        .iter()
        .map(graph::InputSegment::new_core)
        .chain(non_cores.iter().map(graph::InputSegment::new_non_core))
        .collect::<Vec<_>>();
    graph.add_segments(segments.as_slice());
    let solutions = graph.get_paths(src, dst);
    let paths = solutions
        .iter()
        .filter_map(|s| s.path().ok().flatten())
        .filter(|p| !has_loops(p))
        .collect();
    filter_duplicates(paths)
}

/// Returns true if the path has more than 2 interfaces from the same AS. I.e.
/// if it has a loop.
#[inline]
fn has_loops(path: &ScionPath) -> bool {
    let mut ia_counts = HashMap::new();
    for i in path.metadata.as_ref().unwrap().interfaces.as_ref().unwrap() {
        *ia_counts.entry(i.interface.isd_asn).or_insert(0) += 1;
    }
    ia_counts.values().any(|v| *v > 2)
}

/// filter_duplicates removes paths with identical sequences of path interfaces,
/// keeping only the one instance with latest expiry.
/// Duplicates can arise when multiple combinations of different path segments
/// result in the same "effective" path after applying short-cuts.
/// XXX(uniquefine): the duplicates could/should be avoided directly by reducing the
/// available options in the graph, as we could potentially create a large
/// number of duplicates in wide network topologies.
#[inline]
fn filter_duplicates(paths: Vec<ScionPath>) -> Vec<ScionPath> {
    // Store the index of the path with the latest expiry for every unique path fingerprint.
    let mut path_result = Vec::new();
    let mut unique_paths: HashMap<DpPathFingerprint, (u32, usize)> = HashMap::new();
    for path in paths.into_iter() {
        let fingerprint = path.fingerprint();

        match unique_paths.entry(fingerprint) {
            // If we already have a path with the same fingerprint, compare the expiration and keep
            // the one with the later expiration.
            std::collections::hash_map::Entry::Occupied(mut entry) => {
                let (current_expiration, existing_vec_index) = entry.get_mut();
                let new_expiration = path.expiration().unwrap_or(0);

                if new_expiration > *current_expiration {
                    *current_expiration = new_expiration;
                    path_result[*existing_vec_index] = path;
                }
            }
            // Otherwise, collect the path index
            std::collections::hash_map::Entry::Vacant(entry) => {
                let vec_index = path_result.len();
                let expiration = path.expiration().unwrap_or(0);

                path_result.push(path);

                entry.insert((expiration, vec_index));
            }
        }
    }

    path_result
}

#[cfg(test)]
mod tests {
    use super::{test_graph::default_graph, *};
    use crate::segment::UnsignedPathSegment;

    struct TestCase {
        src: IsdAsn,
        dst: IsdAsn,
        cores: Vec<UnsignedPathSegment>,
        non_cores: Vec<UnsignedPathSegment>,
        expected_paths: Vec<Vec<(IsdAsn, u16)>>,
    }

    /// Convert a list of interface strings like "1-ff00:0:131#479" to (IsdAsn, id) tuples.
    fn interfaces(intfs: &[&str]) -> Vec<(IsdAsn, u16)> {
        intfs
            .iter()
            .map(|s| {
                let (ia_str, id_str) = s.split_once('#').unwrap();
                (ia_str.parse().unwrap(), id_str.parse().unwrap())
            })
            .collect()
    }

    fn run_test(test_case: TestCase) {
        let paths = combine(
            test_case.src,
            test_case.dst,
            test_case.cores,
            test_case.non_cores,
        );
        let actual: Vec<Vec<(IsdAsn, u16)>> = paths
            .iter()
            .map(|p| {
                p.metadata
                    .as_ref()
                    .unwrap()
                    .interfaces
                    .as_ref()
                    .unwrap()
                    .iter()
                    .map(|im| (im.interface.isd_asn, im.interface.id))
                    .collect()
            })
            .collect();
        assert_eq!(test_case.expected_paths, actual);
    }

    /// Set the MTU for all ASes, links and peer links in the segment.
    fn set_mtus(segment: &mut UnsignedPathSegment, mtu: u16) {
        for (i, as_entry) in segment.as_entries.iter_mut().enumerate() {
            as_entry.mtu = mtu as u32;
            if i != 0 {
                as_entry.hop_entry.ingress_mtu = mtu;
            }
            for peer_entry in as_entry.peer_entries.iter_mut() {
                peer_entry.peer_mtu = mtu;
            }
        }
    }

    // The test cases are based on the following graph, use
    // <https://mermaid.live> to visualize the graph.
    //
    // ```mermaid
    //     graph TD;
    // 1-ff00:0:120 -->|1->105| 1-ff00:0:130;
    // 1-ff00:0:120 -->|4->3| 1-ff00:0:121;
    // 1-ff00:0:120 -->|2->501| 2-ff00:0:220;
    // 1-ff00:0:120 -->|6->1| 1-ff00:0:110;
    // 1-ff00:0:120 -->|3->502| 2-ff00:0:220;
    // 1-ff00:0:120 -->|5->104| 1-ff00:0:111;
    // 2-ff00:0:222 -->|302->1| 2-ff00:0:221;
    // 2-ff00:0:222 -->|301->4| 2-ff00:0:211;
    // 1-ff00:0:121 -->|1->480,peer| 1-ff00:0:131;
    // 1-ff00:0:121 -->|4->100,peer| 1-ff00:0:111;
    // 1-ff00:0:121 -->|2->2| 1-ff00:0:122;
    // 1-ff00:0:110 -->|3->453| 2-ff00:0:210;
    // 1-ff00:0:110 -->|2->104| 1-ff00:0:130;
    // 1-ff00:0:112 -->|494->103| 1-ff00:0:111;
    // 1-ff00:0:112 -->|495->113| 1-ff00:0:130;
    // 1-ff00:0:122 -->|1->1,peer| 1-ff00:0:133;
    // 2-ff00:0:221 -->|3->1,peer| 2-ff00:0:211;
    // 2-ff00:0:221 -->|2->500| 2-ff00:0:220;
    // 2-ff00:0:212 -->|201->2| 2-ff00:0:211;
    // 2-ff00:0:212 -->|200->3| 2-ff00:0:211;
    // 1-ff00:0:130 -->|111->479| 1-ff00:0:131;
    // 1-ff00:0:130 -->|112->105| 1-ff00:0:111;
    // 2-ff00:0:220 -->|503->450| 2-ff00:0:210;
    // 2-ff00:0:211 -->|6->102,peer| 1-ff00:0:111;
    // 2-ff00:0:211 -->|7->451| 2-ff00:0:210;
    // 2-ff00:0:211 -->|5->101,peer| 1-ff00:0:111;
    // 2-ff00:0:211 -->|8->452| 2-ff00:0:210;
    // 1-ff00:0:131 -->|478->2| 1-ff00:0:132;
    // 1-ff00:0:133 -->|2->1| 1-ff00:0:132;
    // ```

    #[test]
    fn test_00_simple_up_core_down() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:131".parse().unwrap(),
            dst: "1-ff00:0:111".parse().unwrap(),
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111]).unwrap(),
                // down segment
                g.beacon("1-ff00:0:120".parse().unwrap(), &[5]).unwrap(),
            ],
            cores: vec![g.beacon("1-ff00:0:120".parse().unwrap(), &[1]).unwrap()],
            expected_paths: vec![interfaces(&[
                "1-ff00:0:131#479",
                "1-ff00:0:130#111",
                "1-ff00:0:130#105",
                "1-ff00:0:120#1",
                "1-ff00:0:120#5",
                "1-ff00:0:111#104",
            ])],
        });
    }

    #[test]
    fn test_01_simple_up_core() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:131".parse().unwrap(),
            dst: "1-ff00:0:110".parse().unwrap(),
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111]).unwrap(),
            ],
            cores: vec![g.beacon("1-ff00:0:110".parse().unwrap(), &[2]).unwrap()],
            expected_paths: vec![interfaces(&[
                "1-ff00:0:131#479",
                "1-ff00:0:130#111",
                "1-ff00:0:130#104",
                "1-ff00:0:110#2",
            ])],
        });
    }

    #[test]
    fn test_02_simple_up_only() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:131".parse().unwrap(),
            dst: "1-ff00:0:130".parse().unwrap(),
            cores: vec![],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111]).unwrap(),
            ],
            expected_paths: vec![interfaces(&["1-ff00:0:131#479", "1-ff00:0:130#111"])],
        });
    }

    #[test]
    fn test_03_simple_core_down() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:130".parse().unwrap(),
            dst: "1-ff00:0:121".parse().unwrap(),
            cores: vec![g.beacon("1-ff00:0:120".parse().unwrap(), &[1]).unwrap()],
            non_cores: vec![
                // down segment
                g.beacon("1-ff00:0:120".parse().unwrap(), &[4]).unwrap(),
            ],
            expected_paths: vec![interfaces(&[
                "1-ff00:0:130#105",
                "1-ff00:0:120#1",
                "1-ff00:0:120#4",
                "1-ff00:0:121#3",
            ])],
        });
    }

    #[test]
    fn test_04_simple_down_only() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:130".parse().unwrap(),
            dst: "1-ff00:0:111".parse().unwrap(),
            cores: vec![],
            non_cores: vec![
                // down segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[112]).unwrap(),
            ],
            expected_paths: vec![interfaces(&["1-ff00:0:130#112", "1-ff00:0:111#105"])],
        });
    }

    #[test]
    fn test_05_inverted_core() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:131".parse().unwrap(),
            dst: "1-ff00:0:111".parse().unwrap(),
            cores: vec![g.beacon("1-ff00:0:130".parse().unwrap(), &[105]).unwrap()],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111]).unwrap(),
                // down segment
                g.beacon("1-ff00:0:120".parse().unwrap(), &[5]).unwrap(),
            ],
            // The behavior differs from the scionproto implementation. We do allow
            // inverted core segments and traverse them in the reverse direction setting cons_dir to
            // true.
            expected_paths: vec![interfaces(&[
                "1-ff00:0:131#479",
                "1-ff00:0:130#111",
                "1-ff00:0:130#105",
                "1-ff00:0:120#1",
                "1-ff00:0:120#5",
                "1-ff00:0:111#104",
            ])],
        });
    }

    #[test]
    fn test_06_simple_long_up_core_down() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:132".parse().unwrap(),
            dst: "2-ff00:0:212".parse().unwrap(),
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111, 478])
                    .unwrap(),
                // down segment
                g.beacon("2-ff00:0:210".parse().unwrap(), &[451, 2])
                    .unwrap(),
            ],
            cores: vec![
                g.beacon("2-ff00:0:210".parse().unwrap(), &[453, 2])
                    .unwrap(),
            ],
            expected_paths: vec![interfaces(&[
                "1-ff00:0:132#2",
                "1-ff00:0:131#478",
                "1-ff00:0:131#479",
                "1-ff00:0:130#111",
                "1-ff00:0:130#104",
                "1-ff00:0:110#2",
                "1-ff00:0:110#3",
                "2-ff00:0:210#453",
                "2-ff00:0:210#451",
                "2-ff00:0:211#7",
                "2-ff00:0:211#2",
                "2-ff00:0:212#201",
            ])],
        });
    }

    #[test]
    fn test_07_missing_up() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:132".parse().unwrap(),
            dst: "1-ff00:0:122".parse().unwrap(),
            cores: vec![g.beacon("1-ff00:0:120".parse().unwrap(), &[6, 2]).unwrap()],
            non_cores: vec![g.beacon("1-ff00:0:120".parse().unwrap(), &[4, 2]).unwrap()],
            expected_paths: vec![],
        });
    }

    #[test]
    fn test_08_missing_core() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:132".parse().unwrap(),
            dst: "2-ff00:0:211".parse().unwrap(),
            cores: vec![],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111, 478])
                    .unwrap(),
                // down segment
                g.beacon("2-ff00:0:210".parse().unwrap(), &[451]).unwrap(),
            ],
            expected_paths: vec![],
        });
    }

    #[test]
    fn test_09_missing_down() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:132".parse().unwrap(),
            dst: "1-ff00:0:122".parse().unwrap(),
            cores: vec![
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111, 478])
                    .unwrap(),
            ],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:120".parse().unwrap(), &[1]).unwrap(),
            ],
            expected_paths: vec![],
        });
    }

    #[test]
    fn test_10_simple_up_core_down_multiple_cores() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:132".parse().unwrap(),
            dst: "1-ff00:0:112".parse().unwrap(),
            cores: vec![
                g.beacon("1-ff00:0:120".parse().unwrap(), &[6, 2]).unwrap(),
                g.beacon("1-ff00:0:120".parse().unwrap(), &[1]).unwrap(),
            ],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111, 478])
                    .unwrap(),
                // down segment
                g.beacon("1-ff00:0:120".parse().unwrap(), &[5, 103])
                    .unwrap(),
            ],
            expected_paths: vec![
                interfaces(&[
                    "1-ff00:0:132#2",
                    "1-ff00:0:131#478",
                    "1-ff00:0:131#479",
                    "1-ff00:0:130#111",
                    "1-ff00:0:130#105",
                    "1-ff00:0:120#1",
                    "1-ff00:0:120#5",
                    "1-ff00:0:111#104",
                    "1-ff00:0:111#103",
                    "1-ff00:0:112#494",
                ]),
                interfaces(&[
                    "1-ff00:0:132#2",
                    "1-ff00:0:131#478",
                    "1-ff00:0:131#479",
                    "1-ff00:0:130#111",
                    "1-ff00:0:130#104",
                    "1-ff00:0:110#2",
                    "1-ff00:0:110#1",
                    "1-ff00:0:120#6",
                    "1-ff00:0:120#5",
                    "1-ff00:0:111#104",
                    "1-ff00:0:111#103",
                    "1-ff00:0:112#494",
                ]),
            ],
        });
    }

    #[test]
    fn test_11_shortcut_destination_on_path_going_up_vonly_hf_is_from_core() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:133".parse().unwrap(),
            dst: "1-ff00:0:131".parse().unwrap(),
            cores: vec![],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111, 478, 1])
                    .unwrap(),
            ],
            expected_paths: vec![interfaces(&[
                "1-ff00:0:133#2",
                "1-ff00:0:132#1",
                "1-ff00:0:132#2",
                "1-ff00:0:131#478",
            ])],
        });
    }

    #[test]
    fn test_12_shortcut_destination_on_path_going_up_vonly_hf_is_non_core() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:133".parse().unwrap(),
            dst: "1-ff00:0:132".parse().unwrap(),
            cores: vec![],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111, 478, 1])
                    .unwrap(),
            ],
            expected_paths: vec![interfaces(&["1-ff00:0:133#2", "1-ff00:0:132#1"])],
        });
    }

    #[test]
    fn test_13_shortcut_destination_on_path_going_down_verify_hf_is_from_core() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:131".parse().unwrap(),
            dst: "1-ff00:0:132".parse().unwrap(),
            cores: vec![],
            non_cores: vec![
                // down segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111, 478])
                    .unwrap(),
            ],
            expected_paths: vec![interfaces(&["1-ff00:0:131#478", "1-ff00:0:132#2"])],
        });
    }

    #[test]
    fn test_14_shortcut_common_upstream() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "2-ff00:0:212".parse().unwrap(),
            dst: "2-ff00:0:222".parse().unwrap(),
            cores: vec![],
            non_cores: vec![
                // up segment
                g.beacon("2-ff00:0:210".parse().unwrap(), &[452, 2])
                    .unwrap(),
                g.beacon("2-ff00:0:210".parse().unwrap(), &[452, 4])
                    .unwrap(),
            ],
            expected_paths: vec![interfaces(&[
                "2-ff00:0:212#201",
                "2-ff00:0:211#2",
                "2-ff00:0:211#4",
                "2-ff00:0:222#301",
            ])],
        });
    }

    #[test]
    fn test_15_go_through_peer() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "2-ff00:0:212".parse().unwrap(),
            dst: "2-ff00:0:222".parse().unwrap(),
            cores: vec![g.beacon("2-ff00:0:220".parse().unwrap(), &[503]).unwrap()],
            non_cores: vec![
                // up segment
                g.beacon("2-ff00:0:210".parse().unwrap(), &[452, 2])
                    .unwrap(),
                // down segment
                g.beacon("2-ff00:0:220".parse().unwrap(), &[500, 1])
                    .unwrap(),
            ],
            expected_paths: vec![
                interfaces(&[
                    "2-ff00:0:212#201",
                    "2-ff00:0:211#2",
                    "2-ff00:0:211#1",
                    "2-ff00:0:221#3",
                    "2-ff00:0:221#1",
                    "2-ff00:0:222#302",
                ]),
                interfaces(&[
                    "2-ff00:0:212#201",
                    "2-ff00:0:211#2",
                    "2-ff00:0:211#8",
                    "2-ff00:0:210#452",
                    "2-ff00:0:210#450",
                    "2-ff00:0:220#503",
                    "2-ff00:0:220#500",
                    "2-ff00:0:221#2",
                    "2-ff00:0:221#1",
                    "2-ff00:0:222#302",
                ]),
            ],
        });
    }

    #[test]
    fn test_16_start_from_peer() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:131".parse().unwrap(),
            dst: "1-ff00:0:122".parse().unwrap(),
            cores: vec![g.beacon("1-ff00:0:120".parse().unwrap(), &[1]).unwrap()],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111]).unwrap(),
                // down segment
                g.beacon("1-ff00:0:120".parse().unwrap(), &[4, 2]).unwrap(),
            ],
            expected_paths: vec![
                interfaces(&[
                    "1-ff00:0:131#480",
                    "1-ff00:0:121#1",
                    "1-ff00:0:121#2",
                    "1-ff00:0:122#2",
                ]),
                interfaces(&[
                    "1-ff00:0:131#479",
                    "1-ff00:0:130#111",
                    "1-ff00:0:130#105",
                    "1-ff00:0:120#1",
                    "1-ff00:0:120#4",
                    "1-ff00:0:121#3",
                    "1-ff00:0:121#2",
                    "1-ff00:0:122#2",
                ]),
            ],
        });
    }

    #[test]
    fn test_17_start_and_end_on_peer() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:131".parse().unwrap(),
            dst: "1-ff00:0:121".parse().unwrap(),
            cores: vec![g.beacon("1-ff00:0:120".parse().unwrap(), &[1]).unwrap()],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111]).unwrap(),
                // down segment
                g.beacon("1-ff00:0:120".parse().unwrap(), &[4]).unwrap(),
            ],
            expected_paths: vec![
                interfaces(&["1-ff00:0:131#480", "1-ff00:0:121#1"]),
                interfaces(&[
                    "1-ff00:0:131#479",
                    "1-ff00:0:130#111",
                    "1-ff00:0:130#105",
                    "1-ff00:0:120#1",
                    "1-ff00:0:120#4",
                    "1-ff00:0:121#3",
                ]),
            ],
        });
    }

    #[test]
    fn test_18_only_end_on_peer() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:132".parse().unwrap(),
            dst: "1-ff00:0:121".parse().unwrap(),
            cores: vec![g.beacon("1-ff00:0:120".parse().unwrap(), &[1]).unwrap()],
            non_cores: vec![
                // up segment
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111, 478])
                    .unwrap(),
                // down segment
                g.beacon("1-ff00:0:120".parse().unwrap(), &[4]).unwrap(),
            ],
            expected_paths: vec![
                interfaces(&[
                    "1-ff00:0:132#2",
                    "1-ff00:0:131#478",
                    "1-ff00:0:131#480",
                    "1-ff00:0:121#1",
                ]),
                interfaces(&[
                    "1-ff00:0:132#2",
                    "1-ff00:0:131#478",
                    "1-ff00:0:131#479",
                    "1-ff00:0:130#111",
                    "1-ff00:0:130#105",
                    "1-ff00:0:120#1",
                    "1-ff00:0:120#4",
                    "1-ff00:0:121#3",
                ]),
            ],
        });
    }

    #[test]
    fn test_19_dont_use_core_shortcuts() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:110".parse().unwrap(),
            dst: "2-ff00:0:222".parse().unwrap(),
            cores: vec![
                g.beacon("2-ff00:0:210".parse().unwrap(), &[453]).unwrap(),
                g.beacon("2-ff00:0:220".parse().unwrap(), &[503, 453])
                    .unwrap(),
            ],
            non_cores: vec![
                // down segments
                g.beacon("2-ff00:0:210".parse().unwrap(), &[451, 4])
                    .unwrap(),
                g.beacon("2-ff00:0:220".parse().unwrap(), &[500, 1])
                    .unwrap(),
            ],
            expected_paths: vec![
                interfaces(&[
                    "1-ff00:0:110#3",
                    "2-ff00:0:210#453",
                    "2-ff00:0:210#451",
                    "2-ff00:0:211#7",
                    "2-ff00:0:211#4",
                    "2-ff00:0:222#301",
                ]),
                interfaces(&[
                    "1-ff00:0:110#3",
                    "2-ff00:0:210#453",
                    "2-ff00:0:210#450",
                    "2-ff00:0:220#503",
                    "2-ff00:0:220#500",
                    "2-ff00:0:221#2",
                    "2-ff00:0:221#1",
                    "2-ff00:0:222#302",
                ]),
            ],
        });
    }

    #[test]
    fn test_20_core_only() {
        let g = default_graph().unwrap();
        run_test(TestCase {
            src: "1-ff00:0:130".parse().unwrap(),
            dst: "2-ff00:0:210".parse().unwrap(),
            cores: vec![
                g.beacon("2-ff00:0:210".parse().unwrap(), &[453, 2])
                    .unwrap(),
                g.beacon("2-ff00:0:210".parse().unwrap(), &[453, 1, 1])
                    .unwrap(),
                g.beacon("2-ff00:0:210".parse().unwrap(), &[450, 502, 6, 2])
                    .unwrap(),
            ],
            non_cores: vec![],
            expected_paths: vec![
                interfaces(&[
                    "1-ff00:0:130#104",
                    "1-ff00:0:110#2",
                    "1-ff00:0:110#3",
                    "2-ff00:0:210#453",
                ]),
                interfaces(&[
                    "1-ff00:0:130#105",
                    "1-ff00:0:120#1",
                    "1-ff00:0:120#6",
                    "1-ff00:0:110#1",
                    "1-ff00:0:110#3",
                    "2-ff00:0:210#453",
                ]),
                interfaces(&[
                    "1-ff00:0:130#104",
                    "1-ff00:0:110#2",
                    "1-ff00:0:110#1",
                    "1-ff00:0:120#6",
                    "1-ff00:0:120#3",
                    "2-ff00:0:220#502",
                    "2-ff00:0:220#503",
                    "2-ff00:0:210#450",
                ]),
            ],
        });
    }

    #[test]
    fn test_bad_peering() {
        let mut g = default_graph().unwrap();

        // Add a new peering link between AS 111 and AS 121
        g.add_link(
            "1-ff00:0:111".parse().unwrap(),
            4001,
            "1-ff00:0:121".parse().unwrap(),
            4002,
            true,
        )
        .unwrap();

        // Break the peering by deleting one interface
        g.delete_interface("1-ff00:0:121".parse().unwrap(), 4002)
            .unwrap();

        // Also delete the existing 111-121 peering interface (interface 100)
        g.delete_interface("1-ff00:0:111".parse().unwrap(), 100)
            .unwrap();

        run_test(TestCase {
            src: "1-ff00:0:112".parse().unwrap(),
            dst: "1-ff00:0:122".parse().unwrap(),
            non_cores: vec![
                // up segment: 130->111->112 (interfaces 112->105->103->494)
                g.beacon("1-ff00:0:130".parse().unwrap(), &[112, 103])
                    .unwrap(),
                // down segment: 120->121->122 (interfaces 4->3->2->2)
                g.beacon("1-ff00:0:120".parse().unwrap(), &[4, 2]).unwrap(),
            ],
            cores: vec![
                // 120->130 (interface 1->105)
                g.beacon("1-ff00:0:120".parse().unwrap(), &[1]).unwrap(),
            ],
            expected_paths: vec![interfaces(&[
                "1-ff00:0:112#494",
                "1-ff00:0:111#103",
                "1-ff00:0:111#105",
                "1-ff00:0:130#112",
                "1-ff00:0:130#105",
                "1-ff00:0:120#1",
                "1-ff00:0:120#4",
                "1-ff00:0:121#3",
                "1-ff00:0:121#2",
                "1-ff00:0:122#2",
            ])],
        });
    }

    #[test]
    fn test_same_core_parent() {
        let g = default_graph().unwrap();

        run_test(TestCase {
            src: "1-ff00:0:131".parse().unwrap(),
            dst: "1-ff00:0:112".parse().unwrap(),
            non_cores: vec![
                // up segment 130->131 (interface 111->479)
                g.beacon("1-ff00:0:130".parse().unwrap(), &[111]).unwrap(),
                // down segment 130->112 (interface 113->495)
                g.beacon("1-ff00:0:130".parse().unwrap(), &[113]).unwrap(),
            ],
            cores: vec![],
            expected_paths: vec![interfaces(&[
                "1-ff00:0:131#479",
                "1-ff00:0:130#111",
                "1-ff00:0:130#113",
                "1-ff00:0:112#495",
            ])],
        });
    }

    // Create test segments that have MTU 1000 on all hops and links.
    // The segments form the following topology:
    //
    //   ┌───┐     ┌───┐
    //   │130|     │210|
    //   └─┬─┘     └─┬─┘
    //     │         │
    //   ┌─▼─┐     ┌─▼─┐
    //   │111|─ ─ ─┤211|
    //   └─┬─┘     └─┬─┘
    //     │         │
    //   ┌─▼─┐     ┌─▼─┐
    //   │112|     │212|
    //   └───┘     └───┘
    //
    // 130 and 210 are core ASes. The up segment is from 130 to 112.
    // The down segment is from 210 to 212.
    fn mtu_test_segments() -> (UnsignedPathSegment, UnsignedPathSegment) {
        let g = default_graph().unwrap();
        let mut up_segment = g
            .beacon("1-ff00:0:130".parse().unwrap(), &[112, 103])
            .unwrap();
        set_mtus(&mut up_segment, 1000);
        let mut down_segment = g
            .beacon("2-ff00:0:210".parse().unwrap(), &[451, 2])
            .unwrap();
        set_mtus(&mut down_segment, 1000);
        (up_segment, down_segment)
    }

    #[test]
    fn test_mtu_calculation_shortcut() {
        let (mut up, _) = mtu_test_segments();
        // Set AS MTU for 112 (index 2) to be higher than expected final MTU
        up.as_entries[2].mtu = 1005;
        up.as_entries[2].hop_entry.ingress_mtu = 1005;
        // The path MTU is determined by the final hop's (111) AS MTU.
        up.as_entries[1].mtu = 1002;

        let paths = combine(
            "1-ff00:0:112".parse().unwrap(),
            "1-ff00:0:111".parse().unwrap(),
            vec![],
            vec![up],
        );

        assert_eq!(paths.len(), 1, "should produce exactly one combined path");
        assert_eq!(
            paths[0].metadata.as_ref().unwrap().mtu,
            1002,
            "path MTU should be limited by the smallest segment MTU"
        );
    }

    #[test]
    fn test_mtu_calculation_shortcut_ingress() {
        let (mut up, _) = mtu_test_segments();
        // Set AS MTU for 112 (index 2) to be higher
        up.as_entries[2].mtu = 1005;
        // The path MTU is determined by the MTU of the link between 111 and 112.
        up.as_entries[2].hop_entry.ingress_mtu = 1002;
        up.as_entries[1].mtu = 1005;

        let paths = combine(
            "1-ff00:0:112".parse().unwrap(),
            "1-ff00:0:111".parse().unwrap(),
            vec![],
            vec![up],
        );

        assert_eq!(paths.len(), 1, "should produce exactly one combined path");
        assert_eq!(
            paths[0].metadata.as_ref().unwrap().mtu,
            1002,
            "path MTU should be limited by ingress link MTU"
        );
    }

    #[test]
    fn test_mtu_calculation_full_up_segment() {
        let (mut up, _) = mtu_test_segments();
        up.as_entries[2].mtu = 1005;
        up.as_entries[2].hop_entry.ingress_mtu = 1004;
        up.as_entries[1].mtu = 1003;
        up.as_entries[1].hop_entry.ingress_mtu = 1002;
        // The path MTU is determined by the final hop's (130) AS MTU.
        up.as_entries[0].mtu = 1001;

        let paths = combine(
            "1-ff00:0:112".parse().unwrap(),
            "1-ff00:0:130".parse().unwrap(),
            vec![],
            vec![up],
        );

        assert_eq!(paths.len(), 1, "should produce exactly one combined path");
        assert_eq!(
            paths[0].metadata.as_ref().unwrap().mtu,
            1001,
            "path MTU should be limited by the smallest AS MTU in the path"
        );
    }

    // Create test segments with one peer link removed between AS 111 and AS 211.
    // This is needed for the peer MTU tests to ensure only one path is returned per test case.
    fn mtu_test_segments_with_removed_peer_link() -> (UnsignedPathSegment, UnsignedPathSegment) {
        let mut g = default_graph().unwrap();
        // Remove one peer link between 211 and 111 so we only get one path per test case.
        g.remove_link(
            "1-ff00:0:111".parse().unwrap(),
            102,
            "2-ff00:0:211".parse().unwrap(),
            6,
        )
        .unwrap();

        let mut up = g
            .beacon("1-ff00:0:130".parse().unwrap(), &[112, 103])
            .unwrap();
        set_mtus(&mut up, 1000);
        let mut down = g
            .beacon("2-ff00:0:210".parse().unwrap(), &[451, 2])
            .unwrap();
        set_mtus(&mut down, 1000);
        (up, down)
    }

    #[test]
    fn test_mtu_calculation_peer_mtu() {
        let (mut up, mut down) = mtu_test_segments_with_removed_peer_link();

        up.as_entries[2].mtu = 1005;
        up.as_entries[2].hop_entry.ingress_mtu = 1005;
        up.as_entries[1].mtu = 1005;
        down.as_entries[1].mtu = 1005;

        // Path MTU is determined by the peer entry mtus.
        // Set the peer MTU on the up segment (AS 111) to 1002
        if let Some(peer_entry) = up.as_entries[1]
            .peer_entries
            .iter_mut()
            .find(|p| p.peer == "2-ff00:0:211".parse().unwrap())
        {
            peer_entry.peer_mtu = 1002;
        }
        // Set the peer MTU on the down segment (AS 211) to 1001 (this should be the limiting
        // factor)
        if let Some(peer_entry) = down.as_entries[1]
            .peer_entries
            .iter_mut()
            .find(|p| p.peer == "1-ff00:0:111".parse().unwrap())
        {
            peer_entry.peer_mtu = 1001;
        }

        down.as_entries[2].mtu = 1005;
        down.as_entries[2].hop_entry.ingress_mtu = 1005;

        let paths = combine(
            "1-ff00:0:112".parse().unwrap(),
            "2-ff00:0:212".parse().unwrap(),
            vec![],
            vec![up, down],
        );

        assert_eq!(paths.len(), 1, "should produce exactly one combined path");
        assert_eq!(
            paths[0].metadata.as_ref().unwrap().mtu,
            1001,
            "path MTU should be limited by the smallest peer link MTU"
        );
    }

    #[test]
    fn test_mtu_calculation_peer_mtu_down_segment() {
        let (mut up, mut down) = mtu_test_segments_with_removed_peer_link();
        up.as_entries[2].mtu = 1005;
        up.as_entries[2].hop_entry.ingress_mtu = 1005;
        up.as_entries[1].mtu = 1005;

        // Set peer MTUs to be higher than the limiting factor
        if let Some(peer_entry) = up.as_entries[1]
            .peer_entries
            .iter_mut()
            .find(|p| p.peer == "2-ff00:0:211".parse().unwrap())
        {
            peer_entry.peer_mtu = 1005;
        }
        if let Some(peer_entry) = down.as_entries[1]
            .peer_entries
            .iter_mut()
            .find(|p| p.peer == "1-ff00:0:111".parse().unwrap())
        {
            peer_entry.peer_mtu = 1005;
        }

        down.as_entries[1].mtu = 1005;
        down.as_entries[2].hop_entry.ingress_mtu = 1005;
        // The path MTU is determined by the final hop's (212) AS MTU.
        down.as_entries[2].mtu = 1002;

        let paths = combine(
            "1-ff00:0:112".parse().unwrap(),
            "2-ff00:0:212".parse().unwrap(),
            vec![],
            vec![up, down],
        );

        assert_eq!(paths.len(), 1, "should produce exactly one combined path");
        assert_eq!(
            paths[0].metadata.as_ref().unwrap().mtu,
            1002,
            "path MTU should be limited by the destination AS MTU in down segment"
        );
    }
}