nim-libp2p/tests/testswitch.nim

160 lines
5.2 KiB
Nim
Raw Normal View History

2020-01-07 08:06:27 +00:00
import unittest, tables
import chronos
import chronicles
import nimcrypto/sysrand
import ../libp2p/[switch,
2019-09-30 15:48:40 +00:00
multistream,
protocols/identify,
2019-09-30 15:48:40 +00:00
connection,
transports/transport,
transports/tcptransport,
multiaddress,
2019-09-30 15:48:40 +00:00
peerinfo,
crypto/crypto,
2019-09-30 15:48:40 +00:00
peer,
protocols/protocol,
2019-09-30 15:48:40 +00:00
muxers/muxer,
muxers/mplex/mplex,
2019-09-30 15:48:40 +00:00
muxers/mplex/types,
protocols/secure/secio,
protocols/secure/secure]
2019-08-31 17:58:49 +00:00
when defined(nimHasUsed): {.used.}
2019-08-31 17:58:49 +00:00
const TestCodec = "/test/proto/1.0.0"
type
TestProto = ref object of LPProtocol
method init(p: TestProto) {.gcsafe.} =
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
2019-08-31 17:58:49 +00:00
let msg = cast[string](await conn.readLp())
check "Hello!" == msg
await conn.writeLp("Hello!")
2019-09-08 07:43:33 +00:00
await conn.close()
2019-08-31 17:58:49 +00:00
2019-08-31 18:52:56 +00:00
p.codec = TestCodec
2019-08-31 17:58:49 +00:00
p.handler = handle
2019-12-23 18:44:51 +00:00
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) =
var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA))
peerInfo.addrs.add(ma)
let identify = newIdentify(peerInfo)
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(newTransport(TcpTransport))]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let secureManagers = [(SecioCodec, Secure(newSecio(peerInfo.privateKey)))].toTable()
let switch = newSwitch(peerInfo,
2020-02-12 14:43:42 +00:00
transports,
identify,
muxers,
secureManagers)
2019-12-23 18:44:51 +00:00
result = (switch, peerInfo)
2019-09-04 22:00:39 +00:00
2019-12-23 18:44:51 +00:00
suite "Switch":
test "e2e use switch dial proto string":
2019-08-31 17:58:49 +00:00
proc testSwitch(): Future[bool] {.async, gcsafe.} =
2019-09-30 15:48:40 +00:00
let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
2019-08-31 17:58:49 +00:00
var peerInfo1, peerInfo2: PeerInfo
var switch1, switch2: Switch
2020-02-12 14:43:42 +00:00
var awaiters: seq[Future[void]]
2019-08-31 17:58:49 +00:00
(switch1, peerInfo1) = createSwitch(ma1)
2019-09-05 15:19:39 +00:00
2019-09-04 22:00:39 +00:00
let testProto = new TestProto
2019-09-05 15:19:39 +00:00
testProto.init()
2019-09-04 22:00:39 +00:00
testProto.codec = TestCodec
2019-08-31 17:58:49 +00:00
switch1.mount(testProto)
(switch2, peerInfo2) = createSwitch(ma2)
2020-02-12 14:43:42 +00:00
awaiters.add(await switch1.start())
2020-01-07 08:06:27 +00:00
awaiters.add(await switch2.start())
2019-09-30 15:48:40 +00:00
let conn = await switch2.dial(switch1.peerInfo, TestCodec)
2019-08-31 17:58:49 +00:00
await conn.writeLp("Hello!")
let msg = cast[string](await conn.readLp())
check "Hello!" == msg
2020-01-07 08:06:27 +00:00
await allFutures(switch1.stop(), switch2.stop())
await allFutures(awaiters)
2019-08-31 17:58:49 +00:00
result = true
check:
waitFor(testSwitch()) == true
2019-12-23 18:44:51 +00:00
test "e2e use switch no proto string":
proc testSwitch(): Future[bool] {.async, gcsafe.} =
let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
var peerInfo1, peerInfo2: PeerInfo
var switch1, switch2: Switch
2020-01-07 08:06:27 +00:00
var awaiters: seq[Future[void]]
2019-12-23 18:44:51 +00:00
(switch1, peerInfo1) = createSwitch(ma1)
let testProto = new TestProto
testProto.init()
testProto.codec = TestCodec
switch1.mount(testProto)
2019-12-23 18:44:51 +00:00
(switch2, peerInfo2) = createSwitch(ma2)
awaiters.add(await switch1.start())
2020-01-07 08:06:27 +00:00
awaiters.add(await switch2.start())
await switch2.connect(switch1.peerInfo)
let conn = await switch2.dial(switch1.peerInfo, TestCodec)
await conn.writeLp("Hello!")
let msg = cast[string](await conn.readLp())
check "Hello!" == msg
2019-12-23 18:44:51 +00:00
await allFutures(switch1.stop(), switch2.stop())
await allFutures(awaiters)
2019-12-23 18:44:51 +00:00
result = true
check:
waitFor(testSwitch()) == true
# test "e2e: handle read + secio fragmented":
# proc testListenerDialer(): Future[bool] {.async.} =
# let
# server: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
# serverInfo = PeerInfo.init(PrivateKey.random(RSA), [server])
# serverNoise = newSecio(serverInfo.privateKey)
# readTask = newFuture[void]()
# var hugePayload = newSeq[byte](0x1200000)
# check randomBytes(hugePayload) == hugePayload.len
# trace "Sending huge payload", size = hugePayload.len
# proc connHandler(conn: Connection) {.async, gcsafe.} =
# let sconn = await serverNoise.secure(conn)
# defer:
# await sconn.close()
# let msg = await sconn.read(0x1200000)
# check msg == hugePayload
# readTask.complete()
# let
# transport1: TcpTransport = newTransport(TcpTransport)
# asyncCheck await transport1.listen(server, connHandler)
# let
# transport2: TcpTransport = newTransport(TcpTransport)
# clientInfo = PeerInfo.init(PrivateKey.random(RSA), [transport1.ma])
# clientNoise = newSecio(clientInfo.privateKey)
# conn = await transport2.dial(transport1.ma)
# sconn = await clientNoise.secure(conn)
# await sconn.write(hugePayload)
# await readTask
# await sconn.close()
# await transport1.close()
# result = true
# check:
# waitFor(testListenerDialer()) == true