2023-05-18 08:24:17 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
# Nim-Libp2p
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
import options
|
2023-05-18 08:24:17 +00:00
|
|
|
import chronos
|
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
|
|
|
|
|
|
|
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
|
2021-11-24 20:01:12 +00:00
|
|
|
ma {.threadvar.}: seq[MultiAddress]
|
2020-11-25 13:42:02 +00:00
|
|
|
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:
|
2021-12-16 10:05:20 +00:00
|
|
|
ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
2020-11-25 13:42:02 +00:00
|
|
|
remoteSecKey = PrivateKey.random(ECDSA, rng[]).get()
|
2021-10-25 08:26:32 +00:00
|
|
|
remotePeerInfo = PeerInfo.new(
|
2021-11-24 20:01:12 +00:00
|
|
|
remoteSecKey,
|
|
|
|
ma,
|
|
|
|
["/test/proto1/1.0.0", "/test/proto2/1.0.0"])
|
2020-11-25 13:42:02 +00:00
|
|
|
|
2021-06-30 08:59:30 +00:00
|
|
|
transport1 = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
transport2 = TcpTransport.new(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
|
|
|
|
2022-10-20 10:22:28 +00:00
|
|
|
serverFut = transport1.start(ma)
|
|
|
|
await remotePeerInfo.update()
|
|
|
|
|
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)
|
2023-12-05 07:05:32 +00:00
|
|
|
proc acceptHandler(): Future[void] {.async.} =
|
2020-11-25 13:42:02 +00:00
|
|
|
let c = await transport1.accept()
|
|
|
|
await msListen.handle(c)
|
|
|
|
|
|
|
|
acceptFut = acceptHandler()
|
2021-11-24 20:01:12 +00:00
|
|
|
conn = await transport2.dial(transport1.addrs[0])
|
2020-11-25 13:42:02 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
2021-09-08 09:07:46 +00:00
|
|
|
let id = await identifyProto2.identify(conn, remotePeerInfo.peerId)
|
2020-11-25 13:42:02 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
check id.pubkey.get() == remoteSecKey.getPublicKey().get()
|
2021-11-24 20:01:12 +00:00
|
|
|
check id.addrs == ma
|
2020-11-25 13:42:02 +00:00
|
|
|
check id.protoVersion.get() == ProtoVersion
|
|
|
|
check id.agentVersion.get() == AgentVersion
|
|
|
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
2022-03-14 08:39:30 +00:00
|
|
|
check id.signedPeerRecord.isNone()
|
2020-11-25 13:42:02 +00:00
|
|
|
|
|
|
|
asyncTest "custom agent version":
|
|
|
|
const customAgentVersion = "MY CUSTOM AGENT STRING"
|
|
|
|
remotePeerInfo.agentVersion = customAgentVersion
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
|
2023-12-05 07:05:32 +00:00
|
|
|
proc acceptHandler(): Future[void] {.async.} =
|
2020-11-25 13:42:02 +00:00
|
|
|
let c = await transport1.accept()
|
|
|
|
await msListen.handle(c)
|
|
|
|
|
|
|
|
acceptFut = acceptHandler()
|
2021-11-24 20:01:12 +00:00
|
|
|
conn = await transport2.dial(transport1.addrs[0])
|
2020-11-25 13:42:02 +00:00
|
|
|
|
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
2021-09-08 09:07:46 +00:00
|
|
|
let id = await identifyProto2.identify(conn, remotePeerInfo.peerId)
|
2020-11-25 13:42:02 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
check id.pubkey.get() == remoteSecKey.getPublicKey().get()
|
2021-11-24 20:01:12 +00:00
|
|
|
check id.addrs == ma
|
2020-11-25 13:42:02 +00:00
|
|
|
check id.protoVersion.get() == ProtoVersion
|
|
|
|
check id.agentVersion.get() == customAgentVersion
|
|
|
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
2022-03-14 08:39:30 +00:00
|
|
|
check id.signedPeerRecord.isNone()
|
2020-11-25 13:42:02 +00:00
|
|
|
|
|
|
|
asyncTest "handle failed identify":
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
|
|
|
|
proc acceptHandler() {.async.} =
|
|
|
|
var conn: Connection
|
|
|
|
try:
|
|
|
|
conn = await transport1.accept()
|
|
|
|
await msListen.handle(conn)
|
|
|
|
except CatchableError:
|
|
|
|
discard
|
|
|
|
finally:
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
acceptFut = acceptHandler()
|
2021-11-24 20:01:12 +00:00
|
|
|
conn = await transport2.dial(transport1.addrs[0])
|
2020-11-25 13:42:02 +00:00
|
|
|
|
|
|
|
expect IdentityNoMatchError:
|
2021-10-25 08:26:32 +00:00
|
|
|
let pi2 = PeerInfo.new(PrivateKey.random(ECDSA, rng[]).get())
|
2020-11-25 13:42:02 +00:00
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
2021-09-08 09:07:46 +00:00
|
|
|
discard await identifyProto2.identify(conn, pi2.peerId)
|
2021-06-14 17:08:47 +00:00
|
|
|
|
2022-03-14 08:39:30 +00:00
|
|
|
asyncTest "can send signed peer record":
|
|
|
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
|
|
identifyProto1.sendSignedPeerRecord = true
|
2023-12-05 07:05:32 +00:00
|
|
|
proc acceptHandler(): Future[void] {.async.} =
|
2022-03-14 08:39:30 +00:00
|
|
|
let c = await transport1.accept()
|
|
|
|
await msListen.handle(c)
|
|
|
|
|
|
|
|
acceptFut = acceptHandler()
|
|
|
|
conn = await transport2.dial(transport1.addrs[0])
|
|
|
|
|
|
|
|
discard await msDial.select(conn, IdentifyCodec)
|
|
|
|
let id = await identifyProto2.identify(conn, remotePeerInfo.peerId)
|
|
|
|
|
|
|
|
check id.pubkey.get() == remoteSecKey.getPublicKey().get()
|
|
|
|
check id.addrs == ma
|
|
|
|
check id.protoVersion.get() == ProtoVersion
|
|
|
|
check id.agentVersion.get() == AgentVersion
|
|
|
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
2022-04-13 07:35:28 +00:00
|
|
|
check id.signedPeerRecord.get() == remotePeerInfo.signedPeerRecord.envelope
|
2022-03-14 08:39:30 +00:00
|
|
|
|
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
|
|
|
|
conn {.threadvar.}: Connection
|
|
|
|
asyncSetup:
|
2022-11-21 09:55:25 +00:00
|
|
|
switch1 = newStandardSwitch(sendSignedPeerRecord=true)
|
|
|
|
switch2 = newStandardSwitch(sendSignedPeerRecord=true)
|
2021-06-14 17:08:47 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
proc updateStore1(peerId: PeerId, info: IdentifyInfo) {.async.} =
|
|
|
|
switch1.peerStore.updatePeerInfo(info)
|
|
|
|
proc updateStore2(peerId: PeerId, info: IdentifyInfo) {.async.} =
|
|
|
|
switch2.peerStore.updatePeerInfo(info)
|
|
|
|
|
|
|
|
identifyPush1 = IdentifyPush.new(updateStore1)
|
|
|
|
identifyPush2 = IdentifyPush.new(updateStore2)
|
2021-06-14 17:08:47 +00:00
|
|
|
|
|
|
|
switch1.mount(identifyPush1)
|
|
|
|
switch2.mount(identifyPush2)
|
|
|
|
|
2021-12-03 18:23:12 +00:00
|
|
|
await switch1.start()
|
|
|
|
await switch2.start()
|
2021-06-14 17:08:47 +00:00
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
conn = await switch2.dial(
|
|
|
|
switch1.peerInfo.peerId,
|
|
|
|
switch1.peerInfo.addrs,
|
|
|
|
IdentifyPushCodec)
|
2021-06-14 17:08:47 +00:00
|
|
|
|
|
|
|
check:
|
2022-05-25 10:12:57 +00:00
|
|
|
switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs
|
|
|
|
switch2.peerStore[AddressBook][switch1.peerInfo.peerId] == switch1.peerInfo.addrs
|
2023-03-08 11:30:19 +00:00
|
|
|
|
2022-11-21 09:55:25 +00:00
|
|
|
switch1.peerStore[KeyBook][switch2.peerInfo.peerId] == switch2.peerInfo.publicKey
|
|
|
|
switch2.peerStore[KeyBook][switch1.peerInfo.peerId] == switch1.peerInfo.publicKey
|
2021-06-14 17:08:47 +00:00
|
|
|
|
2022-11-21 09:55:25 +00:00
|
|
|
switch1.peerStore[AgentBook][switch2.peerInfo.peerId] == switch2.peerInfo.agentVersion
|
|
|
|
switch2.peerStore[AgentBook][switch1.peerInfo.peerId] == switch1.peerInfo.agentVersion
|
|
|
|
|
|
|
|
switch1.peerStore[ProtoVersionBook][switch2.peerInfo.peerId] == switch2.peerInfo.protoVersion
|
|
|
|
switch2.peerStore[ProtoVersionBook][switch1.peerInfo.peerId] == switch1.peerInfo.protoVersion
|
|
|
|
|
|
|
|
switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] == switch2.peerInfo.protocols
|
|
|
|
switch2.peerStore[ProtoBook][switch1.peerInfo.peerId] == switch1.peerInfo.protocols
|
2021-06-14 17:08:47 +00:00
|
|
|
|
2022-11-21 09:55:25 +00:00
|
|
|
switch1.peerStore[SPRBook][switch2.peerInfo.peerId] == switch2.peerInfo.signedPeerRecord.envelope
|
|
|
|
switch2.peerStore[SPRBook][switch1.peerInfo.peerId] == switch1.peerInfo.signedPeerRecord.envelope
|
2022-03-14 08:39:30 +00:00
|
|
|
|
2021-06-14 17:08:47 +00:00
|
|
|
proc closeAll() {.async.} =
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
await switch1.stop()
|
|
|
|
await switch2.stop()
|
|
|
|
|
|
|
|
asyncTest "simple push identify":
|
|
|
|
switch2.peerInfo.protocols.add("/newprotocol/")
|
2024-06-06 11:05:45 +00:00
|
|
|
switch2.peerInfo.addrs.add(MultiAddress.init("/ip4/127.0.0.1/tcp/5555").tryGet())
|
2021-06-14 17:08:47 +00:00
|
|
|
|
|
|
|
check:
|
2022-05-25 10:12:57 +00:00
|
|
|
switch1.peerStore[AddressBook][switch2.peerInfo.peerId] != switch2.peerInfo.addrs
|
|
|
|
switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] != switch2.peerInfo.protocols
|
2021-06-14 17:08:47 +00:00
|
|
|
|
|
|
|
await identifyPush2.push(switch2.peerInfo, conn)
|
|
|
|
|
2024-02-09 10:51:27 +00:00
|
|
|
checkUntilTimeout: switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] == switch2.peerInfo.protocols
|
|
|
|
checkUntilTimeout: switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs
|
2021-12-13 10:25:33 +00:00
|
|
|
|
2021-06-14 17:08:47 +00:00
|
|
|
await closeAll()
|
|
|
|
|
|
|
|
asyncTest "wrong peer id push identify":
|
|
|
|
switch2.peerInfo.protocols.add("/newprotocol/")
|
2024-06-06 11:05:45 +00:00
|
|
|
switch2.peerInfo.addrs.add(MultiAddress.init("/ip4/127.0.0.1/tcp/5555").tryGet())
|
2021-06-14 17:08:47 +00:00
|
|
|
|
|
|
|
check:
|
2022-05-25 10:12:57 +00:00
|
|
|
switch1.peerStore[AddressBook][switch2.peerInfo.peerId] != switch2.peerInfo.addrs
|
|
|
|
switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] != switch2.peerInfo.protocols
|
2021-06-14 17:08:47 +00:00
|
|
|
|
|
|
|
let oldPeerId = switch2.peerInfo.peerId
|
2021-10-25 08:26:32 +00:00
|
|
|
switch2.peerInfo = PeerInfo.new(PrivateKey.random(newRng()[]).get())
|
2021-06-14 17:08:47 +00:00
|
|
|
|
|
|
|
await identifyPush2.push(switch2.peerInfo, conn)
|
|
|
|
|
2021-12-13 10:25:33 +00:00
|
|
|
# We have no way to know when the message will is received
|
|
|
|
# because it will fail validation inside push identify itself
|
|
|
|
#
|
|
|
|
# So no choice but to sleep
|
|
|
|
await sleepAsync(10.milliseconds)
|
|
|
|
|
2021-06-14 17:08:47 +00:00
|
|
|
await closeAll()
|
|
|
|
|
|
|
|
# Wait the very end to be sure that the push has been processed
|
|
|
|
check:
|
2022-05-25 10:12:57 +00:00
|
|
|
switch1.peerStore[ProtoBook][oldPeerId] != switch2.peerInfo.protocols
|
|
|
|
switch1.peerStore[AddressBook][oldPeerId] != switch2.peerInfo.addrs
|