chat: send and receive as ratchet frames through the outbox payload
All checks were successful
ci / test (pull_request) Successful in 4m36s
ci / sdk (pull_request) Successful in 1m15s
android-ci / android (pull_request) Successful in 9m25s

Chat plaintext is ratchet-encrypted once, before the first delivery attempt,
and the frame is what the outbox stores and retries, so a retried message
re-sends identical bytes instead of advancing the sending chain twice.

- dispatch: Kind::Ratchet for the `rat ` prefix, matched before the unknown
  [a-z]{3} prefix rule
- node: encode_chat_frame / ingest_chat_frame (the wire halves of Node::send
  and handle_incoming), bootstrap derived from the peers' static prekey DH
  because the Noise handshake is frozen and carries no ratchet key
- outbox: nullable outbox.payload + Outbox::send_payload; NULL payload keeps
  the previous row-for-row behaviour
- store: additive outbox.payload migration, enqueue_outbox_payload,
  OutboxEntry::payload
- add_friend_payload: a changed prekey clears the (prekey-bound) ratchet state

tests/ratchet_wire.rs (5) covers one frame per send, byte-identical retries,
out-of-order delivery, replay, tampering and fresh-friend both directions.

tests/tui_spec.rs and crates/onionwire-sdk/src/lib.rs only gained a
`payload: None` field in their OutboxEntry test fixtures: the card's required
field on a pub struct cannot be added without it.
This commit is contained in:
agent 2026-09-16 19:02:52 -04:00
commit 69a15fe909
No known key found for this signature in database
9 changed files with 619 additions and 13 deletions

View file

@ -275,6 +275,62 @@ async fn flush_friend_ignores_the_schedule_when_that_peer_was_seen() {
assert_eq!(fired[0].1, OutboxStatus::Sent);
}
#[tokio::test]
async fn payload_row_sends_the_payload_and_keeps_the_body_as_history() {
let dir = tempfile::tempdir().unwrap();
let store = store_and_friend(dir.path(), "flaky.onion");
// First dial fails, the retry succeeds: the payload must survive that.
let flaky = Flaky::new(1, "connect flaky.onion:80: timed out");
let ob = outbox(Arc::clone(&store), flaky.clone(), 8);
let frame = b"rat \x01opaque-frame";
let status = ob.send_payload(&pk(1), b"chat text", frame).await.unwrap();
assert_eq!(status, OutboxStatus::Queued);
let pending = ob.pending(&pk(1)).unwrap();
assert_eq!(
pending[0].payload.as_deref(),
Some(&frame[..]),
"the queued row carries the frame, not the plaintext"
);
assert_eq!(
pending[0].body, b"chat text",
"the chat row still holds the plaintext"
);
let now = pending[0].next_attempt_at;
let fired = ob.flush_friend(&pk(1), now).await.unwrap();
assert_eq!(fired[0].1, OutboxStatus::Sent);
assert_eq!(
flaky.delivered(),
vec![frame.to_vec()],
"the retry put the frame on the wire, not the body"
);
}
#[tokio::test]
async fn null_payload_rows_still_send_their_body() {
let dir = tempfile::tempdir().unwrap();
let store = store_and_friend(dir.path(), "flaky.onion");
let flaky = Flaky::new(1, "connect flaky.onion:80: timed out");
let ob = outbox(Arc::clone(&store), flaky.clone(), 8);
// A locator/profile/payment-style row: no payload, so the body is the wire
// form. This is the pre-payload behaviour, row for row.
assert_eq!(
ob.send(&pk(1), b"loc abc.onion\n1").await.unwrap(),
OutboxStatus::Queued
);
assert_eq!(ob.pending(&pk(1)).unwrap()[0].payload, None);
let now = ob.pending(&pk(1)).unwrap()[0].next_attempt_at;
assert_eq!(
ob.flush_friend(&pk(1), now).await.unwrap()[0].1,
OutboxStatus::Sent
);
assert_eq!(flaky.delivered(), vec![b"loc abc.onion\n1".to_vec()]);
}
#[tokio::test]
async fn wipe_clears_outbox_rows() {
let dir = tempfile::tempdir().unwrap();