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
//! MAC Addresses Removed Information Element
//!
//! The MAC Addresses Removed IE contains a list of MAC address values that have been removed
//! on an Ethernet PDU session. Per 3GPP TS 29.244 Section 8.2.104, this IE contains raw
//! 6-byte MAC address values with optional C-TAG and S-TAG VLAN identifiers.

use crate::error::PfcpError;
use crate::ie::c_tag::CTag;
use crate::ie::s_tag::STag;
use crate::ie::{Ie, IeType};

/// MAC Addresses Removed
///
/// Contains a list of raw MAC address values removed on an Ethernet PDU session.
///
/// # 3GPP Reference
/// 3GPP TS 29.244 Section 8.2.104
///
/// # Structure (per 3GPP TS 29.244 ยง 8.2.104)
/// - Octet 5: Number of MAC addresses (k)
/// - Octets 6+: MAC address values (6 bytes each)
/// - Length of C-TAG field (1 byte)
/// - C-TAG field (3 bytes if length > 0)
/// - Length of S-TAG field (1 byte)
/// - S-TAG field (3 bytes if length > 0)
///
/// # Examples
///
/// ```
/// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
///
/// // Create with single MAC address
/// let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
/// let removed = MacAddressesRemoved::new(vec![mac1]).unwrap();
/// assert_eq!(removed.addresses().len(), 1);
///
/// // Create with multiple MAC addresses
/// let mac2 = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
/// let removed2 = MacAddressesRemoved::new(vec![mac1, mac2]).unwrap();
/// assert_eq!(removed2.addresses().len(), 2);
///
/// // Marshal and unmarshal
/// let bytes = removed.marshal();
/// let parsed = MacAddressesRemoved::unmarshal(&bytes).unwrap();
/// assert_eq!(removed, parsed);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MacAddressesRemoved {
    /// List of removed MAC address values (6 bytes each)
    addresses: Vec<[u8; 6]>,
    /// Optional Customer VLAN tag (C-TAG)
    c_tag: Option<CTag>,
    /// Optional Service VLAN tag (S-TAG)
    s_tag: Option<STag>,
}

impl MacAddressesRemoved {
    /// Maximum number of MAC addresses (per spec)
    pub const MAX_ADDRESSES: usize = 255;

    /// Create new MAC Addresses Removed IE without VLAN tags
    ///
    /// # Arguments
    /// * `addresses` - List of MAC addresses (max 255)
    ///
    /// # Errors
    /// Returns error if more than 255 addresses provided
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    ///
    /// let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
    /// let removed = MacAddressesRemoved::new(vec![mac]).unwrap();
    /// assert_eq!(removed.addresses().len(), 1);
    /// ```
    pub fn new(addresses: Vec<[u8; 6]>) -> Result<Self, PfcpError> {
        Self::new_with_vlan(addresses, None, None)
    }

    /// Create new MAC Addresses Removed IE with VLAN tags
    ///
    /// Per 3GPP TS 29.244 ยง 8.2.104, C-TAG and S-TAG fields allow multiple instances
    /// of this IE with different VLAN configurations.
    ///
    /// # Arguments
    /// * `addresses` - List of MAC addresses (max 255)
    /// * `c_tag` - Optional Customer VLAN tag
    /// * `s_tag` - Optional Service VLAN tag
    ///
    /// # Errors
    /// Returns error if more than 255 addresses provided
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    /// use rs_pfcp::ie::c_tag::CTag;
    /// use rs_pfcp::ie::s_tag::STag;
    ///
    /// let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
    /// let c_tag = CTag::new(1, false, 100).unwrap();
    /// let s_tag = STag::new(2, false, 200).unwrap();
    ///
    /// let removed = MacAddressesRemoved::new_with_vlan(
    ///     vec![mac],
    ///     Some(c_tag),
    ///     Some(s_tag)
    /// ).unwrap();
    /// assert_eq!(removed.addresses().len(), 1);
    /// assert!(removed.c_tag().is_some());
    /// assert!(removed.s_tag().is_some());
    /// ```
    pub fn new_with_vlan(
        addresses: Vec<[u8; 6]>,
        c_tag: Option<CTag>,
        s_tag: Option<STag>,
    ) -> Result<Self, PfcpError> {
        if addresses.len() > Self::MAX_ADDRESSES {
            return Err(PfcpError::invalid_value(
                "MAC Addresses Removed count",
                addresses.len().to_string(),
                format!("cannot contain more than {} addresses", Self::MAX_ADDRESSES),
            ));
        }
        Ok(MacAddressesRemoved {
            addresses,
            c_tag,
            s_tag,
        })
    }

    /// Create empty MAC Addresses Removed IE
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    ///
    /// let removed = MacAddressesRemoved::empty();
    /// assert_eq!(removed.addresses().len(), 0);
    /// ```
    pub fn empty() -> Self {
        MacAddressesRemoved {
            addresses: Vec::new(),
            c_tag: None,
            s_tag: None,
        }
    }

    /// Get the list of removed MAC address values
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    ///
    /// let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
    /// let removed = MacAddressesRemoved::new(vec![mac]).unwrap();
    /// assert_eq!(removed.addresses()[0], mac);
    /// ```
    pub fn addresses(&self) -> &[[u8; 6]] {
        &self.addresses
    }

    /// Get the number of removed MAC addresses
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    ///
    /// let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
    /// let mac2 = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
    /// let removed = MacAddressesRemoved::new(vec![mac1, mac2]).unwrap();
    /// assert_eq!(removed.count(), 2);
    /// ```
    pub fn count(&self) -> usize {
        self.addresses.len()
    }

    /// Get the optional C-TAG (Customer VLAN tag)
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    /// use rs_pfcp::ie::c_tag::CTag;
    ///
    /// let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
    /// let c_tag = CTag::new(1, false, 100).unwrap();
    /// let removed = MacAddressesRemoved::new_with_vlan(vec![mac], Some(c_tag), None).unwrap();
    /// assert!(removed.c_tag().is_some());
    /// ```
    pub fn c_tag(&self) -> Option<&CTag> {
        self.c_tag.as_ref()
    }

    /// Get the optional S-TAG (Service VLAN tag)
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    /// use rs_pfcp::ie::s_tag::STag;
    ///
    /// let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
    /// let s_tag = STag::new(2, false, 200).unwrap();
    /// let removed = MacAddressesRemoved::new_with_vlan(vec![mac], None, Some(s_tag)).unwrap();
    /// assert!(removed.s_tag().is_some());
    /// ```
    pub fn s_tag(&self) -> Option<&STag> {
        self.s_tag.as_ref()
    }

    /// Marshal MAC Addresses Removed to bytes
    ///
    /// # Returns
    /// Vector containing:
    /// - Count (1 byte)
    /// - MAC addresses (6 bytes each)
    /// - C-TAG length (1 byte) + C-TAG data (3 bytes if present)
    /// - S-TAG length (1 byte) + S-TAG data (3 bytes if present)
    ///
    /// Per 3GPP TS 29.244 ยง 8.2.104
    pub fn marshal(&self) -> Vec<u8> {
        let mut bytes = Vec::new();

        // Octet 5: Number of MAC addresses
        bytes.push(self.addresses.len() as u8);

        // Octets 6+: MAC address values (6 bytes each)
        for mac in &self.addresses {
            bytes.extend_from_slice(mac);
        }

        // C-TAG length (1 byte) + C-TAG data (3 bytes if present)
        if let Some(ref ctag) = self.c_tag {
            bytes.push(3); // Length of C-TAG field
            bytes.extend_from_slice(&ctag.marshal());
        } else {
            bytes.push(0); // C-TAG field absent
        }

        // S-TAG length (1 byte) + S-TAG data (3 bytes if present)
        if let Some(ref stag) = self.s_tag {
            bytes.push(3); // Length of S-TAG field
            bytes.extend_from_slice(&stag.marshal());
        } else {
            bytes.push(0); // S-TAG field absent
        }

        bytes
    }

    /// Unmarshal MAC Addresses Removed from bytes
    ///
    /// # Arguments
    /// * `data` - Byte slice containing MAC addresses data
    ///
    /// # Errors
    /// Returns error if data is too short or malformed
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    ///
    /// let mac = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66];
    /// let removed = MacAddressesRemoved::new(vec![mac]).unwrap();
    /// let bytes = removed.marshal();
    /// let parsed = MacAddressesRemoved::unmarshal(&bytes).unwrap();
    /// assert_eq!(removed, parsed);
    /// ```
    pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
        if data.is_empty() {
            return Err(PfcpError::invalid_length(
                "MAC Addresses Removed",
                IeType::MacAddressesRemoved,
                1,
                0,
            ));
        }

        let count = data[0] as usize;
        let mut offset = 1;

        // Parse MAC addresses
        let mut addresses = Vec::with_capacity(count);
        for _ in 0..count {
            if offset + 6 > data.len() {
                return Err(PfcpError::invalid_length(
                    "MAC Addresses Removed MAC address",
                    IeType::MacAddressesRemoved,
                    offset + 6,
                    data.len(),
                ));
            }

            let mut mac = [0u8; 6];
            mac.copy_from_slice(&data[offset..offset + 6]);
            addresses.push(mac);
            offset += 6;
        }

        // Parse C-TAG length and data (if present)
        let c_tag = if offset < data.len() {
            let c_tag_len = data[offset] as usize;
            offset += 1;

            if c_tag_len > 0 {
                if c_tag_len != 3 {
                    return Err(PfcpError::invalid_value(
                        "MAC Addresses Removed C-TAG length",
                        c_tag_len.to_string(),
                        "must be 0 or 3",
                    ));
                }

                if offset + c_tag_len > data.len() {
                    return Err(PfcpError::invalid_length(
                        "MAC Addresses Removed C-TAG",
                        IeType::MacAddressesRemoved,
                        offset + c_tag_len,
                        data.len(),
                    ));
                }

                let ctag = CTag::unmarshal(&data[offset..offset + c_tag_len])?;
                offset += c_tag_len;
                Some(ctag)
            } else {
                None
            }
        } else {
            None
        };

        // Parse S-TAG length and data (if present)
        let s_tag = if offset < data.len() {
            let s_tag_len = data[offset] as usize;
            offset += 1;

            if s_tag_len > 0 {
                if s_tag_len != 3 {
                    return Err(PfcpError::invalid_value(
                        "MAC Addresses Removed S-TAG length",
                        s_tag_len.to_string(),
                        "must be 0 or 3",
                    ));
                }

                if offset + s_tag_len > data.len() {
                    return Err(PfcpError::invalid_length(
                        "MAC Addresses Removed S-TAG",
                        IeType::MacAddressesRemoved,
                        offset + s_tag_len,
                        data.len(),
                    ));
                }

                let stag = STag::unmarshal(&data[offset..offset + s_tag_len])?;
                Some(stag)
            } else {
                None
            }
        } else {
            None
        };

        Ok(MacAddressesRemoved {
            addresses,
            c_tag,
            s_tag,
        })
    }

    /// Convert to generic IE
    ///
    /// # Example
    /// ```
    /// use rs_pfcp::ie::mac_addresses_removed::MacAddressesRemoved;
    /// use rs_pfcp::ie::IeType;
    ///
    /// let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
    /// let removed = MacAddressesRemoved::new(vec![mac]).unwrap();
    /// let ie = removed.to_ie();
    /// assert_eq!(ie.ie_type, IeType::MacAddressesRemoved);
    /// ```
    pub fn to_ie(&self) -> Ie {
        Ie::new(IeType::MacAddressesRemoved, self.marshal())
    }
}

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

    #[test]
    fn test_mac_addresses_removed_empty() {
        let removed = MacAddressesRemoved::empty();
        assert_eq!(removed.count(), 0);
        assert_eq!(removed.addresses().len(), 0);
    }

    #[test]
    fn test_mac_addresses_removed_single() {
        let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let removed = MacAddressesRemoved::new(vec![mac]).unwrap();
        assert_eq!(removed.count(), 1);
        assert_eq!(removed.addresses()[0], mac);
    }

    #[test]
    fn test_mac_addresses_removed_multiple() {
        let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let mac2 = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
        let mac3 = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66];

        let removed = MacAddressesRemoved::new(vec![mac1, mac2, mac3]).unwrap();
        assert_eq!(removed.count(), 3);
        assert_eq!(removed.addresses()[0], mac1);
        assert_eq!(removed.addresses()[1], mac2);
        assert_eq!(removed.addresses()[2], mac3);
    }

    #[test]
    fn test_mac_addresses_removed_too_many() {
        use crate::error::PfcpError;

        let addresses: Vec<[u8; 6]> = (0..=255).map(|i| [i as u8, 0, 0, 0, 0, 0]).collect();

        let result = MacAddressesRemoved::new(addresses);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, PfcpError::InvalidValue { .. }));
    }

    #[test]
    fn test_mac_addresses_removed_marshal_empty() {
        let removed = MacAddressesRemoved::empty();
        let bytes = removed.marshal();
        // Per ยง8.2.104: count (1) + C-TAG length (1) + S-TAG length (1) = 3 bytes
        assert_eq!(bytes.len(), 3);
        assert_eq!(bytes[0], 0); // Count = 0
        assert_eq!(bytes[1], 0); // C-TAG length = 0 (absent)
        assert_eq!(bytes[2], 0); // S-TAG length = 0 (absent)
    }

    #[test]
    fn test_mac_addresses_removed_marshal_single() {
        let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let removed = MacAddressesRemoved::new(vec![mac]).unwrap();
        let bytes = removed.marshal();

        // Per ยง8.2.104: count (1) + MAC (6) + C-TAG length (1) + S-TAG length (1) = 9 bytes
        assert_eq!(bytes.len(), 9);
        assert_eq!(bytes[0], 1); // Count
        assert_eq!(&bytes[1..7], &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
        assert_eq!(bytes[7], 0); // C-TAG length = 0 (absent)
        assert_eq!(bytes[8], 0); // S-TAG length = 0 (absent)
    }

    #[test]
    fn test_mac_addresses_removed_marshal_multiple() {
        let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let mac2 = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];

        let removed = MacAddressesRemoved::new(vec![mac1, mac2]).unwrap();
        let bytes = removed.marshal();

        // Per ยง8.2.104: count (1) + 2 MACs (12) + C-TAG length (1) + S-TAG length (1) = 15 bytes
        assert_eq!(bytes.len(), 15);
        assert_eq!(bytes[0], 2); // Count
        assert_eq!(&bytes[1..7], &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
        assert_eq!(&bytes[7..13], &[0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]);
        assert_eq!(bytes[13], 0); // C-TAG length = 0 (absent)
        assert_eq!(bytes[14], 0); // S-TAG length = 0 (absent)
    }

    #[test]
    fn test_mac_addresses_removed_unmarshal_empty() {
        let data = [0x00]; // Count = 0
        let removed = MacAddressesRemoved::unmarshal(&data).unwrap();
        assert_eq!(removed.count(), 0);
    }

    #[test]
    fn test_mac_addresses_removed_unmarshal_single() {
        let data = [0x01, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let removed = MacAddressesRemoved::unmarshal(&data).unwrap();
        assert_eq!(removed.count(), 1);
        assert_eq!(removed.addresses()[0], [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
    }

    #[test]
    fn test_mac_addresses_removed_unmarshal_multiple() {
        let data = [
            0x02, // Count = 2
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, // MAC 1
            0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, // MAC 2
        ];
        let removed = MacAddressesRemoved::unmarshal(&data).unwrap();
        assert_eq!(removed.count(), 2);
        assert_eq!(removed.addresses()[0], [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
        assert_eq!(removed.addresses()[1], [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]);
    }

    #[test]
    fn test_mac_addresses_removed_unmarshal_no_data() {
        use crate::error::PfcpError;

        let result = MacAddressesRemoved::unmarshal(&[]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, PfcpError::InvalidLength { .. }));
    }

    #[test]
    fn test_mac_addresses_removed_unmarshal_incomplete() {
        let data = [0x02, 0x00, 0x11, 0x22]; // Says 2 MACs but only partial data
        let result = MacAddressesRemoved::unmarshal(&data);
        assert!(result.is_err());
    }

    #[test]
    fn test_mac_addresses_removed_round_trip() {
        let test_cases = vec![
            vec![],
            vec![[0x00, 0x11, 0x22, 0x33, 0x44, 0x55]],
            vec![
                [0x00, 0x11, 0x22, 0x33, 0x44, 0x55],
                [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF],
            ],
            vec![
                [0x11, 0x22, 0x33, 0x44, 0x55, 0x66],
                [0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC],
                [0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22],
            ],
        ];

        for addresses in test_cases {
            let original = MacAddressesRemoved::new(addresses.clone()).unwrap();
            let marshaled = original.marshal();
            let unmarshaled = MacAddressesRemoved::unmarshal(&marshaled).unwrap();
            assert_eq!(
                original,
                unmarshaled,
                "Failed for {} addresses",
                addresses.len()
            );
        }
    }

    #[test]
    fn test_mac_addresses_removed_to_ie() {
        let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let removed = MacAddressesRemoved::new(vec![mac]).unwrap();
        let ie = removed.to_ie();

        assert_eq!(ie.ie_type, IeType::MacAddressesRemoved);
        assert_eq!(ie.payload.len(), 9); // Per ยง8.2.104: includes VLAN tag length fields

        // Verify IE can be unmarshaled
        let parsed = MacAddressesRemoved::unmarshal(&ie.payload).unwrap();
        assert_eq!(removed, parsed);
    }

    #[test]
    fn test_mac_addresses_removed_scenarios() {
        // Scenario 1: Single device removed
        let device1 = [0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E];
        let removed1 = MacAddressesRemoved::new(vec![device1]).unwrap();
        assert_eq!(removed1.count(), 1);

        // Scenario 2: Multiple devices on network segment
        let devices = vec![
            [0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E],
            [0x00, 0x1B, 0x44, 0x11, 0x3A, 0x2F],
            [0x00, 0x50, 0x56, 0xC0, 0x00, 0x01],
        ];
        let removed2 = MacAddressesRemoved::new(devices).unwrap();
        assert_eq!(removed2.count(), 3);

        // Scenario 3: No devices removed yet
        let removed3 = MacAddressesRemoved::empty();
        assert_eq!(removed3.count(), 0);
    }

    #[test]
    fn test_mac_addresses_removed_with_vlan() {
        use crate::ie::c_tag::CTag;
        use crate::ie::s_tag::STag;

        let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let c_tag = CTag::new(1, false, 100).unwrap();
        let s_tag = STag::new(2, false, 200).unwrap();

        let removed =
            MacAddressesRemoved::new_with_vlan(vec![mac], Some(c_tag), Some(s_tag)).unwrap();

        assert_eq!(removed.count(), 1);
        assert!(removed.c_tag().is_some());
        assert!(removed.s_tag().is_some());
    }

    #[test]
    fn test_mac_addresses_removed_vlan_round_trip() {
        use crate::ie::c_tag::CTag;
        use crate::ie::s_tag::STag;

        let mac1 = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let mac2 = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
        let c_tag = CTag::new(1, false, 100).unwrap();
        let s_tag = STag::new(2, false, 200).unwrap();

        let original =
            MacAddressesRemoved::new_with_vlan(vec![mac1, mac2], Some(c_tag), Some(s_tag)).unwrap();
        let marshaled = original.marshal();
        let unmarshaled = MacAddressesRemoved::unmarshal(&marshaled).unwrap();

        assert_eq!(unmarshaled, original);
        assert_eq!(unmarshaled.count(), 2);
        assert!(unmarshaled.c_tag().is_some());
        assert!(unmarshaled.s_tag().is_some());
    }

    #[test]
    fn test_mac_addresses_removed_c_tag_only() {
        use crate::ie::c_tag::CTag;

        let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let c_tag = CTag::new(1, false, 100).unwrap();

        let removed = MacAddressesRemoved::new_with_vlan(vec![mac], Some(c_tag), None).unwrap();

        assert_eq!(removed.count(), 1);
        assert!(removed.c_tag().is_some());
        assert!(removed.s_tag().is_none());

        // Round trip
        let marshaled = removed.marshal();
        let unmarshaled = MacAddressesRemoved::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled, removed);
    }

    #[test]
    fn test_mac_addresses_removed_s_tag_only() {
        use crate::ie::s_tag::STag;

        let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let s_tag = STag::new(2, false, 200).unwrap();

        let removed = MacAddressesRemoved::new_with_vlan(vec![mac], None, Some(s_tag)).unwrap();

        assert_eq!(removed.count(), 1);
        assert!(removed.c_tag().is_none());
        assert!(removed.s_tag().is_some());

        // Round trip
        let marshaled = removed.marshal();
        let unmarshaled = MacAddressesRemoved::unmarshal(&marshaled).unwrap();
        assert_eq!(unmarshaled, removed);
    }

    #[test]
    fn test_mac_addresses_removed_vlan_marshal_format() {
        use crate::ie::c_tag::CTag;
        use crate::ie::s_tag::STag;

        let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let c_tag = CTag::new(1, false, 100).unwrap();
        let s_tag = STag::new(2, false, 200).unwrap();

        let removed =
            MacAddressesRemoved::new_with_vlan(vec![mac], Some(c_tag), Some(s_tag)).unwrap();
        let bytes = removed.marshal();

        // Per ยง8.2.104: count (1) + MAC (6) + C-TAG length (1) + C-TAG (3) + S-TAG length (1) + S-TAG (3) = 15 bytes
        assert_eq!(bytes.len(), 15);
        assert_eq!(bytes[0], 1); // Count
        assert_eq!(&bytes[1..7], &mac); // MAC address
        assert_eq!(bytes[7], 3); // C-TAG length = 3
                                 // bytes[8..11] = C-TAG data (3 bytes)
        assert_eq!(bytes[11], 3); // S-TAG length = 3
                                  // bytes[12..15] = S-TAG data (3 bytes)
    }
}