framing: fix data

This commit is contained in:
Roman Zeyde
2014-09-10 17:57:43 +03:00
parent 0bdf8b8fb1
commit 410e0c44a7
2 changed files with 16 additions and 2 deletions

View File

@@ -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

View File

@@ -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'))