Skip to content

Commit

Permalink
Fix #58: Checksum is not always 3 ASCII characters
Browse files Browse the repository at this point in the history
Bug introduced in 9f5096a, where
I switched to using f-string formatting, but left off the left zero
pad for the checksum value.

This bug was present in v1.0.16.
  • Loading branch information
da4089 committed Sep 12, 2023
1 parent e46841b commit 0aef214
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion simplefix/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def encode(self, raw=False):
checksum = 0
for c in buf:
checksum += c
buf += b"10=" + fix_val(f"{checksum % 256}") + SOH_STR
buf += b"10=" + fix_val(f"{checksum % 256:03}") + SOH_STR

return buf

Expand Down
13 changes: 13 additions & 0 deletions test/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ def test_raw_checksum(self):
pkt.append_pair(10, 42)
self.assertEqual(b"10=42\x01", pkt.encode(True))

def test_cooked_checksum(self):
"""Test calculation of Checksum(10) in cooked mode"""
msg = FixMessage()
msg.append_pair(8, b'FIX.4.2')
msg.append_pair(35, b'D')
msg.append_pair(5001, b'AAAAA')
self.assertEqual(b"8=FIX.4.2\x01"
b"9=16\x01"
b"35=D\x01"
b"5001=AAAAA\x01"
b"10=048\x01",
msg.encode())

def test_raw_msg_type(self):
"""Test encoding of MessageType(35) in raw mode"""
pkt = FixMessage()
Expand Down

0 comments on commit 0aef214

Please sign in to comment.