Skip to content

Commit

Permalink
Changed checksum calculation in Message and MessageBuilder to include…
Browse files Browse the repository at this point in the history
… Body length tag in calculation
  • Loading branch information
pfeairheller committed Jun 29, 2014
1 parent 46228cf commit aee7b96
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ func (m *Message) rebuild() {
trailer := m.Trailer.(fieldMap)

bodyLength := header.length() + len(m.bodyBytes) + trailer.length()
header.Set(fix.NewIntField(tag.BodyLength, bodyLength))
checkSum := header.total() + trailer.total()
for _, b := range m.bodyBytes {
checkSum += int(b)
}
checkSum %= 256

header.Set(fix.NewIntField(tag.BodyLength, bodyLength))
trailer.Set(newCheckSum(checkSum))

var b bytes.Buffer
Expand Down
2 changes: 1 addition & 1 deletion message_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (m messageBuilder) Build() ([]byte, error) {

func (m messageBuilder) cook() {
bodyLength := m.header.length() + m.body.length() + m.trailer.length()
checkSum := (m.header.total() + m.body.total() + m.trailer.total()) % 256
m.header.Set(fix.NewIntField(tag.BodyLength, bodyLength))
checkSum := (m.header.total() + m.body.total() + m.trailer.total()) % 256
m.trailer.Set(newCheckSum(checkSum))
}
34 changes: 34 additions & 0 deletions message_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package quickfix

import (
"bytes"
"github.com/quickfixgo/quickfix/fix"
"github.com/quickfixgo/quickfix/fix/tag"
"testing"
"github.com/quickfixgo/quickfix/fix/field"
)

var builder MessageBuilder

func TestMessageBuilder_checkBuild(t *testing.T) {
builder = NewMessageBuilder()

builder.Header().Set(field.NewBeginString(fix.BeginString_FIX44))
builder.Header().Set(fix.NewStringField(tag.MsgType, "A"))
builder.Header().Set(fix.NewStringField(tag.SendingTime, "20140615-19:49:56"))

builder.Body().Set(field.NewUsername("my_user"))
builder.Body().Set(field.NewPassword("secret"))

expectedBytes := []byte("8=FIX.4.49=4935=A52=20140615-19:49:56553=my_user554=secret10=072")
result, err := builder.Build()
if err != nil {
t.Error("Unexpected error", err)
}

if !bytes.Equal(expectedBytes, result) {
t.Error("Unexpected bytes, got ", string(result))
}

}

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

msg.rebuild()

expectedBytes := []byte("8=FIX.4.29=12635=D34=249=TW52=20140615-19:49:5656=ISLD122=20140515-19:49:56.65911=10021=140=154=155=TSLA60=00010101-00:00:00.00010=124")
expectedBytes := []byte("8=FIX.4.29=12635=D34=249=TW52=20140615-19:49:5656=ISLD122=20140515-19:49:56.65911=10021=140=154=155=TSLA60=00010101-00:00:00.00010=128")

if !bytes.Equal(expectedBytes, msg.rawMessage) {
t.Error("Unexpected bytes, got ", string(msg.rawMessage))
Expand Down

0 comments on commit aee7b96

Please sign in to comment.