11use super :: * ;
2- use crate :: error:: Result ;
3- use crate :: relay:: relay_none:: * ;
42
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 } ;
87use tokio:: net:: UdpSocket ;
98use util:: vnet:: net:: * ;
109
@@ -62,6 +61,7 @@ async fn test_packet_handler() -> Result<()> {
6261 Arc :: new ( turn_socket) ,
6362 0 ,
6463 DEFAULT_LIFETIME ,
64+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
6565 )
6666 . await ?;
6767
@@ -74,8 +74,6 @@ async fn test_packet_handler() -> Result<()> {
7474 ) ;
7575
7676 let port = {
77- let a = a. lock ( ) . await ;
78-
7977 // add permission with peer1 address
8078 a. add_permission ( Permission :: new ( peer_listener1. local_addr ( ) ?) )
8179 . await ;
@@ -168,11 +166,18 @@ async fn test_create_allocation_duplicate_five_tuple() -> Result<()> {
168166 Arc :: clone ( & turn_socket) ,
169167 0 ,
170168 DEFAULT_LIFETIME ,
169+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
171170 )
172171 . await ?;
173172
174173 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+ )
176181 . await ;
177182 assert ! ( result. is_err( ) , "expected error, but got ok" ) ;
178183
@@ -196,6 +201,7 @@ async fn test_delete_allocation() -> Result<()> {
196201 Arc :: clone ( & turn_socket) ,
197202 0 ,
198203 DEFAULT_LIFETIME ,
204+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
199205 )
200206 . await ?;
201207
@@ -231,7 +237,13 @@ async fn test_allocation_timeout() -> Result<()> {
231237 let five_tuple = random_five_tuple ( ) ;
232238
233239 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+ )
235247 . await ?;
236248
237249 allocations. push ( a) ;
@@ -250,8 +262,7 @@ async fn test_allocation_timeout() -> Result<()> {
250262
251263 let any_outstanding = false ;
252264
253- for allocation in & allocations {
254- let mut a = allocation. lock ( ) . await ;
265+ for a in & allocations {
255266 if a. close ( ) . await . is_ok ( ) {
256267 continue ' outer;
257268 }
@@ -280,6 +291,7 @@ async fn test_manager_close() -> Result<()> {
280291 Arc :: clone ( & turn_socket) ,
281292 0 ,
282293 Duration :: from_millis ( 100 ) ,
294+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
283295 )
284296 . await ?;
285297 allocations. push ( a1) ;
@@ -290,6 +302,7 @@ async fn test_manager_close() -> Result<()> {
290302 Arc :: clone ( & turn_socket) ,
291303 0 ,
292304 Duration :: from_millis ( 200 ) ,
305+ TextAttribute :: new ( ATTR_USERNAME , "user" . into ( ) ) ,
293306 )
294307 . await ?;
295308 allocations. push ( a2) ;
@@ -300,8 +313,7 @@ async fn test_manager_close() -> Result<()> {
300313
301314 m. close ( ) . await ?;
302315
303- for allocation in allocations {
304- let mut a = allocation. lock ( ) . await ;
316+ for a in allocations {
305317 assert ! (
306318 a. close( ) . await . is_err( ) ,
307319 "Allocation should be closed if lifetime timeout"
@@ -310,3 +322,56 @@ async fn test_manager_close() -> Result<()> {
310322
311323 Ok ( ( ) )
312324}
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