@@ -46,7 +46,7 @@ impl EncryptedConnection {
46
46
/// Creates a file for the salt and stores it
47
47
pub fn new ( key : EncryptionKey , opts : & StorageOption ) -> Result < Self , StorageError > {
48
48
use super :: StorageOption :: * ;
49
- Self :: check_for_sqlcipher ( opts) ?;
49
+ Self :: check_for_sqlcipher ( opts, None ) ?;
50
50
51
51
let salt = match opts {
52
52
Ephemeral => None ,
@@ -222,28 +222,28 @@ impl EncryptedConnection {
222
222
}
223
223
}
224
224
225
- fn check_for_sqlcipher ( opts : & StorageOption ) -> Result < ( ) , StorageError > {
225
+ fn check_for_sqlcipher (
226
+ opts : & StorageOption ,
227
+ conn : Option < SqliteConnection > ,
228
+ ) -> Result < CipherVersion , StorageError > {
226
229
if let Some ( path) = opts. path ( ) {
227
230
let exists = std:: path:: Path :: new ( path) . exists ( ) ;
228
231
tracing:: debug!( "db @ [{}] exists? [{}]" , path, exists) ;
229
232
}
230
- let conn = & mut opts. conn ( ) ?;
231
- let cipher_version = sql_query ( "PRAGMA cipher_version" ) . load :: < CipherVersion > ( conn) ?;
233
+ let conn = & mut conn . unwrap_or ( opts. conn ( ) ?) ;
234
+ let mut cipher_version = sql_query ( "PRAGMA cipher_version" ) . load :: < CipherVersion > ( conn) ?;
232
235
if cipher_version. is_empty ( ) {
233
236
return Err ( StorageError :: SqlCipherNotLoaded ) ;
234
237
}
235
- Ok ( ( ) )
238
+ Ok ( cipher_version . pop ( ) . expect ( "checked for empty" ) )
236
239
}
237
240
}
238
241
239
242
impl super :: native:: ValidatedConnection for EncryptedConnection {
240
243
fn validate ( & self , opts : & StorageOption ) -> Result < ( ) , StorageError > {
241
244
let conn = & mut opts. conn ( ) ?;
242
245
243
- let cipher_version = sql_query ( "PRAGMA cipher_version" ) . load :: < CipherVersion > ( conn) ?;
244
- if cipher_version. is_empty ( ) {
245
- return Err ( StorageError :: SqlCipherNotLoaded ) ;
246
- }
246
+ let cipher_version = EncryptedConnection :: check_for_sqlcipher ( opts, None ) ?;
247
247
248
248
// test the key according to
249
249
// https://www.zetetic.net/sqlcipher/sqlcipher-api/#testing-the-key
@@ -259,8 +259,8 @@ impl super::native::ValidatedConnection for EncryptedConnection {
259
259
} = sql_query ( "PRAGMA cipher_provider_version" )
260
260
. get_result :: < CipherProviderVersion > ( conn) ?;
261
261
tracing:: info!(
262
- "Sqlite cipher_version={:? }, cipher_provider_version={:?}" ,
263
- cipher_version. first ( ) . as_ref ( ) . map ( |v| & v . cipher_version) ,
262
+ "Sqlite cipher_version={}, cipher_provider_version={:?}" ,
263
+ cipher_version. cipher_version,
264
264
cipher_provider_version
265
265
) ;
266
266
if tracing:: enabled!( tracing:: Level :: DEBUG ) {
@@ -314,6 +314,16 @@ mod tests {
314
314
const SQLITE3_PLAINTEXT_HEADER : & str = "SQLite format 3\0 " ;
315
315
use StorageOption :: * ;
316
316
317
+ #[ tokio:: test]
318
+ async fn test_sqlcipher_version ( ) {
319
+ let db_path = tmp_path ( ) ;
320
+ {
321
+ let opts = Persistent ( db_path. clone ( ) ) ;
322
+ let v = EncryptedConnection :: check_for_sqlcipher ( & opts, None ) . unwrap ( ) ;
323
+ println ! ( "SQLCipher Version {}" , v. cipher_version) ;
324
+ }
325
+ }
326
+
317
327
#[ tokio:: test]
318
328
async fn test_db_creates_with_plaintext_header ( ) {
319
329
let db_path = tmp_path ( ) ;
0 commit comments