nim-libp2p/tests/testidentify.nim

107 lines
3.5 KiB
Nim
Raw Normal View History

import unittest, options
2019-08-30 05:16:30 +00:00
import chronos, strutils, sequtils
import ../libp2p/[protocols/identify,
multiaddress,
peerinfo,
peer,
connection,
multistream,
2019-10-03 19:30:22 +00:00
transports/transport,
transports/tcptransport,
protocols/protocol,
2019-10-03 19:30:22 +00:00
crypto/crypto]
2019-08-30 05:16:30 +00:00
when defined(nimHasUsed): {.used.}
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.} =
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
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]
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")
let identifyProto1 = newIdentify(remotePeerInfo)
let msListen = newMultistream()
msListen.addHandler(IdentifyCodec, identifyProto1)
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
await msListen.handle(conn)
var transport1 = newTransport(TcpTransport)
serverFut = await transport1.listen(ma, connHandler)
let msDial = newMultistream()
let transport2: TcpTransport = newTransport(TcpTransport)
let conn = await transport2.dial(transport1.ma)
let seckey = PrivateKey.random(RSA)
var peerInfo: PeerInfo
peerInfo.peerId = some(PeerID.init(seckey))
peerInfo.addrs.add(ma)
let identifyProto2 = newIdentify(peerInfo)
let res = await msDial.select(conn, IdentifyCodec)
let id = await identifyProto2.identify(conn, remotePeerInfo)
check id.pubKey.get() == remoteSeckey.getKey()
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
2019-08-30 05:16:30 +00:00
result = true
check:
waitFor(testHandle()) == true
2019-09-23 23:02:13 +00:00
test "handle failed identify":
proc testHandleError() {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let remoteSeckey = PrivateKey.random(RSA)
var remotePeerInfo: PeerInfo
2019-09-12 02:10:38 +00:00
remotePeerInfo.peerId = some(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)
2019-09-12 02:10:38 +00:00
asyncCheck transport1.listen(ma, connHandler)
let msDial = newMultistream()
let transport2: TcpTransport = newTransport(TcpTransport)
let conn = await transport2.dial(transport1.ma)
let seckey = PrivateKey.random(RSA)
var localPeerInfo: PeerInfo
2019-09-12 02:10:38 +00:00
localPeerInfo.peerId = some(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
2019-09-12 02:10:38 +00:00
wrongRemotePeer.peerId = some(PeerID.init(wrongSec))
2019-09-12 02:10:38 +00:00
let id = await identifyProto2.identify(conn, wrongRemotePeer)
await conn.close()
expect IdentityNoMatchError:
waitFor(testHandleError())