import * as Crypto from '../Crypto'; import ExpoCrypto from '../ExpoCrypto'; jest.mock('../ExpoCrypto', () => ({ getRandomBytesAsync: jest.fn(async () => 0), getRandomBase64StringAsync: jest.fn(async () => 0), digestStringAsync: jest.fn(async () => 0), digestString: jest.fn(async () => 0), })); jest.mock('base64-js', () => ({ toByteArray: jest.fn(() => {}) })); it(`asserts invalid algorithm errors`, async () => { await expect(Crypto.digestStringAsync(null as any, '')).rejects.toThrowError(TypeError); await expect(Crypto.digestStringAsync('null' as any, '')).rejects.toThrowError(TypeError); await expect(Crypto.digestStringAsync(2 as any, '')).rejects.toThrowError(TypeError); await expect(Crypto.digestStringAsync(true as any, '')).rejects.toThrowError(TypeError); await expect(Crypto.digestStringAsync(undefined as any, '')).rejects.toThrowError( TypeError ); await expect(Crypto.digestStringAsync({} as any, '')).rejects.toThrowError(TypeError); }); it(`asserts invalid data errors`, async () => { await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, null as any) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, 2 as any) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, true as any) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, undefined as any) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, {} as any) ).rejects.toThrowError(TypeError); }); it(`asserts invalid encoding errors`, async () => { await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, '', { encoding: null as any, }) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, '', { encoding: '' as any }) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, '', { encoding: 2 as any }) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, '', { encoding: true as any, }) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, '', { encoding: undefined as any, }) ).rejects.toThrowError(TypeError); await expect( Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA1, '', { encoding: {} as any }) ).rejects.toThrowError(TypeError); }); it(`accepts valid byte counts`, async () => { for (const value of [0, 1024, 512.5]) { await expect(Crypto.getRandomBytesAsync(value)); expect(ExpoCrypto.getRandomBytesAsync).lastCalledWith(Math.floor(value)); } }); it(`falls back to an alternative native method when getRandomBytesAsync is not available`, async () => { ExpoCrypto.getRandomBytesAsync = null; await expect(Crypto.getRandomBytesAsync(1024)); expect(ExpoCrypto.getRandomBase64StringAsync).toHaveBeenCalled(); }); it(`asserts invalid byte count errors`, async () => { await expect(Crypto.getRandomBytesAsync(-1)).rejects.toThrowError(TypeError); await expect(Crypto.getRandomBytesAsync(1025)).rejects.toThrowError(TypeError); await expect(Crypto.getRandomBytesAsync('invalid' as any)).rejects.toThrowError(TypeError); await expect(Crypto.getRandomBytesAsync(null as any)).rejects.toThrowError(TypeError); await expect(Crypto.getRandomBytesAsync({} as any)).rejects.toThrowError(TypeError); await expect(Crypto.getRandomBytesAsync(NaN)).rejects.toThrowError(TypeError); });