From 4221e79366ca4b04203334061bf7cf0d9dabff5f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 4 Oct 2023 12:33:38 -0500 Subject: [PATCH 1/2] fix: marshall multi-byte strings correctly --- src/dbus_fast/_private/marshaller.py | 2 +- tests/data/messages.json | 12 ++++++++++ tests/test_marshaller.py | 36 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/dbus_fast/_private/marshaller.py b/src/dbus_fast/_private/marshaller.py index d249a287..9da8db8d 100644 --- a/src/dbus_fast/_private/marshaller.py +++ b/src/dbus_fast/_private/marshaller.py @@ -68,7 +68,7 @@ def write_string(self, value: _str, type_: SignatureType) -> int: def _write_string(self, value: _str) -> int: value_bytes = value.encode() - value_len = len(value) + value_len = len(value_bytes) written = self._align(4) + 4 buf = self._buf buf += PACK_UINT32(value_len) diff --git a/tests/data/messages.json b/tests/data/messages.json index a979847a..e8e43d60 100644 --- a/tests/data/messages.json +++ b/tests/data/messages.json @@ -242,5 +242,17 @@ "body": [["hello", "worl"], true, false, ["hello", "worl"], true, false] }, "data": "6c01000148000000010000007e00000001016f00150000002f6f72672f667265656465736b746f702f4442757300000002017300140000006f72672e667265656465736b746f702e4442757300000000030173000500000048656c6c6f00000006017300140000006f72672e667265656465736b746f702e444275730000000008016700086173626261736262000000150000000500000068656c6c6f00000004000000776f726c000000000100000000000000150000000500000068656c6c6f00000004000000776f726c000000000100000000000000" + }, + { + "message": { + "destination": "org.freedesktop.DBus", + "path": "/org/freedesktop/DBus", + "interface": "org.freedesktop.DBus", + "member": "Hello", + "serial": 1, + "signature": "as", + "body": [["//doesntmatter/über"]] + }, + "data": "6c0100011d000000010000007800000001016f00150000002f6f72672f667265656465736b746f702f4442757300000002017300140000006f72672e667265656465736b746f702e4442757300000000030173000500000048656c6c6f00000006017300140000006f72672e667265656465736b746f702e4442757300000000080167000261730019000000140000002f2f646f65736e746d61747465722fc3bc62657200" } ] diff --git a/tests/test_marshaller.py b/tests/test_marshaller.py index 14dec4b7..f52c5e1c 100644 --- a/tests/test_marshaller.py +++ b/tests/test_marshaller.py @@ -678,3 +678,39 @@ def test_unmarshall_mount_message_2(): ], [], ] + + +def test_unmarshall_mount_message_3(): + """Test we mount message unmarshall version 3.""" + + mount_message = ( + b"l\x01\x00\x01\x1d\x00\x00\x00" + b"\x01\x00\x00\x00x\x00\x00\x00" + b"\x01\x01o\x00\x15\x00\x00\x00" + b"/org/fre" + b"edesktop" + b"/DBus\x00\x00\x00" + b"\x02\x01s\x00\x14\x00\x00\x00" + b"org.free" + b"desktop." + b"DBus\x00\x00\x00\x00" + b"\x03\x01s\x00\x05\x00\x00\x00" + b"Hello\x00\x00\x00" + b"\x06\x01s\x00\x14\x00\x00\x00" + b"org.free" + b"desktop." + b"DBus\x00\x00\x00\x00" + b"\x08\x01g\x00\x02as\x00" + b"\x19\x00\x00\x00\x14\x00\x00\x00" + b"//doesnt" + b"matter/\xc3" + b"\xbcber\x00" + ) + + stream = io.BytesIO(mount_message) + unmarshaller = Unmarshaller(stream) + unmarshaller.unmarshall() + message = unmarshaller.message + assert unmarshaller.message.signature == "as" + unpacked = unpack_variants(message.body) + assert unpacked == [["//doesntmatter/über"]] From 362da9392b4900302856132c2c99d7481469ea92 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 4 Oct 2023 12:34:58 -0500 Subject: [PATCH 2/2] fix: marshall multi-byte strings correctly --- tests/test_marshaller.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_marshaller.py b/tests/test_marshaller.py index f52c5e1c..ee8cd60a 100644 --- a/tests/test_marshaller.py +++ b/tests/test_marshaller.py @@ -680,8 +680,8 @@ def test_unmarshall_mount_message_2(): ] -def test_unmarshall_mount_message_3(): - """Test we mount message unmarshall version 3.""" +def test_unmarshall_multi_byte_string(): + """Test unmarshall a multi-byte string.""" mount_message = ( b"l\x01\x00\x01\x1d\x00\x00\x00"