2019-09-04 17:36:07 +00:00
|
|
|
import unittest, options
|
2020-05-06 16:31:47 +00:00
|
|
|
import chronos, strutils
|
2019-10-29 18:51:48 +00:00
|
|
|
import ../libp2p/[protocols/identify,
|
|
|
|
multiaddress,
|
|
|
|
peerinfo,
|
2020-07-01 06:25:09 +00:00
|
|
|
peerid,
|
2020-06-19 17:29:43 +00:00
|
|
|
stream/connection,
|
2019-10-29 18:51:48 +00:00
|
|
|
multistream,
|
2019-10-03 19:30:22 +00:00
|
|
|
transports/transport,
|
2019-10-29 18:51:48 +00:00
|
|
|
transports/tcptransport,
|
2019-10-03 19:30:22 +00:00
|
|
|
crypto/crypto]
|
2020-05-08 20:10:06 +00:00
|
|
|
import ./helpers
|
2019-08-30 05:16:30 +00:00
|
|
|
|
2019-10-29 18:51:48 +00:00
|
|
|
when defined(nimHasUsed): {.used.}
|
|
|
|
|
2019-08-30 05:16:30 +00:00
|
|
|
suite "Identify":
|
2020-04-21 01:24:42 +00:00
|
|
|
teardown:
|
2020-05-08 20:10:06 +00:00
|
|
|
for tracker in testTrackers():
|
2020-06-27 17:33:34 +00:00
|
|
|
# echo tracker.dump()
|
2020-05-08 20:10:06 +00:00
|
|
|
check tracker.isLeaked() == false
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-08-30 23:45:57 +00:00
|
|
|
test "handle identify message":
|
2019-08-30 05:16:30 +00:00
|
|
|
proc testHandle(): Future[bool] {.async.} =
|
2020-05-31 14:22:49 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2020-05-27 18:33:49 +00:00
|
|
|
let remoteSecKey = PrivateKey.random(ECDSA).get()
|
2019-12-10 20:50:35 +00:00
|
|
|
let remotePeerInfo = PeerInfo.init(remoteSecKey,
|
2020-02-11 17:53:39 +00:00
|
|
|
[ma],
|
|
|
|
["/test/proto1/1.0.0",
|
|
|
|
"/test/proto2/1.0.0"])
|
2019-09-12 02:10:38 +00:00
|
|
|
var serverFut: Future[void]
|
2019-09-26 00:11:20 +00:00
|
|
|
let identifyProto1 = newIdentify(remotePeerInfo)
|
|
|
|
let msListen = newMultistream()
|
|
|
|
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
|
|
|
|
await msListen.handle(conn)
|
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
var transport1 = TcpTransport.init()
|
2019-09-26 00:11:20 +00:00
|
|
|
serverFut = await transport1.listen(ma, connHandler)
|
|
|
|
|
|
|
|
let msDial = newMultistream()
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
2019-09-26 00:11:20 +00:00
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
var peerInfo = PeerInfo.init(PrivateKey.random(ECDSA).get(), [ma])
|
2019-09-26 00:11:20 +00:00
|
|
|
let identifyProto2 = newIdentify(peerInfo)
|
2019-12-07 16:36:39 +00:00
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
2019-12-10 20:50:35 +00:00
|
|
|
let id = await identifyProto2.identify(conn, remotePeerInfo)
|
2019-09-26 00:11:20 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
check id.pubKey.get() == remoteSecKey.getKey().get()
|
2019-09-26 00:11:20 +00:00
|
|
|
check id.addrs[0] == ma
|
|
|
|
check id.protoVersion.get() == ProtoVersion
|
|
|
|
# check id.agentVersion.get() == AgentVersion
|
|
|
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await transport1.close()
|
2019-09-12 02:10:38 +00:00
|
|
|
await serverFut
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-08-30 05:16:30 +00:00
|
|
|
result = true
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await transport2.close()
|
|
|
|
|
2019-08-30 05:16:30 +00:00
|
|
|
check:
|
|
|
|
waitFor(testHandle()) == true
|
2019-09-23 23:02:13 +00:00
|
|
|
|
2019-09-04 17:36:07 +00:00
|
|
|
test "handle failed identify":
|
|
|
|
proc testHandleError() {.async.} =
|
2020-05-31 14:22:49 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2020-05-27 18:33:49 +00:00
|
|
|
var remotePeerInfo = PeerInfo.init(PrivateKey.random(ECDSA).get(), [ma])
|
2019-09-04 17:36:07 +00:00
|
|
|
let identifyProto1 = newIdentify(remotePeerInfo)
|
|
|
|
let msListen = newMultistream()
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
let done = newFuture[void]()
|
|
|
|
|
2019-09-04 17:36:07 +00:00
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
|
|
|
|
await msListen.handle(conn)
|
2020-04-21 01:24:42 +00:00
|
|
|
await conn.close()
|
|
|
|
done.complete()
|
2019-09-04 17:36:07 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport1: TcpTransport = TcpTransport.init()
|
2019-09-12 02:10:38 +00:00
|
|
|
asyncCheck transport1.listen(ma, connHandler)
|
2019-09-04 17:36:07 +00:00
|
|
|
|
|
|
|
let msDial = newMultistream()
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
2019-09-26 00:11:20 +00:00
|
|
|
let conn = await transport2.dial(transport1.ma)
|
2019-09-04 17:36:07 +00:00
|
|
|
|
2020-05-27 18:33:49 +00:00
|
|
|
var localPeerInfo = PeerInfo.init(PrivateKey.random(ECDSA).get(), [ma])
|
2019-09-04 17:36:07 +00:00
|
|
|
let identifyProto2 = newIdentify(localPeerInfo)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
2020-05-27 18:33:49 +00:00
|
|
|
discard await identifyProto2.identify(conn, PeerInfo.init(PrivateKey.random(ECDSA).get()))
|
2020-04-21 01:24:42 +00:00
|
|
|
finally:
|
|
|
|
await done.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
await conn.close()
|
|
|
|
await transport2.close()
|
|
|
|
await transport1.close()
|
2019-09-04 17:36:07 +00:00
|
|
|
|
|
|
|
expect IdentityNoMatchError:
|
|
|
|
waitFor(testHandleError())
|