From e37bec02c278153bb2533fdf35daec64325cd2fc Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Wed, 12 Feb 2025 10:48:47 +0100 Subject: [PATCH] unit tests --- babel.config.js | 1 + jest.config.js | 6 ++++++ package.json | 6 +++++- src/public/ecc.js | 10 +++++----- src/public/index.html | 4 ++-- src/public/popups.js | 6 ++++-- tests/ecc.test.js | 23 +++++++++++++++++++++++ 7 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 babel.config.js create mode 100644 jest.config.js create mode 100644 tests/ecc.test.js diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 0000000..9ea84ed --- /dev/null +++ b/babel.config.js @@ -0,0 +1 @@ +module.exports = {presets: ['@babel/preset-env']} diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..34e4834 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,6 @@ +export default { +transform: { + '^.+\\.js$': 'babel-jest', + }, + testEnvironment: 'node', +}; diff --git a/package.json b/package.json index 7bb3735..c4ca3e3 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "app.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "jest" }, "author": "Sam Hadow", "license": "BSD-3-Clause-Attribution", @@ -16,5 +16,9 @@ "express-session": "^1.18.1", "pg": "^8.13.1", "socket.io": "^4.8.1" + }, + "devDependencies": { + "@babel/preset-env": "^7.26.8", + "jest": "^29.7.0" } } diff --git a/src/public/ecc.js b/src/public/ecc.js index 0016c70..20bc31d 100644 --- a/src/public/ecc.js +++ b/src/public/ecc.js @@ -2,7 +2,7 @@ Convert an ArrayBuffer into a string from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String */ -function ab2str(buf) { +export function ab2str(buf) { return String.fromCharCode.apply(null, new Uint8Array(buf)); } @@ -10,7 +10,7 @@ function ab2str(buf) { Convert a string into an ArrayBuffer from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String */ -function str2ab(str) { +export function str2ab(str) { const buf = new ArrayBuffer(str.length); const bufView = new Uint8Array(buf); for (let i = 0, strLen = str.length; i < strLen; i++) { @@ -19,13 +19,13 @@ function str2ab(str) { return buf; } -function exportedKeyToPem(key, type) { +export function exportedKeyToPem(key, type) { let exportedAsString = ab2str(key); let exportedAsBase64 = window.btoa(exportedAsString); return `-----BEGIN ${type.toUpperCase()} KEY-----\n${exportedAsBase64}\n-----END ${type.toUpperCase()} KEY-----`; } -function pemToKey(pemKey, type) { +export function pemToKey(pemKey, type) { const base64 = pemKey.replace(`-----BEGIN ${type.toUpperCase()} KEY-----`, '').replace(`-----END ${type.toUpperCase()} KEY-----`, '').trim(); const binaryDerString = window.atob(base64); const binaryDer = str2ab(binaryDerString); @@ -41,7 +41,7 @@ function pemToKey(pemKey, type) { } -async function genKey() { +export async function genKey() { // Generate keys const { publicKey, privateKey } = await crypto.subtle.generateKey( { diff --git a/src/public/index.html b/src/public/index.html index a038878..97c46da 100644 --- a/src/public/index.html +++ b/src/public/index.html @@ -7,8 +7,8 @@ - - + + diff --git a/src/public/popups.js b/src/public/popups.js index 4b3860e..c52aea7 100644 --- a/src/public/popups.js +++ b/src/public/popups.js @@ -1,4 +1,6 @@ const currentUrl = window.location.href; +import { ab2str, exportedKeyToPem, pemToKey, genKey } from "./ecc.js"; + // close popups with escape key document.addEventListener("keydown", (event) => { @@ -78,10 +80,10 @@ document.getElementById("loginconfirm").addEventListener("click", async function const { challenge } = await response.json(); console.log("Received challenge:", challenge); - privKey = await pemToKey(inputFieldPrivateKey.value, "private"); + let privKey = await pemToKey(inputFieldPrivateKey.value, "private"); const encoder = new TextEncoder(); - encodedData = encoder.encode(challenge); + let encodedData = encoder.encode(challenge); // Sign the data using the private key. const signature = await crypto.subtle.sign( diff --git a/tests/ecc.test.js b/tests/ecc.test.js new file mode 100644 index 0000000..3fe04c4 --- /dev/null +++ b/tests/ecc.test.js @@ -0,0 +1,23 @@ +import { ab2str, str2ab } 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 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); + }); +});