Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assert that header length matches data length when user provides… #11

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions smp/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ class SMPDeserializationError(SMPException):

class SMPMismatchedGroupId(SMPDeserializationError):
...


class SMPMalformed(SMPException):
...
10 changes: 9 additions & 1 deletion smp/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pydantic import BaseModel, ConfigDict

from smp import header as smpheader
from smp.exceptions import SMPMismatchedGroupId
from smp.exceptions import SMPMalformed, SMPMismatchedGroupId

T = TypeVar("T", bound='_MessageBase')

Expand Down Expand Up @@ -86,6 +86,10 @@ def model_post_init(self, _: None) -> None:
](self._COMMAND_ID),
),
)
elif self.header.length != len(data_bytes):
raise SMPMalformed(
f"header.length {self.header.length} != len(data_bytes) {len(data_bytes)}"
)
self._bytes = cast(smpheader.Header, self.header).BYTES + data_bytes


Expand Down Expand Up @@ -123,6 +127,10 @@ def model_post_init(self, _: None) -> None:
](self._COMMAND_ID),
),
)
elif self.header.length != len(data_bytes):
raise SMPMalformed(
f"header.length {self.header.length} != len(data_bytes) {len(data_bytes)}"
)
self._bytes = cast(smpheader.Header, self.header).BYTES + data_bytes


Expand Down
135 changes: 135 additions & 0 deletions tests/test_injected_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"""Test the case where the user forms the header separately."""

from typing import cast

import pytest

from smp import header as smphdr
from smp import image_management as smpimg
from smp.exceptions import SMPMalformed


def test_ImageUploadWriteRequest_injected_header() -> None:
h = smphdr.Header(
op=smphdr.OP.WRITE,
version=smphdr.Version.V0,
flags=smphdr.Flag(0),
length=0,
group_id=smphdr.GroupId.IMAGE_MANAGEMENT,
sequence=0,
command_id=smphdr.CommandId.ImageManagement.UPLOAD,
)

data = bytes([0x00] * 50)

r = smpimg.ImageUploadWriteRequest(
header=smphdr.Header(
op=h.op,
version=h.version,
flags=h.flags,
length=76,
group_id=h.group_id,
sequence=h.sequence,
command_id=h.command_id,
),
off=0,
data=data,
image=1,
len=50,
)

assert cast(smphdr.Header, r.header).length == 76
assert len(r.BYTES) == 76 + smphdr.Header.SIZE

with pytest.raises(SMPMalformed):
r = smpimg.ImageUploadWriteRequest(
header=smphdr.Header(
op=h.op,
version=h.version,
flags=h.flags,
length=84,
group_id=h.group_id,
sequence=h.sequence,
command_id=h.command_id,
),
off=0,
data=data,
image=1,
len=50,
)

with pytest.raises(SMPMalformed):
r = smpimg.ImageUploadWriteRequest(
header=smphdr.Header(
op=h.op,
version=h.version,
flags=h.flags,
length=0,
group_id=h.group_id,
sequence=h.sequence,
command_id=h.command_id,
),
off=0,
data=data,
image=1,
len=50,
)


def test_ImageUploadWriteResponse_injected_header() -> None:
h = smphdr.Header(
op=smphdr.OP.WRITE_RSP,
version=smphdr.Version.V0,
flags=smphdr.Flag(0),
length=0,
group_id=smphdr.GroupId.IMAGE_MANAGEMENT,
sequence=0,
command_id=smphdr.CommandId.ImageManagement.UPLOAD,
)

r = smpimg.ImageUploadProgressWriteResponse(
header=smphdr.Header(
op=h.op,
version=h.version,
flags=h.flags,
length=10,
group_id=h.group_id,
sequence=h.sequence,
command_id=h.command_id,
),
rc=0,
off=0,
)

assert cast(smphdr.Header, r.header).length == 10
assert len(r.BYTES) == 10 + smphdr.Header.SIZE

with pytest.raises(SMPMalformed):
r = smpimg.ImageUploadProgressWriteResponse(
header=smphdr.Header(
op=h.op,
version=h.version,
flags=h.flags,
length=2,
group_id=h.group_id,
sequence=h.sequence,
command_id=h.command_id,
),
rc=0,
off=0,
)

with pytest.raises(SMPMalformed):
r = smpimg.ImageUploadProgressWriteResponse(
header=smphdr.Header(
op=h.op,
version=h.version,
flags=h.flags,
length=0,
group_id=h.group_id,
sequence=h.sequence,
command_id=h.command_id,
),
rc=0,
off=0,
)
Loading