mod common;
use std::path::Path;
use std::process::Command;
use common::{TmpDir, fixture_root, ostree_available, ostree_supports_ed25519};
use ostrya::base64;
use ostrya::{
Checksum, DeltaOptions, Ed25519Signer, Ed25519Verifier, Repo, Summary, SummaryOptions, Value,
};
use ostrya_rt::block_on;
const FIXED_EPOCH: u64 = 1_700_000_000;
const COLLECTION_ID: &str = "org.ostrya.Test";
const ANCHOR_COMMIT: &str = "04fd8792152380dd12ef240cda008ef098407791011c01b3dd4f75f9964d6068";
const SECRET_B64: &str =
"o74ME/dmhvDeYf64dDJQY8kX2piK0M/nyIRWVi30i6DCOzRsHVcvgYToz6zOb5OvK/v8nH6KfLR3dfdsn6ZSyQ==";
const PUBLIC_B64: &str = "wjs0bB1XL4GE6M+szm+Tryv7/Jx+iny0d3X3bJ+mUsk=";
fn copy_tree(from: &Path, to: &Path) {
let status = Command::new("cp")
.args(["-a"])
.arg(from)
.arg(to)
.status()
.expect("run cp");
assert!(status.success(), "cp -a {from:?} {to:?} failed");
}
fn writable_fixture(fixture: &str, tag: &str) -> (TmpDir, std::path::PathBuf) {
let tmp = TmpDir::new(tag);
let repo = tmp.path().join("repo");
copy_tree(&fixture_root().join(fixture).join("repo"), &repo);
(tmp, repo)
}
#[test]
fn plain_summary_is_byte_identical_to_the_tool() {
let (_tmp, repo_dir) = writable_fixture("summary", "summary-plain");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: None,
})
.await
.unwrap();
let got = repo.read_summary().await.unwrap().expect("summary written");
let want = std::fs::read(fixture_root().join("summary").join("summary")).unwrap();
assert_eq!(
got, want,
"the port's summary must be byte-identical to the tool's"
);
});
}
#[test]
fn regenerate_removes_a_stale_signature() {
let (_tmp, repo_dir) = writable_fixture("summary", "summary-stale-sig");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: None,
})
.await
.unwrap();
repo.sign_summary(&Ed25519Signer::from_base64(SECRET_B64).unwrap())
.await
.unwrap();
assert!(repo.read_summary_signature().await.unwrap().is_some());
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: None,
})
.await
.unwrap();
assert!(
repo.read_summary_signature().await.unwrap().is_none(),
"regeneration must drop a stale summary.sig"
);
});
}
#[test]
fn collection_summary_and_anchor_match_the_tool() {
let (_tmp, repo_dir) = writable_fixture("summary-collection", "summary-collection");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
assert_eq!(repo.config().collection_id(), Some(COLLECTION_ID));
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: Some(FIXED_EPOCH),
})
.await
.unwrap();
let anchor = repo
.resolve_rev("ostree-metadata", false)
.await
.unwrap()
.unwrap();
assert_eq!(
anchor.to_hex(),
ANCHOR_COMMIT,
"the ostree-metadata anchor commit must match the tool's"
);
let got = repo.read_summary().await.unwrap().expect("summary written");
let want =
std::fs::read(fixture_root().join("summary-collection").join("summary")).unwrap();
assert_eq!(
got, want,
"the port's collection summary must be byte-identical to the tool's"
);
});
}
#[test]
fn sign_and_verify_round_trip() {
let (_tmp, repo_dir) = writable_fixture("summary", "summary-sign");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: None,
})
.await
.unwrap();
repo.sign_summary(&Ed25519Signer::from_base64(SECRET_B64).unwrap())
.await
.unwrap();
let public = base64::decode(PUBLIC_B64).unwrap();
let trusted = Ed25519Verifier::new([public], Vec::<Vec<u8>>::new()).unwrap();
let outcome = repo.verify_summary(&[&trusted]).await.unwrap();
assert!(outcome.valid, "a signed summary must verify with the key");
let wrong = Ed25519Verifier::new([vec![0u8; 32]], Vec::<Vec<u8>>::new()).unwrap();
let outcome = repo.verify_summary(&[&wrong]).await.unwrap();
assert!(!outcome.valid, "a foreign key must not verify the summary");
});
}
#[test]
fn tool_verifies_a_port_signed_summary() {
if !ostree_supports_ed25519() {
eprintln!("skipping: ostree tool has no ed25519 engine");
return;
}
let (_tmp, repo_dir) = writable_fixture("summary", "summary-tool-verify");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: None,
})
.await
.unwrap();
repo.sign_summary(&Ed25519Signer::from_base64(SECRET_B64).unwrap())
.await
.unwrap();
});
let url = format!("file://{}", repo_dir.display());
let add = |name: &str, key: &str| {
let status = Command::new("ostree")
.arg(format!("--repo={}", repo_dir.display()))
.args(["remote", "add", name, &url, "--no-gpg-verify"])
.arg(format!("--sign-verify=ed25519=inline:{key}"))
.status()
.expect("run ostree remote add");
assert!(status.success(), "ostree remote add {name} failed");
};
let remote_summary = |name: &str| {
Command::new("ostree")
.arg(format!("--repo={}", repo_dir.display()))
.args(["remote", "summary", name])
.output()
.expect("run ostree remote summary")
.status
.success()
};
add("good", PUBLIC_B64);
assert!(
remote_summary("good"),
"the tool must verify a summary the port signed"
);
}
#[test]
fn the_summary_advertises_the_deltas_the_repository_holds() {
let (_tmp, repo_dir) = writable_fixture("summary", "summary-deltas");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: None,
})
.await
.unwrap();
let bytes = repo.read_summary().await.unwrap().unwrap();
let summary = Summary::parse(&bytes).unwrap();
assert!(summary.metadata_value("ostree.static-deltas").is_none());
let refs = repo.list_refs(None).await.unwrap();
let (_, to) = refs.first().expect("the fixture holds a ref");
let (_, from) = refs.get(1).expect("the fixture holds a second ref");
let opts = DeltaOptions {
timestamp: Some(FIXED_EPOCH),
..DeltaOptions::default()
};
let scratch_dir = repo.generate_static_delta(None, to, &opts).await.unwrap();
let from_to_dir = repo
.generate_static_delta(Some(from), to, &opts)
.await
.unwrap();
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: None,
})
.await
.unwrap();
let bytes = repo.read_summary().await.unwrap().unwrap();
let summary = Summary::parse(&bytes).unwrap();
let map = summary
.metadata_value("ostree.static-deltas")
.expect("the summary advertises the deltas");
let advertised = |name: &str| {
map.dict_get(name)
.and_then(Value::as_variant)
.and_then(|(_, value)| value.as_bytes())
.unwrap_or_else(|| panic!("the map must name delta {name}"))
};
let digest_of = |dir: &Path| {
let superblock = std::fs::read(repo_dir.join(dir).join("superblock")).unwrap();
Checksum::sha256(&superblock)
};
assert_eq!(
advertised(&to.to_hex()),
digest_of(&scratch_dir).as_bytes(),
"the map must carry the from-scratch delta's superblock digest"
);
assert_eq!(
advertised(&format!("{}-{}", from.to_hex(), to.to_hex())),
digest_of(&from_to_dir).as_bytes(),
"the map must carry the from-to delta's superblock digest"
);
let keys: Vec<String> = match &summary.metadata {
Value::Array(entries) => entries
.iter()
.filter_map(|entry| match entry {
Value::Tuple(fields) => fields.first()?.as_str().map(str::to_owned),
_ => None,
})
.collect(),
other => panic!("the summary metadata is not a dict: {other:?}"),
};
let position = |key: &str| keys.iter().position(|k| k == key).expect(key);
assert!(
position("ostree.summary.tombstone-commits") < position("ostree.static-deltas")
&& position("ostree.static-deltas") < position("ostree.summary.indexed-deltas"),
"{keys:?}"
);
});
}
#[test]
fn the_tool_reads_the_port_written_delta_map() {
if !ostree_available() {
eprintln!("skipping: ostree tool not available");
return;
}
let (_tmp, repo_dir) = writable_fixture("summary", "summary-deltas-tool");
let to = block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
let refs = repo.list_refs(None).await.unwrap();
let (_, to) = *refs.first().expect("the fixture holds a ref");
repo.generate_static_delta(
None,
&to,
&DeltaOptions {
timestamp: Some(FIXED_EPOCH),
..DeltaOptions::default()
},
)
.await
.unwrap();
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(FIXED_EPOCH),
metadata_commit_timestamp: None,
})
.await
.unwrap();
to
});
let printed = Command::new("ostree")
.arg(format!("--repo={}", repo_dir.display()))
.args(["summary", "--print-metadata-key=ostree.static-deltas"])
.output()
.expect("run ostree summary");
assert!(
printed.status.success(),
"the tool must read the port's summary: {}",
String::from_utf8_lossy(&printed.stderr)
);
let text = String::from_utf8_lossy(&printed.stdout);
assert!(
text.contains(&to.to_hex()),
"the tool must list the delta the port advertised: {text}"
);
}