diff --git a/libp2p/protocols/secure/plaintext.nim b/libp2p/protocols/secure/plaintext.nim index 2075bfa45..3817ef05e 100644 --- a/libp2p/protocols/secure/plaintext.nim +++ b/libp2p/protocols/secure/plaintext.nim @@ -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) diff --git a/tests/testplaintext.nim b/tests/testplaintext.nim index 92a195fbd..97831e1e3 100644 --- a/tests/testplaintext.nim +++ b/tests/testplaintext.nim @@ -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 diff --git a/tests/testswitch.nim b/tests/testswitch.nim index 92a7d9f53..9f8dab317 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -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.} =