2020-07-07 11:14:11 +00:00
|
|
|
import unittest, options, bearssl
|
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-09-21 17:48:19 +00:00
|
|
|
checkTrackers()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
asyncTest "handle identify message":
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
let remoteSecKey = PrivateKey.random(ECDSA, rng[]).get()
|
2020-11-19 02:06:42 +00:00
|
|
|
let remotePeerInfo = PeerInfo.init(
|
|
|
|
remoteSecKey, [ma], ["/test/proto1/1.0.0", "/test/proto2/1.0.0"])
|
2020-11-13 03:44:02 +00:00
|
|
|
var serverFut: Future[void]
|
|
|
|
let identifyProto1 = newIdentify(remotePeerInfo)
|
|
|
|
let msListen = newMultistream()
|
|
|
|
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
|
|
|
|
var transport1 = TcpTransport.init()
|
2020-11-19 02:06:42 +00:00
|
|
|
serverFut = transport1.start(ma)
|
|
|
|
|
|
|
|
proc acceptHandler(): Future[void] {.async, gcsafe.} =
|
|
|
|
let conn = await transport1.accept()
|
|
|
|
await msListen.handle(conn)
|
2020-11-13 03:44:02 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
let acceptFut = acceptHandler()
|
2020-11-13 03:44:02 +00:00
|
|
|
let msDial = newMultistream()
|
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
var peerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma])
|
|
|
|
let identifyProto2 = newIdentify(peerInfo)
|
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
|
|
|
let id = await identifyProto2.identify(conn, remotePeerInfo)
|
|
|
|
|
|
|
|
check id.pubKey.get() == remoteSecKey.getKey().get()
|
|
|
|
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()
|
2020-11-19 02:06:42 +00:00
|
|
|
await acceptFut
|
|
|
|
await transport1.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
await serverFut
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport2.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
asyncTest "handle failed identify":
|
|
|
|
let ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
var remotePeerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma])
|
|
|
|
let identifyProto1 = newIdentify(remotePeerInfo)
|
|
|
|
let msListen = newMultistream()
|
|
|
|
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
let transport1: TcpTransport = TcpTransport.init()
|
2020-11-19 02:06:42 +00:00
|
|
|
asyncCheck transport1.start(ma)
|
|
|
|
|
|
|
|
proc acceptHandler() {.async.} =
|
|
|
|
var conn: Connection
|
|
|
|
try:
|
|
|
|
conn = await transport1.accept()
|
|
|
|
await msListen.handle(conn)
|
|
|
|
except CatchableError:
|
|
|
|
discard
|
|
|
|
finally:
|
|
|
|
await conn.close()
|
2019-09-23 23:02:13 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
let acceptFut = acceptHandler()
|
2020-11-13 03:44:02 +00:00
|
|
|
let msDial = newMultistream()
|
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
var localPeerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma])
|
|
|
|
let identifyProto2 = newIdentify(localPeerInfo)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
expect IdentityNoMatchError:
|
2020-11-19 02:06:42 +00:00
|
|
|
let pi2 = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get())
|
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
|
|
|
discard await identifyProto2.identify(conn, pi2)
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await acceptFut.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
await transport2.stop()
|
|
|
|
await transport1.stop()
|