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
use crate::error::PfcpError;
use crate::ie::{Ie, IeType};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimeOfFirstPacket {
    pub timestamp: u32,
}

impl TimeOfFirstPacket {
    pub fn new(timestamp: u32) -> Self {
        Self { timestamp }
    }

    pub fn marshal_len(&self) -> usize {
        4 // u32 for 3GPP NTP timestamp
    }

    pub fn marshal(&self) -> Vec<u8> {
        self.timestamp.to_be_bytes().to_vec()
    }

    pub fn marshal_to(&self, buf: &mut Vec<u8>) {
        buf.extend_from_slice(&self.timestamp.to_be_bytes());
    }

    pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
        if data.len() < 4 {
            return Err(PfcpError::invalid_length(
                "Time Of First Packet",
                IeType::TimeOfFirstPacket,
                4,
                data.len(),
            ));
        }

        let bytes: [u8; 4] = data[0..4].try_into().unwrap();
        let timestamp = u32::from_be_bytes(bytes);

        Ok(Self { timestamp })
    }

    pub fn to_ie(&self) -> Ie {
        Ie::new(IeType::TimeOfFirstPacket, self.marshal())
    }
}

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

    #[test]
    fn test_time_of_first_packet_new() {
        let timestamp = 0x12345678;
        let tofp = TimeOfFirstPacket::new(timestamp);
        assert_eq!(tofp.timestamp, timestamp);
    }

    #[test]
    fn test_time_of_first_packet_marshal_unmarshal() {
        let timestamp = 0xABCDEF01;
        let tofp = TimeOfFirstPacket::new(timestamp);

        let data = tofp.marshal();
        assert_eq!(data.len(), 4);

        let unmarshaled = TimeOfFirstPacket::unmarshal(&data).unwrap();
        assert_eq!(tofp, unmarshaled);
        assert_eq!(unmarshaled.timestamp, timestamp);
    }

    #[test]
    fn test_time_of_first_packet_marshal_zero() {
        let tofp = TimeOfFirstPacket::new(0);

        let data = tofp.marshal();
        let unmarshaled = TimeOfFirstPacket::unmarshal(&data).unwrap();

        assert_eq!(tofp, unmarshaled);
        assert_eq!(unmarshaled.timestamp, 0);
    }

    #[test]
    fn test_time_of_first_packet_marshal_max_value() {
        let tofp = TimeOfFirstPacket::new(u32::MAX);

        let data = tofp.marshal();
        let unmarshaled = TimeOfFirstPacket::unmarshal(&data).unwrap();

        assert_eq!(tofp, unmarshaled);
        assert_eq!(unmarshaled.timestamp, u32::MAX);
    }

    #[test]
    fn test_time_of_first_packet_to_ie() {
        let timestamp = 0x87654321;
        let tofp = TimeOfFirstPacket::new(timestamp);

        let ie = tofp.to_ie();
        assert_eq!(ie.ie_type, IeType::TimeOfFirstPacket);
    }

    #[test]
    fn test_time_of_first_packet_unmarshal_insufficient_data() {
        let data = [0x00, 0x01, 0x02]; // Only 3 bytes
        let result = TimeOfFirstPacket::unmarshal(&data);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, PfcpError::InvalidLength { .. }));
    }

    #[test]
    fn test_time_of_first_packet_unmarshal_empty_data() {
        let data = [];
        let result = TimeOfFirstPacket::unmarshal(&data);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, PfcpError::InvalidLength { .. }));
    }

    #[test]
    fn test_time_of_first_packet_marshal_len() {
        let tofp = TimeOfFirstPacket::new(42);
        assert_eq!(tofp.marshal_len(), 4);
    }

    #[test]
    fn test_time_of_first_packet_round_trip_various_values() {
        let test_values = [
            0,
            1,
            0x12345678,
            0xABCDEF01,
            0x87654321,
            0xFFFFFFFF,
            u32::MAX,
        ];

        for &value in &test_values {
            let tofp = TimeOfFirstPacket::new(value);
            let data = tofp.marshal();
            let unmarshaled = TimeOfFirstPacket::unmarshal(&data).unwrap();
            assert_eq!(tofp, unmarshaled);
        }
    }

    #[test]
    fn test_time_of_first_packet_byte_order() {
        let tofp = TimeOfFirstPacket::new(0x12345678);
        let data = tofp.marshal();

        // Verify big-endian byte order
        assert_eq!(data, vec![0x12, 0x34, 0x56, 0x78]);
    }
}