unit tests
This commit is contained in:
		
							
								
								
									
										1
									
								
								babel.config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								babel.config.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | module.exports = {presets: ['@babel/preset-env']} | ||||||
							
								
								
									
										6
									
								
								jest.config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								jest.config.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | export default { | ||||||
|  | transform: { | ||||||
|  |     '^.+\\.js$': 'babel-jest', | ||||||
|  |   }, | ||||||
|  |   testEnvironment: 'node', | ||||||
|  | }; | ||||||
| @@ -4,7 +4,7 @@ | |||||||
|   "description": "", |   "description": "", | ||||||
|   "main": "app.js", |   "main": "app.js", | ||||||
|   "scripts": { |   "scripts": { | ||||||
|     "test": "echo \"Error: no test specified\" && exit 1" |     "test": "jest" | ||||||
|   }, |   }, | ||||||
|   "author": "Sam Hadow", |   "author": "Sam Hadow", | ||||||
|   "license": "BSD-3-Clause-Attribution", |   "license": "BSD-3-Clause-Attribution", | ||||||
| @@ -16,5 +16,9 @@ | |||||||
|     "express-session": "^1.18.1", |     "express-session": "^1.18.1", | ||||||
|     "pg": "^8.13.1", |     "pg": "^8.13.1", | ||||||
|     "socket.io": "^4.8.1" |     "socket.io": "^4.8.1" | ||||||
|  |   }, | ||||||
|  |   "devDependencies": { | ||||||
|  |     "@babel/preset-env": "^7.26.8", | ||||||
|  |     "jest": "^29.7.0" | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
| Convert  an ArrayBuffer into a string | Convert  an ArrayBuffer into a string | ||||||
| from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-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)); |     return String.fromCharCode.apply(null, new Uint8Array(buf)); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -10,7 +10,7 @@ function ab2str(buf) { | |||||||
| Convert a string into an ArrayBuffer | Convert a string into an ArrayBuffer | ||||||
| from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String | 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 buf = new ArrayBuffer(str.length); | ||||||
|     const bufView = new Uint8Array(buf); |     const bufView = new Uint8Array(buf); | ||||||
|     for (let i = 0, strLen = str.length; i < strLen; i++) { |     for (let i = 0, strLen = str.length; i < strLen; i++) { | ||||||
| @@ -19,13 +19,13 @@ function str2ab(str) { | |||||||
|     return buf; |     return buf; | ||||||
| } | } | ||||||
|  |  | ||||||
| function exportedKeyToPem(key, type) { | export function exportedKeyToPem(key, type) { | ||||||
|     let exportedAsString = ab2str(key); |     let exportedAsString = ab2str(key); | ||||||
|     let exportedAsBase64 = window.btoa(exportedAsString); |     let exportedAsBase64 = window.btoa(exportedAsString); | ||||||
|     return `-----BEGIN ${type.toUpperCase()} KEY-----\n${exportedAsBase64}\n-----END ${type.toUpperCase()} KEY-----`; |     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 base64 = pemKey.replace(`-----BEGIN ${type.toUpperCase()} KEY-----`, '').replace(`-----END ${type.toUpperCase()} KEY-----`, '').trim(); | ||||||
|     const binaryDerString = window.atob(base64); |     const binaryDerString = window.atob(base64); | ||||||
|     const binaryDer = str2ab(binaryDerString); |     const binaryDer = str2ab(binaryDerString); | ||||||
| @@ -41,7 +41,7 @@ function pemToKey(pemKey, type) { | |||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| async function genKey() { | export async function genKey() { | ||||||
|   // Generate keys |   // Generate keys | ||||||
|     const { publicKey, privateKey } = await crypto.subtle.generateKey( |     const { publicKey, privateKey } = await crypto.subtle.generateKey( | ||||||
|       { |       { | ||||||
|   | |||||||
| @@ -7,8 +7,8 @@ | |||||||
|     <link rel="stylesheet" href="/style.css"> |     <link rel="stylesheet" href="/style.css"> | ||||||
|     <script src="/socket.io/socket.io.js" defer></script> |     <script src="/socket.io/socket.io.js" defer></script> | ||||||
|     <script src="/script.js" defer></script> |     <script src="/script.js" defer></script> | ||||||
|     <script src="/ecc.js" defer></script> |     <script type="module" src="/ecc.js" defer></script> | ||||||
|     <script src="/popups.js" defer></script> |     <script type="module" src="/popups.js" defer></script> | ||||||
|     <!--load bootstrap--> |     <!--load bootstrap--> | ||||||
|     <link rel="stylesheet" href="/css/bootstrap.min.css" /> |     <link rel="stylesheet" href="/css/bootstrap.min.css" /> | ||||||
|   </head> |   </head> | ||||||
|   | |||||||
| @@ -1,4 +1,6 @@ | |||||||
| const currentUrl = window.location.href; | const currentUrl = window.location.href; | ||||||
|  | import { ab2str, exportedKeyToPem, pemToKey, genKey } from "./ecc.js"; | ||||||
|  |  | ||||||
|  |  | ||||||
| // close popups with escape key | // close popups with escape key | ||||||
| document.addEventListener("keydown", (event) => { | document.addEventListener("keydown", (event) => { | ||||||
| @@ -78,10 +80,10 @@ document.getElementById("loginconfirm").addEventListener("click", async function | |||||||
|     const { challenge } = await response.json(); |     const { challenge } = await response.json(); | ||||||
|     console.log("Received challenge:", challenge); |     console.log("Received challenge:", challenge); | ||||||
|  |  | ||||||
|     privKey = await pemToKey(inputFieldPrivateKey.value, "private"); |     let privKey = await pemToKey(inputFieldPrivateKey.value, "private"); | ||||||
|  |  | ||||||
|     const encoder = new TextEncoder(); |     const encoder = new TextEncoder(); | ||||||
|     encodedData = encoder.encode(challenge); |     let encodedData = encoder.encode(challenge); | ||||||
|  |  | ||||||
|     // Sign the data using the private key. |     // Sign the data using the private key. | ||||||
|     const signature = await crypto.subtle.sign( |     const signature = await crypto.subtle.sign( | ||||||
|   | |||||||
							
								
								
									
										23
									
								
								tests/ecc.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								tests/ecc.test.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
		Reference in New Issue
	
	Block a user