mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2025-03-01 08:30:29 +00:00
better test
This commit is contained in:
parent
56b284a964
commit
a797b5e408
@ -28,7 +28,7 @@ type
|
||||
method readMessage*(sconn: PlainTextConnection): Future[seq[byte]] {.async.} =
|
||||
var buffer: array[32768, byte]
|
||||
let length = await sconn.stream.readOnce(addr buffer[0], buffer.len)
|
||||
return @(buffer[0..length])
|
||||
return @(buffer[0 ..< length])
|
||||
|
||||
method write*(sconn: PlainTextConnection, message: seq[byte]): Future[void] =
|
||||
sconn.stream.write(message)
|
||||
|
@ -50,7 +50,8 @@ suite "Plain text":
|
||||
let conn = await transport1.accept()
|
||||
let sconn = await serverPlainText.secure(conn, false, Opt.none(PeerId))
|
||||
try:
|
||||
await sconn.write("Hello!")
|
||||
await sconn.writeLp("Hello 1!")
|
||||
await sconn.writeLp("Hello 2!")
|
||||
finally:
|
||||
await sconn.close()
|
||||
await conn.close()
|
||||
@ -65,8 +66,8 @@ suite "Plain text":
|
||||
|
||||
let sconn = await clientPlainText.secure(conn, true, Opt.some(serverInfo.peerId))
|
||||
|
||||
var msg = newSeq[byte](6)
|
||||
await sconn.readExactly(addr msg[0], 6)
|
||||
discard await sconn.readLp(100)
|
||||
var msg = await sconn.readLp(100)
|
||||
|
||||
await sconn.close()
|
||||
await conn.close()
|
||||
@ -74,7 +75,7 @@ suite "Plain text":
|
||||
await transport1.stop()
|
||||
await transport2.stop()
|
||||
|
||||
check string.fromBytes(msg) == "Hello!"
|
||||
check string.fromBytes(msg) == "Hello 2!"
|
||||
|
||||
asyncTest "e2e: wrong peerid":
|
||||
let
|
||||
|
@ -77,6 +77,46 @@ suite "Switch":
|
||||
check not switch1.isConnected(switch2.peerInfo.peerId)
|
||||
check not switch2.isConnected(switch1.peerInfo.peerId)
|
||||
|
||||
asyncTest "e2e plaintext encryption":
|
||||
let done = newFuture[void]()
|
||||
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
|
||||
try:
|
||||
let msg = string.fromBytes(await conn.readLp(1024))
|
||||
check "Hello!" == msg
|
||||
await conn.writeLp("Hello!")
|
||||
finally:
|
||||
await conn.close()
|
||||
done.complete()
|
||||
|
||||
let testProto = new TestProto
|
||||
testProto.codec = TestCodec
|
||||
testProto.handler = handle
|
||||
|
||||
let switch1 = newStandardSwitch(secureManagers=[PlainText])
|
||||
switch1.mount(testProto)
|
||||
|
||||
let switch2 = newStandardSwitch(secureManagers=[PlainText])
|
||||
await switch1.start()
|
||||
await switch2.start()
|
||||
|
||||
let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec)
|
||||
|
||||
check switch1.isConnected(switch2.peerInfo.peerId)
|
||||
check switch2.isConnected(switch1.peerInfo.peerId)
|
||||
|
||||
await conn.writeLp("Hello!")
|
||||
let msg = string.fromBytes(await conn.readLp(1024))
|
||||
check "Hello!" == msg
|
||||
await conn.close()
|
||||
|
||||
await allFuturesThrowing(
|
||||
done.wait(5.seconds),
|
||||
switch1.stop(),
|
||||
switch2.stop())
|
||||
|
||||
check not switch1.isConnected(switch2.peerInfo.peerId)
|
||||
check not switch2.isConnected(switch1.peerInfo.peerId)
|
||||
|
||||
asyncTest "e2e use switch dial proto string with custom matcher":
|
||||
let done = newFuture[void]()
|
||||
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
|
||||
|
Loading…
x
Reference in New Issue
Block a user