From 5542d7b8dbe2b916f9cea06580170d54fd4af9ac Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 16 Dec 2021 11:07:03 -0400 Subject: [PATCH] Use top level describe() --- utils/ECC/ECC.spec.js | 160 ++++++++++++++++++++------------------- utils/ECC/crypto.spec.js | 40 +++++----- 2 files changed, 102 insertions(+), 98 deletions(-) diff --git a/utils/ECC/ECC.spec.js b/utils/ECC/ECC.spec.js index a6a7a71b..e088844a 100644 --- a/utils/ECC/ECC.spec.js +++ b/utils/ECC/ECC.spec.js @@ -24,96 +24,98 @@ const storageDirectory = Path.resolve(__dirname, `./.test-storage`) console.log(`Storage directory: ${storageDirectory}`) -describe('generateKeyPair()', () => { - it('generates a keypair', async () => { - expect.hasAssertions() - const pair = await generateKeyPair(uuid()) +describe('ECC', () => { + describe('generateKeyPair()', () => { + it('generates a keypair', async () => { + expect.hasAssertions() + const pair = await generateKeyPair(uuid()) - expect(pair.privateKey).toBeInstanceOf(Buffer) - expect(typeof pair.privateKeyBase64 === 'string').toBeTruthy() - expect(pair.publicKey).toBeInstanceOf(Buffer) - expect(typeof pair.publicKeyBase64 === 'string').toBeTruthy() - }) - it('returns the same pair for the same device', async () => { - expect.hasAssertions() - const id = uuid() - const pair = await generateKeyPair(id) - const pairAgain = await generateKeyPair(id) - - expect(pairAgain).toStrictEqual(pair) - }) -}) - -describe('authorizeDevice()/isAuthorizedDevice()', () => { - it('authorizes a device given its ID', async () => { - expect.hasAssertions() - await Storage.init({ - dir: storageDirectory + expect(pair.privateKey).toBeInstanceOf(Buffer) + expect(typeof pair.privateKeyBase64 === 'string').toBeTruthy() + expect(pair.publicKey).toBeInstanceOf(Buffer) + expect(typeof pair.publicKeyBase64 === 'string').toBeTruthy() }) - const deviceId = uuid() - const pair = await generateKeyPair(deviceId) - await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 }) - expect(isAuthorizedDevice({ deviceId })).toBeTruthy() - }) -}) + it('returns the same pair for the same device', async () => { + expect.hasAssertions() + const id = uuid() + const pair = await generateKeyPair(id) + const pairAgain = await generateKeyPair(id) -describe('encryptMessage()/decryptMessage()', () => { - before(() => - Storage.init({ - dir: storageDirectory + expect(pairAgain).toStrictEqual(pair) }) - ) - it('throws if provided with an unauthorized device id when encrypting', async () => { - expect.hasAssertions() - const deviceId = uuid() + }) - try { - await encryptMessage({ - message: uuid(), - deviceId + describe('authorizeDevice()/isAuthorizedDevice()', () => { + it('authorizes a device given its ID', async () => { + expect.hasAssertions() + await Storage.init({ + dir: storageDirectory }) - throw new Error('encryptMessage() did not throw') - } catch (_) { - expect(true).toBeTruthy() - } + const deviceId = uuid() + const pair = await generateKeyPair(deviceId) + await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 }) + expect(isAuthorizedDevice({ deviceId })).toBeTruthy() + }) }) - it('throws if provided with an unknown device id when decrypting', async () => { - expect.hasAssertions() - const deviceId = uuid() - try { - await decryptMessage({ + describe('encryptMessage()/decryptMessage()', () => { + before(() => + Storage.init({ + dir: storageDirectory + }) + ) + it('throws if provided with an unauthorized device id when encrypting', async () => { + expect.hasAssertions() + const deviceId = uuid() + + try { + await encryptMessage({ + message: uuid(), + deviceId + }) + throw new Error('encryptMessage() did not throw') + } catch (_) { + expect(true).toBeTruthy() + } + }) + it('throws if provided with an unknown device id when decrypting', async () => { + expect.hasAssertions() + const deviceId = uuid() + + try { + await decryptMessage({ + deviceId, + encryptedMessage: { + ciphertext: uuid(), + ephemPublicKey: uuid(), + iv: uuid(), + mac: uuid(), + metadata: uuid() + } + }) + throw new Error('decryptMessage() did not throw') + } catch (_) { + expect(true).toBeTruthy() + } + }) + it('encrypts and decrypts messages when given a known device id', async () => { + expect.hasAssertions() + const deviceId = uuid() + + const pair = await generateKeyPair(deviceId) + + await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 }) + + const message = 'Bitcoin fixes this' + + const encryptedMessage = await encryptMessage({ deviceId, message }) + + const decrypted = await decryptMessage({ deviceId, - encryptedMessage: { - ciphertext: uuid(), - ephemPublicKey: uuid(), - iv: uuid(), - mac: uuid(), - metadata: uuid() - } + encryptedMessage }) - throw new Error('decryptMessage() did not throw') - } catch (_) { - expect(true).toBeTruthy() - } - }) - it('encrypts and decrypts messages when given a known device id', async () => { - expect.hasAssertions() - const deviceId = uuid() - const pair = await generateKeyPair(deviceId) - - await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 }) - - const message = 'Bitcoin fixes this' - - const encryptedMessage = await encryptMessage({ deviceId, message }) - - const decrypted = await decryptMessage({ - deviceId, - encryptedMessage + expect(decrypted).toEqual(message) }) - - expect(decrypted).toEqual(message) }) }) diff --git a/utils/ECC/crypto.spec.js b/utils/ECC/crypto.spec.js index a848b65b..d367006d 100644 --- a/utils/ECC/crypto.spec.js +++ b/utils/ECC/crypto.spec.js @@ -10,25 +10,27 @@ const { convertBufferToBase64 } = require('./crypto') -describe('generateRandomString()', () => { - it('creates a random string of the specified length', async () => { - expect.hasAssertions() - const base = Math.ceil(Math.random() * 100) - const len = base % 2 !== 0 ? base + 1 : base - const result = await generateRandomString(len) +describe('crypto', () => { + describe('generateRandomString()', () => { + it('creates a random string of the specified length', async () => { + expect.hasAssertions() + const base = Math.ceil(Math.random() * 100) + const len = base % 2 !== 0 ? base + 1 : base + const result = await generateRandomString(len) - expect(result.length).toEqual(len) - }) -}) - -describe('Buffer <> String <> Buffer', () => { - it('preserves values', async () => { - const rnd = await generateRandomString(24) - - const asBuffer = convertBase64ToBuffer(rnd) - - const asStringAgain = convertBufferToBase64(asBuffer) - - expect(asStringAgain).toEqual(rnd) + expect(result.length).toEqual(len) + }) + }) + + describe('Buffer <> String <> Buffer', () => { + it('preserves values', async () => { + const rnd = await generateRandomString(24) + + const asBuffer = convertBase64ToBuffer(rnd) + + const asStringAgain = convertBufferToBase64(asBuffer) + + expect(asStringAgain).toEqual(rnd) + }) }) })