Changeset View
Changeset View
Standalone View
Standalone View
web/database/utils/worker-crypto-utlis.test.js
Show All 30 Lines | describe('database encryption utils', () => { | ||||
let database; | let database; | ||||
let cryptoKey; | let cryptoKey; | ||||
beforeAll(async () => { | beforeAll(async () => { | ||||
const SQL = await initSqlJs(); | const SQL = await initSqlJs(); | ||||
database = new SQL.Database(); | database = new SQL.Database(); | ||||
setUpMockDb(database); | setUpMockDb(database); | ||||
cryptoKey = await generateDatabaseCryptoKey(); | cryptoKey = await generateDatabaseCryptoKey({ extractable: false }); | ||||
}); | }); | ||||
it('should encrypt database content', async () => { | it('should encrypt database content', async () => { | ||||
const dbContent: Uint8Array = database.export(); | const dbContent: Uint8Array = database.export(); | ||||
const { ciphertext, iv } = await encryptDatabaseFile(dbContent, cryptoKey); | const { ciphertext, iv } = await encryptDatabaseFile(dbContent, cryptoKey); | ||||
expect(iv.byteLength).toBe(IV_LENGTH); | expect(iv.byteLength).toBe(IV_LENGTH); | ||||
expect(ciphertext.length).toBe(dbContent.length + TAG_LENGTH); | expect(ciphertext.length).toBe(dbContent.length + TAG_LENGTH); | ||||
}); | }); | ||||
it('is decryptable', async () => { | it('is decryptable', async () => { | ||||
const dbContent: Uint8Array = database.export(); | const dbContent: Uint8Array = database.export(); | ||||
const encryptedData = await encryptDatabaseFile(dbContent, cryptoKey); | const encryptedData = await encryptDatabaseFile(dbContent, cryptoKey); | ||||
const decrypted = await decryptDatabaseFile(encryptedData, cryptoKey); | const decrypted = await decryptDatabaseFile(encryptedData, cryptoKey); | ||||
expect(decrypted).toEqual(dbContent); | expect(decrypted).toEqual(dbContent); | ||||
}); | }); | ||||
it('should fail with wrong key', async () => { | it('should fail with wrong key', async () => { | ||||
const dbContent: Uint8Array = database.export(); | const dbContent: Uint8Array = database.export(); | ||||
const encryptedData = await encryptDatabaseFile(dbContent, cryptoKey); | const encryptedData = await encryptDatabaseFile(dbContent, cryptoKey); | ||||
const newCryptoKey = await generateDatabaseCryptoKey(); | const newCryptoKey = await generateDatabaseCryptoKey({ | ||||
extractable: false, | |||||
}); | |||||
expect(decryptDatabaseFile(encryptedData, newCryptoKey)).rejects.toThrow(); | expect(decryptDatabaseFile(encryptedData, newCryptoKey)).rejects.toThrow(); | ||||
}); | }); | ||||
it('should fail with wrong content', async () => { | it('should fail with wrong content', async () => { | ||||
const dbContent: Uint8Array = database.export(); | const dbContent: Uint8Array = database.export(); | ||||
const encryptedData = await encryptDatabaseFile(dbContent, cryptoKey); | const encryptedData = await encryptDatabaseFile(dbContent, cryptoKey); | ||||
const randomizedEncryptedData = { | const randomizedEncryptedData = { | ||||
...encryptedData, | ...encryptedData, | ||||
Show All 17 Lines |