@@ -236,3 +236,61 @@ fn handle_error(e: ServerError, transaction_id: TransactionId) -> Response {
236236 message : message. into ( ) ,
237237 } )
238238}
239+
240+ #[ cfg( test) ]
241+ mod tests {
242+ use std:: {
243+ net:: { IpAddr , Ipv4Addr , SocketAddr } ,
244+ sync:: Arc ,
245+ } ;
246+
247+ use crate :: { protocol:: utils:: get_connection_id, tracker:: tracker:: TorrentTracker , udp:: handle_connect, Configuration } ;
248+ use aquatic_udp_protocol:: { ConnectRequest , ConnectResponse , Response , TransactionId } ;
249+
250+ fn initialized_tracker ( ) -> Arc < TorrentTracker > {
251+ let config = Arc :: new ( Configuration :: default ( ) ) ;
252+ Arc :: new ( TorrentTracker :: new ( config) . unwrap ( ) )
253+ }
254+
255+ fn sample_remote_addr ( ) -> SocketAddr {
256+ SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 8080 )
257+ }
258+
259+ #[ tokio:: test]
260+ async fn a_connect_response_should_contain_the_same_transaction_id_as_the_connect_request ( ) {
261+ let request = ConnectRequest {
262+ transaction_id : TransactionId ( 0i32 ) ,
263+ } ;
264+
265+ let response = handle_connect ( sample_remote_addr ( ) , & request, initialized_tracker ( ) )
266+ . await
267+ . unwrap ( ) ;
268+
269+ assert_eq ! (
270+ response,
271+ Response :: Connect ( ConnectResponse {
272+ connection_id: get_connection_id( & sample_remote_addr( ) ) ,
273+ transaction_id: request. transaction_id
274+ } )
275+ ) ;
276+ }
277+
278+ #[ tokio:: test]
279+ async fn a_connect_response_should_contain_a_new_connection_id ( ) {
280+ let request = ConnectRequest {
281+ transaction_id : TransactionId ( 0i32 ) ,
282+ } ;
283+
284+ let response = handle_connect ( sample_remote_addr ( ) , & request, initialized_tracker ( ) )
285+ . await
286+ . unwrap ( ) ;
287+
288+ assert_eq ! (
289+ response,
290+ Response :: Connect ( ConnectResponse {
291+ connection_id: get_connection_id( & sample_remote_addr( ) ) ,
292+ transaction_id: request. transaction_id
293+ } )
294+ ) ;
295+ }
296+ }
0 commit comments