1 use super::*;
2
3 use crate::{allocation::*, error::Result};
4
5 use tokio::net::UdpSocket;
6
7 use std::net::Ipv4Addr;
8 use stun::{attributes::ATTR_USERNAME, textattrs::TextAttribute};
9
create_channel_bind(lifetime: Duration) -> Result<Allocation>10 async fn create_channel_bind(lifetime: Duration) -> Result<Allocation> {
11 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
12 let relay_socket = Arc::clone(&turn_socket);
13 let relay_addr = relay_socket.local_addr()?;
14 let a = Allocation::new(
15 turn_socket,
16 relay_socket,
17 relay_addr,
18 FiveTuple::default(),
19 TextAttribute::new(ATTR_USERNAME, "user".into()),
20 None,
21 );
22
23 let addr = SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 0);
24 let c = ChannelBind::new(ChannelNumber(MIN_CHANNEL_NUMBER), addr);
25
26 a.add_channel_bind(c, lifetime).await?;
27
28 Ok(a)
29 }
30
31 #[tokio::test]
test_channel_bind() -> Result<()>32 async fn test_channel_bind() -> Result<()> {
33 let a = create_channel_bind(Duration::from_millis(20)).await?;
34
35 let result = a.get_channel_addr(&ChannelNumber(MIN_CHANNEL_NUMBER)).await;
36 if let Some(addr) = result {
37 assert_eq!(addr.ip().to_string(), "0.0.0.0");
38 } else {
39 panic!("expected some, but got none");
40 }
41
42 Ok(())
43 }
44
test_channel_bind_start() -> Result<()>45 async fn test_channel_bind_start() -> Result<()> {
46 let a = create_channel_bind(Duration::from_millis(20)).await?;
47 tokio::time::sleep(Duration::from_millis(30)).await;
48
49 assert!(a
50 .get_channel_addr(&ChannelNumber(MIN_CHANNEL_NUMBER))
51 .await
52 .is_none());
53
54 Ok(())
55 }
56
test_channel_bind_reset() -> Result<()>57 async fn test_channel_bind_reset() -> Result<()> {
58 let a = create_channel_bind(Duration::from_millis(30)).await?;
59
60 tokio::time::sleep(Duration::from_millis(20)).await;
61 {
62 let channel_bindings = a.channel_bindings.lock().await;
63 if let Some(c) = channel_bindings.get(&ChannelNumber(MIN_CHANNEL_NUMBER)) {
64 c.refresh(Duration::from_millis(30)).await;
65 }
66 }
67 tokio::time::sleep(Duration::from_millis(20)).await;
68
69 assert!(a
70 .get_channel_addr(&ChannelNumber(MIN_CHANNEL_NUMBER))
71 .await
72 .is_some());
73
74 Ok(())
75 }
76