66import { describe , expect , it } from 'vitest' ;
77
88import { mockLogger } from '../interfaces/utils.js' ;
9+ import { SqliteEncryptionError } from './errors.js' ;
910import { AztecSQLiteOPFSStore } from './store.js' ;
1011
1112function randomKey ( ) : Uint8Array {
@@ -24,16 +25,16 @@ function cloneKey(k: Uint8Array): Uint8Array {
2425describe ( 'AztecSQLiteOPFSStore with encryption' , ( ) => {
2526 it ( 'rejects keys that are not 32 bytes' , async ( ) => {
2627 const short = new Uint8Array ( 16 ) ;
27- await expect ( AztecSQLiteOPFSStore . open ( mockLogger , undefined , true , undefined , short ) ) . rejects . toThrow (
28- / e n c r y p t i o n K e y m u s t b e 3 2 b y t e s / ,
29- ) ;
28+ const promise = AztecSQLiteOPFSStore . open ( mockLogger , undefined , true , undefined , short ) ;
29+ await expect ( promise ) . rejects . toThrow ( SqliteEncryptionError ) ;
30+ await expect ( promise ) . rejects . toMatchObject ( { code : 'invalid_key_length' } ) ;
3031 } ) ;
3132
3233 it ( 'rejects encryption on ephemeral (:memory:) stores' , async ( ) => {
3334 const key = randomKey ( ) ;
34- await expect ( AztecSQLiteOPFSStore . open ( mockLogger , undefined , true , undefined , key ) ) . rejects . toThrow (
35- / n o t s u p p o r t e d f o r e p h e m e r a l / ,
36- ) ;
35+ const promise = AztecSQLiteOPFSStore . open ( mockLogger , undefined , true , undefined , key ) ;
36+ await expect ( promise ) . rejects . toThrow ( SqliteEncryptionError ) ;
37+ await expect ( promise ) . rejects . toMatchObject ( { code : 'encryption_not_supported_for_ephemeral' } ) ;
3738 } ) ;
3839
3940 it ( 'round-trips values with the same key (persistent)' , async ( ) => {
@@ -51,37 +52,43 @@ describe('AztecSQLiteOPFSStore with encryption', () => {
5152 await b . delete ( ) ;
5253 } ) ;
5354
54- it ( 'fails to open with the wrong key' , async ( ) => {
55+ it ( 'fails to open with the wrong key (throws SqliteEncryptionError, code=decrypt_failed) ' , async ( ) => {
5556 const key = randomKey ( ) ;
5657 const name = `test-wrong-${ Date . now ( ) } ` ;
5758 const dir = `/test-wrong-pool-${ Date . now ( ) } ` ;
5859 const a = await AztecSQLiteOPFSStore . open ( mockLogger , name , false , dir , cloneKey ( key ) ) ;
5960 await a . openMap < string , string > ( 'm' ) . set ( 'k1' , 'v1' ) ;
6061 await a . close ( ) ;
6162
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 ( ) ;
63+ let caught : unknown ;
64+ try {
65+ await AztecSQLiteOPFSStore . open ( mockLogger , name , false , dir , randomKey ( ) ) ;
66+ } catch ( err ) {
67+ caught = err ;
68+ }
69+ expect ( caught ) . toBeInstanceOf ( SqliteEncryptionError ) ;
70+ expect ( ( caught as SqliteEncryptionError ) . code ) . toBe ( 'decrypt_failed' ) ;
6771
6872 const c = await AztecSQLiteOPFSStore . open ( mockLogger , name , false , dir , cloneKey ( key ) ) ;
6973 await c . delete ( ) ;
7074 } ) ;
7175
72- it ( 'fails to open an encrypted DB without a key' , async ( ) => {
76+ it ( 'fails to open an encrypted DB without a key (throws SqliteEncryptionError, code=decrypt_failed) ' , async ( ) => {
7377 const key = randomKey ( ) ;
7478 const name = `test-nokey-${ Date . now ( ) } ` ;
7579 const dir = `/test-nokey-pool-${ Date . now ( ) } ` ;
7680 const a = await AztecSQLiteOPFSStore . open ( mockLogger , name , false , dir , cloneKey ( key ) ) ;
7781 await a . openMap < string , string > ( 'm' ) . set ( 'k1' , 'v1' ) ;
7882 await a . close ( ) ;
7983
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 ( ) ;
84+ let caught : unknown ;
85+ try {
86+ await AztecSQLiteOPFSStore . open ( mockLogger , name , false , dir ) ;
87+ } catch ( err ) {
88+ caught = err ;
89+ }
90+ expect ( caught ) . toBeInstanceOf ( SqliteEncryptionError ) ;
91+ expect ( ( caught as SqliteEncryptionError ) . code ) . toBe ( 'decrypt_failed' ) ;
8592
8693 const c = await AztecSQLiteOPFSStore . open ( mockLogger , name , false , dir , cloneKey ( key ) ) ;
8794 await c . delete ( ) ;
0 commit comments