synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-bench / tests / store_repair_cli.rs
| 1 | //! `synapse-bench store-repair` (issue #105) through the real binary: report mode never |
| 2 | //! writes, and a write is refused unless it names one action, an operator key and a |
| 3 | //! backup. The repair itself (roll forward, adopt, signed log) is covered by the |
| 4 | //! `r1_repair_*` tests in `synapse-store`. |
| 5 | |
| 6 | use sha2::{Digest, Sha256}; |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | use std::process::Command; |
| 9 | use synapse_store::blockdev::{BlockDevice, BlockDeviceConfig, FormatPolicy}; |
| 10 | |
| 11 | fn temp_dir(label: &str) -> PathBuf { |
| 12 | let d = std::env::temp_dir().join(format!( |
| 13 | "synapse-store-repair-cli-{label}-{}", |
| 14 | std::process::id() |
| 15 | )); |
| 16 | let _ = std::fs::remove_dir_all(&d); |
| 17 | std::fs::create_dir_all(&d).unwrap(); |
| 18 | d |
| 19 | } |
| 20 | |
| 21 | /// A formatted, empty 16 MiB store in a regular file. |
| 22 | fn formatted_device(dir: &Path) -> PathBuf { |
| 23 | let path = dir.join("raw.device"); |
| 24 | let f = std::fs::File::create(&path).unwrap(); |
| 25 | f.set_len(16 << 20).unwrap(); |
| 26 | f.sync_all().unwrap(); |
| 27 | let cfg = BlockDeviceConfig { |
| 28 | path: path.clone(), |
| 29 | format: FormatPolicy::FormatIfBlank, |
| 30 | meta_region_bytes: 1 << 20, |
| 31 | allow_foreign_signature: false, |
| 32 | }; |
| 33 | drop(BlockDevice::open_path(&cfg, 64 * 1024).unwrap()); |
| 34 | path |
| 35 | } |
| 36 | |
| 37 | fn sha(path: &Path) -> Vec<u8> { |
| 38 | Sha256::digest(std::fs::read(path).unwrap()).to_vec() |
| 39 | } |
| 40 | |
| 41 | fn run(args: &[&str]) -> (bool, String) { |
| 42 | let out = Command::new(env!("CARGO_BIN_EXE_synapse-bench")) |
| 43 | .arg("store-repair") |
| 44 | .args(args) |
| 45 | .output() |
| 46 | .unwrap(); |
| 47 | let text = format!( |
| 48 | "{}{}", |
| 49 | String::from_utf8_lossy(&out.stdout), |
| 50 | String::from_utf8_lossy(&out.stderr) |
| 51 | ); |
| 52 | (out.status.success(), text) |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn store_repair_cli_reports_and_refuses_writes_without_intent() { |
| 57 | let dir = temp_dir("intent"); |
| 58 | let dev = formatted_device(&dir); |
| 59 | let d = dev.to_str().unwrap(); |
| 60 | let key = dir.join("operator.key"); |
| 61 | std::fs::write(&key, format!("{}\n", "0b".repeat(32))).unwrap(); |
| 62 | let k = key.to_str().unwrap(); |
| 63 | let backup = dir.join("backup.bin"); |
| 64 | let b = backup.to_str().unwrap(); |
| 65 | let before = sha(&dev); |
| 66 | |
| 67 | // Report only: read-only, healthy, nothing written. |
| 68 | let (ok, text) = run(&["--device", d]); |
| 69 | assert!(ok, "{text}"); |
| 70 | assert!(text.contains("verdict: healthy"), "{text}"); |
| 71 | assert!(text.contains("repair history: none"), "{text}"); |
| 72 | |
| 73 | // Refusals: no key, no backup, two actions, a key that is not 64 hex characters. |
| 74 | let bad_key = dir.join("bad.key"); |
| 75 | std::fs::write(&bad_key, "not hex").unwrap(); |
| 76 | for (args, needle) in [ |
| 77 | ( |
| 78 | vec!["--device", d, "--roll-forward", "--backup", b], |
| 79 | "--operator-key", |
| 80 | ), |
| 81 | ( |
| 82 | vec!["--device", d, "--roll-forward", "--operator-key", k], |
| 83 | "--backup", |
| 84 | ), |
| 85 | ( |
| 86 | vec![ |
| 87 | "--device", |
| 88 | d, |
| 89 | "--roll-forward", |
| 90 | "--adopt-older-checkpoint", |
| 91 | "--operator-key", |
| 92 | k, |
| 93 | "--backup", |
| 94 | b, |
| 95 | ], |
| 96 | "give one action", |
| 97 | ), |
| 98 | ( |
| 99 | vec![ |
| 100 | "--device", |
| 101 | d, |
| 102 | "--adopt-older-checkpoint", |
| 103 | "--operator-key", |
| 104 | bad_key.to_str().unwrap(), |
| 105 | "--backup", |
| 106 | b, |
| 107 | ], |
| 108 | "64 hex characters", |
| 109 | ), |
| 110 | ] { |
| 111 | let (ok, text) = run(&args); |
| 112 | assert!(!ok, "{args:?}: {text}"); |
| 113 | assert!(text.contains(needle), "{args:?}: {text}"); |
| 114 | } |
| 115 | |
| 116 | // With intent on a healthy device: nothing to do, nothing written, no backup. |
| 117 | let (ok, text) = run(&[ |
| 118 | "--device", |
| 119 | d, |
| 120 | "--roll-forward", |
| 121 | "--operator-key", |
| 122 | k, |
| 123 | "--backup", |
| 124 | b, |
| 125 | ]); |
| 126 | assert!(ok, "{text}"); |
| 127 | assert!(text.contains("nothing was written"), "{text}"); |
| 128 | assert!(!backup.exists()); |
| 129 | assert_eq!(sha(&dev), before, "the device file changed"); |
| 130 | let _ = std::fs::remove_dir_all(&dir); |
| 131 | } |