@@ -154,6 +154,88 @@ fn hello_world_happy_path() {
154154 assert_eq ! ( message, decrypted_message[ 1 ..] . to_vec( ) ) ; // Skip header byte
155155}
156156
157+ #[ test]
158+ #[ cfg( feature = "std" ) ]
159+ fn pingpong_with_closed_connection_sync ( ) {
160+ use bip324:: io:: { Payload , Protocol } ;
161+ use bitcoin:: consensus;
162+ use p2p:: message:: { NetworkMessage , V2NetworkMessage } ;
163+ use std:: net:: { TcpListener , TcpStream } ;
164+ use std:: thread;
165+
166+ let listener = TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
167+ let addr = listener. local_addr ( ) . unwrap ( ) ;
168+
169+ let server = thread:: spawn ( move || {
170+ let ( stream, _) = listener. accept ( ) . unwrap ( ) ;
171+ let reader = stream. try_clone ( ) . unwrap ( ) ;
172+ let writer = stream;
173+
174+ let mut protocol = Protocol :: new (
175+ p2p:: Magic :: REGTEST ,
176+ bip324:: Role :: Responder ,
177+ None ,
178+ None ,
179+ reader,
180+ writer,
181+ )
182+ . expect ( "Failed to create protocol" ) ;
183+
184+ // Read one message
185+ let payload = protocol. read ( ) . expect ( "Failed to read payload" ) ;
186+ let received_message = consensus:: deserialize :: < V2NetworkMessage > ( payload. contents ( ) )
187+ . expect ( "Failed to deserialize" ) ;
188+
189+ if let NetworkMessage :: Ping ( x) = received_message. payload ( ) {
190+ let pong = V2NetworkMessage :: new ( NetworkMessage :: Pong ( * x) ) ;
191+ let message = consensus:: serialize ( & pong) ;
192+ protocol
193+ . write ( & Payload :: genuine ( message) )
194+ . expect ( "Failed to write pong" ) ;
195+ println ! ( "Pong sent, stopping server." ) ;
196+ } else {
197+ panic ! ( "Expected Ping, but received: {received_message:?}" ) ;
198+ }
199+ } ) ;
200+
201+ let stream = TcpStream :: connect ( addr) . unwrap ( ) ;
202+ let reader = stream. try_clone ( ) . unwrap ( ) ;
203+ let writer = stream;
204+
205+ println ! ( "Starting sync BIP-324 handshake" ) ;
206+ let mut protocol = Protocol :: new (
207+ p2p:: Magic :: REGTEST ,
208+ bip324:: Role :: Initiator ,
209+ None ,
210+ None ,
211+ reader,
212+ writer,
213+ )
214+ . expect ( "Failed to create protocol" ) ;
215+
216+ println ! ( "Sending Ping using sync Protocol::write()" ) ;
217+ let ping = V2NetworkMessage :: new ( NetworkMessage :: Ping ( 45324 ) ) ;
218+ let message = consensus:: serialize ( & ping) ;
219+ protocol
220+ . write ( & Payload :: genuine ( message) )
221+ . expect ( "Failed to write ping" ) ;
222+
223+ println ! ( "Reading response using sync Protocol::read()" ) ;
224+ let payload = protocol. read ( ) . expect ( "Failed to read response" ) ;
225+ let response_message = consensus:: deserialize :: < V2NetworkMessage > ( payload. contents ( ) )
226+ . expect ( "Failed to deserialize response" ) ;
227+
228+ assert_eq ! ( NetworkMessage :: Pong ( 45324 ) , * response_message. payload( ) ) ;
229+
230+ println ! ( "Successfully ping-pong'ed using sync Protocol API!" ) ;
231+ server. join ( ) . unwrap ( ) ;
232+
233+ println ! (
234+ "Trying to read another message from the server, while the connection is already closed."
235+ ) ;
236+ assert ! ( protocol. read( ) . is_err( ) ) ;
237+ }
238+
157239#[ tokio:: test]
158240#[ cfg( feature = "tokio" ) ]
159241async fn pingpong_with_closed_connection_async ( ) {
0 commit comments