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 issues #95 and #96 #104

Merged
merged 2 commits into from
Sep 13, 2024
Merged

Fix issues #95 and #96 #104

merged 2 commits into from
Sep 13, 2024

Conversation

hdasch
Copy link

@hdasch hdasch commented Sep 13, 2024

The marshal and unmarshal tests fail when run on a big endian system.

By default, m.marshal and m.unmarshal serialize/deserialize integers in little endian order. Tests pass integer values in expected encoding/value parameters in host order -- the output of struct.pack().

So on big endian systems the byte order of integer values conflict. These changes coerce the serialization/deserialization into host byte order to match the output of struct.pack().

Hugh Daschbach added 2 commits September 13, 2024 01:47
Fix about 16 marshaling error similar to:

===============================================================================
[FAIL]
Traceback (most recent call last):
  File "/usr/lib/python3.11/unittest/case.py", line 57, in testPartExecutor
    yield
  File "/usr/lib/python3.11/unittest/case.py", line 623, in run
    self._callTestMethod(testMethod)
  File "/usr/lib/python3.11/unittest/case.py", line 579, in _callTestMethod
    if method() is not None:
  File "/home/hugh/txdbus/tests/test_marshal.py", line 240, in test_byte
    self.check('ay', [[1, 2, 3, 4]], pack('iBBBB', 4, 1, 2, 3, 4))
  File "/home/hugh/txdbus/tests/test_marshal.py", line 143, in check
    self.assertEqual(
  File "/usr/lib/python3.11/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.11/unittest/case.py", line 866, in _baseAssertEqual
    raise self.failureException(msg)
builtins.AssertionError: b'\x04\x00\x00\x00\x01\x02\x03\x04' != b'\x00\x00\x00\x04\x01\x02\x03\x04' : Binary encoding differs from expected value

tests.test_marshal.TestArrayMarshal.test_byte
===============================================================================
Fix about eight unmarshaling error similar to:

===============================================================================
[ERROR]
Traceback (most recent call last):
  File "/usr/lib/python3.11/unittest/case.py", line 57, in testPartExecutor
    yield
  File "/usr/lib/python3.11/unittest/case.py", line 623, in run
    self._callTestMethod(testMethod)
  File "/usr/lib/python3.11/unittest/case.py", line 579, in _callTestMethod
    if method() is not None:
  File "/home/hugh/txdbus/tests/test_marshal.py", line 487, in test_bad_length
    self.assertRaises(
  File "/usr/lib/python3.11/unittest/case.py", line 766, in assertRaises
    return context.handle('assertRaises', args, kwargs)
  File "/usr/lib/python3.11/unittest/case.py", line 237, in handle
    callable_obj(*args, **kwargs)
  File "/home/hugh/txdbus/tests/test_marshal.py", line 352, in check
    nbytes, value = m.unmarshal(sig, encoding, 0)
  File "/home/hugh/txdbus/txdbus/marshal.py", line 887, in unmarshal
    nbytes, value = unmarshallers[tcode](ct, data, offset, lendian, oobFDs)
  File "/home/hugh/txdbus/txdbus/marshal.py", line 789, in unmarshal_array
    nbytes, value = unmarshallers[tcode](
  File "/home/hugh/txdbus/txdbus/marshal.py", line 812, in unmarshal_struct
    return unmarshal(ct[1:-1], data, offset, lendian, oobFDs)
  File "/home/hugh/txdbus/txdbus/marshal.py", line 887, in unmarshal
    nbytes, value = unmarshallers[tcode](ct, data, offset, lendian, oobFDs)
  File "/home/hugh/txdbus/txdbus/marshal.py", line 706, in unmarshal_int32
    return 4, struct.unpack_from(lendian and '<i' or '>i', data, offset)[0]
struct.error: unpack_from requires a buffer of at least 28 bytes for unpacking 4 bytes at offset 24 (actual buffer size is 24)

tests.test_marshal.TestArrayUnmarshal.test_bad_length
===============================================================================
@WhyNotHugo
Copy link
Collaborator

You're passing the bytes order explicitly in tests.

Does this mean that the m.unmarshal function won't return the right result for consumers unless they also explicitly pass the bytes order?

@hdasch
Copy link
Author

hdasch commented Sep 13, 2024

I misspoke about DBus defaults in my original description. I have edited that to correct and clarify the problem description.

You're passing the bytes order explicitly in tests.

Yes, the changes request integer serialized and deserialized byte order to match the host native byte order. Alternatively, I think changing each of the calls to pack to produce little endian expected value/results would also work. That would be a larger changeset. Requesting host native byte order seems simpler.

Does this mean that the m.unmarshal function won't return the right result for consumers unless they also explicitly pass the bytes order?

I do not think so. The only tests that produce or receive the wrong byte order are the tests that explicitly encode/decode host native buffers. The tests that actually pass data through DBus suggest that other consumers produce and consume their expected byte order.

@cocagne cocagne merged commit 24cd61a into cocagne:master Sep 13, 2024
2 checks passed
@cocagne
Copy link
Owner

cocagne commented Sep 13, 2024

thanks for the PR. I looked at this a few weeks ago but didn't find the root cause of the error.

@hdasch hdasch deleted the bigendian-tests branch September 13, 2024 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants