2019-09-12 12:02:25 +00:00
|
|
|
import unittest, tables, options
|
2019-12-04 04:44:54 +00:00
|
|
|
import chronos
|
2019-10-29 18:51:48 +00:00
|
|
|
import ../libp2p/[switch,
|
2019-09-30 15:48:40 +00:00
|
|
|
multistream,
|
2019-10-29 18:51:48 +00:00
|
|
|
protocols/identify,
|
2019-09-30 15:48:40 +00:00
|
|
|
connection,
|
|
|
|
transports/transport,
|
|
|
|
transports/tcptransport,
|
2019-10-29 18:51:48 +00:00
|
|
|
multiaddress,
|
2019-09-30 15:48:40 +00:00
|
|
|
peerinfo,
|
2019-10-29 18:51:48 +00:00
|
|
|
crypto/crypto,
|
2019-09-30 15:48:40 +00:00
|
|
|
peer,
|
2019-10-29 18:51:48 +00:00
|
|
|
protocols/protocol,
|
2019-09-30 15:48:40 +00:00
|
|
|
muxers/muxer,
|
2019-10-29 18:51:48 +00:00
|
|
|
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
|
|
|
|
2019-10-29 18:51:48 +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.} =
|
2019-10-29 18:51:48 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
suite "Switch":
|
2019-10-29 18:51:48 +00:00
|
|
|
test "e2e use switch":
|
2019-12-04 04:44:54 +00:00
|
|
|
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) {.gcsafe.}=
|
2019-09-04 22:00:39 +00:00
|
|
|
let seckey = PrivateKey.random(RSA)
|
|
|
|
var peerInfo: PeerInfo
|
2019-09-12 12:02:25 +00:00
|
|
|
peerInfo.peerId = some(PeerID.init(seckey))
|
2019-09-04 22:00:39 +00:00
|
|
|
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()
|
2019-09-14 13:55:52 +00:00
|
|
|
let secureManagers = [(SecioCodec, Secure(newSecio(seckey)))].toTable()
|
2019-12-04 04:44:54 +00:00
|
|
|
let switch = newSwitch(peerInfo,
|
|
|
|
transports,
|
|
|
|
identify,
|
|
|
|
muxers,
|
|
|
|
secureManagers)
|
2019-09-04 22:00:39 +00:00
|
|
|
result = (switch, peerInfo)
|
|
|
|
|
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-10-29 18:51:48 +00:00
|
|
|
|
2019-08-31 17:58:49 +00:00
|
|
|
var peerInfo1, peerInfo2: PeerInfo
|
|
|
|
var switch1, switch2: Switch
|
|
|
|
(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)
|
2019-09-13 00:57:11 +00:00
|
|
|
var switch1Fut = await switch1.start()
|
2019-08-31 17:58:49 +00:00
|
|
|
|
|
|
|
(switch2, peerInfo2) = createSwitch(ma2)
|
2019-09-13 00:57:11 +00:00
|
|
|
var switch2Fut = 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
|
|
|
|
|
2019-09-13 00:57:11 +00:00
|
|
|
discard allFutures(switch1.stop(), switch2.stop())
|
2019-08-31 17:58:49 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testSwitch()) == true
|