@@ -58,6 +58,8 @@ async fn test_garbled_message_with_invalid_target_comp_id_gets_ignored() {
5858 then ( & mut mock_counterparty) . gets_disconnected ( ) . await ;
5959}
6060
61+ /// Tests that when a counterparty sends a message with an invalid BeginString,
62+ /// the session logs out and disconnects.
6163#[ tokio:: test]
6264async fn test_message_with_invalid_begin_string ( ) {
6365 let ( _session, mut mock_counterparty) = given_an_active_session ( ) . await ;
@@ -77,13 +79,43 @@ async fn test_message_with_invalid_begin_string() {
7779 then ( & mut mock_counterparty) . gets_disconnected ( ) . await ;
7880}
7981
82+ /// Tests that when a counterparty sends a message with an invalid TargetCompId,
83+ /// the session sends a Reject (MsgType=3) and logs out and disconnects.
8084#[ tokio:: test]
8185async fn test_message_with_invalid_target_comp_id ( ) {
8286 let ( _session, mut mock_counterparty) = given_an_active_session ( ) . await ;
8387
84- // a message with incorrect target comp ID is sent by the counterparty
85- let invalid_message = build_execution_report_with_incorrect_target_comp_id (
88+ // a message with incorrect TargetCompId is sent by the counterparty
89+ let invalid_message = build_execution_report_with_comp_id (
8690 mock_counterparty. next_target_sequence_number ( ) ,
91+ COUNTERPARTY_COMP_ID ,
92+ "WRONG_COMP_ID" ,
93+ ) ;
94+ when ( & mut mock_counterparty)
95+ . sends_raw_message ( invalid_message)
96+ . await ;
97+
98+ // then we send a reject, log out and disconnect
99+ then ( & mut mock_counterparty)
100+ . receives ( |msg| assert_eq ! ( msg. header( ) . get:: <& str >( MSG_TYPE ) . unwrap( ) , "3" ) )
101+ . await ;
102+ then ( & mut mock_counterparty)
103+ . receives ( |msg| assert_eq ! ( msg. header( ) . get:: <& str >( MSG_TYPE ) . unwrap( ) , "5" ) )
104+ . await ;
105+ then ( & mut mock_counterparty) . gets_disconnected ( ) . await ;
106+ }
107+
108+ /// Tests that when a counterparty sends a message with an invalid SenderCompId,
109+ /// the session sends a Reject (MsgType=3) and logs out and disconnects.
110+ #[ tokio:: test]
111+ async fn test_message_with_invalid_sender_comp_id ( ) {
112+ let ( _session, mut mock_counterparty) = given_an_active_session ( ) . await ;
113+
114+ // a message with incorrect SenderCompId is sent by the counterparty
115+ let invalid_message = build_execution_report_with_comp_id (
116+ mock_counterparty. next_target_sequence_number ( ) ,
117+ "WRONG_COMP_ID" ,
118+ OUR_COMP_ID ,
87119 ) ;
88120 when ( & mut mock_counterparty)
89121 . sends_raw_message ( invalid_message)
@@ -186,12 +218,16 @@ fn build_execution_report_with_incorrect_begin_string(msg_seq_num: usize) -> Vec
186218 msg. encode ( & Config :: default ( ) ) . unwrap ( )
187219}
188220
189- fn build_execution_report_with_incorrect_target_comp_id ( msg_seq_num : usize ) -> Vec < u8 > {
221+ fn build_execution_report_with_comp_id (
222+ msg_seq_num : usize ,
223+ sender_comp_id : & str ,
224+ target_comp_id : & str ,
225+ ) -> Vec < u8 > {
190226 let report = TestMessage :: dummy_execution_report ( ) ;
191227
192228 let mut msg = Message :: new ( "FIX.4.4" , report. message_type ( ) ) ;
193- msg. set ( fix44:: SENDER_COMP_ID , COUNTERPARTY_COMP_ID ) ;
194- msg. set ( fix44:: TARGET_COMP_ID , "incorrect-comp-id" ) ;
229+ msg. set ( fix44:: SENDER_COMP_ID , sender_comp_id ) ;
230+ msg. set ( fix44:: TARGET_COMP_ID , target_comp_id ) ;
195231 msg. set ( fix44:: MSG_SEQ_NUM , msg_seq_num) ;
196232 msg. set ( fix44:: SENDING_TIME , Timestamp :: utc_now ( ) ) ;
197233
0 commit comments