Skip to content

Commit

Permalink
Merge pull request #52 from sunshine2050/fix-malformed-messages
Browse files Browse the repository at this point in the history
Handling malformed messages
  • Loading branch information
yen3 authored Apr 13, 2023
2 parents c156d78 + f7a8656 commit cfd6ccd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
12 changes: 11 additions & 1 deletion dlt/core/core_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,15 @@ def use_extended_header(self):
"""Returns True if the DLTMessage has extended header"""
return self.standardheader.htyp & DLT_HTYP_UEH

@property
def _is_extended_header_exists(self):
return self.extendedheader and self.extendedheader.msin

@property
def is_mode_verbose(self):
"""Returns True if the DLTMessage is set to verbose mode"""
if not self._is_extended_header_exists:
return self.verbose
return self.extendedheader.msin & DLT_MSIN_VERB

@property
Expand Down Expand Up @@ -290,6 +296,8 @@ def ctrl_return_type_string(self):
@property
def type(self):
"""Returns message type of the DLTMessage"""
if not self._is_extended_header_exists:
return DLT_TYPE_LOG
return (self.extendedheader.msin & DLT_MSIN_MSTP) >> DLT_MSIN_MSTP_SHIFT

@property
Expand All @@ -301,6 +309,8 @@ def type_string(self):
@property
def subtype(self):
"""Returns message subtype of the DLTMessage"""
if not self._is_extended_header_exists:
return DLT_TYPE_LOG
return (self.extendedheader.msin & DLT_MSIN_MTIN) >> DLT_MSIN_MTIN_SHIFT

@property
Expand Down Expand Up @@ -329,7 +339,7 @@ def payload_decoded(self):
"""
text = b""
if self.is_mode_non_verbose and not self.is_type_control and self.noar == 0:
buf = ctypes.create_string_buffer("\000" * DLT_DAEMON_TEXTSIZE)
buf = ctypes.create_string_buffer(b"\000" * DLT_DAEMON_TEXTSIZE)
dltlib.dlt_message_payload(ctypes.byref(self), buf, DLT_DAEMON_TEXTSIZE, DLT_OUTPUT_ASCII, self.verbose)
return b"[%s] #%s#" % (self.message_id_string, buf.value[4:].rstrip(b"\000"))

Expand Down
17 changes: 16 additions & 1 deletion tests/dlt_message_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@
import pytest

from dlt.dlt import DLTMessage
from tests.utils import create_messages, stream_one, stream_with_params, stream_multiple, msg_benoit, control_one
from tests.utils import (
create_messages,
stream_one,
stream_with_params,
stream_multiple,
stream_multiple_with_malformed_message_at_begining,
msg_benoit,
control_one,
)


class TestsDLTMessageUnit(object):
def test_malformed_message(self):
msgs = create_messages(stream_multiple_with_malformed_message_at_begining, from_file=True)

assert msgs[0].message_id == 1279675715
assert len(msgs) == 3
assert not msgs[0].extendedheader

def test_compare_default_attrs(self):
attrs = {"extendedheader.apid": "DA1", "extendedheader.ctid": "DC1"}
msg = create_messages(stream_one)
Expand Down
15 changes: 15 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


stream_one = io.BytesIO(b"5\x00\x00 MGHS\xdd\xf6e\xca&\x01DA1\x00DC1\x00\x02\x0f\x00\x00\x00\x02\x00\x00\x00\x00")

stream_with_params = (
b"DLT\x01\xc2<\x85W\xc7\xc5\x02\x00MGHS=r\x00\xa0MGHS\x00\x00\x02B\x00X\xd4\xf1A\x08"
b"ENV\x00LVLM\x00\x02\x00\x00-\x00CLevelMonitor::notification() => commandType\x00#"
Expand All @@ -30,6 +31,20 @@
b" currently available 3.7G of space).\nEnforced usage limit is thus 385.9M.\x00"
)

stream_multiple_with_malformed_message_at_begining = (
b"DLT\x01\xfar\xc5c\xf7j\x03\x00\x00\x00\x00\x00\x00\x00\x00LCMFLOW WUP invalidDLT\x01"
b"\xfar\xc5c\x0bo\x03\x00XORA'\x01\x00\x1bXORA\x16\x02\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x11\x04\x00\x00\x00\x00DLT\x01\xfar\xc5c?o\x03"
b"DLT\x01#o\xd1WD>\x0c\x00MGHS5\x00\x00YMGHS\x00\x01\x80\xd1&\x01DA1\x00DC1\x00\x03\x00\x00\x00"
b"\x07\x01\x00SYS\x00\x01\x00FILE\xff\xff\x16\x00File transfer manager.\x12\x00"
b"DLT System ManagerremoDLT\x01#o\xd1Wo>\x0c\x00MGHS=\x00\x01PMGHS\x00\x00\x03\xf4\x00"
b"\x01i\xa6A\x05SYS\x00JOUR\x00\x02\x00\x00\x1b\x002011/11/11 11:11:18.005274\x00\x00\x02\x00\x00"
b"\t\x006.005274\x00\x00\x02\x00\x00\x16\x00systemd-journal[748]:\x00\x00\x02\x00\x00\x0f\x00"
b"Informational:\x00\x00\x02\x00\x00\xcf\x00Runtime journal (/run/log/journal/) is currently"
b" using 8.0M.\nMaximum allowed usage is set to 385.9M.\nLeaving at least 578.8M free (of"
b" currently available 3.7G of space).\nEnforced usage limit is thus 385.9M.\x00"
)

msg_benoit = (
b"DLT\x01\xa5\xd1\xceW\x90\xb9\r\x00MGHS=\x00\x00RMGHS\x00\x00\n[\x00\x0f\x9b#A\x01DEMODATA\x00"
b"\x82\x00\x002\x00Logging from the constructor of a global instance\x00"
Expand Down

0 comments on commit cfd6ccd

Please sign in to comment.