unit tests verify signature
This commit is contained in:
		
							
								
								
									
										5
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								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 | ||||
|   | ||||
| @@ -40,7 +40,6 @@ export function pemToKey(pemKey, type) { | ||||
|     ); | ||||
| } | ||||
|  | ||||
|  | ||||
| export async function genKey() { | ||||
|   // Generate keys | ||||
|     const { publicKey, privateKey } = await crypto.subtle.generateKey( | ||||
|   | ||||
							
								
								
									
										37
									
								
								tests/authentication.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								tests/authentication.test.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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); | ||||
|         }); | ||||
|     }); | ||||
| }); | ||||
| @@ -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); | ||||
|     }); | ||||
| }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user