Create test for large payload

This commit is contained in:
Belma Gutlic 2019-12-24 16:25:49 +01:00
parent 128aba164d
commit 4a04e955dd
2 changed files with 40 additions and 1 deletions

View File

@ -14,7 +14,7 @@ import {
import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder"; import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder";
import {XXHandshake} from "../src/xx"; import {XXHandshake} from "../src/xx";
import {Buffer} from "buffer"; import {Buffer} from "buffer";
import {getKeyPairFromPeerId} from "./utils"; import {getKeyPairFromPeerId, getRandomBuffer} from "./utils";
describe("Noise", () => { describe("Noise", () => {
let remotePeer, localPeer; let remotePeer, localPeer;
@ -96,4 +96,30 @@ describe("Noise", () => {
assert(false, e.message); 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);
}
});
}); });

View File

@ -1,5 +1,6 @@
import * as crypto from 'libp2p-crypto'; import * as crypto from 'libp2p-crypto';
import {KeyPair, PeerId} from "../src/@types/libp2p"; import {KeyPair, PeerId} from "../src/@types/libp2p";
import {bytes} from "../src/@types/basic";
export async function generateEd25519Keys() { export async function generateEd25519Keys() {
return await crypto.keys.generateKeyPair('ed25519'); return await crypto.keys.generateKeyPair('ed25519');
@ -11,3 +12,15 @@ export function getKeyPairFromPeerId(peerId: PeerId): KeyPair {
publicKey: peerId.marshalPubKey(), 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;
}