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
// src/message/association_update_response.rs

//! Association Update Response message implementation.

use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
use crate::message::{header::Header, Message, MsgType};
use crate::types::{Seid, SequenceNumber};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssociationUpdateResponse {
    pub header: Header,
    pub node_id: Ie, // M - 3GPP TS 29.244 Table 7.4.4.4-1 - IE Type 60
    pub cause: Ie,   // M - 3GPP TS 29.244 Table 7.4.4.4-1 - IE Type 19
    pub up_function_features: Option<Ie>, // O - 3GPP TS 29.244 Table 7.4.4.4-1 - IE Type 43
    pub cp_function_features: Option<Ie>, // O - 3GPP TS 29.244 Table 7.4.4.4-1 - IE Type 89
    pub ue_ip_address_usage_information: Vec<Ie>, // O - 3GPP TS 29.244 Table 7.4.4.4-1 - IE Type 267 - Multiple instances, Grouped IE (Sxb/N4 only)
    pub ies: Vec<Ie>,
}

impl AssociationUpdateResponse {
    /// Creates a new Association Update Response message.
    pub fn new(
        seq: impl Into<SequenceNumber>,
        node_id: Ie,
        cause: Ie,
        up_function_features: Option<Ie>,
        cp_function_features: Option<Ie>,
        ue_ip_address_usage_information: Vec<Ie>,
        ies: Vec<Ie>,
    ) -> Self {
        let mut payload_len = node_id.len() + cause.len();
        if let Some(ref ie) = up_function_features {
            payload_len += ie.len();
        }
        if let Some(ref ie) = cp_function_features {
            payload_len += ie.len();
        }
        for ie in &ue_ip_address_usage_information {
            payload_len += ie.len();
        }
        for ie in &ies {
            payload_len += ie.len();
        }

        let mut header = Header::new(MsgType::AssociationUpdateResponse, false, 0, seq);
        header.length = payload_len + (header.len() - 4);

        AssociationUpdateResponse {
            header,
            node_id,
            cause,
            up_function_features,
            cp_function_features,
            ue_ip_address_usage_information,
            ies,
        }
    }
}

impl Message for AssociationUpdateResponse {
    fn msg_type(&self) -> MsgType {
        MsgType::AssociationUpdateResponse
    }

    fn marshal(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(self.marshaled_size());
        self.marshal_into(&mut buf);
        buf
    }

    fn marshal_into(&self, buf: &mut Vec<u8>) {
        buf.reserve(self.marshaled_size());
        self.header.marshal_into(buf);
        self.node_id.marshal_into(buf);
        self.cause.marshal_into(buf);
        if let Some(ref ie) = self.up_function_features {
            ie.marshal_into(buf);
        }
        if let Some(ref ie) = self.cp_function_features {
            ie.marshal_into(buf);
        }
        for ie in &self.ue_ip_address_usage_information {
            ie.marshal_into(buf);
        }
        for ie in &self.ies {
            ie.marshal_into(buf);
        }
    }

    fn marshaled_size(&self) -> usize {
        let mut size = self.header.len() as usize;
        size += self.node_id.len() as usize;
        size += self.cause.len() as usize;
        if let Some(ref ie) = self.up_function_features {
            size += ie.len() as usize;
        }
        if let Some(ref ie) = self.cp_function_features {
            size += ie.len() as usize;
        }
        for ie in &self.ue_ip_address_usage_information {
            size += ie.len() as usize;
        }
        for ie in &self.ies {
            size += ie.len() as usize;
        }
        size
    }

    fn unmarshal(buf: &[u8]) -> Result<Self, PfcpError>
    where
        Self: Sized,
    {
        let header = Header::unmarshal(buf)?;
        let mut node_id = None;
        let mut cause = None;
        let mut up_function_features = None;
        let mut cp_function_features = None;
        let mut ue_ip_address_usage_information = Vec::new();
        let mut ies = Vec::new();

        let mut cursor = header.len() as usize;
        while cursor < buf.len() {
            let ie = Ie::unmarshal(&buf[cursor..])?;
            let ie_len = ie.len() as usize;
            match ie.ie_type {
                IeType::NodeId => node_id = Some(ie),
                IeType::Cause => cause = Some(ie),
                IeType::UpFunctionFeatures => up_function_features = Some(ie),
                IeType::CpFunctionFeatures => cp_function_features = Some(ie),
                IeType::UeIpAddressUsageInformation => ue_ip_address_usage_information.push(ie),
                _ => ies.push(ie),
            }
            cursor += ie_len;
        }

        let node_id = node_id.ok_or(PfcpError::MissingMandatoryIe {
            ie_type: IeType::NodeId,
            message_type: Some(MsgType::AssociationUpdateResponse),
            parent_ie: None,
        })?;
        let cause = cause.ok_or(PfcpError::MissingMandatoryIe {
            ie_type: IeType::Cause,
            message_type: Some(MsgType::AssociationUpdateResponse),
            parent_ie: None,
        })?;

        Ok(AssociationUpdateResponse {
            header,
            node_id,
            cause,
            up_function_features,
            cp_function_features,
            ue_ip_address_usage_information,
            ies,
        })
    }

    fn seid(&self) -> Option<Seid> {
        if self.header.has_seid {
            Some(self.header.seid)
        } else {
            None
        }
    }

    fn sequence(&self) -> SequenceNumber {
        self.header.sequence_number
    }

    fn set_sequence(&mut self, seq: SequenceNumber) {
        self.header.sequence_number = seq;
    }

    fn ies(&self, ie_type: IeType) -> crate::message::IeIter<'_> {
        use crate::message::IeIter;

        match ie_type {
            IeType::NodeId => IeIter::single(Some(&self.node_id), ie_type),
            IeType::Cause => IeIter::single(Some(&self.cause), ie_type),
            IeType::UpFunctionFeatures => {
                IeIter::single(self.up_function_features.as_ref(), ie_type)
            }
            IeType::CpFunctionFeatures => {
                IeIter::single(self.cp_function_features.as_ref(), ie_type)
            }
            IeType::UeIpAddressUsageInformation => {
                IeIter::multiple(&self.ue_ip_address_usage_information, ie_type)
            }
            _ => IeIter::generic(&self.ies, ie_type),
        }
    }

    fn all_ies(&self) -> Vec<&Ie> {
        let mut result = vec![&self.node_id, &self.cause];
        if let Some(ref ie) = self.up_function_features {
            result.push(ie);
        }
        if let Some(ref ie) = self.cp_function_features {
            result.push(ie);
        }
        result.extend(self.ue_ip_address_usage_information.iter());
        result.extend(self.ies.iter());
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ie::cause::{Cause, CauseValue};
    use crate::ie::node_id::NodeId;
    use std::net::Ipv4Addr;

    #[test]
    fn test_association_update_response_marshal_unmarshal() {
        let node_id_ie = Ie::new(
            IeType::NodeId,
            NodeId::IPv4(Ipv4Addr::new(192, 168, 1, 1))
                .marshal()
                .to_vec(),
        );
        let cause_ie = Ie::new(
            IeType::Cause,
            Cause::new(CauseValue::RequestAccepted).marshal().to_vec(),
        );

        let original = AssociationUpdateResponse::new(
            123,
            node_id_ie,
            cause_ie,
            None,
            None,
            Vec::new(),
            Vec::new(),
        );
        let marshaled = original.marshal();
        let unmarshaled = AssociationUpdateResponse::unmarshal(&marshaled).unwrap();

        assert_eq!(
            original.header.message_type,
            unmarshaled.header.message_type
        );
        assert_eq!(
            original.header.sequence_number,
            unmarshaled.header.sequence_number
        );
        assert_eq!(original.node_id, unmarshaled.node_id);
        assert_eq!(original.cause, unmarshaled.cause);
        assert_eq!(
            original.up_function_features,
            unmarshaled.up_function_features
        );
        assert_eq!(
            original.cp_function_features,
            unmarshaled.cp_function_features
        );
    }

    #[test]
    fn test_association_update_response_with_optional_ies() {
        let node_id_ie = Ie::new(
            IeType::NodeId,
            NodeId::IPv4(Ipv4Addr::new(192, 168, 1, 1))
                .marshal()
                .to_vec(),
        );
        let cause_ie = Ie::new(
            IeType::Cause,
            Cause::new(CauseValue::RequestAccepted).marshal().to_vec(),
        );
        let up_features_ie = Ie::new(IeType::UpFunctionFeatures, vec![0x01, 0x02, 0x03, 0x04]);
        let cp_features_ie = Ie::new(IeType::CpFunctionFeatures, vec![0x01, 0x02, 0x03, 0x04]);

        let original = AssociationUpdateResponse::new(
            456,
            node_id_ie,
            cause_ie,
            Some(up_features_ie),
            Some(cp_features_ie),
            Vec::new(),
            Vec::new(),
        );
        let marshaled = original.marshal();
        let unmarshaled = AssociationUpdateResponse::unmarshal(&marshaled).unwrap();

        assert_eq!(original, unmarshaled);
    }

    #[test]
    fn test_association_update_response_missing_required_ie() {
        // Test missing NodeId IE
        let incomplete_data = [
            0x21, 0x08, 0x00, 0x04, // Header (type=8, length=4, seq=0)
            0x00, 0x00, 0x00, 0x00, // No IEs following
        ];
        let result = AssociationUpdateResponse::unmarshal(&incomplete_data);
        assert!(result.is_err());
    }

    #[test]
    fn test_ies() {
        let node_id_ie = Ie::new(
            IeType::NodeId,
            NodeId::IPv4(Ipv4Addr::new(192, 168, 1, 1))
                .marshal()
                .to_vec(),
        );
        let cause_ie = Ie::new(
            IeType::Cause,
            Cause::new(CauseValue::RequestAccepted).marshal().to_vec(),
        );

        let message = AssociationUpdateResponse::new(
            123,
            node_id_ie,
            cause_ie,
            None,
            None,
            Vec::new(),
            Vec::new(),
        );

        assert!(message.ies(IeType::NodeId).next().is_some());
        assert!(message.ies(IeType::Cause).next().is_some());
        assert!(message.ies(IeType::UpFunctionFeatures).next().is_none());
        assert!(message.ies(IeType::Unknown).next().is_none());
    }
}

/// Builder for AssociationUpdateResponse message.
#[derive(Debug, Default)]
pub struct AssociationUpdateResponseBuilder {
    sequence: SequenceNumber,
    node_id: Option<Ie>,
    cause: Option<Ie>,
    up_function_features: Option<Ie>,
    cp_function_features: Option<Ie>,
    ue_ip_address_usage_information: Vec<Ie>,
    ies: Vec<Ie>,
}

impl AssociationUpdateResponseBuilder {
    /// Creates a new AssociationUpdateResponse builder.
    pub fn new(sequence: impl Into<SequenceNumber>) -> Self {
        Self {
            sequence: sequence.into(),
            node_id: None,
            cause: None,
            up_function_features: None,
            cp_function_features: None,
            ue_ip_address_usage_information: Vec::new(),
            ies: Vec::new(),
        }
    }

    /// Sets the node ID IE (required).
    pub fn node_id(mut self, node_id: Ie) -> Self {
        self.node_id = Some(node_id);
        self
    }

    /// Sets the cause from a CauseValue (required).
    ///
    /// Accepts a CauseValue enum. For common cases, use convenience methods like
    /// [`cause_accepted`] or [`cause_rejected`]. For full control, use [`cause_ie`].
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::message::association_update_response::AssociationUpdateResponseBuilder;
    /// use rs_pfcp::ie::{cause::CauseValue, node_id::NodeId, Ie, IeType};
    /// use std::net::Ipv4Addr;
    ///
    /// let node_id = Ie::new(IeType::NodeId, NodeId::IPv4(Ipv4Addr::new(127, 0, 0, 1)).marshal().to_vec());
    /// let response = AssociationUpdateResponseBuilder::new(1)
    ///     .node_id(node_id)
    ///     .cause(CauseValue::RequestAccepted)
    ///     .build();
    /// ```
    ///
    /// [`cause_accepted`]: #method.cause_accepted
    /// [`cause_rejected`]: #method.cause_rejected
    /// [`cause_ie`]: #method.cause_ie
    pub fn cause(mut self, cause_value: crate::ie::cause::CauseValue) -> Self {
        use crate::ie::cause::Cause;
        use crate::ie::{Ie, IeType};
        let cause = Cause::new(cause_value);
        self.cause = Some(Ie::new(IeType::Cause, cause.marshal().to_vec()));
        self
    }

    /// Convenience method to set cause to Request Accepted.
    ///
    /// Equivalent to `.cause(CauseValue::RequestAccepted)`.
    pub fn cause_accepted(self) -> Self {
        self.cause(crate::ie::cause::CauseValue::RequestAccepted)
    }

    /// Convenience method to set cause to Request Rejected.
    ///
    /// Equivalent to `.cause(CauseValue::RequestRejected)`.
    pub fn cause_rejected(self) -> Self {
        self.cause(crate::ie::cause::CauseValue::RequestRejected)
    }

    /// Sets the cause IE directly (required).
    ///
    /// This method provides full control over the IE construction. For common cases,
    /// use [`cause`], [`cause_accepted`], or [`cause_rejected`].
    ///
    /// [`cause`]: #method.cause
    /// [`cause_accepted`]: #method.cause_accepted
    /// [`cause_rejected`]: #method.cause_rejected
    pub fn cause_ie(mut self, cause: Ie) -> Self {
        self.cause = Some(cause);
        self
    }

    /// Sets the UP function features IE (optional).
    pub fn up_function_features(mut self, up_function_features: Ie) -> Self {
        self.up_function_features = Some(up_function_features);
        self
    }

    /// Sets the CP function features IE (optional).
    pub fn cp_function_features(mut self, cp_function_features: Ie) -> Self {
        self.cp_function_features = Some(cp_function_features);
        self
    }

    /// Adds a UE IP Address Usage Information IE (optional, multiple allowed, Sxb/N4 only).
    pub fn ue_ip_address_usage_information(mut self, ie: Ie) -> Self {
        self.ue_ip_address_usage_information.push(ie);
        self
    }

    /// Adds an additional IE.
    pub fn ie(mut self, ie: Ie) -> Self {
        self.ies.push(ie);
        self
    }

    /// Adds multiple additional IEs.
    pub fn ies(mut self, mut ies: Vec<Ie>) -> Self {
        self.ies.append(&mut ies);
        self
    }

    /// Builds the AssociationUpdateResponse message.
    ///
    /// # Panics
    /// Panics if required node_id or cause IEs are not set.
    pub fn build(self) -> AssociationUpdateResponse {
        let node_id = self
            .node_id
            .expect("Node ID IE is required for AssociationUpdateResponse");
        let cause = self
            .cause
            .expect("Cause IE is required for AssociationUpdateResponse");

        AssociationUpdateResponse::new(
            self.sequence,
            node_id,
            cause,
            self.up_function_features,
            self.cp_function_features,
            self.ue_ip_address_usage_information,
            self.ies,
        )
    }

    /// Tries to build the AssociationUpdateResponse message.
    ///
    /// # Returns
    /// Returns an error if required IEs are not set.
    pub fn try_build(self) -> Result<AssociationUpdateResponse, &'static str> {
        let node_id = self
            .node_id
            .ok_or("Node ID IE is required for AssociationUpdateResponse")?;
        let cause = self
            .cause
            .ok_or("Cause IE is required for AssociationUpdateResponse")?;

        Ok(AssociationUpdateResponse::new(
            self.sequence,
            node_id,
            cause,
            self.up_function_features,
            self.cp_function_features,
            self.ue_ip_address_usage_information,
            self.ies,
        ))
    }

    /// Builds the AssociationUpdateResponse message and marshals it to bytes in one step.
    ///
    /// This is a convenience method that combines `build()` and `marshal()`.
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::message::association_update_response::AssociationUpdateResponseBuilder;
    /// use rs_pfcp::ie::{Ie, IeType, cause::CauseValue, node_id::NodeId};
    /// use std::net::Ipv4Addr;
    ///
    /// let node_id = Ie::new(IeType::NodeId, NodeId::IPv4(Ipv4Addr::new(127, 0, 0, 1)).marshal().to_vec());
    /// let bytes = AssociationUpdateResponseBuilder::new(1)
    ///     .node_id(node_id)
    ///     .cause(CauseValue::RequestAccepted)
    ///     .marshal();
    /// ```
    pub fn marshal(self) -> Vec<u8> {
        self.build().marshal()
    }
}

#[cfg(test)]
mod builder_tests {
    use super::*;
    use crate::ie::cause::{Cause, CauseValue};
    use crate::ie::node_id::NodeId;
    use std::net::Ipv4Addr;

    #[test]
    fn test_association_update_response_builder_minimal() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(192, 168, 1, 1));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        let response = AssociationUpdateResponseBuilder::new(12345)
            .node_id(node_id_ie.clone())
            .cause_ie(cause_ie.clone())
            .build();

        assert_eq!(*response.sequence(), 12345);
        assert_eq!(response.seid(), None); // Association messages have no SEID
        assert_eq!(response.msg_type(), MsgType::AssociationUpdateResponse);
        assert_eq!(response.node_id, node_id_ie);
        assert_eq!(response.cause, cause_ie);
        assert!(response.up_function_features.is_none());
        assert!(response.cp_function_features.is_none());
        assert!(response.ies.is_empty());
    }

    #[test]
    fn test_association_update_response_builder_with_up_features() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(10, 0, 0, 1));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        let up_features_ie = Ie::new(IeType::UpFunctionFeatures, vec![0x01, 0x02, 0x03]);

        let response = AssociationUpdateResponseBuilder::new(67890)
            .node_id(node_id_ie.clone())
            .cause_ie(cause_ie.clone())
            .up_function_features(up_features_ie.clone())
            .build();

        assert_eq!(*response.sequence(), 67890);
        assert_eq!(response.node_id, node_id_ie);
        assert_eq!(response.cause, cause_ie);
        assert_eq!(response.up_function_features, Some(up_features_ie));
        assert!(response.cp_function_features.is_none());
    }

    #[test]
    fn test_association_update_response_builder_with_cp_features() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(172, 16, 0, 1));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        let cp_features_ie = Ie::new(IeType::CpFunctionFeatures, vec![0x04, 0x05, 0x06]);

        let response = AssociationUpdateResponseBuilder::new(11111)
            .node_id(node_id_ie.clone())
            .cause_ie(cause_ie.clone())
            .cp_function_features(cp_features_ie.clone())
            .build();

        assert_eq!(*response.sequence(), 11111);
        assert_eq!(response.node_id, node_id_ie);
        assert_eq!(response.cause, cause_ie);
        assert!(response.up_function_features.is_none());
        assert_eq!(response.cp_function_features, Some(cp_features_ie));
    }

    #[test]
    fn test_association_update_response_builder_with_additional_ies() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(203, 0, 113, 1));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        let ie1 = Ie::new(IeType::Unknown, vec![0xAA, 0xBB]);
        let ie2 = Ie::new(IeType::Unknown, vec![0xCC, 0xDD]);
        let ie3 = Ie::new(IeType::Unknown, vec![0xEE, 0xFF]);

        let response = AssociationUpdateResponseBuilder::new(22222)
            .node_id(node_id_ie.clone())
            .cause_ie(cause_ie.clone())
            .ie(ie1.clone())
            .ies(vec![ie2.clone(), ie3.clone()])
            .build();

        assert_eq!(*response.sequence(), 22222);
        assert_eq!(response.node_id, node_id_ie);
        assert_eq!(response.cause, cause_ie);
        assert_eq!(response.ies.len(), 3);
        assert_eq!(response.ies[0], ie1);
        assert_eq!(response.ies[1], ie2);
        assert_eq!(response.ies[2], ie3);
    }

    #[test]
    fn test_association_update_response_builder_full() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(198, 51, 100, 1));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        let up_features_ie = Ie::new(IeType::UpFunctionFeatures, vec![0x11, 0x22]);
        let cp_features_ie = Ie::new(IeType::CpFunctionFeatures, vec![0x33, 0x44]);
        let additional_ie = Ie::new(IeType::Unknown, vec![0xFF, 0xEE, 0xDD]);

        let response = AssociationUpdateResponseBuilder::new(33333)
            .node_id(node_id_ie.clone())
            .cause_ie(cause_ie.clone())
            .up_function_features(up_features_ie.clone())
            .cp_function_features(cp_features_ie.clone())
            .ie(additional_ie.clone())
            .build();

        assert_eq!(*response.sequence(), 33333);
        assert_eq!(response.node_id, node_id_ie);
        assert_eq!(response.cause, cause_ie);
        assert_eq!(response.up_function_features, Some(up_features_ie));
        assert_eq!(response.cp_function_features, Some(cp_features_ie));
        assert_eq!(response.ies.len(), 1);
        assert_eq!(response.ies[0], additional_ie);
    }

    #[test]
    fn test_association_update_response_builder_try_build_success() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(192, 0, 2, 1));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        let result = AssociationUpdateResponseBuilder::new(44444)
            .node_id(node_id_ie.clone())
            .cause_ie(cause_ie.clone())
            .try_build();

        assert!(result.is_ok());
        let response = result.unwrap();
        assert_eq!(*response.sequence(), 44444);
        assert_eq!(response.node_id, node_id_ie);
        assert_eq!(response.cause, cause_ie);
    }

    #[test]
    fn test_association_update_response_builder_try_build_missing_node_id() {
        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        let result = AssociationUpdateResponseBuilder::new(55555)
            .cause_ie(cause_ie)
            .try_build();

        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            "Node ID IE is required for AssociationUpdateResponse"
        );
    }

    #[test]
    fn test_association_update_response_builder_try_build_missing_cause() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(192, 168, 1, 2));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        let result = AssociationUpdateResponseBuilder::new(66666)
            .node_id(node_id_ie)
            .try_build();

        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            "Cause IE is required for AssociationUpdateResponse"
        );
    }

    #[test]
    #[should_panic(expected = "Node ID IE is required for AssociationUpdateResponse")]
    fn test_association_update_response_builder_build_panic_missing_node_id() {
        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        AssociationUpdateResponseBuilder::new(77777)
            .cause_ie(cause_ie)
            .build();
    }

    #[test]
    #[should_panic(expected = "Cause IE is required for AssociationUpdateResponse")]
    fn test_association_update_response_builder_build_panic_missing_cause() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(203, 0, 113, 2));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        AssociationUpdateResponseBuilder::new(88888)
            .node_id(node_id_ie)
            .build();
    }

    #[test]
    fn test_association_update_response_builder_roundtrip() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(172, 16, 100, 1));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());

        let cause = Cause::new(CauseValue::RequestAccepted);
        let cause_ie = Ie::new(IeType::Cause, cause.marshal().to_vec());

        let up_features_ie = Ie::new(IeType::UpFunctionFeatures, vec![0xAB, 0xCD]);

        let original = AssociationUpdateResponseBuilder::new(99999)
            .node_id(node_id_ie)
            .cause_ie(cause_ie)
            .up_function_features(up_features_ie)
            .build();

        let marshaled = original.marshal();
        let unmarshaled = AssociationUpdateResponse::unmarshal(&marshaled).unwrap();

        assert_eq!(original, unmarshaled);
    }

    #[test]
    fn test_association_update_response_ue_ip_usage_roundtrip() {
        let node_id = NodeId::new_ipv4(Ipv4Addr::new(10, 0, 0, 1));
        let node_id_ie = Ie::new(IeType::NodeId, node_id.marshal());
        let cause_ie = Ie::new(
            IeType::Cause,
            Cause::new(CauseValue::RequestAccepted).marshal().to_vec(),
        );
        let ue_usage_ie = Ie::new(IeType::UeIpAddressUsageInformation, vec![0x01, 0x02, 0x03]);

        let original = AssociationUpdateResponseBuilder::new(11111)
            .node_id(node_id_ie)
            .cause_ie(cause_ie)
            .ue_ip_address_usage_information(ue_usage_ie)
            .build();

        assert_eq!(original.ue_ip_address_usage_information.len(), 1);

        let marshaled = original.marshal();
        let unmarshaled = AssociationUpdateResponse::unmarshal(&marshaled).unwrap();
        assert_eq!(original, unmarshaled);
    }
}