Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,18 @@ const (
// - N
ValidateFieldsHaveValues string = "ValidateFieldsHaveValues"

// ValidateChecksum if set to N, the checksum verification of incoming messages will be skipped.
// Useful when dealing with legacy systems or proxies that might malform the checksum.
//
// Required: No
//
// Default: Y
//
// Valid Values:
// - Y
// - N
ValidateChecksum string = "ValidateChecksum"

// CheckLatency if set to Y, messages must be received from the counter-party within a defined number of seconds.
// It is useful to turn this off if a system uses localtime for it's timestamps instead of GMT.
//
Expand Down
3 changes: 2 additions & 1 deletion in_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ func (state inSession) resendMessages(session *session, beginSeqNo, endSeqNo int
nextSeqNum := seqNum
msg := NewMessage()
err := session.store.IterateMessages(beginSeqNo, endSeqNo, func(msgBytes []byte) error {
err := ParseMessageWithDataDictionary(msg, bytes.NewBuffer(msgBytes), session.transportDataDictionary, session.appDataDictionary)
skipChecksum := !session.SessionSettings.ValidateChecksum
err := ParseMessageWithDataDictionary(msg, bytes.NewBuffer(msgBytes), session.transportDataDictionary, session.appDataDictionary, skipChecksum)
if err != nil {
session.log.OnEventf("Resend Msg Parse Error: %v, %v", err.Error(), bytes.NewBuffer(msgBytes).String())
return err // We cant continue with a message that cant be parsed correctly.
Expand Down
1 change: 1 addition & 0 deletions internal/session_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type SessionSettings struct {
ResetSeqTime time.Time
EnableResetSeqTime bool
InChanCapacity int
ValidateChecksum bool

// Required on logon for FIX.T.1 messages.
DefaultApplVerID string
Expand Down
32 changes: 31 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type msgParser struct {
trailerBytes []byte
foundBody bool
foundTrailer bool
skipChecksum bool
}

// in the message header, the first 3 tags in the message header must be 8,9,35.
Expand Down Expand Up @@ -157,7 +158,7 @@ func (m *Message) CopyInto(to *Message) {

// ParseMessage constructs a Message from a byte slice wrapping a FIX message.
func ParseMessage(msg *Message, rawMessage *bytes.Buffer) (err error) {
return ParseMessageWithDataDictionary(msg, rawMessage, nil, nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should probably be changed to include the skipChecksum check in the argument instead as that would provide more control

return ParseMessageWithDataDictionary(msg, rawMessage, nil, nil, false)
}

// ParseMessageWithDataDictionary constructs a Message from a byte slice wrapping a FIX message using an optional session and application DataDictionary for reference.
Expand All @@ -166,12 +167,14 @@ func ParseMessageWithDataDictionary(
rawMessage *bytes.Buffer,
transportDataDictionary *datadictionary.DataDictionary,
appDataDictionary *datadictionary.DataDictionary,
skipChecksum bool,
) (err error) {
// Create msgparser before we go any further.
mp := &msgParser{
msg: msg,
transportDataDictionary: transportDataDictionary,
appDataDictionary: appDataDictionary,
skipChecksum: skipChecksum,
}
mp.msg.rawMessage = rawMessage
mp.rawBytes = rawMessage.Bytes()
Expand Down Expand Up @@ -297,8 +300,35 @@ func doParsing(mp *msgParser) (err error) {
bodyLength, err := mp.msg.Header.getIntNoLock(tagBodyLength)
if err != nil {
err = parseError{OrigError: err.Error()}
return
} else if length != bodyLength && !xmlDataMsg {
err = parseError{OrigError: fmt.Sprintf("Incorrect Message Length, expected %d, got %d", bodyLength, length)}
return
}

calculatedCheckSum := 0
for i := 0; i <= mp.fieldIndex; i++ {
if mp.msg.fields[i].tag == tagCheckSum {
continue
}
calculatedCheckSum += mp.msg.fields[i].total()
}

calculatedCheckSum = calculatedCheckSum % 256

if !mp.skipChecksum {
var expectedCheckSumStr string
expectedCheckSumStr, err = mp.msg.Trailer.getStringNoLock(tagCheckSum)
if err != nil {
err = parseError{OrigError: "CheckSum tag missing"}
return
}

if expectedCheckSumStr != formatCheckSum(calculatedCheckSum) {
err = parseError{
OrigError: fmt.Sprintf("Expected CheckSum=%s, Received CheckSum=%s", formatCheckSum(calculatedCheckSum), expectedCheckSumStr),
}
}
}

return
Expand Down
24 changes: 18 additions & 6 deletions message_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,37 @@ func (suite *MessageRouterTestSuite) SetupTest() {
}

func (suite *MessageRouterTestSuite) TestNoRoute() {
suite.givenTheMessage([]byte("8=FIX.4.39=8735=D49=TW34=356=ISLD52=20160421-14:43:5040=160=20160421-14:43:5054=121=311=id10=235"))
suite.givenTheMessage([]byte("8=FIX.4.39=8735=D49=TW34=356=ISLD52=20160421-14:43:5040=160=20160421-14:43:5054=121=311=id10=236"))

rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageNotRouted()
suite.Equal(NewBusinessMessageRejectError("Unsupported Message Type", 3, nil), rej)
}

func (suite *MessageRouterTestSuite) TestNoRouteWhitelistedMessageTypes() {
var tests = []string{"0", "A", "1", "2", "3", "4", "5", "j"}
var tests = []struct {
msgType string
checksum string
}{
{"0", "216"},
{"A", "233"},
{"1", "217"},
{"2", "218"},
{"3", "219"},
{"4", "220"},
{"5", "221"},
{"j", "018"},
}

for _, test := range tests {
suite.SetupTest()

msg := fmt.Sprintf("8=FIX.4.39=8735=%v49=TW34=356=ISLD52=20160421-14:43:5040=160=20160421-14:43:5054=121=311=id10=235", test)
msg := fmt.Sprintf("8=FIX.4.39=8735=%s49=TW34=356=ISLD52=20160421-14:43:5040=160=20160421-14:43:5054=121=311=id10=%s", test.msgType, test.checksum)
suite.givenTheMessage([]byte(msg))

rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageNotRouted()
suite.Nil(rej, "Message type '%v' should not be rejected by the MessageRouter", test)
suite.Nil(rej, "Message type '%s' should not be rejected by the MessageRouter", test.msgType)
}
}

Expand Down Expand Up @@ -174,7 +186,7 @@ func (suite *MessageRouterTestSuite) TestRouteFIXT50AppWithApplVerID() {
suite.givenTheRoute(ApplVerIDFIX50, "D")
suite.givenTheRoute(ApplVerIDFIX50SP1, "D")

suite.givenTheMessage([]byte("8=FIXT.1.19=8935=D49=TW34=356=ISLD52=20160424-16:48:261128=740=160=20160424-16:48:2611=id21=310=120"))
suite.givenTheMessage([]byte("8=FIXT.1.19=8935=D49=TW34=356=ISLD52=20160424-16:48:261128=740=160=20160424-16:48:2611=id21=310=192"))
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(ApplVerIDFIX50, "D")
suite.Nil(rej)
Expand All @@ -185,7 +197,7 @@ func (suite *MessageRouterTestSuite) TestRouteFIXTAppWithApplVerID() {
suite.givenTheRoute(ApplVerIDFIX50, "D")
suite.givenTheRoute(ApplVerIDFIX50SP1, "D")

suite.givenTheMessage([]byte("8=FIXT.1.19=8935=D49=TW34=356=ISLD52=20160424-16:48:261128=840=160=20160424-16:48:2611=id21=310=120"))
suite.givenTheMessage([]byte("8=FIXT.1.19=8935=D49=TW34=356=ISLD52=20160424-16:48:261128=840=160=20160424-16:48:2611=id21=310=193"))
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(ApplVerIDFIX50SP1, "D")
suite.Nil(rej)
Expand Down
50 changes: 33 additions & 17 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *MessageSuite) TestParseMessageEmpty() {
}

func (s *MessageSuite) TestParseMessage() {
rawMsg := bytes.NewBufferString("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")
rawMsg := bytes.NewBufferString("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=051")

err := ParseMessage(s.msg, rawMsg)
s.Nil(err)
Expand Down Expand Up @@ -99,14 +99,30 @@ func (s *MessageSuite) TestParseMessageWithDataDictionary() {
5050: nil,
},
}
rawMsg := bytes.NewBufferString("8=FIX.4.29=12635=D34=249=TW52=20140515-19:49:56.65956=ISLD10030=CUST11=10021=140=154=155=TSLA60=00010101-00:00:00.0005050=HELLO10=039")
rawMsg := bytes.NewBufferString("8=FIX.4.29=12635=D34=249=TW52=20140515-19:49:56.65956=ISLD10030=CUST11=10021=140=154=155=TSLA60=00010101-00:00:00.0005050=HELLO10=036")

err := ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict)
err := ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false)
s.Nil(err)
s.FieldEquals(Tag(10030), "CUST", s.msg.Header)
s.FieldEquals(Tag(5050), "HELLO", s.msg.Trailer)
}

func (s *MessageSuite) TestParseMessageSkipChecksum() {
rawMsg := bytes.NewBufferString("8=FIX.4.29=12635=D34=249=TW52=20140515-19:49:56.65956=ISLD10030=CUST11=10021=140=154=155=TSLA60=00010101-00:00:00.0005050=HELLO10=035")

// Test Strict Mode (skipChecksum = false) -> Should Fail
err := ParseMessageWithDataDictionary(s.msg, rawMsg, nil, nil, false)
s.NotNil(err, "Expected error when validating invalid checksum")
s.Contains(err.Error(), "Expected CheckSum")

// Test Lenient Mode (skipChecksum = true) -> Should Pass
s.msg = NewMessage()
rawMsg = bytes.NewBufferString("8=FIX.4.29=12635=D34=249=TW52=20140515-19:49:56.65956=ISLD10030=CUST11=10021=140=154=155=TSLA60=00010101-00:00:00.0005050=HELLO10=035")

err = ParseMessageWithDataDictionary(s.msg, rawMsg, nil, nil, true)
s.Nil(err, "Expected no error when skipping checksum validation")
}

func (s *MessageSuite) TestParseOutOfOrder() {
// Allow fields out of order, save for validation.
rawMsg := bytes.NewBufferString("8=FIX.4.09=8135=D11=id21=338=10040=154=155=MSFT34=249=TW52=20140521-22:07:0956=ISLD10=250")
Expand All @@ -127,7 +143,7 @@ func (s *MessageSuite) TestBuild() {
}

func (s *MessageSuite) TestReBuild() {
rawMsg := bytes.NewBufferString("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")
rawMsg := bytes.NewBufferString("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=051")

s.Nil(ParseMessage(s.msg, rawMsg))

Expand Down Expand Up @@ -157,7 +173,7 @@ func (s *MessageSuite) TestRebuildOneRepeatingGroupWithDictionary() {
"10=026")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand All @@ -178,7 +194,7 @@ func (s *MessageSuite) TestRebuildTwoRepeatingGroupsWithDictionary() {
rawMsg := bytes.NewBufferString("8=FIX.4.49=21035=D34=2347=UTF-852=20231231-20:19:4149=0100150=01001a56=TEST44=1211=139761=1010040021=1386=1336=NOPL55=SYMABC54=160=20231231-20:19:4138=140=259=0453=1448=4501447=D452=28354=6355=Public10=104")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand All @@ -199,7 +215,7 @@ func (s *MessageSuite) TestRebuildOneRepeatingGroupWithTwoMembersWithDictionary(
"10=044")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand All @@ -223,7 +239,7 @@ func (s *MessageSuite) TestRebuildTwoSequentialRepeatingGroupWithDictionary() {
"10=243")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand All @@ -248,7 +264,7 @@ func (s *MessageSuite) TestRebuildNestedRepeatingGroupWithDictionary() {
"10=206")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand All @@ -273,7 +289,7 @@ func (s *MessageSuite) TestRebuildDoubleNestedRepeatingGroupWithDictionary() {
"10=117")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand All @@ -298,7 +314,7 @@ func (s *MessageSuite) TestRebuildDoubleNestedThenAnotherRepeatingGroupWithDicti
"10=106")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand All @@ -323,7 +339,7 @@ func (s *MessageSuite) TestRebuildDoubleNestedThenBodyTagThenAnotherRepeatingGro
"10=198")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand Down Expand Up @@ -368,7 +384,7 @@ func (s *MessageSuite) TestRebuildDoubleNestedWithTwoMembersRepeatingGroupWithDi
"10=046")

// When we parse it into a message
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict))
s.Nil(ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict, false))

// And then rebuild the message bytes
rebuildBytes := s.msg.build()
Expand Down Expand Up @@ -419,7 +435,7 @@ func (s *MessageSuite) TestReBuildWithRepeatingGroupForResend() {
}

func (s *MessageSuite) TestReverseRoute() {
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.29=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123")))
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.29=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=033")))

builder := s.msg.reverseRoute()

Expand Down Expand Up @@ -450,15 +466,15 @@ func (s *MessageSuite) TestReverseRoute() {
}

func (s *MessageSuite) TestReverseRouteIgnoreEmpty() {
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.09=12835=D34=249=TW52=20060102-15:04:0556=ISLD115=116=CS128=MG129=CB11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123")))
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.09=12835=D34=249=TW52=20060102-15:04:0556=ISLD115=116=CS128=MG129=CB11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=041")))
builder := s.msg.reverseRoute()

s.False(builder.Header.Has(tagDeliverToCompID), "Should not reverse if empty")
}

func (s *MessageSuite) TestReverseRouteFIX40() {
// The onbehalfof/deliverto location id not supported in fix 4.0.
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.09=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123")))
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.09=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=031")))

builder := s.msg.reverseRoute()

Expand All @@ -468,7 +484,7 @@ func (s *MessageSuite) TestReverseRouteFIX40() {
}

func (s *MessageSuite) TestCopyIntoMessage() {
msgString := "8=FIX.4.29=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123"
msgString := "8=FIX.4.29=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=033"
msgBuf := bytes.NewBufferString(msgString)
s.Nil(ParseMessage(s.msg, msgBuf))

Expand Down
2 changes: 1 addition & 1 deletion repeating_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestRepeatingGroup_ReadRecursive(t *testing.T) {

func TestRepeatingGroup_ReadComplete(t *testing.T) {

rawMsg := bytes.NewBufferString("8=FIXT.1.19=26835=W34=711849=TEST52=20151027-18:41:52.69856=TST22=9948=TSTX15262=7268=4269=4270=0.07499272=20151027273=18:41:52.698269=7270=0.07501272=20151027273=18:41:52.698269=8270=0.07494272=20151027273=18:41:52.698269=B271=60272=20151027273=18:41:52.69810=163")
rawMsg := bytes.NewBufferString("8=FIXT.1.19=26835=W34=711849=TEST52=20151027-18:41:52.69856=TST22=9948=TSTX15262=7268=4269=4270=0.07499272=20151027273=18:41:52.698269=7270=0.07501272=20151027273=18:41:52.698269=8270=0.07494272=20151027273=18:41:52.698269=B271=60272=20151027273=18:41:52.69810=094")

msg := NewMessage()
err := ParseMessage(msg, rawMsg)
Expand Down
8 changes: 8 additions & 0 deletions session_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ func (f sessionFactory) newSession(
}
}

if settings.HasSetting(config.ValidateChecksum) {
if s.ValidateChecksum, err = settings.BoolSetting(config.ValidateChecksum); err != nil {
return
}
} else {
s.ValidateChecksum = true
}

if settings.HasSetting(config.CheckLatency) {
var doCheckLatency bool
if doCheckLatency, err = settings.BoolSetting(config.CheckLatency); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions session_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,32 @@ func (s *SessionFactorySuite) TestStartEndDayWithWeekdaysError() {
s.NotNil(err)
}

func (s *SessionFactorySuite) TestValidateChecksum() {
var tests = []struct {
setting string
expected bool
}{
{"Y", true},
{"N", false},
}

for _, test := range tests {
s.SetupTest()
s.SessionSettings.Set(config.ValidateChecksum, test.setting)

session, err := s.newSession(s.SessionID, s.MessageStoreFactory, s.SessionSettings, s.LogFactory, s.App)
s.Nil(err)
s.NotNil(session)

s.Equal(test.expected, session.ValidateChecksum, "Failed to set ValidateChecksum from config")
}

s.SetupTest()
session, err := s.newSession(s.SessionID, s.MessageStoreFactory, s.SessionSettings, s.LogFactory, s.App)
s.Nil(err)
s.True(session.ValidateChecksum, "Default ValidateChecksum should be true")
}

func (s *SessionFactorySuite) TestDefaultApplVerID() {
s.SessionID = SessionID{BeginString: BeginStringFIXT11, TargetCompID: "TW", SenderCompID: "ISLD"}

Expand Down
3 changes: 2 additions & 1 deletion session_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func (sm *stateMachine) Incoming(session *session, m fixIn) {
session.log.OnIncoming(m.bytes.Bytes())

msg := NewMessage()
if err := ParseMessageWithDataDictionary(msg, m.bytes, session.transportDataDictionary, session.appDataDictionary); err != nil {
skipChecksum := !session.SessionSettings.ValidateChecksum
if err := ParseMessageWithDataDictionary(msg, m.bytes, session.transportDataDictionary, session.appDataDictionary, skipChecksum); err != nil {
session.log.OnEventf("Msg Parse Error: %v, %q", err.Error(), m.bytes)
} else {
msg.ReceiveTime = m.receiveTime
Expand Down
Loading
Loading