stun_rs/attributes/stun/
alternate_server.rs

1use crate::attributes::address_port::{address_port_attribute, address_port_tests};
2use crate::{Decode, Encode};
3
4const ALTERNATE_SERVER: u16 = 0x8023;
5
6address_port_attribute!(
7    /// The alternate server represents an alternate transport address
8    /// identifying a different STUN server that the STUN client should try.
9    ///
10    /// # Examples
11    ///```rust
12    /// # use std::net::{IpAddr, Ipv4Addr, SocketAddr};
13    /// # use stun_rs::attributes::stun::AlternateServer;
14    /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
15    /// let attr = AlternateServer::from(socket);
16    ///
17    /// assert_eq!(attr.socket_address().port(), 8080);
18    /// assert!(attr.socket_address().is_ipv4());
19    ///```
20    AlternateServer,
21    ALTERNATE_SERVER
22);
23
24address_port_tests!(AlternateServer, 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 alternate_server_stunt_attribute() {
34        let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
35        let attr = StunAttribute::AlternateServer(AlternateServer::from(socket));
36        assert!(attr.is_alternate_server());
37        assert!(attr.as_alternate_server().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!("AlternateServer(AlternateServer(127.0.0.1:8080))", dbg_fmt);
45    }
46}