walletkit-sqlite 0.23.2

Safe Rust wrapper around sqlite3mc for WalletKit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
//! `sqlite3mc` encryption configuration.
//!
//! # Encryption flow
//!
//! This crate uses `sqlite3mc` (`SQLite3` Multiple Ciphers) to encrypt
//! `SQLite` databases at rest. The encryption is transparent to SQL -- once a
//! database is opened and keyed, all reads and writes are automatically
//! encrypted/decrypted by the `SQLite` pager layer.
//!
//! The flow when opening a database is:
//!
//! 1. **Open** -- `sqlite3_open_v2` creates or opens the database file.
//!    At this point the file is opaque (encrypted) and no data can be read.
//!
//! 2. **Configure cipher** -- `PRAGMA cipher = 'chacha20'` fixes the on-disk
//!    cipher before the key activates it.
//!
//! 3. **Detect and encrypt or unlock** -- A read from `sqlite_master` succeeds
//!    for a plaintext (or new) database. Such a database is moved out of WAL
//!    mode and atomically encrypted with `PRAGMA rekey`. Existing databases
//!    with a fully encrypted header are unlocked using their old settings and
//!    migrated in place. Both `key` and `rekey` receive the 32-byte
//!    `K_intermediate` as a raw hex key, bypassing the passphrase KDF.
//!
//! 4. **Verify** -- We read from `sqlite_master` after rekeying or keying. A
//!    wrong key returns `SQLITE_NOTADB` because the decrypted page header does
//!    not match the expected `SQLite` magic bytes.
//!
//! 5. **Configure connection** -- The target-specific journal mode and every
//!    connection-level invariant are set and verified.
//!
//! The default cipher is **ChaCha20-Poly1305** (authenticated encryption).
//! All crypto is built into the `sqlite3mc` amalgamation -- no OpenSSL or
//! other external crypto library is needed on any platform.
//!
//! The first 32 bytes of every database header remain plaintext. This gives
//! all targets one on-disk format and lets iOS recognize shared-container
//! databases in WAL mode. Existing databases with fully encrypted headers are
//! migrated in place on their first successful open.

use std::path::Path;

use secrecy::{ExposeSecret, SecretBox};
use zeroize::Zeroizing;

use super::connection::Connection;
use super::error::{DbResult, Error};

const CIPHER_CHACHA20: &str = "chacha20";
const PLAINTEXT_HEADER_SIZE: i64 = 32;
const SQLITE_ERROR: i32 = 1;
const SQLITE_CORRUPT: i32 = 11;
const FOREIGN_KEYS_ON: i64 = 1;
const SYNCHRONOUS_FULL: i64 = 2;
const SECURE_DELETE_ON: i64 = 1;
const TEMP_STORE_MEMORY: i64 = 2;

/// Opens a writable database, applies the encryption key, and configures the connection.
///
/// This is the standard open sequence for databases: open -> select cipher ->
/// encrypt plaintext or unlock encrypted data -> verify -> configure policy.
///
/// See the [module-level documentation](self) for the full encryption flow.
///
/// # Errors
///
/// Returns `Error` if opening, keying, or configuring the connection fails.
pub fn open_encrypted(
    path: &Path,
    k_intermediate: &SecretBox<[u8; 32]>,
) -> DbResult<Connection> {
    #[cfg(not(target_arch = "wasm32"))]
    let conn = Connection::open(path, false)?;
    #[cfg(target_arch = "wasm32")]
    let conn = Connection::open_with_opfs_vfs(path, false)?;
    configure_connection(&conn, k_intermediate)?;
    Ok(conn)
}

/// Configures durable journal settings, foreign keys, and secure deletion.
///
/// - Native uses WAL for concurrent readers during writes.
/// - WASM uses a rollback journal because SAH-pool has no WAL shared-memory
///   methods; WAL would require exclusive locking and provide no concurrency.
/// - `synchronous = FULL` -- maximizes crash consistency by flushing required
///   journal writes before the transaction is reported as committed.
/// - `foreign_keys = ON` -- enforces referential integrity constraints.
/// - `secure_delete = ON` -- overwrites deleted content with zeroes so
///   sensitive data does not linger in free pages.
fn configure_connection(
    conn: &Connection,
    k_intermediate: &SecretBox<[u8; 32]>,
) -> DbResult<()> {
    ensure_cipher(conn)?;
    encrypt_or_unlock(conn, k_intermediate)?;

    #[cfg(not(target_arch = "wasm32"))]
    ensure_journal_mode(conn, "WAL")?;
    // SAH-pool does not expose WAL shared-memory methods. WAL would therefore
    // require locking_mode=EXCLUSIVE before the first database access and
    // provide no concurrency benefit, so WASM deliberately uses the rollback
    // journal until benchmarks justify that extra complexity.
    #[cfg(target_arch = "wasm32")]
    ensure_journal_mode(conn, "DELETE")?;

    ensure_foreign_keys(conn)?;
    ensure_synchronous_full(conn)?;
    ensure_secure_delete(conn)?;
    ensure_temp_store_memory(conn)?;
    Ok(())
}

/// Encrypts or unlocks a database with a plaintext `SQLite` header.
///
/// Earlier `WalletKit` versions encrypted the header completely. Those databases
/// must first be opened with the old settings, moved out of WAL mode, and rekeyed
/// after configuring the plaintext header. Databases that already start with the
/// `SQLite` magic bytes are either plaintext or already use the new format;
/// probing the schema before applying the key distinguishes the two cases.
fn encrypt_or_unlock(
    conn: &Connection,
    k_intermediate: &SecretBox<[u8; 32]>,
) -> DbResult<()> {
    match verify_schema_readable(conn) {
        Ok(()) => {
            // Plaintext (or newly-created) database. Configure the new format
            // before the first rekey so it is never written with an encrypted
            // header.
            ensure_journal_mode(conn, "DELETE")?;
            ensure_plaintext_header(conn)?;
            apply_rekey(conn, k_intermediate)?;
            verify_encryption(conn, "plaintext database encryption verification failed")
        }
        Err(error) if is_plaintext_header_probe_error(&error) => {
            // The SQLite header is visible but the schema is not readable
            // without the codec: this is already the plaintext-header format.
            ensure_plaintext_header(conn)?;
            apply_key(conn, k_intermediate)
        }
        Err(error) if error.code.0 & 0xff == super::ffi::SQLITE_NOTADB => {
            // Legacy WalletKit format. Unlock it with the encrypted-header
            // settings, checkpoint WAL, then atomically rekey with the same raw
            // key after selecting the common plaintext-header format.
            apply_key(conn, k_intermediate)?;
            ensure_journal_mode(conn, "DELETE")?;
            ensure_plaintext_header(conn)?;
            apply_rekey(conn, k_intermediate)?;
            verify_encryption(conn, "plaintext-header migration verification failed")
        }
        Err(error) => Err(error),
    }
}

/// A plaintext-header encrypted page exposes format fields that vanilla
/// `SQLite` tries to parse before the codec is configured. Depending on the
/// encrypted page bytes, that probe can fail either while validating the
/// header fields or while parsing the page body.
fn is_plaintext_header_probe_error(error: &Error) -> bool {
    let primary_code = error.code.0 & 0xff;
    (primary_code == SQLITE_ERROR && error.message == "unsupported file format")
        || (primary_code == SQLITE_CORRUPT
            && error.message == "database disk image is malformed")
}

fn ensure_plaintext_header(conn: &Connection) -> DbResult<()> {
    conn.execute_batch(&format!(
        "PRAGMA plaintext_header_size = {PLAINTEXT_HEADER_SIZE};"
    ))?;
    let actual = conn.query_row("PRAGMA plaintext_header_size;", &[], |row| {
        Ok(row.column_i64(0))
    })?;
    if actual == PLAINTEXT_HEADER_SIZE {
        Ok(())
    } else {
        Err(Error::new(
            -1,
            format!(
                "could not ensure plaintext header size {PLAINTEXT_HEADER_SIZE}: SQLite selected {actual}"
            ),
        ))
    }
}

/// Encrypts or unlocks a database using `WalletKit`'s legacy fully encrypted
/// header format. Kept only to construct migration fixtures.
#[cfg(test)]
fn encrypt_or_unlock_fully_encrypted(
    conn: &Connection,
    k_intermediate: &SecretBox<[u8; 32]>,
) -> DbResult<()> {
    match verify_schema_readable(conn) {
        Ok(()) => {
            ensure_journal_mode(conn, "DELETE")?;
            apply_rekey(conn, k_intermediate)?;
            verify_encryption(conn, "plaintext database encryption verification failed")
        }
        Err(error) if error.code.0 & 0xff == super::ffi::SQLITE_NOTADB => {
            apply_key(conn, k_intermediate)
        }
        Err(error) => Err(error),
    }
}

fn verify_encryption(conn: &Connection, context: &str) -> DbResult<()> {
    verify_schema_readable(conn).map_err(|error| {
        Error::new(error.code.0, format!("{context}: {}", error.message))
    })
}

/// Selects and verifies the on-disk cipher before the key activates it.
///
/// Pinning the cipher prevents a future compile-time default change from
/// silently creating or interpreting databases with a different format.
fn ensure_cipher(conn: &Connection) -> DbResult<()> {
    conn.execute_batch(&format!("PRAGMA cipher = '{CIPHER_CHACHA20}';"))?;
    let actual = conn.query_row("PRAGMA cipher;", &[], |row| Ok(row.column_text(0)))?;
    if actual.eq_ignore_ascii_case(CIPHER_CHACHA20) {
        Ok(())
    } else {
        Err(Error::new(
            -1,
            format!(
                "could not ensure sqlite3mc cipher {CIPHER_CHACHA20}: SQLite selected {actual}"
            ),
        ))
    }
}

/// Applies the `sqlite3mc` encryption key to an open connection.
///
/// The 32-byte `k_intermediate` is hex-encoded and passed as a raw key via
/// `PRAGMA key = "x'<64-hex-chars>'"`. `sqlite3mc` interprets the `x'...'`
/// prefix as a raw key (as opposed to a passphrase that would be run through
/// a KDF first).
///
/// After keying, a lightweight read (`SELECT count(*) FROM sqlite_master`)
/// verifies the key is correct. If it's wrong, `sqlite3mc` fails with
/// `SQLITE_NOTADB` on the first page read.
fn apply_key(conn: &Connection, k_intermediate: &SecretBox<[u8; 32]>) -> DbResult<()> {
    let pragma = raw_key_pragma("key", k_intermediate);

    // execute_batch_zeroized ensures the internal CString copy of the PRAGMA
    // (which contains the hex key) is zeroized after the FFI call returns.
    conn.execute_batch_zeroized(&pragma)?;

    // Touch a page to verify the key works. On failure this produces a clear
    // error rather than a confusing "not a database" later during schema setup.
    verify_schema_readable(conn).map_err(|e| {
        Error::new(
            e.code.0,
            format!(
                "encryption key verification failed (is the key correct?): {}",
                e.message
            ),
        )
    })?;

    // k_intermediate and pragma are zeroized on drop regardless of which exit
    // path we took. raw_key_pragma zeroizes its temporary hex buffer too.
    Ok(())
}

/// Encrypts a readable plaintext database in place with the supplied raw key.
fn apply_rekey(
    conn: &Connection,
    k_intermediate: &SecretBox<[u8; 32]>,
) -> DbResult<()> {
    let pragma = raw_key_pragma("rekey", k_intermediate);
    conn.execute_batch_zeroized(&pragma).map_err(|e| {
        Error::new(
            e.code.0,
            format!("failed to encrypt plaintext database: {}", e.message),
        )
    })
}

fn raw_key_pragma(
    operation: &str,
    k_intermediate: &SecretBox<[u8; 32]>,
) -> Zeroizing<String> {
    let key_hex = Zeroizing::new(hex::encode(k_intermediate.expose_secret()));
    Zeroizing::new(format!("PRAGMA {operation} = \"x'{}'\";", key_hex.as_str()))
}

fn verify_schema_readable(conn: &Connection) -> DbResult<()> {
    conn.execute_batch("SELECT count(*) FROM sqlite_master;")
}

/// Ensures the target-specific journal policy actually took effect.
///
/// Assigning `journal_mode` returns the effective mode because `SQLite` may
/// retain the previous mode when the requested transition is unavailable.
fn ensure_journal_mode(conn: &Connection, requested: &str) -> DbResult<()> {
    let actual =
        conn.query_row(&format!("PRAGMA journal_mode = {requested};"), &[], |row| {
            Ok(row.column_text(0))
        })?;
    if actual.eq_ignore_ascii_case(requested) {
        Ok(())
    } else {
        Err(Error::new(
            -1,
            format!(
                "could not ensure journal mode {requested}: SQLite selected {actual}"
            ),
        ))
    }
}

/// Enables foreign-key enforcement for every connection.
///
/// `SQLite` defaults this setting to off and may silently ignore the assignment
/// inside a transaction or when foreign-key support was omitted at build time.
fn ensure_foreign_keys(conn: &Connection) -> DbResult<()> {
    conn.execute_batch("PRAGMA foreign_keys = ON;")?;
    let actual =
        conn.query_row("PRAGMA foreign_keys;", &[], |row| Ok(row.column_i64(0)))?;
    if actual == FOREIGN_KEYS_ON {
        Ok(())
    } else {
        Err(Error::new(
            -1,
            format!(
                "could not ensure PRAGMA foreign_keys = ON: expected {FOREIGN_KEYS_ON}, got {actual}"
            ),
        ))
    }
}

/// Uses `SQLite`'s strongest ordinary durability policy.
///
/// `FULL` ensures `SQLite` flushes journal content before reporting a transaction
/// as committed, reducing the risk of corruption after a crash or power loss.
fn ensure_synchronous_full(conn: &Connection) -> DbResult<()> {
    conn.execute_batch("PRAGMA synchronous = FULL;")?;
    let actual =
        conn.query_row("PRAGMA synchronous;", &[], |row| Ok(row.column_i64(0)))?;
    if actual == SYNCHRONOUS_FULL {
        Ok(())
    } else {
        Err(Error::new(
            -1,
            format!(
                "could not ensure PRAGMA synchronous = FULL: expected {SYNCHRONOUS_FULL}, got {actual}"
            ),
        ))
    }
}

/// Overwrites deleted content instead of leaving it in reusable database pages.
///
/// This limits plaintext remnants while the encrypted database is open and
/// accessible with its key.
fn ensure_secure_delete(conn: &Connection) -> DbResult<()> {
    conn.execute_batch("PRAGMA secure_delete = ON;")?;
    let actual =
        conn.query_row("PRAGMA secure_delete;", &[], |row| Ok(row.column_i64(0)))?;
    if actual == SECURE_DELETE_ON {
        Ok(())
    } else {
        Err(Error::new(
            -1,
            format!(
                "could not ensure PRAGMA secure_delete = ON: expected {SECURE_DELETE_ON}, got {actual}"
            ),
        ))
    }
}

/// Keeps temporary tables and indices in memory.
///
/// `sqlite3mc` does not encrypt temporary databases, so allowing temporary
/// storage to spill to a filesystem could expose plaintext at rest.
fn ensure_temp_store_memory(conn: &Connection) -> DbResult<()> {
    conn.execute_batch("PRAGMA temp_store = MEMORY;")?;
    let actual =
        conn.query_row("PRAGMA temp_store;", &[], |row| Ok(row.column_i64(0)))?;
    if actual == TEMP_STORE_MEMORY {
        Ok(())
    } else {
        Err(Error::new(
            -1,
            format!(
                "could not ensure PRAGMA temp_store = MEMORY: expected {TEMP_STORE_MEMORY}, got {actual}"
            ),
        ))
    }
}

/// Creates a plaintext (unencrypted) copy of an already-open encrypted database.
///
/// The copy is produced by `ATTACH`-ing a new unencrypted database and copying
/// the caller-specified tables via `CREATE TABLE ... AS SELECT *`. The
/// destination file must not already exist.
///
/// We use `ATTACH` + SQL instead of the `sqlite3_backup` API because
/// `sqlite3mc` requires both source and destination to share the same
/// encryption configuration. Since the destination is unencrypted, the
/// backup API cannot be used.
///
/// # Errors
///
/// Returns `Error` if the `ATTACH`, copy, or `DETACH` fails.
pub fn export_plaintext_copy(
    conn: &Connection,
    dest_path: &Path,
    tables: &[&str],
) -> DbResult<()> {
    let dest_str = dest_path.to_string_lossy();
    let attach_sql = format!(
        "ATTACH DATABASE '{}' AS backup KEY '';",
        dest_str.replace('\'', "''")
    );
    conn.execute_batch(&attach_sql)?;

    let result = (|| {
        let tx = conn.transaction()?;
        for table in tables {
            tx.execute_batch(&format!(
                "CREATE TABLE backup.{table} AS SELECT * FROM {table};"
            ))?;
        }
        tx.commit()
    })();

    // Always detach, even if the copy failed.
    let detach_result = conn.execute_batch("DETACH DATABASE backup;");

    result?;
    detach_result?;
    Ok(())
}

/// Imports data from a plaintext (unencrypted) database into an already-open
/// encrypted database.
///
/// The source database is `ATTACH`ed with an empty key and its contents are
/// copied into the main (empty) encrypted database.
///
/// See [`export_plaintext_copy`] for why `ATTACH` + SQL is used instead of
/// the `sqlite3_backup` API.
///
/// **Schema migration:** The import uses `SELECT *`, so column changes are
/// handled automatically as long as both sides share the same schema. If a
/// caller's schema evolves (e.g. new columns with `NOT NULL` constraints),
/// restoring an older backup into a newer schema will fail. When that happens,
/// the caller needs version-aware import logic.
///
/// # Errors
///
/// Returns `Error` if the `ATTACH`, copy, or `DETACH` fails.
pub fn import_plaintext_copy(
    conn: &Connection,
    source_path: &Path,
    tables: &[&str],
) -> DbResult<()> {
    if !source_path.exists() {
        return Err(Error::new(
            -1,
            format!("backup file does not exist: {}", source_path.display()),
        ));
    }

    let source_str = source_path.to_string_lossy();
    let attach_sql = format!(
        "ATTACH DATABASE '{}' AS backup KEY '';",
        source_str.replace('\'', "''")
    );
    conn.execute_batch(&attach_sql)?;

    // Verify the destination tables are empty before importing. Importing into
    // a non-empty destination could silently merge data if primary keys don't
    // collide.
    let result = (|| {
        for table in tables {
            let count: i64 =
                conn.query_row(&format!("SELECT COUNT(*) FROM {table}"), &[], |row| {
                    Ok(row.column_i64(0))
                })?;
            if count > 0 {
                return Err(Error::new(
                    -1,
                    format!("cannot import into non-empty table: {table}"),
                ));
            }
        }

        // Wrap in a transaction so the restore is atomic — if any INSERT
        // fails, everything is rolled back and the destination stays empty for
        // a retry.
        let tx = conn.transaction()?;
        for table in tables {
            tx.execute_batch(&format!(
                "INSERT INTO {table} SELECT * FROM backup.{table};"
            ))?;
        }
        tx.commit()
    })();

    // Always detach, even if the import failed.
    let detach_result = conn.execute_batch("DETACH DATABASE backup;");

    result?;
    detach_result?;
    Ok(())
}

/// Runs `PRAGMA integrity_check` and returns whether the database is healthy.
///
/// # Errors
///
/// Returns `Error` if the integrity check query fails.
pub fn integrity_check(conn: &Connection) -> DbResult<bool> {
    let result = conn.query_row("PRAGMA integrity_check;", &[], |stmt| {
        Ok(stmt.column_text(0))
    })?;
    Ok(result.trim() == "ok")
}

#[cfg(test)]
mod tests {
    use super::{
        encrypt_or_unlock_fully_encrypted, ensure_cipher, ensure_journal_mode,
        export_plaintext_copy, import_plaintext_copy, integrity_check,
        is_plaintext_header_probe_error, open_encrypted, Error, SQLITE_CORRUPT,
        SQLITE_ERROR,
    };
    use crate::params;
    use crate::test_utils::init_sqlite;
    use crate::Connection;
    use secrecy::SecretBox;

    fn open_fully_encrypted(
        path: &std::path::Path,
        key: &SecretBox<[u8; 32]>,
    ) -> crate::DbResult<Connection> {
        let conn = Connection::open(path, false)?;
        ensure_cipher(&conn)?;
        encrypt_or_unlock_fully_encrypted(&conn, key)?;
        ensure_journal_mode(&conn, "WAL")?;
        Ok(conn)
    }

    #[test]
    fn test_plaintext_header_probe_errors() {
        assert!(is_plaintext_header_probe_error(&Error::new(
            SQLITE_ERROR,
            "unsupported file format",
        )));
        assert!(is_plaintext_header_probe_error(&Error::new(
            SQLITE_CORRUPT,
            "database disk image is malformed",
        )));
        assert!(!is_plaintext_header_probe_error(&Error::new(
            SQLITE_CORRUPT,
            "database or disk is full",
        )));
    }

    #[test]
    fn test_cipher_encrypted_round_trip() {
        init_sqlite();
        let dir = tempfile::tempdir().expect("create temp dir");
        let path = dir.path().join("cipher-test.sqlite");
        let key = SecretBox::init_with(|| [0xABu8; 32]);

        // Create and write
        {
            let conn = open_encrypted(&path, &key).expect("open encrypted");
            conn.execute_batch(
                "CREATE TABLE secret (id INTEGER PRIMARY KEY, val TEXT);",
            )
            .expect("create table");
            conn.execute("INSERT INTO secret (id, val) VALUES (1, 'top-secret')", &[])
                .expect("insert");
        }

        // Re-open with correct key
        {
            let conn = open_encrypted(&path, &key).expect("reopen encrypted");
            let val = conn
                .query_row("SELECT val FROM secret WHERE id = 1", &[], |stmt| {
                    Ok(stmt.column_text(0))
                })
                .expect("query");
            assert_eq!(val, "top-secret");
        }

        // Wrong key should fail
        {
            let wrong_key = SecretBox::init_with(|| [0xCDu8; 32]);
            let result = open_encrypted(&path, &wrong_key);
            assert!(result.is_err(), "wrong key should fail");
        }
    }

    #[test]
    fn test_plaintext_wal_database_migrates_to_plaintext_header() {
        init_sqlite();
        let dir = tempfile::tempdir().expect("create temp dir");
        let path = dir.path().join("plaintext.sqlite");
        let key = SecretBox::init_with(|| [0x42u8; 32]);

        {
            let conn = Connection::open(&path, false).expect("open plaintext");
            let mode = conn
                .query_row("PRAGMA journal_mode = WAL", &[], |row| {
                    Ok(row.column_text(0))
                })
                .expect("enable plaintext WAL");
            assert_eq!(mode.to_ascii_lowercase(), "wal");
            conn.execute_batch(
                "CREATE TABLE secret (id INTEGER PRIMARY KEY, val TEXT);\
                 INSERT INTO secret VALUES (1, 'preserve-me');",
            )
            .expect("write plaintext data");
        }
        assert!(
            std::fs::read(&path)
                .expect("read plaintext")
                .starts_with(b"SQLite format 3\0"),
            "fixture must start as plaintext SQLite"
        );

        {
            let conn = open_encrypted(&path, &key).expect("migrate plaintext");
            let value = conn
                .query_row("SELECT val FROM secret WHERE id = 1", &[], |row| {
                    Ok(row.column_text(0))
                })
                .expect("read migrated data");
            assert_eq!(value, "preserve-me");
        }

        let encrypted_bytes = std::fs::read(&path).expect("read encrypted");
        assert!(
            encrypted_bytes.starts_with(b"SQLite format 3\0"),
            "migration must retain the plaintext SQLite header"
        );
        assert_eq!(encrypted_bytes[18], 2, "database must use WAL read mode");
        assert_eq!(encrypted_bytes[19], 2, "database must use WAL write mode");
        assert_eq!(
            encrypted_bytes[20], 32,
            "header must advertise the cipher's reserved bytes"
        );
        assert!(
            !encrypted_bytes
                .windows("preserve-me".len())
                .any(|window| window == b"preserve-me"),
            "database contents must be encrypted"
        );

        {
            let conn = open_encrypted(&path, &key).expect("reopen migrated database");
            let value = conn
                .query_row("SELECT val FROM secret WHERE id = 1", &[], |row| {
                    Ok(row.column_text(0))
                })
                .expect("read migrated data after reopen");
            assert_eq!(value, "preserve-me");
        }

        let wrong_key = SecretBox::init_with(|| [0x43u8; 32]);
        assert!(
            open_encrypted(&path, &wrong_key).is_err(),
            "migrated database must reject the wrong key"
        );
        assert_eq!(
            std::fs::read(&path).expect("read after wrong-key open"),
            encrypted_bytes,
            "wrong-key open must not modify migrated data"
        );
    }

    #[test]
    fn test_plaintext_header_encrypted_round_trip() {
        init_sqlite();
        let dir = tempfile::tempdir().expect("create temp dir");
        let path = dir.path().join("plaintext-header.sqlite");
        let key = SecretBox::init_with(|| [0x51_u8; 32]);

        {
            let conn =
                open_encrypted(&path, &key).expect("create plaintext-header database");
            conn.execute_batch(
                "CREATE TABLE secret (id INTEGER PRIMARY KEY, val TEXT);\
                 INSERT INTO secret VALUES (1, 'visible-header');",
            )
            .expect("write encrypted data");
        }

        let encrypted_bytes = std::fs::read(&path).expect("read encrypted database");
        assert!(
            encrypted_bytes.starts_with(b"SQLite format 3\0"),
            "the SQLite file header must remain visible"
        );
        assert_eq!(encrypted_bytes[18], 2, "database must use WAL read mode");
        assert_eq!(encrypted_bytes[19], 2, "database must use WAL write mode");
        assert_eq!(
            encrypted_bytes[20], 32,
            "header must advertise the cipher's reserved bytes"
        );
        assert!(
            !encrypted_bytes
                .windows("visible-header".len())
                .any(|window| window == b"visible-header"),
            "database contents must remain encrypted"
        );

        {
            let conn =
                open_encrypted(&path, &key).expect("reopen plaintext-header database");
            let value = conn
                .query_row("SELECT val FROM secret WHERE id = 1", &[], |row| {
                    Ok(row.column_text(0))
                })
                .expect("read encrypted data");
            assert_eq!(value, "visible-header");
        }

        let wrong_key = SecretBox::init_with(|| [0x52_u8; 32]);
        assert!(
            open_encrypted(&path, &wrong_key).is_err(),
            "plaintext-header database must reject the wrong key"
        );
        assert_eq!(
            std::fs::read(&path).expect("read after wrong-key open"),
            encrypted_bytes,
            "wrong-key open must not modify encrypted data"
        );
    }

    #[test]
    fn test_encrypted_header_database_migrates_to_plaintext_header() {
        init_sqlite();
        let dir = tempfile::tempdir().expect("create temp dir");
        let path = dir.path().join("encrypted-header.sqlite");
        let key = SecretBox::init_with(|| [0x61_u8; 32]);

        {
            let conn =
                open_fully_encrypted(&path, &key).expect("create legacy database");
            conn.execute_batch(
                "CREATE TABLE secret (id INTEGER PRIMARY KEY, val TEXT);\
                 INSERT INTO secret VALUES (1, 'preserve-me');",
            )
            .expect("write legacy encrypted data");
        }

        let legacy_bytes = std::fs::read(&path).expect("read legacy database");
        assert!(
            !legacy_bytes.starts_with(b"SQLite format 3\0"),
            "fixture must use the fully-encrypted legacy header"
        );
        assert!(
            !legacy_bytes
                .windows("preserve-me".len())
                .any(|window| window == b"preserve-me"),
            "legacy database contents must be encrypted"
        );

        let wrong_key = SecretBox::init_with(|| [0x62_u8; 32]);
        assert!(
            open_encrypted(&path, &wrong_key).is_err(),
            "legacy database must reject the wrong key before migration"
        );
        assert_eq!(
            std::fs::read(&path).expect("read legacy database after wrong-key open"),
            legacy_bytes,
            "wrong-key open must not migrate or modify the legacy database"
        );

        {
            let conn = open_encrypted(&path, &key).expect("migrate legacy database");
            let value = conn
                .query_row("SELECT val FROM secret WHERE id = 1", &[], |row| {
                    Ok(row.column_text(0))
                })
                .expect("read migrated data");
            assert_eq!(value, "preserve-me");
        }

        let migrated_bytes = std::fs::read(&path).expect("read migrated database");
        assert!(
            migrated_bytes.starts_with(b"SQLite format 3\0"),
            "migration must expose the SQLite header"
        );
        assert_eq!(migrated_bytes[18], 2, "database must use WAL read mode");
        assert_eq!(migrated_bytes[19], 2, "database must use WAL write mode");
        assert_eq!(
            migrated_bytes[20], 32,
            "header must advertise the cipher's reserved bytes"
        );
        assert_ne!(
            migrated_bytes, legacy_bytes,
            "migration must rewrite the encrypted on-disk format"
        );
        assert!(
            !migrated_bytes
                .windows("preserve-me".len())
                .any(|window| window == b"preserve-me"),
            "migrated database contents must remain encrypted"
        );

        {
            let conn = open_encrypted(&path, &key).expect("reopen migrated database");
            let value = conn
                .query_row("SELECT val FROM secret WHERE id = 1", &[], |row| {
                    Ok(row.column_text(0))
                })
                .expect("read migrated data after reopen");
            assert_eq!(value, "preserve-me");
        }

        assert!(
            open_encrypted(&path, &wrong_key).is_err(),
            "migrated database must reject the wrong key"
        );
        assert_eq!(
            std::fs::read(&path).expect("read migrated database after wrong-key open"),
            migrated_bytes,
            "wrong-key open must not modify migrated data"
        );
    }

    #[test]
    fn test_integrity_check() {
        init_sqlite();
        let conn = Connection::open_in_memory().expect("open in-memory db");
        let ok = integrity_check(&conn).expect("check");
        assert!(ok);
    }

    #[test]
    fn test_cipher_plaintext_export_import_roundtrip() {
        init_sqlite();
        let dir = tempfile::tempdir().expect("create temp dir");
        let src_path = dir.path().join("source.sqlite");
        let dest_path = dir.path().join("backup.plain.sqlite");
        let restore_path = dir.path().join("restore.sqlite");
        let key = SecretBox::init_with(|| [0x11u8; 32]);

        {
            let conn = open_encrypted(&src_path, &key).expect("open src");
            conn.execute_batch(
                "CREATE TABLE widgets (id INTEGER PRIMARY KEY, val TEXT NOT NULL);",
            )
            .expect("create table");
            conn.execute(
                "INSERT INTO widgets (id, val) VALUES (?1, ?2)",
                params![1_i64, "alpha"],
            )
            .expect("insert");
            conn.execute(
                "INSERT INTO widgets (id, val) VALUES (?1, ?2)",
                params![2_i64, "beta"],
            )
            .expect("insert");

            export_plaintext_copy(&conn, &dest_path, &["widgets"]).expect("export");
        }

        {
            let conn = open_encrypted(&restore_path, &key).expect("open restore");
            conn.execute_batch(
                "CREATE TABLE widgets (id INTEGER PRIMARY KEY, val TEXT NOT NULL);",
            )
            .expect("create table");
            import_plaintext_copy(&conn, &dest_path, &["widgets"]).expect("import");

            let count: i64 = conn
                .query_row("SELECT COUNT(*) FROM widgets", &[], |row| {
                    Ok(row.column_i64(0))
                })
                .expect("count");
            assert_eq!(count, 2);

            let val = conn
                .query_row("SELECT val FROM widgets WHERE id = 2", &[], |row| {
                    Ok(row.column_text(0))
                })
                .expect("query");
            assert_eq!(val, "beta");
        }
    }

    #[test]
    fn test_cipher_import_rejects_non_empty_destination() {
        init_sqlite();
        let dir = tempfile::tempdir().expect("create temp dir");
        let src_path = dir.path().join("source.sqlite");
        let dest_path = dir.path().join("backup.plain.sqlite");
        let restore_path = dir.path().join("restore.sqlite");
        let key = SecretBox::init_with(|| [0x22u8; 32]);

        {
            let conn = open_encrypted(&src_path, &key).expect("open src");
            conn.execute_batch(
                "CREATE TABLE widgets (id INTEGER PRIMARY KEY, val TEXT NOT NULL);",
            )
            .expect("create table");
            conn.execute(
                "INSERT INTO widgets (id, val) VALUES (?1, ?2)",
                params![1_i64, "alpha"],
            )
            .expect("insert");
            export_plaintext_copy(&conn, &dest_path, &["widgets"]).expect("export");
        }

        let conn = open_encrypted(&restore_path, &key).expect("open restore");
        conn.execute_batch(
            "CREATE TABLE widgets (id INTEGER PRIMARY KEY, val TEXT NOT NULL);",
        )
        .expect("create table");
        conn.execute(
            "INSERT INTO widgets (id, val) VALUES (?1, ?2)",
            params![99_i64, "preexisting"],
        )
        .expect("insert");

        let err = import_plaintext_copy(&conn, &dest_path, &["widgets"])
            .expect_err("import should refuse non-empty destination");
        assert!(
            err.to_string().contains("non-empty table"),
            "expected non-empty-table error, got: {err}"
        );
    }
}