@@ -130,10 +130,31 @@ mod receiving_an_announce_request {
130130 use crate :: servers:: udp:: contract:: send_connection_request;
131131 use crate :: servers:: udp:: Started ;
132132
133- pub async fn send_and_get_announce ( tx_id : TransactionId , c_id : ConnectionId , client : & UdpTrackerClient ) {
134- // Send announce request
133+ pub async fn assert_send_and_get_announce ( tx_id : TransactionId , c_id : ConnectionId , client : & UdpTrackerClient ) {
134+ let response = send_and_get_announce ( tx_id, c_id, client) . await ;
135+ assert ! ( is_ipv4_announce_response( & response) ) ;
136+ }
137+
138+ pub async fn send_and_get_announce (
139+ tx_id : TransactionId ,
140+ c_id : ConnectionId ,
141+ client : & UdpTrackerClient ,
142+ ) -> aquatic_udp_protocol:: Response {
143+ let announce_request = build_sample_announce_request ( tx_id, c_id, client. client . socket . local_addr ( ) . unwrap ( ) . port ( ) ) ;
144+
145+ match client. send ( announce_request. into ( ) ) . await {
146+ Ok ( _) => ( ) ,
147+ Err ( err) => panic ! ( "{err}" ) ,
148+ } ;
135149
136- let announce_request = AnnounceRequest {
150+ match client. receive ( ) . await {
151+ Ok ( response) => response,
152+ Err ( err) => panic ! ( "{err}" ) ,
153+ }
154+ }
155+
156+ fn build_sample_announce_request ( tx_id : TransactionId , c_id : ConnectionId , port : u16 ) -> AnnounceRequest {
157+ AnnounceRequest {
137158 connection_id : ConnectionId ( c_id. 0 ) ,
138159 action_placeholder : AnnounceActionPlaceholder :: default ( ) ,
139160 transaction_id : tx_id,
@@ -146,26 +167,34 @@ mod receiving_an_announce_request {
146167 ip_address : Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) . into ( ) ,
147168 key : PeerKey :: new ( 0i32 ) ,
148169 peers_wanted : NumberOfPeers ( 1i32 . into ( ) ) ,
149- port : Port ( client. client . socket . local_addr ( ) . unwrap ( ) . port ( ) . into ( ) ) ,
150- } ;
170+ port : Port ( port. into ( ) ) ,
171+ }
172+ }
151173
152- match client. send ( announce_request. into ( ) ) . await {
153- Ok ( _) => ( ) ,
154- Err ( err) => panic ! ( "{err}" ) ,
155- } ;
174+ #[ tokio:: test]
175+ async fn should_return_an_announce_response ( ) {
176+ INIT . call_once ( || {
177+ tracing_stderr_init ( LevelFilter :: ERROR ) ;
178+ } ) ;
156179
157- let response = match client. receive ( ) . await {
158- Ok ( response) => response,
180+ let env = Started :: new ( & configuration:: ephemeral ( ) . into ( ) ) . await ;
181+
182+ let client = match UdpTrackerClient :: new ( env. bind_address ( ) , DEFAULT_TIMEOUT ) . await {
183+ Ok ( udp_tracker_client) => udp_tracker_client,
159184 Err ( err) => panic ! ( "{err}" ) ,
160185 } ;
161186
162- // println!("test response {response:?}" );
187+ let tx_id = TransactionId :: new ( 123 ) ;
163188
164- assert ! ( is_ipv4_announce_response( & response) ) ;
189+ let c_id = send_connection_request ( tx_id, & client) . await ;
190+
191+ assert_send_and_get_announce ( tx_id, c_id, & client) . await ;
192+
193+ env. stop ( ) . await ;
165194 }
166195
167196 #[ tokio:: test]
168- async fn should_return_an_announce_response ( ) {
197+ async fn should_return_many_announce_response ( ) {
169198 INIT . call_once ( || {
170199 tracing_stderr_init ( LevelFilter :: ERROR ) ;
171200 } ) ;
@@ -181,13 +210,16 @@ mod receiving_an_announce_request {
181210
182211 let c_id = send_connection_request ( tx_id, & client) . await ;
183212
184- send_and_get_announce ( tx_id, c_id, & client) . await ;
213+ for x in 0 ..1000 {
214+ tracing:: info!( "req no: {x}" ) ;
215+ assert_send_and_get_announce ( tx_id, c_id, & client) . await ;
216+ }
185217
186218 env. stop ( ) . await ;
187219 }
188220
189221 #[ tokio:: test]
190- async fn should_return_many_announce_response ( ) {
222+ async fn should_ban_the_client_ip_if_it_sends_more_than_10_requests_with_a_cookie_value_not_normal ( ) {
191223 INIT . call_once ( || {
192224 tracing_stderr_init ( LevelFilter :: ERROR ) ;
193225 } ) ;
@@ -201,13 +233,30 @@ mod receiving_an_announce_request {
201233
202234 let tx_id = TransactionId :: new ( 123 ) ;
203235
204- let c_id = send_connection_request ( tx_id , & client ) . await ;
236+ // The eleven first requests should be fine
205237
206- for x in 0 ..1000 {
238+ let invalid_connection_id = ConnectionId :: new ( 0 ) ; // Zero is one of the not normal values.
239+
240+ for x in 0 ..=10 {
207241 tracing:: info!( "req no: {x}" ) ;
208- send_and_get_announce ( tx_id, c_id , & client) . await ;
242+ send_and_get_announce ( tx_id, invalid_connection_id , & client) . await ;
209243 }
210244
245+ // The twelfth request should be banned (timeout error)
246+
247+ let announce_request = build_sample_announce_request (
248+ tx_id,
249+ invalid_connection_id,
250+ client. client . socket . local_addr ( ) . unwrap ( ) . port ( ) ,
251+ ) ;
252+
253+ match client. send ( announce_request. into ( ) ) . await {
254+ Ok ( _) => ( ) ,
255+ Err ( err) => panic ! ( "{err}" ) ,
256+ } ;
257+
258+ assert ! ( client. receive( ) . await . is_err( ) ) ;
259+
211260 env. stop ( ) . await ;
212261 }
213262}
0 commit comments