2019-08-24 19:16:34 +00:00
|
|
|
import unittest, strutils, sequtils, sugar
|
2019-08-23 22:17:08 +00:00
|
|
|
import chronos
|
2019-08-26 15:37:15 +00:00
|
|
|
import ../libp2p/connection, ../libp2p/multistreamselect,
|
2019-08-27 21:45:21 +00:00
|
|
|
../libp2p/stream, ../libp2p/connection, ../libp2p/multiaddress,
|
2019-08-29 20:54:55 +00:00
|
|
|
../libp2p/transport, ../libp2p/tcptransport, ../libp2p/protocol
|
2019-08-23 22:17:08 +00:00
|
|
|
|
2019-08-24 19:16:34 +00:00
|
|
|
## Mock stream for select test
|
2019-08-23 22:17:08 +00:00
|
|
|
type
|
2019-08-27 21:45:21 +00:00
|
|
|
TestSelectStream = ref object of LPStream
|
2019-08-23 22:17:08 +00:00
|
|
|
step*: int
|
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method readExactly*(s: TestSelectStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int): Future[void] {.async.} =
|
2019-08-23 22:17:08 +00:00
|
|
|
case s.step:
|
|
|
|
of 1:
|
|
|
|
var buf = newSeq[byte](1)
|
|
|
|
buf[0] = 19
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 2
|
|
|
|
of 2:
|
2019-08-23 23:54:30 +00:00
|
|
|
var buf = "/multistream/1.0.0\n"
|
2019-08-23 22:17:08 +00:00
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 3
|
|
|
|
of 3:
|
|
|
|
var buf = newSeq[byte](1)
|
|
|
|
buf[0] = 18
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 4
|
|
|
|
of 4:
|
|
|
|
var buf = "/test/proto/1.0.0\n"
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
2019-08-26 15:37:15 +00:00
|
|
|
else:
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)),
|
|
|
|
cstring("\0x3na\n"),
|
|
|
|
"\0x3na\n".len())
|
2019-08-23 22:17:08 +00:00
|
|
|
|
2019-08-23 23:54:30 +00:00
|
|
|
proc newTestSelectStream(): TestSelectStream =
|
2019-08-23 22:17:08 +00:00
|
|
|
new result
|
|
|
|
result.step = 1
|
|
|
|
|
2019-08-24 23:47:37 +00:00
|
|
|
## Mock stream for handles `ls` test
|
2019-08-24 19:16:34 +00:00
|
|
|
type
|
|
|
|
LsHandler = proc(procs: seq[byte]): Future[void]
|
|
|
|
|
2019-08-27 21:45:21 +00:00
|
|
|
TestLsStream = ref object of LPStream
|
2019-08-24 19:16:34 +00:00
|
|
|
step*: int
|
2019-08-24 23:47:37 +00:00
|
|
|
ls*: LsHandler
|
2019-08-24 19:16:34 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method readExactly*(s: TestLsStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int): Future[void] {.async.} =
|
2019-08-24 19:16:34 +00:00
|
|
|
case s.step:
|
|
|
|
of 1:
|
|
|
|
var buf = newSeq[byte](1)
|
|
|
|
buf[0] = 19
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 2
|
|
|
|
of 2:
|
|
|
|
var buf = "/multistream/1.0.0\n"
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 3
|
|
|
|
of 3:
|
|
|
|
var buf = newSeq[byte](1)
|
|
|
|
buf[0] = 3
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 4
|
|
|
|
of 4:
|
|
|
|
var buf = "ls\n"
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
2019-08-26 15:37:15 +00:00
|
|
|
else:
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)),
|
|
|
|
cstring("\0x3na\n"),
|
|
|
|
"\0x3na\n".len())
|
2019-08-24 19:16:34 +00:00
|
|
|
|
|
|
|
method write*(s: TestLsStream, msg: seq[byte], msglen = -1) {.async.} =
|
|
|
|
if s.step == 4:
|
|
|
|
await s.ls(msg)
|
|
|
|
|
|
|
|
proc newTestLsStream(ls: LsHandler): TestLsStream =
|
|
|
|
new result
|
|
|
|
result.ls = ls
|
|
|
|
result.step = 1
|
|
|
|
|
2019-08-24 23:47:37 +00:00
|
|
|
## Mock stream for handles `na` test
|
|
|
|
type
|
|
|
|
NaHandler = proc(procs: string): Future[void]
|
|
|
|
|
2019-08-27 21:45:21 +00:00
|
|
|
TestNaStream = ref object of LPStream
|
2019-08-24 23:47:37 +00:00
|
|
|
step*: int
|
|
|
|
na*: NaHandler
|
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method readExactly*(s: TestNaStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int): Future[void] {.async.} =
|
2019-08-24 23:47:37 +00:00
|
|
|
case s.step:
|
|
|
|
of 1:
|
|
|
|
var buf = newSeq[byte](1)
|
|
|
|
buf[0] = 19
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 2
|
|
|
|
of 2:
|
|
|
|
var buf = "/multistream/1.0.0\n"
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 3
|
|
|
|
of 3:
|
|
|
|
var buf = newSeq[byte](1)
|
|
|
|
buf[0] = 18
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
|
|
|
s.step = 4
|
|
|
|
of 4:
|
|
|
|
var buf = "/test/proto/1.0.0\n"
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
|
2019-08-26 15:37:15 +00:00
|
|
|
else:
|
|
|
|
copyMem(cast[pointer](cast[uint](pbytes)),
|
|
|
|
cstring("\0x3na\n"),
|
|
|
|
"\0x3na\n".len())
|
2019-08-24 23:47:37 +00:00
|
|
|
|
|
|
|
method write*(s: TestNaStream, msg: string, msglen = -1) {.async.} =
|
|
|
|
if s.step == 4:
|
|
|
|
await s.na(msg)
|
|
|
|
|
|
|
|
proc newTestNaStream(na: NaHandler): TestNaStream =
|
|
|
|
new result
|
|
|
|
result.na = na
|
|
|
|
result.step = 1
|
|
|
|
|
2019-08-23 23:54:30 +00:00
|
|
|
suite "Multistream select":
|
2019-08-24 19:16:34 +00:00
|
|
|
test "test select custom proto":
|
|
|
|
proc testSelect(): Future[bool] {.async.} =
|
|
|
|
let ms = newMultistream()
|
|
|
|
let conn = newConnection(newTestSelectStream())
|
|
|
|
result = await ms.select(conn, "/test/proto/1.0.0")
|
2019-08-23 22:17:08 +00:00
|
|
|
|
2019-08-24 19:16:34 +00:00
|
|
|
check:
|
|
|
|
waitFor(testSelect()) == true
|
2019-08-23 23:54:30 +00:00
|
|
|
|
|
|
|
test "test handle custom proto":
|
|
|
|
proc testHandle(): Future[bool] {.async.} =
|
|
|
|
let ms = newMultistream()
|
2019-08-28 19:04:08 +00:00
|
|
|
let conn = newConnection(newTestSelectStream())
|
2019-08-23 23:54:30 +00:00
|
|
|
|
2019-08-29 20:54:55 +00:00
|
|
|
var protocol: LPProtocol
|
|
|
|
proc testHandler(protocol: LPProtocol,
|
|
|
|
conn: Connection,
|
2019-08-28 19:04:08 +00:00
|
|
|
proto: string): Future[void] {.async, gcsafe.} =
|
2019-08-23 23:54:30 +00:00
|
|
|
check proto == "/test/proto/1.0.0"
|
|
|
|
|
2019-08-29 20:54:55 +00:00
|
|
|
ms.addHandler("/test/proto/1.0.0", protocol, testHandler)
|
2019-08-23 23:54:30 +00:00
|
|
|
await ms.handle(conn)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
2019-08-24 19:16:34 +00:00
|
|
|
waitFor(testHandle()) == true
|
2019-08-26 15:37:15 +00:00
|
|
|
|
2019-08-24 19:16:34 +00:00
|
|
|
test "test handle `ls`":
|
|
|
|
proc testLs(): Future[bool] {.async.} =
|
|
|
|
let ms = newMultistream()
|
|
|
|
|
|
|
|
proc testLs(proto: seq[byte]): Future[void] {.async.}
|
|
|
|
let conn = newConnection(newTestLsStream(testLs))
|
|
|
|
|
|
|
|
proc testLs(proto: seq[byte]): Future[void] {.async.} =
|
|
|
|
var strProto: string = cast[string](proto)
|
2019-08-25 19:09:08 +00:00
|
|
|
check strProto == "\x26/test/proto1/1.0.0\n/test/proto2/1.0.0\n"
|
2019-08-24 19:16:34 +00:00
|
|
|
await conn.close()
|
|
|
|
|
2019-08-29 20:54:55 +00:00
|
|
|
var protocol: LPProtocol
|
|
|
|
proc testHandler(protocol: LPProtocol,
|
|
|
|
conn: Connection,
|
2019-08-28 19:04:08 +00:00
|
|
|
proto: string): Future[void] {.async, gcsafe.} = discard
|
2019-08-29 20:54:55 +00:00
|
|
|
ms.addHandler("/test/proto1/1.0.0", protocol, testHandler)
|
|
|
|
ms.addHandler("/test/proto2/1.0.0", protocol, testHandler)
|
2019-08-24 19:16:34 +00:00
|
|
|
await ms.handle(conn)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testLs()) == true
|
2019-08-24 23:47:37 +00:00
|
|
|
|
|
|
|
test "test handle `na`":
|
|
|
|
proc testNa(): Future[bool] {.async.} =
|
|
|
|
let ms = newMultistream()
|
|
|
|
|
|
|
|
proc testNa(msg: string): Future[void] {.async.}
|
|
|
|
let conn = newConnection(newTestNaStream(testNa))
|
|
|
|
|
|
|
|
proc testNa(msg: string): Future[void] {.async.} =
|
|
|
|
check cast[string](msg) == "\x3na\n"
|
|
|
|
await conn.close()
|
|
|
|
|
2019-08-29 20:54:55 +00:00
|
|
|
var protocol: LPProtocol
|
|
|
|
proc testHandler(protocol: LPProtocol,
|
|
|
|
conn: Connection,
|
2019-08-28 19:04:08 +00:00
|
|
|
proto: string): Future[void] {.async, gcsafe.} = discard
|
2019-08-29 20:54:55 +00:00
|
|
|
ms.addHandler("/unabvailable/proto/1.0.0", protocol, testHandler)
|
2019-08-24 23:47:37 +00:00
|
|
|
|
|
|
|
await ms.handle(conn)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testNa()) == true
|
2019-08-25 19:09:08 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
test "e2e - handle":
|
2019-08-25 19:09:08 +00:00
|
|
|
proc endToEnd(): Future[bool] {.async.} =
|
2019-08-28 02:32:47 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53350")
|
2019-08-29 20:54:55 +00:00
|
|
|
var protocol: LPProtocol
|
|
|
|
proc testHandler(protocol: LPProtocol,
|
|
|
|
conn: Connection,
|
2019-08-28 19:04:08 +00:00
|
|
|
proto: string): Future[void] {.async, gcsafe.} =
|
2019-08-25 19:09:08 +00:00
|
|
|
check proto == "/test/proto/1.0.0"
|
|
|
|
await conn.writeLp("Hello!")
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let msListen = newMultistream()
|
2019-08-29 20:54:55 +00:00
|
|
|
msListen.addHandler("/test/proto/1.0.0", protocol, testHandler)
|
2019-08-25 19:09:08 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
|
2019-08-25 19:09:08 +00:00
|
|
|
await msListen.handle(conn)
|
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
|
|
|
await transport1.listen(ma, connHandler)
|
|
|
|
|
|
|
|
let msDial = newMultistream()
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(ma)
|
2019-08-26 15:37:15 +00:00
|
|
|
|
2019-08-25 19:09:08 +00:00
|
|
|
let res = await msDial.select(conn, "/test/proto/1.0.0")
|
|
|
|
check res == true
|
|
|
|
|
|
|
|
let hello = cast[string](await conn.readLp())
|
|
|
|
result = hello == "Hello!"
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(endToEnd()) == true
|
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
test "e2e - ls":
|
2019-08-25 19:09:08 +00:00
|
|
|
proc endToEnd(): Future[bool] {.async.} =
|
2019-08-28 02:32:47 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53351")
|
2019-08-25 19:09:08 +00:00
|
|
|
|
|
|
|
let msListen = newMultistream()
|
2019-08-29 20:54:55 +00:00
|
|
|
var protocol: LPProtocol
|
|
|
|
proc testHandler(protocol: LPProtocol,
|
|
|
|
conn: Connection,
|
2019-08-26 15:37:15 +00:00
|
|
|
proto: string): Future[void] {.async.} = discard
|
2019-08-29 20:54:55 +00:00
|
|
|
msListen.addHandler("/test/proto1/1.0.0", protocol, testHandler)
|
|
|
|
msListen.addHandler("/test/proto2/1.0.0", protocol, testHandler)
|
2019-08-25 19:09:08 +00:00
|
|
|
|
|
|
|
let transport1: TcpTransport = newTransport(TcpTransport)
|
2019-08-26 15:37:15 +00:00
|
|
|
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
|
2019-08-25 19:09:08 +00:00
|
|
|
await msListen.handle(conn)
|
|
|
|
|
|
|
|
await transport1.listen(ma, connHandler)
|
|
|
|
|
|
|
|
let msDial = newMultistream()
|
|
|
|
let transport2: TcpTransport = newTransport(TcpTransport)
|
|
|
|
let conn = await transport2.dial(ma)
|
2019-08-26 15:37:15 +00:00
|
|
|
|
2019-08-25 19:09:08 +00:00
|
|
|
let ls = await msDial.list(conn)
|
|
|
|
let protos: seq[string] = @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
|
|
|
await conn.close()
|
|
|
|
result = ls == protos
|
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(endToEnd()) == true
|