keres 0.7.1

A memory-safe, high-performance Rust library for modern file encryption and secure vaults.
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
//! Vault integration tests.

use std::path::Path;

use fs4::FileExt;
use keres::{BatchOp, Error, Padding, Vault, WriteOutcome};
use tempfile::TempDir;

fn make_vault(tag: &str) -> (TempDir, Vault) {
    let dir = tempfile::Builder::new().prefix(tag).tempdir().unwrap();
    let vault_dir = dir.path().join("vault");
    let mut v = Vault::new(vault_dir);
    v.init("master-password").unwrap();
    assert!(v.is_unlocked());
    (dir, v)
}

#[test]
fn init_twice_fails() {
    let (_d, mut v) = make_vault("init2");
    assert!(matches!(v.init("x"), Err(Error::VaultExists)));
}

#[test]
fn init_refuses_to_clobber_a_config_less_vault_with_an_index() {
    let (_d, mut v) = make_vault("initindex");
    std::fs::remove_file(v.path().join("vault.config")).unwrap();
    assert!(matches!(v.init("x"), Err(Error::VaultExists)));
}

#[test]
fn locked_vault_rejects_operations() {
    let (_d, mut v) = make_vault("locked");
    v.lock();
    assert!(!v.is_unlocked());
    let f = v.path().join("nope.txt");
    std::fs::write(&f, b"x").unwrap();
    assert!(matches!(v.add(&f, None), Err(Error::VaultLocked)));
    assert!(matches!(v.list(), Err(Error::VaultLocked)));
}

#[test]
fn concurrent_operations_fail_fast() {
    let (_d, mut v) = make_vault("busy");
    let parent = v.path().parent().unwrap().to_path_buf();
    let src = parent.join("busy.txt");
    std::fs::write(&src, b"payload").unwrap();

    let lock_path = v.path().join("vault.lock");
    let held = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(&lock_path)
        .unwrap();
    FileExt::try_lock(&held).unwrap();

    assert!(matches!(v.add(&src, None), Err(Error::VaultBusy)));
    assert!(matches!(v.list(), Err(Error::VaultBusy)));
    assert!(matches!(
        v.extract("busy.txt", &parent.join("out")),
        Err(Error::VaultBusy)
    ));
    assert!(matches!(
        v.change_password("master-password", "new-password"),
        Err(Error::VaultBusy)
    ));
    assert!(matches!(v.verify("master-password"), Err(Error::VaultBusy)));

    FileExt::unlock(&held).unwrap();
    drop(held);

    // Once the lock is free, the same handle works again.
    v.add(&src, None).unwrap();
    assert_eq!(v.list().unwrap().len(), 1);
}

#[test]
fn unlock_wrong_password_is_authentication_error() {
    let (_d, mut v) = make_vault("wrongunlock");
    v.lock();
    assert!(matches!(v.unlock("bad"), Err(Error::Authentication)));
}

#[test]
fn file_roundtrip() {
    let (_d, mut v) = make_vault("file");
    let src = v.path().parent().unwrap().join("secret.txt");
    std::fs::write(&src, b"hidden payload").unwrap();

    v.add(&src, None).unwrap();
    let entries = v.list().unwrap();
    assert_eq!(entries.len(), 1);
    assert_eq!(entries[0].name, "secret.txt");
    assert_eq!(entries[0].size, 14); // b"hidden payload".len()

    let out = v.path().parent().unwrap().join("out.txt");
    v.extract("secret.txt", &out).unwrap();
    assert_eq!(std::fs::read(&out).unwrap(), b"hidden payload");

    // Duplicate add must not silently overwrite.
    assert!(matches!(v.add(&src, None), Err(Error::EntryExists(_))));
}

#[test]
fn directory_tree_roundtrip_and_recursive_remove() {
    let (_d, mut v) = make_vault("tree");
    let root = v.path().parent().unwrap().join("photos");
    std::fs::create_dir_all(root.join("sub")).unwrap();
    std::fs::write(root.join("a.png"), b"A").unwrap();
    std::fs::write(root.join("sub").join("b.png"), b"BB").unwrap();

    v.add(&root, Some("photos")).unwrap();
    assert_eq!(v.list().unwrap().len(), 1); // only top-level shown

    // Extract recreates the whole subtree under dest.
    let dest = v.path().parent().unwrap().join("restored");
    v.extract("photos", &dest).unwrap();
    assert_eq!(std::fs::read(dest.join("a.png")).unwrap(), b"A");
    assert_eq!(
        std::fs::read(dest.join("sub").join("b.png")).unwrap(),
        b"BB"
    );

    // Remove deletes every blob of the subtree.
    v.remove("photos").unwrap();
    assert!(v.list().unwrap().is_empty());

    // No orphan blobs: data dir holds only index metadata files.
    let data_dir = v.path().join("d");
    let leftovers: Vec<_> = std::fs::read_dir(&data_dir)
        .unwrap()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().is_file() && !e.file_name().to_string_lossy().starts_with('.'))
        .collect();
    assert!(leftovers.is_empty(), "orphaned files: {leftovers:?}");
    for entry in std::fs::read_dir(&data_dir).unwrap().filter_map(|e| e.ok()) {
        if entry.path().is_dir() {
            assert!(
                std::fs::read_dir(entry.path()).unwrap().next().is_none(),
                "empty shard dirs should be cleaned up"
            );
        }
    }
}

#[test]
fn symlinks_are_skipped_on_add() {
    #[cfg(unix)]
    {
        let (_d, mut v) = make_vault("symlink");
        let real = v.path().parent().unwrap().join("real.txt");
        std::fs::write(&real, b"data").unwrap();
        let link = v.path().parent().unwrap().join("link.txt");
        std::os::unix::fs::symlink("/etc/hostname", &link).unwrap();

        // Adding a bare symlink is rejected (or, if canonicalized to a
        // regular file, added as a copy — never followed out of the tree).
        let _ = v.add(&link, Some("innocent"));
        // Directory containing a symlink child.
        let tree = v.path().parent().unwrap().join("tree");
        std::fs::create_dir_all(&tree).unwrap();
        std::fs::write(tree.join("ok.txt"), b"fine").unwrap();
        std::os::unix::fs::symlink("/etc/passwd", tree.join("evil.txt")).unwrap();
        v.add(&tree, None).unwrap();
        v.lock();
        let _ = v;
    }
}

#[test]
fn verify_reports_missing_and_corrupted() {
    let (dir, mut v) = make_vault("verify");
    let f1 = dir.path().join("one.txt");
    let f2 = dir.path().join("two.txt");
    std::fs::write(&f1, b"1").unwrap();
    std::fs::write(&f2, b"22").unwrap();
    v.add(&f1, None).unwrap();
    v.add(&f2, None).unwrap();
    drop(v);

    // Exactly two blobs exist: corrupt one on disk, delete the other.
    let mut blobs = walk_blobs(&dir.path().join("vault"));
    assert_eq!(blobs.len(), 2);
    blobs.sort();

    let bytes = std::fs::read(&blobs[0]).unwrap();
    let corrupted_bytes: Vec<u8> = bytes
        .iter()
        .enumerate()
        .map(|(i, b)| if i == 40 { b ^ 0xFF } else { *b })
        .collect();
    std::fs::write(&blobs[0], &corrupted_bytes).unwrap();
    std::fs::remove_file(&blobs[1]).unwrap();

    let report = Vault::new(dir.path().join("vault"))
        .verify("master-password")
        .unwrap();
    assert_eq!(report.total_entries, 2);
    assert_eq!(report.missing.len() + report.corrupted.len(), 2);
    assert_eq!(report.verified, 0);
}

#[test]
fn replace_updates_file_contents() {
    let (dir, mut v) = make_vault("replace");
    let src = dir.path().join("doc.txt");
    std::fs::write(&src, b"version one").unwrap();
    v.add(&src, None).unwrap();

    let updated = b"version two, longer";
    std::fs::write(&src, updated).unwrap();
    v.replace(&src, "doc.txt").unwrap();

    let out = dir.path().join("out.txt");
    v.extract("doc.txt", &out).unwrap();
    assert_eq!(std::fs::read(&out).unwrap(), updated);
    assert_eq!(v.list().unwrap()[0].size, updated.len() as u64);
}

#[test]
fn replace_missing_entry_fails() {
    let (dir, mut v) = make_vault("replacemissing");
    let src = dir.path().join("doc.txt");
    std::fs::write(&src, b"x").unwrap();
    assert!(matches!(
        v.replace(&src, "nope.txt"),
        Err(Error::EntryNotFound(_))
    ));
}

#[test]
fn replace_directory_fails() {
    let (dir, mut v) = make_vault("replacedir");
    let tree = dir.path().join("tree");
    std::fs::create_dir_all(&tree).unwrap();
    std::fs::write(tree.join("a.txt"), b"a").unwrap();
    v.add(&tree, None).unwrap();

    let src = dir.path().join("replacement.txt");
    std::fs::write(&src, b"x").unwrap();
    assert!(matches!(v.replace(&src, "tree"), Err(Error::Operation(_))));
}

#[test]
fn prune_removes_only_unreferenced_files() {
    let (dir, mut v) = make_vault("prune");
    let src = dir.path().join("keep.txt");
    std::fs::write(&src, b"keep me").unwrap();
    v.add(&src, None).unwrap();

    let data = v.path().join("d");
    let orphan_dir = data.join("zz");
    std::fs::create_dir_all(&orphan_dir).unwrap();
    let orphan = orphan_dir.join("orphan.enc");
    std::fs::write(&orphan, b"ciphertext-ish").unwrap();
    let stale_tmp = data.join(".x.tmp-123-deadbeef");
    std::fs::write(&stale_tmp, b"stale").unwrap();
    let unrelated = data.join("README");
    std::fs::write(&unrelated, b"not ours").unwrap();

    let removed = v.prune_orphans().unwrap();
    assert_eq!(removed, 2);
    assert!(!orphan.exists());
    assert!(!stale_tmp.exists());
    assert!(unrelated.exists(), "prune must not touch unrelated files");

    let out = dir.path().join("out.txt");
    v.extract("keep.txt", &out).unwrap();
    assert_eq!(std::fs::read(&out).unwrap(), b"keep me");
}

#[test]
fn prune_refuses_without_manifest() {
    let (_d, mut v) = make_vault("prunenomanifest");
    std::fs::remove_file(v.path().join("d").join(".manifest.enc")).unwrap();
    assert!(matches!(v.prune_orphans(), Err(Error::InvalidVault)));
}

#[test]
fn rename_preserves_blob_identity() {
    let (dir, mut v) = make_vault("rename");
    let src = dir.path().join("old.txt");
    std::fs::write(&src, b"same bytes").unwrap();
    v.add(&src, None).unwrap();
    let before = walk_blobs(&v.path().join("d"));
    assert_eq!(before.len(), 1);

    v.rename("old.txt", "new.txt").unwrap();

    assert_eq!(
        walk_blobs(&v.path().join("d")),
        before,
        "rename must not move or rewrite the blob"
    );
    assert!(matches!(
        v.extract("old.txt", &dir.path().join("gone")),
        Err(Error::EntryNotFound(_))
    ));
    let out = dir.path().join("out.txt");
    v.extract("new.txt", &out).unwrap();
    assert_eq!(std::fs::read(&out).unwrap(), b"same bytes");
    let names: Vec<_> = v.list().unwrap().into_iter().map(|e| e.name).collect();
    assert_eq!(names, vec!["new.txt"]);
}

#[test]
fn rename_directory_moves_subtree() {
    let (dir, mut v) = make_vault("renamedir");
    let tree = dir.path().join("tree");
    std::fs::create_dir_all(tree.join("sub")).unwrap();
    std::fs::write(tree.join("sub/file.txt"), b"nested").unwrap();
    v.add(&tree, None).unwrap();

    v.rename("tree", "archive").unwrap();
    assert!(matches!(
        v.extract("tree", &dir.path().join("gone")),
        Err(Error::EntryNotFound(_))
    ));

    let out = dir.path().join("restored");
    v.extract("archive", &out).unwrap();
    assert_eq!(std::fs::read(out.join("sub/file.txt")).unwrap(), b"nested");
}

#[test]
fn rename_to_existing_name_fails() {
    let (dir, mut v) = make_vault("renamecollision");
    let a = dir.path().join("a.txt");
    let b = dir.path().join("b.txt");
    std::fs::write(&a, b"a").unwrap();
    std::fs::write(&b, b"b").unwrap();
    v.add(&a, None).unwrap();
    v.add(&b, None).unwrap();

    assert!(matches!(
        v.rename("a.txt", "b.txt"),
        Err(Error::EntryExists(_))
    ));
}

fn walk_blobs(dir: &Path) -> Vec<std::path::PathBuf> {
    let mut out = Vec::new();
    for e in std::fs::read_dir(dir).into_iter().flatten().flatten() {
        let p = e.path();
        if p.is_dir() {
            out.extend(walk_blobs(&p));
        } else {
            let name = p
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_default();
            if name.ends_with(".enc") && name != ".manifest.enc" {
                out.push(p);
            }
        }
    }
    out
}

#[test]
fn change_password_rotates_kek_only() {
    let (_d, mut v) = make_vault("rotatepw");
    let src = v.path().parent().unwrap().join("f.txt");
    std::fs::write(&src, b"data").unwrap();
    v.add(&src, None).unwrap();
    v.lock();

    // Wrong old password rejected.
    assert!(Vault::new(v.path().to_path_buf())
        .change_password("WRONG", "new")
        .is_err());

    let mut fresh = Vault::new(v.path().to_path_buf());
    fresh
        .change_password("master-password", "brand-new")
        .unwrap();

    let mut reopened = Vault::new(v.path().to_path_buf());
    assert!(reopened.unlock("master-password").is_err());
    reopened.unlock("brand-new").unwrap();
    assert_eq!(reopened.list().unwrap().len(), 1);
}

#[test]
fn traversal_names_rejected() {
    let (_d, mut v) = make_vault("traversal");
    let src = v.path().parent().unwrap().join("f.txt");
    std::fs::write(&src, b"x").unwrap();

    for evil in ["../escape", "a/b/../c", "/abs", "..", "."] {
        let r = v.add(&src, Some(evil));
        assert!(
            matches!(r, Err(Error::InvalidEntryName(_))),
            "{evil}: {r:?}"
        );
    }
}

#[test]
fn persistence_across_reopen() {
    let (dir, mut v) = make_vault("reopen");
    let src = dir.path().join("persist.bin");
    let big: Vec<u8> = (0..150_000u32).map(|i| i as u8).collect(); // multichunk
    std::fs::write(&src, &big).unwrap();
    v.add(&src, None).unwrap();
    drop(v);

    let mut v2 = Vault::new(dir.path().join("vault"));
    v2.unlock("master-password").unwrap();
    let out = dir.path().join("back.bin");
    v2.extract("persist.bin", &out).unwrap();
    assert_eq!(std::fs::read(&out).unwrap(), big);
}

#[test]
fn keyslots_add_change_and_revoke() {
    let (_d, mut v) = make_vault("keyslots");
    let src = v.path().parent().unwrap().join("f.txt");
    std::fs::write(&src, b"data").unwrap();
    v.add(&src, None).unwrap();

    let original = v.keyslots().unwrap();
    assert_eq!(original.len(), 1);
    let original_id = original[0].id.clone();

    // The authorizing passphrase must be real, and a duplicate slot is
    // pointless.
    assert!(matches!(
        v.add_keyslot("WRONG", "second"),
        Err(Error::Authentication)
    ));
    assert!(matches!(
        v.add_keyslot("master-password", "master-password"),
        Err(Error::Operation(_))
    ));

    let second = v.add_keyslot("master-password", "second-pass").unwrap();
    let slots = v.keyslots().unwrap();
    assert_eq!(slots.len(), 2);
    assert!(slots.iter().any(|slot| slot.id == second.id));

    v.lock();
    let mut other = Vault::new(v.path().to_path_buf());
    other.unlock("second-pass").unwrap();
    assert_eq!(other.list().unwrap().len(), 1);
    drop(other);

    // Changing one slot leaves the other passphrase and the contents alone.
    let mut with_second = Vault::new(v.path().to_path_buf());
    with_second
        .change_password("second-pass", "second-new")
        .unwrap();
    assert!(
        !with_second.is_unlocked(),
        "change_password must not unlock"
    );
    assert!(Vault::new(v.path().to_path_buf())
        .unlock("second-pass")
        .is_err());
    assert!(Vault::new(v.path().to_path_buf())
        .unlock("second-new")
        .is_ok());
    assert!(Vault::new(v.path().to_path_buf())
        .unlock("master-password")
        .is_ok());

    // Revoking with the target's own passphrase is refused: the authorizing
    // slot must survive.
    let mut revoker = Vault::new(v.path().to_path_buf());
    assert!(matches!(
        revoker.remove_keyslot(&second.id, "second-new"),
        Err(Error::Operation(_))
    ));

    revoker
        .remove_keyslot(&second.id, "master-password")
        .unwrap();
    assert!(Vault::new(v.path().to_path_buf())
        .unlock("second-new")
        .is_err());

    let mut remaining = Vault::new(v.path().to_path_buf());
    remaining.unlock("master-password").unwrap();
    assert_eq!(remaining.list().unwrap().len(), 1);
    assert_eq!(remaining.keyslots().unwrap()[0].id, original_id);
}

#[test]
fn keyslot_removal_cannot_brick_the_vault() {
    let (_d, mut v) = make_vault("slotlast");
    let original_id = v.keyslots().unwrap()[0].id.clone();

    // A single slot cannot be removed: no other passphrase can authorize it.
    assert!(matches!(
        v.remove_keyslot(&original_id, "master-password"),
        Err(Error::Operation(_))
    ));

    let second = v.add_keyslot("master-password", "second").unwrap();
    v.remove_keyslot(&second.id, "master-password").unwrap();

    let slots = v.keyslots().unwrap();
    assert_eq!(slots.len(), 1);
    assert_eq!(slots[0].id, original_id);
    assert!(matches!(
        v.remove_keyslot(&original_id, "master-password"),
        Err(Error::Operation(_))
    ));
}

#[test]
fn recovery_resets_to_a_single_keyslot() {
    let (_d, mut v) = make_vault("slotrecover");
    v.add_keyslot("master-password", "second").unwrap();
    let code = v.create_recovery_kit("master-password").unwrap();
    let dir = v.path().to_path_buf();
    drop(v);

    std::fs::remove_file(dir.join("vault.config")).unwrap();
    let mut recovered = Vault::new(dir.clone());
    recovered.recover(code.expose(), "after-recovery").unwrap();

    // Recovery normalizes the config to one fresh slot; extra passphrases are
    // gone and must be re-added deliberately.
    assert_eq!(recovered.keyslots().unwrap().len(), 1);
    assert!(Vault::new(dir.clone()).unlock("master-password").is_err());
    assert!(Vault::new(dir.clone()).unlock("second").is_err());
    assert!(Vault::new(dir).unlock("after-recovery").is_ok());
}

#[test]
fn mutations_are_journaled_in_the_index_log() {
    let (dir, mut v) = make_vault("indexlog");
    let parent = v.path().parent().unwrap().to_path_buf();
    let snapshot_path = v.path().join("d").join(".manifest.enc");
    let log_path = v.path().join("d").join(".index.log");

    // `init` writes only the snapshot.
    assert!(snapshot_path.is_file());
    assert!(!log_path.exists());
    let snapshot = std::fs::read(&snapshot_path).unwrap();

    let a = parent.join("a.txt");
    std::fs::write(&a, b"alpha").unwrap();
    v.add(&a, None).unwrap();
    let b = parent.join("b.txt");
    std::fs::write(&b, b"beta").unwrap();
    v.add(&b, None).unwrap();

    // Mutations append to the log instead of rewriting the snapshot.
    assert!(log_path.is_file());
    assert_eq!(std::fs::read(&snapshot_path).unwrap(), snapshot);

    v.lock();
    drop(v);
    let mut reopened = Vault::new(dir.path().join("vault"));
    reopened.unlock("master-password").unwrap();
    let names: Vec<_> = reopened
        .list()
        .unwrap()
        .into_iter()
        .map(|e| e.name)
        .collect();
    assert_eq!(names, vec!["a.txt", "b.txt"]);

    reopened.remove("a.txt").unwrap();
    reopened.lock();
    drop(reopened);

    let mut again = Vault::new(dir.path().join("vault"));
    again.unlock("master-password").unwrap();
    let names: Vec<_> = again.list().unwrap().into_iter().map(|e| e.name).collect();
    assert_eq!(names, vec!["b.txt"]);
}

#[test]
fn conditional_write_conflicts_on_stale_version() {
    let (dir, mut v) = make_vault("cond");
    let src = dir.path().join("a.txt");
    std::fs::write(&src, b"alpha").unwrap();
    v.add(&src, None).unwrap();

    let version = v.version().unwrap();
    // A second, intervening mutation advances the version.
    let src2 = dir.path().join("b.txt");
    std::fs::write(&src2, b"beta").unwrap();
    v.add(&src2, None).unwrap();

    // Stale version -> conflict, nothing written.
    let src3 = dir.path().join("c.txt");
    std::fs::write(&src3, b"gamma").unwrap();
    let outcome = v.add_if(&src3, None, Some(version)).unwrap();
    assert!(matches!(outcome, WriteOutcome::Conflict { .. }));

    // Fresh version -> applied.
    let current = v.version().unwrap();
    let outcome = v.add_if(&src3, None, Some(current)).unwrap();
    assert!(matches!(outcome, WriteOutcome::Applied { .. }));
    assert!(v.list().unwrap().iter().any(|e| e.name == "c.txt"));

    // if-none-match: adding an existing name conflicts.
    let outcome = v.add_if(&src3, Some("c.txt"), None).unwrap();
    assert!(matches!(outcome, WriteOutcome::Conflict { .. }));
}

#[test]
fn replace_if_requires_entry() {
    let (dir, mut v) = make_vault("replaceif");
    let src = dir.path().join("a.txt");
    std::fs::write(&src, b"alpha").unwrap();
    v.add(&src, None).unwrap();

    let version = v.version().unwrap();
    let src2 = dir.path().join("a2.txt");
    std::fs::write(&src2, b"alpha2").unwrap();
    let outcome = v.replace_if(&src2, "a.txt", Some(version)).unwrap();
    assert!(matches!(outcome, WriteOutcome::Applied { .. }));

    // Missing entry -> conflict.
    let outcome = v.replace_if(&src2, "nope.txt", None).unwrap();
    assert!(matches!(outcome, WriteOutcome::Conflict { .. }));
}

#[test]
fn apply_batch_commits_once() {
    let (dir, mut v) = make_vault("batch");
    let a = dir.path().join("a.txt");
    let b = dir.path().join("b.txt");
    std::fs::write(&a, b"alpha").unwrap();
    std::fs::write(&b, b"beta").unwrap();

    v.apply_batch(&[
        BatchOp::Add {
            source: a.clone(),
            name: None,
        },
        BatchOp::Add {
            source: b.clone(),
            name: Some("renamed.txt".into()),
        },
    ])
    .unwrap();

    let names: Vec<_> = v.list().unwrap().into_iter().map(|e| e.name).collect();
    assert!(names.contains(&"a.txt".to_string()));
    assert!(names.contains(&"renamed.txt".to_string()));
}

#[test]
fn erase_removes_and_rotates() {
    let (dir, mut v) = make_vault("erase");
    let src = dir.path().join("secret.txt");
    std::fs::write(&src, b"secret").unwrap();
    v.add(&src, None).unwrap();
    let src2 = dir.path().join("keep.txt");
    std::fs::write(&src2, b"keep").unwrap();
    v.add(&src2, None).unwrap();

    v.erase("secret.txt", "new-password").unwrap();
    assert!(!v.is_unlocked());

    let mut v2 = Vault::new(v.path().to_path_buf());
    v2.unlock("new-password").unwrap();
    let names: Vec<_> = v2.list().unwrap().into_iter().map(|e| e.name).collect();
    assert_eq!(names, vec!["keep.txt"]);
    // Old password no longer works.
    assert!(Vault::new(v.path().to_path_buf())
        .unlock("master-password")
        .is_err());
}

#[test]
fn padding_round_trips_through_vault() {
    let dir = tempfile::tempdir().unwrap();
    let mut v = Vault::new(dir.path().join("vault"));
    v.init_with_padding("master-password", Padding::Adaptive)
        .unwrap();
    assert_eq!(v.padding().unwrap(), Padding::Adaptive);

    let src = dir.path().join("f.txt");
    let data = vec![0xABu8; 1000];
    std::fs::write(&src, &data).unwrap();
    v.add(&src, None).unwrap();

    let out = dir.path().join("out.txt");
    v.extract("f.txt", &out).unwrap();
    assert_eq!(std::fs::read(&out).unwrap(), data);

    // Changing policy persists.
    v.set_padding(Padding::fixed(8192).unwrap()).unwrap();
    assert_eq!(v.padding().unwrap(), Padding::fixed(8192).unwrap());
}

#[cfg(unix)]
#[test]
fn remove_commits_the_index_before_deleting_blobs() {
    use std::os::unix::fs::PermissionsExt;

    let (dir, mut v) = make_vault("removeorder");
    let src = dir.path().join("gone.txt");
    std::fs::write(&src, b"bye").unwrap();
    v.add(&src, None).unwrap();

    let blobs = walk_blobs(&v.path().join("d"));
    assert_eq!(blobs.len(), 1);
    let shard = blobs[0].parent().unwrap().to_path_buf();

    // Make the blob's shard directory unwritable so unlinking fails, while the
    // index log (written under `d/`) still succeeds.
    let mut perms = std::fs::metadata(&shard).unwrap().permissions();
    perms.set_mode(0o500);
    std::fs::set_permissions(&shard, perms).unwrap();

    // The entry must disappear even though its blob could not be removed.
    v.remove("gone.txt").unwrap();
    assert!(v.list().unwrap().is_empty());

    // Restore permissions; the leftover blob is a reclaimable orphan.
    let mut perms = std::fs::metadata(&shard).unwrap().permissions();
    perms.set_mode(0o700);
    std::fs::set_permissions(&shard, perms).unwrap();
    assert_eq!(v.prune_orphans().unwrap(), 1);
}

#[test]
fn failed_batch_leaves_the_index_unchanged() {
    let (dir, mut v) = make_vault("batchfail");
    let a = dir.path().join("a.txt");
    std::fs::write(&a, b"original").unwrap();
    v.add(&a, None).unwrap();
    let before = v.version().unwrap();
    let blobs_before = walk_blobs(&v.path().join("d"));

    // A replace writes a fresh copy-on-write blob, then a later failing
    // operation must leave both the index and the visible contents untouched.
    let b = dir.path().join("b.txt");
    std::fs::write(&b, b"new content").unwrap();
    let result = v.apply_batch(&[
        BatchOp::Replace {
            source: b,
            name: "a.txt".into(),
        },
        BatchOp::Remove {
            name: "missing.txt".into(),
        },
    ]);
    assert!(matches!(result, Err(Error::EntryNotFound(_))));

    assert_eq!(v.version().unwrap(), before);
    let out = dir.path().join("out.txt");
    v.extract("a.txt", &out).unwrap();
    assert_eq!(std::fs::read(&out).unwrap(), b"original");

    // The failed replace's blob is an orphan; pruning restores the original
    // blob set exactly.
    assert!(v.prune_orphans().unwrap() >= 1);
    assert_eq!(walk_blobs(&v.path().join("d")), blobs_before);
}

#[test]
fn missing_manifest_is_not_treated_as_an_empty_index() {
    let (dir, mut v) = make_vault("nomanifest");
    let src = dir.path().join("f.txt");
    std::fs::write(&src, b"x").unwrap();
    v.add(&src, None).unwrap();

    std::fs::remove_file(v.path().join("d").join(".manifest.enc")).unwrap();

    assert!(matches!(v.list(), Err(Error::InvalidVault)));
    assert!(matches!(v.version(), Err(Error::InvalidVault)));
    assert!(matches!(
        v.verify("master-password"),
        Err(Error::InvalidVault)
    ));
}

#[test]
fn init_refuses_to_clobber_a_config_less_recovery_vault() {
    let (_dir, mut v) = make_vault("initrecovery");
    v.create_recovery_kit("master-password").unwrap();
    std::fs::remove_file(v.path().join("vault.config")).unwrap();

    assert!(matches!(v.init("other-password"), Err(Error::VaultExists)));
    assert!(
        v.path().join("vault.recovery").is_file(),
        "the recovery kit must survive the refused re-init"
    );
}

#[test]
fn nested_root_names_are_rejected() {
    let (dir, mut v) = make_vault("nestedroot");
    let src = dir.path().join("f.txt");
    std::fs::write(&src, b"x").unwrap();

    assert!(matches!(
        v.add(&src, Some("a/b.txt")),
        Err(Error::InvalidEntryName(_))
    ));
    assert!(matches!(
        v.add_if(&src, Some("a/b.txt"), None),
        Err(Error::InvalidEntryName(_))
    ));

    v.add(&src, Some("a.txt")).unwrap();
    assert!(matches!(
        v.rename("a.txt", "b/c.txt"),
        Err(Error::InvalidEntryName(_))
    ));
    assert_eq!(v.list().unwrap()[0].name, "a.txt");
}

#[test]
fn re_adding_a_tree_fails_without_writing_blobs() {
    let (dir, mut v) = make_vault("treepreflight");
    let tree = dir.path().join("tree");
    std::fs::create_dir_all(tree.join("sub")).unwrap();
    std::fs::write(tree.join("sub/file.txt"), b"nested").unwrap();
    v.add(&tree, None).unwrap();
    let blobs_before = walk_blobs(&v.path().join("d"));

    assert!(matches!(v.add(&tree, None), Err(Error::EntryExists(_))));
    assert_eq!(walk_blobs(&v.path().join("d")), blobs_before);

    let out = dir.path().join("restored");
    v.extract("tree", &out).unwrap();
    assert_eq!(std::fs::read(out.join("sub/file.txt")).unwrap(), b"nested");
}

#[test]
fn failed_erase_leaves_the_entry_and_old_password_intact() {
    let (dir, mut v) = make_vault("erasefail");
    let secret = dir.path().join("secret.txt");
    std::fs::write(&secret, b"secret").unwrap();
    v.add(&secret, None).unwrap();
    let keep = dir.path().join("keep.txt");
    std::fs::write(&keep, b"keep").unwrap();
    v.add(&keep, None).unwrap();

    let blobs = walk_blobs(&v.path().join("d"));
    assert_eq!(blobs.len(), 2);
    let originals: Vec<_> = blobs.iter().map(|p| std::fs::read(p).unwrap()).collect();

    // Corrupt every blob so rotation of the survivor must fail.
    for path in &blobs {
        let mut bytes = std::fs::read(path).unwrap();
        bytes[40] ^= 0xFF;
        std::fs::write(path, bytes).unwrap();
    }

    let vault_path = v.path().to_path_buf();
    assert!(v.erase("secret.txt", "new-password").is_err());
    drop(v);

    // Restore the pre-erase ciphertext; the vault (including the entry that
    // was supposed to be erased) must still be usable under the old password.
    for (path, bytes) in blobs.iter().zip(originals) {
        std::fs::write(path, bytes).unwrap();
    }

    let mut reopened = Vault::new(vault_path);
    reopened.unlock("master-password").unwrap();
    let names: Vec<_> = reopened
        .list()
        .unwrap()
        .into_iter()
        .map(|e| e.name)
        .collect();
    assert!(names.contains(&"secret.txt".to_string()));
    assert!(names.contains(&"keep.txt".to_string()));

    let out = dir.path().join("secret-out.txt");
    reopened.extract("secret.txt", &out).unwrap();
    assert_eq!(std::fs::read(&out).unwrap(), b"secret");

    assert!(Vault::new(reopened.path().to_path_buf())
        .unlock("new-password")
        .is_err());
}

#[test]
fn orphaned_compaction_staging_is_cleaned_up() {
    let (_d, mut v) = make_vault("stagedlog");
    let parent = v.path().parent().unwrap().to_path_buf();
    let staged = v.path().join("d").join(".index.log.next");
    std::fs::write(&staged, b"interrupted write").unwrap();

    let src = parent.join("after-crash.txt");
    std::fs::write(&src, b"payload").unwrap();
    v.add(&src, None).unwrap();

    assert!(!staged.exists());
    assert!(v
        .list()
        .unwrap()
        .iter()
        .any(|e| e.name == "after-crash.txt"));
}