|
| 1 | +//! determinism_roundtrip.rs |
| 2 | +//! |
| 3 | +//! Black-box determinism test: |
| 4 | +//! same input => same output byte-for-byte. |
| 5 | +//! |
| 6 | +//! This test executes the `signia` CLI twice and compares produced bundles. |
| 7 | +//! |
| 8 | +//! How to run: |
| 9 | +//! - build CLI: `cargo build -p signia-cli` |
| 10 | +//! - then: `cargo test -q` (from workspace root) |
| 11 | +//! |
| 12 | +//! Notes: |
| 13 | +//! - The CLI path can be overridden via SIGNIA_BIN. |
| 14 | +//! - If the CLI binary is not found, the test is skipped. |
| 15 | +
|
| 16 | +use std::env; |
| 17 | +use std::fs; |
| 18 | +use std::path::{Path, PathBuf}; |
| 19 | +use std::process::Command; |
| 20 | + |
| 21 | +fn repo_root() -> PathBuf { |
| 22 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")).parent().unwrap().to_path_buf() |
| 23 | +} |
| 24 | + |
| 25 | +fn signia_bin() -> Option<PathBuf> { |
| 26 | + if let Ok(p) = env::var("SIGNIA_BIN") { |
| 27 | + let pb = PathBuf::from(p); |
| 28 | + if pb.exists() { return Some(pb); } |
| 29 | + } |
| 30 | + let p = repo_root().join("target").join("debug").join(if cfg!(windows) { "signia.exe" } else { "signia" }); |
| 31 | + if p.exists() { Some(p) } else { None } |
| 32 | +} |
| 33 | + |
| 34 | +fn run_compile(bin: &Path, input: &Path, typ: &str, out: &Path) { |
| 35 | + let status = Command::new(bin) |
| 36 | + .arg("compile") |
| 37 | + .arg(input) |
| 38 | + .arg("--type").arg(typ) |
| 39 | + .arg("--out").arg(out) |
| 40 | + .status() |
| 41 | + .expect("failed to spawn signia"); |
| 42 | + assert!(status.success(), "signia compile failed"); |
| 43 | +} |
| 44 | + |
| 45 | +fn read_bytes(p: &Path) -> Vec<u8> { |
| 46 | + fs::read(p).unwrap_or_else(|e| panic!("failed to read {}: {e}", p.display())) |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn determinism_dataset() { |
| 51 | + let Some(bin) = signia_bin() else { |
| 52 | + eprintln!("skip: signia CLI not found (set SIGNIA_BIN or build signia-cli)"); |
| 53 | + return; |
| 54 | + }; |
| 55 | + |
| 56 | + let root = repo_root(); |
| 57 | + let input = root.join("tests").join("fixtures").join("dataset_small").join("sample.csv"); |
| 58 | + |
| 59 | + let out1 = root.join("target").join("tmp").join("signia_test_determinism_1"); |
| 60 | + let out2 = root.join("target").join("tmp").join("signia_test_determinism_2"); |
| 61 | + let _ = fs::remove_dir_all(&out1); |
| 62 | + let _ = fs::remove_dir_all(&out2); |
| 63 | + fs::create_dir_all(&out1).unwrap(); |
| 64 | + fs::create_dir_all(&out2).unwrap(); |
| 65 | + |
| 66 | + run_compile(&bin, &input, "dataset", &out1); |
| 67 | + run_compile(&bin, &input, "dataset", &out2); |
| 68 | + |
| 69 | + for name in ["schema.json", "manifest.json", "proof.json"] { |
| 70 | + let b1 = read_bytes(&out1.join(name)); |
| 71 | + let b2 = read_bytes(&out2.join(name)); |
| 72 | + assert_eq!(b1, b2, "bundle file differs: {name}"); |
| 73 | + } |
| 74 | +} |
0 commit comments