Skip to content

Commit

Permalink
refactored checksum validation to eliminate redundant operations
Browse files Browse the repository at this point in the history
  • Loading branch information
eredden committed Feb 8, 2025
1 parent 212b00e commit 8f76e0f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "veeder_root_tls_socket_library"
version = "1.0.5"
version = "1.1.0"
authors = [ {name = "Evan Redden", email = "redden.evan@gmail.com"} ]
description = "Socket library for querying and extracting data from Veeder-Root TLS systems."
readme = "README.md"
Expand Down
17 changes: 5 additions & 12 deletions src/veeder_root_tls_socket_library/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def execute(self,

return self.__handle_response(byte_response, byte_command, is_display)

def __handle_response(self, byte_response: bytes,
def _handle_response(self, byte_response: bytes,
byte_command: bytes, is_display: bool) -> str:
"""
Handles responses from the TLS system after executing a command.
Expand Down Expand Up @@ -133,7 +133,7 @@ def __handle_response(self, byte_response: bytes,

return response

def __data_integrity_check(self, byte_response: bytes) -> bool:
def _data_integrity_check(self, byte_response: bytes) -> bool:
"""
Verifies whether or not a command response retains its integrity
after transmission by comparing it against the response checksum.
Expand All @@ -145,18 +145,11 @@ def __data_integrity_check(self, byte_response: bytes) -> bool:
response = byte_response.decode()
message = response[:-5]
checksum = response[-5:-1]
integrity_threshold = "0b10000000000000000"

# Calculate the 16-bit binary count of the message.
message_int = sum(ord(char) for char in message) & 0xFFFF

# Convert message integer to twos complement integer.
message_int = message_int + (message_int >> 16)

# Convert checksum hexadecimal string into integer.
message_sum = sum(ord(char) for char in message)
checksum_int = int(checksum, 16)

# Compare sum of checksum and message to expected result.
integrity_threshold = "0b10000000000000000"
binary_sum = bin(message_int + checksum_int)

return binary_sum == integrity_threshold
return bin(message_sum + checksum_int) == integrity_threshold
19 changes: 19 additions & 0 deletions tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,24 @@ def test_invalid_command(self):
with self.assertRaises(ValueError):
tls.execute("test")

def test_checksum(self):
"""
Verify that TlsSocket._data_integrity_check() correctly validates outputs.
"""

# Cases are tuples with the full command output and expected checksum validation result.
cases = [
(b"\x01i101002312301342020402&&FB3B\x03", True),
(b"\x01i101002312301342020402&&FB3A\x03", False),
(b"\x01j101002312301342020402&&FB3B\x03", False)
]

with TlsSocket(self.ip, self.port) as tls:
for case in cases:
actual = tls._data_integrity_check(case[0])
expected = case[1]

self.assertEqual(actual, expected)

if __name__ == "__main__":
unittest.main()

0 comments on commit 8f76e0f

Please # to comment.