Debian Codegraph @

third_party/rust/neqo-transport/src/connection/tests/idle.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use std::time::{Duration, Instant};
8
9 use neqo_common::{qtrace, qwarn, Encoder};
10 use test_fixture::{now, split_datagram};
11
12 use super::{
13 super::{Connection, ConnectionParameters, IdleTimeout, Output, State},
14 connect, connect_force_idle, connect_rtt_idle, connect_with_rtt, default_client,
15 default_server, maybe_authenticate, new_client, new_server, send_and_receive, send_something,
16 AT_LEAST_PTO, DEFAULT_STREAM_DATA,
17 };
18 use crate::{
19 packet::PacketBuilder,
20 stats::FrameStats,
21 stream_id::{StreamId, StreamType},
22 tparams::{TransportParameter, TransportParameterId},
23 tracking::PacketNumberSpace,
24 };
25
26 fn default_timeout() -> Duration {
27 ConnectionParameters::default().get_idle_timeout()
28 }
29
30 fn keep_alive_timeout() -> Duration {
31 default_timeout() / 2
32 }
33
34 fn test_idle_timeout(client: &mut Connection, server: &mut Connection, timeout: Duration) {
35 assert!(timeout > Duration::from_secs(1));
36 connect_force_idle(client, server);
37
38 let now = now();
39
40 let res = client.process_output(now);
41 assert_eq!(res, Output::Callback(timeout));
42
43 // Still connected after timeout-1 seconds. Idle timer not reset
44 drop(client.process_output(now + timeout.checked_sub(Duration::from_secs(1)).unwrap()));
45 assert!(matches!(client.state(), State::Confirmed));
46
47 drop(client.process_output(now + timeout));
48
49 // Not connected after timeout.
50 assert!(matches!(client.state(), State::Closed(_)));
51 }
52
53 #[test]
54 fn idle_timeout() {
55 let mut client = default_client();
56 let mut server = default_server();
57 test_idle_timeout(&mut client, &mut server, default_timeout());
58 }
59
60 #[test]
61 fn idle_timeout_custom_client() {
62 const IDLE_TIMEOUT: Duration = Duration::from_secs(5);
63 let mut client = new_client(ConnectionParameters::default().idle_timeout(IDLE_TIMEOUT));
64 let mut server = default_server();
65 test_idle_timeout(&mut client, &mut server, IDLE_TIMEOUT);
66 }
67
68 #[test]
69 fn idle_timeout_custom_server() {
70 const IDLE_TIMEOUT: Duration = Duration::from_secs(5);
71 let mut client = default_client();
72 let mut server = new_server(ConnectionParameters::default().idle_timeout(IDLE_TIMEOUT));
73 test_idle_timeout(&mut client, &mut server, IDLE_TIMEOUT);
74 }
75
76 #[test]
77 fn idle_timeout_custom_both() {
78 const LOWER_TIMEOUT: Duration = Duration::from_secs(5);
79 const HIGHER_TIMEOUT: Duration = Duration::from_secs(10);
80 let mut client = new_client(ConnectionParameters::default().idle_timeout(HIGHER_TIMEOUT));
81 let mut server = new_server(ConnectionParameters::default().idle_timeout(LOWER_TIMEOUT));
82 test_idle_timeout(&mut client, &mut server, LOWER_TIMEOUT);
83 }
84
85 #[test]
86 fn asymmetric_idle_timeout() {
87 const LOWER_TIMEOUT_MS: u64 = 1000;
88 const LOWER_TIMEOUT: Duration = Duration::from_millis(LOWER_TIMEOUT_MS);
89 // Sanity check the constant.
90 assert!(LOWER_TIMEOUT < default_timeout());
91
92 let mut client = default_client();
93 let mut server = default_server();
94
95 // Overwrite the default at the server.
96 server
97 .tps
98 .borrow_mut()
99 .local_mut()
100 .set_integer(TransportParameterId::IdleTimeout, LOWER_TIMEOUT_MS);
101 server.idle_timeout = IdleTimeout::new(LOWER_TIMEOUT);
102
103 // Now connect and force idleness manually.
104 // We do that by following what `force_idle` does and have each endpoint
105 // send two packets, which are delivered out of order. See `force_idle`.
106 connect(&mut client, &mut server);
107 let c1 = send_something(&mut client, now());
108 let c2 = send_something(&mut client, now());
109 server.process_input(c2, now());
110 server.process_input(c1, now());
111 let s1 = send_something(&mut server, now());
112 let s2 = send_something(&mut server, now());
113 client.process_input(s2, now());
114 let ack = client.process(Some(s1), now()).dgram();
115 assert!(ack.is_some());
116 // Now both should have received ACK frames so should be idle.
117 assert_eq!(server.process(ack, now()), Output::Callback(LOWER_TIMEOUT));
118 assert_eq!(
119 client.process_output(now()),
120 Output::Callback(LOWER_TIMEOUT)
121 );
122 }
123
124 #[test]
125 fn tiny_idle_timeout() {
126 const RTT: Duration = Duration::from_millis(500);
127 const LOWER_TIMEOUT_MS: u64 = 100;
128 const LOWER_TIMEOUT: Duration = Duration::from_millis(LOWER_TIMEOUT_MS);
129 // We won't respect a value that is lower than 3*PTO, sanity check.
130 assert!(LOWER_TIMEOUT < 3 * RTT);
131
132 let mut client = default_client();
133 let mut server = default_server();
134
135 // Overwrite the default at the server.
136 server
137 .set_local_tparam(
138 TransportParameterId::IdleTimeout,
139 TransportParameter::Integer(LOWER_TIMEOUT_MS),
140 )
141 .unwrap();
142 server.idle_timeout = IdleTimeout::new(LOWER_TIMEOUT);
143
144 // Now connect with an RTT and force idleness manually.
145 let mut now = connect_with_rtt(&mut client, &mut server, now(), RTT);
146 let c1 = send_something(&mut client, now);
147 let c2 = send_something(&mut client, now);
148 now += RTT / 2;
149 server.process_input(c2, now);
150 server.process_input(c1, now);
151 let s1 = send_something(&mut server, now);
152 let s2 = send_something(&mut server, now);
153 now += RTT / 2;
154 client.process_input(s2, now);
155 let ack = client.process(Some(s1), now).dgram();
156 assert!(ack.is_some());
157
158 // The client should be idle now, but with a different timer.
159 if let Output::Callback(t) = client.process_output(now) {
160 assert!(t > LOWER_TIMEOUT);
161 } else {
162 panic!("Client not idle");
163 }
164
165 // The server should go idle after the ACK, but again with a larger timeout.
166 now += RTT / 2;
167 if let Output::Callback(t) = client.process(ack, now) {
168 assert!(t > LOWER_TIMEOUT);
169 } else {
170 panic!("Client not idle");
171 }
172 }
173
174 #[test]
175 fn idle_send_packet1() {
176 const DELTA: Duration = Duration::from_millis(10);
177
178 let mut client = default_client();
179 let mut server = default_server();
180 let mut now = now();
181 connect_force_idle(&mut client, &mut server);
182
183 let timeout = client.process_output(now).callback();
184 assert_eq!(timeout, default_timeout());
185
186 now += Duration::from_secs(10);
187 let dgram = send_and_receive(&mut client, &mut server, now);
188 assert!(dgram.is_some()); // the server will want to ACK, we can drop that.
189
190 // Still connected after 39 seconds because idle timer reset by the
191 // outgoing packet.
192 now += default_timeout() - DELTA;
193 let dgram = client.process_output(now).dgram();
194 assert!(dgram.is_some()); // PTO
195 assert!(client.state().connected());
196
197 // Not connected after 40 seconds.
198 now += DELTA;
199 let out = client.process_output(now);
200 assert!(matches!(out, Output::None));
201 assert!(client.state().closed());
202 }
203
204 #[test]
205 fn idle_send_packet2() {
206 const GAP: Duration = Duration::from_secs(10);
207 const DELTA: Duration = Duration::from_millis(10);
208
209 let mut client = default_client();
210 let mut server = default_server();
211 connect_force_idle(&mut client, &mut server);
212
213 let mut now = now();
214
215 let timeout = client.process_output(now).callback();
216 assert_eq!(timeout, default_timeout());
217
218 // First transmission at t=GAP.
219 now += GAP;
220 drop(send_something(&mut client, now));
221
222 // Second transmission at t=2*GAP.
223 drop(send_something(&mut client, now + GAP));
224 assert!((GAP * 2 + DELTA) < default_timeout());
225
226 // Still connected just before GAP + default_timeout().
227 now += default_timeout() - DELTA;
228 let dgram = client.process_output(now).dgram();
229 assert!(dgram.is_some()); // PTO
230 assert!(matches!(client.state(), State::Confirmed));
231
232 // Not connected after 40 seconds because timer not reset by second
233 // outgoing packet
234 now += DELTA;
235 let out = client.process_output(now);
236 assert!(matches!(out, Output::None));
237 assert!(matches!(client.state(), State::Closed(_)));
238 }
239
240 #[test]
241 fn idle_recv_packet() {
242 const FUDGE: Duration = Duration::from_millis(10);
243
244 let mut client = default_client();
245 let mut server = default_server();
246 connect_force_idle(&mut client, &mut server);
247
248 let mut now = now();
249
250 let res = client.process_output(now);
251 assert_eq!(res, Output::Callback(default_timeout()));
252
253 let stream = client.stream_create(StreamType::BiDi).unwrap();
254 assert_eq!(stream, 0);
255 assert_eq!(client.stream_send(stream, b"hello").unwrap(), 5);
256
257 // Respond with another packet.
258 // Note that it is important that this not result in the RTT increasing above 0.
259 // Otherwise, the eventual timeout will be extended (and we're not testing that).
260 now += Duration::from_secs(10);
261 let out = client.process_output(now);
262 server.process_input(out.dgram().unwrap(), now);
263 assert_eq!(server.stream_send(stream, b"world").unwrap(), 5);
264 let out = server.process_output(now);
265 assert_ne!(out.as_dgram_ref(), None);
266 drop(client.process(out.dgram(), now));
267 assert!(matches!(client.state(), State::Confirmed));
268
269 // Add a little less than the idle timeout and we're still connected.
270 now += default_timeout() - FUDGE;
271 drop(client.process_output(now));
272 assert!(matches!(client.state(), State::Confirmed));
273
274 now += FUDGE;
275 drop(client.process_output(now));
276
277 assert!(matches!(client.state(), State::Closed(_)));
278 }
279
280 /// Caching packets should not cause the connection to become idle.
281 /// This requires a few tricks to keep the connection from going
282 /// idle while preventing any progress on the handshake.
283 #[test]
284 fn idle_caching() {
285 let mut client = default_client();
286 let mut server = default_server();
287 let start = now();
288 let mut builder = PacketBuilder::short(Encoder::new(), false, None::<&[u8]>);
289
290 // Perform the first round trip, but drop the Initial from the server.
291 // The client then caches the Handshake packet.
292 let dgram = client.process_output(start).dgram();
293 let dgram2 = client.process_output(start).dgram();
294 server.process_input(dgram.unwrap(), start);
295 let dgram = server.process(dgram2, start).dgram();
296 let dgram = client.process(dgram, start).dgram();
297 let dgram = server.process(dgram, start).dgram();
298 let (_, handshake) = split_datagram(&dgram.unwrap());
299 client.process_input(handshake.unwrap(), start);
300
301 // Perform an exchange and keep the connection alive.
302 let middle = start + AT_LEAST_PTO;
303 // This is the RTX of the client Initial.
304 let dgram = client.process_output(middle).dgram();
305
306 // Get the server to send its first probe and throw that away.
307 drop(server.process_output(middle).dgram());
308 // Now let the server process the RTX'ed client Initial. This causes the server
309 // to send CRYPTO frames again, so manually extract and discard those.
310 server.process_input(dgram.unwrap(), middle);
311 let mut tokens = Vec::new();
312 server.crypto.streams_mut().write_frame(
313 PacketNumberSpace::Initial,
314 server.conn_params.sni_slicing_enabled(),
315 &mut builder,
316 &mut tokens,
317 &mut FrameStats::default(),
318 );
319 assert_eq!(tokens.len(), 1);
320 tokens.clear();
321 server.crypto.streams_mut().write_frame(
322 PacketNumberSpace::Initial,
323 server.conn_params.sni_slicing_enabled(),
324 &mut builder,
325 &mut tokens,
326 &mut FrameStats::default(),
327 );
328 assert!(tokens.is_empty());
329 let dgram = server.process_output(middle).dgram();
330
331 // Now only allow the Initial packet from the server through;
332 // it shouldn't contain a CRYPTO frame.
333 let (initial, _) = split_datagram(&dgram.unwrap());
334 let crypto_before_c = client.stats().frame_rx.crypto;
335 let ack_before = client.stats().frame_rx.ack;
336 client.process_input(initial, middle);
337 assert_eq!(client.stats().frame_rx.crypto, crypto_before_c);
338 assert_eq!(client.stats().frame_rx.ack, ack_before + 1);
339
340 let end = start + default_timeout() + (AT_LEAST_PTO / 2);
341 // Now let the server Initial through, with the CRYPTO frame.
342 let dgram = server.process_output(end).dgram();
343 let (initial, _) = split_datagram(&dgram.unwrap());
344 qwarn!("client ingests initial, finally");
345 drop(client.process(Some(initial), end));
346 maybe_authenticate(&mut client);
347 let dgram = client.process_output(end).dgram();
348 let dgram = server.process(dgram, end).dgram();
349 client.process_input(dgram.unwrap(), end);
350 assert_eq!(*client.state(), State::Confirmed);
351 assert_eq!(*server.state(), State::Confirmed);
352 }
353
354 /// This function opens a bidirectional stream and leaves both endpoints
355 /// idle, with the stream left open.
356 /// The stream ID of that stream is returned (along with the new time).
357 fn create_stream_idle_rtt(
358 initiator: &mut Connection,
359 responder: &mut Connection,
360 mut now: Instant,
361 rtt: Duration,
362 ) -> (Instant, StreamId) {
363 let check_idle = |endpoint: &mut Connection, now: Instant| {
364 let delay = endpoint.process_output(now).callback();
365 qtrace!("[{endpoint}] idle timeout {delay:?}");
366 if rtt < default_timeout() / 4 {
367 assert_eq!(default_timeout(), delay);
368 } else {
369 assert!(delay > default_timeout());
370 }
371 };
372
373 // Exchange a message each way on a stream.
374 let stream = initiator.stream_create(StreamType::BiDi).unwrap();
375 _ = initiator.stream_send(stream, DEFAULT_STREAM_DATA).unwrap();
376 let req = initiator.process_output(now).dgram();
377 now += rtt / 2;
378 responder.process_input(req.unwrap(), now);
379
380 // Reordering two packets from the responder forces the initiator to be idle.
381 _ = responder.stream_send(stream, DEFAULT_STREAM_DATA).unwrap();
382 let resp1 = responder.process_output(now).dgram();
383 _ = responder.stream_send(stream, DEFAULT_STREAM_DATA).unwrap();
384 let resp2 = responder.process_output(now).dgram();
385
386 now += rtt / 2;
387 initiator.process_input(resp2.unwrap(), now);
388 initiator.process_input(resp1.unwrap(), now);
389 let ack = initiator.process_output(now).dgram();
390 assert!(ack.is_some());
391 check_idle(initiator, now);
392
393 // Receiving the ACK should return the responder to idle too.
394 now += rtt / 2;
395 responder.process_input(ack.unwrap(), now);
396 check_idle(responder, now);
397
398 (now, stream)
399 }
400
401 fn create_stream_idle(initiator: &mut Connection, responder: &mut Connection) -> StreamId {
402 let (_, stream) = create_stream_idle_rtt(initiator, responder, now(), Duration::new(0, 0));
403 stream
404 }
405
406 fn assert_idle(endpoint: &mut Connection, now: Instant, expected: Duration) {
407 assert_eq!(endpoint.process_output(now).callback(), expected);
408 }
409
410 /// The creator of a stream marks it as important enough to use a keep-alive.
411 #[test]
412 fn keep_alive_initiator() {
413 let mut client = default_client();
414 let mut server = default_server();
415 connect(&mut client, &mut server);
416 let stream = create_stream_idle(&mut server, &mut client);
417 let mut now = now();
418
419 // Marking the stream for keep-alive changes the idle timeout.
420 server.stream_keep_alive(stream, true).unwrap();
421 assert_idle(&mut server, now, keep_alive_timeout());
422
423 // Wait that long and the server should send a PING frame.
424 now += keep_alive_timeout();
425 let pings_before = server.stats().frame_tx.ping;
426 let ping = server.process_output(now).dgram();
427 assert!(ping.is_some());
428 assert_eq!(server.stats().frame_tx.ping, pings_before + 1);
429
430 // Exchange ack for the PING.
431 let out = client.process(ping, now).dgram();
432 let out = server.process(out, now).dgram();
433 assert!(client.process(out, now).dgram().is_none());
434
435 // Check that there will be next keep-alive ping after keep_alive_timeout().
436 assert_idle(&mut server, now, keep_alive_timeout());
437 now += keep_alive_timeout();
438 let pings_before2 = server.stats().frame_tx.ping;
439 let ping = server.process_output(now).dgram();
440 assert!(ping.is_some());
441 assert_eq!(server.stats().frame_tx.ping, pings_before2 + 1);
442 }
443
444 /// Test a keep-alive ping is retransmitted if lost.
445 #[test]
446 fn keep_alive_lost() {
447 let mut client = default_client();
448 let mut server = default_server();
449 connect(&mut client, &mut server);
450 let stream = create_stream_idle(&mut server, &mut client);
451 let mut now = now();
452
453 server.stream_keep_alive(stream, true).unwrap();
454 assert_idle(&mut server, now, keep_alive_timeout());
455
456 // Wait that long and the server should send a PING frame.
457 now += keep_alive_timeout();
458 let pings_before = server.stats().frame_tx.ping;
459 let ping = server.process_output(now).dgram();
460 assert!(ping.is_some());
461 assert_eq!(server.stats().frame_tx.ping, pings_before + 1);
462
463 // Wait for ping to be marked lost.
464 assert!(server.process_output(now).callback() < AT_LEAST_PTO);
465 now += AT_LEAST_PTO;
466 let pings_before2 = server.stats().frame_tx.ping;
467 let ping = server.process_output(now).dgram();
468 assert!(ping.is_some());
469 assert_eq!(server.stats().frame_tx.ping, pings_before2 + 1);
470
471 // Exchange ack for the PING.
472 let out = client.process(ping, now).dgram();
473
474 now += Duration::from_millis(20);
475 let out = server.process(out, now).dgram();
476
477 assert!(client.process(out, now).dgram().is_none());
478
479 // TODO: if we run server.process with current value of now, the server will
480 // return some small timeout for the recovery although it does not have
481 // any outstanding data. Therefore we call it after AT_LEAST_PTO.
482 now += AT_LEAST_PTO;
483 assert_idle(&mut server, now, keep_alive_timeout() - AT_LEAST_PTO);
484 }
485
486 /// The other peer can also keep it alive.
487 #[test]
488 fn keep_alive_responder() {
489 let mut client = default_client();
490 let mut server = default_server();
491 connect(&mut client, &mut server);
492 let stream = create_stream_idle(&mut server, &mut client);
493 let mut now = now();
494
495 client.stream_keep_alive(stream, true).unwrap();
496 assert_idle(&mut client, now, keep_alive_timeout());
497
498 // Wait that long and the client should send a PING frame.
499 now += keep_alive_timeout();
500 eprintln!("after wait");
501 let pings_before = client.stats().frame_tx.ping;
502 let ping = client.process_output(now).dgram();
503 assert!(ping.is_some());
504 assert_eq!(client.stats().frame_tx.ping, pings_before + 1);
505 }
506
507 /// Unmark a stream as being keep-alive.
508 #[test]
509 fn keep_alive_unmark() {
510 let mut client = default_client();
511 let mut server = default_server();
512 connect(&mut client, &mut server);
513 let stream = create_stream_idle(&mut client, &mut server);
514
515 client.stream_keep_alive(stream, true).unwrap();
516 assert_idle(&mut client, now(), keep_alive_timeout());
517
518 client.stream_keep_alive(stream, false).unwrap();
519 assert_idle(&mut client, now(), default_timeout());
520 }
521
522 /// The sender has something to send. Make it send it
523 /// and cause the receiver to become idle by sending something
524 /// else, reordering the packets, and consuming the ACK.
525 /// Note that the sender might not be idle if the thing that it
526 /// sends results in something in addition to an ACK.
527 fn transfer_force_idle(sender: &mut Connection, receiver: &mut Connection) {
528 let dgram = sender.process_output(now()).dgram();
529 let chaff = send_something(sender, now());
530 receiver.process_input(chaff, now());
531 receiver.process_input(dgram.unwrap(), now());
532 let ack = receiver.process_output(now()).dgram();
533 sender.process_input(ack.unwrap(), now());
534 }
535
536 /// Receiving the end of the stream stops keep-alives for that stream.
537 /// Even if that data hasn't been read.
538 #[test]
539 fn keep_alive_close() {
540 let mut client = default_client();
541 let mut server = default_server();
542 connect(&mut client, &mut server);
543 let stream = create_stream_idle(&mut client, &mut server);
544
545 client.stream_keep_alive(stream, true).unwrap();
546 assert_idle(&mut client, now(), keep_alive_timeout());
547
548 client.stream_close_send(stream).unwrap();
549 transfer_force_idle(&mut client, &mut server);
550 assert_idle(&mut client, now(), keep_alive_timeout());
551
552 server.stream_close_send(stream).unwrap();
553 transfer_force_idle(&mut server, &mut client);
554 assert_idle(&mut client, now(), default_timeout());
555 }
556
557 /// Receiving `RESET_STREAM` stops keep-alives for that stream, but only once
558 /// the sending side is also closed.
559 #[test]
560 fn keep_alive_reset() {
561 let mut client = default_client();
562 let mut server = default_server();
563 connect(&mut client, &mut server);
564 let stream = create_stream_idle(&mut client, &mut server);
565
566 client.stream_keep_alive(stream, true).unwrap();
567 assert_idle(&mut client, now(), keep_alive_timeout());
568
569 client.stream_close_send(stream).unwrap();
570 transfer_force_idle(&mut client, &mut server);
571 assert_idle(&mut client, now(), keep_alive_timeout());
572
573 server.stream_reset_send(stream, 0).unwrap();
574 transfer_force_idle(&mut server, &mut client);
575 assert_idle(&mut client, now(), default_timeout());
576
577 // The client will fade away from here.
578 let t = now() + keep_alive_timeout();
579 assert_eq!(client.process_output(t).callback(), keep_alive_timeout());
580 let t = now() + default_timeout();
581 assert_eq!(client.process_output(t), Output::None);
582 }
583
584 /// Stopping sending also cancels the keep-alive.
585 #[test]
586 fn keep_alive_stop_sending() {
587 let mut client = default_client();
588 let mut server = default_server();
589 connect(&mut client, &mut server);
590 let stream = create_stream_idle(&mut client, &mut server);
591
592 client.stream_keep_alive(stream, true).unwrap();
593 assert_idle(&mut client, now(), keep_alive_timeout());
594
595 client.stream_close_send(stream).unwrap();
596 client.stream_stop_sending(stream, 0).unwrap();
597 transfer_force_idle(&mut client, &mut server);
598 // The server will have sent RESET_STREAM, which the client will
599 // want to acknowledge, so force that out.
600 let junk = send_something(&mut server, now());
601 let ack = client.process(Some(junk), now()).dgram();
602 assert!(ack.is_some());
603
604 // Now the client should be idle.
605 assert_idle(&mut client, now(), default_timeout());
606 }
607
608 /// Multiple active streams are tracked properly.
609 #[test]
610 fn keep_alive_multiple_stop() {
611 let mut client = default_client();
612 let mut server = default_server();
613 connect(&mut client, &mut server);
614 let stream = create_stream_idle(&mut client, &mut server);
615
616 client.stream_keep_alive(stream, true).unwrap();
617 assert_idle(&mut client, now(), keep_alive_timeout());
618
619 let other = client.stream_create(StreamType::BiDi).unwrap();
620 client.stream_keep_alive(other, true).unwrap();
621 assert_idle(&mut client, now(), keep_alive_timeout());
622
623 client.stream_keep_alive(stream, false).unwrap();
624 assert_idle(&mut client, now(), keep_alive_timeout());
625
626 client.stream_keep_alive(other, false).unwrap();
627 assert_idle(&mut client, now(), default_timeout());
628 }
629
630 /// If the RTT is too long relative to the idle timeout, the keep-alive is large too.
631 #[test]
632 fn keep_alive_large_rtt() {
633 let mut client = default_client();
634 let mut server = default_server();
635 // Use an RTT that is large enough to cause the PTO timer to exceed half
636 // the idle timeout.
637 let rtt = default_timeout() * 3 / 4;
638 let now = connect_with_rtt(&mut client, &mut server, now(), rtt);
639 let (now, stream) = create_stream_idle_rtt(&mut server, &mut client, now, rtt);
640
641 // Calculating PTO here is tricky as RTTvar has eroded after multiple round trips.
642 // Just check that the delay is larger than the baseline and the RTT.
643 for endpoint in &mut [client, server] {
644 endpoint.stream_keep_alive(stream, true).unwrap();
645 let delay = endpoint.process_output(now).callback();
646 qtrace!("[{endpoint}] new delay {delay:?}");
647 assert!(delay > keep_alive_timeout());
648 assert!(delay > rtt);
649 }
650 }
651
652 /// Only the recipient of a unidirectional stream can keep it alive.
653 #[test]
654 fn keep_alive_uni() {
655 let mut client = default_client();
656 let mut server = default_server();
657 connect(&mut client, &mut server);
658
659 let stream = client.stream_create(StreamType::UniDi).unwrap();
660 client.stream_keep_alive(stream, true).unwrap_err();
661 _ = client.stream_send(stream, DEFAULT_STREAM_DATA).unwrap();
662 let dgram = client.process_output(now()).dgram();
663
664 server.process_input(dgram.unwrap(), now());
665 server.stream_keep_alive(stream, true).unwrap();
666 }
667
668 /// Test a keep-alive ping is send if there are outstanding ack-eliciting packets and that
669 /// the connection is closed after the idle timeout passes.
670 #[test]
671 fn keep_alive_with_ack_eliciting_packet_lost() {
672 const RTT: Duration = Duration::from_millis(500); // PTO will be ~1.1125s
673
674 // The idle time out will be set to ~ 5 * PTO. (IDLE_TIMEOUT/2 > pto and IDLE_TIMEOUT/2 < pto
675 // + 2pto) After handshake all packets will be lost. The following steps will happen after
676 // the handshake:
677 // - data will be sent on a stream that is marked for keep-alive, (at start time)
678 // - PTO timer will trigger first, and the data will be retransmitted together with a PING, (at
679 // the start time + pto)
680 // - keep-alive timer will trigger and a keep-alive PING will be sent, (at the start time +
681 // IDLE_TIMEOUT / 2)
682 // - PTO timer will trigger again. (at the start time + pto + 2*pto)
683 // - Idle time out will trigger (at the timeout + IDLE_TIMEOUT)
684 const IDLE_TIMEOUT: Duration = Duration::from_millis(6000);
685
686 // This test makes too many assumptions about single-packet flights and PTOs for multi-packet
687 // MLKEM flights to work.
688 let mut client = new_client(
689 ConnectionParameters::default()
690 .idle_timeout(IDLE_TIMEOUT)
691 .mlkem(false),
692 );
693 let mut server = default_server();
694 let mut now = connect_rtt_idle(&mut client, &mut server, RTT);
695 // connect_rtt_idle increase now by RTT / 2;
696 now -= RTT / 2;
697 assert_idle(&mut client, now, IDLE_TIMEOUT);
698
699 // Create a stream.
700 let stream = client.stream_create(StreamType::BiDi).unwrap();
701 // Marking the stream for keep-alive changes the idle timeout.
702 client.stream_keep_alive(stream, true).unwrap();
703 assert_idle(&mut client, now, IDLE_TIMEOUT / 2);
704
705 // Send data on the stream that will be lost.
706 _ = client.stream_send(stream, DEFAULT_STREAM_DATA).unwrap();
707 let _lost_packet = client.process_output(now).dgram();
708
709 let pto = client.process_output(now).callback();
710 // Wait for packet to be marked lost.
711 assert!(pto < IDLE_TIMEOUT / 2);
712 now += pto;
713 let retransmit = client.process_output(now).dgram();
714 assert!(retransmit.is_some());
715 let retransmit = client.process_output(now).dgram();
716 assert!(retransmit.is_some());
717
718 // The next callback should be for an idle PING.
719 assert_eq!(
720 client.process_output(now).callback(),
721 IDLE_TIMEOUT / 2 - pto
722 );
723
724 // Wait that long and the client should send a PING frame.
725 now += IDLE_TIMEOUT / 2 - pto;
726 let pings_before = client.stats().frame_tx.ping;
727 let ping = client.process_output(now).dgram();
728 assert!(ping.is_some());
729 assert_eq!(client.stats().frame_tx.ping, pings_before + 1);
730
731 // The next callback is for a PTO, the PTO timer is 2 * pto now.
732 assert_eq!(client.process_output(now).callback(), pto * 2);
733 now += pto * 2;
734 // Now we will retransmit stream data.
735 let retransmit = client.process_output(now).dgram();
736 assert!(retransmit.is_some());
737 let retransmit = client.process_output(now).dgram();
738 assert!(retransmit.is_some());
739
740 // The next callback will be an idle timeout.
741 assert_eq!(
742 client.process_output(now).callback(),
743 IDLE_TIMEOUT / 2 - 2 * pto
744 );
745
746 now += IDLE_TIMEOUT / 2 - 2 * pto;
747 let out = client.process_output(now);
748 assert!(matches!(out, Output::None));
749 assert!(matches!(client.state(), State::Closed(_)));
750 }
751
752 #[test]
753 fn keep_alive_no_unnecessary_ping() {
754 const RTT: Duration = Duration::from_millis(500); // PTO will be ~1.1125s
755
756 let mut client = default_client();
757 let mut server = default_server();
758 let mut now = connect_rtt_idle(&mut client, &mut server, RTT);
759
760 // Create a stream and send data on it that will be lost.
761 let stream = client.stream_create(StreamType::BiDi).unwrap();
762 client.stream_keep_alive(stream, true).unwrap();
763 _ = client.stream_send(stream, DEFAULT_STREAM_DATA).unwrap();
764 let _lost_packet = client.process_output(now).dgram();
765
766 // Client returns PTO timer.
767 assert!(matches!(client.process_output(now), Output::Callback(_)));
768
769 // Wait for idle timeout. This includes PTO. Thus both PTO and idle timeout
770 // are firing now.
771 now += default_timeout() / 2;
772 let retransmit = client.process_output(now).dgram();
773 assert!(retransmit.is_some());
774 let pings_before = client.stats().frame_tx.ping;
775 let pto_ping = client.process_output(now).dgram();
776 assert!(pto_ping.is_some());
777 assert_eq!(client.stats().frame_tx.ping, pings_before + 1);
778
779 // Expect no additional idle timeout ping, given that a PTO ping was just
780 // sent. I.e. expect idle timer to piggy back on PTO ping.
781 let pings_before = client.stats().frame_tx.ping;
782 assert!(client.process_output(now).dgram().is_none());
783 assert_eq!(client.stats().frame_tx.ping, pings_before);
784 }
785
786 #[test]
787 fn keep_alive_with_unresponsive_server() {
788 let mut client = default_client();
789 let mut server = default_server();
790 connect(&mut client, &mut server);
791
792 let mut now = now();
793 let client_stream = client.stream_create(StreamType::BiDi).unwrap();
794 client.stream_keep_alive(client_stream, true).unwrap();
795
796 for _ in 0..100 {
797 if client.stream_send(client_stream, &[0x0; 500]).is_err() {
798 break;
799 }
800 if let Output::Callback(t) = client.process_output(now) {
801 now += t;
802 }
803 }
804 // Connection should be closed due to idle timeout.
805 assert!(matches!(client.state(), State::Closed(_)));
806 }