zero_packet/packet/
parser.rs

1use crate::{
2    datalink::{
3        arp::ArpReader,
4        ethernet::{EthernetReader, ETHERNET_MIN_FRAME_LENGTH},
5    },
6    misc::{EtherType, Icmpv4Type, Icmpv6Type, IpInIp, IpProtocol},
7    network::{
8        checksum::{pseudo_header, verify_internet_checksum},
9        icmpv4::{Icmpv4Reader, ICMPV4_MAX_VALID_CODE},
10        icmpv6::Icmpv6Reader,
11        ipv4::{IPv4Reader, IPV4_MIN_HEADER_LENGTH},
12        ipv6::IPv6Reader,
13    },
14    transport::{
15        tcp::{TcpReader, TCP_MIN_HEADER_LENGTH},
16        udp::UdpReader,
17    },
18};
19
20/// A zero-copy packet parser.
21#[derive(Debug)]
22pub struct PacketParser<'a> {
23    pub ethernet: Option<EthernetReader<'a>>,
24    pub arp: Option<ArpReader<'a>>,
25    pub ipv4: Option<IPv4Reader<'a>>,
26    pub ipv6: Option<IPv6Reader<'a>>,
27    pub ip_in_ip: Option<IpInIp<'a>>,
28    pub tcp: Option<TcpReader<'a>>,
29    pub udp: Option<UdpReader<'a>>,
30    pub icmpv4: Option<Icmpv4Reader<'a>>,
31    pub icmpv6: Option<Icmpv6Reader<'a>>,
32}
33
34impl<'a> PacketParser<'a> {
35    /// Creates a new `PacketParser`.
36    #[inline]
37    fn new() -> Self {
38        Self {
39            ethernet: None,
40            arp: None,
41            ipv4: None,
42            ipv6: None,
43            ip_in_ip: None,
44            tcp: None,
45            udp: None,
46            icmpv4: None,
47            icmpv6: None,
48        }
49    }
50
51    /// Determines the protocols encapsulated in some bytes and attempts to parse them.
52    #[inline]
53    pub fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
54        let mut parser = Self::new();
55
56        let ethernet = EthernetReader::parse(bytes)?;
57        let payload = &bytes[ethernet.header_len()..];
58
59        match EtherType::from(ethernet.ethertype()) {
60            EtherType::Arp => parser.arp = Some(ArpReader::parse(payload)?),
61            EtherType::Ipv4 => parser.parse_ipv4(payload, true)?,
62            EtherType::Ipv6 => parser.parse_ipv6(payload, true)?,
63            _ => (), // Unknown EtherType, proceed.
64        }
65
66        parser.ethernet = Some(ethernet);
67
68        Ok(parser)
69    }
70
71    /// Parsing IPv4 packets.
72    #[inline]
73    fn parse_ipv4(&mut self, payload: &'a [u8], from_ether: bool) -> Result<(), &'static str> {
74        let ipv4 = IPv4Reader::parse(payload)?;
75
76        let payload = ipv4.payload()?;
77        let protocol = IpProtocol::from(ipv4.protocol());
78
79        self.parse_protocol(protocol, payload, &ipv4)?;
80
81        if from_ether {
82            self.ipv4 = Some(ipv4);
83        } else {
84            self.ip_in_ip = Some(IpInIp::Ipv4(ipv4));
85        }
86
87        Ok(())
88    }
89
90    /// Parsing IPv6 packets.
91    #[inline]
92    fn parse_ipv6(&mut self, payload: &'a [u8], from_ether: bool) -> Result<(), &'static str> {
93        let ipv6 = IPv6Reader::parse(payload)?;
94
95        let payload = ipv6.upper_layer_payload();
96        let next_header = IpProtocol::from(ipv6.final_next_header());
97
98        self.parse_protocol(next_header, payload, &ipv6)?;
99
100        if from_ether {
101            self.ipv6 = Some(ipv6);
102        } else {
103            self.ip_in_ip = Some(IpInIp::Ipv6(ipv6));
104        }
105
106        Ok(())
107    }
108
109    /// Parsing encapsulated protocols.
110    #[inline]
111    fn parse_protocol<T: VerifyReader<'a>>(
112        &mut self,
113        protocol: IpProtocol,
114        payload: &'a [u8],
115        reader: &T,
116    ) -> Result<(), &'static str> {
117        match protocol {
118            IpProtocol::Tcp => {
119                self.tcp = Some(TcpReader::parse(payload)?);
120                reader.verify_checksum()?;
121            }
122            IpProtocol::Udp => {
123                self.udp = Some(UdpReader::parse(payload)?);
124                reader.verify_checksum()?;
125            }
126            IpProtocol::Icmpv4 => {
127                self.icmpv4 = Some(Icmpv4Reader::parse(payload)?);
128                reader.verify_checksum()?;
129            }
130            IpProtocol::Icmpv6 => {
131                self.icmpv6 = Some(Icmpv6Reader::parse(payload)?);
132                reader.verify_checksum()?;
133            }
134            IpProtocol::Ipv4 => self.parse_ipv4(payload, false)?,
135            IpProtocol::Ipv6 => self.parse_ipv6(payload, false)?,
136            _ => (), // Unknown protocol, proceed.
137        }
138
139        Ok(())
140    }
141}
142
143/// Trait to parse a protocol header.
144pub trait ParseReader<'a>: Sized {
145    /// Parses the protocol header from the given slice.
146    ///
147    /// Checks certain fields for validity.
148    ///
149    /// May fail if any of the fields are deemed invalid.
150    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str>;
151}
152
153impl<'a> ParseReader<'a> for EthernetReader<'a> {
154    /// Parses an Ethernet frame from a byte slice.
155    ///
156    /// Validates that the byte slice is at least the minimum Ethernet frame length.
157    #[inline]
158    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
159        if bytes.len() < ETHERNET_MIN_FRAME_LENGTH {
160            return Err("Slice needs to be least 64 bytes long to be a valid Ethernet frame.");
161        }
162
163        EthernetReader::new(bytes)
164    }
165}
166
167impl<'a> ParseReader<'a> for ArpReader<'a> {
168    /// Parses an ARP packet from a byte slice.
169    ///
170    /// Validates the ARP operation field.
171    #[inline]
172    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
173        let reader = ArpReader::new(bytes)?;
174
175        if reader.oper() > 2 {
176            return Err("ARP operation field is invalid, expected request (1) or reply (2).");
177        }
178
179        Ok(reader)
180    }
181}
182
183impl<'a> ParseReader<'a> for IPv4Reader<'a> {
184    /// Parses an IPv4 packet from a byte slice.
185    ///
186    /// Validates the IPv4 version, header length, total length, and checksum fields.
187    #[inline]
188    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
189        let reader = IPv4Reader::new(bytes)?;
190
191        if reader.version() != 4 {
192            return Err("IPv4 version field is invalid. Expected version 4.");
193        }
194
195        if reader.header_len() < IPV4_MIN_HEADER_LENGTH {
196            return Err("IPv4 IHL field is invalid. Indicated header length is too short.");
197        }
198
199        if bytes.len() < reader.header_len() {
200            return Err("IPv4 header length is invalid. Indicated header length is too long.");
201        }
202
203        if bytes.len() != reader.total_length() as usize {
204            return Err("IPv4 total length field is invalid. Does not match actual length.");
205        }
206
207        if !reader.valid_checksum()? {
208            return Err("IPv4 checksum is invalid.");
209        }
210
211        Ok(reader)
212    }
213}
214
215impl<'a> ParseReader<'a> for IPv6Reader<'a> {
216    /// Parses an IPv6 packet from a byte slice.
217    ///
218    /// Validates the IPv6 version field.
219    ///
220    /// Note: IPv6 does not have a checksum field.
221    #[inline]
222    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
223        let reader = IPv6Reader::new(bytes)?;
224
225        if reader.version() != 6 {
226            return Err("IPv6 version field is invalid. Expected version 6.");
227        }
228
229        Ok(reader)
230    }
231}
232
233impl<'a> ParseReader<'a> for TcpReader<'a> {
234    /// Parses a TCP segment from a byte slice.
235    ///
236    /// Validates the TCP header length and flags fields.
237    #[inline]
238    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
239        let reader = TcpReader::new(bytes)?;
240
241        if reader.header_len() < TCP_MIN_HEADER_LENGTH {
242            return Err("TCP data offset field is invalid. Indicated header length is too short.");
243        }
244
245        if reader.flags() == 0 {
246            return Err("TCP flags field is invalid.");
247        }
248
249        Ok(reader)
250    }
251}
252
253impl<'a> ParseReader<'a> for UdpReader<'a> {
254    /// Parses a UDP datagram from a byte slice.
255    ///
256    /// Validates the UDP length field against the actual length of the datagram.
257    #[inline]
258    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
259        let reader = UdpReader::new(bytes)?;
260
261        if reader.length() as usize != reader.header_len() + reader.payload().len() {
262            return Err("UDP length field is invalid. Does not match actual length.");
263        }
264
265        Ok(reader)
266    }
267}
268
269impl<'a> ParseReader<'a> for Icmpv4Reader<'a> {
270    /// Parses an ICMPv4 message from a byte slice.
271    ///
272    /// Validates the ICMPv4 type and code fields.
273    #[inline]
274    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
275        let reader = Icmpv4Reader::new(bytes)?;
276
277        if Icmpv4Type::from(reader.icmp_type()) == Icmpv4Type::Unknown {
278            return Err("ICMPv4 type field is invalid.");
279        }
280
281        if reader.icmp_code() > ICMPV4_MAX_VALID_CODE {
282            return Err("ICMPv4 code field is invalid.");
283        }
284
285        Ok(reader)
286    }
287}
288
289impl<'a> ParseReader<'a> for Icmpv6Reader<'a> {
290    /// Parses an ICMPv6 message from a byte slice.
291    ///
292    /// Validates the ICMPv6 type field.
293    #[inline]
294    fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
295        let reader = Icmpv6Reader::new(bytes)?;
296
297        if Icmpv6Type::from(reader.icmp_type()) == Icmpv6Type::Unknown {
298            return Err("ICMPv6 type field is invalid.");
299        }
300
301        Ok(reader)
302    }
303}
304
305/// Trait to verify the checksum of protocols encapsulated in IP packets.
306pub trait VerifyReader<'a> {
307    /// Verifies the checksum of the encapsulated protocol.
308    fn verify_checksum(&self) -> Result<(), &'static str>;
309}
310
311impl<'a> VerifyReader<'a> for IPv4Reader<'a> {
312    /// Verifies the checksum of an encapsulated protocol in an IPv4 packet.
313    ///
314    /// Computes the pseudo-header checksum and verifies it with the encapsulated payload.
315    #[inline]
316    fn verify_checksum(&self) -> Result<(), &'static str> {
317        let src: [u8; 4] = self.src_ip().try_into().unwrap(); // Won't panic.
318        let dest: [u8; 4] = self.dest_ip().try_into().unwrap(); // Won't panic.
319        let protocol = self.protocol();
320
321        // ICMPv4 does not use a pseudo-header for checksum calculation.
322        let sum = if IpProtocol::from(protocol) == IpProtocol::Icmpv4 {
323            0
324        } else {
325            pseudo_header(&src, &dest, protocol, self.payload()?.len())
326        };
327
328        if !verify_internet_checksum(self.payload()?, sum) {
329            return Err("IPv4 encapsulated checksum is invalid.");
330        }
331
332        Ok(())
333    }
334}
335
336impl<'a> VerifyReader<'a> for IPv6Reader<'a> {
337    /// Verifies the checksum of an encapsulated protocol in an IPv6 packet.
338    ///
339    /// Computes the pseudo-header checksum and verifies it with the encapsulated payload.
340    #[inline]
341    fn verify_checksum(&self) -> Result<(), &'static str> {
342        if IpProtocol::from(self.final_next_header()) == IpProtocol::NoNextHeader {
343            return Ok(());
344        }
345
346        let src: [u8; 16] = self.src_addr().try_into().unwrap(); // Won't panic.
347        let dest: [u8; 16] = self.dest_addr().try_into().unwrap(); // Won't panic.
348
349        let sum = pseudo_header(
350            &src,
351            &dest,
352            self.final_next_header(),
353            self.upper_layer_payload().len(),
354        );
355
356        if !verify_internet_checksum(self.upper_layer_payload(), sum) {
357            return Err("IPv6 encapsulated checksum is invalid.");
358        }
359
360        Ok(())
361    }
362}
363
364#[cfg(test)]
365mod tests {
366    use super::*;
367
368    #[test]
369    fn parse_frame_too_short() {
370        // Raw packet data. A valid Ethernet frame needs at least 64 bytes.
371        let packet = [
372            4, 180, 254, 154, 129, 199, 52, 151, 246, 148, 2, 15, 8, 0, 53, 143, 48, 57, 212, 49,
373            112, 57, 123, 6, 87, 113, 192, 168, 1, 1, 192, 168, 1, 2, 0, 99, 0, 11, 0, 0, 0, 123,
374            0, 0, 1, 65, 59, 99, 16, 225, 41, 81, 4, 210,
375        ];
376
377        // Parse the packet.
378        let parser = PacketParser::parse(&packet);
379
380        // Ensure the parser fails.
381        assert!(parser.is_err());
382    }
383
384    #[test]
385    fn vlan_tagged_frame() {
386        // Ethernet II + IPv4 (VLAN tagged) + UDP.
387        let packet = [
388            // Ethernet header.
389            0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E, // Destination MAC.
390            0x5E, 0x4D, 0x3C, 0x2B, 0x1A, 0x00, // Source MAC.
391            0x81, 0x00, // VLAN Ethertype.
392            0x00, 0x64, // VLAN Tag (priority 0, CFI 0, VLAN ID 100).
393            0x08, 0x00, // Ethertype (IPv4).
394            // IPv4 header.
395            0x45, 0x00, 0x00, 0x2e, // Version, IHL, DSCP, ECN, Total Length (46 bytes).
396            0x00, 0x00, 0x00, 0x00, // Identification, Flags, Fragment Offset.
397            0x40, 0x11, 0xf9, 0x6b, // TTL (64), Protocol (UDP), IPv4 Checksum.
398            0xC0, 0xA8, 0x00, 0x01, // Source IP (192.168.0.1).
399            0xC0, 0xA8, 0x00, 0x02, // Destination IP (192.168.0.2).
400            // UDP header.
401            0x04, 0xD2, 0x16, 0x2E, // Source Port (1234), Destination Port (5678).
402            0x00, 0x1a, 0x63, 0x66, // Length (26 bytes), UDP Checksum.
403            // Padding.
404            0x00, 0x00, 0x00, 0x00, // Padding.
405            0x00, 0x00, 0x00, 0x00, // Padding.
406            0x00, 0x00, 0x00, 0x00, // Padding.
407            0x00, 0x00, 0x00, 0x00, // Padding.
408            0x00, 0x00, // Padding.
409        ];
410
411        // Parse the packet.
412        let parser = PacketParser::parse(&packet);
413
414        // Ensure the parser succeeds.
415        assert!(parser.is_ok());
416
417        // Unwrap the parser.
418        let unwrapped = parser.unwrap();
419
420        // Ensure the parser has the expected headers.
421        assert!(unwrapped.ethernet.is_some());
422        assert!(unwrapped.ipv4.is_some());
423        assert!(unwrapped.udp.is_some());
424
425        // Ensure these headers are not present.
426        assert!(unwrapped.icmpv4.is_none());
427        assert!(unwrapped.arp.is_none());
428        assert!(unwrapped.tcp.is_none());
429
430        // Unwrap Ethernet II.
431        let ethernet = unwrapped.ethernet.unwrap();
432
433        // Ensure the VLAN tagging.
434        assert!(ethernet.vlan_tag().is_some());
435        assert!(ethernet.double_vlan_tag().is_none());
436
437        // Ensure the VLAN tag is correct.
438        assert_eq!(ethernet.vlan_tag().unwrap(), (0x8100, 100));
439
440        // Ensure the EtherType is correct.
441        assert_eq!(ethernet.ethertype(), 0x0800);
442    }
443
444    #[test]
445    fn double_vlan_tagged_frame() {
446        // Ethernet II + IPv4 (double VLAN tagged) + UDP.
447        let packet = [
448            // Ethernet header.
449            0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E, // Destination MAC.
450            0x5E, 0x4D, 0x3C, 0x2B, 0x1A, 0x00, // Source MAC.
451            0x88, 0xA8, // Outer VLAN Ethertype.
452            0x00, 0xC8, // Outer VLAN Tag (priority 0, CFI 0, VLAN ID 200).
453            0x81, 0x00, // Inner VLAN Ethertype.
454            0x00, 0x64, // Inner VLAN Tag (priority 0, CFI 0, VLAN ID 100).
455            0x08, 0x00, // Ethertype (IPv4).
456            // IPv4 header.
457            0x45, 0x00, 0x00, 0x2a, // Version, IHL, DSCP, ECN, Total Length (42 bytes).
458            0x00, 0x00, 0x00, 0x00, // Identification, Flags, Fragment Offset.
459            0x40, 0x11, 0xf9, 0x6f, // TTL (64), Protocol (UDP), IPv4 Checksum.
460            0xC0, 0xA8, 0x00, 0x01, // Source IP (192.168.0.1).
461            0xC0, 0xA8, 0x00, 0x02, // Destination IP (192.168.0.2).
462            // UDP header.
463            0x04, 0xD2, 0x16, 0x2E, // Source Port (1234), Destination Port (5678).
464            0x00, 0x16, 0x63, 0x6e, // Length (22 bytes), UDP Checksum (0x6366).
465            // Padding.
466            0x00, 0x00, 0x00, 0x00, // Padding.
467            0x00, 0x00, 0x00, 0x00, // Padding.
468            0x00, 0x00, 0x00, 0x00, // Padding.
469            0x00, 0x00, // Padding.
470        ];
471
472        // Parse the packet.
473        let parser = PacketParser::parse(&packet);
474
475        // Ensure the parser succeeds.
476        assert!(parser.is_ok());
477
478        // Unwrap the parser.
479        let unwrapped = parser.unwrap();
480
481        // Ensure the parser has the expected headers.
482        assert!(unwrapped.ethernet.is_some());
483        assert!(unwrapped.ipv4.is_some());
484        assert!(unwrapped.udp.is_some());
485
486        // Ensure these headers are not present.
487        assert!(unwrapped.icmpv4.is_none());
488        assert!(unwrapped.arp.is_none());
489        assert!(unwrapped.tcp.is_none());
490
491        // Unwrap Ethernet II.
492        let ethernet = unwrapped.ethernet.unwrap();
493
494        // Ensure the VLAN tagging.
495        assert!(ethernet.vlan_tag().is_none());
496        assert!(ethernet.double_vlan_tag().is_some());
497
498        // Ensure the double VLAN tag is correct.
499        assert_eq!(
500            ethernet.double_vlan_tag().unwrap(),
501            ((0x88A8, 200), (0x8100, 100))
502        );
503
504        // Ensure the EtherType is correct.
505        assert_eq!(ethernet.ethertype(), 0x0800);
506    }
507
508    #[test]
509    fn icmpv4_echo_response() {
510        // Ethernet II + IPv4 + ICMP.
511        let packet = [
512            0x08, 0x00, 0x20, 0x86, 0x35, 0x4b, 0x00, 0xe0, 0xf7, 0x26, 0x3f, 0xe9, 0x08, 0x00,
513            0x45, 0x00, 0x00, 0x54, 0xaa, 0xfb, 0x40, 0x00, 0xfc, 0x01, 0xfa, 0x30, 0x8b, 0x85,
514            0xe9, 0x02, 0x8b, 0x85, 0xd9, 0x6e, 0x00, 0x00, 0x45, 0xda, 0x1e, 0x60, 0x00, 0x00,
515            0x33, 0x5e, 0x3a, 0xb8, 0x00, 0x00, 0x42, 0xac, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
516            0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
517            0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
518            0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
519        ];
520
521        // Parse the packet.
522        let parser = PacketParser::parse(&packet);
523
524        // Ensure the parser succeeds.
525        assert!(parser.is_ok());
526
527        // Unwrap the parser.
528        let unwrapped = parser.unwrap();
529
530        // Ensure the parser has the expected fields.
531        assert!(unwrapped.ethernet.is_some());
532        assert!(unwrapped.ipv4.is_some());
533        assert!(unwrapped.icmpv4.is_some());
534        assert!(unwrapped.arp.is_none());
535        assert!(unwrapped.tcp.is_none());
536        assert!(unwrapped.udp.is_none());
537
538        // Unwrap headers.
539        let ethernet = unwrapped.ethernet.unwrap();
540        let ipv4 = unwrapped.ipv4.unwrap();
541        let icmpv4 = unwrapped.icmpv4.unwrap();
542
543        // Ensure the fields are correct.
544        assert_eq!(ethernet.ethertype(), 0x0800);
545        assert_eq!(ipv4.protocol(), 1);
546        assert_eq!(ipv4.checksum(), 0xfa30);
547        assert_eq!(icmpv4.icmp_type(), 0);
548        assert_eq!(icmpv4.icmp_code(), 0);
549        assert_eq!(icmpv4.checksum(), 0x45da);
550    }
551
552    #[test]
553    fn ipv6_icmpv6() {
554        // Ethernet II + IPv6 + ICMPv6.
555        let packet = [
556            0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00, 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd,
557            0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
558            0x00, 0x00, 0x02, 0x00, 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0xfe, 0x80, 0x00, 0x00,
559            0x00, 0x00, 0x00, 0x00, 0x02, 0x60, 0x97, 0xff, 0xfe, 0x07, 0x69, 0xea, 0x87, 0x00,
560            0x68, 0xbd, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
561            0x02, 0x60, 0x97, 0xff, 0xfe, 0x07, 0x69, 0xea, 0x01, 0x01, 0x00, 0x00, 0x86, 0x05,
562            0x80, 0xda,
563        ];
564
565        // Parse the packet.
566        let parser = PacketParser::parse(&packet);
567
568        // Ensure the parser succeeds.
569        assert!(parser.is_ok());
570
571        // Unwrap the parser.
572        let unwrapped = parser.unwrap();
573
574        // Ensure the parser has the expected fields.
575        assert!(unwrapped.ethernet.is_some());
576        assert!(unwrapped.ipv6.is_some());
577        assert!(unwrapped.icmpv6.is_some());
578
579        // Ensure these headers are not present.
580        assert!(unwrapped.icmpv4.is_none());
581        assert!(unwrapped.arp.is_none());
582        assert!(unwrapped.tcp.is_none());
583        assert!(unwrapped.udp.is_none());
584
585        // Unwrap headers.
586        let ethernet = unwrapped.ethernet.unwrap();
587        let ipv6 = unwrapped.ipv6.unwrap();
588        let icmpv6 = unwrapped.icmpv6.unwrap();
589
590        // Ensure the fields are correct.
591        assert_eq!(ethernet.ethertype(), 34525);
592        assert_eq!(ipv6.next_header(), 58);
593        assert_eq!(icmpv6.icmp_type(), 135);
594        assert_eq!(icmpv6.icmp_code(), 0);
595    }
596
597    #[test]
598    fn ipv6_udp_payload() {
599        // Ethernet II + IPv6 + UDP + Payload.
600        let packet = [
601            0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00, 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd,
602            0x60, 0x00, 0x00, 0x00, 0x00, 0x14, 0x11, 0x03, 0x3f, 0xfe, 0x05, 0x07, 0x00, 0x00,
603            0x00, 0x01, 0x02, 0x00, 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe, 0x05, 0x01,
604            0x04, 0x10, 0x00, 0x00, 0x02, 0xc0, 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0xa0, 0x75,
605            0x82, 0xa1, 0x00, 0x14, 0x81, 0x13, 0x07, 0x03, 0x00, 0x00, 0xf9, 0xc8, 0xe7, 0x36,
606            0xef, 0x5d, 0x0a, 0x00,
607        ];
608
609        // Parse the packet.
610        let parser = PacketParser::parse(&packet);
611
612        // Ensure the parser succeeds.
613        assert!(parser.is_ok());
614
615        // Unwrap the parser.
616        let unwrapped = parser.unwrap();
617
618        // Ensure the parser has the expected fields.
619        assert!(unwrapped.ethernet.is_some());
620        assert!(unwrapped.ipv6.is_some());
621        assert!(unwrapped.udp.is_some());
622
623        // Ensure these headers are not present.
624        assert!(unwrapped.icmpv4.is_none());
625        assert!(unwrapped.icmpv6.is_none());
626        assert!(unwrapped.arp.is_none());
627        assert!(unwrapped.tcp.is_none());
628
629        // Unwrap UDP header.
630        let udp = unwrapped.udp.unwrap();
631
632        // Ensure payload is correct.
633        assert_eq!(
634            udp.payload(),
635            [0x07, 0x03, 0x00, 0x00, 0xf9, 0xc8, 0xe7, 0x36, 0xef, 0x5d, 0x0a, 0x00]
636        );
637    }
638
639    #[test]
640    fn ipv6_routing_extension_header() {
641        // Ethernet II + IPv6 + Routing extension header + TCP.
642        let packet = [
643            0x86, 0x93, 0x23, 0xd3, 0x37, 0x8e, 0x22, 0x1a, 0x95, 0xd6, 0x7a, 0x23, 0x86, 0xdd,
644            0x60, 0x0f, 0xbb, 0x74, 0x00, 0x88, 0x2b, 0x3f, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00,
645            0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02,
646            0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x06,
647            0x04, 0x02, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06,
648            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00,
649            0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02,
650            0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1f, 0x90,
651            0xa9, 0xa0, 0xba, 0x31, 0x1e, 0x8d, 0x02, 0x1b, 0x63, 0x8d, 0xa0, 0x12, 0x70, 0xf8,
652            0x8a, 0xf5, 0x00, 0x00, 0x02, 0x04, 0x07, 0x94, 0x04, 0x02, 0x08, 0x0a, 0x80, 0x1d,
653            0xa5, 0x22, 0x80, 0x1d, 0xa5, 0x22, 0x01, 0x03, 0x03, 0x07,
654        ];
655
656        // Parse the packet.
657        let parser = PacketParser::parse(&packet);
658
659        // Ensure the parser succeeds.
660        assert!(parser.is_ok());
661
662        // Unwrap the parser.
663        let unwrapped = parser.unwrap();
664
665        // Ensure the parser has the expected fields.
666        assert!(unwrapped.ethernet.is_some());
667        assert!(unwrapped.ipv6.is_some());
668        assert!(unwrapped.tcp.is_some());
669
670        // Ensure these headers are not present.
671        assert!(unwrapped.icmpv4.is_none());
672        assert!(unwrapped.icmpv6.is_none());
673        assert!(unwrapped.arp.is_none());
674        assert!(unwrapped.udp.is_none());
675
676        // Unwrap IPv6 header.
677        let ipv6 = unwrapped.ipv6.unwrap();
678
679        // Ensure extension headers are present.
680        assert!(ipv6.extension_headers.is_some());
681
682        // Unwrap extension headers.
683        let extension_headers = ipv6.extension_headers.unwrap();
684
685        // Ensure routing extension header is present.
686        assert!(extension_headers.routing.is_some());
687    }
688
689    #[test]
690    fn ipv6_hop_by_hop_options() {
691        // Ethernet II + IPv6 + Hop-by-Hop Options + TCP.
692        let packet = [
693            0x44, 0x2a, 0x60, 0xf6, 0x27, 0x8a, 0x00, 0x0c, 0x29, 0x30, 0x76, 0xb5, 0x86, 0xdd,
694            0x60, 0x00, 0x00, 0x04, 0x00, 0x1c, 0x00, 0x40, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
695            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00,
696            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x00,
697            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x0c, 0x00, 0x87, 0x00, 0x00, 0x52, 0x0c,
698            0x00, 0x00, 0x00, 0x00, 0x50, 0x02, 0x40, 0x38, 0xcb, 0x04, 0x00, 0x00,
699        ];
700
701        // Parse the packet.
702        let parser = PacketParser::parse(&packet);
703
704        // Ensure the parser succeeds.
705        assert!(parser.is_ok());
706
707        // Unwrap the parser.
708        let unwrapped = parser.unwrap();
709
710        // Ensure the parser has the expected fields.
711        assert!(unwrapped.ethernet.is_some());
712        assert!(unwrapped.ipv6.is_some());
713        assert!(unwrapped.tcp.is_some());
714
715        // Ensure these headers are not present.
716        assert!(unwrapped.icmpv4.is_none());
717        assert!(unwrapped.icmpv6.is_none());
718        assert!(unwrapped.arp.is_none());
719        assert!(unwrapped.udp.is_none());
720
721        // Unwrap IPv6 header.
722        let ipv6 = unwrapped.ipv6.unwrap();
723
724        // Ensure extension headers are present.
725        assert!(ipv6.extension_headers.is_some());
726
727        // Unwrap extension headers.
728        let extension_headers = ipv6.extension_headers.unwrap();
729
730        // Ensure hop-by-hop options extension header is present.
731        assert!(extension_headers.hop_by_hop.is_some());
732    }
733
734    #[test]
735    fn ipv6_destination_options() {
736        // Ethernet II + IPv6 + Destination Options + TCP.
737        let packet = [
738            0x44, 0x2a, 0x60, 0xf6, 0x27, 0x8a, 0x00, 0x0c, 0x29, 0x30, 0x76, 0xb5, 0x86, 0xdd,
739            0x60, 0x00, 0x00, 0x05, 0x00, 0x1c, 0x3c, 0x40, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
740            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00,
741            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x00,
742            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x0d, 0x00, 0x87, 0x00, 0x00, 0x52, 0x0d,
743            0x00, 0x00, 0x00, 0x00, 0x50, 0x02, 0x40, 0x38, 0xcb, 0x02, 0x00, 0x00,
744        ];
745
746        // Parse the packet.
747        let parser = PacketParser::parse(&packet);
748
749        // Ensure the parser succeeds.
750        assert!(parser.is_ok());
751
752        // Unwrap the parser.
753        let unwrapped = parser.unwrap();
754
755        // Ensure the parser has the expected fields.
756        assert!(unwrapped.ethernet.is_some());
757        assert!(unwrapped.ipv6.is_some());
758        assert!(unwrapped.tcp.is_some());
759
760        // Ensure these headers are not present.
761        assert!(unwrapped.icmpv4.is_none());
762        assert!(unwrapped.icmpv6.is_none());
763        assert!(unwrapped.arp.is_none());
764        assert!(unwrapped.udp.is_none());
765
766        // Unwrap IPv6 header.
767        let ipv6 = unwrapped.ipv6.unwrap();
768
769        // Ensure extension headers are present.
770        assert!(ipv6.extension_headers.is_some());
771
772        // Unwrap extension headers.
773        let extension_headers = ipv6.extension_headers.unwrap();
774
775        // Ensure hop-by-hop options extension header is present.
776        assert!(extension_headers.destination_1st.is_some());
777    }
778
779    #[test]
780    fn fragment_and_authentication_header() {
781        // IPv6 with fragment extension header and ICMPv6.
782        let pkt1 = [
783            0x70, 0x77, 0x81, 0xdd, 0xc3, 0x7c, 0x00, 0x26, 0x9e, 0x71, 0x9d, 0x33, 0x86, 0xdd,
784            0x60, 0x00, 0x00, 0x00, 0x00, 0x98, 0x2c, 0x9c, 0x26, 0x05, 0x60, 0x00, 0x23, 0xc0,
785            0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x20, 0x01, 0x41, 0xd0,
786            0x00, 0x08, 0xcc, 0xd8, 0x01, 0x37, 0x00, 0x74, 0x01, 0x87, 0x01, 0x01, 0x3a, 0x00,
787            0x00, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x80, 0x00, 0x0b, 0xaf, 0x15, 0x5e, 0x3a, 0xfb,
788            0x4c, 0x9a, 0x02, 0x5c, 0x81, 0x31, 0xcf, 0xf7, 0x5c, 0xed, 0x0c, 0x57, 0x8a, 0xd0,
789            0x7d, 0x40, 0x11, 0x01, 0x08, 0x3a, 0x61, 0x57, 0xea, 0x73, 0xc4, 0xa1, 0x8a, 0x2e,
790            0x23, 0x3b, 0xdc, 0x78, 0x80, 0xa1, 0x25, 0x54, 0xe3, 0x12, 0x90, 0x48, 0xa6, 0x02,
791            0xff, 0x3b, 0x2f, 0xd4, 0x0a, 0x7a, 0x86, 0xf2, 0x81, 0x2d, 0x36, 0x16, 0x9e, 0x20,
792            0x34, 0xf5, 0x0d, 0x63, 0xef, 0xc7, 0xd5, 0x2a, 0x8f, 0x43, 0x6c, 0x64, 0xb7, 0x69,
793            0x0f, 0x6f, 0x86, 0xca, 0x55, 0x57, 0xd7, 0x71, 0x68, 0x1e, 0xac, 0x9c, 0x68, 0x6f,
794            0x93, 0x89, 0xe9, 0x1d, 0x34, 0xbe, 0xdc, 0x50, 0x19, 0x6d, 0x00, 0x53, 0x2d, 0x6a,
795            0xcb, 0x4a, 0x57, 0x61, 0xcf, 0x63, 0x99, 0x19, 0x42, 0x45, 0x46, 0xc7, 0x30, 0x57,
796            0x95, 0x1d, 0x67, 0x55, 0x0a, 0xa7, 0xb0, 0x00, 0x4a, 0xc1, 0x42, 0x43, 0x0e, 0xe4,
797            0x2e, 0x51, 0x05, 0xe2, 0x8c, 0x42, 0xa7, 0xae, 0x4f, 0x1a,
798        ];
799
800        // Parse the packet.
801        let prs1 = PacketParser::parse(&pkt1);
802
803        // Ensure the parser succeeds.
804        assert!(prs1.is_ok());
805
806        // IPv6 with Authentication Header (AH) extension header.
807        let pkt2 = [
808            0x00, 0x1e, 0x7a, 0x79, 0x3f, 0x10, 0x00, 0x15, 0x62, 0x6a, 0xfe, 0xf1, 0x86, 0xdd,
809            0x6e, 0x00, 0x00, 0x00, 0x00, 0x44, 0x33, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
810            0x00, 0x00, 0x02, 0x15, 0x62, 0xff, 0xfe, 0x6a, 0xfe, 0xf1, 0xfe, 0x80, 0x00, 0x00,
811            0x00, 0x00, 0x00, 0x00, 0x02, 0x1e, 0x7a, 0xff, 0xfe, 0x79, 0x3f, 0x10, 0x59, 0x04,
812            0x00, 0x00, 0x00, 0x00, 0x09, 0x07, 0x00, 0x00, 0x00, 0xff, 0xd1, 0x54, 0xcc, 0xe6,
813            0x68, 0xcb, 0xcf, 0xfa, 0xfe, 0xbb, 0x93, 0xb8, 0x03, 0x01, 0x00, 0x2c, 0xc0, 0xa8,
814            0xff, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x22, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
815            0x01, 0x00, 0x00, 0x13, 0x00, 0x0a, 0x00, 0x28, 0xc0, 0xa8, 0xff, 0x0f, 0xc0, 0xa8,
816            0xff, 0x0e, 0xc0, 0xa8, 0xff, 0x0b, 0xc0, 0xa8, 0xff, 0x0f, 0x7f, 0xf4, 0xe0, 0xb7,
817        ];
818
819        // Parse the packet.
820        let prs2 = PacketParser::parse(&pkt2);
821
822        // Ensure the parser succeeds.
823        assert!(prs2.is_ok());
824    }
825
826    #[test]
827    fn extension_headers_chained() {
828        // IPv6 with extension headers chained together.
829        // Ethernet II + IPv6 + Hop-by-Hop + Destination + NoNextHeader.
830        let packet = [
831            // Ethernet II.
832            0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E, // Destination MAC Address.
833            0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5F, // Source MAC Address.
834            0x86, 0xDD, // Type: IPv6 (0x86DD).
835            // IPv6 Header.
836            0x60, 0x00, 0x00, 0x00, // Version, Traffic Class, Flow Label.
837            0x00, 0x20, // Payload Length (32 bytes).
838            0x00, // Next Header: Hop-by-Hop Options (0x00).
839            0x40, // Hop Limit.
840            0x20, 0x01, 0x0D, 0xB8, 0x85, 0xA3, 0x00, 0x00, // Source Address.
841            0x00, 0x00, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x34, // Source Address.
842            0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Destination Address.
843            0x02, 0x02, 0xB3, 0xFF, 0xFE, 0x1E, 0x83, 0x29, // Destination Address.
844            // Hop-by-Hop Options Header.
845            0x3C, // Next Header: Destination Options (0x3C).
846            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Options and padding.
847            // Destination Options Header.
848            0x3B, // Next Header: No Next Header (0x3B).
849            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Options and padding.
850        ];
851
852        // Parse the packet.
853        let parser = PacketParser::parse(&packet);
854
855        // Ensure the parser succeeds.
856        assert!(parser.is_ok());
857
858        // Unwrap the parser.
859        let unwrapped = parser.unwrap();
860
861        // Ensure the parser has the expected fields.
862        assert!(unwrapped.ethernet.is_some());
863        assert!(unwrapped.ipv6.is_some());
864
865        // Ensure these headers are not present.
866        assert!(unwrapped.icmpv4.is_none());
867        assert!(unwrapped.icmpv6.is_none());
868        assert!(unwrapped.arp.is_none());
869        assert!(unwrapped.udp.is_none());
870        assert!(unwrapped.tcp.is_none());
871
872        // Unwrap IPv6 header.
873        let ipv6 = unwrapped.ipv6.unwrap();
874
875        // Ensure extension headers are present.
876        assert!(ipv6.extension_headers.is_some());
877
878        // Unwrap extension headers.
879        let extension_headers = ipv6.extension_headers.unwrap();
880
881        // Ensure hop-by-hop options extension header is present.
882        assert!(extension_headers.hop_by_hop.is_some());
883
884        // Ensure destination options extension header is present.
885        assert!(extension_headers.destination_1st.is_some());
886    }
887
888    #[test]
889    pub fn ipv6_in_ipv6_with_extension_header() {
890        // Ethernet II + IPv6 + Routing Header (Segment Routing) + IPv6 + TCP.
891        let packet = [
892            0x86, 0x93, 0x23, 0xd3, 0x37, 0x8e, 0x22, 0x1a, 0x95, 0xd6, 0x7a, 0x23, 0x86, 0xdd,
893            0x60, 0x0f, 0xbb, 0x74, 0x00, 0x88, 0x2b, 0x3f, 0xfc, 0x00, 0x00, 0x42, 0x00, 0x00,
894            0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfc, 0x00, 0x00, 0x02,
895            0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x29, 0x06,
896            0x04, 0x02, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06,
897            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00,
898            0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02,
899            0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x60, 0x0f,
900            0xbb, 0x74, 0x00, 0x28, 0x06, 0x40, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
901            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00,
902            0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1f, 0x90, 0xa9, 0xa0,
903            0xba, 0x31, 0x1e, 0x8d, 0x02, 0x1b, 0x63, 0x8d, 0xa0, 0x12, 0x70, 0xf8, 0x8a, 0xf5,
904            0x00, 0x00, 0x02, 0x04, 0x07, 0x94, 0x04, 0x02, 0x08, 0x0a, 0x80, 0x1d, 0xa5, 0x22,
905            0x80, 0x1d, 0xa5, 0x22, 0x01, 0x03, 0x03, 0x07,
906        ];
907
908        // Parse the packet.
909        let parser = PacketParser::parse(&packet);
910
911        // Ensure the parser succeeds.
912        assert!(parser.is_ok());
913    }
914
915    #[test]
916    pub fn ipv6_in_ipv4() {
917        // Ethernet II + IPv4 + IPv6 + ICMPv6.
918        let packet = [
919            0xc2, 0x01, 0x42, 0x02, 0x00, 0x00, 0xc2, 0x00, 0x42, 0x02, 0x00, 0x00, 0x08, 0x00,
920            0x45, 0x00, 0x00, 0x78, 0x00, 0x09, 0x00, 0x00, 0xff, 0x29, 0xa7, 0x51, 0x0a, 0x00,
921            0x00, 0x01, 0x0a, 0x00, 0x00, 0x02, 0x60, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x3a, 0x40,
922            0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
923            0x00, 0x01, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
924            0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x7e, 0x8f, 0x18, 0xdc, 0x00, 0x00, 0x00, 0x01,
925            0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
926            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
927            0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
928            0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
929        ];
930
931        // Parse the packet.
932        let parser = PacketParser::parse(&packet);
933
934        // Ensure the parser succeeds.
935        assert!(parser.is_ok());
936    }
937
938    #[test]
939    fn ipv4_in_ipv4() {
940        // Ethernet II + IPv4 + IPv4 + ICMP.
941        let packet = [
942            0xc2, 0x01, 0x57, 0x75, 0x00, 0x00, 0xc2, 0x00, 0x57, 0x75, 0x00, 0x00, 0x08, 0x00,
943            0x45, 0x00, 0x00, 0x78, 0x00, 0x14, 0x00, 0x00, 0xff, 0x04, 0xa7, 0x6b, 0x0a, 0x00,
944            0x00, 0x01, 0x0a, 0x00, 0x00, 0x02, 0x45, 0x00, 0x00, 0x64, 0x00, 0x14, 0x00, 0x00,
945            0xff, 0x01, 0xb5, 0x7f, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x08, 0x00,
946            0x43, 0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x3b, 0x38,
947            0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
948            0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
949            0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
950            0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
951            0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
952        ];
953
954        // Parse the packet.
955        let parser = PacketParser::parse(&packet);
956
957        // Ensure the parser succeeds.
958        assert!(parser.is_ok());
959    }
960}