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.
|
|
|
|
|
|
|
|
import chronos
|
|
|
|
import
|
|
|
|
../libp2p/[
|
|
|
|
protocols/ping,
|
2021-06-08 16:53:45 +00:00
|
|
|
multiaddress,
|
|
|
|
peerinfo,
|
|
|
|
peerid,
|
|
|
|
stream/connection,
|
|
|
|
multistream,
|
|
|
|
transports/transport,
|
|
|
|
transports/tcptransport,
|
|
|
|
crypto/crypto,
|
|
|
|
upgrademngrs/upgrade,
|
|
|
|
]
|
|
|
|
import ./helpers
|
|
|
|
|
|
|
|
suite "Ping":
|
2021-09-01 06:38:24 +00:00
|
|
|
var
|
|
|
|
ma {.threadvar.}: MultiAddress
|
|
|
|
serverFut {.threadvar.}: Future[void]
|
|
|
|
acceptFut {.threadvar.}: Future[void]
|
|
|
|
pingProto1 {.threadvar.}: Ping
|
|
|
|
pingProto2 {.threadvar.}: Ping
|
|
|
|
transport1 {.threadvar.}: Transport
|
|
|
|
transport2 {.threadvar.}: Transport
|
|
|
|
msListen {.threadvar.}: MultistreamSelect
|
|
|
|
msDial {.threadvar.}: MultistreamSelect
|
|
|
|
conn {.threadvar.}: Connection
|
|
|
|
pingReceivedCount {.threadvar.}: int
|
|
|
|
|
|
|
|
asyncSetup:
|
2021-12-16 10:05:20 +00:00
|
|
|
ma = MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2021-09-01 06:38:24 +00:00
|
|
|
|
|
|
|
transport1 = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
transport2 = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
|
2023-12-05 07:05:32 +00:00
|
|
|
proc handlePing(peer: PeerId) {.async, closure.} =
|
2021-09-01 06:38:24 +00:00
|
|
|
inc pingReceivedCount
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2021-09-01 06:38:24 +00:00
|
|
|
pingProto1 = Ping.new()
|
|
|
|
pingProto2 = Ping.new(handlePing)
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
msListen = MultistreamSelect.new()
|
|
|
|
msDial = MultistreamSelect.new()
|
2021-09-01 06:38:24 +00:00
|
|
|
|
|
|
|
pingReceivedCount = 0
|
|
|
|
|
|
|
|
asyncTeardown:
|
|
|
|
await conn.close()
|
|
|
|
await acceptFut
|
|
|
|
await transport1.stop()
|
|
|
|
await serverFut
|
|
|
|
await transport2.stop()
|
2021-06-08 16:53:45 +00:00
|
|
|
checkTrackers()
|
|
|
|
|
2021-09-01 06:38:24 +00:00
|
|
|
asyncTest "simple ping":
|
|
|
|
msListen.addHandler(PingCodec, pingProto1)
|
2021-11-24 20:01:12 +00:00
|
|
|
serverFut = transport1.start(@[ma])
|
2023-12-05 07:05:32 +00:00
|
|
|
proc acceptHandler(): Future[void] {.async.} =
|
2021-09-01 06:38:24 +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])
|
2021-09-01 06:38:24 +00:00
|
|
|
|
|
|
|
discard await msDial.select(conn, PingCodec)
|
|
|
|
let time = await pingProto2.ping(conn)
|
|
|
|
|
|
|
|
check not time.isZero()
|
|
|
|
|
|
|
|
asyncTest "ping callback":
|
|
|
|
msDial.addHandler(PingCodec, pingProto2)
|
2021-11-24 20:01:12 +00:00
|
|
|
serverFut = transport1.start(@[ma])
|
2023-12-05 07:05:32 +00:00
|
|
|
proc acceptHandler(): Future[void] {.async.} =
|
2021-09-01 06:38:24 +00:00
|
|
|
let c = await transport1.accept()
|
|
|
|
discard await msListen.select(c, PingCodec)
|
|
|
|
discard await pingProto1.ping(c)
|
|
|
|
|
|
|
|
acceptFut = acceptHandler()
|
2021-11-24 20:01:12 +00:00
|
|
|
conn = await transport2.dial(transport1.addrs[0])
|
2021-09-01 06:38:24 +00:00
|
|
|
|
|
|
|
await msDial.handle(conn)
|
|
|
|
check pingReceivedCount == 1
|
|
|
|
|
|
|
|
asyncTest "bad ping data ack":
|
|
|
|
type FakePing = ref object of LPProtocol
|
|
|
|
let fakePingProto = FakePing()
|
2023-12-05 07:05:32 +00:00
|
|
|
proc fakeHandle(conn: Connection, proto: string) {.async, closure.} =
|
2021-09-01 06:38:24 +00:00
|
|
|
var
|
|
|
|
buf: array[32, byte]
|
|
|
|
fakebuf: array[32, byte]
|
|
|
|
await conn.readExactly(addr buf[0], 32)
|
2021-10-25 08:26:32 +00:00
|
|
|
await conn.write(@fakebuf)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2021-09-01 06:38:24 +00:00
|
|
|
fakePingProto.codec = PingCodec
|
|
|
|
fakePingProto.handler = fakeHandle
|
|
|
|
|
|
|
|
msListen.addHandler(PingCodec, fakePingProto)
|
2021-11-24 20:01:12 +00:00
|
|
|
serverFut = transport1.start(@[ma])
|
2023-12-05 07:05:32 +00:00
|
|
|
proc acceptHandler(): Future[void] {.async.} =
|
2021-09-01 06:38:24 +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])
|
2021-09-01 06:38:24 +00:00
|
|
|
|
|
|
|
discard await msDial.select(conn, PingCodec)
|
|
|
|
let p = pingProto2.ping(conn)
|
|
|
|
var raised = false
|
|
|
|
try:
|
|
|
|
discard await p
|
|
|
|
check false #should have raised
|
|
|
|
except WrongPingAckError:
|
|
|
|
raised = true
|
|
|
|
check raised
|