2019-09-04 17:36:07 +00:00
|
|
|
import unittest, options
|
2019-08-30 05:16:30 +00:00
|
|
|
import chronos, strutils, sequtils
|
2019-09-06 07:13:17 +00:00
|
|
|
import ../libp2p/protocols/identify,
|
|
|
|
../libp2p/multiaddress,
|
|
|
|
../libp2p/peerinfo,
|
|
|
|
../libp2p/peer,
|
|
|
|
../libp2p/connection,
|
|
|
|
../libp2p/multistream,
|
|
|
|
../libp2p/transports/transport,
|
|
|
|
../libp2p/transports/tcptransport,
|
|
|
|
../libp2p/protocols/protocol,
|
2019-08-30 15:28:07 +00:00
|
|
|
../libp2p/crypto/crypto
|
2019-08-30 05:16:30 +00:00
|
|
|
|
|
|
|
suite "Identify":
|
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.} =
|
2019-08-30 23:45:57 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53360")
|
2019-08-30 05:16:30 +00:00
|
|
|
|
|
|
|
let remoteSeckey = PrivateKey.random(RSA)
|
2019-09-12 02:10:38 +00:00
|
|
|
var remotePeerInfo: PeerInfo
|
|
|
|
var serverFut: Future[void]
|
|
|
|
var transport: TcpTransport
|
2019-08-30 05:16:30 +00:00
|
|
|
proc receiver() {.async.} =
|
2019-09-12 02:10:38 +00:00
|
|
|
remotePeerInfo.peerId = some(PeerID.init(remoteSeckey))
|
|
|
|
remotePeerInfo.addrs.add(ma)
|
|
|
|
remotePeerInfo.protocols.add("/test/proto1/1.0.0")
|
|
|
|
remotePeerInfo.protocols.add("/test/proto2/1.0.0")
|
2019-08-30 05:16:30 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
let identifyProto = newIdentify(remotePeerInfo)
|
2019-08-30 05:16:30 +00:00
|
|
|
let msListen = newMultistream()
|
|
|
|
|
2019-08-30 23:45:57 +00:00
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto)
|
2019-08-30 05:16:30 +00:00
|
|
|
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
|
|
|
|
await msListen.handle(conn)
|
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
transport = newTransport(TcpTransport)
|
|
|
|
serverFut = await transport.listen(ma, connHandler)
|
2019-08-30 05:16:30 +00:00
|
|
|
|
|
|
|
proc sender() {.async.} =
|
|
|
|
let msDial = newMultistream()
|
|
|
|
let transport: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport.dial(ma)
|
|
|
|
|
|
|
|
let seckey = PrivateKey.random(RSA)
|
|
|
|
var peerInfo: PeerInfo
|
2019-09-12 02:10:38 +00:00
|
|
|
peerInfo.peerId = some(PeerID.init(seckey))
|
2019-08-30 05:16:30 +00:00
|
|
|
peerInfo.addrs.add(ma)
|
|
|
|
|
2019-09-03 20:41:55 +00:00
|
|
|
let identifyProto = newIdentify(peerInfo)
|
2019-08-30 05:16:30 +00:00
|
|
|
let res = await msDial.select(conn, IdentifyCodec)
|
2019-09-12 02:10:38 +00:00
|
|
|
let id = await identifyProto.identify(conn, remotePeerInfo)
|
2019-08-30 05:16:30 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
check id.pubKey.get() == remoteSeckey.getKey()
|
2019-08-30 05:16:30 +00:00
|
|
|
check id.addrs[0] == ma
|
2019-09-12 02:10:38 +00:00
|
|
|
check id.protoVersion.get() == ProtoVersion
|
|
|
|
# check id.agentVersion.get() == AgentVersion
|
2019-09-04 17:36:07 +00:00
|
|
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
2019-08-30 05:16:30 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
await allFutures(sender(), receiver())
|
|
|
|
await transport.close()
|
|
|
|
await serverFut
|
2019-08-30 05:16:30 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
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.} =
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53361")
|
|
|
|
|
|
|
|
let remoteSeckey = PrivateKey.random(RSA)
|
|
|
|
var remotePeerInfo: PeerInfo
|
2019-09-12 02:10:38 +00:00
|
|
|
remotePeerInfo.peerId = some(PeerID.init(remoteSeckey))
|
2019-09-04 17:36:07 +00:00
|
|
|
remotePeerInfo.addrs.add(ma)
|
|
|
|
|
|
|
|
let identifyProto1 = newIdentify(remotePeerInfo)
|
|
|
|
let msListen = newMultistream()
|
|
|
|
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
|
|
|
|
await msListen.handle(conn)
|
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2019-09-12 02:10:38 +00:00
|
|
|
asyncCheck transport1.listen(ma, connHandler)
|
2019-09-04 17:36:07 +00:00
|
|
|
|
|
|
|
let msDial = newMultistream()
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(ma)
|
|
|
|
|
|
|
|
let seckey = PrivateKey.random(RSA)
|
|
|
|
var localPeerInfo: PeerInfo
|
2019-09-12 02:10:38 +00:00
|
|
|
localPeerInfo.peerId = some(PeerID.init(seckey))
|
2019-09-04 17:36:07 +00:00
|
|
|
localPeerInfo.addrs.add(ma)
|
|
|
|
|
|
|
|
let identifyProto2 = newIdentify(localPeerInfo)
|
|
|
|
let res = await msDial.select(conn, IdentifyCodec)
|
|
|
|
|
|
|
|
let wrongSec = PrivateKey.random(RSA)
|
|
|
|
var wrongRemotePeer: PeerInfo
|
2019-09-12 02:10:38 +00:00
|
|
|
wrongRemotePeer.peerId = some(PeerID.init(wrongSec))
|
2019-09-04 17:36:07 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
let id = await identifyProto2.identify(conn, wrongRemotePeer)
|
2019-09-04 17:36:07 +00:00
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
expect IdentityNoMatchError:
|
|
|
|
waitFor(testHandleError())
|