1 use super::*;
2
3 use crate::proto::lifetime::DEFAULT_LIFETIME;
4 use std::str::FromStr;
5 use stun::{attributes::ATTR_USERNAME, textattrs::TextAttribute};
6 use tokio::net::UdpSocket;
7
8 #[tokio::test]
test_has_permission() -> Result<()>9 async fn test_has_permission() -> Result<()> {
10 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
11 let relay_socket = Arc::clone(&turn_socket);
12 let relay_addr = relay_socket.local_addr()?;
13 let a = Allocation::new(
14 turn_socket,
15 relay_socket,
16 relay_addr,
17 FiveTuple::default(),
18 TextAttribute::new(ATTR_USERNAME, "user".into()),
19 None,
20 );
21
22 let addr1 = SocketAddr::from_str("127.0.0.1:3478")?;
23 let addr2 = SocketAddr::from_str("127.0.0.1:3479")?;
24 let addr3 = SocketAddr::from_str("127.0.0.2:3478")?;
25
26 let p1 = Permission::new(addr1);
27 let p2 = Permission::new(addr2);
28 let p3 = Permission::new(addr3);
29
30 a.add_permission(p1).await;
31 a.add_permission(p2).await;
32 a.add_permission(p3).await;
33
34 let found_p1 = a.has_permission(&addr1).await;
35 assert!(found_p1, "Should keep the first one.");
36
37 let found_p2 = a.has_permission(&addr2).await;
38 assert!(found_p2, "Second one should be ignored.");
39
40 let found_p3 = a.has_permission(&addr3).await;
41 assert!(found_p3, "Permission with another IP should be found");
42
43 Ok(())
44 }
45
46 #[tokio::test]
test_add_permission() -> Result<()>47 async fn test_add_permission() -> Result<()> {
48 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
49 let relay_socket = Arc::clone(&turn_socket);
50 let relay_addr = relay_socket.local_addr()?;
51 let a = Allocation::new(
52 turn_socket,
53 relay_socket,
54 relay_addr,
55 FiveTuple::default(),
56 TextAttribute::new(ATTR_USERNAME, "user".into()),
57 None,
58 );
59
60 let addr = SocketAddr::from_str("127.0.0.1:3478")?;
61 let p = Permission::new(addr);
62 a.add_permission(p).await;
63
64 let found_p = a.has_permission(&addr).await;
65 assert!(found_p, "Should keep the first one.");
66
67 Ok(())
68 }
69
70 #[tokio::test]
test_remove_permission() -> Result<()>71 async fn test_remove_permission() -> Result<()> {
72 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
73 let relay_socket = Arc::clone(&turn_socket);
74 let relay_addr = relay_socket.local_addr()?;
75 let a = Allocation::new(
76 turn_socket,
77 relay_socket,
78 relay_addr,
79 FiveTuple::default(),
80 TextAttribute::new(ATTR_USERNAME, "user".into()),
81 None,
82 );
83
84 let addr = SocketAddr::from_str("127.0.0.1:3478")?;
85
86 let p = Permission::new(addr);
87 a.add_permission(p).await;
88
89 let found_p = a.has_permission(&addr).await;
90 assert!(found_p, "Should keep the first one.");
91
92 a.remove_permission(&addr).await;
93
94 let found_permission = a.has_permission(&addr).await;
95 assert!(
96 !found_permission,
97 "Got permission should be nil after removed."
98 );
99
100 Ok(())
101 }
102
103 #[tokio::test]
test_add_channel_bind() -> Result<()>104 async fn test_add_channel_bind() -> Result<()> {
105 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
106 let relay_socket = Arc::clone(&turn_socket);
107 let relay_addr = relay_socket.local_addr()?;
108 let a = Allocation::new(
109 turn_socket,
110 relay_socket,
111 relay_addr,
112 FiveTuple::default(),
113 TextAttribute::new(ATTR_USERNAME, "user".into()),
114 None,
115 );
116
117 let addr = SocketAddr::from_str("127.0.0.1:3478")?;
118 let c = ChannelBind::new(ChannelNumber(MIN_CHANNEL_NUMBER), addr);
119
120 a.add_channel_bind(c, DEFAULT_LIFETIME).await?;
121
122 let c2 = ChannelBind::new(ChannelNumber(MIN_CHANNEL_NUMBER + 1), addr);
123 let result = a.add_channel_bind(c2, DEFAULT_LIFETIME).await;
124 assert!(
125 result.is_err(),
126 "should failed with conflicted peer address"
127 );
128
129 let addr2 = SocketAddr::from_str("127.0.0.1:3479")?;
130 let c3 = ChannelBind::new(ChannelNumber(MIN_CHANNEL_NUMBER), addr2);
131 let result = a.add_channel_bind(c3, DEFAULT_LIFETIME).await;
132 assert!(result.is_err(), "should fail with conflicted number.");
133
134 Ok(())
135 }
136
137 #[tokio::test]
test_get_channel_by_number() -> Result<()>138 async fn test_get_channel_by_number() -> Result<()> {
139 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
140 let relay_socket = Arc::clone(&turn_socket);
141 let relay_addr = relay_socket.local_addr()?;
142 let a = Allocation::new(
143 turn_socket,
144 relay_socket,
145 relay_addr,
146 FiveTuple::default(),
147 TextAttribute::new(ATTR_USERNAME, "user".into()),
148 None,
149 );
150
151 let addr = SocketAddr::from_str("127.0.0.1:3478")?;
152 let c = ChannelBind::new(ChannelNumber(MIN_CHANNEL_NUMBER), addr);
153
154 a.add_channel_bind(c, DEFAULT_LIFETIME).await?;
155
156 let exist_channel_addr = a
157 .get_channel_addr(&ChannelNumber(MIN_CHANNEL_NUMBER))
158 .await
159 .unwrap();
160 assert_eq!(addr, exist_channel_addr);
161
162 let not_exist_channel = a
163 .get_channel_addr(&ChannelNumber(MIN_CHANNEL_NUMBER + 1))
164 .await;
165 assert!(
166 not_exist_channel.is_none(),
167 "should be nil for not existed channel."
168 );
169
170 Ok(())
171 }
172
173 #[tokio::test]
test_get_channel_by_addr() -> Result<()>174 async fn test_get_channel_by_addr() -> Result<()> {
175 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
176 let relay_socket = Arc::clone(&turn_socket);
177 let relay_addr = relay_socket.local_addr()?;
178 let a = Allocation::new(
179 turn_socket,
180 relay_socket,
181 relay_addr,
182 FiveTuple::default(),
183 TextAttribute::new(ATTR_USERNAME, "user".into()),
184 None,
185 );
186
187 let addr = SocketAddr::from_str("127.0.0.1:3478")?;
188 let addr2 = SocketAddr::from_str("127.0.0.1:3479")?;
189 let c = ChannelBind::new(ChannelNumber(MIN_CHANNEL_NUMBER), addr);
190
191 a.add_channel_bind(c, DEFAULT_LIFETIME).await?;
192
193 let exist_channel_number = a.get_channel_number(&addr).await.unwrap();
194 assert_eq!(ChannelNumber(MIN_CHANNEL_NUMBER), exist_channel_number);
195
196 let not_exist_channel = a.get_channel_number(&addr2).await;
197 assert!(
198 not_exist_channel.is_none(),
199 "should be nil for not existed channel."
200 );
201
202 Ok(())
203 }
204
205 #[tokio::test]
test_remove_channel_bind() -> Result<()>206 async fn test_remove_channel_bind() -> Result<()> {
207 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
208 let relay_socket = Arc::clone(&turn_socket);
209 let relay_addr = relay_socket.local_addr()?;
210 let a = Allocation::new(
211 turn_socket,
212 relay_socket,
213 relay_addr,
214 FiveTuple::default(),
215 TextAttribute::new(ATTR_USERNAME, "user".into()),
216 None,
217 );
218
219 let addr = SocketAddr::from_str("127.0.0.1:3478")?;
220 let number = ChannelNumber(MIN_CHANNEL_NUMBER);
221 let c = ChannelBind::new(number, addr);
222
223 a.add_channel_bind(c, DEFAULT_LIFETIME).await?;
224
225 a.remove_channel_bind(number).await;
226
227 let not_exist_channel = a.get_channel_addr(&number).await;
228 assert!(
229 not_exist_channel.is_none(),
230 "should be nil for not existed channel."
231 );
232
233 let not_exist_channel = a.get_channel_number(&addr).await;
234 assert!(
235 not_exist_channel.is_none(),
236 "should be nil for not existed channel."
237 );
238
239 Ok(())
240 }
241
242 #[tokio::test]
test_allocation_refresh() -> Result<()>243 async fn test_allocation_refresh() -> Result<()> {
244 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
245 let relay_socket = Arc::clone(&turn_socket);
246 let relay_addr = relay_socket.local_addr()?;
247 let a = Allocation::new(
248 turn_socket,
249 relay_socket,
250 relay_addr,
251 FiveTuple::default(),
252 TextAttribute::new(ATTR_USERNAME, "user".into()),
253 None,
254 );
255
256 a.start(DEFAULT_LIFETIME).await;
257 a.refresh(Duration::from_secs(0)).await;
258
259 assert!(!a.stop(), "lifetimeTimer has expired");
260
261 Ok(())
262 }
263
264 #[tokio::test]
test_allocation_close() -> Result<()>265 async fn test_allocation_close() -> Result<()> {
266 let turn_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
267 let relay_socket = Arc::clone(&turn_socket);
268 let relay_addr = relay_socket.local_addr()?;
269 let a = Allocation::new(
270 turn_socket,
271 relay_socket,
272 relay_addr,
273 FiveTuple::default(),
274 TextAttribute::new(ATTR_USERNAME, "user".into()),
275 None,
276 );
277
278 // add mock lifetimeTimer
279 a.start(DEFAULT_LIFETIME).await;
280
281 // add channel
282 let addr = SocketAddr::from_str("127.0.0.1:3478")?;
283 let number = ChannelNumber(MIN_CHANNEL_NUMBER);
284 let c = ChannelBind::new(number, addr);
285
286 a.add_channel_bind(c, DEFAULT_LIFETIME).await?;
287
288 // add permission
289 a.add_permission(Permission::new(addr)).await;
290
291 a.close().await?;
292
293 Ok(())
294 }
295