nim-libp2p/tests/testidentify.nim

101 lines
3.4 KiB
Nim
Raw Normal View History

import unittest, options
2019-08-30 05:16:30 +00:00
import chronos, strutils, sequtils
import ../libp2p/identify, ../libp2p/multiaddress,
2019-08-30 15:28:07 +00:00
../libp2p/peerinfo, ../libp2p/peer,
../libp2p/connection, ../libp2p/identify,
../libp2p/multistream, ../libp2p/transport,
../libp2p/tcptransport, ../libp2p/protocol,
../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)
proc receiver() {.async.} =
var peerInfo: PeerInfo
peerInfo.peerId = PeerID.init(remoteSeckey)
peerInfo.addrs.add(ma)
peerInfo.protocols.add("/test/proto1/1.0.0")
peerInfo.protocols.add("/test/proto2/1.0.0")
2019-08-30 05:16:30 +00:00
2019-09-03 20:41:55 +00:00
let identifyProto = newIdentify(peerInfo)
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)
let transport: TcpTransport = newTransport(TcpTransport)
await transport.listen(ma, connHandler)
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
peerInfo.peerId = PeerID.init(seckey)
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-08-30 23:45:57 +00:00
let id = await identifyProto.identify(conn)
2019-08-30 05:16:30 +00:00
await conn.close()
check id.pubKey == remoteSeckey.getKey()
check id.addrs[0] == ma
check id.protoVersion == ProtoVersion
check id.agentVersion == AgentVersion
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
2019-08-30 05:16:30 +00:00
await allFutures(receiver(), sender())
result = true
check:
waitFor(testHandle()) == true
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
remotePeerInfo.peerId = PeerID.init(remoteSeckey)
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)
await transport1.listen(ma, connHandler)
let msDial = newMultistream()
let transport2: TcpTransport = newTransport(TcpTransport)
let conn = await transport2.dial(ma)
let seckey = PrivateKey.random(RSA)
var localPeerInfo: PeerInfo
localPeerInfo.peerId = PeerID.init(seckey)
localPeerInfo.addrs.add(ma)
let identifyProto2 = newIdentify(localPeerInfo)
let res = await msDial.select(conn, IdentifyCodec)
let wrongSec = PrivateKey.random(RSA)
var wrongRemotePeer: PeerInfo
wrongRemotePeer.peerId = PeerID.init(wrongSec)
let id = await identifyProto2.identify(conn, some(wrongRemotePeer))
await conn.close()
expect IdentityNoMatchError:
waitFor(testHandleError())
2019-08-30 05:16:30 +00:00