|
| 1 | +import { |
| 2 | + PowerSyncDatabase, |
| 3 | + WASQLiteOpenFactory, |
| 4 | + WASQLiteVFS, |
| 5 | + WebPowerSyncDatabaseOptionsWithOpenFactory, |
| 6 | + WebPowerSyncDatabaseOptionsWithSettings |
| 7 | +} from '@powersync/web'; |
| 8 | +import { v4 as uuid } from 'uuid'; |
| 9 | +import { describe, expect, it } from 'vitest'; |
| 10 | +import { testSchema } from './utils/testDb'; |
| 11 | + |
| 12 | +describe('Encryption Tests', { sequential: true }, () => { |
| 13 | + it('IDBBatchAtomicVFS encryption', async () => { |
| 14 | + await testEncryption({ |
| 15 | + schema: testSchema, |
| 16 | + database: { dbFilename: 'iddb-file.db' }, |
| 17 | + encryptionKey: 'iddb-key' |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('OPFSCoopSyncVFS encryption', async () => { |
| 22 | + await testEncryption({ |
| 23 | + schema: testSchema, |
| 24 | + database: new WASQLiteOpenFactory({ |
| 25 | + dbFilename: 'opfs-file.db', |
| 26 | + vfs: WASQLiteVFS.OPFSCoopSyncVFS, |
| 27 | + encryptionKey: 'opfs-key' |
| 28 | + }) |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('AccessHandlePoolVFS encryption', async () => { |
| 33 | + await testEncryption({ |
| 34 | + schema: testSchema, |
| 35 | + database: new WASQLiteOpenFactory({ |
| 36 | + dbFilename: 'ahp-file.db', |
| 37 | + vfs: WASQLiteVFS.AccessHandlePoolVFS, |
| 38 | + encryptionKey: 'ahp-key' |
| 39 | + }) |
| 40 | + }); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +/** |
| 45 | + * The open/close and open again flow is an easy way to verify that encryption is working. |
| 46 | + */ |
| 47 | +const testEncryption = async ( |
| 48 | + options: WebPowerSyncDatabaseOptionsWithSettings | WebPowerSyncDatabaseOptionsWithOpenFactory |
| 49 | +) => { |
| 50 | + let powersync = new PowerSyncDatabase(options as any); |
| 51 | + |
| 52 | + await powersync.init(); |
| 53 | + await powersync.close(); |
| 54 | + |
| 55 | + powersync = new PowerSyncDatabase(options as any); |
| 56 | + |
| 57 | + await powersync.init(); |
| 58 | + await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['test', uuid()]); |
| 59 | + const results = await powersync.getAll('SELECT * FROM assets'); |
| 60 | + expect(results.length).toBe(1); |
| 61 | + |
| 62 | + await powersync.disconnectAndClear(); |
| 63 | + await powersync.close(); |
| 64 | +}; |
0 commit comments