Debt: cleanup warnings #426 (#536)

* testswitch cleanups

* Debt: cleanup warnings
Fixes #426
This commit is contained in:
Giovanni Petrantoni 2021-02-26 00:24:49 +09:00 committed by GitHub
parent 2015a878ad
commit eac6cd3dbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 88 deletions

View File

@ -9,6 +9,7 @@
## This module implements Public Key and Private Key interface for libp2p. ## This module implements Public Key and Private Key interface for libp2p.
{.push raises: [Defect].} {.push raises: [Defect].}
from strutils import split, strip, cmpIgnoreCase from strutils import split, strip, cmpIgnoreCase
const libp2p_pki_schemes* {.strdefine.} = "rsa,ed25519,secp256k1,ecnist" const libp2p_pki_schemes* {.strdefine.} = "rsa,ed25519,secp256k1,ecnist"
@ -867,7 +868,7 @@ proc ephemeral*(
keypair = ? EcKeyPair.random(Secp384r1, rng).orError(KeyError) keypair = ? EcKeyPair.random(Secp384r1, rng).orError(KeyError)
elif scheme == Secp521r1: elif scheme == Secp521r1:
keypair = ? EcKeyPair.random(Secp521r1, rng).orError(KeyError) keypair = ? EcKeyPair.random(Secp521r1, rng).orError(KeyError)
ok(EcKeyPair(keypair)) ok(keypair)
proc ephemeral*( proc ephemeral*(
scheme: string, rng: var BrHmacDrbgContext): CryptoResult[EcKeyPair] = scheme: string, rng: var BrHmacDrbgContext): CryptoResult[EcKeyPair] =
@ -1006,7 +1007,7 @@ proc write*[T: PublicKey|PrivateKey](pb: var ProtoBuffer, field: int,
write(pb, field, key.getBytes().tryGet()) write(pb, field, key.getBytes().tryGet())
proc write*(pb: var ProtoBuffer, field: int, sig: Signature) {. proc write*(pb: var ProtoBuffer, field: int, sig: Signature) {.
inline, raises: [Defect, ResultError[CryptoError]].} = inline, raises: [Defect].} =
write(pb, field, sig.getBytes()) write(pb, field, sig.getBytes())
proc initProtoField*(index: int, key: PublicKey|PrivateKey): ProtoField {. proc initProtoField*(index: int, key: PublicKey|PrivateKey): ProtoField {.

View File

@ -55,10 +55,10 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
let conn = await switch2.dial(switch1.peerInfo, TestCodec) let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await conn.writeLp("Hello!") await conn.writeLp("Hello!")
let msg = string.fromBytes(await conn.readLp(1024)) let msg = string.fromBytes(await conn.readLp(1024))
@ -73,8 +73,8 @@ suite "Switch":
# this needs to go at end # this needs to go at end
await allFuturesThrowing(awaiters) await allFuturesThrowing(awaiters)
check not switch1.isConnected(switch2.peerInfo) check not switch1.isConnected(switch2.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
asyncTest "e2e use switch dial proto string with custom matcher": asyncTest "e2e use switch dial proto string with custom matcher":
let done = newFuture[void]() let done = newFuture[void]()
@ -104,10 +104,10 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
let conn = await switch2.dial(switch1.peerInfo, callProto) let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, callProto)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await conn.writeLp("Hello!") await conn.writeLp("Hello!")
let msg = string.fromBytes(await conn.readLp(1024)) let msg = string.fromBytes(await conn.readLp(1024))
@ -122,8 +122,8 @@ suite "Switch":
# this needs to go at end # this needs to go at end
await allFuturesThrowing(awaiters) await allFuturesThrowing(awaiters)
check not switch1.isConnected(switch2.peerInfo) check not switch1.isConnected(switch2.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
asyncTest "e2e should not leak bufferstreams and connections on channel close": asyncTest "e2e should not leak bufferstreams and connections on channel close":
let done = newFuture[void]() let done = newFuture[void]()
@ -148,10 +148,10 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
let conn = await switch2.dial(switch1.peerInfo, TestCodec) let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await conn.writeLp("Hello!") await conn.writeLp("Hello!")
let msg = string.fromBytes(await conn.readLp(1024)) let msg = string.fromBytes(await conn.readLp(1024))
@ -167,8 +167,8 @@ suite "Switch":
# this needs to go at end # this needs to go at end
await allFuturesThrowing(awaiters) await allFuturesThrowing(awaiters)
check not switch1.isConnected(switch2.peerInfo) check not switch1.isConnected(switch2.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
asyncTest "e2e use connect then dial": asyncTest "e2e use connect then dial":
var awaiters: seq[Future[void]] var awaiters: seq[Future[void]]
@ -192,11 +192,11 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
await switch2.connect(switch1.peerInfo) await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs)
let conn = await switch2.dial(switch1.peerInfo, TestCodec) let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await conn.writeLp("Hello!") await conn.writeLp("Hello!")
let msg = string.fromBytes(await conn.readLp(1024)) let msg = string.fromBytes(await conn.readLp(1024))
@ -209,8 +209,8 @@ suite "Switch":
) )
await allFuturesThrowing(awaiters) await allFuturesThrowing(awaiters)
check not switch1.isConnected(switch2.peerInfo) check not switch1.isConnected(switch2.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
asyncTest "e2e should not leak on peer disconnect": asyncTest "e2e should not leak on peer disconnect":
var awaiters: seq[Future[void]] var awaiters: seq[Future[void]]
@ -220,15 +220,15 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
await switch2.connect(switch1.peerInfo) await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await switch2.disconnect(switch1.peerInfo) await switch2.disconnect(switch1.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) check await(checkExpiring((not switch1.isConnected(switch2.peerInfo.peerId))))
checkTracker(LPChannelTrackerName) checkTracker(LPChannelTrackerName)
checkTracker(SecureConnTrackerName) checkTracker(SecureConnTrackerName)
@ -269,15 +269,15 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
await switch2.connect(switch1.peerInfo) await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await switch2.disconnect(switch1.peerInfo) await switch2.disconnect(switch1.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) check await(checkExpiring((not switch1.isConnected(switch2.peerInfo.peerId))))
checkTracker(LPChannelTrackerName) checkTracker(LPChannelTrackerName)
checkTracker(SecureConnTrackerName) checkTracker(SecureConnTrackerName)
@ -324,15 +324,15 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
await switch2.connect(switch1.peerInfo) await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await switch2.disconnect(switch1.peerInfo) await switch2.disconnect(switch1.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) check await(checkExpiring((not switch1.isConnected(switch2.peerInfo.peerId))))
checkTracker(LPChannelTrackerName) checkTracker(LPChannelTrackerName)
checkTracker(SecureConnTrackerName) checkTracker(SecureConnTrackerName)
@ -378,15 +378,15 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
await switch2.connect(switch1.peerInfo) await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await switch2.disconnect(switch1.peerInfo) await switch2.disconnect(switch1.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) check await(checkExpiring((not switch1.isConnected(switch2.peerInfo.peerId))))
checkTracker(LPChannelTrackerName) checkTracker(LPChannelTrackerName)
checkTracker(SecureConnTrackerName) checkTracker(SecureConnTrackerName)
@ -432,15 +432,15 @@ suite "Switch":
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
await switch2.connect(switch1.peerInfo) await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs)
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
await switch2.disconnect(switch1.peerInfo) await switch2.disconnect(switch1.peerInfo.peerId)
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) check await(checkExpiring((not switch1.isConnected(switch2.peerInfo.peerId))))
checkTracker(LPChannelTrackerName) checkTracker(LPChannelTrackerName)
checkTracker(SecureConnTrackerName) checkTracker(SecureConnTrackerName)
@ -495,20 +495,20 @@ suite "Switch":
awaiters.add(await switch2.start()) awaiters.add(await switch2.start())
awaiters.add(await switch3.start()) awaiters.add(await switch3.start())
await switch2.connect(switch1.peerInfo) # should trigger 1st Join event await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs) # should trigger 1st Join event
await switch3.connect(switch1.peerInfo) # should trigger 2nd Join event await switch3.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs) # should trigger 2nd Join event
check switch1.isConnected(switch2.peerInfo) check switch1.isConnected(switch2.peerInfo.peerId)
check switch2.isConnected(switch1.peerInfo) check switch2.isConnected(switch1.peerInfo.peerId)
check switch3.isConnected(switch1.peerInfo) check switch3.isConnected(switch1.peerInfo.peerId)
await switch2.disconnect(switch1.peerInfo) # should trigger 1st Left event await switch2.disconnect(switch1.peerInfo.peerId) # should trigger 1st Left event
await switch3.disconnect(switch1.peerInfo) # should trigger 2nd Left event await switch3.disconnect(switch1.peerInfo.peerId) # should trigger 2nd Left event
check not switch2.isConnected(switch1.peerInfo) check not switch2.isConnected(switch1.peerInfo.peerId)
check not switch3.isConnected(switch1.peerInfo) check not switch3.isConnected(switch1.peerInfo.peerId)
check await(checkExpiring((not switch1.isConnected(switch2.peerInfo)))) check await(checkExpiring((not switch1.isConnected(switch2.peerInfo.peerId))))
check await(checkExpiring((not switch1.isConnected(switch3.peerInfo)))) check await(checkExpiring((not switch1.isConnected(switch3.peerInfo.peerId))))
checkTracker(LPChannelTrackerName) checkTracker(LPChannelTrackerName)
checkTracker(SecureConnTrackerName) checkTracker(SecureConnTrackerName)
@ -555,7 +555,7 @@ suite "Switch":
switches.add(newStandardSwitch( switches.add(newStandardSwitch(
privKey = some(peerInfo.privateKey), privKey = some(peerInfo.privateKey),
rng = rng)) rng = rng))
onConnect = switches[1].connect(switches[0].peerInfo) onConnect = switches[1].connect(switches[0].peerInfo.peerId, switches[0].peerInfo.addrs)
await onConnect await onConnect
await done await done
@ -603,7 +603,7 @@ suite "Switch":
switches.add(newStandardSwitch( switches.add(newStandardSwitch(
privKey = some(peerInfo.privateKey), privKey = some(peerInfo.privateKey),
rng = rng)) rng = rng))
onConnect = switches[i].connect(switches[0].peerInfo) onConnect = switches[i].connect(switches[0].peerInfo.peerId, switches[0].peerInfo.addrs)
await onConnect await onConnect
await done await done
@ -627,7 +627,7 @@ suite "Switch":
try: try:
let conn = await transport.accept() let conn = await transport.accept()
discard await conn.readLp(100) discard await conn.readLp(100)
except CatchableError as exc: except CatchableError:
discard discard
let handlerWait = acceptHandler() let handlerWait = acceptHandler()
@ -687,8 +687,6 @@ suite "Switch":
await allFuturesThrowing(awaiters) await allFuturesThrowing(awaiters)
asyncTest "e2e calling closeWithEOF on the same stream should not assert": asyncTest "e2e calling closeWithEOF on the same stream should not assert":
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
proc handle(conn: Connection, proto: string) {.async, gcsafe.} = proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
discard await conn.readLp(100) discard await conn.readLp(100)
@ -704,8 +702,7 @@ suite "Switch":
var awaiters: seq[Future[void]] var awaiters: seq[Future[void]]
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
var peerId = PeerID.init(PrivateKey.random(ECDSA, rng[]).get()).get() let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec)
let conn = await switch2.dial(switch1.peerInfo, TestCodec)
proc closeReader() {.async.} = proc closeReader() {.async.} =
await conn.closeWithEOF() await conn.closeWithEOF()
@ -728,12 +725,12 @@ suite "Switch":
asyncTest "connect to inexistent peer": asyncTest "connect to inexistent peer":
let switch2 = newStandardSwitch(secureManagers = [SecureProtocol.Noise]) let switch2 = newStandardSwitch(secureManagers = [SecureProtocol.Noise])
let sfut = await switch2.start() discard await switch2.start()
let someAddr = MultiAddress.init("/ip4/127.128.0.99").get() let someAddr = MultiAddress.init("/ip4/127.128.0.99").get()
let seckey = PrivateKey.random(ECDSA, rng[]).get() let seckey = PrivateKey.random(ECDSA, rng[]).get()
let somePeer = PeerInfo.init(secKey, [someAddr]) let somePeer = PeerInfo.init(secKey, [someAddr])
expect(DialFailedError): expect(DialFailedError):
let conn = await switch2.dial(somePeer, TestCodec) discard await switch2.dial(somePeer.peerId, somePeer.addrs, TestCodec)
await switch2.stop() await switch2.stop()
asyncTest "e2e total connection limits on incoming connections": asyncTest "e2e total connection limits on incoming connections":
@ -750,15 +747,15 @@ suite "Switch":
switches.add(switch) switches.add(switch)
awaiters.add(await switch.start()) awaiters.add(await switch.start())
check await switch.connect(destPeerInfo) check await switch.connect(destPeerInfo.peerId, destPeerInfo.addrs)
.withTimeout(100.millis) .withTimeout(1000.millis)
let switchFail = newStandardSwitch() let switchFail = newStandardSwitch()
switches.add(switchFail) switches.add(switchFail)
awaiters.add(await switchFail.start()) awaiters.add(await switchFail.start())
check not(await switchFail.connect(destPeerInfo) check not(await switchFail.connect(destPeerInfo.peerId, destPeerInfo.addrs)
.withTimeout(100.millis)) .withTimeout(1000.millis))
await allFuturesThrowing( await allFuturesThrowing(
allFutures(switches.mapIt( it.stop() ))) allFutures(switches.mapIt( it.stop() )))
@ -779,11 +776,11 @@ suite "Switch":
awaiters.add(await dstSwitch.start()) awaiters.add(await dstSwitch.start())
for s in switches: for s in switches:
check await srcSwitch.connect(s.peerInfo) check await srcSwitch.connect(s.peerInfo.peerId, s.peerInfo.addrs)
.withTimeout(100.millis) .withTimeout(1000.millis)
expect TooManyConnectionsError: expect TooManyConnectionsError:
await srcSwitch.connect(dstSwitch.peerInfo) await srcSwitch.connect(dstSwitch.peerInfo.peerId, dstSwitch.peerInfo.addrs)
switches.add(srcSwitch) switches.add(srcSwitch)
switches.add(dstSwitch) switches.add(dstSwitch)
@ -792,7 +789,7 @@ suite "Switch":
allFutures(switches.mapIt( it.stop() ))) allFutures(switches.mapIt( it.stop() )))
await allFuturesThrowing(awaiters) await allFuturesThrowing(awaiters)
asyncTest "e2e max incoming connection limits": asyncTest "e2e max incoming connection limits":
var awaiters: seq[Future[void]] var awaiters: seq[Future[void]]
var switches: seq[Switch] var switches: seq[Switch]
@ -806,15 +803,15 @@ suite "Switch":
switches.add(switch) switches.add(switch)
awaiters.add(await switch.start()) awaiters.add(await switch.start())
check await switch.connect(destPeerInfo) check await switch.connect(destPeerInfo.peerId, destPeerInfo.addrs)
.withTimeout(100.millis) .withTimeout(1000.millis)
let switchFail = newStandardSwitch() let switchFail = newStandardSwitch()
switches.add(switchFail) switches.add(switchFail)
awaiters.add(await switchFail.start()) awaiters.add(await switchFail.start())
check not(await switchFail.connect(destPeerInfo) check not(await switchFail.connect(destPeerInfo.peerId, destPeerInfo.addrs)
.withTimeout(100.millis)) .withTimeout(1000.millis))
await allFuturesThrowing( await allFuturesThrowing(
allFutures(switches.mapIt( it.stop() ))) allFutures(switches.mapIt( it.stop() )))
@ -835,11 +832,11 @@ suite "Switch":
awaiters.add(await dstSwitch.start()) awaiters.add(await dstSwitch.start())
for s in switches: for s in switches:
check await srcSwitch.connect(s.peerInfo) check await srcSwitch.connect(s.peerInfo.peerId, s.peerInfo.addrs)
.withTimeout(100.millis) .withTimeout(1000.millis)
expect TooManyConnectionsError: expect TooManyConnectionsError:
await srcSwitch.connect(dstSwitch.peerInfo) await srcSwitch.connect(dstSwitch.peerInfo.peerId, dstSwitch.peerInfo.addrs)
switches.add(srcSwitch) switches.add(srcSwitch)
switches.add(dstSwitch) switches.add(dstSwitch)