rs-pfcp 0.4.0

High-performance Rust implementation of PFCP (Packet Forwarding Control Protocol) for 5G networks with 100% 3GPP TS 29.244 Release 18 compliance
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
//! F-TEID IE.

use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
use crate::types::Teid;
use std::net::{Ipv4Addr, Ipv6Addr};

/// Represents a F-TEID.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Fteid {
    pub v4: bool,
    pub v6: bool,
    pub ch: bool,
    pub chid: bool,
    pub teid: Teid,
    pub ipv4_address: Option<Ipv4Addr>,
    pub ipv6_address: Option<Ipv6Addr>,
    pub choose_id: u8,
}

impl Fteid {
    /// Creates a new F-TEID.
    pub fn new(
        v4: bool,
        v6: bool,
        teid: impl Into<Teid>,
        ipv4_address: Option<Ipv4Addr>,
        ipv6_address: Option<Ipv6Addr>,
        choose_id: u8,
    ) -> Self {
        Fteid {
            v4,
            v6,
            ch: false,
            chid: false,
            teid: teid.into(),
            ipv4_address,
            ipv6_address,
            choose_id,
        }
    }

    /// Creates a new F-TEID with CHOOSE and CHOOSE ID flags.
    ///
    /// **Note:** Consider using `FteidBuilder` for better ergonomics and validation.
    #[allow(clippy::too_many_arguments)]
    pub fn new_with_choose(
        v4: bool,
        v6: bool,
        ch: bool,
        chid: bool,
        teid: impl Into<Teid>,
        ipv4_address: Option<Ipv4Addr>,
        ipv6_address: Option<Ipv6Addr>,
        choose_id: u8,
    ) -> Self {
        Fteid {
            v4,
            v6,
            ch,
            chid,
            teid: teid.into(),
            ipv4_address,
            ipv6_address,
            choose_id,
        }
    }

    /// Marshals the F-TEID into a byte vector.
    pub fn marshal(&self) -> Vec<u8> {
        let mut data = Vec::new();
        let mut flags = 0;
        if self.v4 {
            flags |= 0x01; // V4 flag (bit 0)
        }
        if self.v6 {
            flags |= 0x02; // V6 flag (bit 1)
        }
        if self.ch {
            flags |= 0x04; // CH flag (bit 2)
        }
        if self.chid {
            flags |= 0x08; // CHID flag (bit 3)
        }
        data.push(flags);
        data.extend_from_slice(&self.teid.0.to_be_bytes());
        if let Some(addr) = self.ipv4_address {
            data.extend_from_slice(&addr.octets());
        }
        if let Some(addr) = self.ipv6_address {
            data.extend_from_slice(&addr.octets());
        }
        // Only include choose_id if CHID flag is set
        if self.chid {
            data.push(self.choose_id);
        }
        data
    }

    /// Unmarshals a byte slice into an F-TEID.
    ///
    /// Per 3GPP TS 29.244, F-TEID requires minimum 5 bytes (1 byte flags + 4 bytes TEID).
    pub fn unmarshal(payload: &[u8]) -> Result<Self, PfcpError> {
        if payload.len() < 5 {
            return Err(PfcpError::invalid_length(
                "F-TEID",
                IeType::Fteid,
                5,
                payload.len(),
            ));
        }
        let flags = payload[0];
        let v4 = flags & 0x01 != 0;
        let v6 = flags & 0x02 != 0;
        let ch = flags & 0x04 != 0;
        let chid = flags & 0x08 != 0;
        let teid = u32::from_be_bytes([payload[1], payload[2], payload[3], payload[4]]);
        let mut offset = 5;
        let ipv4_address = if v4 && !ch {
            // IPv4 address is present only if V4 flag is set and CHOOSE flag is NOT set
            if payload.len() < offset + 4 {
                return Err(PfcpError::invalid_length(
                    "F-TEID IPv4",
                    IeType::Fteid,
                    offset + 4,
                    payload.len(),
                ));
            }
            let addr = Ipv4Addr::new(
                payload[offset],
                payload[offset + 1],
                payload[offset + 2],
                payload[offset + 3],
            );
            offset += 4;
            Some(addr)
        } else if v4 && ch {
            // V4 flag set with CHOOSE flag - check if address is present
            if payload.len() >= offset + 4 {
                // Address is present (optional with CHOOSE)
                let addr = Ipv4Addr::new(
                    payload[offset],
                    payload[offset + 1],
                    payload[offset + 2],
                    payload[offset + 3],
                );
                offset += 4;
                Some(addr)
            } else {
                // No address present with CHOOSE flag
                None
            }
        } else {
            None
        };
        let ipv6_address = if v6 && !ch {
            // IPv6 address is present only if V6 flag is set and CHOOSE flag is NOT set
            if payload.len() < offset + 16 {
                return Err(PfcpError::invalid_length(
                    "F-TEID IPv6",
                    IeType::Fteid,
                    offset + 16,
                    payload.len(),
                ));
            }
            let mut octets = [0; 16];
            octets.copy_from_slice(&payload[offset..offset + 16]);
            offset += 16;
            Some(Ipv6Addr::from(octets))
        } else if v6 && ch {
            // V6 flag set with CHOOSE flag - check if address is present
            if payload.len() >= offset + 16 {
                // Address is present (optional with CHOOSE)
                let mut octets = [0; 16];
                octets.copy_from_slice(&payload[offset..offset + 16]);
                offset += 16;
                Some(Ipv6Addr::from(octets))
            } else {
                // No address present with CHOOSE flag
                None
            }
        } else {
            None
        };
        // Only read choose_id if CHID flag is set
        let choose_id = if chid {
            if payload.len() < offset + 1 {
                return Err(PfcpError::invalid_length(
                    "F-TEID choose ID",
                    IeType::Fteid,
                    offset + 1,
                    payload.len(),
                ));
            }
            payload[offset]
        } else {
            0
        };
        Ok(Fteid {
            v4,
            v6,
            ch,
            chid,
            teid: Teid(teid),
            ipv4_address,
            ipv6_address,
            choose_id,
        })
    }

    /// Wraps the F-TEID in a Fteid IE.
    pub fn to_ie(&self) -> Ie {
        Ie::new(IeType::Fteid, self.marshal())
    }
}

/// Builder for constructing F-TEID Information Elements with validation.
///
/// The F-TEID builder provides a safe and ergonomic way to construct F-TEID IEs
/// with proper validation of flag combinations and automatic handling of
/// the complex interactions between CHOOSE flags and IP addresses.
///
/// # Examples
///
/// ```rust
/// use rs_pfcp::ie::f_teid::FteidBuilder;
/// use std::net::Ipv4Addr;
///
/// // Simple IPv4 F-TEID
/// let fteid = FteidBuilder::new()
///     .teid(0x12345678)
///     .ipv4("192.168.1.1".parse().unwrap())
///     .build()
///     .unwrap();
///
/// // F-TEID with CHOOSE flag (UPF selects IP)
/// let choose_fteid = FteidBuilder::new()
///     .teid(0x87654321)
///     .choose_ipv4()
///     .build()
///     .unwrap();
///
/// // F-TEID with CHOOSE ID for correlation
/// let choose_id_fteid = FteidBuilder::new()
///     .teid(0xABCDEF00)
///     .choose_ipv4()
///     .choose_id(42)
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Default)]
pub struct FteidBuilder {
    teid: Option<Teid>,
    ipv4_address: Option<Ipv4Addr>,
    ipv6_address: Option<Ipv6Addr>,
    choose_ipv4: bool,
    choose_ipv6: bool,
    choose_id: Option<u8>,
}

impl FteidBuilder {
    /// Creates a new F-TEID builder.
    pub fn new() -> Self {
        FteidBuilder::default()
    }

    /// Sets the TEID (Tunnel Endpoint Identifier).
    ///
    /// This is a required field for all F-TEID instances.
    pub fn teid(mut self, teid: impl Into<Teid>) -> Self {
        self.teid = Some(teid.into());
        self
    }

    /// Sets the IPv4 address.
    ///
    /// Cannot be used together with `choose_ipv4()`.
    pub fn ipv4(mut self, addr: Ipv4Addr) -> Self {
        self.ipv4_address = Some(addr);
        self
    }

    /// Sets the IPv6 address.
    ///
    /// Cannot be used together with `choose_ipv6()`.
    pub fn ipv6(mut self, addr: Ipv6Addr) -> Self {
        self.ipv6_address = Some(addr);
        self
    }

    /// Sets both IPv4 and IPv6 addresses for dual-stack configuration.
    pub fn dual_stack(mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Self {
        self.ipv4_address = Some(ipv4);
        self.ipv6_address = Some(ipv6);
        self
    }

    /// Enables CHOOSE flag for IPv4 (UPF will select the IPv4 address).
    ///
    /// Cannot be used together with `ipv4()`.
    pub fn choose_ipv4(mut self) -> Self {
        self.choose_ipv4 = true;
        self
    }

    /// Enables CHOOSE flag for IPv6 (UPF will select the IPv6 address).
    ///
    /// Cannot be used together with `ipv6()`.
    pub fn choose_ipv6(mut self) -> Self {
        self.choose_ipv6 = true;
        self
    }

    /// Enables CHOOSE flags for both IPv4 and IPv6.
    pub fn choose_dual_stack(mut self) -> Self {
        self.choose_ipv4 = true;
        self.choose_ipv6 = true;
        self
    }

    /// Sets the CHOOSE ID for correlation when CHOOSE flags are used.
    ///
    /// The CHOOSE ID allows correlation of multiple F-TEID IEs that should
    /// use the same chosen address. Only meaningful when used with choose_* methods.
    pub fn choose_id(mut self, id: u8) -> Self {
        self.choose_id = Some(id);
        self
    }

    /// Builds the F-TEID with validation.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - TEID is not set
    /// - No IP addressing method is specified (neither explicit addresses nor CHOOSE flags)
    /// - Both explicit address and CHOOSE flag are set for the same IP version
    /// - CHOOSE ID is set but no CHOOSE flags are enabled
    pub fn build(self) -> Result<Fteid, PfcpError> {
        let teid = self.teid.ok_or(PfcpError::validation_error(
            "FteidBuilder",
            "teid",
            "TEID is required",
        ))?;

        // Validate IPv4 configuration
        if self.ipv4_address.is_some() && self.choose_ipv4 {
            return Err(PfcpError::validation_error(
                "FteidBuilder",
                "ipv4_address",
                "Cannot specify both explicit IPv4 address and CHOOSE IPv4 flag",
            ));
        }

        // Validate IPv6 configuration
        if self.ipv6_address.is_some() && self.choose_ipv6 {
            return Err(PfcpError::validation_error(
                "FteidBuilder",
                "ipv6_address",
                "Cannot specify both explicit IPv6 address and CHOOSE IPv6 flag",
            ));
        }

        // Ensure at least one IP addressing method is specified
        let has_ipv4 = self.ipv4_address.is_some() || self.choose_ipv4;
        let has_ipv6 = self.ipv6_address.is_some() || self.choose_ipv6;

        if !has_ipv4 && !has_ipv6 {
            return Err(PfcpError::validation_error(
                "FteidBuilder",
                "ipv4_address",
                "At least one IP addressing method must be specified (IPv4, IPv6, or CHOOSE flags)",
            ));
        }

        // Validate CHOOSE ID usage
        if self.choose_id.is_some() && !self.choose_ipv4 && !self.choose_ipv6 {
            return Err(PfcpError::validation_error(
                "FteidBuilder",
                "choose_id",
                "CHOOSE ID can only be used with CHOOSE flags",
            ));
        }

        // Determine flags
        let v4 = self.ipv4_address.is_some() || self.choose_ipv4;
        let v6 = self.ipv6_address.is_some() || self.choose_ipv6;
        let ch = self.choose_ipv4 || self.choose_ipv6;
        let chid = self.choose_id.is_some();
        let choose_id = self.choose_id.unwrap_or(0);

        Ok(Fteid {
            v4,
            v6,
            ch,
            chid,
            teid,
            ipv4_address: self.ipv4_address,
            ipv6_address: self.ipv6_address,
            choose_id,
        })
    }
}

impl Fteid {
    /// Returns a builder for constructing F-TEID instances.
    pub fn builder() -> FteidBuilder {
        FteidBuilder::new()
    }

    /// Creates a simple IPv4 F-TEID.
    pub fn ipv4(teid: impl Into<Teid>, addr: Ipv4Addr) -> Self {
        FteidBuilder::new()
            .teid(teid)
            .ipv4(addr)
            .build()
            .expect("IPv4 F-TEID construction should not fail")
    }

    /// Creates a simple IPv6 F-TEID.
    pub fn ipv6(teid: impl Into<Teid>, addr: Ipv6Addr) -> Self {
        FteidBuilder::new()
            .teid(teid)
            .ipv6(addr)
            .build()
            .expect("IPv6 F-TEID construction should not fail")
    }

    /// Creates a dual-stack F-TEID with both IPv4 and IPv6 addresses.
    pub fn dual_stack(teid: impl Into<Teid>, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Self {
        FteidBuilder::new()
            .teid(teid)
            .dual_stack(ipv4, ipv6)
            .build()
            .expect("Dual-stack F-TEID construction should not fail")
    }

    /// Creates an F-TEID with CHOOSE IPv4 flag.
    pub fn choose_ipv4(teid: impl Into<Teid>) -> Self {
        FteidBuilder::new()
            .teid(teid)
            .choose_ipv4()
            .build()
            .expect("CHOOSE IPv4 F-TEID construction should not fail")
    }

    /// Creates an F-TEID with CHOOSE IPv6 flag.
    pub fn choose_ipv6(teid: impl Into<Teid>) -> Self {
        FteidBuilder::new()
            .teid(teid)
            .choose_ipv6()
            .build()
            .expect("CHOOSE IPv6 F-TEID construction should not fail")
    }

    /// Creates an F-TEID with CHOOSE flags for both IPv4 and IPv6.
    pub fn choose_dual_stack(teid: impl Into<Teid>) -> Self {
        FteidBuilder::new()
            .teid(teid)
            .choose_dual_stack()
            .build()
            .expect("CHOOSE dual-stack F-TEID construction should not fail")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{Ipv4Addr, Ipv6Addr};

    #[test]
    fn test_fteid_marshal_unmarshal_ipv4() {
        let fteid = Fteid::new(
            true,
            false,
            0x12345678,
            Some(Ipv4Addr::new(192, 168, 0, 1)),
            None,
            0,
        );
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled, fteid);
    }

    #[test]
    fn test_fteid_marshal_unmarshal_ipv6() {
        let fteid = Fteid::new(
            false,
            true,
            0x12345678,
            None,
            Some(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
            0,
        );
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled, fteid);
    }

    #[test]
    fn test_fteid_marshal_unmarshal_ipv4_ipv6() {
        let fteid = Fteid::new(
            true,
            true,
            0x12345678,
            Some(Ipv4Addr::new(192, 168, 0, 1)),
            Some(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
            0,
        );
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled, fteid);
    }

    #[test]
    fn test_fteid_unmarshal_short_payload() {
        let data = [0; 4];
        let result = Fteid::unmarshal(&data);
        assert!(result.is_err());

        let data_ipv4 = [1, 0, 0, 0, 0, 1, 2, 3];
        let result_ipv4 = Fteid::unmarshal(&data_ipv4);
        assert!(result_ipv4.is_err());

        let data_ipv6 = [
            2, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
        ];
        let result_ipv6 = Fteid::unmarshal(&data_ipv6);
        assert!(result_ipv6.is_err());
    }

    #[test]
    fn test_fteid_with_choose_flags() {
        let fteid = Fteid::new_with_choose(
            true,
            false,
            true,  // ch = true
            false, // chid = false
            0x12345678,
            Some(Ipv4Addr::new(192, 168, 0, 1)),
            None,
            0,
        );
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled, fteid);
        assert!(unmarshaled.ch);
        assert!(!unmarshaled.chid);

        // Verify marshaled data doesn't include choose_id when chid=false
        assert_eq!(marshaled.len(), 9); // flags(1) + teid(4) + ipv4(4) = 9 bytes
    }

    #[test]
    fn test_fteid_with_choose_id() {
        let fteid = Fteid::new_with_choose(
            true,
            false,
            false, // ch = false
            true,  // chid = true
            0x12345678,
            Some(Ipv4Addr::new(192, 168, 0, 1)),
            None,
            42, // choose_id
        );
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled, fteid);
        assert!(!unmarshaled.ch);
        assert!(unmarshaled.chid);
        assert_eq!(unmarshaled.choose_id, 42);

        // Verify marshaled data includes choose_id when chid=true
        assert_eq!(marshaled.len(), 10); // flags(1) + teid(4) + ipv4(4) + choose_id(1) = 10 bytes
        assert_eq!(marshaled[9], 42); // Last byte should be choose_id
    }

    #[test]
    fn test_fteid_flags_encoding() {
        let fteid = Fteid::new_with_choose(
            true, // v4 = true (bit 0)
            true, // v6 = true (bit 1)
            true, // ch = true (bit 2)
            true, // chid = true (bit 3)
            0x12345678,
            Some(Ipv4Addr::new(192, 168, 0, 1)),
            Some(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
            100, // choose_id
        );
        let marshaled = fteid.marshal();

        // First byte should have all flags set: 0x01 | 0x02 | 0x04 | 0x08 = 0x0F
        assert_eq!(marshaled[0], 0x0F);

        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled, fteid);
        assert!(unmarshaled.v4);
        assert!(unmarshaled.v6);
        assert!(unmarshaled.ch);
        assert!(unmarshaled.chid);
        assert_eq!(unmarshaled.choose_id, 100);
    }

    #[test]
    fn test_fteid_no_choose_id_without_chid_flag() {
        // Test that choose_id is not included when chid flag is false
        let fteid = Fteid::new(
            true,
            false,
            0x12345678,
            Some(Ipv4Addr::new(192, 168, 0, 1)),
            None,
            123, // This should be ignored since chid=false
        );
        let marshaled = fteid.marshal();

        // Should not include choose_id byte
        assert_eq!(marshaled.len(), 9); // flags(1) + teid(4) + ipv4(4) = 9 bytes

        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled.choose_id, 0); // Should be 0 when chid=false
        assert!(!unmarshaled.chid);
    }

    #[test]
    fn test_fteid_unmarshal_missing_choose_id() {
        // Test error when CHID flag is set but choose_id byte is missing
        let data = [0x08, 0x12, 0x34, 0x56, 0x78]; // chid=true but no choose_id byte
        let result = Fteid::unmarshal(&data);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("choose ID"));
    }

    // Builder pattern tests

    #[test]
    fn test_fteid_builder_ipv4() {
        let fteid = FteidBuilder::new()
            .teid(0x12345678)
            .ipv4(Ipv4Addr::new(192, 168, 1, 1))
            .build()
            .unwrap();

        assert!(fteid.v4);
        assert!(!fteid.v6);
        assert!(!fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x12345678));
        assert_eq!(fteid.ipv4_address, Some(Ipv4Addr::new(192, 168, 1, 1)));
        assert_eq!(fteid.ipv6_address, None);
        assert_eq!(fteid.choose_id, 0);

        // Test round-trip marshaling
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(fteid, unmarshaled);
    }

    #[test]
    fn test_fteid_builder_ipv6() {
        let ipv6_addr = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
        let fteid = FteidBuilder::new()
            .teid(0x87654321)
            .ipv6(ipv6_addr)
            .build()
            .unwrap();

        assert!(!fteid.v4);
        assert!(fteid.v6);
        assert!(!fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x87654321));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, Some(ipv6_addr));
        assert_eq!(fteid.choose_id, 0);

        // Test round-trip marshaling
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(fteid, unmarshaled);
    }

    #[test]
    fn test_fteid_builder_dual_stack() {
        let ipv4_addr = Ipv4Addr::new(10, 0, 0, 1);
        let ipv6_addr = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
        let fteid = FteidBuilder::new()
            .teid(0xABCDEF00)
            .dual_stack(ipv4_addr, ipv6_addr)
            .build()
            .unwrap();

        assert!(fteid.v4);
        assert!(fteid.v6);
        assert!(!fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0xABCDEF00));
        assert_eq!(fteid.ipv4_address, Some(ipv4_addr));
        assert_eq!(fteid.ipv6_address, Some(ipv6_addr));
        assert_eq!(fteid.choose_id, 0);

        // Test round-trip marshaling
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(fteid, unmarshaled);
    }

    #[test]
    fn test_fteid_builder_choose_ipv4() {
        let fteid = FteidBuilder::new()
            .teid(0x11111111)
            .choose_ipv4()
            .build()
            .unwrap();

        assert!(fteid.v4);
        assert!(!fteid.v6);
        assert!(fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x11111111));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, None);
        assert_eq!(fteid.choose_id, 0);

        // Test round-trip marshaling
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(fteid, unmarshaled);
    }

    #[test]
    fn test_fteid_builder_choose_ipv6() {
        let fteid = FteidBuilder::new()
            .teid(0x22222222)
            .choose_ipv6()
            .build()
            .unwrap();

        assert!(!fteid.v4);
        assert!(fteid.v6);
        assert!(fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x22222222));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, None);
        assert_eq!(fteid.choose_id, 0);

        // Test round-trip marshaling
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(fteid, unmarshaled);
    }

    #[test]
    fn test_fteid_builder_choose_dual_stack() {
        let fteid = FteidBuilder::new()
            .teid(0x33333333)
            .choose_dual_stack()
            .build()
            .unwrap();

        assert!(fteid.v4);
        assert!(fteid.v6);
        assert!(fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x33333333));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, None);
        assert_eq!(fteid.choose_id, 0);

        // Test round-trip marshaling
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(fteid, unmarshaled);
    }

    #[test]
    fn test_fteid_builder_choose_with_id() {
        let fteid = FteidBuilder::new()
            .teid(0x44444444)
            .choose_ipv4()
            .choose_id(42)
            .build()
            .unwrap();

        assert!(fteid.v4);
        assert!(!fteid.v6);
        assert!(fteid.ch);
        assert!(fteid.chid);
        assert_eq!(fteid.teid, Teid(0x44444444));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, None);
        assert_eq!(fteid.choose_id, 42);

        // Test round-trip marshaling
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(fteid, unmarshaled);
    }

    #[test]
    fn test_fteid_builder_choose_dual_stack_with_id() {
        let fteid = FteidBuilder::new()
            .teid(0x55555555)
            .choose_dual_stack()
            .choose_id(100)
            .build()
            .unwrap();

        assert!(fteid.v4);
        assert!(fteid.v6);
        assert!(fteid.ch);
        assert!(fteid.chid);
        assert_eq!(fteid.teid, Teid(0x55555555));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, None);
        assert_eq!(fteid.choose_id, 100);

        // Test round-trip marshaling
        let marshaled = fteid.marshal();
        let unmarshaled = Fteid::unmarshal(&marshaled).unwrap();
        assert_eq!(fteid, unmarshaled);
    }

    // Error cases for builder validation

    #[test]
    fn test_fteid_builder_missing_teid() {
        let result = FteidBuilder::new()
            .ipv4(Ipv4Addr::new(192, 168, 1, 1))
            .build();

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("TEID is required"));
    }

    #[test]
    fn test_fteid_builder_no_ip_method() {
        let result = FteidBuilder::new().teid(0x12345678).build();

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("At least one IP addressing method"));
    }

    #[test]
    fn test_fteid_builder_conflicting_ipv4() {
        let result = FteidBuilder::new()
            .teid(0x12345678)
            .ipv4(Ipv4Addr::new(192, 168, 1, 1))
            .choose_ipv4()
            .build();

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Cannot specify both explicit IPv4 address and CHOOSE IPv4 flag"));
    }

    #[test]
    fn test_fteid_builder_conflicting_ipv6() {
        let result = FteidBuilder::new()
            .teid(0x12345678)
            .ipv6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1))
            .choose_ipv6()
            .build();

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Cannot specify both explicit IPv6 address and CHOOSE IPv6 flag"));
    }

    #[test]
    fn test_fteid_builder_choose_id_without_choose() {
        let result = FteidBuilder::new()
            .teid(0x12345678)
            .ipv4(Ipv4Addr::new(192, 168, 1, 1))
            .choose_id(42)
            .build();

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("CHOOSE ID can only be used with CHOOSE flags"));
    }

    // Convenience method tests

    #[test]
    fn test_fteid_convenience_ipv4() {
        let addr = Ipv4Addr::new(10, 0, 0, 1);
        let fteid = Fteid::ipv4(0x12345678, addr);

        assert!(fteid.v4);
        assert!(!fteid.v6);
        assert!(!fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x12345678));
        assert_eq!(fteid.ipv4_address, Some(addr));
        assert_eq!(fteid.ipv6_address, None);
    }

    #[test]
    fn test_fteid_convenience_ipv6() {
        let addr = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
        let fteid = Fteid::ipv6(0x87654321, addr);

        assert!(!fteid.v4);
        assert!(fteid.v6);
        assert!(!fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x87654321));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, Some(addr));
    }

    #[test]
    fn test_fteid_convenience_dual_stack() {
        let ipv4 = Ipv4Addr::new(10, 0, 0, 1);
        let ipv6 = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
        let fteid = Fteid::dual_stack(0xABCDEF00, ipv4, ipv6);

        assert!(fteid.v4);
        assert!(fteid.v6);
        assert!(!fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0xABCDEF00));
        assert_eq!(fteid.ipv4_address, Some(ipv4));
        assert_eq!(fteid.ipv6_address, Some(ipv6));
    }

    #[test]
    fn test_fteid_convenience_choose_ipv4() {
        let fteid = Fteid::choose_ipv4(0x11111111);

        assert!(fteid.v4);
        assert!(!fteid.v6);
        assert!(fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x11111111));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, None);
    }

    #[test]
    fn test_fteid_convenience_choose_ipv6() {
        let fteid = Fteid::choose_ipv6(0x22222222);

        assert!(!fteid.v4);
        assert!(fteid.v6);
        assert!(fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x22222222));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, None);
    }

    #[test]
    fn test_fteid_convenience_choose_dual_stack() {
        let fteid = Fteid::choose_dual_stack(0x33333333);

        assert!(fteid.v4);
        assert!(fteid.v6);
        assert!(fteid.ch);
        assert!(!fteid.chid);
        assert_eq!(fteid.teid, Teid(0x33333333));
        assert_eq!(fteid.ipv4_address, None);
        assert_eq!(fteid.ipv6_address, None);
    }

    #[test]
    fn test_fteid_builder_method() {
        let fteid = Fteid::builder()
            .teid(0x99999999)
            .ipv4(Ipv4Addr::new(172, 16, 0, 1))
            .build()
            .unwrap();

        assert_eq!(fteid.teid, Teid(0x99999999));
        assert_eq!(fteid.ipv4_address, Some(Ipv4Addr::new(172, 16, 0, 1)));
    }

    #[test]
    fn test_fteid_builder_method_chaining() {
        // Test that builder methods can be chained in any order
        let fteid = Fteid::builder()
            .choose_id(50)
            .choose_ipv6()
            .teid(0xDEADBEEF)
            .build()
            .unwrap();

        assert!(fteid.v6);
        assert!(fteid.ch);
        assert!(fteid.chid);
        assert_eq!(fteid.teid, Teid(0xDEADBEEF));
        assert_eq!(fteid.choose_id, 50);
    }
}