|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use super::*; |
| 5 | +use s2n_quic_core::crypto::tls::ConnectionInfo; |
| 6 | +use s2n_tls::{ |
| 7 | + callbacks::{ClientHelloCallback, ConnectionFuture}, |
| 8 | + error::Error as S2nError, |
| 9 | +}; |
| 10 | +use std::{ |
| 11 | + pin::Pin, |
| 12 | + sync::{Arc, Mutex}, |
| 13 | +}; |
| 14 | + |
| 15 | +struct TestClientHelloHandle { |
| 16 | + // The ClientHelloCallback trait requires `&self` as a immutable reference. |
| 17 | + // We use Arc<Mutex<>> to enable interior mutability - allowing us to mutate the recorded |
| 18 | + // ConnectionInfo through an immutable reference. |
| 19 | + recorded_info: Arc<Mutex<Option<ConnectionInfo>>>, |
| 20 | +} |
| 21 | + |
| 22 | +impl TestClientHelloHandle { |
| 23 | + pub fn new(recorded_info: Arc<Mutex<Option<ConnectionInfo>>>) -> Self { |
| 24 | + Self { recorded_info } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl ClientHelloCallback for TestClientHelloHandle { |
| 29 | + fn on_client_hello( |
| 30 | + &self, |
| 31 | + connection: &mut s2n_tls::connection::Connection, |
| 32 | + ) -> Result<Option<Pin<Box<dyn ConnectionFuture>>>, S2nError> { |
| 33 | + let connection_info = connection.application_context::<ConnectionInfo>(); |
| 34 | + |
| 35 | + assert!(connection_info.is_some()); |
| 36 | + if let Some(info) = connection_info { |
| 37 | + *self.recorded_info.lock().unwrap() = Some(*info); |
| 38 | + } |
| 39 | + |
| 40 | + Ok(None) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Tests that ConnectionInfo is accessible in the client hello callback and contains |
| 45 | +/// the correct local (server) and remote (client) socket addresses. |
| 46 | +/// |
| 47 | +/// This test: |
| 48 | +/// 1. Creates a server with a client hello callback that records ConnectionInfo |
| 49 | +/// 2. Records the actual server and client socket addresses during connection setup |
| 50 | +/// 3. Verifies that the ConnectionInfo captured in the callback matches the expected addresses |
| 51 | +/// |
| 52 | +/// Note: Uses interior mutability (Arc<Mutex<>>) to store data from the callback since |
| 53 | +/// ClientHelloCallback requires an immutable reference (&self). |
| 54 | +#[test] |
| 55 | +#[cfg_attr(miri, ignore)] |
| 56 | +fn ch_callback_connection_info_test() { |
| 57 | + let model = Model::default(); |
| 58 | + |
| 59 | + let ch_callback_handle_inner = Arc::new(Mutex::new(None)); |
| 60 | + let ch_callback_handle_inner_clone = ch_callback_handle_inner.clone(); |
| 61 | + |
| 62 | + let mut server_local_address = None; |
| 63 | + let mut server_remote_address = None; |
| 64 | + |
| 65 | + test(model.clone(), |handle| { |
| 66 | + let server_tls = tls::s2n_tls::Server::builder() |
| 67 | + .with_certificate(certificates::CERT_PEM, certificates::KEY_PEM) |
| 68 | + .unwrap() |
| 69 | + .with_client_hello_handler(TestClientHelloHandle::new(ch_callback_handle_inner_clone)) |
| 70 | + .unwrap() |
| 71 | + .build() |
| 72 | + .unwrap(); |
| 73 | + |
| 74 | + let server = Server::builder() |
| 75 | + .with_io(handle.builder().build()?)? |
| 76 | + .with_tls(server_tls)? |
| 77 | + .with_event(tracing_events(true, model.clone()))? |
| 78 | + .with_random(Random::with_seed(456))? |
| 79 | + .start()?; |
| 80 | + |
| 81 | + let server_addr = start_server(server)?; |
| 82 | + server_local_address = Some(server_addr); |
| 83 | + |
| 84 | + let client = build_client(handle, model.clone(), true)?; |
| 85 | + server_remote_address = Some(client.local_addr().unwrap()); |
| 86 | + |
| 87 | + start_client(client, server_addr, Data::new(1000))?; |
| 88 | + |
| 89 | + Ok(server_addr) |
| 90 | + }) |
| 91 | + .unwrap(); |
| 92 | + |
| 93 | + let connection_info = ch_callback_handle_inner.lock().unwrap().unwrap(); |
| 94 | + |
| 95 | + // Verify that the ConnectionInfo contains the exact server local address |
| 96 | + assert_eq!(connection_info.local_address, server_local_address.unwrap()); |
| 97 | + |
| 98 | + // Verify that the ConnectionInfo contains the exact server's remote address (client's local address) |
| 99 | + assert_eq!( |
| 100 | + connection_info.remote_address, |
| 101 | + server_remote_address.unwrap() |
| 102 | + ); |
| 103 | +} |
0 commit comments