2020-11-18 00:48:26 +00:00
|
|
|
import unittest, sequtils
|
2020-07-17 15:36:48 +00:00
|
|
|
import chronos
|
|
|
|
import ../libp2p/[connmanager,
|
|
|
|
stream/connection,
|
|
|
|
crypto/crypto,
|
|
|
|
muxers/muxer,
|
2020-11-18 00:48:26 +00:00
|
|
|
peerinfo,
|
|
|
|
errors]
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
import helpers
|
|
|
|
|
|
|
|
type
|
|
|
|
TestMuxer = ref object of Muxer
|
|
|
|
peerInfo: PeerInfo
|
|
|
|
|
|
|
|
method newStream*(
|
|
|
|
m: TestMuxer,
|
|
|
|
name: string = "",
|
|
|
|
lazy: bool = false):
|
|
|
|
Future[Connection] {.async, gcsafe.} =
|
|
|
|
result = Connection.init(m.peerInfo, Direction.Out)
|
|
|
|
|
|
|
|
suite "Connection Manager":
|
|
|
|
teardown:
|
2020-09-21 17:48:19 +00:00
|
|
|
checkTrackers()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "add and retrieve a connection":
|
2020-07-17 15:36:48 +00:00
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
|
|
|
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
check conn in connMngr
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let peerConn = connMngr.selectConn(peer.peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
check peerConn == conn
|
|
|
|
check peerConn.dir == Direction.In
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
|
2020-12-03 01:24:48 +00:00
|
|
|
asyncTest "shouldn't allow a closed connection":
|
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
expect CatchableError:
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
|
|
|
|
await connMngr.close()
|
|
|
|
|
|
|
|
asyncTest "shouldn't allow an EOFed connection":
|
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
|
|
|
conn.isEof = true
|
|
|
|
|
|
|
|
expect CatchableError:
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await connMngr.close()
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "add and retrieve a muxer":
|
2020-07-17 15:36:48 +00:00
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
|
|
|
let muxer = new Muxer
|
|
|
|
muxer.connection = conn
|
|
|
|
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
check muxer in connMngr
|
|
|
|
|
|
|
|
let peerMuxer = connMngr.selectMuxer(conn)
|
|
|
|
check peerMuxer == muxer
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
|
2020-12-03 01:24:48 +00:00
|
|
|
asyncTest "shouldn't allow a muxer for an untracked connection":
|
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
|
|
|
let muxer = new Muxer
|
|
|
|
muxer.connection = conn
|
|
|
|
|
|
|
|
expect CatchableError:
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await muxer.close()
|
|
|
|
await connMngr.close()
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "get conn with direction":
|
2020-07-17 15:36:48 +00:00
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn1 = Connection.init(peer, Direction.Out)
|
|
|
|
let conn2 = Connection.init(peer, Direction.In)
|
|
|
|
|
|
|
|
connMngr.storeConn(conn1)
|
|
|
|
connMngr.storeConn(conn2)
|
|
|
|
check conn1 in connMngr
|
|
|
|
check conn2 in connMngr
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let outConn = connMngr.selectConn(peer.peerId, Direction.Out)
|
|
|
|
let inConn = connMngr.selectConn(peer.peerId, Direction.In)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
check outConn != inConn
|
|
|
|
check outConn.dir == Direction.Out
|
|
|
|
check inConn.dir == Direction.In
|
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "get muxed stream for peer":
|
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new TestMuxer
|
|
|
|
muxer.peerInfo = peer
|
|
|
|
muxer.connection = conn
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
check muxer in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-24 06:37:45 +00:00
|
|
|
let stream = await connMngr.getStream(peer.peerId)
|
2020-11-18 00:48:26 +00:00
|
|
|
check not(isNil(stream))
|
|
|
|
check stream.peerInfo == peer
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
await stream.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "get stream from directed connection":
|
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new TestMuxer
|
|
|
|
muxer.peerInfo = peer
|
|
|
|
muxer.connection = conn
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
check muxer in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-24 06:37:45 +00:00
|
|
|
let stream1 = await connMngr.getStream(peer.peerId, Direction.In)
|
2020-11-18 00:48:26 +00:00
|
|
|
check not(isNil(stream1))
|
2020-11-24 06:37:45 +00:00
|
|
|
let stream2 = await connMngr.getStream(peer.peerId, Direction.Out)
|
2020-11-18 00:48:26 +00:00
|
|
|
check isNil(stream2)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
await stream1.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "get stream from any connection":
|
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let muxer = new TestMuxer
|
|
|
|
muxer.peerInfo = peer
|
|
|
|
muxer.connection = conn
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
check muxer in connMngr
|
|
|
|
|
2020-11-24 06:37:45 +00:00
|
|
|
let stream = await connMngr.getStream(conn)
|
2020-11-18 00:48:26 +00:00
|
|
|
check not(isNil(stream))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
|
|
|
await stream.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "should raise on too many connections":
|
|
|
|
let connMngr = ConnManager.init(1)
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
|
|
|
|
connMngr.storeConn(Connection.init(peer, Direction.In))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let conns = @[
|
|
|
|
Connection.init(peer, Direction.In),
|
|
|
|
Connection.init(peer, Direction.In)]
|
2020-07-17 15:36:48 +00:00
|
|
|
|
|
|
|
expect TooManyConnections:
|
2020-11-18 00:48:26 +00:00
|
|
|
connMngr.storeConn(conns[0])
|
|
|
|
connMngr.storeConn(conns[1])
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFutures(conns.mapIt( it.close() )))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "cleanup on connection close":
|
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
|
|
|
let conn = Connection.init(peer, Direction.In)
|
|
|
|
let muxer = new Muxer
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
muxer.connection = conn
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
check conn in connMngr
|
|
|
|
check muxer in connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await conn.close()
|
|
|
|
await sleepAsync(10.millis)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
check conn notin connMngr
|
|
|
|
check muxer notin connMngr
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
asyncTest "drop connections for peer":
|
|
|
|
let connMngr = ConnManager.init()
|
|
|
|
let peer = PeerInfo.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet())
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
for i in 0..<2:
|
|
|
|
let dir = if i mod 2 == 0:
|
|
|
|
Direction.In else:
|
|
|
|
Direction.Out
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
let conn = Connection.init(peer, dir)
|
|
|
|
let muxer = new Muxer
|
|
|
|
muxer.connection = conn
|
|
|
|
|
|
|
|
connMngr.storeConn(conn)
|
|
|
|
connMngr.storeMuxer(muxer)
|
|
|
|
|
|
|
|
check conn in connMngr
|
|
|
|
check muxer in connMngr
|
|
|
|
check not(isNil(connMngr.selectConn(peer.peerId, dir)))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
check peer.peerId in connMngr
|
|
|
|
await connMngr.dropPeer(peer.peerId)
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
check peer.peerId notin connMngr
|
|
|
|
check isNil(connMngr.selectConn(peer.peerId, Direction.In))
|
|
|
|
check isNil(connMngr.selectConn(peer.peerId, Direction.Out))
|
2020-07-17 15:36:48 +00:00
|
|
|
|
2020-11-18 00:48:26 +00:00
|
|
|
await connMngr.close()
|