From 8dab3074a6df4cae7c998e989c02ef7b11fde5c8 Mon Sep 17 00:00:00 2001 From: Greg Wolff <57912069+gwolffblues@users.noreply.github.com> Date: Fri, 17 Nov 2023 12:03:00 -0500 Subject: [PATCH 1/2] Update crc comparison to use int datatype comparing CRC as strings was causing some issues as the hexadecimal codes may be upper or lower case. transforming to integers from the string resolves this issue, as case no longer matters, and the transform ignores case. --- notecard/notecard.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/notecard/notecard.py b/notecard/notecard.py index 8e15d42..9e45ece 100644 --- a/notecard/notecard.py +++ b/notecard/notecard.py @@ -168,7 +168,9 @@ def _crc_error(self, rsp_bytes): # the decoding process. When re-encoded, the string representation is # also truncated, and so the CRC will be computed over a different # string than was originally sent, resulting in a CRC error. - seq_number, crc = rsp_json['crc'].split(':') + seq_number_str, crc_str = rsp_json['crc'].split(':') + seq_number = int(seq_number_str,16) + crc = int(crc_str, 16) # Remove the 'crc' field from the response. rsp_str = rsp_bytes.decode() rsp_str_crc_removed = rsp_str.split('"crc":')[0] @@ -179,17 +181,21 @@ def _crc_error(self, rsp_bytes): # Compute the CRC over the response, with the 'crc' field removed. bytes_for_crc = rsp_str_crc_removed.encode('utf-8') - computed_crc = '{:08x}'.format(crc32(bytes_for_crc)).upper() - expected_seq_number = '{:04x}'.format(self._last_request_seq_number) + #computed_crc = '{:08x}'.format(crc32(bytes_for_crc)).upper() + computed_crc = crc32(bytes_for_crc) + #expected_seq_number = '{:04x}'.format(self._last_request_seq_number).upper() + expected_seq_number = self._last_request_seq_number if seq_number != expected_seq_number: if self._debug: + expected_seq_str = '{:04x}'.format(expected_seq_number) print('Sequence number mismatch. Expected ' + \ - f'{expected_seq_number}, received {seq_number}.') + f'{expected_seq_str}, received {seq_number_str}.') return True elif crc != computed_crc: if self._debug: - print(f'CRC error. Computed {computed_crc}, received {crc}.') + computed_crc_str = '{:08x}'.format(computed_crc).upper() + print(f'CRC error. Computed {computed_crc_str}, received {crc_str}.') return True return False From e8aea90140ec5527a9874b76c9a511f75a191a4c Mon Sep 17 00:00:00 2001 From: Greg Wolff <57912069+gwolffblues@users.noreply.github.com> Date: Fri, 17 Nov 2023 12:07:06 -0500 Subject: [PATCH 2/2] Added spaces after comment indicator Flake tests were failing from a comment formatting issue --- notecard/notecard.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notecard/notecard.py b/notecard/notecard.py index 9e45ece..26f0675 100644 --- a/notecard/notecard.py +++ b/notecard/notecard.py @@ -181,9 +181,9 @@ def _crc_error(self, rsp_bytes): # Compute the CRC over the response, with the 'crc' field removed. bytes_for_crc = rsp_str_crc_removed.encode('utf-8') - #computed_crc = '{:08x}'.format(crc32(bytes_for_crc)).upper() + # computed_crc = '{:08x}'.format(crc32(bytes_for_crc)).upper() computed_crc = crc32(bytes_for_crc) - #expected_seq_number = '{:04x}'.format(self._last_request_seq_number).upper() + # expected_seq_number = '{:04x}'.format(self._last_request_seq_number).upper() expected_seq_number = self._last_request_seq_number if seq_number != expected_seq_number: