Merge branch 'main' into wt/t_17e3d3cf-rustls-default
This commit is contained in:
commit
049b612169
7 changed files with 472 additions and 14 deletions
199
tests/ratchet.rs
199
tests/ratchet.rs
|
|
@ -147,6 +147,130 @@ fn round_trip_across_two_dh_ratchet_steps() {
|
|||
assert_eq!(a.decrypt(&m4).expect("decrypt m4"), b"m4");
|
||||
}
|
||||
|
||||
/// The ratchet key a frame advertises: `dh_pub[0..32]` of the header.
|
||||
fn dh_pub(frame: &[u8]) -> [u8; 32] {
|
||||
header_at(frame)[..32].try_into().expect("32")
|
||||
}
|
||||
|
||||
/// Lockstep on a bidirectional burst: every frame after a peer has stepped
|
||||
/// carries the key that peer is now sending with, and the receiver *adopts
|
||||
/// exactly that key* before its sending chain rekeys.
|
||||
///
|
||||
/// `round_trip_across_two_dh_ratchet_steps` only asserts that neither sending
|
||||
/// ratchet stayed put; it would still pass if the two sides stepped onto
|
||||
/// unconnected epochs, and it never checks that either side adopted the key the
|
||||
/// other advertised. This test reads the key out of the wire header and asserts
|
||||
/// the adoption, per step, on a burst in both directions.
|
||||
///
|
||||
/// The bootstrap tie-break means one A→B/B→A round cannot step both sides: only
|
||||
/// the lower-key peer rotates its sending chain on establishment, and the other
|
||||
/// peer has still only ever seen bootstrap keys. The burst therefore drives the
|
||||
/// second and third rounds, and pins the intermediate state at each one.
|
||||
#[test]
|
||||
fn bidirectional_burst_keeps_the_dh_ratchet_in_lockstep() {
|
||||
let (mut a, mut b) = peers();
|
||||
let (a0, b0) = (a.public(), b.public());
|
||||
assert_eq!(a.remote_pub(), b0, "a starts keyed to b's bootstrap key");
|
||||
assert_eq!(b.remote_pub(), a0, "b starts keyed to a's bootstrap key");
|
||||
|
||||
// Round 1 — A→B then B→A. Only a rotates here (it sorts lower).
|
||||
let m1 = a.encrypt(b"a/1").expect("encrypt");
|
||||
assert_eq!(
|
||||
dh_pub(&m1),
|
||||
a0,
|
||||
"a's first frame advertises its bootstrap key"
|
||||
);
|
||||
assert_eq!(b.decrypt(&m1).expect("b decrypts a/1"), b"a/1");
|
||||
assert_eq!(b.public(), b0, "b has no new key to adopt yet");
|
||||
assert_eq!(b.remote_pub(), a0);
|
||||
|
||||
let m2 = b.encrypt(b"b/1").expect("encrypt");
|
||||
assert_eq!(
|
||||
dh_pub(&m2),
|
||||
b0,
|
||||
"b's first frame advertises its bootstrap key"
|
||||
);
|
||||
assert_eq!(a.decrypt(&m2).expect("a decrypts b/1"), b"b/1");
|
||||
let a1 = a.public();
|
||||
assert_ne!(a1, a0, "round 1 must rotate a's sending ratchet key");
|
||||
assert_eq!(a.remote_pub(), b0, "a is keyed to the key b advertised");
|
||||
|
||||
// Round 2 — a's fresh key reaches b, which is the step the one-round
|
||||
// exchange never takes.
|
||||
let m3 = a.encrypt(b"a/2").expect("encrypt");
|
||||
assert_eq!(dh_pub(&m3), a1, "a advertises the key it just ratcheted to");
|
||||
assert_eq!(b.decrypt(&m3).expect("b decrypts a/2"), b"a/2");
|
||||
let b1 = b.public();
|
||||
assert_ne!(b1, b0, "b must DH-ratchet on a's new key");
|
||||
assert_eq!(b.remote_pub(), a1, "b adopted exactly the key a advertised");
|
||||
|
||||
// Round 3 — the mirror image: b's fresh key reaches a.
|
||||
let m4 = b.encrypt(b"b/2").expect("encrypt");
|
||||
assert_eq!(dh_pub(&m4), b1, "b advertises the key it just ratcheted to");
|
||||
assert_eq!(a.decrypt(&m4).expect("a decrypts b/2"), b"b/2");
|
||||
let a2 = a.public();
|
||||
assert_ne!(a2, a1, "a stepped onto a fresh key of its own");
|
||||
assert_eq!(a.remote_pub(), b1, "a adopted exactly the key b advertised");
|
||||
|
||||
// The burst proper: three frames each way on the epoch both sides now hold.
|
||||
let to_b: Vec<Vec<u8>> = (0..3)
|
||||
.map(|i| {
|
||||
a.encrypt(format!("a/burst{i}").as_bytes())
|
||||
.expect("encrypt")
|
||||
})
|
||||
.collect();
|
||||
for frame in &to_b {
|
||||
assert_eq!(dh_pub(frame), a2, "the burst stays on a's current key");
|
||||
}
|
||||
for (i, frame) in to_b.iter().enumerate() {
|
||||
assert_eq!(
|
||||
b.decrypt(frame).expect("burst a→b"),
|
||||
format!("a/burst{i}").as_bytes()
|
||||
);
|
||||
}
|
||||
let b2 = b.public();
|
||||
assert_ne!(
|
||||
b2, b1,
|
||||
"b's first burst frame is a full step, not a chain read"
|
||||
);
|
||||
assert_eq!(b.remote_pub(), a2, "b adopted a's burst key");
|
||||
assert_eq!(b.recv_index(), 3, "all three burst frames were consumed");
|
||||
assert_eq!(b.skipped_len(), 0, "in-order burst skipped nothing");
|
||||
|
||||
let to_a: Vec<Vec<u8>> = (0..3)
|
||||
.map(|i| {
|
||||
b.encrypt(format!("b/burst{i}").as_bytes())
|
||||
.expect("encrypt")
|
||||
})
|
||||
.collect();
|
||||
for frame in &to_a {
|
||||
assert_eq!(dh_pub(frame), b2, "the burst stays on b's current key");
|
||||
}
|
||||
for (i, frame) in to_a.iter().enumerate() {
|
||||
assert_eq!(
|
||||
a.decrypt(frame).expect("burst b→a"),
|
||||
format!("b/burst{i}").as_bytes()
|
||||
);
|
||||
}
|
||||
|
||||
// Lockstep after the burst: the peer that received last is keyed to the
|
||||
// other's *current* key, and the sender's own key is the one its burst
|
||||
// advertised. (b's view of a is a2 — a's step generated a3, which has not
|
||||
// been sent yet; that lag is the ratchet's normal one-header asymmetry.)
|
||||
assert_eq!(a.remote_pub(), b2);
|
||||
assert_eq!(a.remote_pub(), b.public(), "a is keyed to b's current key");
|
||||
assert_eq!(b.remote_pub(), a2, "b is keyed to a's last advertised key");
|
||||
assert_ne!(a.public(), a2, "a stepped again on b's burst");
|
||||
assert_ne!(a.public(), a0, "a never ratcheted across the whole burst");
|
||||
assert_ne!(b.public(), b0, "b never ratcheted across the whole burst");
|
||||
assert_eq!(a.skipped_len(), 0);
|
||||
assert_eq!(a.recv_index(), 3);
|
||||
|
||||
// And the conversation still flows on the epoch the burst left behind.
|
||||
let after = a.encrypt(b"a/after").expect("encrypt");
|
||||
assert_eq!(b.decrypt(&after).expect("a/after"), b"a/after");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_order_within_one_chain() {
|
||||
let (mut a, mut b) = peers();
|
||||
|
|
@ -372,6 +496,81 @@ fn state_round_trips_and_rejects_garbage() {
|
|||
assert!(RatchetState::from_bytes(&huge_count).is_err());
|
||||
}
|
||||
|
||||
/// Fixed size of a v1 state blob's prefix: the version byte, the root key, the
|
||||
/// local secret and public, the remote public, both chain keys, both counters,
|
||||
/// `prev_send_len`, the established flag and the skipped-key count.
|
||||
/// Documented in `src/ratchet.rs`'s `to_bytes` layout.
|
||||
const STATE_PREFIX_LEN: usize = 1 + 32 + 32 + 32 + 32 + 32 + 4 + 32 + 4 + 4 + 1 + 4;
|
||||
|
||||
/// Freeze guard for the persisted wire format. A stored session is only
|
||||
/// meaningful if both sides agree byte for byte on the layout, so a version
|
||||
/// bump or a field insertion must break this test on purpose.
|
||||
///
|
||||
/// `0x01` is asserted as a literal: asserting `STATE_VERSION` would silently
|
||||
/// follow a bump instead of catching it, which is the whole point of the
|
||||
/// guard.
|
||||
#[test]
|
||||
fn ratchet_state_blob_is_frozen_at_version_v1() {
|
||||
let local = RatchetKeyPair::from_secret([0x11u8; 32]);
|
||||
let remote = RatchetKeyPair::from_secret([0x22u8; 32]);
|
||||
let state = RatchetState::bootstrap(ROOT, local, remote.public());
|
||||
let bytes = state.to_bytes();
|
||||
|
||||
assert_eq!(
|
||||
STATE_VERSION, 1,
|
||||
"the frozen version byte changed: this is a wire-format break, not a refactor"
|
||||
);
|
||||
assert_eq!(
|
||||
bytes[0], 0x01,
|
||||
"a state blob must start with the v1 version byte"
|
||||
);
|
||||
assert_eq!(
|
||||
bytes.len(),
|
||||
STATE_PREFIX_LEN,
|
||||
"the fixed part of the v1 layout changed (a field was added, removed or resized)"
|
||||
);
|
||||
assert_eq!(
|
||||
bytes.len(),
|
||||
210,
|
||||
"the documented v1 prefix is 210 bytes; a different number means the \
|
||||
format moved and stored sessions need a migration path"
|
||||
);
|
||||
}
|
||||
|
||||
/// A blob written by another version must come back as a typed
|
||||
/// `UnsupportedStateVersion`, never as a panic and never as a state decoded
|
||||
/// under the wrong layout. The guard must also not be over-eager: the
|
||||
/// untouched v1 blob still loads.
|
||||
#[test]
|
||||
fn ratchet_state_foreign_version_is_rejected_not_misread() {
|
||||
let (mut a, mut b) = peers();
|
||||
// Decrypting m2 first leaves m0/m1 in the skipped store, so the blob is
|
||||
// longer than the fixed prefix and the check cannot pass by luck.
|
||||
let frames: Vec<Vec<u8>> = (0..3)
|
||||
.map(|i| a.encrypt(format!("m{i}").as_bytes()).expect("encrypt"))
|
||||
.collect();
|
||||
assert_eq!(b.decrypt(&frames[2]).expect("m2"), b"m2");
|
||||
let bytes = b.to_bytes();
|
||||
assert!(
|
||||
bytes.len() > STATE_PREFIX_LEN,
|
||||
"expected skipped keys in the blob, got {} bytes",
|
||||
bytes.len()
|
||||
);
|
||||
|
||||
for version in [0x00u8, 0x02, 0x7f, 0xff] {
|
||||
let mut foreign = bytes.clone();
|
||||
foreign[0] = version;
|
||||
assert_eq!(
|
||||
RatchetState::from_bytes(&foreign).expect_err("a foreign version must be rejected"),
|
||||
Error::UnsupportedStateVersion(version),
|
||||
"version {version:#04x} must be rejected under its own code, not misread or panicked on"
|
||||
);
|
||||
}
|
||||
|
||||
let reloaded = RatchetState::from_bytes(&bytes).expect("the untouched v1 blob still loads");
|
||||
assert_eq!(reloaded.to_bytes(), bytes, "v1 round trip is not stable");
|
||||
}
|
||||
|
||||
/// Mirrors the private `MIN_FRAME_LEN` in `src/ratchet.rs`
|
||||
/// (`HEADER_OFFSET + HEADER_LEN + NONCE_LEN + TAG_LEN`), which the crate does
|
||||
/// not export.
|
||||
|
|
|
|||
|
|
@ -531,7 +531,10 @@ fn identical_bodies_join_by_message_id_not_body() {
|
|||
"delivered row (id 8) must keep its mark: {:?}",
|
||||
lines[1]
|
||||
);
|
||||
assert_eq!(summary.note().as_deref(), Some("1 queued"));
|
||||
assert_eq!(
|
||||
summary.note().as_deref(),
|
||||
Some("peer unreachable, queued (1/8, retry 30s)")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -621,13 +624,19 @@ fn footer_summary_counts_queued_and_keeps_fail_closed() {
|
|||
queued: 2,
|
||||
failed: 0,
|
||||
first_error: None,
|
||||
attempts: 3,
|
||||
next_in_secs: Some(45),
|
||||
};
|
||||
assert_eq!(queued.note().as_deref(), Some("2 queued"));
|
||||
assert_eq!(
|
||||
queued.note().as_deref(),
|
||||
Some("peer unreachable, queued (3/8, retry 45s)")
|
||||
);
|
||||
|
||||
let failed = OutboxSummary {
|
||||
queued: 0,
|
||||
failed: 1,
|
||||
first_error: Some("connect peer.onion:80: connection timed out".into()),
|
||||
..OutboxSummary::default()
|
||||
};
|
||||
let note = failed.note().expect("failed rows must produce a note");
|
||||
assert!(note.starts_with("send failed:"), "{note:?}");
|
||||
|
|
@ -637,9 +646,15 @@ fn footer_summary_counts_queued_and_keeps_fail_closed() {
|
|||
queued: 3,
|
||||
failed: 1,
|
||||
first_error: Some("connect peer.onion:80: connection timed out".into()),
|
||||
attempts: 2,
|
||||
next_in_secs: Some(60),
|
||||
};
|
||||
let mixed_note = mixed.note().expect("mixed rows must produce a note");
|
||||
assert!(mixed_note.contains("3 queued"), "{mixed_note:?}");
|
||||
assert!(
|
||||
mixed_note.contains("queued (2/8, retry 1m)"),
|
||||
"{mixed_note:?}"
|
||||
);
|
||||
assert!(mixed_note.starts_with("peer unreachable"), "{mixed_note:?}");
|
||||
assert!(mixed_note.contains("send failed"), "{mixed_note:?}");
|
||||
|
||||
// Fail-closed copy stays visible in the real footer at the 80-col floor.
|
||||
|
|
@ -653,7 +668,10 @@ fn footer_summary_counts_queued_and_keeps_fail_closed() {
|
|||
assert!(line.contains("TOR") && line.contains("? help"), "{line:?}");
|
||||
|
||||
let queued_line = status_footer(80, FP, ONION, queued.note().as_deref(), main_footer_hints());
|
||||
assert!(queued_line.contains("2 queued"), "{queued_line:?}");
|
||||
assert!(
|
||||
queued_line.contains("queued (3/8, retry 45s)"),
|
||||
"{queued_line:?}"
|
||||
);
|
||||
assert!(queued_line.chars().count() <= 80);
|
||||
|
||||
// `! ` ornament (colour off) still lands on the derived failing note.
|
||||
|
|
@ -731,7 +749,7 @@ fn chat_and_footer_rows_fit_80x24_and_wrap_instead_of_clipping() {
|
|||
"long row was clipped, not wrapped:\n{screen}"
|
||||
);
|
||||
assert!(
|
||||
screen.contains("1 queued"),
|
||||
screen.contains("peer unreachable, queued (3/8, retry 45s)"),
|
||||
"footer summary missing:\n{screen}"
|
||||
);
|
||||
println!("===== AFTER outbox 80x24 (real draw_chat + status_footer) =====\n{screen}");
|
||||
|
|
@ -789,7 +807,10 @@ async fn renders_a_queued_row_from_a_real_outbox_send() {
|
|||
"real outbox row did not render: {:?}",
|
||||
lines[0]
|
||||
);
|
||||
assert_eq!(summary.note().as_deref(), Some("1 queued"));
|
||||
assert_eq!(
|
||||
summary.note().as_deref(),
|
||||
Some("peer unreachable, queued (1/8, retry 45s)")
|
||||
);
|
||||
|
||||
// And once a send crosses the circuit the row is gone, so the same message
|
||||
// renders as delivered.
|
||||
|
|
|
|||
Loading…
Reference in a new issue