#![forbid(unsafe_code)]
mod common;
use std::os::fd::AsFd;
use std::path::{Path, PathBuf};
use std::process::Command;
use ostrya::{
Checksum, CommitOptions, ComposefsOptions, CreateOptions, DirMeta, Error, FileMeta,
MutableTree, Repo, RepoMode, VerityPolicy,
};
use ostrya_composefs::FsVerityHasher;
use ostrya_core::{ObjectType, Xattrs, loose_path};
use ostrya_rt::block_on;
use common::{COMMIT, HELLO_TXT, TmpDir, fixture_repo, fixture_root};
fn composefs_dir() -> PathBuf {
fixture_root().join("composefs")
}
fn manifest_value(key: &str) -> Option<String> {
let text = std::fs::read_to_string(fixture_root().join("MANIFEST")).ok()?;
text.lines()
.find_map(|l| l.strip_prefix(&format!("{key}=")))
.map(|v| v.trim().to_owned())
}
fn manifest_digest(key: &str) -> Option<String> {
manifest_value(key).filter(|s| !s.is_empty())
}
fn scratch_fixture_repo(scratch: &TmpDir, mode: &str) -> PathBuf {
let src = fixture_repo(mode);
let dst = scratch.path().join("repo");
let status = Command::new("cp")
.arg("-a")
.arg(&src)
.arg(&dst)
.status()
.expect("run cp to copy the fixture repo");
assert!(status.success(), "cp -a failed to copy the fixture repo");
dst
}
fn content_object(repo_dir: &Path, checksum: &str) -> PathBuf {
repo_dir.join("objects").join(loose_path(
&Checksum::from_hex(checksum).unwrap(),
ObjectType::File,
RepoMode::BareUser,
))
}
fn rewrite_payload(path: &Path) {
use std::io::Write;
let mut bytes = std::fs::read(path).expect("read the object payload");
assert!(!bytes.is_empty(), "the object has a payload to rewrite");
bytes[0] ^= 0xff;
let mut file = std::fs::OpenOptions::new()
.write(true)
.open(path)
.expect("open the object for writing");
file.write_all(&bytes).expect("rewrite the object payload");
file.flush().expect("flush the rewritten object");
}
fn to_hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn check_export(stem: &str, digest_key: &str, verity: VerityPolicy) {
let (Some(digest), Ok(golden)) = (
manifest_digest(digest_key),
std::fs::read(composefs_dir().join(format!("{stem}.cfs"))),
) else {
eprintln!("composefs fixture {stem} absent; skipping");
return;
};
let scratch = TmpDir::new("composefs-export-to");
let image_path = scratch.path().join(format!("{stem}.cfs"));
let repo_dir = fixture_repo("bare-user");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
let commit = Checksum::from_hex(COMMIT).unwrap();
let image = repo
.export_composefs(&commit, &ComposefsOptions { verity })
.await
.unwrap();
assert_eq!(
image.bytes.len(),
golden.len(),
"{stem}: image length {} != golden {}",
image.bytes.len(),
golden.len()
);
if let Some(pos) = image.bytes.iter().zip(&golden).position(|(a, b)| a != b) {
panic!(
"{stem}: image diverges from golden at byte {pos:#x}: got {:#04x}, want {:#04x}",
image.bytes[pos], golden[pos]
);
}
assert_eq!(
to_hex(&image.fs_verity),
digest,
"{stem}: fs-verity digest mismatch"
);
let file = std::fs::File::create(&image_path).expect("create the image file");
let streamed = repo
.export_composefs_to(&commit, &ComposefsOptions { verity }, file.as_fd())
.await
.unwrap();
drop(file);
let written = std::fs::read(&image_path).expect("read the exported image back");
assert_eq!(
written, image.bytes,
"{stem}: the fd form wrote different bytes than the buffer form"
);
assert_eq!(
to_hex(&streamed),
digest,
"{stem}: the fd form returned a different digest"
);
assert_eq!(
streamed,
FsVerityHasher::hash(&written),
"{stem}: the returned digest differs from the digest of the file"
);
});
}
#[test]
fn export_matches_golden_image_and_digest() {
check_export("tree", "composefs_digest", VerityPolicy::Computed);
}
#[test]
fn noverity_export_matches_golden_image_and_digest() {
check_export(
"tree-noverity",
"composefs_noverity_digest",
VerityPolicy::Disabled,
);
}
#[test]
fn stores_digest_in_commit_metadata() {
let Some(digest) = manifest_digest("composefs_digest") else {
eprintln!("composefs fixture absent; skipping");
return;
};
let scratch = TmpDir::new("composefs-meta");
let dst = scratch_fixture_repo(&scratch, "bare-user");
block_on(async {
let repo = Repo::open(&dst).await.unwrap();
let commit = Checksum::from_hex(COMMIT).unwrap();
let txn = repo.transaction().await.unwrap();
let new_commit = repo
.commit_add_composefs_metadata(&txn, &commit)
.await
.unwrap();
assert_ne!(
new_commit, commit,
"adding the digest metadata yields a distinct commit"
);
txn.commit().await.unwrap();
let (obj, _) = repo.load_commit(&new_commit).await.unwrap();
let value = obj
.metadata
.dict_get("ostree.composefs.digest.v0")
.expect("ostree.composefs.digest.v0 present in the new commit");
let (_, inner) = value.as_variant().expect("digest value is a variant");
let bytes = inner.as_bytes().expect("digest is a byte array");
assert_eq!(
to_hex(bytes),
digest,
"stored digest equals the tool's recorded digest"
);
});
}
#[test]
fn transaction_digest_matches_recorded_digest() {
let Some(digest) = manifest_digest("composefs_digest") else {
eprintln!("composefs fixture absent; skipping");
return;
};
let repo_dir = fixture_repo("bare-user");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
let (tree, commit) = repo.read_commit(COMMIT).await.unwrap();
let txn = repo.transaction().await.unwrap();
let staged = txn.composefs_digest(&tree).await.unwrap();
txn.abort().await.unwrap();
assert_eq!(
to_hex(&staged),
digest,
"the staged-tree digest equals the tool's recorded digest"
);
let image = repo
.export_composefs(
&commit,
&ComposefsOptions {
verity: VerityPolicy::Computed,
},
)
.await
.unwrap();
assert_eq!(
staged, image.fs_verity,
"the staged-tree digest equals the exported image's digest"
);
});
}
#[test]
fn disabled_policy_reads_no_payload() {
let computed = ComposefsOptions {
verity: VerityPolicy::Computed,
};
let disabled = ComposefsOptions {
verity: VerityPolicy::Disabled,
};
let scratch = TmpDir::new("composefs-noverity-payload");
let repo_dir = scratch_fixture_repo(&scratch, "bare-user");
block_on(async {
let repo = Repo::open(&repo_dir).await.unwrap();
let commit = Checksum::from_hex(COMMIT).unwrap();
let before_computed = repo.export_composefs(&commit, &computed).await.unwrap();
let before_disabled = repo.export_composefs(&commit, &disabled).await.unwrap();
rewrite_payload(&content_object(&repo_dir, HELLO_TXT));
let after_computed = repo.export_composefs(&commit, &computed).await.unwrap();
let after_disabled = repo.export_composefs(&commit, &disabled).await.unwrap();
assert_eq!(
before_disabled.bytes, after_disabled.bytes,
"a rewritten payload leaves the Disabled image unchanged"
);
assert_ne!(
before_computed.bytes, after_computed.bytes,
"a rewritten payload changes the Computed image"
);
assert_ne!(
before_disabled.fs_verity, before_computed.fs_verity,
"the two policies produce distinct images"
);
});
}
#[derive(Clone, Copy)]
enum Carrier {
Root,
File,
}
async fn export_xattrs(
repo_dir: &Path,
mode: RepoMode,
carrier: Carrier,
xattrs: Xattrs,
) -> Result<(), Error> {
let repo = Repo::create(repo_dir, CreateOptions::new(mode))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let mut mtree = MutableTree::new();
let root_xattrs = match carrier {
Carrier::Root => xattrs,
Carrier::File => {
let meta = FileMeta {
uid: 0,
gid: 0,
mode: 0o100644,
xattrs,
};
let file = txn.write_regfile_inline(None, &meta, b"").await.unwrap();
mtree.replace_file("f", file).unwrap();
Xattrs::empty()
}
};
let dirmeta_bytes = DirMeta {
uid: 0,
gid: 0,
mode: 0o040755,
xattrs: root_xattrs,
}
.serialize()
.unwrap();
let dirmeta = txn
.write_metadata(ObjectType::DirMeta, None, &dirmeta_bytes)
.await
.unwrap();
mtree.set_metadata_checksum(dirmeta);
let root = txn.write_mtree(&mut mtree).await.unwrap();
let commit = txn
.write_commit(
CommitOptions {
subject: Some("xattr budget".to_owned()),
timestamp: Some(0),
..CommitOptions::default()
},
&root,
)
.await
.unwrap();
txn.set_ref("budget", Some(&commit));
txn.commit().await.unwrap();
if mode == RepoMode::BareUser {
return repo
.export_composefs(&commit, &ComposefsOptions::default())
.await
.map(|_| ());
}
let (tree, _) = repo.read_commit("budget").await.unwrap();
let txn = repo.transaction().await.unwrap();
let walked = txn.composefs_digest(&tree).await.map(|_| ());
txn.abort().await.unwrap();
walked
}
#[test]
fn holds_an_inode_to_the_composefs_xattr_budget() {
let scratch = TmpDir::new("composefs-xattr-budget");
let name = b"user.long\0".to_vec();
let at_budget = 32755 - 7 - 9;
block_on(async {
export_xattrs(
&scratch.path().join("at"),
RepoMode::BareUser,
Carrier::Root,
Xattrs::new([(name.clone(), vec![b'x'; at_budget])]).unwrap(),
)
.await
.expect("the export builds the image at the budget");
let err = export_xattrs(
&scratch.path().join("over"),
RepoMode::BareUser,
Carrier::Root,
Xattrs::new([(name, vec![b'x'; at_budget + 1])]).unwrap(),
)
.await
.expect_err("the export refuses one byte past the budget");
let text = err.to_string();
assert!(
matches!(err, Error::Unsupported(_)),
"the export refused with {err:?}"
);
assert!(
text.contains("user.long") && text.contains("32756"),
"the refusal names the attribute and the bytes it takes: {text}"
);
});
}
#[test]
fn the_composefs_xattr_budget_covers_the_inode() {
let scratch = TmpDir::new("composefs-xattr-budget-sum");
let each = (32756 / 2) - 7 - 6;
block_on(async {
let err = export_xattrs(
scratch.path(),
RepoMode::BareUser,
Carrier::Root,
Xattrs::new([
(b"user.a\0".to_vec(), vec![b'x'; each]),
(b"user.b\0".to_vec(), vec![b'y'; each]),
])
.unwrap(),
)
.await
.expect_err("the export refuses the pair");
let text = err.to_string();
assert!(
matches!(err, Error::Unsupported(_)),
"the export refused with {err:?}"
);
assert!(
text.contains("user.b") && text.contains("32756"),
"the refusal names the second attribute: {text}"
);
});
}
#[test]
fn the_composefs_xattr_budget_covers_a_regular_file() {
let scratch = TmpDir::new("composefs-xattr-budget-file");
let name = b"user.long\0".to_vec();
let at_budget = 32755 - 7 - 9;
block_on(async {
export_xattrs(
&scratch.path().join("at"),
RepoMode::Archive,
Carrier::File,
Xattrs::new([(name.clone(), vec![b'x'; at_budget])]).unwrap(),
)
.await
.expect("the export builds the image at the budget");
let err = export_xattrs(
&scratch.path().join("over"),
RepoMode::Archive,
Carrier::File,
Xattrs::new([(name, vec![b'x'; at_budget + 1])]).unwrap(),
)
.await
.expect_err("the export refuses one byte past the budget");
assert!(
matches!(err, Error::Unsupported(_)),
"the export refused with {err:?}"
);
});
}
async fn export_symlink(dir: &Path, len: usize) -> Result<(), Error> {
let repo = Repo::create(dir, CreateOptions::new(RepoMode::BareUser))
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let meta = FileMeta {
uid: 0,
gid: 0,
mode: 0o120777,
xattrs: Xattrs::empty(),
};
let link = txn
.write_symlink(&"z".repeat(len), &meta, None)
.await
.unwrap();
let mut mtree = MutableTree::new();
mtree.replace_file("l", link).unwrap();
let dirmeta_bytes = DirMeta {
uid: 0,
gid: 0,
mode: 0o040755,
xattrs: Xattrs::empty(),
}
.serialize()
.unwrap();
let dirmeta = txn
.write_metadata(ObjectType::DirMeta, None, &dirmeta_bytes)
.await
.unwrap();
mtree.set_metadata_checksum(dirmeta);
let root = txn.write_mtree(&mut mtree).await.unwrap();
let commit = txn
.write_commit(
CommitOptions {
subject: Some("symlink".to_owned()),
timestamp: Some(0),
..CommitOptions::default()
},
&root,
)
.await
.unwrap();
txn.commit().await.unwrap();
repo.export_composefs(&commit, &ComposefsOptions::default())
.await
.map(|_| ())
}
#[test]
fn holds_a_symlink_target_to_its_inode_block() {
let scratch = TmpDir::new("composefs-symlink-block");
block_on(async {
export_symlink(&scratch.path().join("at"), 4063)
.await
.expect("the export builds the image at 4063 bytes");
let err = export_symlink(&scratch.path().join("over"), 4064)
.await
.expect_err("the export refuses a target that fills the block");
let text = err.to_string();
assert!(
matches!(err, Error::Unsupported(_)),
"the export refused with {err:?}"
);
assert!(
text.contains("4064"),
"the refusal names the length: {text}"
);
});
}
#[test]
fn holds_an_xattr_name_to_the_erofs_length_field() {
let scratch = TmpDir::new("composefs-xattr-name");
let name_of = |len: usize| {
let mut name = b"user.".to_vec();
name.resize(len, b'n');
name.push(0);
name
};
block_on(async {
export_xattrs(
&scratch.path().join("at"),
RepoMode::Archive,
Carrier::File,
Xattrs::new([(name_of(255), b"v".to_vec())]).unwrap(),
)
.await
.expect("the export builds the image at 255 bytes of name");
let err = export_xattrs(
&scratch.path().join("over"),
RepoMode::Archive,
Carrier::File,
Xattrs::new([(name_of(256), b"v".to_vec())]).unwrap(),
)
.await
.expect_err("the export refuses a 256-byte name");
let text = err.to_string();
assert!(
matches!(err, Error::Unsupported(_)),
"the export refused with {err:?}"
);
assert!(
text.contains("user.nn") && text.contains("256"),
"the refusal names the attribute and its length: {text}"
);
});
}
#[test]
fn digest_metadata_runs_in_a_non_backing_mode() {
let Some(digest) = manifest_digest("composefs_digest") else {
eprintln!("composefs fixture absent; skipping");
return;
};
let scratch = TmpDir::new("composefs-archive-meta");
let dst = scratch_fixture_repo(&scratch, "archive");
block_on(async {
let repo = Repo::open(&dst).await.unwrap();
assert_eq!(repo.mode(), RepoMode::Archive, "the fixture is archive");
let commit = Checksum::from_hex(COMMIT).unwrap();
let err = repo
.export_composefs(&commit, &ComposefsOptions::default())
.await
.map(|_| ())
.expect_err("an archive repository writes no image");
assert!(
matches!(err, Error::Unsupported(_)),
"the export refused with {err:?}"
);
let txn = repo.transaction().await.unwrap();
let stored = repo
.commit_add_composefs_metadata(&txn, &commit)
.await
.unwrap();
txn.commit().await.unwrap();
let (obj, _) = repo.load_commit(&stored).await.unwrap();
let value = obj
.metadata
.dict_get("ostree.composefs.digest.v0")
.expect("the new commit carries the digest key");
let (_, inner) = value.as_variant().expect("digest value is a variant");
let bytes = inner.as_bytes().expect("digest is a byte array");
assert_eq!(
to_hex(bytes),
digest,
"the archive repository records the digest the tool recorded"
);
});
}