2020-05-06 16:31:47 +00:00
|
|
|
import unittest, strformat, strformat, random
|
2019-09-09 17:33:32 +00:00
|
|
|
import chronos, nimcrypto/utils, chronicles
|
2020-04-21 01:24:42 +00:00
|
|
|
import ../libp2p/[errors,
|
|
|
|
connection,
|
2019-10-29 18:51:48 +00:00
|
|
|
stream/lpstream,
|
|
|
|
stream/bufferstream,
|
|
|
|
transports/tcptransport,
|
|
|
|
transports/transport,
|
2019-10-03 19:30:22 +00:00
|
|
|
multiaddress,
|
2019-10-29 18:51:48 +00:00
|
|
|
muxers/mplex/mplex,
|
|
|
|
muxers/mplex/coder,
|
2019-10-03 19:30:22 +00:00
|
|
|
muxers/mplex/types,
|
2020-03-11 15:12:08 +00:00
|
|
|
muxers/mplex/lpchannel,
|
|
|
|
vbuffer,
|
|
|
|
varint]
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2019-10-29 18:51:48 +00:00
|
|
|
when defined(nimHasUsed): {.used.}
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
const
|
|
|
|
StreamTransportTrackerName = "stream.transport"
|
|
|
|
StreamServerTrackerName = "stream.server"
|
|
|
|
|
2019-09-03 20:40:51 +00:00
|
|
|
suite "Mplex":
|
2020-04-21 01:24:42 +00:00
|
|
|
teardown:
|
|
|
|
let
|
|
|
|
trackers = [
|
|
|
|
getTracker(BufferStreamTrackerName),
|
|
|
|
getTracker(AsyncStreamWriterTrackerName),
|
|
|
|
getTracker(TcpTransportTrackerName),
|
|
|
|
getTracker(AsyncStreamReaderTrackerName),
|
|
|
|
getTracker(StreamTransportTrackerName),
|
|
|
|
getTracker(StreamServerTrackerName)
|
|
|
|
]
|
|
|
|
for tracker in trackers:
|
|
|
|
if not isNil(tracker):
|
|
|
|
# echo tracker.dump()
|
|
|
|
check tracker.isLeaked() == false
|
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
test "encode header with channel id 0":
|
|
|
|
proc testEncodeHeader(): Future[bool] {.async.} =
|
|
|
|
proc encHandler(msg: seq[byte]) {.async.} =
|
|
|
|
check msg == fromHex("000873747265616d2031")
|
2019-10-29 18:51:48 +00:00
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
let stream = newBufferStream(encHandler)
|
|
|
|
let conn = newConnection(stream)
|
2019-09-12 02:10:38 +00:00
|
|
|
await conn.writeMsg(0, MessageType.New, cast[seq[byte]]("stream 1"))
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
result = true
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await stream.close()
|
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
check:
|
|
|
|
waitFor(testEncodeHeader()) == true
|
|
|
|
|
|
|
|
test "encode header with channel id other than 0":
|
2019-09-04 01:43:57 +00:00
|
|
|
proc testEncodeHeader(): Future[bool] {.async.} =
|
2019-09-07 06:20:03 +00:00
|
|
|
proc encHandler(msg: seq[byte]) {.async.} =
|
|
|
|
check msg == fromHex("88010873747265616d2031")
|
2019-10-29 18:51:48 +00:00
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
let stream = newBufferStream(encHandler)
|
|
|
|
let conn = newConnection(stream)
|
2019-09-12 02:10:38 +00:00
|
|
|
await conn.writeMsg(17, MessageType.New, cast[seq[byte]]("stream 1"))
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-09-04 01:43:57 +00:00
|
|
|
result = true
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await stream.close()
|
|
|
|
|
2019-09-04 01:43:57 +00:00
|
|
|
check:
|
|
|
|
waitFor(testEncodeHeader()) == true
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
test "encode header and body with channel id 0":
|
|
|
|
proc testEncodeHeaderBody(): Future[bool] {.async.} =
|
|
|
|
var step = 0
|
|
|
|
proc encHandler(msg: seq[byte]) {.async.} =
|
|
|
|
check msg == fromHex("020873747265616d2031")
|
2019-10-29 18:51:48 +00:00
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
let stream = newBufferStream(encHandler)
|
|
|
|
let conn = newConnection(stream)
|
2019-09-12 02:10:38 +00:00
|
|
|
await conn.writeMsg(0, MessageType.MsgOut, cast[seq[byte]]("stream 1"))
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
result = true
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await stream.close()
|
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
check:
|
|
|
|
waitFor(testEncodeHeaderBody()) == true
|
|
|
|
|
|
|
|
test "encode header and body with channel id other than 0":
|
|
|
|
proc testEncodeHeaderBody(): Future[bool] {.async.} =
|
|
|
|
var step = 0
|
|
|
|
proc encHandler(msg: seq[byte]) {.async.} =
|
|
|
|
check msg == fromHex("8a010873747265616d2031")
|
|
|
|
|
|
|
|
let stream = newBufferStream(encHandler)
|
|
|
|
let conn = newConnection(stream)
|
2019-09-12 02:10:38 +00:00
|
|
|
await conn.writeMsg(17, MessageType.MsgOut, cast[seq[byte]]("stream 1"))
|
2019-09-07 06:20:03 +00:00
|
|
|
await conn.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
result = true
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await stream.close()
|
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
check:
|
|
|
|
waitFor(testEncodeHeaderBody()) == true
|
|
|
|
|
|
|
|
test "decode header with channel id 0":
|
2019-09-04 01:43:57 +00:00
|
|
|
proc testDecodeHeader(): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
let stream = newBufferStream()
|
2019-09-07 06:20:03 +00:00
|
|
|
let conn = newConnection(stream)
|
|
|
|
await stream.pushTo(fromHex("000873747265616d2031"))
|
2019-09-08 06:32:41 +00:00
|
|
|
let msg = await conn.readMsg()
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2020-02-12 14:43:42 +00:00
|
|
|
check msg.id == 0
|
|
|
|
check msg.msgType == MessageType.New
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-02-12 14:43:42 +00:00
|
|
|
result = true
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await stream.close()
|
|
|
|
|
2019-09-04 01:43:57 +00:00
|
|
|
check:
|
|
|
|
waitFor(testDecodeHeader()) == true
|
2019-09-07 06:20:03 +00:00
|
|
|
|
|
|
|
test "decode header and body with channel id 0":
|
|
|
|
proc testDecodeHeader(): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
let stream = newBufferStream()
|
2019-09-07 06:20:03 +00:00
|
|
|
let conn = newConnection(stream)
|
|
|
|
await stream.pushTo(fromHex("021668656C6C6F2066726F6D206368616E6E656C20302121"))
|
2019-09-08 06:32:41 +00:00
|
|
|
let msg = await conn.readMsg()
|
2019-09-07 06:20:03 +00:00
|
|
|
|
2020-02-12 14:43:42 +00:00
|
|
|
check msg.id == 0
|
|
|
|
check msg.msgType == MessageType.MsgOut
|
|
|
|
check cast[string](msg.data) == "hello from channel 0!!"
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-02-12 14:43:42 +00:00
|
|
|
result = true
|
2019-09-07 06:20:03 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await stream.close()
|
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
check:
|
|
|
|
waitFor(testDecodeHeader()) == true
|
|
|
|
|
|
|
|
test "decode header and body with channel id other than 0":
|
|
|
|
proc testDecodeHeader(): Future[bool] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
let stream = newBufferStream()
|
2019-09-07 06:20:03 +00:00
|
|
|
let conn = newConnection(stream)
|
|
|
|
await stream.pushTo(fromHex("8a011668656C6C6F2066726F6D206368616E6E656C20302121"))
|
2019-09-08 06:32:41 +00:00
|
|
|
let msg = await conn.readMsg()
|
2019-09-07 06:20:03 +00:00
|
|
|
|
2020-02-12 14:43:42 +00:00
|
|
|
check msg.id == 17
|
|
|
|
check msg.msgType == MessageType.MsgOut
|
|
|
|
check cast[string](msg.data) == "hello from channel 0!!"
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-02-12 14:43:42 +00:00
|
|
|
result = true
|
2019-09-07 06:20:03 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await stream.close()
|
|
|
|
|
2019-09-07 06:20:03 +00:00
|
|
|
check:
|
|
|
|
waitFor(testDecodeHeader()) == true
|
2020-02-12 14:43:42 +00:00
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
test "e2e - read/write receiver":
|
|
|
|
proc testNewStream(): Future[bool] {.async.} =
|
2020-02-09 03:46:52 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
2019-09-25 22:57:27 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
var
|
|
|
|
done = newFuture[void]()
|
|
|
|
done2 = newFuture[void]()
|
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
2019-10-29 18:51:48 +00:00
|
|
|
proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
|
2019-09-25 22:57:27 +00:00
|
|
|
let msg = await stream.readLp()
|
|
|
|
check cast[string](msg) == "Hello from stream!"
|
|
|
|
await stream.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
done.complete()
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
let mplexListen = newMplex(conn)
|
|
|
|
mplexListen.streamHandler = handleMplexListen
|
2020-04-21 01:24:42 +00:00
|
|
|
await mplexListen.handle()
|
|
|
|
await conn.close()
|
|
|
|
done2.complete()
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2020-04-21 01:24:42 +00:00
|
|
|
let lfut = await transport1.listen(ma, connHandler)
|
2020-02-12 14:43:42 +00:00
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let mplexDial = newMplex(conn)
|
2020-02-11 17:30:36 +00:00
|
|
|
let stream = await mplexDial.newStream()
|
|
|
|
let openState = cast[LPChannel](stream.stream).isOpen
|
|
|
|
await stream.writeLp("Hello from stream!")
|
|
|
|
await conn.close()
|
|
|
|
check openState # not lazy
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-02-11 17:30:36 +00:00
|
|
|
result = true
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await done.wait(5000.millis)
|
|
|
|
await done2.wait(5000.millis)
|
|
|
|
await stream.close()
|
|
|
|
await conn.close()
|
|
|
|
await transport2.close()
|
|
|
|
await transport1.close()
|
|
|
|
await lfut
|
|
|
|
|
2020-02-11 17:30:36 +00:00
|
|
|
check:
|
|
|
|
waitFor(testNewStream()) == true
|
|
|
|
|
|
|
|
test "e2e - read/write receiver lazy":
|
|
|
|
proc testNewStream(): Future[bool] {.async.} =
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
var
|
|
|
|
done = newFuture[void]()
|
|
|
|
done2 = newFuture[void]()
|
|
|
|
|
2020-02-11 17:30:36 +00:00
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
|
|
|
proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
|
|
|
|
let msg = await stream.readLp()
|
|
|
|
check cast[string](msg) == "Hello from stream!"
|
|
|
|
await stream.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
done.complete()
|
2020-02-11 17:30:36 +00:00
|
|
|
|
|
|
|
let mplexListen = newMplex(conn)
|
|
|
|
mplexListen.streamHandler = handleMplexListen
|
2020-04-21 01:24:42 +00:00
|
|
|
await mplexListen.handle()
|
|
|
|
done2.complete()
|
2020-02-11 17:30:36 +00:00
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2020-03-27 23:37:00 +00:00
|
|
|
let listenFut = await transport1.listen(ma, connHandler)
|
2020-02-11 17:30:36 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let mplexDial = newMplex(conn)
|
|
|
|
let stream = await mplexDial.newStream("", true)
|
|
|
|
let openState = cast[LPChannel](stream.stream).isOpen
|
2019-09-25 22:57:27 +00:00
|
|
|
await stream.writeLp("Hello from stream!")
|
|
|
|
await conn.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
|
2020-02-12 14:43:42 +00:00
|
|
|
check not openState # assert lazy
|
2019-09-25 22:57:27 +00:00
|
|
|
result = true
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await done.wait(5000.millis)
|
|
|
|
await done2.wait(5000.millis)
|
|
|
|
await conn.close()
|
|
|
|
await stream.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await mplexDial.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
await transport2.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await transport1.close()
|
|
|
|
await listenFut
|
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
check:
|
|
|
|
waitFor(testNewStream()) == true
|
|
|
|
|
2020-04-03 15:26:46 +00:00
|
|
|
test "e2e - write fragmented":
|
2020-02-06 06:24:11 +00:00
|
|
|
proc testNewStream(): Future[bool] {.async.} =
|
2020-02-07 06:41:24 +00:00
|
|
|
let
|
|
|
|
ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
2020-02-09 03:46:52 +00:00
|
|
|
listenJob = newFuture[void]()
|
2020-02-06 06:24:11 +00:00
|
|
|
|
2020-04-03 15:26:46 +00:00
|
|
|
var bigseq = newSeqOfCap[uint8](MaxMsgSize * 2)
|
|
|
|
for _ in 0..<MaxMsgSize:
|
|
|
|
bigseq.add(uint8(rand(uint('A')..uint('z'))))
|
|
|
|
|
2020-02-06 06:24:11 +00:00
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
|
|
|
proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
|
2020-02-11 05:48:52 +00:00
|
|
|
defer:
|
|
|
|
await stream.close()
|
2020-04-03 15:26:46 +00:00
|
|
|
let msg = await stream.readLp()
|
|
|
|
check msg == bigseq
|
|
|
|
trace "Bigseq check passed!"
|
2020-02-09 03:46:52 +00:00
|
|
|
listenJob.complete()
|
2020-02-06 06:24:11 +00:00
|
|
|
|
|
|
|
let mplexListen = newMplex(conn)
|
|
|
|
mplexListen.streamHandler = handleMplexListen
|
|
|
|
discard mplexListen.handle()
|
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2020-03-27 23:37:00 +00:00
|
|
|
let listenFut = await transport1.listen(ma, connHandler)
|
2020-02-06 06:24:11 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let mplexDial = newMplex(conn)
|
|
|
|
let stream = await mplexDial.newStream()
|
2020-03-27 14:25:52 +00:00
|
|
|
|
2020-04-03 15:26:46 +00:00
|
|
|
await stream.writeLp(bigseq)
|
2020-02-11 05:48:52 +00:00
|
|
|
try:
|
2020-04-03 15:26:46 +00:00
|
|
|
await listenJob.wait(millis(5000))
|
2020-02-11 05:48:52 +00:00
|
|
|
except AsyncTimeoutError:
|
2020-04-03 15:26:46 +00:00
|
|
|
check false
|
2020-03-27 14:25:52 +00:00
|
|
|
|
2020-02-06 06:24:11 +00:00
|
|
|
result = true
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await stream.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await mplexDial.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
await conn.close()
|
|
|
|
await transport2.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await transport1.close()
|
|
|
|
await listenFut
|
|
|
|
|
2020-02-06 06:24:11 +00:00
|
|
|
check:
|
|
|
|
waitFor(testNewStream()) == true
|
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
test "e2e - read/write initiator":
|
|
|
|
proc testNewStream(): Future[bool] {.async.} =
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
let done = newFuture[void]()
|
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
2019-10-29 18:51:48 +00:00
|
|
|
proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
|
2019-09-25 22:57:27 +00:00
|
|
|
await stream.writeLp("Hello from stream!")
|
|
|
|
await stream.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
done.complete()
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
let mplexListen = newMplex(conn)
|
|
|
|
mplexListen.streamHandler = handleMplexListen
|
|
|
|
await mplexListen.handle()
|
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2020-03-27 23:37:00 +00:00
|
|
|
let listenFut = await transport1.listen(ma, connHandler)
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let mplexDial = newMplex(conn)
|
|
|
|
let dialFut = mplexDial.handle()
|
|
|
|
let stream = await mplexDial.newStream("DIALER")
|
|
|
|
let msg = cast[string](await stream.readLp())
|
|
|
|
check msg == "Hello from stream!"
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
# await dialFut
|
|
|
|
result = true
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await done.wait(5000.millis)
|
|
|
|
await stream.close()
|
|
|
|
await conn.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await mplexDial.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
await transport2.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await transport1.close()
|
|
|
|
await listenFut
|
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
check:
|
|
|
|
waitFor(testNewStream()) == true
|
|
|
|
|
|
|
|
test "e2e - multiple streams":
|
|
|
|
proc testNewStream(): Future[bool] {.async.} =
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
let done = newFuture[void]()
|
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
var count = 1
|
|
|
|
var listenConn: Connection
|
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
2019-10-29 18:51:48 +00:00
|
|
|
proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
|
2019-09-25 22:57:27 +00:00
|
|
|
let msg = await stream.readLp()
|
|
|
|
check cast[string](msg) == &"stream {count}!"
|
|
|
|
count.inc
|
|
|
|
await stream.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
if count == 10:
|
|
|
|
done.complete()
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
listenConn = conn
|
|
|
|
let mplexListen = newMplex(conn)
|
|
|
|
mplexListen.streamHandler = handleMplexListen
|
|
|
|
await mplexListen.handle()
|
|
|
|
|
2020-03-27 23:37:00 +00:00
|
|
|
let transport1 = newTransport(TcpTransport)
|
|
|
|
let listenFut = await transport1.listen(ma, connHandler)
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let mplexDial = newMplex(conn)
|
2020-03-27 23:37:00 +00:00
|
|
|
for i in 1..10:
|
2019-09-25 22:57:27 +00:00
|
|
|
let stream = await mplexDial.newStream()
|
|
|
|
await stream.writeLp(&"stream {i}!")
|
|
|
|
await stream.close()
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await done.wait(5000.millis)
|
|
|
|
await conn.close()
|
|
|
|
await transport2.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await mplexDial.close()
|
2019-09-25 22:57:27 +00:00
|
|
|
await listenConn.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await transport1.close()
|
|
|
|
await listenFut
|
|
|
|
|
2019-09-25 22:57:27 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testNewStream()) == true
|
|
|
|
|
|
|
|
test "e2e - multiple read/write streams":
|
|
|
|
proc testNewStream(): Future[bool] {.async.} =
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
|
|
|
|
|
|
|
var count = 1
|
|
|
|
var listenConn: Connection
|
2020-04-21 01:24:42 +00:00
|
|
|
let done = newFuture[void]()
|
2019-09-25 22:57:27 +00:00
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
|
|
|
listenConn = conn
|
2019-10-29 18:51:48 +00:00
|
|
|
proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
|
2019-09-25 22:57:27 +00:00
|
|
|
let msg = await stream.readLp()
|
|
|
|
check cast[string](msg) == &"stream {count} from dialer!"
|
|
|
|
await stream.writeLp(&"stream {count} from listener!")
|
|
|
|
count.inc
|
|
|
|
await stream.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
if count == 10:
|
|
|
|
done.complete()
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
let mplexListen = newMplex(conn)
|
|
|
|
mplexListen.streamHandler = handleMplexListen
|
2020-04-21 01:24:42 +00:00
|
|
|
await mplexListen.handle()
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2020-03-27 23:37:00 +00:00
|
|
|
let transportFut = await transport1.listen(ma, connHandler)
|
2019-09-25 22:57:27 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let mplexDial = newMplex(conn)
|
|
|
|
let dialFut = mplexDial.handle()
|
2019-10-29 18:51:48 +00:00
|
|
|
dialFut.addCallback(proc(udata: pointer = nil) {.gcsafe.}
|
2019-11-06 18:25:33 +00:00
|
|
|
= trace "completed dialer")
|
2019-09-25 22:57:27 +00:00
|
|
|
for i in 1..10:
|
|
|
|
let stream = await mplexDial.newStream("dialer stream")
|
|
|
|
await stream.writeLp(&"stream {i} from dialer!")
|
|
|
|
let msg = await stream.readLp()
|
|
|
|
check cast[string](msg) == &"stream {i} from listener!"
|
|
|
|
await stream.close()
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await done.wait(5.seconds)
|
2019-09-25 22:57:27 +00:00
|
|
|
await conn.close()
|
|
|
|
await listenConn.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
await allFuturesThrowing(dialFut)
|
|
|
|
await mplexDial.close()
|
|
|
|
await transport2.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await transport1.close()
|
|
|
|
await transportFut
|
2019-09-25 22:57:27 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testNewStream()) == true
|
2019-09-08 06:32:41 +00:00
|
|
|
|
2019-09-04 06:40:11 +00:00
|
|
|
test "half closed - channel should close for write":
|
|
|
|
proc testClosedForWrite(): Future[void] {.async.} =
|
2019-09-07 06:20:03 +00:00
|
|
|
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard
|
2020-04-21 01:24:42 +00:00
|
|
|
let
|
|
|
|
buff = newBufferStream(writeHandler)
|
|
|
|
conn = newConnection(buff)
|
|
|
|
chann = newChannel(1, conn, true)
|
|
|
|
try:
|
|
|
|
await chann.close()
|
|
|
|
await chann.write("Hello")
|
|
|
|
finally:
|
|
|
|
await chann.cleanUp()
|
|
|
|
await conn.close()
|
2019-09-04 06:40:11 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
expect LPStreamEOFError:
|
2019-09-04 06:40:11 +00:00
|
|
|
waitFor(testClosedForWrite())
|
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
test "half closed - channel should close for read by remote":
|
|
|
|
proc testClosedForRead(): Future[void] {.async.} =
|
|
|
|
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard
|
2020-04-21 01:24:42 +00:00
|
|
|
let
|
|
|
|
buff = newBufferStream(writeHandler)
|
|
|
|
conn = newConnection(buff)
|
|
|
|
chann = newChannel(1, conn, true)
|
2020-03-27 14:25:52 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
try:
|
|
|
|
await chann.pushTo(cast[seq[byte]]("Hello!"))
|
|
|
|
await chann.closedByRemote()
|
2020-05-06 16:31:47 +00:00
|
|
|
discard await chann.read(6) # this should work, since there is data in the buffer
|
|
|
|
discard await chann.read(6) # this should throw
|
2020-04-21 01:24:42 +00:00
|
|
|
finally:
|
|
|
|
await chann.cleanUp()
|
|
|
|
await conn.close()
|
2020-03-27 14:25:52 +00:00
|
|
|
|
|
|
|
expect LPStreamEOFError:
|
|
|
|
waitFor(testClosedForRead())
|
2019-09-04 06:40:11 +00:00
|
|
|
|
2020-03-11 15:12:08 +00:00
|
|
|
test "jitter - channel should be able to handle erratic read/writes":
|
|
|
|
proc test(): Future[bool] {.async.} =
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
|
|
|
|
|
|
|
var complete = newFuture[void]()
|
2020-03-11 22:23:39 +00:00
|
|
|
const MsgSize = 1024
|
2020-03-11 15:12:08 +00:00
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
|
|
|
proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
|
|
|
|
let msg = await stream.readLp()
|
|
|
|
check msg.len == MsgSize
|
|
|
|
await stream.close()
|
|
|
|
complete.complete()
|
|
|
|
|
|
|
|
let mplexListen = newMplex(conn)
|
|
|
|
mplexListen.streamHandler = handleMplexListen
|
|
|
|
discard mplexListen.handle()
|
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2020-03-27 23:37:00 +00:00
|
|
|
let listenFut = await transport1.listen(ma, connHandler)
|
2020-03-11 15:12:08 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let mplexDial = newMplex(conn)
|
|
|
|
let stream = await mplexDial.newStream()
|
|
|
|
var bigseq = newSeqOfCap[uint8](MaxMsgSize + 1)
|
|
|
|
for _ in 0..<MsgSize: # write one less than max size
|
|
|
|
bigseq.add(uint8(rand(uint('A')..uint('z'))))
|
|
|
|
|
|
|
|
## create lenght prefixed libp2p frame
|
|
|
|
var buf = initVBuffer()
|
|
|
|
buf.writeSeq(bigseq)
|
|
|
|
buf.finish()
|
|
|
|
|
|
|
|
## create mplex header
|
|
|
|
var mplexBuf = initVBuffer()
|
|
|
|
mplexBuf.writePBVarint((1.uint shl 3) or ord(MessageType.MsgOut).uint)
|
|
|
|
mplexBuf.writePBVarint(buf.buffer.len.uint) # size should be always sent
|
|
|
|
|
2020-03-11 15:36:45 +00:00
|
|
|
await conn.write(mplexBuf.buffer)
|
2020-03-11 15:12:08 +00:00
|
|
|
proc writer() {.async.} =
|
|
|
|
var sent = 0
|
|
|
|
randomize()
|
|
|
|
let total = buf.buffer.len
|
2020-03-11 22:23:39 +00:00
|
|
|
const min = 20
|
|
|
|
const max = 50
|
2020-03-11 15:12:08 +00:00
|
|
|
while sent < total:
|
2020-03-11 22:23:39 +00:00
|
|
|
var size = rand(min..max)
|
|
|
|
size = if size > buf.buffer.len: buf.buffer.len else: size
|
|
|
|
var send = buf.buffer[0..<size]
|
2020-03-11 15:12:08 +00:00
|
|
|
await conn.write(send)
|
2020-03-11 22:23:39 +00:00
|
|
|
sent += size
|
|
|
|
buf.buffer = buf.buffer[size..^1]
|
|
|
|
|
|
|
|
await writer()
|
|
|
|
|
|
|
|
await stream.close()
|
|
|
|
await conn.close()
|
|
|
|
await complete
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await transport2.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await transport1.close()
|
|
|
|
await listenFut
|
|
|
|
|
2020-03-11 22:23:39 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(test()) == true
|
|
|
|
|
|
|
|
test "jitter - channel should handle 1 byte read/write":
|
|
|
|
proc test(): Future[bool] {.async.} =
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
|
|
|
|
|
|
|
var complete = newFuture[void]()
|
|
|
|
const MsgSize = 512
|
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
|
|
|
proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
|
|
|
|
let msg = await stream.readLp()
|
|
|
|
check msg.len == MsgSize
|
|
|
|
await stream.close()
|
|
|
|
complete.complete()
|
|
|
|
|
|
|
|
let mplexListen = newMplex(conn)
|
|
|
|
mplexListen.streamHandler = handleMplexListen
|
|
|
|
discard mplexListen.handle()
|
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2020-03-27 23:37:00 +00:00
|
|
|
let listenFut = await transport1.listen(ma, connHandler)
|
2020-03-11 22:23:39 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let mplexDial = newMplex(conn)
|
|
|
|
let stream = await mplexDial.newStream()
|
|
|
|
var bigseq = newSeqOfCap[uint8](MaxMsgSize + 1)
|
|
|
|
for _ in 0..<MsgSize: # write one less than max size
|
|
|
|
bigseq.add(uint8(rand(uint('A')..uint('z'))))
|
|
|
|
|
|
|
|
## create lenght prefixed libp2p frame
|
|
|
|
var buf = initVBuffer()
|
|
|
|
buf.writeSeq(bigseq)
|
|
|
|
buf.finish()
|
|
|
|
|
|
|
|
## create mplex header
|
|
|
|
var mplexBuf = initVBuffer()
|
|
|
|
mplexBuf.writePBVarint((1.uint shl 3) or ord(MessageType.MsgOut).uint)
|
|
|
|
mplexBuf.writePBVarint(buf.buffer.len.uint) # size should be always sent
|
|
|
|
|
|
|
|
await conn.write(mplexBuf.buffer)
|
|
|
|
proc writer() {.async.} =
|
|
|
|
for i in buf.buffer:
|
|
|
|
await conn.write(@[i])
|
2020-03-11 15:12:08 +00:00
|
|
|
|
|
|
|
await writer()
|
|
|
|
|
|
|
|
await stream.close()
|
|
|
|
await conn.close()
|
|
|
|
await complete
|
2020-04-21 01:24:42 +00:00
|
|
|
await transport2.close()
|
2020-03-27 23:37:00 +00:00
|
|
|
await transport1.close()
|
|
|
|
await listenFut
|
|
|
|
|
2020-03-11 15:12:08 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(test()) == true
|
|
|
|
|
2019-09-04 06:40:11 +00:00
|
|
|
test "reset - channel should fail reading":
|
|
|
|
proc testResetRead(): Future[void] {.async.} =
|
2019-09-07 06:20:03 +00:00
|
|
|
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard
|
2020-04-21 01:24:42 +00:00
|
|
|
let
|
|
|
|
buff = newBufferStream(writeHandler)
|
|
|
|
conn = newConnection(buff)
|
|
|
|
chann = newChannel(1, conn, true)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await chann.reset()
|
2020-05-06 16:31:47 +00:00
|
|
|
var data = await chann.read(1)
|
2020-04-21 01:24:42 +00:00
|
|
|
doAssert(len(data) == 1)
|
|
|
|
finally:
|
|
|
|
await chann.cleanUp()
|
|
|
|
await conn.close()
|
2019-09-04 06:40:11 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
expect LPStreamEOFError:
|
2019-09-04 06:40:11 +00:00
|
|
|
waitFor(testResetRead())
|
|
|
|
|
|
|
|
test "reset - channel should fail writing":
|
|
|
|
proc testResetWrite(): Future[void] {.async.} =
|
2019-09-07 06:20:03 +00:00
|
|
|
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard
|
2020-04-21 01:24:42 +00:00
|
|
|
let
|
|
|
|
buff = newBufferStream(writeHandler)
|
|
|
|
conn = newConnection(buff)
|
|
|
|
chann = newChannel(1, conn, true)
|
|
|
|
try:
|
|
|
|
await chann.reset()
|
|
|
|
await chann.write(cast[seq[byte]]("Hello!"))
|
|
|
|
finally:
|
|
|
|
await chann.cleanUp()
|
|
|
|
await conn.close()
|
2019-09-04 06:40:11 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
expect LPStreamEOFError:
|
2019-09-04 06:40:11 +00:00
|
|
|
waitFor(testResetWrite())
|
2019-09-04 06:51:16 +00:00
|
|
|
|
|
|
|
test "should not allow pushing data to channel when remote end closed":
|
|
|
|
proc testResetWrite(): Future[void] {.async.} =
|
2019-09-07 06:20:03 +00:00
|
|
|
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard
|
2020-04-21 01:24:42 +00:00
|
|
|
let
|
|
|
|
buff = newBufferStream(writeHandler)
|
|
|
|
conn = newConnection(buff)
|
|
|
|
chann = newChannel(1, conn, true)
|
|
|
|
try:
|
|
|
|
await chann.closedByRemote()
|
|
|
|
await chann.pushTo(@[byte(1)])
|
|
|
|
finally:
|
|
|
|
await chann.cleanUp()
|
|
|
|
await conn.close()
|
2019-09-04 06:51:16 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
expect LPStreamEOFError:
|
2019-09-04 06:51:16 +00:00
|
|
|
waitFor(testResetWrite())
|