@@ -130,10 +130,31 @@ mod receiving_an_announce_request {
130
130
use crate :: servers:: udp:: contract:: send_connection_request;
131
131
use crate :: servers:: udp:: Started ;
132
132
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
+ } ;
135
149
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 {
137
158
connection_id : ConnectionId ( c_id. 0 ) ,
138
159
action_placeholder : AnnounceActionPlaceholder :: default ( ) ,
139
160
transaction_id : tx_id,
@@ -146,26 +167,34 @@ mod receiving_an_announce_request {
146
167
ip_address : Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) . into ( ) ,
147
168
key : PeerKey :: new ( 0i32 ) ,
148
169
peers_wanted : NumberOfPeers ( 1i32 . into ( ) ) ,
149
- port : Port ( client. client . socket . local_addr ( ) . unwrap ( ) . port ( ) . into ( ) ) ,
150
- } ;
170
+ port : Port ( port. into ( ) ) ,
171
+ }
172
+ }
151
173
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
+ } ) ;
156
179
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,
159
184
Err ( err) => panic ! ( "{err}" ) ,
160
185
} ;
161
186
162
- // println!("test response {response:?}" );
187
+ let tx_id = TransactionId :: new ( 123 ) ;
163
188
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 ;
165
194
}
166
195
167
196
#[ tokio:: test]
168
- async fn should_return_an_announce_response ( ) {
197
+ async fn should_return_many_announce_response ( ) {
169
198
INIT . call_once ( || {
170
199
tracing_stderr_init ( LevelFilter :: ERROR ) ;
171
200
} ) ;
@@ -181,13 +210,16 @@ mod receiving_an_announce_request {
181
210
182
211
let c_id = send_connection_request ( tx_id, & client) . await ;
183
212
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
+ }
185
217
186
218
env. stop ( ) . await ;
187
219
}
188
220
189
221
#[ 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 ( ) {
191
223
INIT . call_once ( || {
192
224
tracing_stderr_init ( LevelFilter :: ERROR ) ;
193
225
} ) ;
@@ -201,13 +233,30 @@ mod receiving_an_announce_request {
201
233
202
234
let tx_id = TransactionId :: new ( 123 ) ;
203
235
204
- let c_id = send_connection_request ( tx_id , & client ) . await ;
236
+ // The eleven first requests should be fine
205
237
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 {
207
241
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 ;
209
243
}
210
244
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
+
211
260
env. stop ( ) . await ;
212
261
}
213
262
}
0 commit comments