Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / pass / lib / cache / crypto.spec.ts
blobb2c57e25582c771847a83b843a69e2a9910bfc8e
1 import { ARGON2_PARAMS, CryptoProxy } from '@proton/crypto';
2 import { decryptData, generateKey, importSymmetricKey } from '@proton/pass/lib/crypto/utils/crypto-helpers';
3 import { releaseCryptoProxy, setupCryptoProxyForTesting } from '@proton/pass/lib/crypto/utils/testing';
4 import { PassEncryptionTag } from '@proton/pass/types';
5 import { stringToUint8Array, uint8ArrayToString } from '@proton/shared/lib/helpers/encoding';
7 import { getOfflineComponents } from './crypto';
9 describe('cache crypto operations', () => {
10     beforeAll(() => setupCryptoProxyForTesting());
11     afterAll(() => releaseCryptoProxy());
13     describe('getOfflineComponents', () => {
14         test('Should compute offline components correctly for a given password', async () => {
15             /** mock argon2 to avoid jest keeping a dangling worker alive */
16             const argon2 = jest.spyOn(CryptoProxy, 'computeArgon2').mockImplementation(async () => generateKey());
18             const randomPassword = uint8ArrayToString(generateKey());
19             const components = await getOfflineComponents(randomPassword);
21             expect(components.offlineConfig.salt).toBeDefined();
22             expect(components.offlineConfig.params).toStrictEqual(ARGON2_PARAMS.RECOMMENDED);
23             expect(components.offlineKD).toBeDefined();
24             expect(components.offlineVerifier).toBeDefined();
26             const offlineKey = await importSymmetricKey(stringToUint8Array(components.offlineKD));
27             const verifier = stringToUint8Array(components.offlineVerifier);
28             await expect((() => decryptData(offlineKey, verifier, PassEncryptionTag.Offline))()).resolves.toBeDefined();
30             argon2.mockRestore();
31         });
32     });
33 });