| 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; |
| 8 |
|
| 9 |
use test_fixture::{datagram, now}; |
| 10 |
|
| 11 |
use super::{ |
| 12 |
super::{Connection, Output, State}, |
| 13 |
connect, connect_force_idle, default_client, default_server, send_something, |
| 14 |
}; |
| 15 |
use crate::{ |
| 16 |
tparams::{TransportParameter, TransportParameterId::StatelessResetToken}, |
| 17 |
AppError, CloseReason, Error, ERROR_APPLICATION_CLOSE, |
| 18 |
}; |
| 19 |
|
| 20 |
fn assert_draining(c: &Connection, expected: &Error) { |
| 21 |
assert!(c.state().closed()); |
| 22 |
if let State::Draining { |
| 23 |
error: CloseReason::Transport(error), |
| 24 |
.. |
| 25 |
} = c.state() |
| 26 |
{ |
| 27 |
assert_eq!(error, expected); |
| 28 |
} else { |
| 29 |
panic!(); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn connection_close() { |
| 35 |
let mut client = default_client(); |
| 36 |
let mut server = default_server(); |
| 37 |
connect(&mut client, &mut server); |
| 38 |
|
| 39 |
let now = now(); |
| 40 |
|
| 41 |
client.close(now, 42, ""); |
| 42 |
|
| 43 |
let stats_before = client.stats().frame_tx; |
| 44 |
let out = client.process_output(now); |
| 45 |
let stats_after = client.stats().frame_tx; |
| 46 |
assert_eq!( |
| 47 |
stats_after.connection_close, |
| 48 |
stats_before.connection_close + 1 |
| 49 |
); |
| 50 |
assert_eq!(stats_after.ack, stats_before.ack + 1); |
| 51 |
|
| 52 |
server.process_input(out.dgram().unwrap(), now); |
| 53 |
assert_draining(&server, &Error::PeerApplicationError(42)); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn connection_close_with_long_reason_string() { |
| 58 |
let mut client = default_client(); |
| 59 |
let mut server = default_server(); |
| 60 |
connect(&mut client, &mut server); |
| 61 |
|
| 62 |
let now = now(); |
| 63 |
// Create a long string and use it as the close reason. |
| 64 |
let long_reason = String::from_utf8([0x61; 2048].to_vec()).unwrap(); |
| 65 |
client.close(now, 42, long_reason); |
| 66 |
|
| 67 |
let stats_before = client.stats().frame_tx; |
| 68 |
let out = client.process_output(now); |
| 69 |
let stats_after = client.stats().frame_tx; |
| 70 |
assert_eq!( |
| 71 |
stats_after.connection_close, |
| 72 |
stats_before.connection_close + 1 |
| 73 |
); |
| 74 |
assert_eq!(stats_after.ack, stats_before.ack + 1); |
| 75 |
|
| 76 |
server.process_input(out.dgram().unwrap(), now); |
| 77 |
assert_draining(&server, &Error::PeerApplicationError(42)); |
| 78 |
} |
| 79 |
|
| 80 |
// During the handshake, an application close should be sanitized. |
| 81 |
#[test] |
| 82 |
fn early_application_close() { |
| 83 |
let mut client = default_client(); |
| 84 |
let mut server = default_server(); |
| 85 |
|
| 86 |
// One flight each. |
| 87 |
let dgram = client.process_output(now()).dgram(); |
| 88 |
assert!(dgram.is_some()); |
| 89 |
let dgram = server.process(dgram, now()).dgram(); |
| 90 |
assert!(dgram.is_some()); |
| 91 |
|
| 92 |
server.close(now(), 77, String::new()); |
| 93 |
assert!(server.state().closed()); |
| 94 |
let dgram = server.process_output(now()).dgram(); |
| 95 |
assert!(dgram.is_some()); |
| 96 |
|
| 97 |
client.process_input(dgram.unwrap(), now()); |
| 98 |
assert_draining(&client, &Error::PeerError(ERROR_APPLICATION_CLOSE)); |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn bad_tls_version() { |
| 103 |
let mut client = default_client(); |
| 104 |
// Do a bad, bad thing. |
| 105 |
client |
| 106 |
.crypto |
| 107 |
.tls_mut() |
| 108 |
.set_option(neqo_crypto::Opt::Tls13CompatMode, true) |
| 109 |
.unwrap(); |
| 110 |
let mut server = default_server(); |
| 111 |
|
| 112 |
let dgram = client.process_output(now()).dgram(); |
| 113 |
let dgram2 = client.process_output(now()).dgram(); |
| 114 |
assert!(dgram.is_some() && dgram2.is_some()); |
| 115 |
server.process_input(dgram.unwrap(), now()); |
| 116 |
let dgram = server.process(dgram2, now()).dgram(); |
| 117 |
assert_eq!( |
| 118 |
*server.state(), |
| 119 |
State::Closed(CloseReason::Transport(Error::ProtocolViolation)) |
| 120 |
); |
| 121 |
assert!(dgram.is_some()); |
| 122 |
client.process_input(dgram.unwrap(), now()); |
| 123 |
assert_draining(&client, &Error::PeerError(Error::ProtocolViolation.code())); |
| 124 |
} |
| 125 |
|
| 126 |
/// Test the interaction between the loss recovery timer |
| 127 |
/// and the closing timer. |
| 128 |
#[test] |
| 129 |
fn closing_timers_interation() { |
| 130 |
let mut client = default_client(); |
| 131 |
let mut server = default_server(); |
| 132 |
connect(&mut client, &mut server); |
| 133 |
|
| 134 |
let mut now = now(); |
| 135 |
|
| 136 |
// We're going to induce time-based loss recovery so that timer is set. |
| 137 |
let _p1 = send_something(&mut client, now); |
| 138 |
let p2 = send_something(&mut client, now); |
| 139 |
let ack = server.process(Some(p2), now).dgram(); |
| 140 |
assert!(ack.is_some()); // This is an ACK. |
| 141 |
|
| 142 |
// After processing the ACK, we should be on the loss recovery timer. |
| 143 |
let cb = client.process(ack, now).callback(); |
| 144 |
assert_ne!(cb, Duration::from_secs(0)); |
| 145 |
now += cb; |
| 146 |
|
| 147 |
// Rather than let the timer pop, close the connection. |
| 148 |
client.close(now, 0, ""); |
| 149 |
let client_close = client.process_output(now).dgram(); |
| 150 |
assert!(client_close.is_some()); |
| 151 |
// This should now report the end of the closing period, not a |
| 152 |
// zero-duration wait driven by the (now defunct) loss recovery timer. |
| 153 |
let client_close_timer = client.process_output(now).callback(); |
| 154 |
assert_ne!(client_close_timer, Duration::from_secs(0)); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn closing_and_draining() { |
| 159 |
const APP_ERROR: AppError = 7; |
| 160 |
let mut client = default_client(); |
| 161 |
let mut server = default_server(); |
| 162 |
connect(&mut client, &mut server); |
| 163 |
|
| 164 |
// Save a packet from the client for later. |
| 165 |
let p1 = send_something(&mut client, now()); |
| 166 |
|
| 167 |
// Close the connection. |
| 168 |
client.close(now(), APP_ERROR, ""); |
| 169 |
let client_close = client.process_output(now()).dgram(); |
| 170 |
assert!(client_close.is_some()); |
| 171 |
let client_close_timer = client.process_output(now()).callback(); |
| 172 |
assert_ne!(client_close_timer, Duration::from_secs(0)); |
| 173 |
// The client will spit out the same packet in response to anything it receives. |
| 174 |
let p3 = send_something(&mut server, now()); |
| 175 |
let client_close2 = client.process(Some(p3), now()).dgram(); |
| 176 |
assert_eq!( |
| 177 |
client_close.as_ref().unwrap().len(), |
| 178 |
client_close2.as_ref().unwrap().len() |
| 179 |
); |
| 180 |
|
| 181 |
// After this time, the client should transition to closed. |
| 182 |
let end = client.process_output(now() + client_close_timer); |
| 183 |
assert_eq!(end, Output::None); |
| 184 |
assert_eq!( |
| 185 |
*client.state(), |
| 186 |
State::Closed(CloseReason::Application(APP_ERROR)) |
| 187 |
); |
| 188 |
|
| 189 |
// When the server receives the close, it too should generate CONNECTION_CLOSE. |
| 190 |
let server_close = server.process(client_close, now()).dgram(); |
| 191 |
assert!(server.state().closed()); |
| 192 |
assert!(server_close.is_some()); |
| 193 |
// .. but it ignores any further close packets. |
| 194 |
let server_close_timer = server.process(client_close2, now()).callback(); |
| 195 |
assert_ne!(server_close_timer, Duration::from_secs(0)); |
| 196 |
// Even a legitimate packet without a close in it. |
| 197 |
let server_close_timer2 = server.process(Some(p1), now()).callback(); |
| 198 |
assert_eq!(server_close_timer, server_close_timer2); |
| 199 |
|
| 200 |
let end = server.process_output(now() + server_close_timer); |
| 201 |
assert_eq!(end, Output::None); |
| 202 |
assert_eq!( |
| 203 |
*server.state(), |
| 204 |
State::Closed(CloseReason::Transport(Error::PeerApplicationError( |
| 205 |
APP_ERROR |
| 206 |
))) |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
/// Test that a client can handle a stateless reset correctly. |
| 211 |
#[test] |
| 212 |
fn stateless_reset_client() { |
| 213 |
let mut client = default_client(); |
| 214 |
let mut server = default_server(); |
| 215 |
server |
| 216 |
.set_local_tparam(StatelessResetToken, TransportParameter::Bytes(vec![77; 16])) |
| 217 |
.unwrap(); |
| 218 |
connect_force_idle(&mut client, &mut server); |
| 219 |
|
| 220 |
client.process_input(datagram(vec![77; 21]), now()); |
| 221 |
assert_draining(&client, &Error::StatelessReset); |
| 222 |
} |