Skip to content

Commit 0ef2ac4

Browse files
harinda05harindachrjohn
authored
Validate Logon message before acting upon ResetMsgSeqNum flag (#549)
* [Issue-474] - ResetMsgSeqNum flag in Logon message is processed prior to application level logon message validation fix * [Issue-474] - testcase * Corrected usage of `resetSeqNumFlag` in `logonTo` Co-authored-by: harinda <harinda@yaalalabs.com> Co-authored-by: Christoph John <christoph.john@macd.com>
1 parent 71811d0 commit 0ef2ac4

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

quickfixj-core/src/main/java/quickfix/Session.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,12 +1815,14 @@ private boolean verify(Message msg, boolean checkTooHigh, boolean checkTooLow)
18151815
return false;
18161816
}
18171817

1818-
if (checkTooHigh && isTargetTooHigh(msgSeqNum)) {
1819-
doTargetTooHigh(msg);
1820-
return false;
1821-
} else if (checkTooLow && isTargetTooLow(msgSeqNum)) {
1822-
doTargetTooLow(msg);
1823-
return false;
1818+
if(!state.isResetReceived()){
1819+
if (checkTooHigh && isTargetTooHigh(msgSeqNum)) {
1820+
doTargetTooHigh(msg);
1821+
return false;
1822+
} else if (checkTooLow && isTargetTooLow(msgSeqNum)) {
1823+
doTargetTooLow(msg);
1824+
return false;
1825+
}
18241826
}
18251827

18261828
// Handle poss dup where msgSeq is as expected
@@ -2174,6 +2176,10 @@ private void nextLogon(Message logon) throws FieldNotFound, RejectLogon, Incorre
21742176
state.setResetReceived(true);
21752177
}
21762178

2179+
if (!verify(logon, false, validateSequenceNumbers)) {
2180+
return;
2181+
}
2182+
21772183
if (state.isResetReceived()) {
21782184
getLog().onEvent("Logon contains ResetSeqNumFlag=Y, resetting sequence numbers to 1");
21792185
if (!state.isResetSent()) {
@@ -2190,9 +2196,6 @@ private void nextLogon(Message logon) throws FieldNotFound, RejectLogon, Incorre
21902196
resetState();
21912197
}
21922198

2193-
if (!verify(logon, false, validateSequenceNumbers)) {
2194-
return;
2195-
}
21962199

21972200
// reset logout messages
21982201
state.setLogoutReceived(false);

quickfixj-core/src/test/java/quickfix/SessionTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import quickfix.field.Text;
2828
import quickfix.field.converter.UtcTimeOnlyConverter;
2929
import quickfix.field.converter.UtcTimestampConverter;
30+
import quickfix.field.ResetSeqNumFlag;
3031
import quickfix.fix44.Heartbeat;
3132
import quickfix.fix44.Logon;
3233
import quickfix.fix44.Logout;
@@ -2761,6 +2762,83 @@ public void initiatorSession_ShouldWaitForLogoutTimeoutBeforeDisconnecting_WhenS
27612762
assertFor244(application, responder, 1, 0, 1, true);
27622763
}
27632764

2765+
/**
2766+
* https://github.com/quickfix-j/quickfixj/issues/474
2767+
* */
2768+
@Test
2769+
public void msgseqnum_getting_reset_in_rejected_logon_scenario_fix() throws Exception {
2770+
2771+
final UnitTestApplication application = new UnitTestApplication(){
2772+
int logonCount = 0;
2773+
@Override
2774+
public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound,
2775+
IncorrectDataFormat, IncorrectTagValue, RejectLogon {
2776+
super.fromAdmin(message, sessionId);
2777+
if (message.getHeader().getString(MsgType.FIELD).equals(Logon.MSGTYPE)) {
2778+
logonCount += 1;
2779+
}
2780+
if (logonCount == 2) {
2781+
logonCount += 1;
2782+
throw new RejectLogon("RejectLogon");
2783+
}
2784+
}
2785+
};
2786+
2787+
final SessionID sessionID = new SessionID(
2788+
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
2789+
2790+
Session initSession = createSession(sessionID, application, true,
2791+
true, true);
2792+
final SessionState state = getSessionState(initSession);
2793+
2794+
final UnitTestResponder responder = new UnitTestResponder();
2795+
2796+
try{
2797+
initSession.setResponder(responder);
2798+
initSession.next();
2799+
logonTo(initSession);
2800+
2801+
Message logon = application.toAdminMessages.get(0);
2802+
assertEquals(MsgType.LOGON, logon.getHeader().getString(MsgType.FIELD));
2803+
2804+
2805+
assertEquals(2, state.getNextSenderMsgSeqNum());
2806+
assertTrue(initSession.isLoggedOn());
2807+
2808+
initSession.generateHeartbeat();
2809+
initSession.next(createHeartbeatMessage(2));
2810+
assertEquals(3, state.getNextSenderMsgSeqNum());
2811+
2812+
initSession.generateHeartbeat();
2813+
initSession.next(createHeartbeatMessage(3));
2814+
assertEquals(4, state.getNextSenderMsgSeqNum());
2815+
2816+
initSession.logout();
2817+
initSession.next();
2818+
2819+
assertEquals(5, state.getNextSenderMsgSeqNum());
2820+
assertEquals(4, state.getNextTargetMsgSeqNum());
2821+
2822+
2823+
} catch (Exception e) {
2824+
throw new Exception(e);}
2825+
2826+
try {
2827+
initSession.logon();
2828+
2829+
// logon message which will get rejected
2830+
initSession.next();
2831+
logonTo(initSession,1,true);
2832+
2833+
} catch (Exception e){
2834+
throw e;
2835+
}
2836+
2837+
assertEquals(6, state.getNextSenderMsgSeqNum());
2838+
assertEquals(4, state.getNextTargetMsgSeqNum());
2839+
2840+
}
2841+
27642842
/**
27652843
* https://github.com/quickfix-j/quickfixj/issues/244
27662844
* */
@@ -2945,6 +3023,18 @@ private void logonTo(Session session, int sequence) throws FieldNotFound,
29453023
session.next(receivedLogon);
29463024
}
29473025

3026+
private void logonTo(Session session, int sequence, boolean resetSeqNumFlag) throws FieldNotFound,
3027+
RejectLogon, IncorrectDataFormat, IncorrectTagValue,
3028+
UnsupportedMessageType, IOException, InvalidMessage {
3029+
final Logon receivedLogon = new Logon();
3030+
setUpHeader(session.getSessionID(), receivedLogon, true, sequence);
3031+
receivedLogon.setInt(HeartBtInt.FIELD, 30);
3032+
receivedLogon.setInt(EncryptMethod.FIELD, 0);
3033+
receivedLogon.setBoolean(ResetSeqNumFlag.FIELD, resetSeqNumFlag);
3034+
receivedLogon.toString(); // calculate length and checksum
3035+
session.next(receivedLogon);
3036+
}
3037+
29483038
private void logoutFrom(Session session, int sequence)
29493039
throws FieldNotFound, RejectLogon, IncorrectDataFormat,
29503040
IncorrectTagValue, UnsupportedMessageType, IOException,

0 commit comments

Comments
 (0)