mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2025-03-01 16:40:32 +00:00
Raise defect for missing transports or addresses
This commit is contained in:
parent
696b1518ef
commit
d4a65503ac
@ -136,7 +136,7 @@ proc withNameResolver*(b: SwitchBuilder, nameResolver: NameResolver): SwitchBuil
|
||||
b
|
||||
|
||||
proc build*(b: SwitchBuilder): Switch
|
||||
{.raises: [Defect, LPError].} =
|
||||
{.raises: [Defect, LPDefect, LPError].} =
|
||||
|
||||
if b.rng == nil: # newRng could fail
|
||||
raise newException(Defect, "Cannot initialize RNG")
|
||||
@ -210,7 +210,7 @@ proc newStandardSwitch*(
|
||||
maxOut = -1,
|
||||
maxConnsPerPeer = MaxConnectionsPerPeer,
|
||||
nameResolver: NameResolver = nil): Switch
|
||||
{.raises: [Defect, LPError].} =
|
||||
{.raises: [Defect, LPDefect, LPError].} =
|
||||
if SecureProtocol.Secio in secureManagers:
|
||||
quit("Secio is deprecated!") # use of secio is unsafe
|
||||
|
||||
|
@ -9,6 +9,8 @@ type
|
||||
# Base exception type for libp2p
|
||||
LPError* = object of CatchableError
|
||||
|
||||
LPDefect* = object of CatchableError
|
||||
|
||||
func toException*(e: cstring): ref LPError =
|
||||
(ref LPError)(msg: $e)
|
||||
|
||||
|
@ -261,7 +261,13 @@ proc newSwitch*(peerInfo: PeerInfo,
|
||||
connManager: ConnManager,
|
||||
ms: MultistreamSelect,
|
||||
nameResolver: NameResolver = nil): Switch
|
||||
{.raises: [Defect, LPError].} =
|
||||
{.raises: [Defect, LPDefect, LPError].} =
|
||||
|
||||
if transports.len == 0:
|
||||
raise newException(LPDefect, "Provide at least one transport")
|
||||
|
||||
if peerInfo.addrs.len == 0:
|
||||
raise newException(LPDefect, "Provide at least one address")
|
||||
|
||||
if secureManagers.len == 0:
|
||||
raise newException(LPError, "Provide at least one secure manager")
|
||||
|
@ -21,6 +21,7 @@ logScope:
|
||||
topics = "libp2p transport"
|
||||
|
||||
type
|
||||
TransportDefect* = object of Defect
|
||||
ListenErrorCallback* = proc (
|
||||
ma: MultiAddress,
|
||||
err: ref CatchableError): Future[ref TransportListenError]
|
||||
@ -56,11 +57,16 @@ const ListenErrorDefault* =
|
||||
|
||||
method start*(
|
||||
self: Transport,
|
||||
addrs: seq[MultiAddress]) {.base, async, raises: [Defect, TransportListenError].} =
|
||||
addrs: seq[MultiAddress])
|
||||
{.base, async, raises: [Defect, TransportDefect, TransportListenError].} =
|
||||
## start the transport
|
||||
##
|
||||
|
||||
trace "starting transport on addrs", address = $addrs
|
||||
if addrs.len == 0:
|
||||
raise newException(TransportDefect,
|
||||
"Transport requires at least one address to be started")
|
||||
|
||||
self.addrs = addrs
|
||||
self.running = true
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{.used.}
|
||||
|
||||
import options, sequtils, sets, tables
|
||||
import options, sequtils, sets, sugar, tables
|
||||
import chronos
|
||||
import stew/byteutils
|
||||
import nimcrypto/sysrand
|
||||
@ -17,6 +17,7 @@ import ../libp2p/[errors,
|
||||
protocols/secure/secure,
|
||||
muxers/muxer,
|
||||
muxers/mplex/lpchannel,
|
||||
muxers/mplex/mplex,
|
||||
stream/lpstream,
|
||||
nameresolving/nameresolver,
|
||||
nameresolving/mockresolver,
|
||||
@ -1492,3 +1493,60 @@ suite "Switch":
|
||||
check switch.transports[0].addrs == @[maTcp1, maUdp, maTcp2]
|
||||
|
||||
await switch.stop()
|
||||
|
||||
asyncTest "should raise defect if no transports provided during construction":
|
||||
let
|
||||
mAddrs = buildLocalTcpAddrs(3)
|
||||
transport = (upgr: Upgrade) -> Transport => MockTransport.new(upgr)
|
||||
|
||||
# builder should raise defect with addresses, but no transports
|
||||
expect LPDefect:
|
||||
discard SwitchBuilder
|
||||
.new()
|
||||
.withRng(rng) # Give the application RNG
|
||||
.withAddresses(mAddrs) # Our local address(es)
|
||||
.withMplex() # Use Mplex as muxer
|
||||
.withNoise() # Use Noise as secure manager
|
||||
.build()
|
||||
|
||||
# builder should raise defect with transports, but no addresses
|
||||
expect LPDefect:
|
||||
echo "[testswitch] building switch with no addresses"
|
||||
discard SwitchBuilder
|
||||
.new()
|
||||
.withRng(rng) # Give the application RNG
|
||||
.withTransport(transport)
|
||||
.withAddresses(@[])
|
||||
.withMplex() # Use Mplex as muxer
|
||||
.withNoise() # Use Noise as secure manager
|
||||
.build()
|
||||
|
||||
# should raise defect with addresses, but no transports
|
||||
var
|
||||
seckey = PrivateKey.random(rng[]).get()
|
||||
peerInfo = PeerInfo.new(seckey, mAddrs)
|
||||
identify = Identify.new(peerInfo)
|
||||
muxers = initTable[string, MuxerProvider]()
|
||||
|
||||
muxers[MplexCodec] =
|
||||
MuxerProvider.new((conn: Connection) -> Muxer => Mplex.new(conn), MplexCodec)
|
||||
|
||||
expect LPDefect:
|
||||
discard newSwitch(
|
||||
peerInfo = peerInfo,
|
||||
transports = @[],
|
||||
identity = identify,
|
||||
muxers = muxers,
|
||||
connManager = ConnManager.new(),
|
||||
ms = MultistreamSelect.new())
|
||||
|
||||
# should raise defect with transports, but no addresses
|
||||
peerInfo.addrs = @[]
|
||||
expect LPDefect:
|
||||
discard newSwitch(
|
||||
peerInfo = peerInfo,
|
||||
transports = @[transport(Upgrade.new())],
|
||||
identity = identify,
|
||||
muxers = muxers,
|
||||
connManager = ConnManager.new(),
|
||||
ms = MultistreamSelect.new())
|
||||
|
@ -250,6 +250,14 @@ suite "TCP transport":
|
||||
|
||||
await transport.stop()
|
||||
|
||||
asyncTest "should raise Defect on start with no addresses":
|
||||
let transport = TcpTransport.new(upgrade = Upgrade())
|
||||
|
||||
expect TransportDefect:
|
||||
await transport.start(@[])
|
||||
|
||||
await transport.stop()
|
||||
|
||||
commonTransportTest(
|
||||
"TcpTransport",
|
||||
proc (): Transport = TcpTransport.new(upgrade = Upgrade()),
|
||||
|
@ -180,6 +180,14 @@ suite "WebSocket transport":
|
||||
|
||||
await transport.stop()
|
||||
|
||||
asyncTest "should raise Defect on start with no addresses":
|
||||
let transport = WsTransport.new(upgrade = Upgrade())
|
||||
|
||||
expect TransportDefect:
|
||||
await transport.start(@[])
|
||||
|
||||
await transport.stop()
|
||||
|
||||
commonTransportTest(
|
||||
"WebSocket",
|
||||
proc (): Transport = WsTransport.new(Upgrade()),
|
||||
|
Loading…
x
Reference in New Issue
Block a user