From ac4f0aab98ac05b541a9e7edf8302a10d69e5042 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 14 Jul 2021 13:03:57 +1000 Subject: [PATCH] Only include lib files, segregate browser tests --- karma.conf.js | 9 ++++- src/lib/waku.browser.spec.ts | 29 --------------- src/tests/browser/version_1.spec.ts | 57 +++++++++++++++++++++++++++++ tsconfig.karma.json | 5 +++ 4 files changed, 70 insertions(+), 30 deletions(-) delete mode 100644 src/lib/waku.browser.spec.ts create mode 100644 src/tests/browser/version_1.spec.ts diff --git a/karma.conf.js b/karma.conf.js index 9441fd5f4a..ddee5ead6d 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,7 +1,11 @@ module.exports = function (config) { config.set({ frameworks: ['mocha', 'karma-typescript'], - files: [{ pattern: 'src/**/*browser.spec.ts' }], + files: [ + 'src/lib/**/*.ts', + 'src/proto/**/*.ts', + 'src/tests/browser/*.spec.ts', + ], preprocessors: { '**/*.ts': ['karma-typescript'], }, @@ -15,6 +19,9 @@ module.exports = function (config) { browsers: ['Chromium'], singleRun: true, karmaTypescriptConfig: { + bundlerOptions: { + entrypoints: /src\/tests\/browser\/.*\.spec\.ts$/, + }, tsconfig: './tsconfig.karma.json', coverageOptions: { instrumentation: false, diff --git a/src/lib/waku.browser.spec.ts b/src/lib/waku.browser.spec.ts deleted file mode 100644 index c27b68474b..0000000000 --- a/src/lib/waku.browser.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { expect } from 'chai'; - -import { NOISE_KEY_1, NOISE_KEY_2 } from '../test_utils/'; - -import { Waku } from './waku'; - -describe('Waku Dial', function () { - it('js connects to js', async function () { - this.timeout(10_000); - const [waku1, waku2] = await Promise.all([ - Waku.create({ - staticNoiseKey: NOISE_KEY_1, - libp2p: { addresses: { listen: ['/ip4/0.0.0.0/tcp/0/ws'] } }, - }), - Waku.create({ staticNoiseKey: NOISE_KEY_2 }), - ]); - const waku1MultiAddrWithId = waku1.getLocalMultiaddrWithID(); - - await waku2.dial(waku1MultiAddrWithId); - - const waku2PeerId = waku2.libp2p.peerId; - - const waku1Peers = waku1.libp2p.peerStore.peers; - - expect(waku1Peers.has(waku2PeerId.toB58String())).to.be.true; - - await Promise.all([waku1.stop(), waku2.stop()]); - }); -}); diff --git a/src/tests/browser/version_1.spec.ts b/src/tests/browser/version_1.spec.ts new file mode 100644 index 0000000000..fd5348e87e --- /dev/null +++ b/src/tests/browser/version_1.spec.ts @@ -0,0 +1,57 @@ +import { expect } from 'chai'; +import fc from 'fast-check'; + +import { + clearDecode, + clearEncode, + decryptAsymmetric, + encryptAsymmetric, + getPublicKey, +} from '../../lib/waku_message/version_1'; + +describe('Waku Message Version 1', function () { + it('Sign & Recover', function () { + fc.assert( + fc.property( + fc.uint8Array(), + fc.uint8Array({ minLength: 32, maxLength: 32 }), + (message, privKey) => { + const enc = clearEncode(message, privKey); + const res = clearDecode(enc.payload); + + const pubKey = getPublicKey(privKey); + + expect(res?.payload).deep.equal( + message, + 'Payload was not encrypted then decrypted correctly' + ); + expect(res?.sig?.publicKey).deep.equal( + pubKey, + 'signature Public key was not recovered from encrypted then decrypted signature' + ); + expect(enc?.sig?.publicKey).deep.equal( + pubKey, + 'Incorrect signature public key was returned when signing the payload' + ); + } + ) + ); + }); + + it('Asymmetric encrypt & Decrypt', async function () { + await fc.assert( + fc.asyncProperty( + fc.uint8Array({ minLength: 1 }), + fc.uint8Array({ minLength: 32, maxLength: 32 }), + async (message, privKey) => { + const publicKey = getPublicKey(privKey); + + const enc = await encryptAsymmetric(message, publicKey); + const res = await decryptAsymmetric(enc, privKey); + + expect(res).deep.equal(message); + } + ) + ); + }); +}); diff --git a/tsconfig.karma.json b/tsconfig.karma.json index c7b7d3028b..f3eb49d1dd 100644 --- a/tsconfig.karma.json +++ b/tsconfig.karma.json @@ -3,5 +3,10 @@ "compilerOptions": { "noEmit": false }, + "include": [ + "src/lib/**/*.ts", + "src/proto/**/*.ts", + "src/tests/browser/**/*.ts" + ], "exclude": ["node_modules/**"] }