use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use stegobench_core::registry::Entry;
pub mod adapter;
pub mod availability;
pub mod embed;
pub mod exec;
pub mod parsers;
pub mod runner;
pub mod selftest;
pub use availability::{registry_reach, Availability, Presence, Reach};
pub use parsers::Reading;
pub use selftest::Verified;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WorkItem {
pub id: String,
pub path: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Record {
pub id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub score: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub verdict: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub elapsed_ms: Option<u64>,
}
impl Record {
pub fn is_complete(&self) -> bool {
self.score.is_some() || self.verdict.is_some() || self.error.is_some()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Describe {
pub protocol: u32,
pub name: String,
pub version: String,
}
#[derive(Debug, thiserror::Error)]
pub enum PluginError {
#[error("{name} is not installed: {reason}")]
NotInstalled { name: String, reason: String },
#[error("{name} could not be run: {source}")]
Spawn {
name: String,
#[source]
source: std::io::Error,
},
#[error("{name} produced output this version cannot read: {reason}")]
BadOutput { name: String, reason: String },
}
pub const DEFAULT_HOST_ENTRYPOINT: &str = "python3";
pub fn hash_file(path: &Path) -> std::io::Result<String> {
let bytes = std::fs::read(path)?;
let mut h = Sha256::new();
h.update(&bytes);
Ok(format!("sha256:{:x}", h.finalize()))
}
pub fn which(program: &str) -> Option<PathBuf> {
if program.contains('/') {
let p = PathBuf::from(program);
return p.is_file().then_some(p);
}
std::env::var_os("PATH").and_then(|paths| {
std::env::split_paths(&paths)
.map(|dir| dir.join(program))
.find(|p| p.is_file())
})
}
const VERSION_TIMEOUT: Duration = Duration::from_secs(10);
const VERSION_LINES_SCANNED: usize = 10;
const VERSION_LINE_MAX: usize = 200;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VersionProbe {
Reported(String),
Refused(String),
NotAsked,
}
impl VersionProbe {
pub fn pin_label(&self) -> &str {
match self {
VersionProbe::Reported(v) => v,
VersionProbe::Refused(why) => why,
VersionProbe::NotAsked => "unknown version",
}
}
}
pub fn binary_version(entry: &Entry) -> VersionProbe {
let Some(bin) = entry.binary.as_ref() else {
return VersionProbe::NotAsked;
};
let Some(program) = bin.command.first() else {
return VersionProbe::NotAsked;
};
let Some(path) = which(program) else {
return VersionProbe::NotAsked;
};
let mut cmd = Command::new(&path);
cmd.args(&bin.version_args);
let Ok(out) = exec::captured(cmd, program, VERSION_TIMEOUT) else {
return VersionProbe::NotAsked;
};
let text = String::from_utf8_lossy(if out.stdout.is_empty() {
&out.stderr
} else {
&out.stdout
});
let parser = entry.invoke.as_ref().map(|i| i.parser.as_str());
read_version(&text, parser)
}
fn read_version(text: &str, parser: Option<&str>) -> VersionProbe {
let candidate = text
.lines()
.take(VERSION_LINES_SCANNED)
.map(str::trim)
.filter(|line| line.len() <= VERSION_LINE_MAX && !line.chars().any(char::is_control))
.find(|line| line.split_whitespace().any(is_version_token));
let Some(line) = candidate else {
let first = text.lines().next().unwrap_or("").trim();
return VersionProbe::Refused(format!("no version in: {}", clip(first, 22)));
};
let bare_number = !line.chars().any(|c| c.is_ascii_alphabetic());
if bare_number {
let reads_as_an_answer = parser.is_some_and(|p| {
matches!(
parsers::parse(p, line, ""),
Reading::Score(_) | Reading::Verdict(true)
)
});
if reads_as_an_answer {
return VersionProbe::Refused(format!("{} is a score, not a version", clip(line, 12)));
}
}
VersionProbe::Reported(line.to_string())
}
fn is_version_token(token: &str) -> bool {
let trimmed = token.trim_matches(|c: char| !c.is_ascii_alphanumeric());
let stripped = trimmed
.strip_prefix('v')
.or_else(|| trimmed.strip_prefix('V'))
.unwrap_or(trimmed);
let head: String = stripped
.chars()
.take_while(|c| c.is_ascii_digit() || *c == '.')
.collect();
let mut groups = head.split('.').peekable();
let mut count = 0usize;
while let Some(group) = groups.next() {
if group.is_empty() {
return groups.peek().is_none() && count >= 2;
}
if !group.chars().all(|c| c.is_ascii_digit()) {
return false;
}
count += 1;
}
count >= 2
}
fn clip(text: &str, max: usize) -> String {
if text.chars().count() <= max {
return text.to_string();
}
let head: String = text.chars().take(max.saturating_sub(3)).collect();
format!("{head}...")
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(unix)]
#[test]
fn a_version_probe_returns_even_when_the_tool_leaves_a_child_holding_the_pipe() {
let dir = tempfile::tempdir().expect("tmp");
let script = dir.path().join("forker.sh");
std::fs::write(
&script,
"#!/bin/sh\nsleep 120 &\necho 'forker 9.9'\nexit 0\n",
)
.expect("written");
let mut perms = std::fs::metadata(&script).expect("stat").permissions();
std::os::unix::fs::PermissionsExt::set_mode(&mut perms, 0o755);
std::fs::set_permissions(&script, perms).expect("chmod");
let entry: Entry = toml::from_str(&format!(
"name = \"forker\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
[binary]\ncommand = [\"{}\"]\nversion_args = [\"--version\"]\n\
[selftest]\nmust_detect = \"a.png\"\nmust_clear = \"b.png\"\n",
script.display()
))
.expect("the entry parses");
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(binary_version(&entry));
});
match rx.recv_timeout(Duration::from_secs(VERSION_TIMEOUT.as_secs() + 20)) {
Ok(version) => assert_eq!(
version,
VersionProbe::Reported("forker 9.9".into()),
"the tool did answer, so its answer should survive the bound"
),
Err(_) => panic!(
"the version probe never returned: a child holding the pipe \
open is enough to hang it again"
),
}
}
#[test]
fn the_shapes_real_tools_answer_in_are_all_recorded() {
for answer in [
"zsteg 0.2.13",
"v1.4",
"StegExpose 0.0.2",
"stegcore 1.2.3-rc1",
"zsteg 0.2.13 (c) 2019 Andrey Zaikin, all rights reserved",
"tool version 1.4.",
] {
assert_eq!(
read_version(answer, Some("number")),
VersionProbe::Reported(answer.trim().to_string()),
"an honest version was refused: {answer:?}"
);
}
}
#[test]
fn a_version_on_the_third_line_of_a_banner_is_found() {
assert_eq!(
read_version("MyTool\nCopyright (c) 2019 nobody\n1.4.2\n", Some("number")),
VersionProbe::Reported("1.4.2".into()),
"the copyright year must not be mistaken for the version either"
);
}
#[test]
fn a_traceback_is_refused_rather_than_recorded_as_a_version() {
match read_version(
"Traceback (most recent call last):\n File \"x.py\", line 1, in <module>\n",
Some("number"),
) {
VersionProbe::Refused(why) => assert!(why.contains("Traceback"), "got {why}"),
other => panic!("expected a refusal, got {other:?}"),
}
}
#[test]
fn a_tool_that_answers_the_version_probe_with_its_score_is_refused() {
match read_version("0.010\n", Some("number")) {
VersionProbe::Refused(why) => {
assert!(why.contains("0.010"), "the answer is named: {why}");
assert!(why.contains("score"), "and why it was refused: {why}");
}
other => panic!("expected a refusal, got {other:?}"),
}
}
#[test]
fn a_bare_number_is_refused_only_where_the_tools_own_parser_reads_it() {
assert!(matches!(
read_version("1.0\n", Some("number")),
VersionProbe::Refused(_)
));
assert_eq!(
read_version("1.0\n", Some("stegcore")),
VersionProbe::Reported("1.0".into())
);
}
#[test]
fn a_usage_message_and_an_empty_answer_are_both_refused() {
for answer in ["", "usage: tool [-h] [--version]\n", " \n"] {
assert!(
matches!(
read_version(answer, Some("number")),
VersionProbe::Refused(_)
),
"this is not a version: {answer:?}"
);
}
}
#[test]
fn a_probe_that_floods_the_output_is_bounded() {
let mut flood = "no version here\n".repeat(VERSION_LINES_SCANNED + 5);
flood.push_str("1.2.3\n");
assert!(matches!(
read_version(&flood, Some("number")),
VersionProbe::Refused(_)
));
}
#[test]
fn an_overlong_line_is_not_a_version_even_with_a_number_in_it() {
let long = format!("{} 1.2.3", "x".repeat(VERSION_LINE_MAX));
assert!(matches!(
read_version(&long, Some("number")),
VersionProbe::Refused(_)
));
}
#[test]
fn a_pin_label_is_short_enough_for_the_column_it_is_printed_in() {
for probe in [
read_version("0.010\n", Some("number")),
read_version("Traceback (most recent call last):\n", Some("number")),
VersionProbe::NotAsked,
] {
let label = probe.pin_label();
assert!(
label.chars().count() <= 40,
"{label:?} is {} characters",
label.chars().count()
);
}
}
#[test]
fn a_record_with_a_score_is_complete() {
let r = Record {
id: "1".into(),
score: Some(0.7),
verdict: None,
error: None,
elapsed_ms: None,
};
assert!(r.is_complete());
}
#[test]
fn a_record_with_an_error_is_also_complete() {
let r = Record {
id: "1".into(),
score: None,
verdict: None,
error: Some("decode failed".into()),
elapsed_ms: None,
};
assert!(r.is_complete());
}
#[test]
fn an_empty_placeholder_is_not_complete() {
let r = Record {
id: "1".into(),
score: None,
verdict: None,
error: None,
elapsed_ms: None,
};
assert!(!r.is_complete());
}
#[test]
fn a_verdict_only_plugin_still_produces_complete_records() {
let r = Record {
id: "1".into(),
score: None,
verdict: Some(true),
error: None,
elapsed_ms: None,
};
assert!(r.is_complete());
}
#[test]
fn hashing_a_file_is_stable_and_prefixed() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("x");
std::fs::write(&p, b"hello").unwrap();
let a = hash_file(&p).unwrap();
assert_eq!(a, hash_file(&p).unwrap());
assert!(a.starts_with("sha256:"));
}
#[test]
fn hashing_notices_a_changed_byte() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("x");
std::fs::write(&p, b"hello").unwrap();
let before = hash_file(&p).unwrap();
std::fs::write(&p, b"hellp").unwrap();
assert_ne!(before, hash_file(&p).unwrap());
}
#[cfg(unix)]
#[test]
fn which_finds_something_that_exists_and_not_something_that_does_not() {
assert!(which("sh").is_some(), "sh should be on PATH");
assert!(which("definitely-not-a-real-program-xyzzy").is_none());
}
#[cfg(unix)]
#[test]
fn an_absolute_path_is_used_directly() {
assert!(which("/bin/sh").is_some() || which("/usr/bin/sh").is_some());
assert!(which("/nope/nothing").is_none());
}
#[test]
fn records_round_trip_and_omit_what_is_absent() {
let r = Record {
id: "42".into(),
score: Some(0.5),
verdict: None,
error: None,
elapsed_ms: Some(10),
};
let text = serde_json::to_string(&r).unwrap();
assert!(
!text.contains("verdict"),
"absent fields should not be written"
);
assert_eq!(serde_json::from_str::<Record>(&text).unwrap(), r);
}
}