feat: the window and the server say each new state in words; a proof cut short is made again; the proof does not mistake the books' own recovery for a changed head
- books ahead of the folder (as after a restore from an older backup) offer a new backup set beside the old one, at the person's request: books.back_up_now new_set, back-up --new-set, a button in the Backups window - a key file lost from a sealed destination stops backups and asks for the passphrase; plain is written only after an explicit removal, which the record keeps (a damaged record falls back to what the folder itself holds) - a restore that is not of the newest backup says so first, in the warning colour, with both dates and how many backups it passes over - a folder a sync service keeps (or may) is said in plain words at set-up and in the window: one computer only, kept offline, a second computer its own - a drill that finds only an older whole backup fails in words - a manifest whose proof was cut short is marked unproven and proven by the next backup, even one with nothing new; a saved backup file is proven whole before it is given its name - the proof compares the journal its pieces make with the head the manifest records before the books open, and lets opening add only after it - the restore tests are split from backups.rs to keep each under a minute; the clone-of-an-old-git-backup test builds the working tree without git Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
9ac47a3f10
commit
344b3b4abe
21 files changed
+2346
-690
No files matched your search
@@ -4,6 +4,7 @@
|
||||
//! the INVARIANTS of ledger-mcp backup.rs and ledger-store backup, and
|
||||
//! plan 3.7's named tests.
|
||||
#![cfg(unix)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
use ledger_mcp::admin::{self, Request, Response};
|
||||
use ledger_store::backup::{Ctx, find, verify_found};
|
||||
@@ -637,171 +638,6 @@ fn files_below(dir: &Path, prefix: &str) -> Vec<PathBuf> {
|
||||
all
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn restore_beside_existing_books_and_on_a_clean_install() {
|
||||
let home = Home::new("beside");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
let (folder, want) = backed_up_acme(&home).await;
|
||||
// Beside: its own books, the same figures and head.
|
||||
let r = restore(&home, "acme-copy", &folder, false).unwrap();
|
||||
assert_eq!(r.replaced, None);
|
||||
assert_eq!(r.head, live_head(&home, "acme"));
|
||||
let copy = connect(
|
||||
&home,
|
||||
"acme-copy",
|
||||
home.token("acme-copy", &["read", "admin"]),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(figures(©).await, want);
|
||||
// Its own backups carry on in the same folder, under its own name; the
|
||||
// original's stay as they were.
|
||||
let orig = proven(&home, &folder.join("wanderbooks-acme"), "orig-before");
|
||||
let s = call(©, "books.back_up_now", json!({})).await.unwrap();
|
||||
assert_eq!(s["targets"][0]["last_error"], Value::Null, "{s}");
|
||||
assert_eq!(
|
||||
proven(&home, &folder.join("wanderbooks-acme-copy"), "copy"),
|
||||
live_head(&home, "acme-copy")
|
||||
);
|
||||
assert_eq!(
|
||||
proven(&home, &folder.join("wanderbooks-acme"), "orig-after"),
|
||||
orig
|
||||
);
|
||||
drop(copy);
|
||||
assert!(server.close().success());
|
||||
// restore_on_clean_install: another computer's root, nothing in it.
|
||||
let other = Home::new("clean");
|
||||
let server = Server::start(&other, Duration::from_secs(3600));
|
||||
assert_eq!(books_held(&other), Vec::<String>::new());
|
||||
let r = restore(&other, "acme", &folder.join("wanderbooks-acme"), false).unwrap();
|
||||
assert_eq!(r.head, live_head(&home, "acme"));
|
||||
let c = connect(&other, "acme", other.token("acme", &["read"])).await;
|
||||
assert_eq!(figures(&c).await, want);
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn restore_never_overwrites_and_replacing_keeps_the_old_books() {
|
||||
let home = Home::new("replace");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
let (folder, want) = backed_up_acme(&home).await;
|
||||
let at_backup = live_head(&home, "acme");
|
||||
let c = connect(&home, "acme", home.token("acme", &ALL)).await;
|
||||
post_one(&c, 6).await;
|
||||
let moved_on = live_head(&home, "acme");
|
||||
assert!(moved_on.0 > at_backup.0);
|
||||
// Not over them.
|
||||
let refused = restore(&home, "acme", &folder, false).unwrap_err();
|
||||
assert!(refused.contains("already books called"), "{refused}");
|
||||
assert_eq!(live_head(&home, "acme"), moved_on);
|
||||
// Replacing books that aren't there is refused too.
|
||||
let refused = restore(&home, "nobody", &folder, true).unwrap_err();
|
||||
assert!(refused.contains("no books called"), "{refused}");
|
||||
// Replacing: the old books kept whole in legacy/, the backup in place.
|
||||
let r = restore(&home, "acme", &folder, true).unwrap();
|
||||
let fresh = connect(&home, "acme", home.token("acme", &["read"])).await;
|
||||
let deadline = Instant::now() + Duration::from_secs(30);
|
||||
loop {
|
||||
let s = call(&fresh, "books.backup", json!({})).await.unwrap();
|
||||
if s["targets"][0]["last_ok_at"].is_string() {
|
||||
break;
|
||||
}
|
||||
assert!(
|
||||
Instant::now() < deadline,
|
||||
"the restored books weren't backed up: {s}"
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
drop(fresh);
|
||||
let kept = home.root().join("legacy").join(r.replaced.clone().unwrap());
|
||||
let old = ledger_store::Snapshot::load(&kept).unwrap();
|
||||
assert_eq!((old.journal_seq(), old.head_hash().to_string()), moved_on);
|
||||
assert_eq!(live_head(&home, "acme"), at_backup);
|
||||
let c = connect(&home, "acme", home.token("acme", &ALL)).await;
|
||||
assert_eq!(figures(&c).await, want);
|
||||
// And it is written to and backed up as before: the folder's backup of
|
||||
// these books goes on from where it was.
|
||||
post_one(&c, 7).await;
|
||||
let s = call(&c, "books.back_up_now", json!({})).await.unwrap();
|
||||
assert_eq!(s["targets"][0]["last_error"], Value::Null, "{s}");
|
||||
assert_eq!(proven(&home, &folder, "after"), live_head(&home, "acme"));
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn restore_refuses_a_damaged_backup_and_writes_nothing() {
|
||||
let home = Home::new("damaged");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
let (folder, _) = backed_up_acme(&home).await;
|
||||
let before = books_held(&home);
|
||||
let books = home.root().join("books");
|
||||
let names = || {
|
||||
let mut v: Vec<String> = std::fs::read_dir(&books)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
|
||||
.collect();
|
||||
v.sort();
|
||||
v
|
||||
};
|
||||
let listed = names();
|
||||
// Each damage in a copy of its own, made from the good one.
|
||||
type Spoil<'a> = (&'a str, &'a dyn Fn(&Path));
|
||||
let damage: [Spoil; 5] = [
|
||||
("flipped", &|set: &Path| {
|
||||
let j = &files_below(set, "journal/")[0];
|
||||
let mut b = std::fs::read(j).unwrap();
|
||||
let mid = b.len() / 2;
|
||||
b[mid] ^= 1;
|
||||
std::fs::write(j, b).unwrap();
|
||||
}),
|
||||
("no-files", &|set: &Path| {
|
||||
for f in files_below(set, "files/") {
|
||||
std::fs::remove_file(f).unwrap();
|
||||
}
|
||||
}),
|
||||
("cut", &|set: &Path| {
|
||||
let j = &files_below(set, "journal/")[0];
|
||||
let mut b = std::fs::read(j).unwrap();
|
||||
b.truncate(b.len() - 10);
|
||||
std::fs::write(j, b).unwrap();
|
||||
}),
|
||||
("manifest-cut", &|set: &Path| {
|
||||
for f in files_below(set, "manifests/") {
|
||||
let b = std::fs::read(&f).unwrap();
|
||||
std::fs::write(&f, &b[..b.len() / 2]).unwrap();
|
||||
}
|
||||
}),
|
||||
("no-manifest", &|set: &Path| {
|
||||
for f in files_below(set, "manifests/") {
|
||||
std::fs::remove_file(f).unwrap();
|
||||
}
|
||||
}),
|
||||
];
|
||||
for (tag, spoil) in damage {
|
||||
let copy = home.outside(&format!("bad-{tag}"));
|
||||
copy_dir(&folder.join("wanderbooks-acme"), ©);
|
||||
spoil(©);
|
||||
let why = restore(&home, "acme-bad", ©, false).unwrap_err();
|
||||
assert!(why.contains("can't be restored"), "{tag}: {why}");
|
||||
assert!(
|
||||
!why.contains(&home.root().display().to_string()),
|
||||
"{tag}: {why}"
|
||||
);
|
||||
assert!(!why.contains(©.display().to_string()), "{tag}: {why}");
|
||||
assert_eq!(books_held(&home), before, "{tag}");
|
||||
assert_eq!(names(), listed, "{tag}");
|
||||
}
|
||||
// A place that isn't a backup: said, nothing written.
|
||||
let why = restore(&home, "acme-bad", &home.outside("nothing-here"), false).unwrap_err();
|
||||
assert!(!why.contains("nothing-here"), "{why}");
|
||||
assert_eq!(names(), listed);
|
||||
assert!(server.close().success());
|
||||
}
|
||||
|
||||
fn copy_dir(from: &Path, to: &Path) {
|
||||
std::fs::create_dir_all(to).unwrap();
|
||||
for e in std::fs::read_dir(from).unwrap() {
|
||||
@@ -902,187 +738,6 @@ async fn a_write_waits_for_the_quiet_before_it_is_backed_up() {
|
||||
assert!(server.close().success());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn a_restore_is_of_the_backup_previewed_only() {
|
||||
let home = Home::new("previewed");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
let (folder, _) = backed_up_acme(&home).await;
|
||||
let request = |expect_head: Option<(u64, String)>, confirmed| Request::Restore {
|
||||
id: "copy".into(),
|
||||
source: folder.display().to_string(),
|
||||
replace: false,
|
||||
confirmed,
|
||||
expect_head,
|
||||
passphrase: None,
|
||||
};
|
||||
// A preview leaves nothing behind in the books' folder, and clears
|
||||
// what a restore that never finished left.
|
||||
let books = home.root().join("books");
|
||||
std::fs::create_dir(books.join(".restore-junk")).unwrap();
|
||||
let previewed = match home.admin(request(None, false)) {
|
||||
Response::RestorePreview(p) => p.head,
|
||||
other => panic!("{other:?}"),
|
||||
};
|
||||
let dotted: Vec<_> = std::fs::read_dir(&books)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
|
||||
.filter(|n| n.starts_with('.'))
|
||||
.collect();
|
||||
assert!(dotted.is_empty(), "{dotted:?}");
|
||||
// Confirmed with no head: refused.
|
||||
match home.admin(request(None, true)) {
|
||||
Response::Refused(why) => assert!(why.contains("preview first"), "{why}"),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
// The backup moves on after the preview: refused, nothing written.
|
||||
let c = connect(&home, "acme", home.token("acme", &ALL)).await;
|
||||
post_one(&c, 9).await;
|
||||
call(&c, "books.back_up_now", json!({})).await.unwrap();
|
||||
match home.admin(request(Some(previewed), true)) {
|
||||
Response::Refused(why) => assert!(why.contains("isn't the one previewed"), "{why}"),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
assert_eq!(books_held(&home), vec!["acme".to_string()]);
|
||||
assert!(!home.root().join("books").join("copy").exists());
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn a_restore_brings_the_vault_and_its_key() {
|
||||
let home = Home::new("vault");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
let c = connect(&home, "acme", home.token("acme", &ALL)).await;
|
||||
call(
|
||||
&c,
|
||||
"books.add_party",
|
||||
json!({"name": "Mo", "roles": ["member"]}),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
call(
|
||||
&c,
|
||||
"books.set_tin",
|
||||
json!({"party": "Mo", "tin": "12-3456789", "tin_type": "ein"}),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let (folder, want) = backed_up_acme(&home).await;
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
let other = Home::new("vault-clean");
|
||||
let server = Server::start(&other, Duration::from_secs(3600));
|
||||
let request = |expect_head: Option<(u64, String)>| Request::Restore {
|
||||
id: "acme".into(),
|
||||
source: folder.display().to_string(),
|
||||
replace: false,
|
||||
confirmed: expect_head.is_some(),
|
||||
expect_head,
|
||||
passphrase: None,
|
||||
};
|
||||
let preview = match other.admin(request(None)) {
|
||||
Response::RestorePreview(p) => p,
|
||||
other => panic!("{other:?}"),
|
||||
};
|
||||
assert_eq!(preview.vault_files, 1);
|
||||
let r = match other.admin(request(Some(preview.head))) {
|
||||
Response::Restored(r) => r,
|
||||
other => panic!("{other:?}"),
|
||||
};
|
||||
assert_eq!(r.vault_files, 1);
|
||||
// The record and its key, byte for byte.
|
||||
let files = |h: &Home, sub: &str| {
|
||||
let mut v: Vec<(String, Vec<u8>)> = std::fs::read_dir(h.book_dir("acme").join(sub))
|
||||
.unwrap()
|
||||
.map(|e| {
|
||||
let e = e.unwrap();
|
||||
(
|
||||
e.file_name().to_string_lossy().into_owned(),
|
||||
std::fs::read(e.path()).unwrap(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
v.sort();
|
||||
v
|
||||
};
|
||||
for sub in ["vault", "keys"] {
|
||||
assert!(!files(&home, sub).is_empty(), "{sub}");
|
||||
assert_eq!(files(&other, sub), files(&home, sub), "{sub}");
|
||||
}
|
||||
let c = connect(&other, "acme", other.token("acme", &["read"])).await;
|
||||
assert_eq!(figures(&c).await, want);
|
||||
let party = call(&c, "books.party", json!({"party": "Mo"}))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(party["party"]["tin"]["in_vault"], true, "{party}");
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
// A backup missing the vault's key, or the TIN record the books name,
|
||||
// is not whole: refused, nothing restored.
|
||||
let third = Home::new("vault-short");
|
||||
let server = Server::start(&third, Duration::from_secs(3600));
|
||||
for gone in ["keys/", "vault/"] {
|
||||
let copy = home.outside(&format!("no-{}", gone.trim_end_matches('/')));
|
||||
copy_dir(&folder.join("wanderbooks-acme"), ©);
|
||||
for f in files_below(©, gone) {
|
||||
std::fs::remove_file(f).unwrap();
|
||||
}
|
||||
let why = restore(&third, "acme", ©, false).unwrap_err();
|
||||
assert!(why.contains("can't be restored"), "{gone}: {why}");
|
||||
assert_eq!(books_held(&third), Vec::<String>::new());
|
||||
}
|
||||
assert!(server.close().success());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn a_replacing_restore_that_fails_leaves_the_books_open() {
|
||||
let home = Home::new("replace-bad");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
let (folder, want) = backed_up_acme(&home).await;
|
||||
let before = live_head(&home, "acme");
|
||||
let bad = home.outside("bad");
|
||||
copy_dir(&folder.join("wanderbooks-acme"), &bad);
|
||||
let j = &files_below(&bad, "journal/")[0];
|
||||
let mut b = std::fs::read(j).unwrap();
|
||||
let mid = b.len() / 2;
|
||||
b[mid] ^= 1;
|
||||
std::fs::write(j, b).unwrap();
|
||||
// Its preview is refused; so is a confirm (sent without one), which
|
||||
// lets the books go first, then fails: they are open again, as they
|
||||
// were, nothing moved to legacy.
|
||||
assert!(restore(&home, "acme", &bad, true).is_err());
|
||||
let why = match home.admin(Request::Restore {
|
||||
id: "acme".into(),
|
||||
source: bad.display().to_string(),
|
||||
replace: true,
|
||||
confirmed: true,
|
||||
expect_head: Some(before.clone()),
|
||||
passphrase: None,
|
||||
}) {
|
||||
Response::Refused(why) => why,
|
||||
other => panic!("{other:?}"),
|
||||
};
|
||||
assert!(why.contains("can't be"), "{why}");
|
||||
match home.admin(Request::List) {
|
||||
Response::Books(b) => {
|
||||
assert_eq!(b.len(), 1);
|
||||
assert!(b[0].open, "{b:?}");
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
assert_eq!(live_head(&home, "acme"), before);
|
||||
let legacy = home.root().join("legacy");
|
||||
assert!(!legacy.exists() || std::fs::read_dir(&legacy).unwrap().count() == 0);
|
||||
let c = connect(&home, "acme", home.token("acme", &ALL)).await;
|
||||
assert_eq!(figures(&c).await, want);
|
||||
post_one(&c, 3).await;
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
}
|
||||
|
||||
/// The book's backup record (the test's own data root).
|
||||
fn records(home: &Home, book: &str) -> Vec<Value> {
|
||||
std::fs::read_to_string(home.book_dir(book).join("ops").join("backup.jsonl"))
|
||||
@@ -1092,44 +747,6 @@ fn records(home: &Home, book: &str) -> Vec<Value> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn restored_books_never_make_their_backup_folder_again() {
|
||||
let home = Home::new("restore-mount");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
let (folder, _) = backed_up_acme(&home).await;
|
||||
assert!(server.close().success());
|
||||
// On another computer, its backup disk not mounted (the mount point
|
||||
// there, empty): restored from a copy of the backup.
|
||||
let copy = home.outside("copy-of-disk");
|
||||
copy_dir(&folder, ©);
|
||||
std::fs::rename(folder.join("wanderbooks-acme"), home.outside("unmounted")).unwrap();
|
||||
let other = Home::new("restore-mount-new");
|
||||
let server = Server::start(&other, Duration::from_secs(3600));
|
||||
restore(&other, "acme", ©, false).unwrap();
|
||||
let c = connect(&other, "acme", other.token("acme", &["read", "admin"])).await;
|
||||
let s = call(&c, "books.back_up_now", json!({})).await.unwrap();
|
||||
assert!(
|
||||
s["targets"][0]["last_error"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.contains("holds no backup"),
|
||||
"{s}"
|
||||
);
|
||||
assert!(
|
||||
!folder.join("wanderbooks-acme").exists(),
|
||||
"made on the wrong disk"
|
||||
);
|
||||
// The human says it's a new disk: begun there again.
|
||||
let s = call(&c, "books.back_up_now", json!({"new_disk": true}))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(s["targets"][0]["last_error"], Value::Null, "{s}");
|
||||
assert!(folder.join("wanderbooks-acme").join("manifests").is_dir());
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn a_damaged_backup_record_never_keeps_the_books_shut() {
|
||||
let home = Home::new("damaged-record");
|
||||
@@ -1250,28 +867,14 @@ async fn old_settings_and_an_old_backup_history_are_left_alone_and_said() {
|
||||
|
||||
/// docs/backups.md, "A backup made with an older version": a person who
|
||||
/// still has a backup made with git clones it themselves (git is their own
|
||||
/// tool now) and adopts the plain folder. This makes such a backup the way
|
||||
/// an older version did (a repository whose one commit is the books'
|
||||
/// files), clones it as the docs say, and adopts the clone with its vault
|
||||
/// and key as the docs say.
|
||||
/// tool now) and adopts the plain folder. What a clone leaves is a working
|
||||
/// tree: the books' files as the older version committed them, beside a
|
||||
/// `.git` folder. This builds that working tree from the books' files and
|
||||
/// adopts it with its vault and key as the docs say. It needs no git on
|
||||
/// the machine, so it never skips: the adoption is of a folder, and a
|
||||
/// folder is all there is to it.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn a_clone_of_an_old_git_backup_is_adopted_as_a_plain_folder() {
|
||||
let git = |dir: &Path, args: &[&str]| {
|
||||
let out = Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(dir)
|
||||
.args(["-c", "user.name=t", "-c", "user.email=t@t.invalid"])
|
||||
.args(args)
|
||||
.output();
|
||||
match out {
|
||||
Ok(o) => {
|
||||
assert!(o.status.success(), "{args:?}: {o:?}");
|
||||
true
|
||||
}
|
||||
// No git on this machine: the path is the person's own tool's.
|
||||
Err(_) => false,
|
||||
}
|
||||
};
|
||||
let home = Home::new("old-backup");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
@@ -1293,41 +896,28 @@ async fn a_clone_of_an_old_git_backup_is_adopted_as_a_plain_folder() {
|
||||
let (folder, want) = backed_up_acme(&home).await;
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
// The old backup: the books' files, committed, in a bare repository.
|
||||
// The working tree of a clone of the old backup: the books' files, and
|
||||
// the repository's own folder beside them (a few of the files git
|
||||
// keeps there, which adoption must leave out).
|
||||
let book = home.book_dir("acme");
|
||||
let work = home.outside("old-work");
|
||||
std::fs::create_dir_all(&work).unwrap();
|
||||
let clone = home.outside("acme-old");
|
||||
std::fs::create_dir_all(&clone).unwrap();
|
||||
for part in ["journal.jsonl", "blobs", "vault", "keys", "access.jsonl"] {
|
||||
let from = book.join(part);
|
||||
if from.is_dir() {
|
||||
copy_dir(&from, &work.join(part));
|
||||
copy_dir(&from, &clone.join(part));
|
||||
} else if from.is_file() {
|
||||
std::fs::copy(&from, work.join(part)).unwrap();
|
||||
std::fs::copy(&from, clone.join(part)).unwrap();
|
||||
}
|
||||
}
|
||||
if !git(&work, &["init", "-q", "-b", "main"]) {
|
||||
return;
|
||||
}
|
||||
git(&work, &["add", "-A", "-f"]);
|
||||
git(&work, &["commit", "-q", "-m", "seq 1 head x\n\nbook acme"]);
|
||||
let bare = home.outside("acme.git");
|
||||
let out = Command::new("git")
|
||||
.args(["clone", "-q", "--bare"])
|
||||
.arg(&work)
|
||||
.arg(&bare)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
// The person's steps: clone it, then adopt the folder with its vault
|
||||
// and key.
|
||||
let clone = home.outside("acme-old");
|
||||
let out = Command::new("git")
|
||||
.args(["clone", "-q"])
|
||||
.arg(&bare)
|
||||
.arg(&clone)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
std::fs::create_dir_all(clone.join(".git/refs/heads")).unwrap();
|
||||
std::fs::write(clone.join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
|
||||
std::fs::write(
|
||||
clone.join(".git/config"),
|
||||
"[core]\n\trepositoryformatversion = 0\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(clone.join("journal.jsonl").is_file() && clone.join("vault").is_dir());
|
||||
let other = Home::new("old-backup-new");
|
||||
let server = Server::start(&other, Duration::from_secs(3600));
|
||||
let request = |confirmed: bool, expect_head: Option<(u64, String)>| Request::Adopt {
|
||||
@@ -1362,154 +952,3 @@ async fn a_clone_of_an_old_git_backup_is_adopted_as_a_plain_folder() {
|
||||
assert!(server.close().success());
|
||||
let _ = folder;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn a_passphrase_seals_the_backups_and_a_restore_asks_for_it() {
|
||||
// D-92 from the window's side: set up through the admin socket, a backup
|
||||
// made sealed, the status said, a restore on another computer asking for
|
||||
// the passphrase, a wrong one refused, a saved file sealed too, and the
|
||||
// passphrase taken off.
|
||||
const PASS: &str = "correct horse battery staple";
|
||||
let home = Home::new("sealed");
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
home.create("acme");
|
||||
let folder = home.outside("disk");
|
||||
std::fs::create_dir_all(&folder).unwrap();
|
||||
let said = |r: Response| match r {
|
||||
Response::Said { lines } => lines.join(" "),
|
||||
other => panic!("{other:?}"),
|
||||
};
|
||||
// A folder that isn't there, or is inside the books, is refused in words.
|
||||
match home.admin(Request::BackupSetup {
|
||||
id: "acme".into(),
|
||||
folder: Some(home.outside("nowhere").display().to_string()),
|
||||
passphrase: None,
|
||||
remove_passphrase: false,
|
||||
}) {
|
||||
Response::Refused(why) => assert!(why.contains("isn't there"), "{why}"),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
let words = said(home.admin(Request::BackupSetup {
|
||||
id: "acme".into(),
|
||||
folder: Some(folder.display().to_string()),
|
||||
passphrase: Some(admin::Passphrase(PASS.into())),
|
||||
remove_passphrase: false,
|
||||
}));
|
||||
assert!(words.contains("cannot be recovered"), "{words}");
|
||||
assert!(!words.contains(PASS));
|
||||
let c = connect(&home, "acme", home.token("acme", &ALL)).await;
|
||||
post_one(&c, 3).await;
|
||||
let s = call(&c, "books.back_up_now", json!({})).await.unwrap();
|
||||
assert_eq!(s["targets"][0]["last_error"], Value::Null, "{s}");
|
||||
assert_eq!(s["targets"][0]["sealed"], true, "{s}");
|
||||
match home.admin(Request::BackupStatus { id: "acme".into() }) {
|
||||
Response::BackupStatus(st) => {
|
||||
assert!(st.sealed && st.last_ok_at.is_some(), "{st:?}");
|
||||
assert_eq!(
|
||||
st.folder.as_deref(),
|
||||
Some(folder.display().to_string().as_str())
|
||||
);
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
// What is in the folder says nothing of the books.
|
||||
let names: Vec<String> = files_below(&folder, "")
|
||||
.into_iter()
|
||||
.map(|p| {
|
||||
p.strip_prefix(&folder)
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.into_owned()
|
||||
})
|
||||
.collect();
|
||||
assert!(
|
||||
!names
|
||||
.iter()
|
||||
.any(|n| n.contains("acme") || n.contains("journal")),
|
||||
"{names:?}"
|
||||
);
|
||||
// Nothing of the passphrase or its key is in the backup record, the
|
||||
// access log or the journal.
|
||||
let book = home.book_dir("acme");
|
||||
for file in ["ops/backup.jsonl", "access.jsonl", "journal.jsonl"] {
|
||||
let text = std::fs::read_to_string(book.join(file)).unwrap_or_default();
|
||||
assert!(!text.contains(PASS), "{file}");
|
||||
}
|
||||
let key = std::fs::read_to_string(book.join("local").join("backup-key.json")).unwrap();
|
||||
let master = key
|
||||
.split('"')
|
||||
.nth(key.split('"').position(|p| p == "master").unwrap() + 2)
|
||||
.unwrap()
|
||||
.to_string();
|
||||
for file in ["ops/backup.jsonl", "access.jsonl", "journal.jsonl"] {
|
||||
let text = std::fs::read_to_string(book.join(file)).unwrap_or_default();
|
||||
assert!(!text.contains(&master), "{file}");
|
||||
}
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
// Another computer: the passphrase is asked for, and a wrong one refused.
|
||||
let other = Home::new("sealed-new");
|
||||
let server = Server::start(&other, Duration::from_secs(3600));
|
||||
let why = restore(&other, "acme", &folder, false).unwrap_err();
|
||||
assert!(why.contains("passphrase"), "{why}");
|
||||
let why = restore_with(
|
||||
&other,
|
||||
"acme",
|
||||
&folder,
|
||||
false,
|
||||
Some("not the right words at all"),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(why.contains("isn't the passphrase"), "{why}");
|
||||
assert_eq!(books_held(&other), Vec::<String>::new());
|
||||
let r = restore_with(&other, "acme", &folder, false, Some(PASS)).unwrap();
|
||||
assert_eq!(r.head, live_head(&home, "acme"));
|
||||
// The restored books keep backing up sealed, with no question asked.
|
||||
assert!(
|
||||
other
|
||||
.book_dir("acme")
|
||||
.join("local")
|
||||
.join("backup-key.json")
|
||||
.is_file()
|
||||
);
|
||||
// Saved as one file: sealed too, and restorable from.
|
||||
let file = other.outside("acme.wbbackup");
|
||||
let words = said(other.admin(Request::BackupSave {
|
||||
id: "acme".into(),
|
||||
path: file.display().to_string(),
|
||||
}));
|
||||
assert!(words.contains("protected by the passphrase"), "{words}");
|
||||
assert!(server.close().success());
|
||||
let third = Home::new("sealed-file");
|
||||
let server = Server::start(&third, Duration::from_secs(3600));
|
||||
assert!(restore(&third, "acme", &file, false).is_err());
|
||||
let r = restore_with(&third, "acme", &file, false, Some(PASS)).unwrap();
|
||||
assert_eq!(r.head, live_head(&home, "acme"));
|
||||
assert!(server.close().success());
|
||||
// The passphrase taken off: the next backup is plain, in the old plain
|
||||
// folder, and the sealed one is untouched.
|
||||
let server = Server::start(&home, Duration::from_secs(3600));
|
||||
let sealed_before: Vec<_> = files_below(&folder, "sealed-");
|
||||
let bytes_before: Vec<Vec<u8>> = sealed_before
|
||||
.iter()
|
||||
.map(|p| std::fs::read(p).unwrap())
|
||||
.collect();
|
||||
said(home.admin(Request::BackupSetup {
|
||||
id: "acme".into(),
|
||||
folder: None,
|
||||
passphrase: None,
|
||||
remove_passphrase: true,
|
||||
}));
|
||||
let c = connect(&home, "acme", home.token("acme", &ALL)).await;
|
||||
let s = call(&c, "books.back_up_now", json!({})).await.unwrap();
|
||||
assert_eq!(s["targets"][0]["sealed"], false, "{s}");
|
||||
assert_eq!(s["targets"][0]["last_error"], Value::Null, "{s}");
|
||||
assert!(folder.join("wanderbooks-acme").join("manifests").is_dir());
|
||||
let bytes_after: Vec<Vec<u8>> = sealed_before
|
||||
.iter()
|
||||
.map(|p| std::fs::read(p).unwrap())
|
||||
.collect();
|
||||
assert_eq!(bytes_before, bytes_after);
|
||||
drop(c);
|
||||
assert!(server.close().success());
|
||||
}
|
||||
Reference in new issue
Block a user