|
| 1 | +/** |
| 2 | + * Tests for sqlite3mc-backed page-level encryption. |
| 3 | + * Exercises the `encryptionKey` parameter added to `AztecSQLiteOPFSStore.open()`. |
| 4 | + * Mixes ephemeral and persistent stores so we cover both `:memory:` and OPFS paths. |
| 5 | + */ |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +import { mockLogger } from '../interfaces/utils.js'; |
| 9 | +import { AztecSQLiteOPFSStore } from './store.js'; |
| 10 | + |
| 11 | +function randomKey(): Uint8Array { |
| 12 | + return globalThis.crypto.getRandomValues(new Uint8Array(32)); |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Clone a key into a fresh Uint8Array. `AztecSQLiteOPFSStore.open()` transfers |
| 17 | + * the key's ArrayBuffer to the worker (detaching it on the caller side), so |
| 18 | + * callers that open multiple stores or re-use a key must pass a copy per call. |
| 19 | + */ |
| 20 | +function cloneKey(k: Uint8Array): Uint8Array { |
| 21 | + return new Uint8Array(k); |
| 22 | +} |
| 23 | + |
| 24 | +describe('AztecSQLiteOPFSStore with encryption', () => { |
| 25 | + it('rejects keys that are not 32 bytes', async () => { |
| 26 | + const short = new Uint8Array(16); |
| 27 | + await expect(AztecSQLiteOPFSStore.open(mockLogger, undefined, true, undefined, short)).rejects.toThrow( |
| 28 | + /encryptionKey must be 32 bytes/, |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it('rejects encryption on ephemeral (:memory:) stores', async () => { |
| 33 | + const key = randomKey(); |
| 34 | + await expect(AztecSQLiteOPFSStore.open(mockLogger, undefined, true, undefined, key)).rejects.toThrow( |
| 35 | + /not supported for ephemeral/, |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('round-trips values with the same key (persistent)', async () => { |
| 40 | + const key = randomKey(); |
| 41 | + const name = `test-enc-${Date.now()}`; |
| 42 | + const dir = `/test-enc-pool-${Date.now()}`; |
| 43 | + const a = await AztecSQLiteOPFSStore.open(mockLogger, name, false, dir, cloneKey(key)); |
| 44 | + const mapA = a.openMap<string, string>('m'); |
| 45 | + await mapA.set('k1', 'v1'); |
| 46 | + await a.close(); |
| 47 | + |
| 48 | + const b = await AztecSQLiteOPFSStore.open(mockLogger, name, false, dir, cloneKey(key)); |
| 49 | + const mapB = b.openMap<string, string>('m'); |
| 50 | + expect(await mapB.getAsync('k1')).toBe('v1'); |
| 51 | + await b.delete(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('fails to open with the wrong key', async () => { |
| 55 | + const key = randomKey(); |
| 56 | + const name = `test-wrong-${Date.now()}`; |
| 57 | + const dir = `/test-wrong-pool-${Date.now()}`; |
| 58 | + const a = await AztecSQLiteOPFSStore.open(mockLogger, name, false, dir, cloneKey(key)); |
| 59 | + await a.openMap<string, string>('m').set('k1', 'v1'); |
| 60 | + await a.close(); |
| 61 | + |
| 62 | + await expect(async () => { |
| 63 | + const b = await AztecSQLiteOPFSStore.open(mockLogger, name, false, dir, randomKey()); |
| 64 | + await b.openMap<string, string>('m').getAsync('k1'); |
| 65 | + await b.close(); |
| 66 | + }).rejects.toThrow(); |
| 67 | + |
| 68 | + const c = await AztecSQLiteOPFSStore.open(mockLogger, name, false, dir, cloneKey(key)); |
| 69 | + await c.delete(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('fails to open an encrypted DB without a key', async () => { |
| 73 | + const key = randomKey(); |
| 74 | + const name = `test-nokey-${Date.now()}`; |
| 75 | + const dir = `/test-nokey-pool-${Date.now()}`; |
| 76 | + const a = await AztecSQLiteOPFSStore.open(mockLogger, name, false, dir, cloneKey(key)); |
| 77 | + await a.openMap<string, string>('m').set('k1', 'v1'); |
| 78 | + await a.close(); |
| 79 | + |
| 80 | + await expect(async () => { |
| 81 | + const b = await AztecSQLiteOPFSStore.open(mockLogger, name, false, dir); |
| 82 | + await b.openMap<string, string>('m').getAsync('k1'); |
| 83 | + await b.close(); |
| 84 | + }).rejects.toThrow(); |
| 85 | + |
| 86 | + const c = await AztecSQLiteOPFSStore.open(mockLogger, name, false, dir, cloneKey(key)); |
| 87 | + await c.delete(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments