use byteorder::{BigEndian, ByteOrder};
use stun_types::{attribute::*, message::StunParseError};
#[derive(Debug, Clone)]
pub struct ConnectionId {
id: u32,
}
impl AttributeStaticType for ConnectionId {
const TYPE: AttributeType = AttributeType::new(0x002a);
}
impl Attribute for ConnectionId {
fn get_type(&self) -> AttributeType {
Self::TYPE
}
fn length(&self) -> u16 {
4
}
}
impl AttributeWrite for ConnectionId {
fn to_raw(&self) -> RawAttribute<'_> {
let mut data = [0; 4];
BigEndian::write_u32(&mut data, self.id);
RawAttribute::new(self.get_type(), &data).into_owned()
}
fn write_into_unchecked(&self, dest: &mut [u8]) {
self.write_header_unchecked(dest);
BigEndian::write_u32(&mut dest[4..8], self.id);
}
}
impl AttributeFromRaw<'_> for ConnectionId {
fn from_raw_ref(raw: &RawAttribute) -> Result<Self, StunParseError>
where
Self: Sized,
{
Self::try_from(raw)
}
}
impl TryFrom<&RawAttribute<'_>> for ConnectionId {
type Error = StunParseError;
fn try_from(raw: &RawAttribute) -> Result<Self, Self::Error> {
raw.check_type_and_len(Self::TYPE, 4..=4)?;
Ok(Self {
id: BigEndian::read_u32(&raw.value[..4]),
})
}
}
impl ConnectionId {
pub fn new(id: u32) -> Self {
Self { id }
}
pub fn id(&self) -> u32 {
self.id
}
}
impl core::fmt::Display for ConnectionId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}: {:x?}", self.get_type(), self.id())
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;
use byteorder::{BigEndian, ByteOrder};
use tracing::trace;
#[test]
fn requested_transport() {
let _log = crate::tests::test_init_log();
let trans = ConnectionId::new(17);
assert_eq!(trans.get_type(), ConnectionId::TYPE);
assert_eq!(trans.id(), 17);
}
#[test]
fn requested_transport_raw() {
let _log = crate::tests::test_init_log();
let trans = ConnectionId::new(17);
assert_eq!(trans.get_type(), ConnectionId::TYPE);
assert_eq!(trans.id(), 17);
let raw: RawAttribute = trans.to_raw();
trace!("raw: {raw:?}");
assert_eq!(raw.get_type(), ConnectionId::TYPE);
let trans2 = ConnectionId::try_from(&raw).unwrap();
assert_eq!(trans2.get_type(), ConnectionId::TYPE);
assert_eq!(trans2.id(), 17);
}
#[test]
fn requested_transport_raw_wrong_type() {
let _log = crate::tests::test_init_log();
let trans = ConnectionId::new(17);
assert_eq!(trans.get_type(), ConnectionId::TYPE);
assert_eq!(trans.id(), 17);
let raw: RawAttribute = trans.to_raw();
let mut data: Vec<_> = raw.into();
BigEndian::write_u16(&mut data[0..2], 0);
assert!(matches!(
ConnectionId::try_from(&RawAttribute::from_bytes(data.as_ref()).unwrap()),
Err(StunParseError::WrongAttributeImplementation)
));
}
#[test]
fn requested_transport_write_into() {
let _log = crate::tests::test_init_log();
let trans = ConnectionId::new(17);
assert_eq!(trans.get_type(), ConnectionId::TYPE);
assert_eq!(trans.id(), 17);
let mut data = [0; 8];
trans.write_into(&mut data).unwrap();
let raw = RawAttribute::from_bytes(&data).unwrap();
let trans2 = ConnectionId::from_raw_ref(&raw).unwrap();
assert_eq!(trans.id(), trans2.id());
}
#[test]
#[should_panic = "out of range"]
fn requested_transport_write_into_unchecked() {
let _log = crate::tests::test_init_log();
let trans = ConnectionId::new(17);
assert_eq!(trans.get_type(), ConnectionId::TYPE);
assert_eq!(trans.id(), 17);
let mut data = [0; 7];
trans.write_into_unchecked(&mut data);
}
}