2021-06-30 08:59:30 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import sequtils
|
|
|
|
import chronos, stew/byteutils
|
|
|
|
import ../libp2p/[stream/connection,
|
|
|
|
transports/transport,
|
|
|
|
upgrademngrs/upgrade,
|
|
|
|
multiaddress,
|
|
|
|
errors,
|
|
|
|
wire]
|
|
|
|
|
|
|
|
import ./helpers
|
|
|
|
|
2022-01-05 15:27:33 +00:00
|
|
|
type TransportProvider* = proc(): Transport {.gcsafe, raises: [Defect].}
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
proc commonTransportTest*(name: string, prov: TransportProvider, ma: string) =
|
|
|
|
suite name & " common tests":
|
2021-06-30 08:59:30 +00:00
|
|
|
teardown:
|
|
|
|
checkTrackers()
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
asyncTest "can handle local address":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init(ma).tryGet()]
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport1 = prov()
|
|
|
|
await transport1.start(ma)
|
2021-11-24 20:01:12 +00:00
|
|
|
check transport1.handles(transport1.addrs[0])
|
2021-08-03 13:48:03 +00:00
|
|
|
await transport1.stop()
|
|
|
|
|
2021-10-14 11:16:34 +00:00
|
|
|
asyncTest "e2e: handle observedAddr":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init(ma).tryGet()]
|
2021-10-14 11:16:34 +00:00
|
|
|
|
|
|
|
let transport1 = prov()
|
|
|
|
await transport1.start(ma)
|
|
|
|
|
|
|
|
let transport2 = prov()
|
|
|
|
|
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
|
|
|
let conn = await transport1.accept()
|
|
|
|
check transport1.handles(conn.observedAddr)
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
let conn = await transport2.dial(transport1.addrs[0])
|
2021-10-14 11:16:34 +00:00
|
|
|
|
|
|
|
check transport2.handles(conn.observedAddr)
|
|
|
|
|
|
|
|
await conn.close() #for some protocols, closing requires actively reading, so we must close here
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(
|
|
|
|
transport1.stop(),
|
|
|
|
transport2.stop()))
|
|
|
|
|
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
|
|
|
|
2021-06-30 08:59:30 +00:00
|
|
|
asyncTest "e2e: handle write":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init(ma).tryGet()]
|
2021-06-30 08:59:30 +00:00
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport1 = prov()
|
2021-06-30 08:59:30 +00:00
|
|
|
await transport1.start(ma)
|
|
|
|
|
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
|
|
|
let conn = await transport1.accept()
|
|
|
|
await conn.write("Hello!")
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport2 = prov()
|
2021-11-24 20:01:12 +00:00
|
|
|
let conn = await transport2.dial(transport1.addrs[0])
|
2021-06-30 08:59:30 +00:00
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
await conn.close() #for some protocols, closing requires actively reading, so we must close here
|
2021-06-30 08:59:30 +00:00
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(
|
|
|
|
transport1.stop(),
|
|
|
|
transport2.stop()))
|
2021-06-30 08:59:30 +00:00
|
|
|
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
2021-08-03 13:48:03 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2021-06-30 08:59:30 +00:00
|
|
|
|
|
|
|
asyncTest "e2e: handle read":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init(ma).tryGet()]
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport1 = prov()
|
|
|
|
await transport1.start(ma)
|
2021-06-30 08:59:30 +00:00
|
|
|
|
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
|
|
|
let conn = await transport1.accept()
|
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport2 = prov()
|
2021-11-24 20:01:12 +00:00
|
|
|
let conn = await transport2.dial(transport1.addrs[0])
|
2021-06-30 08:59:30 +00:00
|
|
|
await conn.write("Hello!")
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
await conn.close() #for some protocols, closing requires actively reading, so we must close here
|
2021-06-30 08:59:30 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(
|
|
|
|
transport1.stop(),
|
|
|
|
transport2.stop()))
|
2021-06-30 08:59:30 +00:00
|
|
|
|
|
|
|
asyncTest "e2e: handle dial cancellation":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init(ma).tryGet()]
|
2021-06-30 08:59:30 +00:00
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport1 = prov()
|
2021-06-30 08:59:30 +00:00
|
|
|
await transport1.start(ma)
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport2 = prov()
|
2021-11-24 20:01:12 +00:00
|
|
|
let cancellation = transport2.dial(transport1.addrs[0])
|
2021-06-30 08:59:30 +00:00
|
|
|
|
|
|
|
await cancellation.cancelAndWait()
|
|
|
|
check cancellation.cancelled
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(
|
|
|
|
transport1.stop(),
|
|
|
|
transport2.stop()))
|
2021-06-30 08:59:30 +00:00
|
|
|
|
|
|
|
asyncTest "e2e: handle accept cancellation":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init(ma).tryGet()]
|
2021-06-30 08:59:30 +00:00
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport1 = prov()
|
2021-06-30 08:59:30 +00:00
|
|
|
await transport1.start(ma)
|
|
|
|
|
|
|
|
let acceptHandler = transport1.accept()
|
|
|
|
await acceptHandler.cancelAndWait()
|
|
|
|
check acceptHandler.cancelled
|
|
|
|
|
|
|
|
await transport1.stop()
|
2021-08-03 13:48:03 +00:00
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
asyncTest "e2e should allow multiple local addresses":
|
2022-01-10 11:29:52 +00:00
|
|
|
when defined(windows):
|
|
|
|
# this randomly locks the Windows CI job
|
|
|
|
skip()
|
|
|
|
return
|
2021-11-24 20:01:12 +00:00
|
|
|
let addrs = @[MultiAddress.init(ma).tryGet(),
|
|
|
|
MultiAddress.init(ma).tryGet()]
|
|
|
|
|
|
|
|
|
|
|
|
let transport1 = prov()
|
|
|
|
await transport1.start(addrs)
|
|
|
|
|
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
|
|
|
while true:
|
|
|
|
let conn = await transport1.accept()
|
|
|
|
await conn.write("Hello!")
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
|
|
|
|
|
|
|
check transport1.addrs.len == 2
|
|
|
|
check transport1.addrs[0] != transport1.addrs[1]
|
|
|
|
|
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
|
|
|
|
proc client(ma: MultiAddress) {.async.} =
|
|
|
|
let conn1 = await transport1.dial(ma)
|
|
|
|
await conn1.readExactly(addr msg[0], 6)
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
await conn1.close()
|
|
|
|
|
|
|
|
#Dial the same server multiple time in a row
|
|
|
|
await client(transport1.addrs[0])
|
|
|
|
await client(transport1.addrs[0])
|
|
|
|
await client(transport1.addrs[0])
|
|
|
|
|
|
|
|
#Dial the same server on different addresses
|
|
|
|
await client(transport1.addrs[1])
|
|
|
|
await client(transport1.addrs[0])
|
|
|
|
await client(transport1.addrs[1])
|
|
|
|
|
|
|
|
#Cancel a dial
|
|
|
|
#TODO add back once chronos fixes cancellation
|
|
|
|
#let
|
|
|
|
# dial1 = transport1.dial(transport1.addrs[1])
|
|
|
|
# dial2 = transport1.dial(transport1.addrs[0])
|
|
|
|
#await dial1.cancelAndWait()
|
|
|
|
#await dial2.cancelAndWait()
|
|
|
|
|
|
|
|
await handlerWait.cancelAndWait()
|
|
|
|
|
|
|
|
await transport1.stop()
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
asyncTest "e2e: stopping transport kills connections":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init(ma).tryGet()]
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
let transport1 = prov()
|
|
|
|
await transport1.start(ma)
|
|
|
|
|
|
|
|
let transport2 = prov()
|
|
|
|
|
|
|
|
let acceptHandler = transport1.accept()
|
2021-11-24 20:01:12 +00:00
|
|
|
let conn = await transport2.dial(transport1.addrs[0])
|
2021-08-03 13:48:03 +00:00
|
|
|
let serverConn = await acceptHandler
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(
|
|
|
|
transport1.stop(),
|
|
|
|
transport2.stop()))
|
|
|
|
|
|
|
|
check serverConn.closed()
|
|
|
|
check conn.closed()
|
|
|
|
|
|
|
|
asyncTest "read or write on closed connection":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init(ma).tryGet()]
|
2021-08-03 13:48:03 +00:00
|
|
|
let transport1 = prov()
|
|
|
|
await transport1.start(ma)
|
|
|
|
|
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
|
|
|
let conn = await transport1.accept()
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
let conn = await transport1.dial(transport1.addrs[0])
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
try:
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
|
|
|
check false
|
|
|
|
except CatchableError as exc:
|
|
|
|
check true
|
|
|
|
|
|
|
|
# we don't HAVE to throw on write on EOF
|
|
|
|
# (at least TCP doesn't)
|
|
|
|
try:
|
|
|
|
await conn.write(msg)
|
|
|
|
except CatchableError as exc:
|
|
|
|
check true
|
|
|
|
|
|
|
|
await conn.close() #for some protocols, closing requires actively reading, so we must close here
|
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
|
|
|
|
|
|
|
await transport1.stop()
|