2021-05-21 16:27:01 +00:00
|
|
|
import options, bearssl
|
2021-06-14 17:08:47 +00:00
|
|
|
import chronos, strutils, sequtils, sets, algorithm
|
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,
|
2021-06-14 17:08:47 +00:00
|
|
|
switch,
|
|
|
|
builders,
|
2019-10-29 18:51:48 +00:00
|
|
|
transports/tcptransport,
|
2021-03-18 15:20:36 +00:00
|
|
|
crypto/crypto,
|
|
|
|
upgrademngrs/upgrade]
|
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-25 13:42:02 +00:00
|
|
|
suite "handle identify message":
|
|
|
|
var
|
|
|
|
ma {.threadvar.}: MultiAddress
|
|
|
|
remoteSecKey {.threadvar.}: PrivateKey
|
|
|
|
remotePeerInfo {.threadvar.}: PeerInfo
|
|
|
|
serverFut {.threadvar.}: Future[void]
|
|
|
|
acceptFut {.threadvar.}: Future[void]
|
|
|
|
identifyProto1 {.threadvar.}: Identify
|
|
|
|
identifyProto2 {.threadvar.}: Identify
|
|
|
|
transport1 {.threadvar.}: Transport
|
|
|
|
transport2 {.threadvar.}: Transport
|
|
|
|
msListen {.threadvar.}: MultistreamSelect
|
|
|
|
msDial {.threadvar.}: MultistreamSelect
|
|
|
|
conn {.threadvar.}: Connection
|
|
|
|
|
|
|
|
asyncSetup:
|
|
|
|
ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
remoteSecKey = PrivateKey.random(ECDSA, rng[]).get()
|
|
|
|
remotePeerInfo = PeerInfo.init(
|
|
|
|
remoteSecKey, [ma], ["/test/proto1/1.0.0", "/test/proto2/1.0.0"])
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
transport1 = TcpTransport.init(upgrade = Upgrade())
|
|
|
|
transport2 = TcpTransport.init(upgrade = Upgrade())
|
2020-11-25 13:42:02 +00:00
|
|
|
|
2021-06-07 07:32:08 +00:00
|
|
|
identifyProto1 = Identify.new(remotePeerInfo)
|
|
|
|
identifyProto2 = Identify.new(remotePeerInfo)
|
2020-11-25 13:42:02 +00:00
|
|
|
|
2021-06-07 07:32:08 +00:00
|
|
|
msListen = MultistreamSelect.new()
|
|
|
|
msDial = MultistreamSelect.new()
|
2020-11-25 13:42:02 +00:00
|
|
|
|
|
|
|
asyncTeardown:
|
|
|
|
await conn.close()
|
|
|
|
await acceptFut
|
|
|
|
await transport1.stop()
|
|
|
|
await serverFut
|
|
|
|
await transport2.stop()
|
|
|
|
|
|
|
|
asyncTest "default agent version":
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
serverFut = transport1.start(ma)
|
|
|
|
proc acceptHandler(): Future[void] {.async, gcsafe.} =
|
|
|
|
let c = await transport1.accept()
|
|
|
|
await msListen.handle(c)
|
|
|
|
|
|
|
|
acceptFut = acceptHandler()
|
|
|
|
conn = await transport2.dial(transport1.ma)
|
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
2020-11-25 13:42:02 +00:00
|
|
|
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"]
|
|
|
|
|
|
|
|
asyncTest "custom agent version":
|
|
|
|
const customAgentVersion = "MY CUSTOM AGENT STRING"
|
|
|
|
remotePeerInfo.agentVersion = customAgentVersion
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
|
|
|
|
serverFut = transport1.start(ma)
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2020-11-25 13:42:02 +00:00
|
|
|
proc acceptHandler(): Future[void] {.async, gcsafe.} =
|
|
|
|
let c = await transport1.accept()
|
|
|
|
await msListen.handle(c)
|
|
|
|
|
|
|
|
acceptFut = acceptHandler()
|
|
|
|
conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
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() == customAgentVersion
|
|
|
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
|
|
|
|
|
|
|
asyncTest "handle failed identify":
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
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()
|
|
|
|
|
|
|
|
acceptFut = acceptHandler()
|
|
|
|
conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
expect IdentityNoMatchError:
|
|
|
|
let pi2 = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get())
|
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
|
|
|
discard await identifyProto2.identify(conn, pi2)
|
2021-06-14 17:08:47 +00:00
|
|
|
|
|
|
|
suite "handle push identify message":
|
|
|
|
var
|
|
|
|
switch1 {.threadvar.}: Switch
|
|
|
|
switch2 {.threadvar.}: Switch
|
|
|
|
identifyPush1 {.threadvar.}: IdentifyPush
|
|
|
|
identifyPush2 {.threadvar.}: IdentifyPush
|
|
|
|
awaiters {.threadvar.}: seq[Future[void]]
|
|
|
|
conn {.threadvar.}: Connection
|
|
|
|
asyncSetup:
|
|
|
|
switch1 = newStandardSwitch()
|
|
|
|
switch2 = newStandardSwitch()
|
|
|
|
|
|
|
|
identifyPush1 = IdentifyPush.new(switch1.connManager)
|
|
|
|
identifyPush2 = IdentifyPush.new(switch2.connManager)
|
|
|
|
|
|
|
|
switch1.mount(identifyPush1)
|
|
|
|
switch2.mount(identifyPush2)
|
|
|
|
|
|
|
|
awaiters.add(await switch1.start())
|
|
|
|
awaiters.add(await switch2.start())
|
|
|
|
|
|
|
|
conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, IdentifyPushCodec)
|
|
|
|
|
|
|
|
let storedInfo1 = switch1.peerStore.get(switch2.peerInfo.peerId)
|
|
|
|
let storedInfo2 = switch2.peerStore.get(switch1.peerInfo.peerId)
|
|
|
|
|
|
|
|
check:
|
|
|
|
storedInfo1.peerId == switch2.peerInfo.peerId
|
|
|
|
storedInfo2.peerId == switch1.peerInfo.peerId
|
|
|
|
|
|
|
|
storedInfo1.addrs.toSeq() == switch2.peerInfo.addrs
|
|
|
|
storedInfo2.addrs.toSeq() == switch1.peerInfo.addrs
|
|
|
|
|
|
|
|
storedInfo1.protos.toSeq() == switch2.peerInfo.protocols
|
|
|
|
storedInfo2.protos.toSeq() == switch1.peerInfo.protocols
|
|
|
|
|
|
|
|
proc closeAll() {.async.} =
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
await switch1.stop()
|
|
|
|
await switch2.stop()
|
|
|
|
|
|
|
|
# this needs to go at end
|
|
|
|
await allFuturesThrowing(awaiters)
|
|
|
|
|
|
|
|
asyncTest "simple push identify":
|
|
|
|
switch2.peerInfo.protocols.add("/newprotocol/")
|
|
|
|
switch2.peerInfo.addrs.add(MultiAddress.init("/ip4/127.0.0.1/tcp/5555").tryGet())
|
|
|
|
|
|
|
|
check:
|
|
|
|
switch1.peerStore.get(switch2.peerInfo.peerId).addrs.toSeq() != switch2.peerInfo.addrs
|
|
|
|
switch1.peerStore.get(switch2.peerInfo.peerId).protos != switch2.peerInfo.protocols.toSet()
|
|
|
|
|
|
|
|
await identifyPush2.push(switch2.peerInfo, conn)
|
|
|
|
|
|
|
|
await closeAll()
|
|
|
|
|
|
|
|
# Wait the very end to be sure that the push has been processed
|
|
|
|
var aprotos = switch1.peerStore.get(switch2.peerInfo.peerId).protos.toSeq()
|
|
|
|
var bprotos = switch2.peerInfo.protocols
|
|
|
|
aprotos.sort()
|
|
|
|
bprotos.sort()
|
|
|
|
check:
|
|
|
|
aprotos == bprotos
|
|
|
|
switch1.peerStore.get(switch2.peerInfo.peerId).addrs == switch2.peerInfo.addrs.toSet()
|
|
|
|
|
|
|
|
|
|
|
|
asyncTest "wrong peer id push identify":
|
|
|
|
switch2.peerInfo.protocols.add("/newprotocol/")
|
|
|
|
switch2.peerInfo.addrs.add(MultiAddress.init("/ip4/127.0.0.1/tcp/5555").tryGet())
|
|
|
|
|
|
|
|
check:
|
|
|
|
switch1.peerStore.get(switch2.peerInfo.peerId).addrs != switch2.peerInfo.addrs.toSet()
|
|
|
|
switch1.peerStore.get(switch2.peerInfo.peerId).protos.toSeq() != switch2.peerInfo.protocols
|
|
|
|
|
|
|
|
let oldPeerId = switch2.peerInfo.peerId
|
|
|
|
switch2.peerInfo = PeerInfo.init(PrivateKey.random(newRng()[]).get())
|
|
|
|
|
|
|
|
await identifyPush2.push(switch2.peerInfo, conn)
|
|
|
|
|
|
|
|
await closeAll()
|
|
|
|
|
|
|
|
# Wait the very end to be sure that the push has been processed
|
|
|
|
var aprotos = switch1.peerStore.get(oldPeerId).protos.toSeq()
|
|
|
|
var bprotos = switch2.peerInfo.protocols
|
|
|
|
aprotos.sort()
|
|
|
|
bprotos.sort()
|
|
|
|
check:
|
|
|
|
aprotos != bprotos
|
|
|
|
switch1.peerStore.get(oldPeerId).addrs.toSeq() != switch2.peerInfo.addrs
|