mirror of https://github.com/vacp2p/nim-libp2p.git
Fail only if all addresses in PeerRecord are invalid (#898)
Fixes https://github.com/waku-org/nwaku/issues/1768
This commit is contained in:
parent
d40d324160
commit
67102873ba
|
@ -51,10 +51,12 @@ proc decode*(
|
||||||
for address in addressInfos:
|
for address in addressInfos:
|
||||||
var addressInfo = AddressInfo()
|
var addressInfo = AddressInfo()
|
||||||
let subProto = initProtoBuffer(address)
|
let subProto = initProtoBuffer(address)
|
||||||
if ? subProto.getField(1, addressInfo.address) == false:
|
let f = subProto.getField(1, addressInfo.address)
|
||||||
return err(ProtoError.RequiredFieldMissing)
|
if f.isOk() and f.get():
|
||||||
|
record.addresses &= addressInfo
|
||||||
|
|
||||||
record.addresses &= addressInfo
|
if record.addresses.len == 0:
|
||||||
|
return err(ProtoError.RequiredFieldMissing)
|
||||||
|
|
||||||
ok(record)
|
ok(record)
|
||||||
|
|
||||||
|
|
|
@ -77,3 +77,41 @@ suite "Signed Routing Record":
|
||||||
buffer = routingRecord.envelope.encode().tryGet()
|
buffer = routingRecord.envelope.encode().tryGet()
|
||||||
|
|
||||||
check SignedPeerRecord.decode(buffer).error == EnvelopeInvalidSignature
|
check SignedPeerRecord.decode(buffer).error == EnvelopeInvalidSignature
|
||||||
|
|
||||||
|
test "Decode doesn't fail if some addresses are invalid":
|
||||||
|
let
|
||||||
|
rng = newRng()
|
||||||
|
privKey = PrivateKey.random(rng[]).tryGet()
|
||||||
|
peerId = PeerId.init(privKey).tryGet()
|
||||||
|
multiAddresses = @[MultiAddress(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()]
|
||||||
|
routingRecord = PeerRecord.init(peerId, multiAddresses, 42)
|
||||||
|
|
||||||
|
buffer = routingRecord.encode()
|
||||||
|
parsedRR = PeerRecord.decode(buffer).tryGet()
|
||||||
|
|
||||||
|
check parsedRR.addresses.len == 1
|
||||||
|
|
||||||
|
test "Decode doesn't fail if there are no addresses":
|
||||||
|
let
|
||||||
|
rng = newRng()
|
||||||
|
privKey = PrivateKey.random(rng[]).tryGet()
|
||||||
|
peerId = PeerId.init(privKey).tryGet()
|
||||||
|
multiAddresses = newSeq[MultiAddress]()
|
||||||
|
routingRecord = PeerRecord.init(peerId, multiAddresses, 42)
|
||||||
|
|
||||||
|
buffer = routingRecord.encode()
|
||||||
|
parsedRR = PeerRecord.decode(buffer).tryGet()
|
||||||
|
|
||||||
|
check parsedRR.addresses.len == 0
|
||||||
|
|
||||||
|
test "Decode fails if all addresses are invalid":
|
||||||
|
let
|
||||||
|
rng = newRng()
|
||||||
|
privKey = PrivateKey.random(rng[]).tryGet()
|
||||||
|
peerId = PeerId.init(privKey).tryGet()
|
||||||
|
multiAddresses = @[MultiAddress(), MultiAddress()]
|
||||||
|
routingRecord = PeerRecord.init(peerId, multiAddresses, 42)
|
||||||
|
|
||||||
|
buffer = routingRecord.encode()
|
||||||
|
|
||||||
|
check PeerRecord.decode(buffer).isErr
|
||||||
|
|
Loading…
Reference in New Issue