From 410e0c44a7e6f8246ebcd5e00bae3aaae2842040 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 10 Sep 2014 17:57:43 +0300 Subject: [PATCH] framing: fix data --- amodem/framing.py | 4 ++-- tests/test_framing.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/amodem/framing.py b/amodem/framing.py index 62b291a..546e2a6 100644 --- a/amodem/framing.py +++ b/amodem/framing.py @@ -64,13 +64,13 @@ class Framer(object): length = struct.calcsize(fmt) chunk = bytearray(itertools.islice(data, length)) if len(chunk) < length: - raise StopIteration() + raise ValueError('missing prefix data') return struct.unpack(fmt, chunk) def _take_len(self, data, length): chunk = bytearray(itertools.islice(data, length)) if len(chunk) < length: - raise StopIteration() + raise ValueError('missing payload data') return chunk diff --git a/tests/test_framing.py b/tests/test_framing.py index 29f3bf1..3a13074 100644 --- a/tests/test_framing.py +++ b/tests/test_framing.py @@ -33,3 +33,17 @@ def test_main(data): encoded = framing.encode(data) decoded = framing.decode(encoded) assert bytearray(decoded) == data + +def test_fail(): + encoded = list(framing.encode('')) + encoded[-1] = not encoded[-1] + with pytest.raises(ValueError): + list(framing.decode(encoded)) + +def test_missing(): + f = framing.Framer() + with pytest.raises(ValueError): + list(f.decode(b'\x00')) + with pytest.raises(ValueError): + list(f.decode(b'\x01\x02\x03\x04')) +