Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / lib / cache / crypto.spec.ts
blob47ac1e903aa13cd4d026861d99b9488625604d75
1 import { ARGON2_PARAMS } from 'pmcrypto-v6-canary/lib/constants';
3 import { CryptoProxy } from '@proton/crypto/lib';
4 import { decryptData, generateKey, importSymmetricKey } from '@proton/pass/lib/crypto/utils/crypto-helpers';
5 import { releaseCryptoProxy, setupCryptoProxyForTesting } from '@proton/pass/lib/crypto/utils/testing';
6 import { PassEncryptionTag } from '@proton/pass/types';
7 import { stringToUint8Array, uint8ArrayToString } from '@proton/shared/lib/helpers/encoding';
9 import { getOfflineComponents } from './crypto';
11 describe('cache crypto operations', () => {
12     beforeAll(() => setupCryptoProxyForTesting());
13     afterAll(() => releaseCryptoProxy());
15     describe('getOfflineComponents', () => {
16         test('Should compute offline components correctly for a given password', async () => {
17             /** mock argon2 to avoid jest keeping a dangling worker alive */
18             const argon2 = jest.spyOn(CryptoProxy, 'computeArgon2').mockImplementation(async () => generateKey());
20             const randomPassword = uint8ArrayToString(generateKey());
21             const components = await getOfflineComponents(randomPassword);
23             expect(components.offlineConfig.salt).toBeDefined();
24             expect(components.offlineConfig.params).toStrictEqual(ARGON2_PARAMS.RECOMMENDED);
25             expect(components.offlineKD).toBeDefined();
26             expect(components.offlineVerifier).toBeDefined();
28             const offlineKey = await importSymmetricKey(stringToUint8Array(components.offlineKD));
29             const verifier = stringToUint8Array(components.offlineVerifier);
30             await expect((() => decryptData(offlineKey, verifier, PassEncryptionTag.Offline))()).resolves.toBeDefined();
32             argon2.mockRestore();
33         });
34     });
35 });