-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add encryption module unit tests
- Loading branch information
1 parent
1fbe5ef
commit 472e382
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/dapp-toolkit/src/modules/wallet-request/encryption/encryption.module.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
}) |