Merge pull request #304 from status-im/run-test-on-browser-by-default

This commit is contained in:
Franck Royer 2021-09-24 17:32:14 +10:00 committed by GitHub
commit a056227538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 284 additions and 282 deletions

View File

@ -17,7 +17,7 @@ module.exports = function (config) {
singleRun: true, singleRun: true,
karmaTypescriptConfig: { karmaTypescriptConfig: {
bundlerOptions: { bundlerOptions: {
entrypoints: /.*\.browser\.spec\.ts$/, entrypoints: /^.*[^(node)]\.spec\.ts$/,
}, },
coverageOptions: { coverageOptions: {
instrumentation: false, instrumentation: false,

View File

@ -17,7 +17,7 @@ import { generateSymmetricKey } from './waku_message/version_1';
const TestContentTopic = '/test/1/waku/utf8'; const TestContentTopic = '/test/1/waku/utf8';
describe('Waku Dial', function () { describe('Waku Dial [node only]', function () {
let waku: Waku; let waku: Waku;
let waku2: Waku; let waku2: Waku;
let nimWaku: NimWaku; let nimWaku: NimWaku;

View File

@ -10,7 +10,7 @@ import { WakuMessage } from '../waku_message';
const TestContentTopic = '/test/1/waku-light-push/utf8'; const TestContentTopic = '/test/1/waku-light-push/utf8';
describe('Waku Light Push', () => { describe('Waku Light Push [node only]', () => {
let waku: Waku; let waku: Waku;
let nimWaku: NimWaku; let nimWaku: NimWaku;

View File

@ -1,134 +0,0 @@
import { expect } from 'chai';
import fc from 'fast-check';
import { getPublicKey } from './version_1';
import { WakuMessage } from './index';
const TestContentTopic = '/test/1/waku-message/utf8';
describe('Waku Message: Browser & Node', function () {
it('Waku message round trip binary serialization [clear]', async function () {
await fc.assert(
fc.asyncProperty(fc.string(), async (s) => {
const msg = await WakuMessage.fromUtf8String(s, TestContentTopic);
const binary = msg.encode();
const actual = await WakuMessage.decode(binary);
expect(actual).to.deep.equal(msg);
})
);
});
it('Payload to utf-8', async function () {
await fc.assert(
fc.asyncProperty(fc.string(), async (s) => {
const msg = await WakuMessage.fromUtf8String(s, TestContentTopic);
const utf8 = msg.payloadAsUtf8;
return utf8 === s;
})
);
});
it('Waku message round trip binary encryption [asymmetric, no signature]', async function () {
await fc.assert(
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, TestContentTopic, {
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, TestContentTopic, {
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, TestContentTopic, {
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, TestContentTopic, {
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);
}
)
);
});
it('Waku message round trip utf-8 including emojis', async function () {
const messageText = '😁🤣🥧🤦👩‍🎓';
const wakuMessage = await WakuMessage.fromUtf8String(
messageText,
TestContentTopic
);
const decodedText = wakuMessage.payloadAsUtf8;
expect(decodedText).to.eq(messageText);
});
});

View File

@ -0,0 +1,177 @@
import { expect } from 'chai';
import debug from 'debug';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: No types available
import TCP from 'libp2p-tcp';
import {
makeLogFileName,
NimWaku,
NOISE_KEY_1,
WakuRelayMessage,
} from '../../test_utils';
import { delay } from '../delay';
import { hexToBuf } from '../utils';
import { Waku } from '../waku';
import {
generatePrivateKey,
generateSymmetricKey,
getPublicKey,
} from './version_1';
import { WakuMessage } from './index';
const dbg = debug('waku:test:message');
const TestContentTopic = '/test/1/waku-message/utf8';
describe('Waku Message [node only]', function () {
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();
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ staticnode: multiAddrWithId, rpcPrivate: true });
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: TestContentTopic,
payload: Buffer.from(messageText, 'utf-8').toString('hex'),
};
const privateKey = generatePrivateKey();
waku.relay.addDecryptionKey(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,
TestContentTopic,
{
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);
});
it('JS decrypts nim message [symmetric, no signature]', async function () {
this.timeout(10000);
await delay(200);
const messageText = 'Here is a message encrypted in a symmetric manner.';
const message: WakuRelayMessage = {
contentTopic: TestContentTopic,
payload: Buffer.from(messageText, 'utf-8').toString('hex'),
};
const symKey = generateSymmetricKey();
waku.relay.addDecryptionKey(symKey);
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
(resolve) => {
waku.relay.addObserver(resolve);
}
);
dbg('Post message');
await nimWaku.postSymmetricMessage(message, symKey);
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 [symmetric, no signature]', async function () {
this.timeout(5000);
const symKey = await nimWaku.getSymmetricKey();
const messageText =
'This is a message I am going to encrypt with a symmetric key';
const message = await WakuMessage.fromUtf8String(
messageText,
TestContentTopic,
{
symKey: symKey,
}
);
await waku.relay.send(message);
let msgs: WakuRelayMessage[] = [];
while (msgs.length === 0) {
await delay(200);
msgs = await nimWaku.getSymmetricMessages(symKey);
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
expect(hexToBuf(msgs[0].payload).toString('utf-8')).to.equal(messageText);
});
});
});

View File

@ -1,177 +1,134 @@
import { expect } from 'chai'; import { expect } from 'chai';
import debug from 'debug'; import fc from 'fast-check';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: No types available
import TCP from 'libp2p-tcp';
import { import { getPublicKey } from './version_1';
makeLogFileName,
NimWaku,
NOISE_KEY_1,
WakuRelayMessage,
} from '../../test_utils';
import { delay } from '../delay';
import { hexToBuf } from '../utils';
import { Waku } from '../waku';
import {
generatePrivateKey,
generateSymmetricKey,
getPublicKey,
} from './version_1';
import { WakuMessage } from './index'; import { WakuMessage } from './index';
const dbg = debug('waku:test:message');
const TestContentTopic = '/test/1/waku-message/utf8'; const TestContentTopic = '/test/1/waku-message/utf8';
describe('Waku Message: Node only', function () { describe('Waku Message: Browser & Node', function () {
describe('Interop: Nim', function () { it('Waku message round trip binary serialization [clear]', async function () {
let waku: Waku; await fc.assert(
let nimWaku: NimWaku; fc.asyncProperty(fc.string(), async (s) => {
const msg = await WakuMessage.fromUtf8String(s, TestContentTopic);
const binary = msg.encode();
const actual = await WakuMessage.decode(binary);
beforeEach(async function () { expect(actual).to.deep.equal(msg);
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();
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ staticnode: multiAddrWithId, rpcPrivate: true });
await new Promise((resolve) =>
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
); );
}); });
afterEach(async function () { it('Payload to utf-8', async function () {
nimWaku ? nimWaku.stop() : null; await fc.assert(
waku ? await waku.stop() : null; fc.asyncProperty(fc.string(), async (s) => {
}); const msg = await WakuMessage.fromUtf8String(s, TestContentTopic);
const utf8 = msg.payloadAsUtf8;
it('JS decrypts nim message [asymmetric, no signature]', async function () { return utf8 === s;
this.timeout(10000); })
await delay(200);
const messageText = 'Here is an encrypted message.';
const message: WakuRelayMessage = {
contentTopic: TestContentTopic,
payload: Buffer.from(messageText, 'utf-8').toString('hex'),
};
const privateKey = generatePrivateKey();
waku.relay.addDecryptionKey(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 () { it('Waku message round trip binary encryption [asymmetric, no signature]', async function () {
this.timeout(5000); await fc.assert(
fc.asyncProperty(
fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
async (payload, privKey) => {
const publicKey = getPublicKey(privKey);
const keyPair = await nimWaku.getAsymmetricKeyPair(); const msg = await WakuMessage.fromBytes(payload, TestContentTopic, {
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,
TestContentTopic,
{
encPublicKey: publicKey, 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);
}); });
it('JS decrypts nim message [symmetric, no signature]', async function () { const wireBytes = msg.encode();
this.timeout(10000); const actual = await WakuMessage.decode(wireBytes, [privKey]);
await delay(200);
const messageText = 'Here is a message encrypted in a symmetric manner.'; expect(actual?.payload).to.deep.equal(payload);
const message: WakuRelayMessage = {
contentTopic: TestContentTopic,
payload: Buffer.from(messageText, 'utf-8').toString('hex'),
};
const symKey = generateSymmetricKey();
waku.relay.addDecryptionKey(symKey);
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
(resolve) => {
waku.relay.addObserver(resolve);
} }
)
); );
dbg('Post message');
await nimWaku.postSymmetricMessage(message, symKey);
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 [symmetric, no signature]', async function () { it('Waku message round trip binary encryption [asymmetric, signature]', async function () {
this.timeout(5000); 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 symKey = await nimWaku.getSymmetricKey(); const msg = await WakuMessage.fromBytes(payload, TestContentTopic, {
encPublicKey: encPubKey,
sigPrivKey: sigPrivKey,
});
const messageText = const wireBytes = msg.encode();
'This is a message I am going to encrypt with a symmetric key'; const actual = await WakuMessage.decode(wireBytes, [encPrivKey]);
const message = await WakuMessage.fromUtf8String(
messageText, expect(actual?.payload).to.deep.equal(payload);
TestContentTopic, 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, TestContentTopic, {
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, TestContentTopic, {
symKey: symKey, 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);
} }
)
);
});
it('Waku message round trip utf-8 including emojis', async function () {
const messageText = '😁🤣🥧🤦👩‍🎓';
const wakuMessage = await WakuMessage.fromUtf8String(
messageText,
TestContentTopic
); );
await waku.relay.send(message); const decodedText = wakuMessage.payloadAsUtf8;
let msgs: WakuRelayMessage[] = []; expect(decodedText).to.eq(messageText);
while (msgs.length === 0) {
await delay(200);
msgs = await nimWaku.getSymmetricMessages(symKey);
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
expect(hexToBuf(msgs[0].payload).toString('utf-8')).to.equal(messageText);
});
}); });
}); });

View File

@ -18,8 +18,10 @@ const log = debug('waku:test');
const TestContentTopic = '/test/1/waku-relay/utf8'; const TestContentTopic = '/test/1/waku-relay/utf8';
describe('Waku Relay', () => { describe('Waku Relay [node only]', () => {
describe('js only', () => { // Node needed as we don't have a way to connect 2 js waku
// nodes in the browser yet
describe('2 js nodes', () => {
afterEach(function () { afterEach(function () {
if (this.currentTest?.state === 'failed') { if (this.currentTest?.state === 'failed') {
console.log(`Test failed, log file name is ${makeLogFileName(this)}`); console.log(`Test failed, log file name is ${makeLogFileName(this)}`);