Skip to content

Commit

Permalink
Recalculate checksum on data change
Browse files Browse the repository at this point in the history
ref #240
  • Loading branch information
jklmnn committed Aug 26, 2020
1 parent d93c87a commit 5979df6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
13 changes: 6 additions & 7 deletions rflx/pyrflx/typevalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,9 @@ def check_outgoing_condition_satisfied() -> None:
if checksum_calculation:
self._preset_fields(field_name)
for checksum in self._checksums.values():
if not self._fields[checksum.field_name].set and self._is_checksum_settable(
checksum
):
if (
not self._fields[checksum.field_name].set or checksum.calculated
) and self._is_checksum_settable(checksum):
self._set_checksum(checksum)

def _preset_fields(self, fld: str) -> None:
Expand Down Expand Up @@ -892,7 +892,7 @@ def set_checksum_function(self, checksums: Dict[str, Callable]) -> None:
)
for field_name, checksum in self._checksums.items():
if field_name == checksum_field_name:
checksum.set_checksum_function(checksum_function)
checksum.function = checksum_function
else:
raise KeyError(
f"cannot set checksum function: field {checksum_field_name} "
Expand Down Expand Up @@ -957,6 +957,7 @@ def valid_path(value_range: ValueRange) -> bool:

def _set_checksum(self, checksum: "MessageValue.Checksum") -> None:
self._fields[checksum.field_name].typeval.assign(0)
checksum.calculated = True
checksum_value = self._calculate_checksum(checksum)
self.set(checksum.field_name, checksum_value, False)

Expand Down Expand Up @@ -1134,6 +1135,7 @@ class Checksum:
def __init__(self, field_name: str, parameters: Sequence[Expr]):
self.field_name = field_name
self.function: Optional[Callable] = None
self.calculated = False

@dataclass
class ExpressionTuple:
Expand All @@ -1145,9 +1147,6 @@ class ExpressionTuple:
assert isinstance(expr, (ValueRange, Attribute, Variable))
self.parameters.append(ExpressionTuple(expr))

def set_checksum_function(self, function: Callable) -> None:
self.function = function

@dataclass
class Field:
typeval: TypeValue
Expand Down
24 changes: 24 additions & 0 deletions tests/test_pyrflx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,30 @@ def test_checksum_auto_icmp(icmp_checksum: MessageValue) -> None:
icmp_checksum.set("Data", test_data)
assert icmp_checksum.get("Checksum") == 12824
assert icmp_checksum.bytestring == b"\x08\x00\x32\x18\x00\x05\x00\x01" + test_data
assert icmp_checksum.valid_message


def test_checksum_auto_icmp_change_field(icmp_checksum: MessageValue) -> None:
test_data = (
b"\x47\xb4\x67\x5e\x00\x00\x00\x00"
b"\x4a\xfc\x0d\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x16\x17"
b"\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27"
b"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37"
)
icmp_checksum.set_checksum_function({"Checksum": icmp_checksum_function})
icmp_checksum.set("Tag", "Echo_Request")
icmp_checksum.set("Code_Zero", 0)
icmp_checksum.set("Identifier", 5)
icmp_checksum.set("Sequence_Number", 1)
icmp_checksum.set("Data", test_data)
assert icmp_checksum.get("Checksum") == 12824
assert icmp_checksum.bytestring == b"\x08\x00\x32\x18\x00\x05\x00\x01" + test_data
assert icmp_checksum.valid_message
icmp_checksum.set("Sequence_Number", 2)
icmp_checksum.set("Data", test_data)
assert icmp_checksum.get("Checksum") == 12823
assert icmp_checksum.bytestring == b"\x08\x00\x32\x17\x00\x05\x00\x02" + test_data
assert icmp_checksum.valid_message


def test_checksum_parse_icmp(icmp_checksum: MessageValue) -> None:
Expand Down

0 comments on commit 5979df6

Please sign in to comment.