stun_rs/attributes/stun/
mapped_address.rs

1use crate::attributes::address_port::{address_port_attribute, address_port_tests};
2use crate::{Decode, Encode};
3
4const MAPPED_ADDRESS: u16 = 0x0001;
5
6address_port_attribute!(
7    /// The MAPPED-ADDRESS attribute indicates a reflexive transport
8    /// address of the client.
9    ///
10    /// # Examples
11    ///```rust
12    /// # use std::net::{IpAddr, Ipv4Addr, SocketAddr};
13    /// # use stun_rs::attributes::stun::MappedAddress;
14    /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
15    /// let attr = MappedAddress::from(socket);
16    ///
17    /// assert_eq!(attr.socket_address().port(), 8080);
18    /// assert!(attr.socket_address().is_ipv4());
19    ///```
20    MappedAddress,
21    MAPPED_ADDRESS
22);
23
24address_port_tests!(MappedAddress, super);
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29    use crate::StunAttribute;
30    use std::net::{IpAddr, Ipv4Addr, SocketAddr};
31
32    #[test]
33    fn mapped_address_stunt_attribute() {
34        let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
35        let attr = StunAttribute::MappedAddress(MappedAddress::from(socket));
36        assert!(attr.is_mapped_address());
37        assert!(attr.as_mapped_address().is_ok());
38        assert!(attr.as_error_code().is_err());
39
40        assert!(attr.attribute_type().is_comprehension_required());
41        assert!(!attr.attribute_type().is_comprehension_optional());
42
43        let dbg_fmt = format!("{:?}", attr);
44        assert_eq!("MappedAddress(MappedAddress(127.0.0.1:8080))", dbg_fmt);
45    }
46}