Index: core/src/main/java/quickfix/Session.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/src/main/java/quickfix/Session.java (revision 1136) +++ core/src/main/java/quickfix/Session.java (revision ) @@ -1585,8 +1585,6 @@ String msgType; try { final Message.Header header = msg.getHeader(); - final String senderCompID = header.getString(SenderCompID.FIELD); - final String targetCompID = header.getString(TargetCompID.FIELD); final Date sendingTime = header.getUtcTimeStamp(SendingTime.FIELD); msgType = header.getString(MsgType.FIELD); int msgSeqNum = 0; @@ -1604,7 +1602,7 @@ return false; } - if (!isCorrectCompID(senderCompID, targetCompID)) { + if (!isCorrectCompID(msg)) { doBadCompID(msg); return false; } @@ -2484,10 +2482,14 @@ } } - private boolean isCorrectCompID(String senderCompID, String targetCompID) { + private boolean isCorrectCompID(Message message) throws FieldNotFound{ + if (!checkCompID) { return true; } + final Header header = message.getHeader(); + final String senderCompID = header.getString(SenderCompID.FIELD); + final String targetCompID = header.getString(TargetCompID.FIELD); return sessionID.getSenderCompID().equals(targetCompID) && sessionID.getTargetCompID().equals(senderCompID); } Index: core/src/test/java/quickfix/SessionTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/src/test/java/quickfix/SessionTest.java (revision 1136) +++ core/src/test/java/quickfix/SessionTest.java (revision ) @@ -26,8 +26,11 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.Test; import quickfix.field.ApplVerID; @@ -1141,4 +1144,86 @@ } } + @Test + public void testTargetCompID() throws Exception + { + TargetCompIDTest sut = new TargetCompIDTest(); + sut.failWhenReceivedLogonWithoutTargetCompID(); + sut.succeedWhenReceivedLogonWithoutTargetCompID(); + } + + private class TargetCompIDTest { + public void receiveLogon(final boolean checkCompID) + { + SessionSettings settings = new SessionSettings(); + settings.setString(SessionFactory.SETTING_CONNECTION_TYPE, SessionFactory.INITIATOR_CONNECTION_TYPE); + settings.setLong(Session.SETTING_HEARTBTINT, 5); + settings.setString(Session.SETTING_REJECT_INVALID_MESSAGE, "N"); + settings.setString(Session.SETTING_VALIDATE_FIELDS_OUT_OF_ORDER, "N"); + settings.setString(Session.SETTING_START_TIME, "00:00:00"); + settings.setString(Session.SETTING_END_TIME, "23:59:59"); + settings.setString(Session.SETTING_CHECK_LATENCY, "N"); + settings.setBool(Session.SETTING_CHECK_COMP_ID, checkCompID); + // + final CountDownLatch latch = new CountDownLatch(1); + final SessionID sessionID = new SessionID("FIX.4.0", "user", "TARGET"); + LogFactory logFactory = mock(LogFactory.class); + Log log = mock(Log.class); + stub(logFactory.create(sessionID)).toReturn(log); + SessionFactory factory = new DefaultSessionFactory( + new ApplicationAdapter() + { + @Override + public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon + { + if (checkCompID) { + Assert.fail("Unexpected invocation"); + } else { + Assert.assertEquals("Unexpected SessionID", sessionID, sessionId); + Assert.assertEquals(270109, message.getHeader().getInt(MsgSeqNum.FIELD)); + latch.countDown(); + } + } + }, + new MemoryStoreFactory(), + new CompositeLogFactory(new LogFactory[]{ + new ScreenLogFactory(true, true, true), + logFactory + }) + ); + try { + Session session = factory.create(sessionID, settings); + Assert.assertNotNull(session); + Message logonResponse = MessageUtils.parse(session, "8=FIX.4.0|9=129|35=A|49=user|34=270109|5006=36|50=GW1|43=N|52=20140220-19:00:01|122=20140220-19:00:01|108=1|95=29|96=TraderID=user|Password=secret|10=36|".replace('|', '\001')); + Assert.assertNotNull(logonResponse); + // + getSessionState(session).setLogonSent(true); + session.next(logonResponse); + if (checkCompID) { + verify(logFactory).create(sessionID); + verify(log).onErrorEvent("Warn: incoming message with missing field: 56: Required tag missing, field=56: " + logonResponse); + verify(log).onErrorEvent("Rejecting invalid message: quickfix.FieldNotFound: Field [56] was not found in message.: " + logonResponse); + verify(log).onErrorEvent("Required field missing from logon"); + verify(log).onEvent("Already disconnected: Required field missing from logon"); + } else { + if (!latch.await(5, TimeUnit.SECONDS)) { + Assert.fail("Failed to process Logon within 5 sec"); + } + } + } catch (Exception e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + } + + public void failWhenReceivedLogonWithoutTargetCompID() + { + receiveLogon(true); + } + + public void succeedWhenReceivedLogonWithoutTargetCompID() + { + receiveLogon(false); + } + } }