diff --git a/src/main.rs b/src/main.rs index 70a0ee5b..b53c2320 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2599,17 +2599,21 @@ fn resolve_message_inbox(root: &Path, id: &str, host: &str) -> Result { /// Body from `-m`, else stdin (so `st2 message send x < file` works). fn body_or_stdin(body: Option) -> Result { - match body { - Some(b) => Ok(b), + let body = match body { + Some(body) => body, None => { use std::io::Read as _; - let mut s = String::new(); + let mut body = String::new(); std::io::stdin() - .read_to_string(&mut s) + .read_to_string(&mut body) .context("reading message body from stdin")?; - Ok(s) + body } + }; + if body.is_empty() { + anyhow::bail!("message body must not be empty"); } + Ok(body) } /// `[identity] ` positionals: if `second` is present, `first` is the identity; otherwise diff --git a/tests/message_cli.rs b/tests/message_cli.rs index 7838d91b..863ea49f 100644 --- a/tests/message_cli.rs +++ b/tests/message_cli.rs @@ -79,6 +79,35 @@ fn sent(root: &Path, identity: &str, extra: &[&str]) -> std::process::Output { .unwrap() } +#[test] +fn send_rejects_empty_explicit_and_stdin_bodies_without_persisting_a_message() { + for body_arg in [Some(""), None] { + let tmp = tempfile::tempdir().unwrap(); + write_agent(tmp.path(), "sender"); + write_agent(tmp.path(), "recipient"); + + let mut command = Command::new(env!("CARGO_BIN_EXE_st2")); + command + .args(["message", "send", "recipient", "--root"]) + .arg(tmp.path()) + .args(["--host", "h", "--as", "sender"]) + .stdin(Stdio::null()); + if let Some(body) = body_arg { + command.args(["-m", body]); + } + + let output = command.output().unwrap(); + assert!(!output.status.success()); + assert!( + String::from_utf8_lossy(&output.stderr).contains("message body must not be empty"), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert!(!tmp.path().join("h/recipient/resources").exists()); + assert!(!tmp.path().join("h/sender/resources").exists()); + } +} + #[test] fn event_metadata_is_exposed_by_list_and_read_json() { let tmp = tempfile::tempdir().unwrap();