1 use super::*;
2 use crate::auth::*;
3 use crate::relay::relay_static::*;
4 use crate::server::{config::*, *};
5
6 use std::net::IpAddr;
7 use tokio::net::UdpSocket;
8 use tokio::time::Duration;
9
10 use util::vnet::net::*;
11
create_listening_test_client(rto_in_ms: u16) -> Result<Client>12 async fn create_listening_test_client(rto_in_ms: u16) -> Result<Client> {
13 let conn = UdpSocket::bind("0.0.0.0:0").await?;
14
15 let c = Client::new(ClientConfig {
16 stun_serv_addr: String::new(),
17 turn_serv_addr: String::new(),
18 username: String::new(),
19 password: String::new(),
20 realm: String::new(),
21 software: "TEST SOFTWARE".to_owned(),
22 rto_in_ms,
23 conn: Arc::new(conn),
24 vnet: None,
25 })
26 .await?;
27
28 c.listen().await?;
29
30 Ok(c)
31 }
32
create_listening_test_client_with_stun_serv() -> Result<Client>33 async fn create_listening_test_client_with_stun_serv() -> Result<Client> {
34 let conn = UdpSocket::bind("0.0.0.0:0").await?;
35
36 let c = Client::new(ClientConfig {
37 stun_serv_addr: "stun1.l.google.com:19302".to_owned(),
38 turn_serv_addr: String::new(),
39 username: String::new(),
40 password: String::new(),
41 realm: String::new(),
42 software: "TEST SOFTWARE".to_owned(),
43 rto_in_ms: 0,
44 conn: Arc::new(conn),
45 vnet: None,
46 })
47 .await?;
48
49 c.listen().await?;
50
51 Ok(c)
52 }
53
54 #[tokio::test]
test_client_with_stun_send_binding_request() -> Result<()>55 async fn test_client_with_stun_send_binding_request() -> Result<()> {
56 //env_logger::init();
57
58 let c = create_listening_test_client_with_stun_serv().await?;
59
60 let resp = c.send_binding_request().await?;
61 log::debug!("mapped-addr: {}", resp);
62 {
63 let ci = c.client_internal.lock().await;
64 let tm = ci.tr_map.lock().await;
65 assert_eq!(0, tm.size(), "should be no transaction left");
66 }
67
68 c.close().await?;
69
70 Ok(())
71 }
72
73 #[tokio::test]
test_client_with_stun_send_binding_request_to_parallel() -> Result<()>74 async fn test_client_with_stun_send_binding_request_to_parallel() -> Result<()> {
75 env_logger::init();
76
77 let c1 = create_listening_test_client(0).await?;
78 let c2 = c1.clone();
79
80 let (stared_tx, mut started_rx) = mpsc::channel::<()>(1);
81 let (finished_tx, mut finished_rx) = mpsc::channel::<()>(1);
82
83 let to = lookup_host(true, "stun1.l.google.com:19302").await?;
84
85 tokio::spawn(async move {
86 drop(stared_tx);
87 if let Ok(resp) = c2.send_binding_request_to(&to.to_string()).await {
88 log::debug!("mapped-addr: {}", resp);
89 }
90 drop(finished_tx);
91 });
92
93 let _ = started_rx.recv().await;
94
95 let resp = c1.send_binding_request_to(&to.to_string()).await?;
96 log::debug!("mapped-addr: {}", resp);
97
98 let _ = finished_rx.recv().await;
99
100 c1.close().await?;
101
102 Ok(())
103 }
104
105 #[tokio::test]
test_client_with_stun_send_binding_request_to_timeout() -> Result<()>106 async fn test_client_with_stun_send_binding_request_to_timeout() -> Result<()> {
107 //env_logger::init();
108
109 let c = create_listening_test_client(10).await?;
110
111 let to = lookup_host(true, "127.0.0.1:9").await?;
112
113 let result = c.send_binding_request_to(&to.to_string()).await;
114 assert!(result.is_err(), "expected error, but got ok");
115
116 c.close().await?;
117
118 Ok(())
119 }
120
121 struct TestAuthHandler;
122 impl AuthHandler for TestAuthHandler {
auth_handle(&self, username: &str, realm: &str, _src_addr: SocketAddr) -> Result<Vec<u8>>123 fn auth_handle(&self, username: &str, realm: &str, _src_addr: SocketAddr) -> Result<Vec<u8>> {
124 Ok(generate_auth_key(username, realm, "pass"))
125 }
126 }
127
128 // Create an allocation, and then delete all nonces
129 // The subsequent Write on the allocation will cause a CreatePermission
130 // which will be forced to handle a stale nonce response
131 #[tokio::test]
test_client_nonce_expiration() -> Result<()>132 async fn test_client_nonce_expiration() -> Result<()> {
133 // env_logger::init();
134
135 // here, it should use static port, like "0.0.0.0:3478",
136 // but, due to different test environment, let's fake it by using "0.0.0.0:0"
137 // to auto assign a "static" port
138 let conn = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
139 let server_port = conn.local_addr()?.port();
140
141 let server = Server::new(ServerConfig {
142 conn_configs: vec![ConnConfig {
143 conn,
144 relay_addr_generator: Box::new(RelayAddressGeneratorStatic {
145 relay_address: IpAddr::from_str("127.0.0.1")?,
146 address: "0.0.0.0".to_owned(),
147 net: Arc::new(Net::new(None)),
148 }),
149 }],
150 realm: "webrtc.rs".to_owned(),
151 auth_handler: Arc::new(TestAuthHandler {}),
152 channel_bind_timeout: Duration::from_secs(0),
153 alloc_close_notify: None,
154 })
155 .await?;
156
157 let conn = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
158
159 let client = Client::new(ClientConfig {
160 stun_serv_addr: format!("127.0.0.1:{server_port}"),
161 turn_serv_addr: format!("127.0.0.1:{server_port}"),
162 username: "foo".to_owned(),
163 password: "pass".to_owned(),
164 realm: String::new(),
165 software: String::new(),
166 rto_in_ms: 0,
167 conn,
168 vnet: None,
169 })
170 .await?;
171
172 client.listen().await?;
173
174 let allocation = client.allocate().await?;
175
176 {
177 let mut nonces = server.nonces.lock().await;
178 nonces.clear();
179 }
180
181 allocation
182 .send_to(&[0x00], SocketAddr::from_str("127.0.0.1:8080")?)
183 .await?;
184
185 // Shutdown
186 client.close().await?;
187 server.close().await?;
188
189 Ok(())
190 }
191