| 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 |
#![allow(clippy::allow_attributes, dead_code, reason = "Exported.")] |
| 8 |
|
| 9 |
use std::{cell::RefCell, rc::Rc}; |
| 10 |
|
| 11 |
use neqo_common::{event::Provider as _, IpTosDscp}; |
| 12 |
use neqo_crypto::{AllowZeroRtt, AuthenticationStatus, ResumptionToken}; |
| 13 |
use neqo_transport::{ |
| 14 |
server::{ConnectionRef, Server, ValidateAddress}, |
| 15 |
Connection, ConnectionEvent, ConnectionParameters, State, Stats, |
| 16 |
}; |
| 17 |
use test_fixture::{default_client, now, CountingConnectionIdGenerator}; |
| 18 |
|
| 19 |
/// # Panics |
| 20 |
/// |
| 21 |
/// When the count of received packets doesn't match the count of received packets with the |
| 22 |
/// (default) DSCP. |
| 23 |
pub fn assert_dscp(stats: &Stats) { |
| 24 |
assert_eq!(stats.dscp_rx[IpTosDscp::Cs0], stats.packets_rx); |
| 25 |
} |
| 26 |
|
| 27 |
/// Create a server. This is different than the one in the fixture, which is a single connection. |
| 28 |
pub fn new_server(params: ConnectionParameters) -> Server { |
| 29 |
Server::new( |
| 30 |
now(), |
| 31 |
test_fixture::DEFAULT_KEYS, |
| 32 |
test_fixture::DEFAULT_ALPN, |
| 33 |
test_fixture::anti_replay(), |
| 34 |
Box::new(AllowZeroRtt {}), |
| 35 |
Rc::new(RefCell::new(CountingConnectionIdGenerator::default())), |
| 36 |
params, |
| 37 |
) |
| 38 |
.expect("should create a server") |
| 39 |
} |
| 40 |
|
| 41 |
/// Create a server. This is different than the one in the fixture, which is a single connection. |
| 42 |
pub fn default_server() -> Server { |
| 43 |
new_server(ConnectionParameters::default()) |
| 44 |
} |
| 45 |
|
| 46 |
// Check that there is at least one connection. Returns a ref to the first confirmed connection. |
| 47 |
pub fn connected_server(server: &Server) -> ConnectionRef { |
| 48 |
#[expect( |
| 49 |
clippy::mutable_key_type, |
| 50 |
reason = "ActiveConnectionRef::Hash doesn't access any of the interior mutable types." |
| 51 |
)] |
| 52 |
let server_connections = server.active_connections(); |
| 53 |
// Find confirmed connections. There should only be one. |
| 54 |
let mut confirmed = server_connections |
| 55 |
.iter() |
| 56 |
.filter(|c: &&ConnectionRef| *c.borrow().state() == State::Confirmed); |
| 57 |
let c = confirmed.next().expect("one confirmed"); |
| 58 |
c.clone() |
| 59 |
} |
| 60 |
|
| 61 |
/// Connect. This returns a reference to the server connection. |
| 62 |
pub fn connect(client: &mut Connection, server: &mut Server) -> ConnectionRef { |
| 63 |
server.set_validation(ValidateAddress::Never); |
| 64 |
|
| 65 |
assert_eq!(*client.state(), State::Init); |
| 66 |
let out = client.process_output(now()); // ClientHello |
| 67 |
let out2 = client.process_output(now()); // ClientHello |
| 68 |
assert!(out.as_dgram_ref().is_some() && out2.as_dgram_ref().is_some()); |
| 69 |
_ = server.process(out.dgram(), now()); // ACK |
| 70 |
let out = server.process(out2.dgram(), now()); // ServerHello... |
| 71 |
assert!(out.as_dgram_ref().is_some()); |
| 72 |
|
| 73 |
// Ingest the server Certificate. |
| 74 |
let out = client.process(out.dgram(), now()); |
| 75 |
assert!(out.as_dgram_ref().is_some()); // This should just be an ACK. |
| 76 |
let out = server.process(out.dgram(), now()); |
| 77 |
let out = client.process(out.dgram(), now()); |
| 78 |
let out = server.process(out.dgram(), now()); |
| 79 |
assert!(out.as_dgram_ref().is_none()); // So the server should have nothing to say. |
| 80 |
|
| 81 |
// Now mark the server as authenticated. |
| 82 |
client.authenticated(AuthenticationStatus::Ok, now()); |
| 83 |
let out = client.process_output(now()); |
| 84 |
assert!(out.as_dgram_ref().is_some()); |
| 85 |
assert_eq!(*client.state(), State::Connected); |
| 86 |
let out = server.process(out.dgram(), now()); |
| 87 |
assert!(out.as_dgram_ref().is_some()); // ACK + HANDSHAKE_DONE + NST |
| 88 |
|
| 89 |
// Have the client process the HANDSHAKE_DONE. |
| 90 |
let out = client.process(out.dgram(), now()); |
| 91 |
assert!(out.as_dgram_ref().is_none()); |
| 92 |
assert_eq!(*client.state(), State::Confirmed); |
| 93 |
assert_dscp(&client.stats()); |
| 94 |
connected_server(server) |
| 95 |
} |
| 96 |
|
| 97 |
#[cfg(test)] |
| 98 |
/// Scrub through client events to find a resumption token. |
| 99 |
pub fn find_ticket(client: &mut Connection) -> ResumptionToken { |
| 100 |
client |
| 101 |
.events() |
| 102 |
.find_map(|e| { |
| 103 |
if let ConnectionEvent::ResumptionToken(token) = e { |
| 104 |
Some(token) |
| 105 |
} else { |
| 106 |
None |
| 107 |
} |
| 108 |
}) |
| 109 |
.unwrap() |
| 110 |
} |
| 111 |
|
| 112 |
#[cfg(test)] |
| 113 |
/// Connect to the server and have it generate a ticket. |
| 114 |
pub fn generate_ticket(server: &mut Server) -> ResumptionToken { |
| 115 |
let mut client = default_client(); |
| 116 |
let server_conn = connect(&mut client, server); |
| 117 |
|
| 118 |
server_conn.borrow_mut().send_ticket(now(), &[]).unwrap(); |
| 119 |
let out = server.process_output(now()); |
| 120 |
client.process_input(out.dgram().unwrap(), now()); // Consume ticket, ignore output. |
| 121 |
let ticket = find_ticket(&mut client); |
| 122 |
|
| 123 |
// Have the client close the connection and then let the server clean up. |
| 124 |
client.close(now(), 0, "got a ticket"); |
| 125 |
let out = client.process_output(now()); |
| 126 |
drop(server.process(out.dgram(), now())); |
| 127 |
// Calling active_connections clears the set of active connections. |
| 128 |
assert_eq!(server.active_connections().len(), 1); |
| 129 |
ticket |
| 130 |
} |