use dig_peer::rpc;
const STRANGER_MARKER: &str = "PEER-CHOSEN-TEXT-0f9a1c";
#[test]
fn serde_json_quotes_the_peer_input_back_verbatim() {
let body = format!(r#""{STRANGER_MARKER}""#);
let raw = serde_json::from_slice::<u64>(body.as_bytes())
.expect_err("a string is not a u64")
.to_string();
assert!(raw.contains(STRANGER_MARKER), "premise gone: {raw}");
}
#[test]
fn peer_bytes_never_reach_a_codec_error() {
let body = format!(r#""{STRANGER_MARKER}""#);
let err = rpc::from_json::<u64>(body.as_bytes()).expect_err("a string is not a u64");
for rendered in [err.to_string(), format!("{err:?}")] {
assert!(
!rendered.contains(STRANGER_MARKER),
"the codec error echoed the peer's bytes: {rendered}"
);
}
}
#[test]
fn a_codec_error_still_diagnoses_the_failure() {
let body = format!(r#""{STRANGER_MARKER}""#);
let err = rpc::from_json::<u64>(body.as_bytes()).expect_err("a string is not a u64");
let msg = err.to_string();
assert!(
msg.contains("line 1") && msg.contains("column"),
"a developer must still be able to locate the failure: {msg}"
);
assert!(
msg.contains("data"),
"a developer must still be able to tell malformed JSON from a wrong shape: {msg}"
);
}
#[test]
fn a_forged_log_line_cannot_be_smuggled_through_a_codec_error() {
let body = serde_json::to_vec(&serde_json::json!(format!(
"{STRANGER_MARKER}\n2026-07-31T00:00:00Z ERROR forged"
)))
.expect("the fixture serializes");
let err = rpc::from_json::<u64>(&body).expect_err("a string is not a u64");
let msg = err.to_string();
assert!(
!msg.contains('\n') && !msg.contains('\r'),
"a peer forged a line break: {msg:?}"
);
assert!(!msg.contains("forged"), "the error echoed it: {msg:?}");
}