1
0

lnmsg: add more tests (for encode_msg, decode_msg)

This commit is contained in:
SomberNight
2020-03-16 03:31:39 +01:00
parent 71a4302ec0
commit c69937395e
3 changed files with 216 additions and 8 deletions

View File

@@ -12,6 +12,7 @@ class FieldEncodingNotMinimal(MalformedMsg): pass
class UnknownMandatoryTLVRecordType(MalformedMsg): pass
class MsgTrailingGarbage(MalformedMsg): pass
class MsgInvalidFieldOrder(MalformedMsg): pass
class UnexpectedFieldSizeForEncoder(MalformedMsg): pass
def _num_remaining_bytes_to_read(fd: io.BytesIO) -> int:
@@ -210,7 +211,7 @@ def _write_field(*, fd: io.BytesIO, field_type: str, count: Union[int, str],
if not isinstance(value, (bytes, bytearray)):
raise Exception(f"can only write bytes into fd. got: {value!r}")
if count != "..." and total_len != len(value):
raise Exception(f"unexpected field size. expected: {total_len}, got {len(value)}")
raise UnexpectedFieldSizeForEncoder(f"expected: {total_len}, got {len(value)}")
nbytes_written = fd.write(value)
if nbytes_written != len(value):
raise Exception(f"tried to write {len(value)} bytes, but only wrote {nbytes_written}!?")