Remove dupe tests

This commit is contained in:
Franck Royer 2021-07-14 21:25:13 +10:00
parent 6afecd9989
commit 302fc20243
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 76 additions and 187 deletions

View File

@ -4,7 +4,7 @@ import fc from 'fast-check';
import { WakuMessage } from '../../lib/waku_message'; import { WakuMessage } from '../../lib/waku_message';
import { getPublicKey } from '../../lib/waku_message/version_1'; import { getPublicKey } from '../../lib/waku_message/version_1';
describe('Waku Message', function () { describe('Waku Message: Browser & Node', function () {
it('Waku message round trip binary serialization [clear]', async function () { it('Waku message round trip binary serialization [clear]', async function () {
await fc.assert( await fc.assert(
fc.asyncProperty(fc.string(), async (s) => { fc.asyncProperty(fc.string(), async (s) => {

View File

@ -1,6 +1,5 @@
import { expect } from 'chai'; import { expect } from 'chai';
import debug from 'debug'; import debug from 'debug';
import fc from 'fast-check';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: No types available // @ts-ignore: No types available
import TCP from 'libp2p-tcp'; import TCP from 'libp2p-tcp';
@ -21,200 +20,90 @@ import { DefaultContentTopic, WakuMessage } from './index';
const dbg = debug('waku:test:message'); const dbg = debug('waku:test:message');
describe('Waku Message', function () { describe('Waku Message: Node only', function () {
it('Waku message round trip binary serialization [clear]', async function () { describe('Interop: Nim', function () {
await fc.assert( let waku: Waku;
fc.asyncProperty(fc.string(), async (s) => { let nimWaku: NimWaku;
const msg = await WakuMessage.fromUtf8String(s);
const binary = msg.encode();
const actual = await WakuMessage.decode(binary);
expect(actual).to.deep.equal(msg); beforeEach(async function () {
}) this.timeout(30_000);
);
});
it('Payload to utf-8', async function () { waku = await Waku.create({
await fc.assert( staticNoiseKey: NOISE_KEY_1,
fc.asyncProperty(fc.string(), async (s) => { libp2p: {
const msg = await WakuMessage.fromUtf8String(s); addresses: { listen: ['/ip4/0.0.0.0/tcp/0'] },
const utf8 = msg.payloadAsUtf8; modules: { transport: [TCP] },
},
});
return utf8 === s; const multiAddrWithId = waku.getLocalMultiaddrWithID();
}) nimWaku = new NimWaku(makeLogFileName(this));
); await nimWaku.start({ staticnode: multiAddrWithId, rpcPrivate: true });
});
it('Waku message round trip binary encryption [asymmetric, no signature]', async function () { await new Promise((resolve) =>
await fc.assert( waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
fc.asyncProperty( );
fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
async (payload, privKey) => {
const publicKey = getPublicKey(privKey);
const msg = await WakuMessage.fromBytes(payload, {
encPublicKey: publicKey,
});
const wireBytes = msg.encode();
const actual = await WakuMessage.decode(wireBytes, [privKey]);
expect(actual?.payload).to.deep.equal(payload);
}
)
);
});
it('Waku message round trip binary encryption [asymmetric, signature]', async function () {
await fc.assert(
fc.asyncProperty(
fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
async (payload, sigPrivKey, encPrivKey) => {
const sigPubKey = getPublicKey(sigPrivKey);
const encPubKey = getPublicKey(encPrivKey);
const msg = await WakuMessage.fromBytes(payload, {
encPublicKey: encPubKey,
sigPrivKey: sigPrivKey,
});
const wireBytes = msg.encode();
const actual = await WakuMessage.decode(wireBytes, [encPrivKey]);
expect(actual?.payload).to.deep.equal(payload);
expect(actual?.signaturePublicKey).to.deep.equal(sigPubKey);
}
)
);
});
it('Waku message round trip binary encryption [symmetric, no signature]', async function () {
await fc.assert(
fc.asyncProperty(
fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
async (payload, key) => {
const msg = await WakuMessage.fromBytes(payload, {
symKey: key,
});
const wireBytes = msg.encode();
const actual = await WakuMessage.decode(wireBytes, [key]);
expect(actual?.payload).to.deep.equal(payload);
}
)
);
});
it('Waku message round trip binary encryption [symmetric, signature]', async function () {
await fc.assert(
fc.asyncProperty(
fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
async (payload, sigPrivKey, symKey) => {
const sigPubKey = getPublicKey(sigPrivKey);
const msg = await WakuMessage.fromBytes(payload, {
symKey: symKey,
sigPrivKey: sigPrivKey,
});
const wireBytes = msg.encode();
const actual = await WakuMessage.decode(wireBytes, [symKey]);
expect(actual?.payload).to.deep.equal(payload);
expect(actual?.signaturePublicKey).to.deep.equal(sigPubKey);
}
)
);
});
});
describe('Interop: Nim', function () {
let waku: Waku;
let nimWaku: NimWaku;
beforeEach(async function () {
this.timeout(30_000);
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
libp2p: {
addresses: { listen: ['/ip4/0.0.0.0/tcp/0'] },
modules: { transport: [TCP] },
},
}); });
const multiAddrWithId = waku.getLocalMultiaddrWithID(); afterEach(async function () {
nimWaku = new NimWaku(makeLogFileName(this)); nimWaku ? nimWaku.stop() : null;
await nimWaku.start({ staticnode: multiAddrWithId, rpcPrivate: true }); waku ? await waku.stop() : null;
await new Promise((resolve) =>
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
);
});
afterEach(async function () {
nimWaku ? nimWaku.stop() : null;
waku ? await waku.stop() : null;
});
it('JS decrypts nim message [asymmetric, no signature]', async function () {
this.timeout(10000);
await delay(200);
const messageText = 'Here is an encrypted message.';
const message: WakuRelayMessage = {
contentTopic: DefaultContentTopic,
payload: Buffer.from(messageText, 'utf-8').toString('hex'),
};
const privateKey = generatePrivateKey();
waku.relay.addDecryptionPrivateKey(privateKey);
const receivedMsgPromise: Promise<WakuMessage> = new Promise((resolve) => {
waku.relay.addObserver(resolve);
}); });
const publicKey = getPublicKey(privateKey); it('JS decrypts nim message [asymmetric, no signature]', async function () {
dbg('Post message'); this.timeout(10000);
await nimWaku.postAsymmetricMessage(message, publicKey);
const receivedMsg = await receivedMsgPromise;
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
expect(receivedMsg.version).to.eq(1);
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
});
it('Js encrypts message for nim [asymmetric, no signature]', async function () {
this.timeout(5000);
const keyPair = await nimWaku.getAsymmetricKeyPair();
const privateKey = hexToBuf(keyPair.privateKey);
const publicKey = hexToBuf(keyPair.publicKey);
const messageText = 'This is a message I am going to encrypt';
const message = await WakuMessage.fromUtf8String(messageText, {
encPublicKey: publicKey,
});
await waku.relay.send(message);
let msgs: WakuRelayMessage[] = [];
while (msgs.length === 0) {
await delay(200); await delay(200);
msgs = await nimWaku.getAsymmetricMessages(privateKey);
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic); const messageText = 'Here is an encrypted message.';
expect(hexToBuf(msgs[0].payload).toString('utf-8')).to.equal(messageText); const message: WakuRelayMessage = {
contentTopic: DefaultContentTopic,
payload: Buffer.from(messageText, 'utf-8').toString('hex'),
};
const privateKey = generatePrivateKey();
waku.relay.addDecryptionPrivateKey(privateKey);
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
(resolve) => {
waku.relay.addObserver(resolve);
}
);
const publicKey = getPublicKey(privateKey);
dbg('Post message');
await nimWaku.postAsymmetricMessage(message, publicKey);
const receivedMsg = await receivedMsgPromise;
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
expect(receivedMsg.version).to.eq(1);
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
});
it('Js encrypts message for nim [asymmetric, no signature]', async function () {
this.timeout(5000);
const keyPair = await nimWaku.getAsymmetricKeyPair();
const privateKey = hexToBuf(keyPair.privateKey);
const publicKey = hexToBuf(keyPair.publicKey);
const messageText = 'This is a message I am going to encrypt';
const message = await WakuMessage.fromUtf8String(messageText, {
encPublicKey: publicKey,
});
await waku.relay.send(message);
let msgs: WakuRelayMessage[] = [];
while (msgs.length === 0) {
await delay(200);
msgs = await nimWaku.getAsymmetricMessages(privateKey);
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
expect(hexToBuf(msgs[0].payload).toString('utf-8')).to.equal(messageText);
});
}); });
}); });