#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::Command as PCommand;
use std::sync::atomic::{AtomicU64, Ordering};
use super::*;
use crate::cli::{Format, SourceKind};
use brygge_ir::builder::{AtomDraft, IrBuilder};
use brygge_ir::model::{
ChangedPart, CopyRecord, DropRecord, Identity, ImportProvenance, LossBoundary, MetadataClaims,
RefRecord, TransformRecord,
};
static COUNTER: AtomicU64 = AtomicU64::new(0);
fn git_available() -> bool {
PCommand::new("git")
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
struct TempRepo {
dir: PathBuf,
}
impl Drop for TempRepo {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.dir);
}
}
impl TempRepo {
fn new() -> Self {
let r = Self::new_plain();
r.git(&["init", "-q", "--initial-branch=main"]);
r
}
fn new_plain() -> Self {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!(
"brygge-cli-test-{}-{nanos}-{n}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
Self { dir }
}
fn path(&self) -> &Path {
&self.dir
}
fn git(&self, args: &[&str]) -> String {
let out = PCommand::new("git")
.current_dir(&self.dir)
.args(args)
.env("GIT_AUTHOR_NAME", "A U Thor")
.env("GIT_AUTHOR_EMAIL", "author@example.com")
.env("GIT_AUTHOR_DATE", "2005-04-07T22:13:13 +0000")
.env("GIT_COMMITTER_NAME", "C O Mitter")
.env("GIT_COMMITTER_EMAIL", "committer@example.com")
.env("GIT_COMMITTER_DATE", "2005-04-07T22:13:13 +0000")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_SYSTEM", "/dev/null")
.output()
.expect("run git");
assert!(
out.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
fn commit(&self, file: &str, contents: &str, msg: &str) {
std::fs::write(self.dir.join(file), contents).unwrap();
self.git(&["add", "-A"]);
self.git(&["-c", "commit.gpgsign=false", "commit", "-q", "-m", msg]);
}
}
fn simple_repo(seed: &str) -> TempRepo {
let r = TempRepo::new();
r.commit("a.txt", &format!("{seed} one\n"), "c1");
r.commit("a.txt", &format!("{seed} two\n"), "c2");
r
}
struct CleanupDir(PathBuf);
impl Drop for CleanupDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn tmp_ir_path(tag: &str) -> PathBuf {
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!("brygge-cli-{tag}-{}-{n}.ir", std::process::id()))
}
#[test]
fn spill_dir_missing_is_a_usage_error() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("spill-missing");
let out = repo.dir.join("out.ir");
let missing = repo.dir.join("does-not-exist");
let code = run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
Some(&missing),
Format::Machine,
);
assert_eq!(code, exit::USAGE);
assert!(!out.exists(), "no artifact on a usage error");
}
#[test]
fn spill_dir_a_file_not_a_directory_is_a_usage_error() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("spill-file");
let out = repo.dir.join("out.ir");
let file = repo.dir.join("a-plain-file");
std::fs::write(&file, b"not a directory").unwrap();
let code = run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
Some(&file),
Format::Machine,
);
assert_eq!(code, exit::USAGE);
assert!(!out.exists(), "no artifact on a usage error");
}
#[test]
fn spill_dir_given_a_good_directory_is_used_and_the_artifact_is_byte_identical() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("spill-good");
let default_out = repo.dir.join("default.ir");
let spill_out = repo.dir.join("spill.ir");
let spill_dir = repo.dir.join("spill");
std::fs::create_dir_all(&spill_dir).unwrap();
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&default_out,
false,
false,
None,
Format::Machine,
),
exit::CLEAN
);
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&spill_out,
false,
false,
Some(&spill_dir),
Format::Machine,
),
exit::CLEAN
);
assert_eq!(
std::fs::read(&default_out).unwrap(),
std::fs::read(&spill_out).unwrap(),
"--spill-dir never changes the artifact"
);
assert_eq!(
std::fs::read_dir(&spill_dir).unwrap().count(),
0,
"the spill file is never left behind"
);
}
#[test]
fn verify_spill_dir_missing_is_a_usage_error() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("verify-spill-missing");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let missing = repo.dir.join("does-not-exist");
let code = run_verify(&out, Some(repo.path()), Some(&missing), Format::Machine);
assert_eq!(code, exit::USAGE);
}
#[test]
fn verify_spill_dir_a_file_not_a_directory_is_a_usage_error() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("verify-spill-file");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let file = repo.dir.join("a-plain-file");
std::fs::write(&file, b"not a directory").unwrap();
let code = run_verify(&out, Some(repo.path()), Some(&file), Format::Machine);
assert_eq!(code, exit::USAGE);
}
#[test]
fn verify_spill_dir_given_a_good_directory_is_used_and_left_empty_and_the_verdict_is_unchanged() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("verify-spill-good");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let spill_dir = repo.dir.join("spill");
std::fs::create_dir_all(&spill_dir).unwrap();
let without_spill_dir = run_verify(&out, Some(repo.path()), None, Format::Machine);
let with_spill_dir = run_verify(&out, Some(repo.path()), Some(&spill_dir), Format::Machine);
assert_eq!(
without_spill_dir, with_spill_dir,
"--spill-dir never changes the verdict"
);
assert_eq!(with_spill_dir, exit::CLEAN);
assert_eq!(
std::fs::read_dir(&spill_dir).unwrap().count(),
0,
"the spill file is never left behind"
);
}
#[test]
fn decode_inspect_verify_roundtrip() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("x");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
assert!(out.exists(), "the artifact was written");
assert_eq!(run_inspect(&out, false, Format::Human), exit::CLEAN);
assert_eq!(run_inspect(&out, true, Format::Machine), exit::CLEAN);
assert_eq!(run_verify(&out, None, None, Format::Machine), exit::CLEAN);
assert_eq!(
run_verify(&out, Some(repo.path()), None, Format::Human),
exit::CLEAN,
"the artifact corresponds to its source (VF-2)"
);
}
#[test]
fn verify_internal_catches_tamper_with_no_source() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("y");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let mut bytes = std::fs::read(&out).unwrap();
let last = bytes.len() - 1;
bytes[last] ^= 0xff; std::fs::write(&out, &bytes).unwrap();
assert_eq!(
run_verify(&out, None, None, Format::Machine),
exit::VERIFY_FAILED,
"a tampered artifact fails integrity, no source needed (VF-3)"
);
}
#[test]
fn verify_against_source_detects_a_mismatch() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("x");
let other = simple_repo("z"); let other_out = other.dir.join("o.ir");
assert_eq!(
run_decode(
SourceKind::Git,
other.path(),
&other_out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
assert_eq!(
run_verify(&other_out, Some(repo.path()), None, Format::Human),
exit::VERIFY_FAILED
);
}
#[test]
fn a_failing_internal_check_with_a_corresponding_source_still_reports_against_source_separately() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("sep");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let mut bytes = std::fs::read(&out).unwrap();
let last = bytes.len() - 1;
bytes[last] ^= 0xff;
std::fs::write(&out, &bytes).unwrap();
assert_eq!(
run_verify(&out, Some(repo.path()), None, Format::Human),
exit::VERIFY_FAILED
);
}
#[test]
fn against_source_with_a_nonexistent_path_is_not_checked_not_a_mismatch() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("nonexistent-source");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let bogus = std::env::temp_dir().join("brygge-cli-does-not-exist-at-all");
assert_eq!(
run_verify(&out, Some(&bogus), None, Format::Machine),
exit::FAILURE,
"a source that cannot be re-decoded is 'not checked' — a runtime failure (exit 1), never a \
claimed mismatch (review 003 R-1)"
);
}
#[test]
fn a_genuine_internal_failure_dominates_the_exit_even_when_against_source_is_also_not_checked() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let a1 = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
b.add_atom(AtomDraft {
parents: vec![a1],
ops: vec![PathOp::Modify {
path: "never-added".into(),
blob,
mode: 0o100_644,
status: EpistemicStatus::Stated,
}],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c2"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
let path = tmp_ir_path("notchecked-plus-fail");
std::fs::write(&path, brygge_ir::to_bytes(&ir)).unwrap();
let bogus = std::env::temp_dir().join("brygge-cli-does-not-exist-at-all-2");
assert_eq!(
run_verify(&path, Some(&bogus), None, Format::Machine),
exit::VERIFY_FAILED,
"a genuine internal failure dominates the exit class even though against-source also could \
not be checked (review 003 R-1)"
);
let _ = std::fs::remove_file(&path);
}
#[test]
fn decode_a_real_clone_succeeds_and_verifies_against_source() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let origin = simple_repo("clone-src");
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let clone_dir =
std::env::temp_dir().join(format!("brygge-cli-clone-{}-{n}", std::process::id()));
let _cleanup = CleanupDir(clone_dir.clone());
let status = PCommand::new("git")
.args(["clone", "-q"])
.arg(origin.path())
.arg(&clone_dir)
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_SYSTEM", "/dev/null")
.status()
.expect("run git clone");
assert!(status.success(), "git clone failed");
assert!(
clone_dir.join(".git/refs/remotes/origin/HEAD").exists(),
"a real clone creates the exact symbolic ref CR-16 covers"
);
let clone_out = clone_dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
&clone_dir,
&clone_out,
false,
false,
None,
Format::Machine
),
exit::CLEAN,
"a clone's symbolic HEAD is a representation-class drop, still a clean import (CR-16)"
);
assert!(clone_out.exists());
assert_eq!(
run_verify(&clone_out, Some(&clone_dir), None, Format::Human),
exit::CLEAN,
"verify --against-source succeeds on a real clone (CR-16)"
);
let origin_out = origin.dir.join("origin-out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
origin.path(),
&origin_out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let origin_ir = brygge_ir::from_bytes(&std::fs::read(&origin_out).unwrap())
.unwrap()
.ir;
let clone_ir = brygge_ir::from_bytes(&std::fs::read(&clone_out).unwrap())
.unwrap()
.ir;
let origin_ids: std::collections::BTreeSet<_> = origin_ir
.atoms
.iter()
.map(|a| a.source.atom_id.clone())
.collect();
let clone_ids: std::collections::BTreeSet<_> = clone_ir
.atoms
.iter()
.map(|a| a.source.atom_id.clone())
.collect();
assert_eq!(
origin_ids, clone_ids,
"the clone carries exactly the origin's commits"
);
}
#[test]
fn guard_decoder_catches_a_panic_and_returns_a_typed_fault() {
let result = guard_decoder(|| -> u32 { panic!("boom") });
match result {
Err(msg) => {
assert!(msg.contains("internal decoder fault"), "{msg}");
assert!(msg.contains("boom"), "{msg}");
assert!(msg.contains("brygge bug"), "{msg}");
}
Ok(_) => panic!("expected the panic to be caught, not propagated"),
}
}
#[test]
fn guard_decoder_passes_through_a_normal_result() {
assert_eq!(guard_decoder(|| 42u32), Ok(42));
}
#[test]
fn decode_of_a_submodule_exits_floor_refusal() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("s");
let head = repo.git(&["rev-parse", "HEAD"]);
repo.git(&[
"update-index",
"--add",
"--cacheinfo",
&format!("160000,{head},sub"),
]);
repo.git(&[
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"-m",
"add gitlink",
]);
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Human
),
exit::FLOOR_REFUSAL
);
assert!(!out.exists(), "no artifact is written on a refusal");
}
#[test]
fn a_resource_limit_from_every_decoder_maps_to_floor_refusal_with_the_stated_message() {
let (code, msg) = map_cvs_error(CvsError::ResourceLimit {
what: "an RCS file".to_string(),
ceiling: "100 bytes".to_string(),
});
assert_eq!(code, exit::FLOOR_REFUSAL);
assert_eq!(
msg,
"refused: an RCS file exceeds brygge's ceiling (100 bytes)"
);
let (code, msg) = map_svn_error(SvnError::ResourceLimit {
what: "the dumpstream".to_string(),
ceiling: "100 bytes".to_string(),
});
assert_eq!(code, exit::FLOOR_REFUSAL);
assert_eq!(
msg,
"refused: the dumpstream exceeds brygge's ceiling (100 bytes)"
);
let (code, msg) = map_git_error(GitError::ResourceLimit {
what: "a blob".to_string(),
ceiling: "100 bytes".to_string(),
});
assert_eq!(code, exit::FLOOR_REFUSAL);
assert_eq!(msg, "refused: a blob exceeds brygge's ceiling (100 bytes)");
let (code, msg) = map_hg_error(HgError::ResourceLimit {
what: "a decompressed revision".to_string(),
ceiling: "100 bytes".to_string(),
});
assert_eq!(code, exit::FLOOR_REFUSAL);
assert_eq!(
msg,
"refused: a decompressed revision exceeds brygge's ceiling (100 bytes)"
);
}
#[test]
fn missing_artifact_is_a_plain_failure() {
let missing = std::env::temp_dir().join("brygge-cli-nope-does-not-exist.ir");
assert_eq!(run_inspect(&missing, false, Format::Human), exit::FAILURE);
assert_eq!(
run_verify(&missing, None, None, Format::Machine),
exit::FAILURE
);
}
#[test]
fn faithfulness_statement_text_per_source() {
let git = faithfulness_statement(SourceKind::Git);
assert!(git.starts_with("Git:"));
assert!(git.contains("--infer-renames"));
assert!(git.contains("Unverifiable"));
let hg = faithfulness_statement(SourceKind::Hg);
assert!(hg.starts_with("Mercurial:"));
assert!(hg.contains("Unverifiable"));
let svn = faithfulness_statement(SourceKind::Svn);
assert!(svn.starts_with("Subversion:"));
assert!(svn.contains("--reconstruct-refs"));
assert!(svn.contains("Unverifiable"));
let cvs = faithfulness_statement(SourceKind::Cvs);
assert!(cvs.starts_with("CVS has no atomic commits"));
assert!(cvs.contains("Unverifiable"));
assert!(cvs.contains("Branch history is imported only with --reconstruct-refs"));
assert!(cvs.contains("otherwise the main line is."));
}
fn tmp_siblings(dir: &Path) -> Vec<PathBuf> {
std::fs::read_dir(dir)
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| {
p.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| n.contains(".brygge-tmp-"))
})
.collect()
}
#[test]
fn a_pre_existing_artifact_survives_a_failing_decode_and_leaves_no_temp_file() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("atomic");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let good_bytes = std::fs::read(&out).unwrap();
let head = repo.git(&["rev-parse", "HEAD"]);
repo.git(&[
"update-index",
"--add",
"--cacheinfo",
&format!("160000,{head},sub"),
]);
repo.git(&[
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"-m",
"break it",
]);
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::FLOOR_REFUSAL
);
assert_eq!(
std::fs::read(&out).unwrap(),
good_bytes,
"the pre-existing artifact is untouched by a failed decode"
);
assert!(
tmp_siblings(&repo.dir).is_empty(),
"no temp file remains after a failure"
);
}
#[test]
fn a_successful_decode_replaces_the_existing_artifact_and_leaves_no_temp_file() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("replace");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let first_bytes = std::fs::read(&out).unwrap();
repo.commit("a.txt", "a third state\n", "c3");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let second_bytes = std::fs::read(&out).unwrap();
assert_ne!(first_bytes, second_bytes, "the artifact was replaced");
assert!(tmp_siblings(&repo.dir).is_empty(), "no temp file remains");
}
#[test]
fn inspect_default_equals_decodes_end_of_run_report() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("eq");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let ir = brygge_ir::from_bytes(&std::fs::read(&out).unwrap())
.unwrap()
.ir;
assert_eq!(
brygge_ir::honesty::summary(&ir).render_human(),
brygge_ir::honesty::summary(&ir).render_human(),
"reproducible (FS-02)"
);
}
#[test]
fn inspect_atoms_adds_the_listing_and_reads_the_artifact_once() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("atoms");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let ir = brygge_ir::from_bytes(&std::fs::read(&out).unwrap())
.unwrap()
.ir;
let human = render_atoms_human(&ir, brygge_ir::version::CURRENT);
assert!(human.contains("atoms (topological order):"));
assert!(human.contains("refs:") || ir.refs.is_empty());
assert!(human.contains("loss boundary:"));
let machine = render_atoms_machine(&ir, brygge_ir::version::CURRENT);
assert!(machine.starts_with("inspect_version=4\n"));
assert!(machine.contains("atom.0.id="));
}
#[test]
fn inspect_report_says_unverifiable_never_unverified() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("unverifiable");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let ir = brygge_ir::from_bytes(&std::fs::read(&out).unwrap())
.unwrap()
.ir;
let human = brygge_ir::honesty::summary(&ir).render_human();
assert!(human.contains("Unverifiable"));
assert!(!human.contains("Unverified"));
}
#[test]
fn representation_drops_appear_only_on_the_not_history_line() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("split");
let out = repo.dir.join("out.ir");
assert_eq!(
run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let ir = brygge_ir::from_bytes(&std::fs::read(&out).unwrap())
.unwrap()
.ir;
let human = brygge_ir::honesty::summary(&ir).render_human();
assert!(human.contains("not history (no content or claim lost):"));
let after_split = human
.split("dropped (recorded loss):")
.nth(1)
.expect("section present");
assert!(!after_split.contains("representation"));
}
fn blank_provenance(kind: brygge_ir::SourceKind, decoder: &str) -> ImportProvenance {
ImportProvenance {
source: brygge_ir::SourceIdentity {
kind,
repo_id: b"r".to_vec(),
atom_id: b"r".to_vec(),
signatures: Vec::new(),
extras: Vec::new(),
},
brygge_version: "0.1.0".into(),
decoder: decoder.into(),
decoder_version: "0.1.0".into(),
params: BTreeMap::new(),
}
}
fn src(kind: brygge_ir::SourceKind, atom: &[u8]) -> brygge_ir::SourceIdentity {
brygge_ir::SourceIdentity {
kind,
repo_id: b"r".to_vec(),
atom_id: atom.to_vec(),
signatures: Vec::new(),
extras: Vec::new(),
}
}
fn stated_add(path: &str, blob: brygge_ir::BlobId) -> PathOp {
PathOp::Add {
path: path.into(),
blob,
mode: 0o100_644,
status: EpistemicStatus::Stated,
}
}
fn verify_bytes(bytes: &[u8]) -> i32 {
let path = tmp_ir_path("verify-check");
std::fs::write(&path, bytes).unwrap();
let code = run_verify(&path, None, None, Format::Machine);
let _ = std::fs::remove_file(&path);
code
}
#[test]
fn verify_integrity_fails_on_a_flipped_byte() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
let mut bytes = brygge_ir::to_bytes(&ir);
let last = bytes.len() - 1;
bytes[last] ^= 0xFF;
assert_eq!(verify_bytes(&bytes), exit::VERIFY_FAILED);
}
#[test]
fn verify_structure_fails_on_a_dangling_parent() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let bogus_parent = brygge_ir::AtomId([0xABu8; 32]);
let _ = b
.add_atom(AtomDraft {
parents: vec![bogus_parent],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
#[test]
fn verify_fails_on_a_duplicate_ref_in_a_hand_built_artifact() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let a1 = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
b.add_ref(RefRecord {
name: "main".into(),
kind: RefKind::Branch,
target: a1,
status: EpistemicStatus::Stated,
source: None,
annotation: None,
})
.unwrap();
b.add_ref(RefRecord {
name: "other".into(),
kind: RefKind::Branch,
target: a1,
status: EpistemicStatus::Stated,
source: None,
annotation: None,
})
.unwrap();
let mut ir = b.finish().unwrap();
let dup = ir.refs[0].clone();
ir.refs.push(dup);
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
#[test]
fn two_ops_for_one_path_is_now_rejected_at_build_time_not_verify_time() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let blob2 = b.add_blob(b"y".to_vec()).unwrap();
let err = b.add_atom(AtomDraft {
parents: vec![],
ops: vec![
stated_add("a", blob),
PathOp::Modify {
path: "a".into(),
blob: blob2,
mode: 0o100_644,
status: EpistemicStatus::Stated,
},
],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
});
assert!(
err.is_err(),
"two ops on one path must be rejected at add_atom"
);
}
#[test]
fn verify_replay_fails_on_modify_of_an_absent_path() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![PathOp::Modify {
path: "never-added".into(),
blob,
mode: 0o100_644,
status: EpistemicStatus::Stated,
}],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
#[test]
fn verify_replay_fails_on_add_of_a_present_path() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let a1 = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![a1],
ops: vec![stated_add("a", blob)], copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c2"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
fn derivation_missing_params(kind: DerivationKind) -> Derivation {
Derivation {
kind,
by: "d".into(),
decoder_version: "0.1.0".into(),
params: BTreeMap::new(),
confidence: Some(100),
}
}
#[test]
fn verify_derivations_fails_on_a_missing_required_param() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Derived(derivation_missing_params(
DerivationKind::InferredRename,
)),
})
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
#[test]
fn verify_derivations_fails_on_confidence_over_100() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let mut d = derivation_missing_params(DerivationKind::NormalizedMetadata);
d.confidence = Some(101);
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Derived(d),
})
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
#[test]
fn verify_source_invariants_fails_when_a_cvs_atom_is_stripped_to_stated() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Cvs,
"brygge-decode-cvs",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Cvs, b"a@1.1"),
status: EpistemicStatus::Stated, })
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
#[test]
fn verify_source_invariants_accepts_a_cvs_branch_point_atom() {
use brygge_ir::status::{Derivation, DerivationKind};
let derived = |kind: DerivationKind, params: &[(&str, &str)]| {
EpistemicStatus::Derived(Derivation {
kind,
by: "brygge-decode-cvs".to_string(),
decoder_version: "0.3.0".to_string(),
params: params
.iter()
.map(|(k, v)| ((*k).to_string(), (*v).to_string()))
.collect(),
confidence: None,
})
};
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Cvs,
"brygge-decode-cvs",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let root = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Cvs, b"a@1.1"),
status: {
let mut d = derived(
DerivationKind::ReconstructedChangeset,
&[
("window_secs", "180"),
("cluster_keys", "author,log"),
("confidence_rule", "span-overlap-v1"),
("date_rule", "latest-per-file"),
],
);
if let EpistemicStatus::Derived(x) = &mut d {
x.confidence = Some(100);
}
d
},
})
.unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![root],
ops: vec![stated_add("b", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Cvs, b"branch-point:BR"),
status: derived(
DerivationKind::ReconstructedBranch,
&[("source", "cvs-symbol"), ("line", "BR")],
),
})
.unwrap();
let ir = b.finish().unwrap();
assert!(matches!(check_source_invariants(&ir), CheckOutcome::Pass));
}
#[test]
fn verify_provenance_fails_on_an_empty_decoder() {
let mut b = IrBuilder::new(blank_provenance(brygge_ir::SourceKind::Git, ""));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
#[test]
fn verify_loss_boundary_fails_on_an_empty_what() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
b.set_loss(LossBoundary {
dropped: vec![DropRecord {
class: LossClass::Representation,
what: String::new(),
reason: "something".into(),
step: None,
}],
});
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
}
#[test]
fn a_well_formed_artifact_passes_every_check() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let a1 = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims {
author: Some(Identity {
name: brygge_ir::model::Text::utf8("A"),
email: Some(brygge_ir::model::Text::utf8("a@example.com")),
}),
..MetadataClaims::default()
},
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
b.add_ref(RefRecord {
name: "main".into(),
kind: RefKind::Branch,
target: a1,
status: EpistemicStatus::Stated,
source: None,
annotation: None,
})
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::CLEAN);
}
#[test]
fn replay_of_a_linear_10000_atom_history_clones_nothing_and_peaks_at_one() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let mut parent = None;
for i in 0..10_000u32 {
let blob = b.add_blob(format!("content-{i}").into_bytes()).unwrap();
let path = format!("f{i}.txt");
let atom = b
.add_atom(AtomDraft {
parents: parent.into_iter().collect(),
ops: vec![stated_add(&path, blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, format!("c{i}").as_bytes()),
status: EpistemicStatus::Stated,
})
.unwrap();
parent = Some(atom);
}
let ir = b.finish().unwrap();
let (outcome, stats) = replay_ir(&ir);
assert!(matches!(outcome, CheckOutcome::Pass));
assert_eq!(
stats.peak, 1,
"a linear history retains exactly one tree at a time (review 003 R-3)"
);
assert_eq!(
stats.clones, 0,
"a linear history clones no tree at all — every reference is a last reference (review 003 R-3)"
);
}
#[test]
fn replay_of_a_branch_point_clones_once_for_the_earlier_sibling_only() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"root".to_vec()).unwrap();
let root = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("root.txt", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"root"),
status: EpistemicStatus::Stated,
})
.unwrap();
let blob_a = b.add_blob(b"a".to_vec()).unwrap();
let _child_a = b
.add_atom(AtomDraft {
parents: vec![root],
ops: vec![stated_add("a.txt", blob_a)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"a"),
status: EpistemicStatus::Stated,
})
.unwrap();
let blob_b = b.add_blob(b"b".to_vec()).unwrap();
let _child_b = b
.add_atom(AtomDraft {
parents: vec![root],
ops: vec![stated_add("b.txt", blob_b)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"b"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
let (outcome, stats) = replay_ir(&ir);
assert!(
matches!(outcome, CheckOutcome::Pass),
"both children replay correctly against independent copies of the root's tree"
);
assert_eq!(
stats.clones, 1,
"exactly one of the two children clones; the other takes the original"
);
}
#[test]
fn a_hostile_commit_message_renders_escaped_in_inspect_atoms_human() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims {
message: Some(brygge_ir::model::Text::utf8(
"clear\x1b[2J and \u{202E}reordered",
)),
..MetadataClaims::default()
},
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
let human = render_atoms_human(&ir, brygge_ir::version::CURRENT);
assert!(!human.contains('\x1b'), "the raw ESC byte must not appear");
assert!(
!human.contains('\u{202E}'),
"the raw RLO char must not appear"
);
assert!(human.contains("\\u{001b}"));
assert!(human.contains("\\u{202e}"));
}
#[test]
fn a_line_separator_in_a_commit_subject_renders_escaped_in_inspect_atoms_human() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims {
message: Some(brygge_ir::model::Text::utf8(
"subject\u{2028}forged second line",
)),
..MetadataClaims::default()
},
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
let ir = b.finish().unwrap();
let human = render_atoms_human(&ir, brygge_ir::version::CURRENT);
assert!(
!human.contains('\u{2028}'),
"the raw line separator must not appear"
);
assert!(human.contains("\\u{2028}"));
}
#[test]
fn a_ref_named_with_equals_cannot_forge_a_machine_key() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let a1 = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: EpistemicStatus::Stated,
})
.unwrap();
b.add_ref(RefRecord {
name: "a=b".into(),
kind: RefKind::Branch,
target: a1,
status: EpistemicStatus::Stated,
source: None,
annotation: None,
})
.unwrap();
let ir = b.finish().unwrap();
let machine = render_atoms_machine(&ir, brygge_ir::version::CURRENT);
assert!(machine.contains("ref.0.name=a%3Db"));
for line in machine.lines() {
if let Some((key, _)) = line.split_once('=') {
assert!(!key.contains("a=b"), "line: {line}");
}
}
}
fn svnadmin_available() -> bool {
PCommand::new("svnadmin")
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
fn svn_props(pairs: &[(&str, &str)]) -> Vec<u8> {
let mut b = Vec::new();
for (k, v) in pairs {
b.extend(format!("K {}\n", k.len()).into_bytes());
b.extend(k.as_bytes());
b.push(b'\n');
b.extend(format!("V {}\n", v.len()).into_bytes());
b.extend(v.as_bytes());
b.push(b'\n');
}
b.extend(b"PROPS-END\n");
b
}
fn svn_revision(buf: &mut Vec<u8>, num: u64, props: &[(&str, &str)]) {
let p = svn_props(props);
buf.extend(format!("Revision-number: {num}\n").into_bytes());
buf.extend(format!("Prop-content-length: {}\n", p.len()).into_bytes());
buf.extend(format!("Content-length: {}\n\n", p.len()).into_bytes());
buf.extend(p);
buf.extend(b"\n");
}
fn svn_add_file(buf: &mut Vec<u8>, path: &str, content: &[u8]) {
let p = svn_props(&[]);
buf.extend(format!("Node-path: {path}\nNode-kind: file\nNode-action: add\n").into_bytes());
buf.extend(format!("Prop-content-length: {}\n", p.len()).into_bytes());
buf.extend(format!("Text-content-length: {}\n", content.len()).into_bytes());
buf.extend(format!("Content-length: {}\n\n", p.len() + content.len()).into_bytes());
buf.extend(p);
buf.extend(content);
buf.extend(b"\n\n");
}
fn svn_add_dir(buf: &mut Vec<u8>, path: &str, copyfrom: Option<(u64, &str)>) {
buf.extend(format!("Node-path: {path}\nNode-kind: dir\nNode-action: add\n").into_bytes());
if let Some((r, p)) = copyfrom {
buf.extend(format!("Node-copyfrom-rev: {r}\nNode-copyfrom-path: {p}\n").into_bytes());
}
buf.extend(b"\n\n");
}
fn svn_dump_with_branch() -> Vec<u8> {
let mut b = Vec::new();
b.extend(b"SVN-fs-dump-format-version: 2\n\n");
b.extend(b"UUID: 33333333-4444-5555-6666-777777777777\n\n");
svn_revision(&mut b, 0, &[("svn:date", "2024-02-01T00:00:00.000000Z")]);
svn_revision(
&mut b,
1,
&[
("svn:author", "carol"),
("svn:date", "2024-02-02T00:00:00.000000Z"),
("svn:log", "trunk"),
],
);
svn_add_dir(&mut b, "trunk", None);
svn_add_file(&mut b, "trunk/a.txt", b"one\n");
svn_revision(
&mut b,
2,
&[
("svn:author", "carol"),
("svn:date", "2024-02-03T00:00:00.000000Z"),
("svn:log", "branch"),
],
);
svn_add_dir(&mut b, "branches", None);
svn_add_dir(&mut b, "branches/x", Some((1, "trunk")));
b
}
fn write_temp_dump(bytes: &[u8]) -> PathBuf {
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!("brygge-cli-svn-{}-{n}.dump", std::process::id()));
std::fs::write(&path, bytes).unwrap();
path
}
#[test]
fn decode_svn_from_a_dumpfile_reconstructs_refs_and_verifies() {
let dump = write_temp_dump(&svn_dump_with_branch());
let out = tmp_ir_path("svn");
let code = run_decode(
SourceKind::Svn,
&dump,
&out,
false,
true,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
assert!(out.exists());
assert_eq!(run_inspect(&out, false, Format::Human), exit::CLEAN);
assert_eq!(run_verify(&out, None, None, Format::Machine), exit::CLEAN);
assert_eq!(
run_verify(&out, Some(&dump), None, Format::Human),
exit::CLEAN,
"the artifact corresponds to its dumpfile (VF-2)"
);
let _ = std::fs::remove_file(&dump);
let _ = std::fs::remove_file(&out);
}
#[test]
fn decode_svn_flat_layout_with_reconstruct_is_a_convention_violation() {
let mut b = Vec::new();
b.extend(b"SVN-fs-dump-format-version: 2\n\n");
b.extend(b"UUID: 88888888-9999-0000-1111-222222222222\n\n");
svn_revision(&mut b, 0, &[("svn:date", "2024-02-01T00:00:00.000000Z")]);
svn_revision(
&mut b,
1,
&[
("svn:log", "flat"),
("svn:date", "2024-02-02T00:00:00.000000Z"),
],
);
svn_add_file(&mut b, "main.c", b"x\n");
let dump = write_temp_dump(&b);
let out = tmp_ir_path("svn-flat");
let code = run_decode(
SourceKind::Svn,
&dump,
&out,
false,
true,
None,
Format::Machine,
);
assert_eq!(code, exit::CONVENTION_VIOLATION);
let _ = std::fs::remove_file(&dump);
let _ = std::fs::remove_file(&out);
}
#[test]
fn decode_svn_from_a_live_repository_via_svnadmin() {
if !svnadmin_available() {
eprintln!("skipping: svnadmin not on PATH");
return;
}
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let repo = std::env::temp_dir().join(format!("brygge-cli-svnrepo-{}-{n}", std::process::id()));
let _ = std::fs::remove_dir_all(&repo);
assert!(
PCommand::new("svnadmin")
.arg("create")
.arg(&repo)
.status()
.map(|s| s.success())
.unwrap_or(false)
);
use std::io::Write as _;
let mut child = PCommand::new("svnadmin")
.arg("load")
.arg(&repo)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("spawn svnadmin load");
child
.stdin
.take()
.unwrap()
.write_all(&svn_dump_with_branch())
.unwrap();
assert!(child.wait().map(|s| s.success()).unwrap_or(false));
let out = repo.join("out.ir");
let code = run_decode(
SourceKind::Svn,
&repo,
&out,
false,
true,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
assert!(out.exists());
assert_eq!(run_verify(&out, None, None, Format::Machine), exit::CLEAN);
let _ = std::fs::remove_dir_all(&repo);
}
#[test]
fn cr_07_6_verifying_a_dumpfile_decode_against_a_live_repository_is_not_checked() {
if !svnadmin_available() {
eprintln!("skipping: svnadmin not on PATH");
return;
}
let dump = write_temp_dump(&svn_dump_with_branch());
let out = tmp_ir_path("svn-form-mismatch");
let code = run_decode(
SourceKind::Svn,
&dump,
&out,
false,
true,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
assert!(out.exists());
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let repo = std::env::temp_dir().join(format!(
"brygge-cli-svn-form-mismatch-{}-{n}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&repo);
assert!(
PCommand::new("svnadmin")
.arg("create")
.arg(&repo)
.status()
.map(|s| s.success())
.unwrap_or(false)
);
use std::io::Write as _;
let mut child = PCommand::new("svnadmin")
.arg("load")
.arg(&repo)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("spawn svnadmin load");
child
.stdin
.take()
.unwrap()
.write_all(&svn_dump_with_branch())
.unwrap();
assert!(child.wait().map(|s| s.success()).unwrap_or(false));
assert_eq!(
run_verify(&out, Some(&repo), None, Format::Machine),
exit::FAILURE,
"a cross-form comparison must be incomplete, never pass or fail"
);
let ir = read_ir(&out).unwrap().ir;
let against = run_against_source(&ir, &repo, &brygge_ir::Storage::Memory);
assert_eq!(against.machine_label(), "not-checked");
assert_eq!(
against.detail(),
Some("the artifact was made from a dumpfile; verify against the same form")
);
let verdict = Verdict::from(true, &against);
assert_eq!(verdict.machine_label(), "incomplete");
let _ = std::fs::remove_file(&dump);
let _ = std::fs::remove_file(&out);
let _ = std::fs::remove_dir_all(&repo);
}
#[test]
fn cr_07_6_r1_an_svnadmin_version_difference_alone_does_not_fail_verification() {
let dump = write_temp_dump(&svn_dump_with_branch());
let out = tmp_ir_path("svn-version-note");
let code = run_decode(
SourceKind::Svn,
&dump,
&out,
false,
true,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
let fresh = |version: &str, brygge_version: Option<&str>| {
let mut ir = read_ir(&out).unwrap().ir;
ir.provenance
.params
.insert("svnadmin_version".to_string(), version.to_string());
if let Some(v) = brygge_version {
ir.provenance.brygge_version = v.to_string();
}
ir
};
assert_eq!(
svn_version_note(&fresh("1.14.1", None), &fresh("1.14.1", None)),
None
);
assert_eq!(
align_svnadmin_version(&fresh("1.14.1", None), fresh("1.14.1", None)),
fresh("1.14.1", None)
);
assert_eq!(
svn_version_note(&fresh("1.14.1", None), &fresh("1.14.2", None)),
Some("1.14.1 vs 1.14.2".to_string())
);
let aligned = align_svnadmin_version(&fresh("1.14.1", None), fresh("1.14.2", None));
assert_eq!(
fresh("1.14.1", None),
aligned,
"aligning the version param must make an otherwise-identical IR equal"
);
let ir3 = fresh("1.14.2", Some("9.9.9-different"));
let aligned3 = align_svnadmin_version(&fresh("1.14.1", None), ir3);
assert_ne!(
fresh("1.14.1", None),
aligned3,
"a genuine content difference must survive version alignment"
);
let _ = std::fs::remove_file(&dump);
let _ = std::fs::remove_file(&out);
}
#[test]
fn a_version_note_on_success_is_its_own_key_never_a_failure_detail() {
let checks: Vec<(&'static str, CheckOutcome)> = vec![("integrity", CheckOutcome::Pass)];
let against = AgainstSourceOutcome::Corresponds(Some("1.14.1 vs 1.14.2".to_string()));
let verdict = Verdict::from(true, &against);
let lines = verify_machine_lines(&checks, true, &against, &verdict, 0);
assert!(lines.contains(&"verify.against_source=corresponds".to_string()));
assert!(lines.contains(&"verify.against_source.note=1.14.1%20vs%201.14.2".to_string()));
assert!(lines.contains(&"verify.result=pass".to_string()));
assert!(
!lines.iter().any(|l| l.contains(".detail")),
"a note is not a failure detail: {lines:?}"
);
let against = AgainstSourceOutcome::DoesNotCorrespond("atom count differs".to_string());
let verdict = Verdict::from(true, &against);
let lines = verify_machine_lines(&checks, true, &against, &verdict, 0);
assert!(
lines
.iter()
.any(|l| l == "verify.against_source.detail=atom%20count%20differs")
);
assert!(!lines.iter().any(|l| l.contains(".note")));
}
fn cvs_single_rev(author: &str, date: &str, log: &str, content: &str) -> Vec<u8> {
format!(
"head\t1.1;\naccess;\nsymbols;\nlocks; strict;\n\n\n\
1.1\ndate\t{date};\tauthor {author};\tstate Exp;\nbranches;\nnext\t;\n\n\n\
desc\n@@\n\n\n\
1.1\nlog\n@{log}@\ntext\n@{content}@\n"
)
.into_bytes()
}
fn cvs_repo() -> TempRepo {
TempRepo::new_plain()
}
fn write_vfile(repo: &TempRepo, rel: &str, bytes: &[u8]) {
let p = repo.dir.join(format!("{rel},v"));
if let Some(parent) = p.parent() {
std::fs::create_dir_all(parent).unwrap();
}
std::fs::write(p, bytes).unwrap();
}
#[test]
fn decode_cvs_reconstructs_derived_changesets_and_reproduces() {
let repo = cvs_repo();
write_vfile(
&repo,
"a.c",
&cvs_single_rev("alice", "2024.01.01.12.00.00", "add", "aaa\n"),
);
write_vfile(
&repo,
"b.c",
&cvs_single_rev("alice", "2024.01.01.12.00.01", "add", "bbb\n"),
);
let out = repo.dir.join("out.ir");
let code = run_decode(
SourceKind::Cvs,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
assert!(out.exists());
assert_eq!(run_inspect(&out, false, Format::Human), exit::CLEAN);
assert_eq!(run_verify(&out, None, None, Format::Machine), exit::CLEAN);
assert_eq!(
run_verify(&out, Some(repo.path()), None, Format::Human),
exit::CLEAN,
"the CVS reconstruction reproduces from its repository (VF-1)"
);
}
fn cvs_with_one_branch_revision() -> Vec<u8> {
"head\t1.2;\naccess;\nsymbols\n\tBR:1.2.0.2;\nlocks; strict;\n\n\n\
1.2\ndate\t2024.01.02.00.00.00;\tauthor alice;\tstate Exp;\nbranches\n\t1.2.2.1;\nnext\t1.1;\n\n\
1.1\ndate\t2024.01.01.00.00.00;\tauthor alice;\tstate Exp;\nbranches;\nnext\t;\n\n\
1.2.2.1\ndate\t2024.01.03.00.00.00;\tauthor alice;\tstate Exp;\nbranches;\nnext\t;\n\n\n\
desc\n@@\n\n\
1.2\nlog\n@two@\ntext\n@two\n@\n\n\
1.1\nlog\n@one@\ntext\n@d1 1\na1 1\none\n@\n\n\
1.2.2.1\nlog\n@on the branch@\ntext\n@d1 1\na1 1\nbr\n@\n"
.as_bytes()
.to_vec()
}
#[test]
fn a_cvs_repository_with_a_branch_revision_verifies_its_own_artifact() {
let repo = cvs_repo();
write_vfile(&repo, "a.txt", &cvs_with_one_branch_revision());
let out = repo.dir.join("out.ir");
let code = run_decode(
SourceKind::Cvs,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
let ir = brygge_ir::from_bytes(&std::fs::read(&out).unwrap())
.unwrap()
.ir;
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.class == brygge_ir::model::LossClass::Other),
"the branch revision is recorded as a drop"
);
let code = run_verify(&out, None, None, Format::Machine);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"verify must not fail integrity (exit 50): got {code}"
);
}
fn cvs_with_a_nested_branch(p: &str, nest_on_branch: bool) -> Vec<u8> {
let (sym, brs, nest_admin) = if nest_on_branch {
("NEST:1.2.2.1.0.2", "branches\n\t1.2.2.1.2.1;\n", true)
} else {
("NEST:1.2.0.4", "branches;\n", false)
};
let mut s = format!(
"head\t1.2;\naccess;\nsymbols\n\tBR:1.2.0.2\n\t{sym};\nlocks; strict;\n\n\n\
1.2\ndate\t2024.01.02.00.00.00;\tauthor alice;\tstate Exp;\nbranches\n\t1.2.2.1;\nnext\t1.1;\n\n\
1.1\ndate\t2024.01.01.00.00.00;\tauthor alice;\tstate Exp;\nbranches;\nnext\t;\n\n\
1.2.2.1\ndate\t2024.02.01.00.00.00;\tauthor alice;\tstate Exp;\n{brs}next\t1.2.2.2;\n\n\
1.2.2.2\ndate\t2024.02.03.00.00.00;\tauthor alice;\tstate Exp;\nbranches;\nnext\t;\n\n"
);
if nest_admin {
s.push_str("1.2.2.1.2.1\ndate\t2024.03.01.00.00.00;\tauthor alice;\tstate Exp;\nbranches;\nnext\t;\n\n");
}
s.push_str(&format!(
"\ndesc\n@@\n\n\
1.2\nlog\n@edit@\ntext\n@{p}2\n@\n\n\
1.1\nlog\n@start@\ntext\n@d1 1\na1 1\n{p}1\n@\n\n\
1.2.2.1\nlog\n@br one@\ntext\n@d1 1\na1 1\n{p}b1\n@\n\n\
1.2.2.2\nlog\n@br two@\ntext\n@d1 1\na1 1\n{p}b2\n@\n"
));
if nest_admin {
s.push_str(&format!(
"\n1.2.2.1.2.1\nlog\n@nest@\ntext\n@d1 1\na1 1\n{p}n1\n@\n"
));
}
s.into_bytes()
}
#[test]
fn decode_cvs_nested_and_mixed_branches_reproduce_and_verify_against_source() {
let repo = cvs_repo();
write_vfile(&repo, "a.c", &cvs_with_a_nested_branch("a", true));
write_vfile(&repo, "b.c", &cvs_with_a_nested_branch("b", true));
write_vfile(&repo, "c.c", &cvs_with_a_nested_branch("c", false));
let one = repo.dir.join("one.ir");
let two = repo.dir.join("two.ir");
for out in [&one, &two] {
let code = run_decode(
SourceKind::Cvs,
repo.path(),
out,
false,
true,
None,
Format::Machine,
);
assert_eq!(
code,
exit::CONVENTION_VIOLATION,
"the mixed symbol is an approximate branch point (exit 30)"
);
}
assert_eq!(
std::fs::read(&one).unwrap(),
std::fs::read(&two).unwrap(),
"two decodes are identical"
);
let ir = brygge_ir::from_bytes(&std::fs::read(&one).unwrap())
.unwrap()
.ir;
assert!(ir.refs.iter().any(|r| r.name == "NEST"));
assert_eq!(run_verify(&one, None, None, Format::Machine), exit::CLEAN);
assert_eq!(
run_verify(&one, Some(repo.path()), None, Format::Human),
exit::CLEAN,
"the branch reconstruction reproduces from its repository (VF-1)"
);
}
#[test]
fn decode_cvs_whole_import_under_floor_is_refused() {
let repo = cvs_repo();
for i in 0..10 {
let date = format!("2024.03.01.00.{:02}.00", i);
write_vfile(
&repo,
&format!("f{i}.c"),
&cvs_single_rev("alice", &date, "big", "x\n"),
);
}
let out = repo.dir.join("out.ir");
let code = run_decode(
SourceKind::Cvs,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert_eq!(code, exit::FLOOR_REFUSAL);
assert!(!out.exists());
}
#[test]
fn decode_cvs_partial_under_floor_imports_and_exits_convention_violation() {
let repo = cvs_repo();
write_vfile(
&repo,
"x.c",
&cvs_single_rev("alice", "2024.01.01.00.00.00", "tidy", "x\n"),
);
write_vfile(
&repo,
"y.c",
&cvs_single_rev("alice", "2024.01.01.00.00.01", "tidy", "y\n"),
);
write_vfile(
&repo,
"p.c",
&cvs_single_rev("bob", "2024.02.01.00.00.00", "sprawl", "p\n"),
);
write_vfile(
&repo,
"q.c",
&cvs_single_rev("bob", "2024.02.01.00.01.40", "sprawl", "q\n"),
);
write_vfile(
&repo,
"r.c",
&cvs_single_rev("bob", "2024.02.01.00.03.20", "sprawl", "r\n"),
);
let out = repo.dir.join("out.ir");
let code = run_decode(
SourceKind::Cvs,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert_eq!(code, exit::CONVENTION_VIOLATION);
assert!(
out.exists(),
"the import still writes (confident changesets imported)"
);
}
#[test]
fn decode_cvs_cr01_branch_revision_exits_recorded_loss() {
let repo = cvs_repo();
let content = b"head\t1.3;\naccess;\nsymbols;\nlocks; strict;\n\n\n\
1.3\ndate\t2024.01.03.00.00.00;\tauthor alice;\tstate Exp;\nbranches;\nnext\t1.2;\n\n\
1.2\ndate\t2024.01.02.00.00.00;\tauthor alice;\tstate Exp;\nbranches\n\t1.2.2.1;\nnext\t1.1;\n\n\
1.2.2.1\ndate\t2024.01.02.12.00.00;\tauthor alice;\tstate Exp;\nbranches;\nnext\t;\n\n\
1.1\ndate\t2024.01.01.00.00.00;\tauthor alice;\tstate Exp;\nbranches;\nnext\t;\n\n\n\
desc\n@@\n\n\n\
1.3\nlog\n@trunk r3@\ntext\n@line1\nline2\nline3\n@\n\n\n\
1.2\nlog\n@trunk r2@\ntext\n@d3 1\n@\n\n\n\
1.2.2.1\nlog\n@branch commit@\ntext\n@d1 2\na2 1\nBRANCH CONTENT\n@\n\n\n\
1.1\nlog\n@trunk r1@\ntext\n@d2 1\n@\n";
write_vfile(&repo, "f.c", content);
let out = repo.dir.join("out.ir");
let code = run_decode(
SourceKind::Cvs,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert_eq!(code, exit::RECORDED_LOSS);
assert!(out.exists());
}
#[test]
fn verify_cvs_derivations_requires_confidence_rule_as_well_as_date_rule() {
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Cvs,
"brygge-decode-cvs",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let mut params = BTreeMap::new();
params.insert("window_secs".to_string(), "180".to_string());
params.insert("cluster_keys".to_string(), "author,log".to_string());
params.insert("date_rule".to_string(), "latest-per-file".to_string());
let derivation = Derivation {
kind: DerivationKind::ReconstructedChangeset,
by: "brygge-decode-cvs".into(),
decoder_version: "0.1.0".into(),
params,
confidence: Some(90),
};
let _ = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("a", blob)],
copies: vec![],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Cvs, b"a@1.1"),
status: EpistemicStatus::Derived(derivation),
})
.unwrap();
let ir = b.finish().unwrap();
assert_eq!(verify_bytes(&brygge_ir::to_bytes(&ir)), exit::VERIFY_FAILED);
let outcome = check_derivations(&ir);
assert!(outcome.is_fail());
assert_eq!(
outcome.detail(),
Some("a ReconstructedChangeset derivation is missing required param 'confidence_rule'")
);
}
fn hg_available() -> bool {
PCommand::new("hg")
.arg("--version")
.env("HGRCPATH", "/dev/null")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
struct HgRepo {
dir: PathBuf,
}
impl Drop for HgRepo {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.dir);
}
}
impl HgRepo {
fn new() -> Self {
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("brygge-cli-hg-{}-{n}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let r = Self { dir };
r.run(&["init", r.dir.to_str().unwrap()]);
r
}
fn path(&self) -> &Path {
&self.dir
}
fn run(&self, args: &[&str]) {
let ok = PCommand::new("hg")
.env("HGRCPATH", "/dev/null")
.arg("--cwd")
.arg(&self.dir)
.arg("-R")
.arg(&self.dir)
.arg("--config")
.arg("ui.username=A U Thor <a@example.com>")
.args(args)
.output()
.map(|o| o.status.success())
.unwrap_or(false);
assert!(ok, "hg {args:?} failed");
}
fn commit(&self, rel: &str, contents: &str, epoch: &str, msg: &str) {
std::fs::write(self.dir.join(rel), contents).unwrap();
self.run(&["add"]);
self.run(&["commit", "-d", &format!("{epoch} 0"), "-m", msg]);
}
}
#[test]
fn decode_hg_and_verify_a_good_artifact_from_every_decoder() {
if !hg_available() {
eprintln!("skipping: hg not on PATH");
return;
}
let repo = HgRepo::new();
repo.commit("a.txt", "one\n", "1700000000", "c1");
repo.commit("a.txt", "two\n", "1700000100", "c2");
let out = tmp_ir_path("hg");
let code = run_decode(
SourceKind::Hg,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
assert!(out.exists());
assert_eq!(run_verify(&out, None, None, Format::Machine), exit::CLEAN);
assert_eq!(
run_verify(&out, Some(repo.path()), None, Format::Human),
exit::CLEAN,
"the artifact corresponds to its hg source (VF-2)"
);
let _ = std::fs::remove_file(&out);
}
fn scratch_dir(tag: &str) -> PathBuf {
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let d = std::env::temp_dir().join(format!("brygge-atomic-{tag}-{}-{n}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
fn planted_tmp_path(dir: &Path, name: &str) -> PathBuf {
dir.join(format!(".{name}.brygge-tmp-{}", std::process::id()))
}
fn write_bytes(bytes: &'static [u8]) -> impl FnOnce(&mut std::fs::File) -> std::io::Result<u64> {
move |f| {
f.write_all(bytes)?;
Ok(bytes.len() as u64)
}
}
#[cfg(unix)]
#[test]
fn a_planted_symlink_at_the_temp_path_fails_the_write_and_its_target_is_untouched() {
let dir = scratch_dir("symlink");
let victim = dir.join("victim.txt");
std::fs::write(&victim, b"precious").unwrap();
let out = dir.join("out.ir");
let tmp = planted_tmp_path(&dir, "out.ir");
std::os::unix::fs::symlink(&victim, &tmp).unwrap();
let err = atomic_write(&out, write_bytes(b"artifact bytes")).unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::AlreadyExists);
assert_eq!(
std::fs::read(&victim).unwrap(),
b"precious",
"the target was written through"
);
assert!(!out.exists(), "no artifact may be produced");
assert!(
std::fs::symlink_metadata(&tmp)
.unwrap()
.file_type()
.is_symlink()
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_planted_regular_file_at_the_temp_path_is_not_truncated_or_removed() {
let dir = scratch_dir("file");
let out = dir.join("out.ir");
let tmp = planted_tmp_path(&dir, "out.ir");
std::fs::write(&tmp, b"planted").unwrap();
assert!(atomic_write(&out, write_bytes(b"artifact bytes")).is_err());
assert_eq!(std::fs::read(&tmp).unwrap(), b"planted");
assert!(!out.exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_normal_write_still_succeeds_and_leaves_no_temp_file() {
let dir = scratch_dir("ok");
let out = dir.join("out.ir");
atomic_write(&out, write_bytes(b"artifact bytes")).unwrap();
assert_eq!(std::fs::read(&out).unwrap(), b"artifact bytes");
assert!(tmp_siblings(&dir).is_empty());
let _ = std::fs::remove_dir_all(&dir);
}
#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn a_non_utf8_output_file_name_is_written_without_a_lossy_conversion() {
use std::os::unix::ffi::OsStrExt;
let dir = scratch_dir("nonutf8");
let out = dir.join(std::ffi::OsStr::from_bytes(b"out-\xff.ir"));
atomic_write(&out, write_bytes(b"x")).unwrap();
assert_eq!(std::fs::read(&out).unwrap(), b"x");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn help_lists_flagged_in_the_vocabulary_and_no_internal_ids() {
let help = crate::cli::USAGE;
assert!(help.contains("VOCABULARY:") && help.contains("flagged"));
for id in ["FS-", "VF-", "CL-", "CR-", "PR-", "INV-", "RFC"] {
assert!(!help.contains(id), "--help mentions the internal id {id}");
}
}
fn machine_output_fixture() -> Ir {
use brygge_ir::model::{CopyRecord, Flag, FlagKind, LossClass, Text};
let other = |name: &str| {
EpistemicStatus::Derived(Derivation {
kind: DerivationKind::Other(name.to_string()),
by: "d".into(),
decoder_version: "0".into(),
params: BTreeMap::new(),
confidence: None,
})
};
let renamed = EpistemicStatus::Derived(Derivation {
kind: DerivationKind::InferredRename,
by: "d".into(),
decoder_version: "0".into(),
params: BTreeMap::new(),
confidence: Some(90),
});
let mut b = IrBuilder::new(blank_provenance(
brygge_ir::SourceKind::Git,
"brygge-decode-git",
));
let blob = b.add_blob(b"x".to_vec()).unwrap();
let a1 = b
.add_atom(AtomDraft {
parents: vec![],
ops: vec![stated_add("old", blob)],
copies: vec![],
metadata: MetadataClaims {
message: Some(Text {
bytes: b"caf\xe9 100%\nsecond line".to_vec(),
encoding: None,
}),
..MetadataClaims::default()
},
source: src(brygge_ir::SourceKind::Git, b"c1"),
status: other("bespoke kind = x"),
})
.unwrap();
let a2 = b
.add_atom(AtomDraft {
parents: vec![a1],
ops: vec![
PathOp::Delete {
path: "old".into(),
status: EpistemicStatus::Stated,
},
stated_add("new", blob),
],
copies: vec![
CopyRecord {
from: "old".into(),
from_atom: a1,
to: "new".into(),
status: renamed,
},
CopyRecord {
from: "old".into(),
from_atom: a1,
to: "newer".into(),
status: other("odd copy"),
},
],
metadata: MetadataClaims::default(),
source: src(brygge_ir::SourceKind::Git, b"c2"),
status: EpistemicStatus::Stated,
})
.unwrap();
b.add_ref(RefRecord {
name: "refs/odd".into(),
kind: RefKind::Other("phase: draft".into()),
target: a2,
status: other("ref made up"),
source: None,
annotation: None,
})
.unwrap();
b.set_loss(LossBoundary {
dropped: vec![DropRecord {
class: LossClass::AdvisoryUnreliable,
what: "mergeinfo".into(),
reason: "advisory".into(),
step: None,
}],
});
b.add_flag(Flag {
kind: FlagKind::BelowConfidenceFloor,
what: "changesets".into(),
count: 2,
reason: "low".into(),
});
b.finish().unwrap()
}
fn assert_machine_keys_follow_the_rule(text: &str) -> Vec<String> {
let mut keys = Vec::new();
for line in text.lines() {
let (key, _) = line
.split_once('=')
.unwrap_or_else(|| panic!("a machine line without `=`: {line:?}"));
assert!(!key.is_empty(), "empty key in {line:?}");
for seg in key.split('.') {
assert!(
!seg.is_empty()
&& seg
.bytes()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'_'),
"key {key:?} breaks `^[a-z0-9_]+(\\.[a-z0-9_]+)*$` (line {line:?})"
);
}
keys.push(key.to_string());
}
keys
}
fn every_verify_line_shape() -> Vec<String> {
let checks: Vec<(&'static str, CheckOutcome)> = CHECK_NAMES
.iter()
.enumerate()
.map(|(i, n)| {
let o = match i {
0 => CheckOutcome::Pass,
1 => CheckOutcome::Fail("a detail = with\nweird bytes".to_string()),
_ => CheckOutcome::NotChecked("the artifact failed to decode".to_string()),
};
(*n, o)
})
.collect();
let against = AgainstSourceOutcome::Corresponds(Some("1.14.1 vs 1.14.2".to_string()));
let verdict = Verdict::from(false, &against);
verify_machine_lines(&checks, false, &against, &verdict, 2)
}
#[test]
fn part2_5_1_keys_that_embed_a_label_are_snake_case_and_values_stay_kebab_case() {
let ir = machine_output_fixture();
let report = brygge_ir::honesty::summary(&ir).render_machine();
for key in [
"derived.inferred_rename=",
"derived.other=",
"dropped.advisory_unreliable=",
"flagged.below_confidence_floor=",
] {
assert!(
report.lines().any(|l| l.starts_with(key)),
"{key} in\n{report}"
);
}
let lines = every_verify_line_shape();
assert!(
lines
.iter()
.any(|l| l.starts_with("verify.check.source_invariants="))
);
assert!(
lines
.iter()
.any(|l| l.starts_with("verify.check.loss_boundary="))
);
let atoms = render_atoms_machine(&ir, brygge_ir::version::CURRENT);
assert!(atoms.contains("atom.1.copy.0.status=derived:inferred-rename"));
assert!(lines.iter().any(|l| l == "verify.check.replay=not-checked"));
}
#[test]
fn part2_5_2_inspect_atoms_does_not_repeat_the_atoms_key() {
let ir = machine_output_fixture();
let report = brygge_ir::honesty::summary(&ir).render_machine();
let atoms = render_atoms_machine(&ir, brygge_ir::version::CURRENT);
assert_eq!(
report.lines().filter(|l| l.starts_with("atoms=")).count(),
1
);
assert!(
!atoms.lines().any(|l| l.starts_with("atoms=")),
"the listing repeats `atoms=`:\n{atoms}"
);
let all = format!("{report}{atoms}");
let keys = assert_machine_keys_follow_the_rule(&all);
let mut seen = std::collections::BTreeSet::new();
for k in &keys {
assert!(seen.insert(k.clone()), "duplicate key {k}");
}
}
#[test]
fn part2_5_3_atom_message_is_the_raw_bytes_percent_encoded_once() {
let ir = machine_output_fixture();
let atoms = render_atoms_machine(&ir, brygge_ir::version::CURRENT);
assert!(
atoms.contains("atom.0.message=caf%E9%20100%25%0Asecond%20line\n"),
"{atoms}"
);
assert!(!atoms.contains("byte(s)") && !atoms.contains("\\u{"));
let human = render_atoms_human(&ir, brygge_ir::version::CURRENT);
assert!(!human.contains('\u{fffd}'));
assert!(atoms.contains("atom.1.message=\n"));
}
#[test]
fn part2_5_4_details_belong_to_their_check_and_follow_its_line() {
let lines = every_verify_line_shape();
let at = |prefix: &str| {
lines
.iter()
.position(|l| l.starts_with(prefix))
.unwrap_or_else(|| panic!("no {prefix} in {lines:?}"))
};
let structure = at("verify.check.structure=");
assert_eq!(
at("verify.check.structure.detail="),
structure + 1,
"the detail follows its own check's line"
);
assert!(
!lines
.iter()
.any(|l| l.starts_with("verify.check.integrity.detail"))
);
assert!(
!lines
.iter()
.any(|l| l.starts_with("verify.detail.") || l.starts_with("verify.note."))
);
assert!(
lines
.iter()
.any(|l| l.starts_with("verify.against_source.note="))
);
assert!(
!lines
.iter()
.any(|l| l.starts_with("verify.against_source.detail"))
);
assert!(lines.contains(
&"verify.check.structure.detail=a%20detail%20%3D%20with%0Aweird%20bytes".to_string()
));
}
#[test]
fn part2_5_5_a_check_that_could_not_run_is_not_checked_never_n_a() {
let lines = every_verify_line_shape();
assert!(lines.contains(&"verify.check.provenance=not-checked".to_string()));
assert!(!lines.iter().any(|l| l.contains("n/a")));
assert_eq!(
CheckOutcome::NotChecked(String::new()).label(),
"not-checked"
);
assert_eq!(
AgainstSourceOutcome::NotChecked(String::new()).machine_label(),
"not-checked"
);
assert_eq!(AgainstSourceOutcome::NotRun.machine_label(), "not-run");
}
#[test]
fn part2_5_6_skipped_non_critical_fields_are_visible_to_machines() {
let ir = machine_output_fixture();
let report = brygge_ir::honesty::summary(&ir);
assert!(
report
.render_machine()
.lines()
.any(|l| l == "skipped_non_critical_fields=0")
);
assert!(
report
.with_skipped_non_critical_fields(4)
.render_machine()
.lines()
.any(|l| l == "skipped_non_critical_fields=4")
);
let lines = every_verify_line_shape();
assert!(lines.contains(&"verify.skipped_non_critical_fields=2".to_string()));
let none = verify_machine_lines(
&[("integrity", CheckOutcome::Pass)],
true,
&AgainstSourceOutcome::NotRun,
&Verdict::Pass,
0,
);
assert!(none.contains(&"verify.skipped_non_critical_fields=0".to_string()));
}
#[test]
fn part2_5_7_other_is_not_flattened_the_label_is_other_and_the_name_has_a_companion_key() {
let ir = machine_output_fixture();
let atoms = render_atoms_machine(&ir, brygge_ir::version::CURRENT);
for expected in [
"atom.0.status=derived:other\n",
"atom.0.status_other=bespoke%20kind%20%3D%20x\n",
"atom.1.copy.1.status=derived:other\n",
"atom.1.copy.1.status_other=odd%20copy\n",
"ref.0.kind=other\n",
"ref.0.kind_other=phase:%20draft\n",
"ref.0.status=derived:other\n",
"ref.0.status_other=ref%20made%20up\n",
] {
assert!(atoms.contains(expected), "missing {expected:?} in\n{atoms}");
}
assert!(!atoms.contains("atom.1.status_other"));
assert!(!atoms.contains("atom.1.copy.0.status_other"));
}
#[test]
fn part2_5_versions_are_bumped() {
assert_eq!(brygge_ir::honesty::REPORT_VERSION, 3);
assert_eq!(INSPECT_VERSION, 4);
assert_eq!(VERIFY_VERSION, 4);
assert!(every_verify_line_shape().contains(&"verify_version=4".to_string()));
assert!(
render_atoms_machine(&machine_output_fixture(), brygge_ir::version::CURRENT)
.starts_with("inspect_version=4\n")
);
}
#[test]
fn part2_5_every_machine_key_matches_the_key_rule_across_every_key_family() {
let ir = machine_output_fixture();
let mut keys =
assert_machine_keys_follow_the_rule(&brygge_ir::honesty::summary(&ir).render_machine());
keys.extend(assert_machine_keys_follow_the_rule(&render_atoms_machine(
&ir,
brygge_ir::version::CURRENT,
)));
keys.extend(assert_machine_keys_follow_the_rule(
&every_verify_line_shape().join("\n"),
));
for family in [
"report_version",
"skipped_non_critical_fields",
"derived.inferred_rename",
"dropped.advisory_unreliable",
"flagged.below_confidence_floor",
"inspect_version",
"atom.0.status_other",
"atom.1.copy.0.from_atom",
"ref.0.kind_other",
"loss.0.class",
"flag.0.count",
"verify.check.source_invariants",
"verify.check.structure.detail",
"verify.against_source.note",
"verify.skipped_non_critical_fields",
"verify.result",
] {
assert!(
keys.iter().any(|k| k == family),
"family {family} not exercised: {keys:?}"
);
}
}
#[test]
fn an_hg_artifacts_params_carry_no_rename_or_infer_keys() {
if !hg_available() {
eprintln!("skipping: hg not on PATH");
return;
}
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let repo = std::env::temp_dir().join(format!("brygge-cli-hg-{}-{n}", std::process::id()));
let _guard = CleanupDir(repo.clone());
let _ = std::fs::remove_dir_all(&repo);
std::fs::create_dir_all(&repo).unwrap();
let hg = |args: &[&str]| {
let out = PCommand::new("hg")
.current_dir(&repo)
.args(args)
.env("HGRCPATH", "/dev/null")
.env("HGUSER", "A U Thor <a@example.com>")
.output()
.expect("run hg");
assert!(
out.status.success(),
"hg {args:?}: {}",
String::from_utf8_lossy(&out.stderr)
);
};
hg(&["init"]);
std::fs::write(repo.join("a.txt"), "one\n").unwrap();
hg(&["add", "a.txt"]);
hg(&["commit", "-d", "0 0", "-m", "c1"]);
hg(&["mv", "a.txt", "b.txt"]);
hg(&["commit", "-d", "1 0", "-m", "c2 (a recorded rename)"]);
let out = repo
.join("..")
.join(format!("brygge-hg-params-{}-{n}.ir", std::process::id()));
assert_eq!(
run_decode(
SourceKind::Hg,
&repo,
&out,
false,
false,
None,
Format::Machine
),
exit::CLEAN
);
let ir = brygge_ir::from_bytes(&std::fs::read(&out).unwrap())
.unwrap()
.ir;
let keys: Vec<&String> = ir.provenance.params.keys().collect();
assert!(
!keys
.iter()
.any(|k| k.starts_with("rename_") || k.as_str() == "infer_renames"),
"an hg artifact must not claim an inference that never ran: {keys:?}"
);
assert!(
ir.atoms
.iter()
.flat_map(|a| &a.copies)
.any(|c| c.from == "a.txt" && c.to == "b.txt" && !c.status.is_derived())
);
assert_eq!(
run_verify(&out, Some(&repo), None, Format::Machine),
exit::CLEAN
);
let _ = std::fs::remove_file(&out);
}
fn plain_opts() -> SourceOpts {
SourceOpts {
infer_renames: false,
reconstruct_refs: false,
layout: LayoutPolicy::default(),
cvs_window: 300,
cvs_floor: 50,
}
}
const ALL_KINDS: [SourceKind; 4] = [
SourceKind::Git,
SourceKind::Hg,
SourceKind::Svn,
SourceKind::Cvs,
];
#[test]
fn a_missing_source_path_is_source_not_found_for_every_kind() {
let missing = std::env::temp_dir().join(format!("brygge-cli-missing-{}", std::process::id()));
let want = format!("source not found: {}", missing.display());
for kind in ALL_KINDS {
match decode_source(kind, &missing, &plain_opts(), &brygge_ir::Storage::Memory) {
Err((code, msg)) => {
assert_eq!(code, exit::FAILURE, "{kind:?}");
assert!(msg.contains(&want), "{kind:?}: {msg}");
assert!(!msg.contains("svnadmin"), "{kind:?} ran nothing: {msg}");
}
Ok(_) => panic!("{kind:?}: decoded a source that does not exist"),
}
}
}
#[test]
fn a_url_is_still_a_floor_refusal_not_source_not_found() {
match decode_source(
SourceKind::Svn,
Path::new("svn://example.org/r"),
&plain_opts(),
&brygge_ir::Storage::Memory,
) {
Err((code, msg)) => {
assert_eq!(code, exit::FLOOR_REFUSAL);
assert!(msg.contains("remote-source"), "{msg}");
assert!(msg.contains("svnrdump dump <url> > repo.dump"), "{msg}");
assert!(!msg.contains("source not found"), "{msg}");
}
Ok(_) => panic!("decoded a URL"),
}
match decode_source(
SourceKind::Cvs,
Path::new(":pserver:x@example.org:/cvs"),
&plain_opts(),
&brygge_ir::Storage::Memory,
) {
Err((code, _)) => assert_eq!(code, exit::FLOOR_REFUSAL),
Ok(_) => panic!("decoded a :pserver: source"),
}
}
#[cfg(unix)]
#[test]
fn an_svn_fifo_is_neither_a_dumpfile_nor_a_repository_and_does_not_block() {
let path = std::env::temp_dir().join(format!("brygge-cli-fifo-{}", std::process::id()));
if !PCommand::new("mkfifo")
.arg(&path)
.status()
.is_ok_and(|s| s.success())
{
eprintln!("skipping: mkfifo is not available");
return;
}
let (tx, rx) = std::sync::mpsc::channel();
let p = path.clone();
std::thread::spawn(move || {
let _ = tx.send(
decode_source(
SourceKind::Svn,
&p,
&plain_opts(),
&brygge_ir::Storage::Memory,
)
.map(|_| ()),
);
});
let got = rx
.recv_timeout(std::time::Duration::from_secs(10))
.expect("refused without blocking on the FIFO");
let _ = std::fs::remove_file(&path);
match got {
Err((code, msg)) => {
assert_eq!(code, exit::FAILURE);
assert!(
msg.contains("SVN source is neither a dumpfile nor a repository directory"),
"{msg}"
);
}
Ok(()) => panic!("decoded a FIFO"),
}
}
#[test]
fn against_source_with_a_missing_path_is_not_checked_with_the_same_wording() {
let missing =
std::env::temp_dir().join(format!("brygge-cli-missing-vs-{}", std::process::id()));
let want = format!("source not found: {}", missing.display());
for (kind, decoder) in [
(brygge_ir::SourceKind::Git, "brygge-decode-git"),
(brygge_ir::SourceKind::Hg, "brygge-decode-hg"),
(brygge_ir::SourceKind::Svn, "brygge-decode-svn"),
(brygge_ir::SourceKind::Cvs, "brygge-decode-cvs"),
] {
let is_svn = kind == brygge_ir::SourceKind::Svn;
let label = decoder;
let mut prov = blank_provenance(kind, decoder);
if is_svn {
prov.params.insert("source_form".into(), "dumpfile".into());
}
let ir = IrBuilder::new(prov).finish().unwrap();
match run_against_source(&ir, &missing, &brygge_ir::Storage::Memory) {
AgainstSourceOutcome::NotChecked(detail) => {
assert!(detail.contains(&want), "{label}: {detail}");
assert!(
!detail.contains("verify against the same form"),
"{label}: {detail}"
);
}
_ => panic!("{label}: expected NotChecked"),
}
}
}
#[test]
fn verify_against_source_corresponds_on_a_0_2_0_artifact() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.commit("a.txt", "cli-0.2.0-fixture one\n", "c1");
r.commit("a.txt", "cli-0.2.0-fixture two\n", "c2");
let fixture =
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/contract-0.2.0-simple-repo.ir");
let bytes = std::fs::read(&fixture).unwrap();
assert_eq!(
&bytes[9..12],
&[0u8, 2, 0],
"the fixture is contract 0.2.0 (major 0, minor 2, patch 0)"
);
assert_eq!(
run_verify(&fixture, Some(r.path()), None, Format::Human),
exit::CLEAN,
"verify --against-source corresponds on a 0.2.0 artifact, against the repository it was \
decoded from (review 099 R-4)"
);
let decoded = brygge_ir::from_file(&fixture).unwrap();
match run_against_source(&decoded.ir, r.path(), &brygge_ir::Storage::Memory) {
AgainstSourceOutcome::Corresponds(note) => assert_version_note("0.6.0", note),
other => panic!("expected Corresponds across brygge versions, got {other:?}"),
}
}
fn assert_version_note(fixture_version: &str, note: Option<String>) {
let this_version = env!("CARGO_PKG_VERSION");
if this_version == fixture_version {
assert_eq!(
note, None,
"this build's own version equals the fixture's: no version gap, no note"
);
} else {
assert_eq!(
note,
Some(format!(
"decoded by brygge {fixture_version}, re-derived by {this_version}; the history is identical"
)),
"the note names both versions and says the history is identical"
);
}
}
#[test]
fn verify_against_source_reproduces_on_a_0_2_0_cvs_artifact_across_brygge_versions() {
let repo = cvs_repo();
write_vfile(
&repo,
"a.c",
&cvs_single_rev("alice", "2024.01.01.12.00.00", "add", "aaa\n"),
);
write_vfile(
&repo,
"b.c",
&cvs_single_rev("alice", "2024.01.01.12.00.01", "add", "bbb\n"),
);
let fixture =
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/contract-0.2.0-cvs-repro.ir");
let decoded = brygge_ir::from_file(&fixture).unwrap();
assert_eq!(decoded.contract, brygge_ir::ContractVersion::new(0, 2, 0));
match run_against_source(&decoded.ir, repo.path(), &brygge_ir::Storage::Memory) {
AgainstSourceOutcome::Reproduces(note) => assert_version_note("0.6.0", note),
other => panic!("expected Reproduces across brygge versions, got {other:?}"),
}
}
#[test]
fn verify_against_source_corresponds_on_a_0_2_0_git_rename_artifact_across_brygge_versions() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.commit("a.txt", "rename-fixture one\n", "c1");
r.commit("b.txt", "rename-fixture two\n", "c2");
r.git(&["mv", "a.txt", "c.txt"]);
r.git(&[
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"-m",
"c3-rename",
]);
let fixture =
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/contract-0.2.0-git-rename.ir");
let decoded = brygge_ir::from_file(&fixture).unwrap();
assert_eq!(decoded.contract, brygge_ir::ContractVersion::new(0, 2, 0));
match run_against_source(&decoded.ir, r.path(), &brygge_ir::Storage::Memory) {
AgainstSourceOutcome::Corresponds(note) => assert_version_note("0.6.0", note),
other => panic!("expected Corresponds across brygge versions, got {other:?}"),
}
}
#[test]
fn encode_accepts_a_0_2_0_artifact() {
let fixture =
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/contract-0.2.0-simple-repo.ir");
let decoded = brygge_ir::from_file(&fixture).expect("the 0.2.0 fixture decodes");
assert_eq!(decoded.contract, brygge_ir::ContractVersion::new(0, 2, 0));
let (_, exit_code) = verify_internal_report(&fixture, Format::Human);
assert_eq!(
exit_code,
exit::CLEAN,
"a 0.2.0 artifact's internal checks must pass before `encode` would run a plugin on it"
);
}
fn rebuild_with_root_author_changed(ir: &brygge_ir::Ir, mark: Option<[u8; 32]>) -> brygge_ir::Ir {
let mut b = IrBuilder::new(ir.provenance.clone());
for id in ir.content.ids() {
let bytes = ir.content.get(id).unwrap().unwrap();
b.add_blob(bytes).unwrap();
}
if let Some(consumed) = mark {
b.add_transform(TransformRecord {
name: "rename-fixer".into(),
version: "1.0.0".into(),
params: BTreeMap::new(),
consumed,
});
}
let mut id_map: BTreeMap<brygge_ir::AtomId, brygge_ir::AtomId> = BTreeMap::new();
for (i, atom) in ir.atoms.iter().enumerate() {
let parents = atom
.parents
.iter()
.map(|p| *id_map.get(p).expect("parents precede their children"))
.collect();
let copies = atom
.copies
.iter()
.map(|c| CopyRecord {
from: c.from.clone(),
from_atom: *id_map
.get(&c.from_atom)
.expect("a copy's from_atom precedes it"),
to: c.to.clone(),
status: c.status.clone(),
})
.collect();
let mut metadata = atom.metadata.clone();
let mut status = atom.status.clone();
if i == 0 {
metadata.author = Some(Identity {
name: brygge_ir::Text::utf8("Transformed Author"),
email: Some(brygge_ir::Text::utf8("transformed@example.com")),
});
if mark.is_some() {
status = EpistemicStatus::Transformed {
step: 0,
changed: vec![ChangedPart::Author],
prior: Box::new(status),
};
}
}
let new_id = b
.add_atom(AtomDraft {
parents,
ops: atom.ops.clone(),
copies,
metadata,
source: atom.source.clone(),
status,
})
.unwrap();
id_map.insert(atom.id, new_id);
}
for rf in &ir.refs {
b.add_ref(RefRecord {
name: rf.name.clone(),
kind: rf.kind.clone(),
target: *id_map.get(&rf.target).expect("a ref's target precedes it"),
status: rf.status.clone(),
source: rf.source.clone(),
annotation: rf.annotation.clone(),
})
.unwrap();
}
b.set_loss(ir.loss.clone());
for flag in &ir.flags {
b.add_flag(flag.clone());
}
b.finish().unwrap()
}
fn write_ir_to(ir: &brygge_ir::Ir, path: &Path) {
std::fs::write(path, brygge_ir::to_bytes(ir)).unwrap();
}
#[test]
fn verify_against_source_root_corresponds_and_wrong_consumed_through_the_real_reader() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("transform-fixture");
let out = tmp_ir_path("transform-root");
let code = run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
let fresh = read_ir(&out).unwrap().ir;
let consumed = brygge_ir::digest_of(&fresh).unwrap();
let honest = tmp_ir_path("transform-root-honest");
write_ir_to(
&rebuild_with_root_author_changed(&fresh, Some(consumed)),
&honest,
);
let ir1 = read_ir(&honest).unwrap().ir;
let against = run_against_source(&ir1, repo.path(), &brygge_ir::Storage::Memory);
assert_eq!(against.machine_label(), "root-corresponds");
assert_eq!(Verdict::from(true, &against).machine_label(), "pass");
assert_eq!(Verdict::from(true, &against).exit_code(), exit::CLEAN);
assert!(
against
.note()
.is_some_and(|n| n.contains("1 transform(s) after it were not re-run")),
"{:?}",
against.note()
);
assert_eq!(
run_verify(&honest, Some(repo.path()), None, Format::Machine),
exit::CLEAN,
"the real reader, check_structure, and the comparison together must still reach root-corresponds"
);
let wrong = tmp_ir_path("transform-root-wrong");
write_ir_to(
&rebuild_with_root_author_changed(&fresh, Some([0u8; 32])),
&wrong,
);
let ir2 = read_ir(&wrong).unwrap().ir;
let against = run_against_source(&ir2, repo.path(), &brygge_ir::Storage::Memory);
assert_eq!(against.machine_label(), "does-not-correspond");
assert_eq!(
against.detail(),
Some("the chain's root is not this source's decode")
);
assert_eq!(
run_verify(&wrong, Some(repo.path()), None, Format::Machine),
exit::VERIFY_FAILED
);
let _ = std::fs::remove_file(&out);
let _ = std::fs::remove_file(&honest);
let _ = std::fs::remove_file(&wrong);
}
#[test]
fn verify_against_source_rejects_a_well_formed_artifact_that_lies_about_being_unchanged() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("transform-forgery");
let out = tmp_ir_path("transform-forgery-base");
let code = run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
let fresh = read_ir(&out).unwrap().ir;
let lie = tmp_ir_path("transform-forgery-lie");
write_ir_to(&rebuild_with_root_author_changed(&fresh, None), &lie);
let (checks, internal_pass) = compute_internal_checks(&brygge_ir::from_file(&lie));
let failing: Vec<&str> = checks
.iter()
.filter(|(_, outcome)| outcome.label() != "pass")
.map(|(name, _)| *name)
.collect();
assert!(
internal_pass,
"a lying-but-well-formed artifact must still pass every internal check: {failing:?}"
);
let liar_ir = read_ir(&lie).unwrap().ir;
let against = run_against_source(&liar_ir, repo.path(), &brygge_ir::Storage::Memory);
assert_eq!(
against.machine_label(),
"does-not-correspond",
"the content really did change, even though nothing in the artifact admits it"
);
assert_eq!(
run_verify(&lie, Some(repo.path()), None, Format::Machine),
exit::VERIFY_FAILED
);
let _ = std::fs::remove_file(&out);
let _ = std::fs::remove_file(&lie);
}
fn rebuild_with_version(
ir: &brygge_ir::Ir,
brygge_version: &str,
decoder_version: &str,
) -> brygge_ir::Ir {
let mut provenance = ir.provenance.clone();
provenance.brygge_version = brygge_version.to_string();
provenance.decoder_version = decoder_version.to_string();
let mut b = IrBuilder::new(provenance);
for id in ir.content.ids() {
let bytes = ir.content.get(id).unwrap().unwrap();
b.add_blob(bytes).unwrap();
}
for t in &ir.transforms {
b.add_transform(t.clone());
}
let mut id_map: BTreeMap<brygge_ir::AtomId, brygge_ir::AtomId> = BTreeMap::new();
for atom in &ir.atoms {
let parents = atom
.parents
.iter()
.map(|p| *id_map.get(p).expect("parents precede their children"))
.collect();
let copies = atom
.copies
.iter()
.map(|c| CopyRecord {
from: c.from.clone(),
from_atom: *id_map
.get(&c.from_atom)
.expect("a copy's from_atom precedes it"),
to: c.to.clone(),
status: c.status.clone(),
})
.collect();
let new_id = b
.add_atom(AtomDraft {
parents,
ops: atom.ops.clone(),
copies,
metadata: atom.metadata.clone(),
source: atom.source.clone(),
status: atom.status.clone(),
})
.unwrap();
id_map.insert(atom.id, new_id);
}
for rf in &ir.refs {
b.add_ref(RefRecord {
name: rf.name.clone(),
kind: rf.kind.clone(),
target: *id_map.get(&rf.target).expect("a ref's target precedes it"),
status: rf.status.clone(),
source: rf.source.clone(),
annotation: rf.annotation.clone(),
})
.unwrap();
}
b.set_loss(ir.loss.clone());
for flag in &ir.flags {
b.add_flag(flag.clone());
}
b.finish().unwrap()
}
#[test]
fn verify_against_source_not_checked_when_recorded_version_differs_and_history_does_too() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("version-mismatch");
let out = tmp_ir_path("version-mismatch-base");
let code = run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
let fresh = read_ir(&out).unwrap().ir;
let edited = tmp_ir_path("version-mismatch-edited");
write_ir_to(
&rebuild_with_version(
&rebuild_with_root_author_changed(&fresh, None),
"0.0.1",
"0.0.1",
),
&edited,
);
let ir1 = read_ir(&edited).unwrap().ir;
let against = run_against_source(&ir1, repo.path(), &brygge_ir::Storage::Memory);
assert_eq!(against.machine_label(), "not-checked");
let detail = against.detail().expect("not-checked carries a detail");
assert!(
detail.contains("decoded by brygge 0.0.1"),
"names the artifact's own version: {detail:?}"
);
assert!(
detail.contains("run `verify --against-source` with brygge 0.0.1"),
"says which brygge to re-run with: {detail:?}"
);
assert_eq!(Verdict::from(true, &against).machine_label(), "incomplete");
assert_eq!(
run_verify(&edited, Some(repo.path()), None, Format::Machine),
exit::FAILURE,
"incomplete is never pass (0), and never fail's exit 50 either"
);
let _ = std::fs::remove_file(&out);
let _ = std::fs::remove_file(&edited);
}
#[test]
fn verify_against_source_root_corresponds_with_both_notes_across_brygge_versions() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("version-chain");
let out = tmp_ir_path("version-chain-base");
let code = run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
let fresh = read_ir(&out).unwrap().ir;
let root_at_0_0_1 = rebuild_with_version(&fresh, "0.0.1", "0.0.1");
let consumed = brygge_ir::digest_of(&root_at_0_0_1).unwrap();
let honest = tmp_ir_path("version-chain-honest");
write_ir_to(
&rebuild_with_root_author_changed(&root_at_0_0_1, Some(consumed)),
&honest,
);
let ir1 = read_ir(&honest).unwrap().ir;
let against = run_against_source(&ir1, repo.path(), &brygge_ir::Storage::Memory);
assert_eq!(against.machine_label(), "root-corresponds");
let note = against.note().expect("root-corresponds carries a note");
assert!(
note.contains("1 transform(s) after it were not re-run"),
"keeps the existing chain note: {note:?}"
);
assert!(
note.contains("decoded by brygge 0.0.1"),
"and adds the version note: {note:?}"
);
assert!(note.ends_with("the history is identical"), "{note:?}");
assert_eq!(Verdict::from(true, &against).machine_label(), "pass");
assert_eq!(
run_verify(&honest, Some(repo.path()), None, Format::Machine),
exit::CLEAN
);
let _ = std::fs::remove_file(&out);
let _ = std::fs::remove_file(&honest);
}
#[test]
fn a_malformed_transform_chain_is_refused_by_the_reader_not_given_a_verdict() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let repo = simple_repo("transform-malformed");
let out = tmp_ir_path("transform-malformed-base");
let code = run_decode(
SourceKind::Git,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
let fresh = read_ir(&out).unwrap().ir;
let consumed = brygge_ir::digest_of(&fresh).unwrap();
let mut malformed = rebuild_with_root_author_changed(&fresh, Some(consumed));
malformed.transforms.clear();
let bad = tmp_ir_path("transform-malformed-bytes");
write_ir_to(&malformed, &bad);
match brygge_ir::from_file(&bad) {
Err(brygge_ir::Error::NonCanonical(m)) => assert!(m.contains("step"), "{m}"),
other => panic!("expected a NonCanonical refusal, got {other:?}"),
}
assert_eq!(
run_verify(&bad, Some(repo.path()), None, Format::Machine),
exit::VERIFY_FAILED,
"a read failure must still exit as a failure, never silently as a verdict of any kind"
);
let _ = std::fs::remove_file(&out);
let _ = std::fs::remove_file(&bad);
}
#[test]
fn verify_against_source_root_reproduces_for_cvs() {
let repo = cvs_repo();
write_vfile(
&repo,
"a.c",
&cvs_single_rev("alice", "2024.01.01.12.00.00", "add", "aaa\n"),
);
let out = repo.dir.join("out.ir");
let code = run_decode(
SourceKind::Cvs,
repo.path(),
&out,
false,
false,
None,
Format::Machine,
);
assert!(
code == exit::CLEAN || code == exit::RECORDED_LOSS,
"unexpected exit {code}"
);
let fresh = read_ir(&out).unwrap().ir;
let consumed = brygge_ir::digest_of(&fresh).unwrap();
let honest = repo.dir.join("transformed.ir");
write_ir_to(
&rebuild_with_root_author_changed(&fresh, Some(consumed)),
&honest,
);
let ir1 = read_ir(&honest).unwrap().ir;
let against = run_against_source(&ir1, repo.path(), &brygge_ir::Storage::Memory);
assert_eq!(against.machine_label(), "root-reproduces");
assert_eq!(Verdict::from(true, &against).machine_label(), "pass");
assert_eq!(
run_verify(&honest, Some(repo.path()), None, Format::Machine),
exit::CLEAN
);
}
#[test]
fn inspect_shows_the_transform_chain_overview() {
let ir = rebuild_with_root_author_changed(&machine_output_fixture(), Some([0x11; 32]));
let human = render_transforms_human(&ir, false);
assert!(human.contains("transforms: 1"), "{human}");
assert!(
human.contains("0. rename-fixer 1.0.0 — consumed sha256:"),
"{human}"
);
assert!(human.contains("changed 1 atom(s)"), "{human}");
assert!(
!human.contains(" "),
"params are not shown without --atoms: {human}"
);
let with_params = render_transforms_human(&ir, true);
assert!(with_params.contains("transforms: 1"), "{with_params}");
let machine = render_transforms_machine(&ir);
assert!(machine.contains("transforms=1"), "{machine}");
assert!(
machine.contains("transform.0.name=rename-fixer"),
"{machine}"
);
assert!(machine.contains("transform.0.version=1.0.0"), "{machine}");
assert!(machine.contains("transform.0.changed_atoms=1"), "{machine}");
assert!(machine.contains("transform.0.changed_refs=0"), "{machine}");
assert!(machine.contains("transform.0.dropped=0"), "{machine}");
assert_eq!(render_transforms_human(&machine_output_fixture(), true), "");
assert_eq!(render_transforms_machine(&machine_output_fixture()), "");
}