ocpp icon indicating copy to clipboard operation
ocpp copied to clipboard

ocpp.message.unpack can crash with UnicodeDecodeError

Open OrangeTux opened this issue 6 years ago • 1 comments

If ocpp.messages.unpack is provided with some special data it can crash with a UnicodeDecodeError.

I found this bug while using Hypothesis with the following test:

@given(binary())
def test_unpack_and_pack(data):
    try:
        assert unpack(data) == pack(data)
    except Exception as e:
        assert type(e) in [FormatViolationError, ProtocolError, PropertyConstraintViolationError]

The tests fails if data holds b'\x80.


data = b'\x80'

    @given(binary())
    def test_unpack_and_pack(data):
        try:
            assert unpack(data) == pack(data)
        except Exception as e:
>           assert type(e) in [FormatViolationError, ProtocolError,
                    PropertyConstraintViolationError]
E           AssertionError: assert <class 'UnicodeDecodeError'> in [<class 'ocpp.exceptions.FormatViolationError'>, <class 'ocpp.exceptions.ProtocolError'>, <class 'ocpp.exceptions.PropertyConstraintViolationError'>]
E            +  where <class 'UnicodeDecodeError'> = type(UnicodeDecodeError('utf-8', b'\x80', 0, 1, 'invalid start byte'))

tests/test_messages.py:189: AssertionError
-------------------------------------------------------------------------------- Hypothesis ---------------------------------------------------------------------------------
Falsifying example: test_unpack_and_pack(data=b'\x80')

OrangeTux avatar May 29 '19 11:05 OrangeTux

As this is quite old, I've re-run the test and it still exist

`test_hypothesis.py::test_unpack_and_pack FAILED [100%] test_hypothesis.py:6 (test_unpack_and_pack) <class 'UnicodeDecodeError'> != [<class 'ocpp.exceptions.FormatViolationError'>, <class 'ocpp.exceptions.ProtocolError'>, <class 'ocpp.exceptions.PropertyConstraintViolationError'>]

data = b'\x80'

@given(binary())
def test_unpack_and_pack(data):
    try:
      assert unpack(data) == pack(data)

test_hypothesis.py:10:

============================== 1 failed in 0.58s ===============================`

Jared-Newell-Mobility avatar Nov 15 '23 14:11 Jared-Newell-Mobility