Skip to content

Commit

Permalink
fix: marshall multi-byte strings correctly (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 4, 2023
1 parent f9e5d1d commit 4de31a3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dbus_fast/_private/marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions tests/data/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
36 changes: 36 additions & 0 deletions tests/test_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,39 @@ def test_unmarshall_mount_message_2():
],
[],
]


def test_unmarshall_multi_byte_string():
"""Test unmarshall a multi-byte string."""

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"]]

0 comments on commit 4de31a3

Please sign in to comment.