pagedb 0.1.0-beta.6

Encrypted, portable, embedded page store with B+ tree and segment-file surfaces.
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
//! Rekey integration tests: main-db and immutable-segment transitions.
//!
//! These live beside the implementation rather than in `tests/` because they
//! re-derive DEKs and re-open page envelopes directly to prove the epoch
//! transition rewrote what it claimed — key derivation and the page envelope
//! are both crate-internal.
#![allow(clippy::pedantic)]

use crate::options::RetainPolicy;
use crate::vfs::memory::MemVfs;
use crate::{
    Db, Evictable, OpenOptions, PagedbError, RealmId, RealmQuotas, SegmentKind, SegmentPageKind,
};

const PAGE: usize = 4096;
const KEK0: [u8; 32] = [0xAA; 32];
const REALM: RealmId = RealmId::new([0x01; 16]);

/// Open a fresh database at epoch 0.
async fn fresh_db() -> (MemVfs, Db<MemVfs>) {
    let vfs = MemVfs::new();
    let db = Db::open_internal(vfs.clone(), KEK0, PAGE, REALM)
        .await
        .unwrap();
    (vfs, db)
}

// ── Test 1 ─────────────────────────────────────────────────────────────────

/// After rekeying from epoch 0 to epoch 1, all keys inserted before the rekey
/// are still readable after reopening.
#[tokio::test(flavor = "current_thread")]
async fn rekey_main_db_only() {
    let (vfs, db) = fresh_db().await;

    // Write some data at epoch 0.
    {
        let mut tx = db.begin_write().await.unwrap();
        tx.put(b"alpha", b"value-alpha").await.unwrap();
        tx.put(b"beta", b"value-beta").await.unwrap();
        tx.put(b"gamma", b"value-gamma").await.unwrap();
        tx.commit().await.unwrap();
    }

    // Rekey to epoch 1.
    db.rekey_db(KEK0, 1).await.unwrap();

    // Reopen using epoch 1 credentials.
    drop(db);
    let db2 = Db::open_existing(vfs, KEK0, PAGE, REALM).await.unwrap();

    // All keys must be readable.
    let rx = db2.begin_read().await.unwrap();
    assert_eq!(
        rx.get(b"alpha").await.unwrap().as_deref(),
        Some(b"value-alpha".as_slice())
    );
    assert_eq!(
        rx.get(b"beta").await.unwrap().as_deref(),
        Some(b"value-beta".as_slice())
    );
    assert_eq!(
        rx.get(b"gamma").await.unwrap().as_deref(),
        Some(b"value-gamma".as_slice())
    );
    drop(rx);
}

/// Rekey must rewrite roots named only by retained commit-history metadata
/// before the source epoch is retired.
#[tokio::test(flavor = "current_thread")]
async fn rekey_preserves_retained_historical_snapshots_after_reopen() {
    let vfs = MemVfs::new();
    let options = OpenOptions::default().with_commit_history_retain(RetainPolicy::Unbounded);
    let db = Db::open_internal_with_options(vfs.clone(), KEK0, PAGE, REALM, options)
        .await
        .unwrap();
    db.set_realm_quotas(
        REALM,
        RealmQuotas {
            max_pages: Some(10),
            ..RealmQuotas::default()
        },
    )
    .await
    .unwrap();
    let historical_large = vec![0xA5; PAGE * 3];
    let latest_large = vec![0x5A; PAGE * 2];

    let first = {
        let mut tx = db.begin_write().await.unwrap();
        for index in 0u16..256 {
            tx.put(
                format!("history-key-{index:04}").as_bytes(),
                &index.to_le_bytes(),
            )
            .await
            .unwrap();
        }
        tx.put(b"versioned", b"before-rekey").await.unwrap();
        tx.put(b"historical-overflow", &historical_large)
            .await
            .unwrap();
        tx.commit().await.unwrap()
    };
    db.set_realm_quotas(
        REALM,
        RealmQuotas {
            max_pages: Some(20),
            ..RealmQuotas::default()
        },
    )
    .await
    .unwrap();
    {
        let mut tx = db.begin_write().await.unwrap();
        tx.put(b"versioned", b"latest").await.unwrap();
        tx.put(b"historical-overflow", &latest_large).await.unwrap();
        tx.commit().await.unwrap();
    }

    db.rekey_db(KEK0, 1).await.unwrap();
    drop(db);

    let reopened = Db::open_existing(vfs, KEK0, PAGE, REALM).await.unwrap();
    let historical = reopened.begin_read_at(first).await.unwrap();
    assert_eq!(
        historical.get(b"versioned").await.unwrap().as_deref(),
        Some(b"before-rekey".as_slice())
    );
    assert_eq!(
        historical
            .get(b"historical-overflow")
            .await
            .unwrap()
            .as_deref(),
        Some(historical_large.as_slice())
    );
    let first_index = 0u16.to_le_bytes();
    assert_eq!(
        historical
            .get(b"history-key-0000")
            .await
            .unwrap()
            .as_deref(),
        Some(first_index.as_slice())
    );
    assert!(matches!(
        historical.open_segment("absent").await,
        Err(PagedbError::NotFound)
    ));
    let latest = reopened.begin_read().await.unwrap();
    assert_eq!(
        latest.get(b"versioned").await.unwrap().as_deref(),
        Some(b"latest".as_slice())
    );
    assert_eq!(
        latest.get(b"historical-overflow").await.unwrap().as_deref(),
        Some(latest_large.as_slice())
    );
}

// ── Test 2 ─────────────────────────────────────────────────────────────────

/// Linked immutable segments are replaced under the target epoch while
/// preserving their logical payloads.
#[tokio::test(flavor = "current_thread")]
async fn rekey_with_segments() {
    let (_vfs, db) = fresh_db().await;

    // Create and seal two segments at epoch 0.
    let (meta1, extent) = {
        let mut w = db
            .create_segment(REALM, SegmentKind::Unspecified)
            .await
            .unwrap();
        w.append_page(SegmentPageKind::Data, b"segment-1-data")
            .await
            .unwrap();
        w.append_page(SegmentPageKind::Index, b"segment-1-index")
            .await
            .unwrap();
        w.append_page(SegmentPageKind::Overflow, b"segment-1-overflow")
            .await
            .unwrap();
        let extent = w
            .append_extent(&[b"segment-1-extent-a", b"segment-1-extent-b"])
            .await
            .unwrap();
        w.set_manifest(b"segment-1-manifest").unwrap();
        let m = w.seal().await.unwrap();
        let mut tx = db.begin_write().await.unwrap();
        tx.link_segment("z-seg", &m).await.unwrap();
        tx.commit().await.unwrap();
        (m, extent)
    };
    let meta2 = {
        let mut w = db
            .create_segment(REALM, SegmentKind::Unspecified)
            .await
            .unwrap();
        w.append_page(SegmentPageKind::Data, b"segment-2-page")
            .await
            .unwrap();
        let mut m = w.seal().await.unwrap();
        m.evictable = Evictable::Replaceable;
        let mut tx = db.begin_write().await.unwrap();
        tx.link_segment("a-seg", &m).await.unwrap();
        tx.commit().await.unwrap();
        m
    };

    assert_eq!(meta1.mk_epoch, 0);
    assert_eq!(meta2.mk_epoch, 0);

    let old_txn = db.begin_read().await.unwrap();
    let old_reader = old_txn.open_segment("z-seg").await.unwrap();
    db.rekey_db(KEK0, 1).await.unwrap();

    assert!(
        old_reader
            .read_page(1)
            .await
            .unwrap()
            .starts_with(b"segment-1-data")
    );
    let r1 = db.open_segment(REALM, "z-seg").await.unwrap();
    assert!(
        r1.read_page(1)
            .await
            .unwrap()
            .starts_with(b"segment-1-data")
    );
    assert_eq!(r1.meta().mk_epoch, 1);
    assert!(
        r1.read_page(2)
            .await
            .unwrap()
            .starts_with(b"segment-1-index")
    );
    assert!(
        r1.read_page(3)
            .await
            .unwrap()
            .starts_with(b"segment-1-overflow")
    );
    let migrated_extent = r1.find_extent(extent.start_page_id).await.unwrap();
    assert!(migrated_extent[0].starts_with(b"segment-1-extent-a"));
    assert!(migrated_extent[1].starts_with(b"segment-1-extent-b"));
    let r2 = db.open_segment(REALM, "a-seg").await.unwrap();
    assert!(
        r2.read_page(1)
            .await
            .unwrap()
            .starts_with(b"segment-2-page")
    );
    assert_eq!(r2.meta().mk_epoch, 1);
    assert_eq!(r2.meta().evictable, Evictable::Replaceable);
    drop(r2);
    drop(r1);
    drop(old_reader);
    drop(old_txn);

    let gc = db.gc_now().await.unwrap();
    assert!(gc.reclaimed_segments >= 1);
}

/// A page sealed under epoch 0 must NOT be decryptable when its AAD claims
/// epoch 1. This validates that per-page epoch routing is actually enforced
/// by the AEAD binding.
#[tokio::test(flavor = "current_thread")]
async fn rekey_aad_misroute_across_epoch() {
    use crate::CipherId;
    use crate::crypto::aad::{Aad, AadFields, MAIN_DB_SEGMENT_ID};
    use crate::crypto::cipher::Cipher;
    use crate::crypto::kdf::{derive_dek, derive_mk};
    use crate::crypto::nonce::MainDbNonceGen;
    use crate::pager::format::data_page::{open_data_page, seal_data_page};
    use crate::pager::format::page_kind::PageKind;

    let file_id = [0xABu8; 16];
    let kek_salt = [0xCDu8; 16];

    // Derive epoch 0 and epoch 1 master keys from the same KEK.
    let mk0 = derive_mk(&KEK0, &kek_salt, 0).unwrap();
    let mk1 = derive_mk(&KEK0, &kek_salt, 1).unwrap();

    let realm = REALM;
    let dek0 = derive_dek(&mk0, realm, &file_id).unwrap();
    let dek1 = derive_dek(&mk1, realm, &file_id).unwrap();
    let cipher0 = Cipher::new_aes_gcm(&dek0);
    let cipher1 = Cipher::new_aes_gcm(&dek1);

    // Seal a page under epoch 0.
    let mut nonce_gen = MainDbNonceGen::new(&file_id, 1_000_000);
    let nonce = nonce_gen.next_nonce().unwrap();
    let page_id: u64 = 10;
    let aad0 = Aad::from_fields(AadFields {
        cipher_id: CipherId::Aes256Gcm.as_byte(),
        page_kind: PageKind::BTreeLeaf.as_byte(),
        mk_epoch: 0,
        page_id,
        realm_id: realm,
        segment_id: MAIN_DB_SEGMENT_ID,
    });
    let mut buf = vec![0u8; PAGE];
    buf[24..29].copy_from_slice(b"hello");
    seal_data_page(&mut buf, PageKind::BTreeLeaf, 0, 0, &nonce, &aad0, &cipher0).unwrap();

    // Attempt to open with AAD claiming epoch 1 — must fail.
    let aad1 = Aad::from_fields(AadFields {
        cipher_id: CipherId::Aes256Gcm.as_byte(),
        page_kind: PageKind::BTreeLeaf.as_byte(),
        mk_epoch: 1,
        page_id,
        realm_id: realm,
        segment_id: MAIN_DB_SEGMENT_ID,
    });
    let mut buf_clone = buf.clone();
    let err = open_data_page(&mut buf_clone, &aad1, &cipher1).unwrap_err();
    assert!(
        matches!(err, PagedbError::ChecksumFailure),
        "epoch-misrouted decrypt must fail with ChecksumFailure, got: {:?}",
        err
    );

    // Confirm that correct AAD (epoch 0 with epoch 0 key) succeeds.
    let mut buf_ok = buf.clone();
    open_data_page(&mut buf_ok, &aad0, &cipher0).unwrap();
}

// ── Test 5 ─────────────────────────────────────────────────────────────────

/// After a completed rekey, all data written at epoch 0 remains readable at
/// epoch 1 even for large trees that span multiple B+ tree nodes. This
/// verifies that the pager's epoch-routing path correctly handles mixed-epoch
/// pages that are in cache (populated by rekey_walk) vs pages read fresh.
#[tokio::test(flavor = "current_thread")]
async fn mixed_epoch_pages_readable() {
    let (vfs, db) = fresh_db().await;

    // Write enough data to guarantee multiple B+ tree pages.
    {
        let mut tx = db.begin_write().await.unwrap();
        for i in 0u32..64 {
            let key = format!("mixed-key-{:04}", i);
            let val = format!("mixed-val-{:04}", i);
            tx.put(key.as_bytes(), val.as_bytes()).await.unwrap();
        }
        tx.commit().await.unwrap();
    }

    // Perform the rekey.
    db.rekey_db(KEK0, 1).await.unwrap();

    // All data must be readable without reopening (in-memory epoch switch worked).
    let rx = db.begin_read().await.unwrap();
    for i in 0u32..64 {
        let key = format!("mixed-key-{:04}", i);
        let expected = format!("mixed-val-{:04}", i);
        let got = rx.get(key.as_bytes()).await.unwrap();
        assert_eq!(
            got.as_deref(),
            Some(expected.as_bytes()),
            "key {} missing after rekey",
            key
        );
    }
    drop(rx);

    // Also confirm the db reopens cleanly and data is persisted correctly.
    drop(db);
    let db2 = Db::open_existing(vfs, KEK0, PAGE, REALM).await.unwrap();
    let rx2 = db2.begin_read().await.unwrap();
    for i in 0u32..64 {
        let key = format!("mixed-key-{:04}", i);
        let expected = format!("mixed-val-{:04}", i);
        let got = rx2.get(key.as_bytes()).await.unwrap();
        assert_eq!(
            got.as_deref(),
            Some(expected.as_bytes()),
            "key {} missing after reopen",
            key
        );
    }
    drop(rx2);
}

/// An existing reader keeps its pinned logical snapshot even after rekey
/// rewrites durable main-db pages and clean cache entries are evicted.
#[tokio::test(flavor = "current_thread")]
async fn rekey_preserves_preexisting_main_reader_snapshot_after_cache_evict() {
    let (_vfs, db) = fresh_db().await;
    let old_large_value = vec![0xA5; PAGE];
    {
        let mut tx = db.begin_write().await.unwrap();
        tx.put(b"versioned", b"before").await.unwrap();
        tx.put(b"large", &old_large_value).await.unwrap();
        tx.commit().await.unwrap();
    }

    let reader = db.begin_read().await.unwrap();
    {
        let mut tx = db.begin_write().await.unwrap();
        tx.put(b"versioned", b"after").await.unwrap();
        tx.put(b"large", b"replacement").await.unwrap();
        tx.commit().await.unwrap();
    }

    db.rekey_db(KEK0, 1).await.unwrap();
    db.evict_main_pages(REALM);

    assert_eq!(
        reader.get(b"versioned").await.unwrap().as_deref(),
        Some(b"before".as_slice())
    );
    assert_eq!(
        reader.get(b"large").await.unwrap().as_deref(),
        Some(old_large_value.as_slice())
    );
}

/// Rekey must re-encrypt the durable free-list chain and every reusable page
/// it names; otherwise physical integrity checks cannot authenticate those
/// pages after the source epoch is retired.
#[tokio::test(flavor = "current_thread")]
async fn rekey_preserves_durable_free_list_across_reopen() {
    let vfs = MemVfs::new();
    let options = OpenOptions::default().with_commit_history_retain(RetainPolicy::Disabled);
    let db = Db::open_internal_with_options(vfs.clone(), KEK0, PAGE, REALM, options.clone())
        .await
        .unwrap();
    let mut insert = db.begin_write().await.unwrap();
    for index in 0..300_u32 {
        insert
            .put(format!("free-{index:05}").as_bytes(), &[0x7A; 128])
            .await
            .unwrap();
    }
    insert.commit().await.unwrap();
    let mut delete = db.begin_write().await.unwrap();
    for index in 0..250_u32 {
        delete
            .delete(format!("free-{index:05}").as_bytes())
            .await
            .unwrap();
    }
    delete.commit().await.unwrap();
    assert!(db.stats().await.unwrap().free_list_pending_entries > 0);

    db.rekey_db(KEK0, 1).await.unwrap();
    drop(db);

    let reopened = Db::open_existing_with_options(vfs, KEK0, PAGE, REALM, options)
        .await
        .unwrap();
    assert!(reopened.stats().await.unwrap().free_list_pending_entries > 0);
    let report = crate::recovery::deep_walk::run_deep_walk(&reopened)
        .await
        .unwrap();
    assert!(report.is_clean(), "free-list rekey report: {report:?}");
}

/// Epochs are monotonic. Rejecting the current or an older epoch must leave
/// the live store usable and must not persist a rekey intent.
#[tokio::test(flavor = "current_thread")]
async fn rekey_rejects_non_advancing_epoch() {
    let (vfs, db) = fresh_db().await;
    {
        let mut tx = db.begin_write().await.unwrap();
        tx.put(b"stable", b"value").await.unwrap();
        tx.commit().await.unwrap();
    }

    assert!(matches!(
        db.rekey_db(KEK0, 0).await,
        Err(PagedbError::RekeyStateInvalid { .. })
    ));
    assert_eq!(
        db.begin_read()
            .await
            .unwrap()
            .get(b"stable")
            .await
            .unwrap()
            .as_deref(),
        Some(b"value".as_slice())
    );

    db.rekey_db(KEK0, 2).await.unwrap();
    assert!(matches!(
        db.rekey_db(KEK0, 1).await,
        Err(PagedbError::RekeyStateInvalid { .. })
    ));
    drop(db);

    let reopened = Db::open_existing(vfs, KEK0, PAGE, REALM).await.unwrap();
    assert_eq!(
        reopened
            .begin_read()
            .await
            .unwrap()
            .get(b"stable")
            .await
            .unwrap()
            .as_deref(),
        Some(b"value".as_slice())
    );
}

/// A catalog too large to fit one page must be re-sealed in full.
///
/// The catalog is the one reader-visible root that cannot be re-sealed before
/// the target header is published — it carries the intent that admits recovery
/// from a stale header. Only the pages a rekey happens to write through get
/// re-sealed by that write, so a catalog spanning several pages is where a
/// missing catalog traversal shows up: everything off the written path stays
/// sealed under an epoch that retirement then destroys.
fn counter_name(index: u32) -> String {
    // Long names on purpose: the catalog must span more pages than any single
    // copy-on-write path through it touches.
    format!("catalog-counter-{index:05}-{}", "n".repeat(200))
}

#[tokio::test(flavor = "current_thread")]
async fn rekey_reseals_a_catalog_larger_than_one_page() {
    let vfs = MemVfs::new();
    // Retention disabled on purpose: with history retained, the newest retained
    // row names the live catalog root, so the retained-root walk re-seals the
    // catalog incidentally and hides whether the rekey covers it on its own.
    let options = OpenOptions::default().with_commit_history_retain(RetainPolicy::Disabled);
    let db = Db::open_internal_with_options(vfs.clone(), KEK0, PAGE, REALM, options.clone())
        .await
        .unwrap();

    // Enough counter rows to push the catalog tree past a single page.
    {
        let mut tx = db.begin_write().await.unwrap();
        for index in 0..400_u32 {
            let mut counter = tx.counter(&counter_name(index)).unwrap();
            counter.set(u64::from(index) + 1).await.unwrap();
            drop(counter);
        }
        tx.commit().await.unwrap();
    }
    let catalog_pages_before = db.stats().await.unwrap().main_db_next_page_id;
    assert!(
        catalog_pages_before > 8,
        "test setup must build a multi-page catalog, got {catalog_pages_before} pages"
    );

    db.rekey_db(KEK0, 1).await.unwrap();
    drop(db);

    let reopened = Db::open_existing_with_options(vfs, KEK0, PAGE, REALM, options)
        .await
        .unwrap();
    let report = crate::recovery::deep_walk::run_deep_walk(&reopened)
        .await
        .unwrap();
    assert!(
        report.is_clean(),
        "multi-page catalog rekey report: {report:?}"
    );

    // Every counter must still decode under the target epoch.
    let mut tx = reopened.begin_write().await.unwrap();
    for index in [0_u32, 199, 399] {
        let counter = tx.counter(&counter_name(index)).unwrap();
        assert_eq!(counter.get().await.unwrap(), u64::from(index) + 1);
        drop(counter);
    }
    tx.commit().await.unwrap();
}

/// Rekey must not abandon the pages its own catalog transitions supersede.
///
/// Each durable stage rewrites the catalog copy-on-write. Without routing the
/// superseded pages onto the free list they are unreachable from every root and
/// absent from the free list — a permanent leak that grows with every rekey,
/// and one the source epoch's retirement makes unauthenticatable.
#[tokio::test(flavor = "current_thread")]
async fn rekey_leaves_no_leaked_pages() {
    let vfs = MemVfs::new();
    let db = Db::open_internal(vfs.clone(), KEK0, PAGE, REALM)
        .await
        .unwrap();
    {
        let mut tx = db.begin_write().await.unwrap();
        for index in 0..200_u32 {
            tx.put(format!("leak-{index:05}").as_bytes(), &[0x5C; 96])
                .await
                .unwrap();
        }
        tx.commit().await.unwrap();
    }

    for epoch in 1..=3_u64 {
        db.rekey_db(KEK0, epoch).await.unwrap();
    }
    drop(db);

    let reopened = Db::open_existing(vfs, KEK0, PAGE, REALM).await.unwrap();
    let report = crate::recovery::deep_walk::run_deep_walk(&reopened)
        .await
        .unwrap();
    assert!(report.is_clean(), "repeated rekey report: {report:?}");
    assert!(
        report.orphan_page_ids.is_empty(),
        "rekey leaked {} pages: {:?}",
        report.orphan_page_ids.len(),
        report.orphan_page_ids
    );
}