better test

This commit is contained in:
Tanguy 2022-12-16 13:45:21 +01:00
parent 56b284a964
commit a797b5e408
No known key found for this signature in database
GPG Key ID: 7DD8EC6B6CE6C45E
3 changed files with 46 additions and 5 deletions

View File

@ -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)

View File

@ -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

View File

@ -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.} =