From cc1767e0d083156c0207370e0adbf672aa47ec68 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Wed, 12 Feb 2025 15:38:32 +0100 Subject: [PATCH] unit tests verify signature --- Makefile | 5 ++--- src/public/ecc.js | 1 - tests/authentication.test.js | 37 ++++++++++++++++++++++++++++++++++++ tests/ecc.test.js | 36 +++++++++++++++++------------------ 4 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 tests/authentication.test.js diff --git a/Makefile b/Makefile index 88feac3..12a05c1 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,11 @@ -run: clean build +run: test clean build podman pod create --name=e2ee -p 3333:3333 podman run -d --pod=e2ee -e POSTGRES_PASSWORD="password" -e POSTGRES_DB="e2ee" -e POSTGRES_USER="e2ee" -e POSTGRES_INITDB_ARGS="--encoding=UTF-8 --lc-collate=C --lc-ctype=C" --name=e2ee-db docker.io/library/postgres:15 podman run -d --pod=e2ee -e POSTGRES_PASSWORD="password" -e POSTGRES_DB="e2ee" -e POSTGRES_USER="e2ee" -e SHARED_SECRET="toto" --name=e2ee-app e2ee-messaging-service:latest build: podman build -t e2ee-messaging-service . test: - curl http://localhost:3333 - podman logs e2ee + npm test clean: podman pod rm -f e2ee diff --git a/src/public/ecc.js b/src/public/ecc.js index 20bc31d..b2bd6d6 100644 --- a/src/public/ecc.js +++ b/src/public/ecc.js @@ -40,7 +40,6 @@ export function pemToKey(pemKey, type) { ); } - export async function genKey() { // Generate keys const { publicKey, privateKey } = await crypto.subtle.generateKey( diff --git a/tests/authentication.test.js b/tests/authentication.test.js new file mode 100644 index 0000000..1cf1cd7 --- /dev/null +++ b/tests/authentication.test.js @@ -0,0 +1,37 @@ +const { subtle } = require('node:crypto').webcrypto; +const authentication = require('../src/authentication'); + +describe('authentication module', () => { + describe('verifySignature', () => { + it('should return the public key if the signature is verified successfully', async () => { + const { publicKey, privateKey } = await crypto.subtle.generateKey( + { + name: "Ed25519", + }, + true, + ["sign", "verify"], + ); + const exportedPubkey = await crypto.subtle.exportKey("spki", publicKey); + let exportedAsString = String.fromCharCode.apply(null, new Uint8Array(exportedPubkey)); + let exportedAsBase64 = Buffer.from(exportedAsString, 'binary').toString('base64'); + let pemKey = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64}\n-----END PUBLIC KEY-----`; + const publicKeys = [pemKey,]; + const msg = 'test message'; + const encoder = new TextEncoder(); + const encodedData = encoder.encode(msg); + const signature = await crypto.subtle.sign( + { + name: "Ed25519", + }, + privateKey, + encodedData, + ); + const result = await authentication.verifySignature(encodedData, signature, publicKeys); + expect(result).toBe(pemKey); + + let fakeKey = `-----BEGIN PUBLIC KEY-----MCowBQYDK2VwAyEAmrBLT6lyiFh/eUticsIFNY6AkjXuQPqj0Qvb99pCJJk=-----END PUBLIC KEY-----` + const result2 = await authentication.verifySignature(encodedData, signature, [fakeKey,]); + expect(result2).toBe(null); + }); + }); +}); diff --git a/tests/ecc.test.js b/tests/ecc.test.js index 3fe04c4..7072dc4 100644 --- a/tests/ecc.test.js +++ b/tests/ecc.test.js @@ -1,23 +1,23 @@ -import { ab2str, str2ab } from '../src/public/ecc.js'; +import { ab2str, str2ab, exportedKeyToPem, pemToKey, genKey } from '../src/public/ecc.js'; describe('ecc.js functions', () => { - // Test ab2str - it('should convert ArrayBuffer to string', () => { - const buffer = new ArrayBuffer(5); - const view = new Uint8Array(buffer); - view.set([72, 101, 108, 108, 111]); // ASCII values for "Hello" - const result = ab2str(buffer); - expect(result).toBe("Hello"); - }); + // Test ab2str + it('should convert ArrayBuffer to string', () => { + const buffer = new ArrayBuffer(5); + const view = new Uint8Array(buffer); + view.set([72, 101, 108, 108, 111]); // ASCII values for "Hello" + const result = ab2str(buffer); + expect(result).toBe("Hello"); + }); - // Test str2ab - it('should convert string to ArrayBuffer', () => { - const str = "Hello"; - const result = str2ab(str); - const expectedBuffer = new ArrayBuffer(5); - const expectedView = new Uint8Array(expectedBuffer); - expectedView.set([72, 101, 108, 108, 111]); - expect(result).toEqual(expectedBuffer); - }); + // Test str2ab + it('should convert string to ArrayBuffer', () => { + const str = "Hello"; + const result = str2ab(str); + const expectedBuffer = new ArrayBuffer(5); + const expectedView = new Uint8Array(expectedBuffer); + expectedView.set([72, 101, 108, 108, 111]); + expect(result).toEqual(expectedBuffer); + }); });