mirror of
https://github.com/romanz/amodem.git
synced 2026-03-17 07:05:59 +08:00
framing: fix data
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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'))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user