Fix MultiAddress.protoAddress() bug for fixed arrays.

Add tests for it.
This commit is contained in:
cheatfate 2020-04-28 14:43:44 +03:00
parent 8a22c073c7
commit 290ba712e9
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
2 changed files with 9 additions and 1 deletions

View File

@ -482,7 +482,7 @@ proc protoArgument*(ma: MultiAddress, value: var openarray[byte]): int =
if proto.kind == Fixed: if proto.kind == Fixed:
result = proto.size result = proto.size
if len(value) >= result: if len(value) >= result:
if vb.data.readArray(value) != proto.size: if vb.data.readArray(value.toOpenArray(0, proto.size - 1)) != proto.size:
raise newException(MultiAddressError, "Decoding protocol error") raise newException(MultiAddressError, "Decoding protocol error")
elif proto.kind in {Length, Path}: elif proto.kind in {Length, Path}:
if vb.data.readSeq(buffer) == -1: if vb.data.readSeq(buffer) == -1:

View File

@ -391,3 +391,11 @@ suite "MultiAddress test suite":
discard MultiAddress.init(multiCodec("udp"), -1) discard MultiAddress.init(multiCodec("udp"), -1)
discard MultiAddress.init(multiCodec("dccp"), -1) discard MultiAddress.init(multiCodec("dccp"), -1)
discard MultiAddress.init(multiCodec("sctp"), -1) discard MultiAddress.init(multiCodec("sctp"), -1)
test "MultiAddress protoAddress(fixed) test":
var
address_v4: array[4, byte]
address_v6: array[16, byte]
check:
MultiAddress.init("/ip4/0.0.0.0").protoAddress() == address_v4
MultiAddress.init("/ip6/::0").protoAddress() == address_v6