use crate::Record;
#[derive(Debug, Clone, PartialEq)]
pub enum Reading {
Score(f64),
Verdict(bool),
Failed(String),
}
pub fn zsteg(stdout: &str, stderr: &str) -> Reading {
let combined = format!("{stdout}\n{stderr}");
for line in combined.lines() {
let line = line.trim();
if line.starts_with("[?]") {
return Reading::Verdict(true);
}
if (line.contains("text:") || line.contains("file:")) && !line.contains("nothing :(") {
return Reading::Verdict(true);
}
}
Reading::Verdict(false)
}
pub fn stegexpose(stdout: &str, _stderr: &str) -> Reading {
for line in stdout.lines().rev() {
let line = line.trim();
if line.is_empty() || line.starts_with("File name") {
continue;
}
if let Some(last) = line.rsplit(',').next() {
if let Ok(v) = last.trim().parse::<f64>() {
return Reading::Score(v);
}
}
}
Reading::Failed("no numeric score in StegExpose output".into())
}
pub fn number(stdout: &str, stderr: &str) -> Reading {
let mut found: Vec<f64> = Vec::new();
for line in stdout.lines() {
if let Ok(v) = line.trim().parse::<f64>() {
if v.is_finite() {
found.push(v);
if found.len() > 1 {
break;
}
}
}
}
match found.len() {
1 => Reading::Score(found[0]),
0 => {
let why = stderr.lines().next_back().unwrap_or("no output").trim();
Reading::Failed(format!("no number on stdout: {why}"))
}
_ => Reading::Failed(format!(
"more than one number on stdout ({} and {}) for one image, and \
nothing here can know which is the answer. A tool asked about one \
image answers once; if it answers about several, declare \
invoke.batch and the keyed protocol so each answer names its own \
image",
found[0], found[1]
)),
}
}
pub fn stegcore(stdout: &str, stderr: &str) -> Reading {
let Ok(v) = serde_json::from_str::<serde_json::Value>(stdout) else {
let why = stderr.lines().next_back().unwrap_or("no output").trim();
return Reading::Failed(format!("stegcore printed no JSON: {why}"));
};
if v.get("ok").and_then(|o| o.as_bool()) == Some(false) {
let why = v
.get("error")
.and_then(|e| e.as_str())
.unwrap_or("unspecified");
return Reading::Failed(format!("stegcore reported failure: {why}"));
}
let first = v
.get("data")
.and_then(|d| d.as_array().and_then(|a| a.first()).or(Some(d)));
match first
.and_then(|r| r.get("overall_score"))
.and_then(|s| s.as_f64())
{
Some(s) => Reading::Score(s),
None => Reading::Failed("no overall_score in stegcore output".into()),
}
}
pub fn parse(parser: &str, stdout: &str, stderr: &str) -> Reading {
match parser {
"zsteg" => zsteg(stdout, stderr),
"stegexpose" => stegexpose(stdout, stderr),
"number" => number(stdout, stderr),
"stegcore" => stegcore(stdout, stderr),
other => Reading::Failed(format!("no built-in parser named {other:?}")),
}
}
pub fn parse_keyed(parser: &str, stdout: &str, stderr: &str, keys: &[String]) -> Vec<Reading> {
let mut found: Vec<Option<&str>> = vec![None; keys.len()];
let tail = stderr.lines().next_back().unwrap_or("no output").trim();
for line in stdout.lines() {
let line = line.trim_end();
if line.trim().is_empty() {
continue;
}
let Some((key, answer)) = line.split_once('\t') else {
return vec![
Reading::Failed(format!(
"a batched tool printed a line with no tab in it, so no \
answer in this batch can be attributed to an image. The \
protocol is `<path>\\t<answer>`. The line was: {line:?}"
));
keys.len()
];
};
let key = key.trim();
match keys.iter().position(|k| k == key) {
Some(i) => found[i] = Some(answer),
None => {
return vec![
Reading::Failed(format!(
"a batched tool answered for {key:?}, which was not in \
the batch it was given. Every other answer it printed \
is therefore unattributable, so the whole batch is \
refused rather than partly believed"
));
keys.len()
]
}
}
}
keys.iter()
.zip(found)
.map(|(key, answer)| match answer {
Some(text) => parse(parser, text, stderr),
None => Reading::Failed(format!(
"the tool was asked about {key} in a batch and printed no line \
for it. Recorded as unanswered rather than as a score, because \
a missing answer is not a zero. stderr: {tail}"
)),
})
.collect()
}
impl Reading {
pub fn into_record(self, id: impl Into<String>) -> Record {
let id = id.into();
match self {
Reading::Score(s) => Record {
id,
score: Some(s),
verdict: None,
error: None,
elapsed_ms: None,
},
Reading::Verdict(v) => Record {
id,
score: None,
verdict: Some(v),
error: None,
elapsed_ms: None,
},
Reading::Failed(e) => Record {
id,
score: None,
verdict: None,
error: Some(e),
elapsed_ms: None,
},
}
}
pub fn says_stego(&self, higher_means_stego: bool, threshold: f64) -> Option<bool> {
match self {
Reading::Verdict(v) => Some(*v),
Reading::Score(s) => Some(if higher_means_stego {
*s > threshold
} else {
*s < threshold
}),
Reading::Failed(_) => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zsteg_finds_the_question_mark_marker() {
assert_eq!(
zsteg("[?] 4096 bytes of extra data\n", ""),
Reading::Verdict(true)
);
}
#[test]
fn zsteg_reads_stderr_because_on_jpeg_stdout_is_empty() {
assert_eq!(
zsteg(
"",
"[?] 4096 bytes of extra data after IEND\nerror: not a PNG"
),
Reading::Verdict(true)
);
}
#[test]
fn zsteg_treats_a_text_finding_as_a_finding() {
assert_eq!(
zsteg("b1,rgb,lsb,xy .. text: \"hello\"\n", ""),
Reading::Verdict(true)
);
}
#[test]
fn zsteg_does_not_fire_on_its_own_nothing_found_line() {
assert_eq!(
zsteg("b1,rgb,lsb,xy .. text: nothing :(\n", ""),
Reading::Verdict(false)
);
}
#[test]
fn zsteg_on_a_clean_image_says_no_rather_than_failing() {
assert_eq!(zsteg("", ""), Reading::Verdict(false));
}
#[test]
fn zsteg_ignores_a_plus_marker_which_is_not_what_it_prints() {
assert_eq!(
zsteg("[+] something unrelated\n", ""),
Reading::Verdict(false)
);
}
#[test]
fn stegexpose_takes_the_last_column_of_the_last_row() {
let out = "File name,Secret size,Primary Sets,Chi Square,Sample Pairs,RS analysis,Fusion (mean)\n\
a.png,1024,0.1,0.2,0.3,0.4,0.2751\n";
assert_eq!(stegexpose(out, ""), Reading::Score(0.2751));
}
#[test]
fn stegexpose_without_a_number_fails_rather_than_guessing_zero() {
assert!(matches!(
stegexpose("File name,Fusion (mean)\n", ""),
Reading::Failed(_)
));
}
#[test]
fn a_bare_number_is_read_as_a_score() {
assert_eq!(number("0.0412345678\n", ""), Reading::Score(0.0412345678));
}
#[test]
fn a_negative_estimate_is_kept_because_it_is_the_noise_floor() {
assert_eq!(number("-0.0093\n", ""), Reading::Score(-0.0093));
}
#[test]
fn no_number_reports_why_rather_than_returning_zero() {
let r = number(
"",
"aletheia is not importable in this container: no module",
);
match r {
Reading::Failed(why) => assert!(why.contains("not importable"), "got {why}"),
other => panic!("expected Failed, got {other:?}"),
}
}
#[test]
fn a_non_finite_estimate_is_not_a_score() {
assert!(matches!(number("NaN\n", "bad"), Reading::Failed(_)));
}
#[test]
fn stegcore_reads_the_score_out_of_its_envelope() {
let out = r#"{"ok":true,"data":[{"overall_score":0.4192,"verdict":"suspicious"}]}"#;
assert_eq!(stegcore(out, ""), Reading::Score(0.4192));
}
#[test]
fn a_stegcore_failure_is_not_read_as_a_clean_image() {
let out = r#"{"ok":false,"error":"unsupported format"}"#;
match stegcore(out, "") {
Reading::Failed(why) => assert!(why.contains("unsupported format")),
other => panic!("expected Failed, got {other:?}"),
}
}
#[test]
fn stegcore_without_json_says_so_rather_than_scoring_zero() {
assert!(matches!(stegcore("not json", "boom"), Reading::Failed(_)));
}
#[test]
fn an_unknown_parser_name_fails_loudly() {
assert!(matches!(parse("nope", "", ""), Reading::Failed(_)));
}
#[test]
fn a_failed_reading_is_neither_yes_nor_no() {
assert_eq!(Reading::Failed("x".into()).says_stego(true, 0.5), None);
}
#[test]
fn a_score_is_compared_in_the_declared_direction() {
assert_eq!(Reading::Score(0.9).says_stego(true, 0.5), Some(true));
assert_eq!(Reading::Score(0.9).says_stego(false, 0.5), Some(false));
}
#[test]
fn a_reading_becomes_a_complete_record() {
assert!(Reading::Verdict(false).into_record("1").is_complete());
assert!(Reading::Failed("x".into()).into_record("1").is_complete());
}
#[test]
fn every_name_the_registry_accepts_is_a_parser_this_file_dispatches() {
for name in stegobench_core::registry::PARSERS {
let reading = parse(name, "", "");
if let Reading::Failed(why) = &reading {
assert!(
!why.contains("no built-in parser named"),
"{name} is accepted by the registry and dispatches to nothing"
);
}
}
let invented = parse("not-a-parser-xyzzy", "", "");
match invented {
Reading::Failed(why) => assert!(
why.contains("no built-in parser named"),
"an unknown parser should say so: {why}"
),
other => panic!("an unknown parser was handled: {other:?}"),
}
}
fn keys(n: usize) -> Vec<String> {
(0..n).map(|i| format!("/work/{i:06}.png")).collect()
}
#[test]
fn two_numbers_for_one_image_is_refused_rather_than_resolved_by_guessing() {
let got = number("0.05\n0.07\n", "");
match &got {
Reading::Failed(why) => {
assert!(why.contains("more than one number"), "{why}");
assert!(why.contains("0.05") && why.contains("0.07"), "{why}");
assert!(why.contains("invoke.batch"), "no route out is named: {why}");
}
other => panic!("an ambiguous answer was resolved by guessing: {other:?}"),
}
}
#[test]
fn one_number_among_prose_is_still_one_answer() {
assert_eq!(
number("reading image\n0.42\ndone\n", ""),
Reading::Score(0.42)
);
}
#[test]
fn no_number_at_all_still_says_so_in_its_own_words() {
match number("nothing numeric here\n", "it fell over") {
Reading::Failed(why) => {
assert!(why.contains("no number on stdout"), "{why}");
assert!(why.contains("it fell over"), "the tool's own words: {why}");
}
other => panic!("{other:?}"),
}
}
#[test]
fn a_keyed_batch_maps_each_answer_to_the_image_it_names() {
let k = keys(3);
let out = format!("{}\t0.25\n{}\t0.50\n{}\t0.75\n", k[0], k[1], k[2]);
let got = parse_keyed("number", &out, "", &k);
assert_eq!(
got,
vec![
Reading::Score(0.25),
Reading::Score(0.50),
Reading::Score(0.75)
]
);
}
#[test]
fn answers_out_of_order_land_on_the_right_images() {
let k = keys(3);
let out = format!("{}\t0.75\n{}\t0.25\n{}\t0.50\n", k[2], k[0], k[1]);
let got = parse_keyed("number", &out, "", &k);
assert_eq!(
got,
vec![
Reading::Score(0.25),
Reading::Score(0.50),
Reading::Score(0.75)
]
);
}
#[test]
fn a_skipped_image_is_unanswered_and_does_not_shift_the_others() {
let k = keys(3);
let out = format!("{}\t0.25\n{}\t0.75\n", k[0], k[2]);
let got = parse_keyed("number", &out, "boom: cannot read it", &k);
assert_eq!(got[0], Reading::Score(0.25));
assert_eq!(got[2], Reading::Score(0.75));
match &got[1] {
Reading::Failed(why) => {
assert!(why.contains("printed no line for it"), "{why}");
assert!(why.contains("not a zero"), "{why}");
}
other => panic!("expected the skipped image to be unanswered, got {other:?}"),
}
}
#[test]
fn an_answer_for_an_image_not_in_the_batch_fails_the_whole_batch() {
let k = keys(2);
let out = format!("{}\t0.25\n/work/999999.png\t0.9\n", k[0]);
let got = parse_keyed("number", &out, "", &k);
assert_eq!(got.len(), 2);
for reading in &got {
match reading {
Reading::Failed(why) => assert!(why.contains("not in the batch"), "{why}"),
other => panic!("expected the batch to be refused, got {other:?}"),
}
}
}
#[test]
fn a_line_with_no_tab_fails_the_whole_batch_rather_than_being_skipped() {
let k = keys(2);
let got = parse_keyed("number", "0.25\n0.75\n", "", &k);
assert_eq!(got.len(), 2);
for reading in &got {
match reading {
Reading::Failed(why) => assert!(why.contains("no tab in it"), "{why}"),
other => panic!("expected refusal, got {other:?}"),
}
}
}
#[test]
fn a_batch_that_printed_nothing_leaves_every_image_unanswered() {
let k = keys(3);
let got = parse_keyed("number", "", "the container died", &k);
assert_eq!(got.len(), 3);
for reading in &got {
match reading {
Reading::Failed(why) => assert!(why.contains("the container died"), "{why}"),
other => panic!("expected unanswered, got {other:?}"),
}
}
}
#[test]
fn blank_lines_and_trailing_whitespace_are_not_answers_and_are_not_errors() {
let k = keys(1);
let out = format!("\n \n{}\t0.5 \n\n", k[0]);
assert_eq!(
parse_keyed("number", &out, "", &k),
vec![Reading::Score(0.5)]
);
}
#[test]
fn an_empty_batch_asks_nothing_and_answers_nothing() {
assert!(parse_keyed("number", "", "", &[]).is_empty());
}
#[test]
fn a_negative_estimate_survives_the_keyed_route() {
let k = keys(1);
let out = format!("{}\t-0.0067568111\n", k[0]);
assert_eq!(
parse_keyed("number", &out, "", &k),
vec![Reading::Score(-0.0067568111)]
);
}
#[test]
fn each_answer_goes_through_the_same_parser_an_unbatched_run_would_use() {
let k = keys(1);
let out = format!("{}\tnot-a-number\n", k[0]);
match &parse_keyed("number", &out, "stderr tail", &k)[0] {
Reading::Failed(why) => assert!(why.contains("no number on stdout"), "{why}"),
other => panic!("expected the number parser to refuse, got {other:?}"),
}
}
#[test]
fn only_the_first_tab_separates_so_an_extra_column_is_refused_not_guessed_at() {
let k = keys(1);
let out = format!("{}\t0.5\textra\n", k[0]);
match &parse_keyed("number", &out, "", &k)[0] {
Reading::Failed(why) => assert!(why.contains("no number on stdout"), "{why}"),
other => panic!("expected an off protocol line to be refused, got {other:?}"),
}
}
}