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
use crate::*;

/// In case a route header is present it is also possible
/// to attach a "final destination" header.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv6RoutingExtensions {
    pub routing: Ipv6RawExtHeader,
    pub final_destination_options: Option<Ipv6RawExtHeader>,
}

impl Ipv6RoutingExtensions {
    /// Minimum length required for routing extension headers in bytes/octets.
    pub const MIN_LEN: usize = Ipv6RawExtHeader::MAX_LEN;

    /// Maximum summed up length of all extension headers in bytes/octets.
    pub const MAX_LEN: usize = Ipv6RawExtHeader::MAX_LEN * 2;

    /// Return the length of the headers in bytes.
    pub fn header_len(&self) -> usize {
        self.routing.header_len()
            + self
                .final_destination_options
                .as_ref()
                .map(|h| h.header_len())
                .unwrap_or(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_gens::ipv6_raw_ext_any;
    use proptest::prelude::*;

    #[test]
    fn debug() {
        use alloc::format;

        let a: Ipv6RoutingExtensions = Ipv6RoutingExtensions {
            routing: Ipv6RawExtHeader::new_raw(0.into(), &[0; 6]).unwrap(),
            final_destination_options: None,
        };
        assert_eq!(
            &format!(
                "Ipv6RoutingExtensions {{ routing: {:?}, final_destination_options: {:?} }}",
                a.routing, a.final_destination_options,
            ),
            &format!("{:?}", a)
        );
    }

    #[test]
    fn clone_eq() {
        let a: Ipv6RoutingExtensions = Ipv6RoutingExtensions {
            routing: Ipv6RawExtHeader::new_raw(0.into(), &[0; 6]).unwrap(),
            final_destination_options: None,
        };
        assert_eq!(a, a.clone());
    }

    proptest! {
        #[test]
        fn header_len(
            routing in ipv6_raw_ext_any(),
            final_destination_options in ipv6_raw_ext_any()
        ) {
            // without final dest options
            assert_eq!(
                Ipv6RoutingExtensions{
                    routing: routing.clone(),
                    final_destination_options: None,
                }.header_len(),
                routing.header_len()
            );

            // with final dest options
            assert_eq!(
                Ipv6RoutingExtensions{
                    routing: routing.clone(),
                    final_destination_options: Some(final_destination_options.clone()),
                }.header_len(),
                routing.header_len() + final_destination_options.header_len()
            );
        }
    }
}