nim-libp2p/tests/testping.nim
Dmitriy Ryajov 73168b6eae
Add support for multiple addresses to transports (#598)
* add test for multiple local addresses

* allow transports to listen on multiple addrs

* fix tcp transport accept

* check switch addrs are correct

* switch test to port 0

* close accepted peers on close

* ignore CancelledError in transport accept

* test ci

* only accept in accept loop

* avoid accept greedyness

* close acceptedPeers

* accept doesn't crash on cancelled fut

* add common transport test

* close conn on handling failure

* close accepted peers in two steps

* test for macos

* revert accept greedyness

* fix dialing cancel

* test chronos fix

* add ws

* ws cancellation

* small fix

* remove chronos blocked test

* fix testping

* Fix transport's switch start (like #609)

* bump chronos

* Websocket: handle both ws & wss

Co-authored-by: Tanguy Cizain <tanguycizain@gmail.com>
Co-authored-by: Tanguy <tanguy@status.im>
2021-11-24 14:01:12 -06:00

116 lines
3.4 KiB
Nim

import options, bearssl
import chronos, strutils
import ../libp2p/[protocols/identify,
protocols/ping,
multiaddress,
peerinfo,
wire,
peerid,
stream/connection,
multistream,
transports/transport,
transports/tcptransport,
crypto/crypto,
upgrademngrs/upgrade]
import ./helpers
when defined(nimHasUsed): {.used.}
suite "Ping":
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:
ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
transport1 = TcpTransport.new(upgrade = Upgrade())
transport2 = TcpTransport.new(upgrade = Upgrade())
proc handlePing(peer: PeerId) {.async, gcsafe, closure.} =
inc pingReceivedCount
pingProto1 = Ping.new()
pingProto2 = Ping.new(handlePing)
msListen = MultistreamSelect.new()
msDial = MultistreamSelect.new()
pingReceivedCount = 0
asyncTeardown:
await conn.close()
await acceptFut
await transport1.stop()
await serverFut
await transport2.stop()
checkTrackers()
asyncTest "simple ping":
msListen.addHandler(PingCodec, pingProto1)
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.addrs[0])
discard await msDial.select(conn, PingCodec)
let time = await pingProto2.ping(conn)
check not time.isZero()
asyncTest "ping callback":
msDial.addHandler(PingCodec, pingProto2)
serverFut = transport1.start(@[ma])
proc acceptHandler(): Future[void] {.async, gcsafe.} =
let c = await transport1.accept()
discard await msListen.select(c, PingCodec)
discard await pingProto1.ping(c)
acceptFut = acceptHandler()
conn = await transport2.dial(transport1.addrs[0])
await msDial.handle(conn)
check pingReceivedCount == 1
asyncTest "bad ping data ack":
type FakePing = ref object of LPProtocol
let fakePingProto = FakePing()
proc fakeHandle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
var
buf: array[32, byte]
fakebuf: array[32, byte]
await conn.readExactly(addr buf[0], 32)
await conn.write(@fakebuf)
fakePingProto.codec = PingCodec
fakePingProto.handler = fakeHandle
msListen.addHandler(PingCodec, fakePingProto)
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.addrs[0])
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