11// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4+ use super :: { client:: tokio:: Client as ClientTokio , server:: tokio:: Server as ServerTokio } ;
45use crate :: {
56 either:: Either ,
67 event:: { self , testing} ,
78 path:: secret,
9+ psk:: { client:: Provider as ClientProvider , server:: Provider as ServerProvider } ,
810 stream:: {
911 application, client as stream_client,
1012 environment:: { bach, tokio, udp, Environment } ,
11- recv, send,
1213 server:: { self as stream_server, accept, stats} ,
1314 socket:: Protocol ,
1415 } ,
16+ testing:: NoopSubscriber ,
1517} ;
1618use s2n_quic_core:: dc:: { self , ApplicationParams } ;
1719use s2n_quic_platform:: socket;
@@ -31,8 +33,6 @@ thread_local! {
3133pub type Subscriber = ( Arc < event:: testing:: Subscriber > , event:: tracing:: Subscriber ) ;
3234
3335pub type Stream = application:: Stream < Subscriber > ;
34- pub type Writer = send:: application:: Writer < Subscriber > ;
35- pub type Reader = recv:: application:: Reader < Subscriber > ;
3636
3737const DEFAULT_POOLED : bool = true ;
3838
@@ -47,6 +47,264 @@ pub(crate) const MAX_DATAGRAM_SIZE: u16 = if cfg!(target_os = "linux") {
4747
4848type Env = Either < tokio:: Environment < Subscriber > , bach:: Environment < Subscriber > > ;
4949
50+ pub fn bind_pair (
51+ protocol : Protocol ,
52+ server_addr : SocketAddr ,
53+ client : ClientProvider ,
54+ server : ServerProvider ,
55+ ) -> (
56+ ClientTokio < ClientProvider , NoopSubscriber > ,
57+ ServerTokio < ServerProvider , NoopSubscriber > ,
58+ ) {
59+ let test_subscriber = NoopSubscriber { } ;
60+ let client = ClientTokio :: < ClientProvider , NoopSubscriber > :: builder ( )
61+ . with_default_protocol ( protocol)
62+ . build ( client, test_subscriber. clone ( ) )
63+ . unwrap ( ) ;
64+
65+ let server = ServerTokio :: < ServerProvider , NoopSubscriber > :: builder ( )
66+ . with_address ( server_addr)
67+ . with_protocol ( protocol)
68+ . with_workers ( 1 . try_into ( ) . unwrap ( ) )
69+ . build ( server, test_subscriber)
70+ . unwrap ( ) ;
71+
72+ ( client, server)
73+ }
74+
75+ macro_rules! check_pair_addrs {
76+ ( $local: ident, $peer: ident) => {
77+ debug_assert_eq!(
78+ $local. local_addr( ) . ok( ) . map( |addr| addr. port( ) ) ,
79+ $peer. peer_addr( ) . ok( ) . map( |addr| addr. port( ) )
80+ ) ;
81+ } ;
82+ }
83+
84+ macro_rules! dcquic_context {
85+ ( $protocol: ident) => {
86+ use super :: Protocol ;
87+ use std:: net:: SocketAddr ;
88+
89+ pub type Stream = crate :: stream:: application:: Stream <super :: NoopSubscriber >;
90+
91+ pub struct Context ( super :: Context ) ;
92+
93+ #[ allow( dead_code) ]
94+ impl Context {
95+ pub async fn new( ) -> Self {
96+ Self ( super :: Context :: new( Protocol :: $protocol) . await )
97+ }
98+
99+ pub fn new_sync( addr: SocketAddr ) -> Self {
100+ Self ( super :: Context :: new_sync( Protocol :: $protocol, addr) )
101+ }
102+
103+ pub async fn bind( addr: SocketAddr ) -> Self {
104+ Self ( super :: Context :: bind( Protocol :: $protocol, addr) . await )
105+ }
106+
107+ pub fn acceptor_addr( & self ) -> SocketAddr {
108+ self . 0 . acceptor_addr( )
109+ }
110+
111+ pub fn handshake_addr( & self ) -> SocketAddr {
112+ self . 0 . handshake_addr( )
113+ }
114+
115+ pub async fn pair( & self ) -> ( Stream , Stream ) {
116+ self . 0 . pair( ) . await
117+ }
118+
119+ pub async fn pair_with( & self , acceptor_addr: SocketAddr ) -> ( Stream , Stream ) {
120+ self . 0 . pair_with( acceptor_addr) . await
121+ }
122+
123+ pub fn protocol( & self ) -> Protocol {
124+ self . 0 . protocol
125+ }
126+ }
127+ } ;
128+ }
129+
130+ pub mod dcquic {
131+ use crate :: {
132+ psk:: { client:: Provider as ClientProvider , server:: Provider as ServerProvider } ,
133+ stream:: {
134+ client:: tokio:: Client as ClientTokio ,
135+ server:: tokio:: Server as ServerTokio ,
136+ socket:: Protocol ,
137+ testing:: { bind_pair, NoopSubscriber } ,
138+ } ,
139+ testing:: server_name,
140+ } ;
141+ use std:: net:: SocketAddr ;
142+
143+ pub type Stream = crate :: stream:: application:: Stream < NoopSubscriber > ;
144+
145+ pub mod tcp {
146+ dcquic_context ! ( Tcp ) ;
147+ }
148+
149+ pub mod udp {
150+ dcquic_context ! ( Udp ) ;
151+ }
152+
153+ pub struct Context {
154+ pub ( crate ) server : ServerTokio < ServerProvider , NoopSubscriber > ,
155+ pub ( crate ) client : ClientTokio < ClientProvider , NoopSubscriber > ,
156+ protocol : Protocol ,
157+ }
158+
159+ impl Context {
160+ pub async fn new ( protocol : Protocol ) -> Self {
161+ if protocol. is_udp ( ) {
162+ Self :: bind ( protocol, "[::1]:0" . parse ( ) . unwrap ( ) ) . await
163+ } else {
164+ Self :: bind ( protocol, "127.0.0.1:0" . parse ( ) . unwrap ( ) ) . await
165+ }
166+ }
167+
168+ pub fn new_sync ( protocol : Protocol , addr : SocketAddr ) -> Self {
169+ let ( client, server) = crate :: testing:: pair_sync ( ) ;
170+ let ( client, server) = bind_pair ( protocol, addr, client, server) ;
171+ Self {
172+ client,
173+ server,
174+ protocol,
175+ }
176+ }
177+
178+ pub async fn bind ( protocol : Protocol , addr : SocketAddr ) -> Self {
179+ Self :: bind_with ( protocol, addr, crate :: testing:: Pair :: default ( ) ) . await
180+ }
181+
182+ pub async fn bind_with (
183+ protocol : Protocol ,
184+ addr : SocketAddr ,
185+ pair : crate :: testing:: Pair ,
186+ ) -> Self {
187+ let ( client, server) = pair. build ( ) . await ;
188+ let ( client, server) = bind_pair ( protocol, addr, client, server) ;
189+ Self {
190+ client,
191+ server,
192+ protocol,
193+ }
194+ }
195+
196+ pub fn acceptor_addr ( & self ) -> SocketAddr {
197+ self . server . acceptor_addr ( ) . expect ( "acceptor_addr" )
198+ }
199+
200+ pub fn handshake_addr ( & self ) -> SocketAddr {
201+ self . server . handshake_addr ( ) . expect ( "handshake_addr" )
202+ }
203+
204+ pub async fn pair ( & self ) -> ( Stream , Stream ) {
205+ self . pair_with ( self . acceptor_addr ( ) ) . await
206+ }
207+
208+ pub async fn pair_with ( & self , acceptor_addr : SocketAddr ) -> ( Stream , Stream ) {
209+ let handshake_addr = self . handshake_addr ( ) ;
210+ let ( client, server) = tokio:: join!(
211+ async move {
212+ self . client
213+ . connect( handshake_addr, acceptor_addr, server_name( ) )
214+ . await
215+ . expect( "connect" )
216+ } ,
217+ async move {
218+ let ( conn, _) = self . server. accept( ) . await . expect( "accept" ) ;
219+ conn
220+ }
221+ ) ;
222+
223+ check_pair_addrs ! ( client, server) ;
224+
225+ // the client doesn't have a response from the server so it might not know its
226+ // port yet
227+ if self . protocol ( ) . is_udp ( ) {
228+ let acceptor_port = acceptor_addr. port ( ) ;
229+ let server_local_port = server. local_addr ( ) . unwrap ( ) . port ( ) ;
230+ let client_peer_port = client. peer_addr ( ) . unwrap ( ) . port ( ) ;
231+ assert ! (
232+ [ acceptor_port, server_local_port] . contains( & client_peer_port) ,
233+ "acceptor_port={acceptor_port}, server_local_port={server_local_port}, client_peer_port={client_peer_port}"
234+ ) ;
235+ } else {
236+ check_pair_addrs ! ( server, client) ;
237+ }
238+
239+ ( client, server)
240+ }
241+
242+ pub fn protocol ( & self ) -> Protocol {
243+ self . protocol
244+ }
245+
246+ #[ allow( dead_code) ]
247+ pub fn split (
248+ self ,
249+ ) -> (
250+ ClientTokio < ClientProvider , NoopSubscriber > ,
251+ ServerTokio < ServerProvider , NoopSubscriber > ,
252+ ) {
253+ ( self . client , self . server )
254+ }
255+ }
256+ }
257+
258+ pub mod tcp {
259+ use super :: Protocol ;
260+ use std:: net:: SocketAddr ;
261+ use tokio:: net:: { TcpListener , TcpStream } ;
262+
263+ pub type Stream = TcpStream ;
264+
265+ pub struct Context {
266+ acceptor : TcpListener ,
267+ }
268+
269+ impl Context {
270+ pub async fn new ( ) -> Self {
271+ Self :: bind ( "127.0.0.1:0" . parse ( ) . unwrap ( ) ) . await
272+ }
273+
274+ pub async fn bind ( addr : SocketAddr ) -> Self {
275+ let acceptor = TcpListener :: bind ( addr) . await . expect ( "bind" ) ;
276+ Self { acceptor }
277+ }
278+
279+ pub fn acceptor_addr ( & self ) -> SocketAddr {
280+ self . acceptor . local_addr ( ) . expect ( "acceptor_addr" )
281+ }
282+
283+ pub async fn pair ( & self ) -> ( Stream , Stream ) {
284+ self . pair_with ( self . acceptor_addr ( ) ) . await
285+ }
286+
287+ pub async fn pair_with ( & self , acceptor_addr : SocketAddr ) -> ( Stream , Stream ) {
288+ let ( client, server) = tokio:: join!(
289+ async move { Stream :: connect( acceptor_addr) . await . expect( "connect" ) } ,
290+ async move {
291+ let ( conn, _addr) = self . acceptor. accept( ) . await . expect( "accept" ) ;
292+ conn
293+ }
294+ ) ;
295+
296+ check_pair_addrs ! ( client, server) ;
297+ check_pair_addrs ! ( server, client) ;
298+
299+ ( client, server)
300+ }
301+
302+ pub fn protocol ( & self ) -> Protocol {
303+ Protocol :: Tcp
304+ }
305+ }
306+ }
307+
50308#[ derive( Clone ) ]
51309pub struct Client {
52310 map : secret:: Map ,
0 commit comments