1
1
use super :: * ;
2
- use crate :: error:: Result ;
3
- use crate :: relay:: relay_none:: * ;
4
2
5
- use crate :: proto:: lifetime:: DEFAULT_LIFETIME ;
6
- use std:: net:: Ipv4Addr ;
7
- use std:: str:: FromStr ;
3
+ use crate :: { error:: Result , proto:: lifetime:: DEFAULT_LIFETIME , relay:: relay_none:: * } ;
4
+
5
+ use std:: { net:: Ipv4Addr , str:: FromStr } ;
6
+ use stun:: { attributes:: ATTR_USERNAME , textattrs:: TextAttribute } ;
8
7
use tokio:: net:: UdpSocket ;
9
8
use util:: vnet:: net:: * ;
10
9
@@ -62,6 +61,7 @@ async fn test_packet_handler() -> Result<()> {
62
61
Arc :: new ( turn_socket) ,
63
62
0 ,
64
63
DEFAULT_LIFETIME ,
64
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
65
65
)
66
66
. await ?;
67
67
@@ -74,8 +74,6 @@ async fn test_packet_handler() -> Result<()> {
74
74
) ;
75
75
76
76
let port = {
77
- let a = a. lock ( ) . await ;
78
-
79
77
// add permission with peer1 address
80
78
a. add_permission ( Permission :: new ( peer_listener1. local_addr ( ) ?) )
81
79
. await ;
@@ -168,11 +166,18 @@ async fn test_create_allocation_duplicate_five_tuple() -> Result<()> {
168
166
Arc :: clone ( & turn_socket) ,
169
167
0 ,
170
168
DEFAULT_LIFETIME ,
169
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
171
170
)
172
171
. await ?;
173
172
174
173
let result = m
175
- . create_allocation ( five_tuple, Arc :: clone ( & turn_socket) , 0 , DEFAULT_LIFETIME )
174
+ . create_allocation (
175
+ five_tuple,
176
+ Arc :: clone ( & turn_socket) ,
177
+ 0 ,
178
+ DEFAULT_LIFETIME ,
179
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
180
+ )
176
181
. await ;
177
182
assert ! ( result. is_err( ) , "expected error, but got ok" ) ;
178
183
@@ -196,6 +201,7 @@ async fn test_delete_allocation() -> Result<()> {
196
201
Arc :: clone ( & turn_socket) ,
197
202
0 ,
198
203
DEFAULT_LIFETIME ,
204
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
199
205
)
200
206
. await ?;
201
207
@@ -231,7 +237,13 @@ async fn test_allocation_timeout() -> Result<()> {
231
237
let five_tuple = random_five_tuple ( ) ;
232
238
233
239
let a = m
234
- . create_allocation ( five_tuple, Arc :: clone ( & turn_socket) , 0 , lifetime)
240
+ . create_allocation (
241
+ five_tuple,
242
+ Arc :: clone ( & turn_socket) ,
243
+ 0 ,
244
+ lifetime,
245
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
246
+ )
235
247
. await ?;
236
248
237
249
allocations. push ( a) ;
@@ -250,8 +262,7 @@ async fn test_allocation_timeout() -> Result<()> {
250
262
251
263
let any_outstanding = false ;
252
264
253
- for allocation in & allocations {
254
- let mut a = allocation. lock ( ) . await ;
265
+ for a in & allocations {
255
266
if a. close ( ) . await . is_ok ( ) {
256
267
continue ' outer;
257
268
}
@@ -280,6 +291,7 @@ async fn test_manager_close() -> Result<()> {
280
291
Arc :: clone ( & turn_socket) ,
281
292
0 ,
282
293
Duration :: from_millis ( 100 ) ,
294
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
283
295
)
284
296
. await ?;
285
297
allocations. push ( a1) ;
@@ -290,6 +302,7 @@ async fn test_manager_close() -> Result<()> {
290
302
Arc :: clone ( & turn_socket) ,
291
303
0 ,
292
304
Duration :: from_millis ( 200 ) ,
305
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
293
306
)
294
307
. await ?;
295
308
allocations. push ( a2) ;
@@ -300,8 +313,7 @@ async fn test_manager_close() -> Result<()> {
300
313
301
314
m. close ( ) . await ?;
302
315
303
- for allocation in allocations {
304
- let mut a = allocation. lock ( ) . await ;
316
+ for a in allocations {
305
317
assert ! (
306
318
a. close( ) . await . is_err( ) ,
307
319
"Allocation should be closed if lifetime timeout"
@@ -310,3 +322,56 @@ async fn test_manager_close() -> Result<()> {
310
322
311
323
Ok ( ( ) )
312
324
}
325
+
326
+ #[ tokio:: test]
327
+ async fn test_delete_allocation_by_username ( ) -> Result < ( ) > {
328
+ let turn_socket: Arc < dyn Conn + Send + Sync > = Arc :: new ( UdpSocket :: bind ( "0.0.0.0:0" ) . await ?) ;
329
+
330
+ let m = new_test_manager ( ) ;
331
+
332
+ let five_tuple1 = random_five_tuple ( ) ;
333
+ let five_tuple2 = random_five_tuple ( ) ;
334
+ let five_tuple3 = random_five_tuple ( ) ;
335
+
336
+ let _ = m
337
+ . create_allocation (
338
+ five_tuple1. clone ( ) ,
339
+ Arc :: clone ( & turn_socket) ,
340
+ 0 ,
341
+ DEFAULT_LIFETIME ,
342
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
343
+ )
344
+ . await ?;
345
+ let _ = m
346
+ . create_allocation (
347
+ five_tuple2. clone ( ) ,
348
+ Arc :: clone ( & turn_socket) ,
349
+ 0 ,
350
+ DEFAULT_LIFETIME ,
351
+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
352
+ )
353
+ . await ?;
354
+ let _ = m
355
+ . create_allocation (
356
+ five_tuple3. clone ( ) ,
357
+ Arc :: clone ( & turn_socket) ,
358
+ 0 ,
359
+ DEFAULT_LIFETIME ,
360
+ TextAttribute :: new ( ATTR_USERNAME , String :: from ( "user2" ) ) ,
361
+ )
362
+ . await ?;
363
+
364
+ assert_eq ! ( m. allocations. lock( ) . await . len( ) , 3 ) ;
365
+
366
+ m. delete_allocations_by_username ( "user" ) . await ;
367
+
368
+ assert_eq ! ( m. allocations. lock( ) . await . len( ) , 1 ) ;
369
+
370
+ assert ! (
371
+ m. get_allocation( & five_tuple1) . await . is_none( )
372
+ && m. get_allocation( & five_tuple2) . await . is_none( )
373
+ && m. get_allocation( & five_tuple3) . await . is_some( )
374
+ ) ;
375
+
376
+ Ok ( ( ) )
377
+ }
0 commit comments