Skip to content

Commit

Permalink
Fix: Checksum algorithm shall force 0x00 value to 0xFF (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-mp authored Jul 2, 2023
1 parent fe25fe9 commit fbe59a0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
8 changes: 6 additions & 2 deletions intex_spa/intex_spa_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ def checksum_as_int(data: str) -> int:
calculated_checksum = calculated_checksum - (
int("0x" + data[index : index + 2], 16)
)
return calculated_checksum % 0xFF
calculated_checksum = calculated_checksum % 0xFF
# Fix: https://github.com/mathieu-mp/intex-spa/issues/27
if calculated_checksum == 0x00:
calculated_checksum = 0xFF
return calculated_checksum


def checksum_as_str(data: str) -> str:
"""Return string checksum for the given data, as expected by Intex Spa protocol"""
# Return checksum as a hex string without 0x prefix
return hex(checksum_as_int(data) % 0xFF)[2:].upper()
return hex(checksum_as_int(data))[2:].upper()


class IntexSpaQuery:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_intex_spa_query_checksum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from intex_spa.intex_spa_query import IntexSpaQuery

import pytest

valid_status_responses = [
b'{"sid":"12345678901234","data":"FFFF110F010700220000000080808022000012","result":"ok","type":2}\n',
b'{"sid":"12345678901234","data":"FFFF110F01070064000000008080806700008A","result":"ok","type":2}\n', # From https://github.com/mathieu-mp/intex-spa/issues/27
b'{"sid":"12345678901234","data":"FFFF110F0107006400000085808085670000FF","result":"ok","type":2}\n', # From https://github.com/mathieu-mp/intex-spa/issues/27
]
invalid_status_responses = [
b'{"sid":"12345678901234","data":"FFFF110F010700220000000080808022000044","result":"ok","type":2}\n', # Arbitrary false checksum
b'{"sid":"12345678901234","data":"00000000000000000000000000000000000000","result":"ok","type":2}\n', # Checksum 0x00 means no checksum calculation
]


@pytest.mark.parametrize("status_response", valid_status_responses)
def test_valid_intex_spa_checksums(status_response):
query = IntexSpaQuery(intent="status")
query.intex_timestamp = "12345678901234"
query.render_response_data(received_bytes=status_response)


@pytest.mark.parametrize("status_response", invalid_status_responses)
def test_invalid_intex_spa_checksums(status_response):
query = IntexSpaQuery(intent="status")
query.intex_timestamp = "12345678901234"
with pytest.raises(AssertionError):
query.render_response_data(received_bytes=status_response)

0 comments on commit fbe59a0

Please sign in to comment.