From 472e382082e8bbab944fa621dca65825fde0bdad Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Fri, 22 Nov 2024 12:32:52 +0100 Subject: [PATCH] test: add encryption module unit tests --- .../encryption/encryption.module.spec.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.spec.ts diff --git a/packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.spec.ts b/packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.spec.ts new file mode 100644 index 00000000..e94e98af --- /dev/null +++ b/packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.spec.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from 'vitest' +import { EncryptionModule } from './encryption.module' + +describe('encryption module', () => { + const encryptionModule = EncryptionModule() + + it('should encrypt data', async () => { + const data = Buffer.from('data') + const encryptionKey = Buffer.from('12345678901234567890123456789012') + const result = await encryptionModule + .encrypt(data, encryptionKey, encryptionModule.createIV()) + .map((value) => { + expect(value).toBeDefined() + }) + + expect(result.isOk()).toBeTruthy() + }) + + it('should return error for invalid key length', async () => { + const data = Buffer.from('data') + const encryptionKey = Buffer.from('key') + const result = await encryptionModule.encrypt( + data, + encryptionKey, + encryptionModule.createIV(), + ) + expect(result.isErr()).toBeTruthy() + }) + + it('should return 12 bytes IV', () => { + const iv = encryptionModule.createIV() + expect(iv.length).toBe(12) + }) + + it('should decrypt previously encrypted data', async () => { + const data = Buffer.from('data') + const encryptionKey = Buffer.from('12345678901234567890123456789012') + const iv = encryptionModule.createIV() + const result = await encryptionModule + .encrypt(data, encryptionKey, iv) + .andThen((sealed) => + encryptionModule.decrypt(sealed.ciphertext, encryptionKey, iv), + ) + .map((decrypted) => + expect(decrypted.toString('hex')).toEqual(data.toString('hex')), + ) + expect(result.isOk()).toBeTruthy() + }) +})