From a8e383fa8d5dc173d0ba497d8d86caf3bbfefb1e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mart=C3=ADn=20Ra=C3=BAl=20Villalba?= Date: Wed, 5 Oct 2011 13:50:11 -0300 Subject: [PATCH] Allow Message to better support streaming (#7) --- src/ant/core/message.py | 7 +++++++ src/ant/core/tests/message_tests.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/ant/core/message.py b/src/ant/core/message.py index 3433f26..a20a0a3 100644 --- a/src/ant/core/message.py +++ b/src/ant/core/message.py @@ -79,11 +79,18 @@ class Message(object): return raw def decode(self, raw): + if len(raw) < 5: + raise MessageError('Could not decode (message is incomplete).') + sync, length, type_ = struct.unpack('BBB', raw[:3]) + if sync != MESSAGE_TX_SYNC: raise MessageError('Could not decode (expected TX sync).') if length > 9: raise MessageError('Could not decode (payload too long).') + if len(raw) < (length + 4): + raise MessageError('Could not decode (message is incomplete).') + self.setType(type_) self.setPayload(raw[3:length + 3]) diff --git a/src/ant/core/tests/message_tests.py b/src/ant/core/tests/message_tests.py index 7a21e8a..ca7882b 100644 --- a/src/ant/core/tests/message_tests.py +++ b/src/ant/core/tests/message_tests.py @@ -78,6 +78,10 @@ class MessageTest(unittest.TestCase): self.assertTrue(isinstance(handler, ChannelAssignMessage)) self.assertRaises(MessageError, self.message.getHandler, '\xA4\x03\xFF\x00\x00\x00\xE5') + self.assertRaises(MessageError, self.message.getHandler, + '\xA4\x03\x42') + self.assertRaises(MessageError, self.message.getHandler, + '\xA4\x05\x42\x00\x00\x00\x00') class ChannelMessageTest(unittest.TestCase): def setUp(self): -- 2.11.4.GIT