use std::path::{Path, PathBuf};
use stegobench_core::registry::Registry;
include!(concat!(env!("OUT_DIR"), "/embedded_registry.rs"));
include!(concat!(env!("OUT_DIR"), "/embedded_adapters.rs"));
const INSTALL_SUBDIR: &str = "stegobench";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Source {
Explicit(PathBuf),
WorkingDirectory(PathBuf),
BesideExecutable(PathBuf),
UserData(PathBuf),
SystemData(PathBuf),
BuiltIn,
}
impl Source {
pub fn path(&self) -> Option<&Path> {
match self {
Source::Explicit(p)
| Source::WorkingDirectory(p)
| Source::BesideExecutable(p)
| Source::UserData(p)
| Source::SystemData(p) => Some(p),
Source::BuiltIn => None,
}
}
pub fn tag(&self) -> &'static str {
match self {
Source::Explicit(_) => "explicit",
Source::WorkingDirectory(_) => "working-directory",
Source::BesideExecutable(_) => "beside-executable",
Source::UserData(_) => "user-data",
Source::SystemData(_) => "system-data",
Source::BuiltIn => "built-in",
}
}
pub fn how(&self) -> &'static str {
match self {
Source::Explicit(_) => "you named it, with --registry or STEGOBENCH_REGISTRY",
Source::WorkingDirectory(_) => "found under the directory you are in",
Source::BesideExecutable(_) => "found beside the stegobench executable",
Source::UserData(_) => "found in your user data directory",
Source::SystemData(_) => "found in the system data directory",
Source::BuiltIn => "compiled into this binary, because no registry was found on disk",
}
}
pub fn line(&self) -> String {
match self.path() {
Some(p) => format!("registry {}", crate::resolved_path(p).display()),
None => "registry built in".to_string(),
}
}
}
#[derive(Debug)]
pub struct Resolved {
pub registry: Registry,
pub source: Source,
_adapters: Option<tempfile::TempDir>,
adapter_roots: Vec<PathBuf>,
}
impl Resolved {
pub fn from_dir(dir: &Path) -> Result<Self, Error> {
Ok(Resolved {
registry: load(dir)?,
source: Source::Explicit(dir.to_path_buf()),
_adapters: None,
adapter_roots: stegobench_plugin::adapter::roots_for_registry(dir),
})
}
pub fn built_in() -> Result<Self, Error> {
let registry = embedded()?;
let adapters = unpack_built_in_adapters(®istry)?;
let adapter_roots = adapters
.as_ref()
.map(|d| vec![d.path().to_path_buf()])
.unwrap_or_default();
Ok(Resolved {
registry,
source: Source::BuiltIn,
_adapters: adapters,
adapter_roots,
})
}
pub fn adapter_roots(&self) -> &[PathBuf] {
&self.adapter_roots
}
pub fn to_json(&self) -> serde_json::Value {
serde_json::json!({
"source": self.source.tag(),
"path": self.source.path().map(|p| p.display().to_string()),
"how": self.source.how(),
"tools": self.registry.entries.len(),
"corpora": self.registry.corpora.len(),
})
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{source}{}", registry_footer(.path, *.explicit, .source))]
Named {
path: String,
explicit: bool,
#[source]
source: Box<stegobench_core::registry::RegistryError>,
},
#[error(
"no registry found, and this build carries no built-in copy.\n\
Looked for `plugins/registry` under: {searched}\n\
Point --registry at a directory of tool descriptions, or run \
stegobench from a checkout."
)]
NoneAnywhere { searched: String },
#[error("the registry compiled into this binary is not loadable, which is a bug: {0}")]
BuiltInBroken(String),
}
impl Error {
pub fn exit_code(&self) -> i32 {
stegobench_core::exit::FAILURE
}
}
fn registry_footer(
path: &str,
explicit: bool,
source: &stegobench_core::registry::RegistryError,
) -> String {
use stegobench_core::registry::RegistryError;
match source {
RegistryError::Read { .. } if explicit => format!(
"\n{path} was named with --registry or STEGOBENCH_REGISTRY. Fix \
the path, or drop the flag to let stegobench find one."
),
_ => format!("\nregistry {path}"),
}
}
fn load(dir: &Path) -> Result<Registry, Error> {
Registry::load(dir).map_err(|source| Error::Named {
path: dir.display().to_string(),
explicit: true,
source: Box::new(source),
})
}
pub fn resolve(explicit: Option<&Path>) -> Result<Resolved, Error> {
resolve_with(explicit, non_empty("STEGOBENCH_REGISTRY").as_deref())
}
pub fn resolve_with(explicit: Option<&Path>, from_env: Option<&str>) -> Result<Resolved, Error> {
let named = explicit
.map(Path::to_path_buf)
.or_else(|| from_env.filter(|v| !v.is_empty()).map(PathBuf::from));
if let Some(dir) = named {
return Resolved::from_dir(&dir);
}
let candidates = search_path();
for (dir, make) in &candidates {
if dir.is_dir() {
let registry = Registry::load(dir).map_err(|source| Error::Named {
path: dir.display().to_string(),
explicit: false,
source: Box::new(source),
})?;
return Ok(Resolved {
registry,
source: make(dir.clone()),
_adapters: None,
adapter_roots: stegobench_plugin::adapter::roots_for_registry(dir),
});
}
}
if EMBEDDED_REGISTRY.is_empty() {
return Err(Error::NoneAnywhere {
searched: candidates
.iter()
.map(|(p, _)| p.display().to_string())
.collect::<Vec<_>>()
.join(", "),
});
}
Resolved::built_in()
}
type Candidate = (PathBuf, fn(PathBuf) -> Source);
pub fn search_path() -> Vec<Candidate> {
let mut out: Vec<Candidate> = Vec::new();
out.push((
PathBuf::from("plugins").join("registry"),
Source::WorkingDirectory,
));
for dir in beside_executable() {
out.push((dir, Source::BesideExecutable));
}
if let Some(dir) = user_data_dir() {
out.push((dir.join(INSTALL_SUBDIR).join("registry"), Source::UserData));
}
for dir in system_data_dirs() {
out.push((
dir.join(INSTALL_SUBDIR).join("registry"),
Source::SystemData,
));
}
out
}
fn beside_executable() -> Vec<PathBuf> {
let Ok(exe) = std::env::current_exe() else {
return Vec::new();
};
let Some(dir) = exe.parent() else {
return Vec::new();
};
vec![
dir.join("plugins").join("registry"),
dir.join("registry"),
dir.join("..")
.join("share")
.join(INSTALL_SUBDIR)
.join("registry"),
]
}
pub fn user_data_dir() -> Option<PathBuf> {
if cfg!(target_os = "windows") {
return non_empty("APPDATA").map(PathBuf::from);
}
if cfg!(target_os = "macos") {
return non_empty("HOME").map(|h| PathBuf::from(h).join("Library/Application Support"));
}
match non_empty("XDG_DATA_HOME").map(PathBuf::from) {
Some(p) if p.is_absolute() => Some(p),
_ => non_empty("HOME").map(|h| PathBuf::from(h).join(".local/share")),
}
}
pub(crate) fn system_data_dirs() -> Vec<PathBuf> {
if cfg!(target_os = "windows") {
return non_empty("PROGRAMDATA")
.map(|p| vec![PathBuf::from(p)])
.unwrap_or_default();
}
vec![
PathBuf::from("/usr/local/share"),
PathBuf::from("/usr/share"),
]
}
pub fn non_empty(key: &str) -> Option<String> {
std::env::var(key).ok().filter(|v| !v.is_empty())
}
fn embedded() -> Result<Registry, Error> {
let mut reg = Registry::default();
for (path, text) in EMBEDDED_REGISTRY {
let in_corpora = path.starts_with(&format!("{}/", stegobench_core::registry::CORPORA_DIR));
if in_corpora {
let entry: stegobench_core::corpus::CorpusEntry =
toml::from_str(text).map_err(|e| Error::BuiltInBroken(format!("{path}: {e}")))?;
entry.validate().map_err(|problems| {
Error::BuiltInBroken(format!("{path}: {}", problems.join("; ")))
})?;
if reg.corpora.insert(entry.id.clone(), entry).is_some() {
return Err(Error::BuiltInBroken(format!(
"{path}: a second corpus claims that id"
)));
}
} else {
let entry: stegobench_core::registry::Entry =
toml::from_str(text).map_err(|e| Error::BuiltInBroken(format!("{path}: {e}")))?;
entry.validate().map_err(|problems| {
Error::BuiltInBroken(format!("{path}: {}", problems.join("; ")))
})?;
if reg.entries.insert(entry.name.clone(), entry).is_some() {
return Err(Error::BuiltInBroken(format!(
"{path}: a second tool claims that name"
)));
}
}
}
for id in reg.corpora.keys() {
if reg.entries.contains_key(id) {
return Err(Error::BuiltInBroken(format!(
"{id} is registered both as a tool and as a corpus"
)));
}
}
Ok(reg)
}
fn unpack_built_in_adapters(registry: &Registry) -> Result<Option<tempfile::TempDir>, Error> {
let wanted: Vec<String> = registry
.entries
.values()
.filter_map(|e| e.invoke.as_ref().and_then(|i| i.adapter.clone()))
.collect();
if wanted.is_empty() {
return Ok(None);
}
let scratch = tempfile::Builder::new()
.prefix("stegobench-adapters-")
.tempdir()
.map_err(|e| Error::BuiltInBroken(format!("could not unpack the adapters: {e}")))?;
let mut written: std::collections::HashSet<&str> = std::collections::HashSet::new();
for (name, bytes) in EMBEDDED_ADAPTERS {
if name.contains('/') || name.contains('\\') || name.contains("..") {
return Err(Error::BuiltInBroken(format!(
"built-in adapter {name:?} is not a plain file name"
)));
}
let dest = scratch.path().join(name);
std::fs::write(&dest, bytes).map_err(|e| {
Error::BuiltInBroken(format!("could not write the built-in adapter {name}: {e}"))
})?;
written.insert(*name);
}
for rel in &wanted {
let base = Path::new(rel)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or_default();
if !written.contains(base) {
return Err(Error::BuiltInBroken(format!(
"an entry names the adapter {rel}, which is not compiled into this binary"
)));
}
}
Ok(Some(scratch))
}
#[cfg(test)]
mod tests {
use super::*;
fn shipped() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../plugins/registry")
}
#[test]
fn every_fixture_a_registry_entry_names_is_a_fixture_that_ships() {
let reg = Registry::load(&shipped()).expect("the real registry loads");
let have: Vec<&str> = crate::fixtures::EMBEDDED_FIXTURES
.iter()
.map(|(name, _)| *name)
.collect();
assert!(!have.is_empty(), "no fixtures were compiled in");
let mut missing: Vec<String> = Vec::new();
let mut checked = 0usize;
for entry in reg.entries.values() {
let mut named: Vec<(&str, &str)> = Vec::new();
if let Some(test) = &entry.selftest {
named.push(("selftest.must_detect", test.must_detect.as_str()));
named.push(("selftest.must_clear", test.must_clear.as_str()));
}
if let Some(rt) = &entry.roundtrip {
named.push(("roundtrip.cover", rt.cover.as_str()));
}
for (field, name) in named {
checked += 1;
let bare = name.strip_prefix("fixtures/").unwrap_or(name);
if !have.contains(&bare) {
missing.push(format!(
"{}: {field} names {name:?}, which is not a fixture that \
ships. The fixtures are: {}",
entry.name,
have.join(", ")
));
}
}
}
assert!(
missing.is_empty(),
"a registry entry names a fixture that does not exist:\n {}",
missing.join("\n ")
);
assert!(
checked >= 10,
"only {checked} fixture references were checked, which is too few \
for this registry: the walk found nothing to look at"
);
}
#[test]
fn every_built_in_adapter_resolves_against_the_roots_this_resolution_carries() {
let resolved = Resolved::built_in().expect("the built-in registry loads");
let declared: Vec<&str> = resolved
.registry
.entries
.values()
.filter_map(|e| e.invoke.as_ref().and_then(|i| i.adapter.as_deref()))
.collect();
assert!(
!declared.is_empty(),
"no entry declares an adapter, so this test proves nothing; if that \
is now true, delete it rather than leaving it passing vacuously"
);
for path in declared {
assert!(
Path::new(path).is_relative(),
"{path} was rewritten into the entry, which is what makes \
`describe --json` differ between two runs of one binary"
);
let found = stegobench_plugin::adapter::resolve(path, resolved.adapter_roots())
.unwrap_or_else(|why| panic!("{why}"));
assert!(found.is_file(), "{} is not there", found.display());
}
}
#[test]
fn the_unpacked_adapters_live_exactly_as_long_as_the_resolution() {
let resolved = Resolved::built_in().expect("the built-in registry loads");
let declared = resolved
.registry
.entries
.values()
.find_map(|e| e.invoke.as_ref().and_then(|i| i.adapter.clone()))
.expect("at least one entry declares an adapter");
let found = stegobench_plugin::adapter::resolve(&declared, resolved.adapter_roots())
.expect("it resolves while the resolution is alive");
assert!(found.is_file());
let roots: Vec<PathBuf> = resolved.adapter_roots().to_vec();
drop(resolved);
assert!(
!found.exists(),
"{} outlived its resolution, so the scratch directory is leaking",
found.display()
);
if let Ok(after) = stegobench_plugin::adapter::resolve(&declared, &roots) {
assert_ne!(
after, found,
"the scratch copy is still being handed out after its \
resolution was dropped"
);
}
}
#[test]
fn the_built_in_registry_is_the_registry_on_disk() {
let disk = Registry::load(&shipped()).expect("the shipped registry loads");
let built_in = embedded().expect("the built-in registry loads");
assert_eq!(
disk.entries.keys().collect::<Vec<_>>(),
built_in.entries.keys().collect::<Vec<_>>(),
"the built-in registry names different tools from plugins/registry"
);
assert_eq!(
disk.corpora.keys().collect::<Vec<_>>(),
built_in.corpora.keys().collect::<Vec<_>>(),
"the built-in registry names different corpora from plugins/registry"
);
for (name, entry) in &disk.entries {
assert_eq!(
Some(entry),
built_in.entries.get(name),
"{name} differs between the built-in registry and plugins/registry"
);
}
for (id, entry) in &disk.corpora {
assert_eq!(
Some(entry),
built_in.corpora.get(id),
"{id} differs between the built-in registry and plugins/registry"
);
}
}
#[test]
fn the_environment_names_a_registry_only_when_it_has_something_in_it() {
let tmp = tempfile::tempdir().expect("tmp");
let from_env = tmp.path().join("from-env");
let from_flag = tmp.path().join("from-flag");
for dir in [&from_env, &from_flag] {
std::fs::create_dir_all(dir).expect("dirs");
}
let r = resolve_with(None, Some(&from_env.display().to_string())).expect("loads");
assert_eq!(r.source.path(), Some(from_env.as_path()));
let r =
resolve_with(Some(&from_flag), Some(&from_env.display().to_string())).expect("loads");
assert_eq!(
r.source.path(),
Some(from_flag.as_path()),
"the environment overrode an explicit --registry"
);
let empty = resolve_with(None, Some(""));
assert!(
empty.is_err() || empty.expect("ok").source.path() != Some(Path::new("")),
"an empty variable was read as a registry path"
);
let missing = resolve_with(None, Some(&tmp.path().join("nope").display().to_string()));
assert!(
missing.is_err(),
"a named registry that is absent fell back"
);
}
#[test]
fn a_resolved_registry_carries_the_roots_its_adapters_need() {
let tmp = tempfile::tempdir().expect("tmp");
let registry = tmp
.path()
.join("prefix")
.join("stegobench")
.join("registry");
std::fs::create_dir_all(®istry).expect("dirs");
let resolved = Resolved::from_dir(®istry).expect("an empty registry loads");
let roots = resolved.adapter_roots();
assert!(
roots.contains(&tmp.path().join("prefix")),
"the checkout layout root is missing: {roots:?}"
);
assert!(
roots.contains(&tmp.path().join("prefix").join("stegobench")),
"the install layout root is missing: {roots:?}"
);
}
#[test]
fn every_toml_file_in_the_registry_is_embedded() {
let mut on_disk: Vec<String> = Vec::new();
let mut stack = vec![shipped()];
while let Some(d) = stack.pop() {
for item in std::fs::read_dir(&d)
.expect("the registry directory reads")
.flatten()
{
let p = item.path();
if p.is_dir() {
stack.push(p);
} else if p.extension().is_some_and(|e| e == "toml") {
on_disk.push(
p.strip_prefix(shipped())
.expect("under the registry root")
.components()
.map(|c| c.as_os_str().to_string_lossy().into_owned())
.collect::<Vec<_>>()
.join("/"),
);
}
}
}
on_disk.sort();
let mut embedded_paths: Vec<String> = EMBEDDED_REGISTRY
.iter()
.map(|(p, _)| p.to_string())
.collect();
embedded_paths.sort();
assert_eq!(
on_disk, embedded_paths,
"build.rs embedded a different set of files from the one in plugins/registry"
);
assert!(
!embedded_paths.is_empty(),
"nothing was embedded, so an installed binary has no fallback registry"
);
}
#[test]
fn the_working_directory_is_searched_before_anything_installed() {
let path = search_path();
assert_eq!(
path.first().map(|(p, _)| p.clone()),
Some(PathBuf::from("plugins").join("registry")),
"the checkout is no longer searched first, so an editor's changes \
can be answered from an installed copy"
);
assert!(
path.len() >= 2,
"only {} candidate(s), so nothing installed is searched at all",
path.len()
);
}
#[test]
fn no_installed_candidate_is_relative() {
for (p, _) in search_path().into_iter().skip(1) {
assert!(
p.is_absolute(),
"{} is relative, so it means a different directory depending on \
where the command was typed",
p.display()
);
}
}
#[test]
fn a_named_registry_that_is_missing_is_an_error_not_a_fallback() {
let missing = PathBuf::from("/nonexistent/stegobench/registry");
let err = resolve(Some(&missing)).expect_err("a missing named registry fails");
assert!(
matches!(err, Error::Named { .. }),
"got {err:?}, which is not the named-path failure"
);
let text = err.to_string();
assert!(
text.contains(&missing.display().to_string()),
"the message does not name the path that was asked for: {text}"
);
assert!(
text.contains("--registry") && text.contains("drop the flag"),
"the message does not say what to do next: {text}"
);
}
#[test]
fn a_named_registry_that_is_there_is_used_as_given() {
let resolved = resolve(Some(&shipped())).expect("the shipped registry resolves");
assert_eq!(resolved.source, Source::Explicit(shipped()));
assert!(!resolved.registry.entries.is_empty());
}
#[test]
fn the_built_in_registry_resolves_and_says_so() {
let resolved = Resolved::built_in().expect("the built-in registry loads");
assert_eq!(resolved.source, Source::BuiltIn);
assert_eq!(resolved.source.path(), None);
assert_eq!(resolved.source.tag(), "built-in");
assert_eq!(resolved.source.line(), "registry built in");
assert!(!resolved.registry.entries.is_empty());
}
#[test]
fn the_reported_source_names_the_directory_that_answered() {
let resolved = Resolved::from_dir(&shipped()).expect("loads");
let json = resolved.to_json();
assert_eq!(json["source"], "explicit");
assert_eq!(
json["path"].as_str().expect("a path"),
shipped().display().to_string()
);
assert!(json["tools"].as_u64().expect("a count") > 0);
assert!(resolved.source.line().contains("plugins/registry"));
}
#[test]
fn an_empty_environment_variable_is_not_a_path() {
assert_eq!(non_empty("STEGOBENCH_A_VARIABLE_NOBODY_SETS"), None);
}
#[test]
fn every_source_variant_describes_itself() {
let p = PathBuf::from("/x");
for source in [
Source::Explicit(p.clone()),
Source::WorkingDirectory(p.clone()),
Source::BesideExecutable(p.clone()),
Source::UserData(p.clone()),
Source::SystemData(p.clone()),
Source::BuiltIn,
] {
assert!(!source.tag().is_empty());
assert!(source.how().len() > 10, "{:?} has no explanation", source);
assert!(!source.line().is_empty());
}
}
#[test]
fn only_a_path_refusal_tells_the_reader_to_fix_the_path() {
let tmp = tempfile::tempdir().expect("tmp");
let dir = tmp.path().join("reg");
std::fs::create_dir_all(dir.join("detectors")).expect("dirs");
std::fs::write(
dir.join("detectors/broken.toml"),
"name = \"broken\"\nkind = \"detector\"\nlicence = \"MIT\"\nthis is not toml\n",
)
.expect("write");
let bad_entry = resolve_with(Some(&dir), None).expect_err("a malformed entry refuses");
let text = bad_entry.to_string();
assert!(
!text.contains("Fix the path") && !text.contains("drop the flag"),
"a malformed entry was blamed on the path:\n{text}"
);
assert!(
text.contains(&format!("registry {}", dir.display())),
"the refusal does not name the registry it read:\n{text}"
);
let missing = tmp.path().join("nope");
let bad_path = resolve_with(Some(&missing), None).expect_err("a missing path refuses");
let text = bad_path.to_string();
assert!(
text.contains("Fix the path") && text.contains("drop the flag"),
"the path advice went missing from the refusal it answers:\n{text}"
);
}
}