diff --git a/test/noise.test.ts b/test/noise.test.ts index 6d42cd5..ddc285e 100644 --- a/test/noise.test.ts +++ b/test/noise.test.ts @@ -14,7 +14,7 @@ import { import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder"; import {XXHandshake} from "../src/xx"; import {Buffer} from "buffer"; -import {getKeyPairFromPeerId} from "./utils"; +import {getKeyPairFromPeerId, getRandomBuffer} from "./utils"; describe("Noise", () => { let remotePeer, localPeer; @@ -96,4 +96,30 @@ describe("Noise", () => { assert(false, e.message); } }) + + + it("should test large payloads", async() => { + try { + const { privateKey: libp2pInitPrivKey } = getKeyPairFromPeerId(localPeer); + const { privateKey: libp2pRespPrivKey } = getKeyPairFromPeerId(remotePeer); + const noiseInit = new Noise(libp2pInitPrivKey); + const noiseResp = new Noise(libp2pRespPrivKey); + + const [inboundConnection, outboundConnection] = DuplexPair(); + const [outbound, inbound] = await Promise.all([ + noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer), + noiseResp.secureInbound(remotePeer, inboundConnection, localPeer), + ]); + const wrappedInbound = Wrap(inbound.conn); + const wrappedOutbound = Wrap(outbound.conn); + + const largePlaintext = getRandomBuffer(100000); + wrappedOutbound.writeLP(largePlaintext); + const response = await wrappedInbound.readLP(); + expect(response.equals(largePlaintext)).to.be.true; + } catch (e) { + console.error(e); + assert(false, e.message); + } + }); }); diff --git a/test/utils.ts b/test/utils.ts index 2ac5b7b..7f68b0d 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,5 +1,6 @@ import * as crypto from 'libp2p-crypto'; import {KeyPair, PeerId} from "../src/@types/libp2p"; +import {bytes} from "../src/@types/basic"; export async function generateEd25519Keys() { return await crypto.keys.generateKeyPair('ed25519'); @@ -11,3 +12,15 @@ export function getKeyPairFromPeerId(peerId: PeerId): KeyPair { publicKey: peerId.marshalPubKey(), } } + +export function getRandomBuffer(size: number) : bytes { + size = Math.max(1, size<<0); + + const buf = Buffer.alloc(size); + let i = 0; + for (; i < size; ++i) { + buf[i] = (Math.random() * 0xFF) << 0; + } + + return buf; +}