Skip to content

Commit

Permalink
Does not allow an empty MA (#877)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Apr 6, 2023
1 parent 0041ed4 commit 80cca0e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions libp2p/multiaddress.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type
coder*: Transcoder

MultiAddress* = object
data*: VBuffer
data: VBuffer

MaPatternOp* = enum
Eq, Or, And
Expand Down Expand Up @@ -63,6 +63,10 @@ const
IPPROTO_TCP = Protocol.IPPROTO_TCP
IPPROTO_UDP = Protocol.IPPROTO_UDP

proc data*(ma: MultiAddress): VBuffer =
## Returns the data buffer of the MultiAddress.
return ma.data

proc hash*(a: MultiAddress): Hash =
var h: Hash = 0
h = h !& hash(a.data.buffer)
Expand Down Expand Up @@ -873,6 +877,8 @@ proc getProtocol(name: string): MAProtocol {.inline.} =
proc init*(mtype: typedesc[MultiAddress],
value: string): MaResult[MultiAddress] =
## Initialize MultiAddress object from string representation ``value``.
if len(value) == 0 or value == "/":
return err("multiaddress: Address must not be empty!")
var parts = value.trimRight('/').split('/')
if len(parts[0]) != 0:
err("multiaddress: Invalid MultiAddress, must start with `/`")
Expand Down Expand Up @@ -920,7 +926,7 @@ proc init*(mtype: typedesc[MultiAddress],
data: openArray[byte]): MaResult[MultiAddress] =
## Initialize MultiAddress with array of bytes ``data``.
if len(data) == 0:
err("multiaddress: Address could not be empty!")
err("multiaddress: Address must not be empty!")
else:
var res: MultiAddress
res.data = initVBuffer()
Expand Down
4 changes: 4 additions & 0 deletions tests/testmultiaddress.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest2
import stew/byteutils
import ../libp2p/[multicodec, multiaddress]

when defined(nimHasUsed): {.used.}
Expand Down Expand Up @@ -62,6 +63,8 @@ const
]

FailureVectors = [
"",
"/",
"/ip4",
"/ip4/::1",
"/ip4/fdpsofodsajfdoisa",
Expand Down Expand Up @@ -299,6 +302,7 @@ suite "MultiAddress test suite":
test "go-multiaddr failure test vectors":
for item in FailureVectors:
check MultiAddress.init(item).isErr()
check MultiAddress.init(item.toBytes()).isErr()

test "rust-multiaddr success test vectors":
## Rust test vectors are with changed UDP encoding and without WSS
Expand Down
1 change: 0 additions & 1 deletion tests/testnameresolve.nim
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ suite "Name resolving":
test "getHostname":
check:
MultiAddress.init("/dnsaddr/bootstrap.libp2p.io/").tryGet().getHostname == "bootstrap.libp2p.io"
MultiAddress.init("").tryGet().getHostname == ""
MultiAddress.init("/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN").tryGet().getHostname == "147.75.69.143"
MultiAddress.init("/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN").tryGet().getHostname == "2604:1380:1000:6000::1"
MultiAddress.init("/dns/localhost/udp/0").tryGet().getHostname == "localhost"
Expand Down

0 comments on commit 80cca0e

Please sign in to comment.