wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Per-message cap coverage for streaming admission.
//!
//! Streaming frames are admitted while the message is still arriving, so the
//! same metadata-inclusive cap that guards buffered assembly must be applied
//! here too: metadata counts alongside the body, a declared total may only
//! tighten the measurement, and an unrepresentable sum is refused rather than
//! saturating to a value a `usize::MAX` limit would accept.
//!
//! Split from `streaming_tests.rs` to keep each test module within the
//! repository's module size limit.

use std::{
    num::NonZeroUsize,
    time::{Duration, Instant},
};

use super::{
    EnvelopeRouting,
    FirstFrameHeader,
    FirstFrameInput,
    MessageAssemblyError,
    MessageAssemblyState,
    MessageKey,
};

/// Assembly timeout used by these tests.
const TIMEOUT: Duration = Duration::from_mins(1);

/// Build an assembly state with a per-message cap of `limit` bytes.
fn state_with_limit(limit: usize) -> MessageAssemblyState {
    let max = NonZeroUsize::new(limit).expect("test limit must be non-zero");
    MessageAssemblyState::new(max, TIMEOUT)
}

#[test]
fn first_frame_cap_counts_the_declared_total_in_place_of_the_body() {
    // A declared total may only tighten the cap, so it is measured even when
    // the body the frame carries so far is short of it. Four metadata bytes
    // plus a declared eleven retain fifteen.
    let mut state = state_with_limit(11);
    let start = Instant::now();
    let header = FirstFrameHeader {
        message_key: MessageKey(1),
        metadata_len: 4,
        body_len: 7,
        total_body_len: Some(11),
        is_last: false,
    };
    let input = FirstFrameInput::new(
        &header,
        EnvelopeRouting::default(),
        vec![0x01, 0x02, 0x03, 0x04],
        b"abcdefg",
    )
    .expect("header lengths must match the payload");

    let error = state
        .accept_streaming_first_frame_at(input, start)
        .expect_err("a declaration larger than the body must tighten the cap");

    assert!(
        matches!(
            error,
            MessageAssemblyError::MessageTooLarge { attempted: 15, .. }
        ),
        "the declaration plus metadata should be measured, got {error:?}"
    );
}

#[test]
fn first_frame_cap_counts_metadata_alongside_the_body() {
    // Four metadata bytes plus seven body bytes retain eleven, one over a
    // ten-byte cap. A body-only reading of the cap would have admitted the
    // frame, so this pins metadata-inclusive measurement on the first-frame
    // streaming path, mirroring the continuation coverage above.
    let mut state = state_with_limit(10);
    let start = Instant::now();
    let header = FirstFrameHeader {
        message_key: MessageKey(1),
        metadata_len: 4,
        body_len: 7,
        total_body_len: None,
        is_last: true,
    };
    let input = FirstFrameInput::new(
        &header,
        EnvelopeRouting::default(),
        vec![0x01, 0x02, 0x03, 0x04],
        b"abcdefg",
    )
    .expect("header lengths must match the payload");

    let error = state
        .accept_streaming_first_frame_at(input, start)
        .expect_err("metadata plus body exceeds the cap");

    assert!(
        matches!(
            error,
            MessageAssemblyError::MessageTooLarge { attempted: 11, .. }
        ),
        "metadata plus the body should be measured, got {error:?}"
    );
}

#[test]
fn first_frame_cap_rejects_an_unrepresentable_sum_at_the_largest_limit() {
    // The behavioural guard for the checked helper on this path. Saturating
    // arithmetic would collapse the sum to `usize::MAX`, which is not greater
    // than a `usize::MAX` limit, so the frame would be admitted with no way to
    // measure what it retains.
    let max = NonZeroUsize::new(usize::MAX).expect("usize::MAX is non-zero");
    let mut state = MessageAssemblyState::new(max, TIMEOUT);
    let start = Instant::now();
    let header = FirstFrameHeader {
        message_key: MessageKey(1),
        metadata_len: 1,
        body_len: 0,
        total_body_len: Some(usize::MAX),
        is_last: false,
    };
    let input = FirstFrameInput::new(&header, EnvelopeRouting::default(), vec![0x01], b"")
        .expect("header lengths must match the payload");

    let error = state
        .accept_streaming_first_frame_at(input, start)
        .expect_err("an unrepresentable sum must be rejected");

    assert!(
        matches!(
            error,
            MessageAssemblyError::MessageTooLarge {
                attempted: usize::MAX,
                ..
            }
        ),
        "the overflow sentinel should be reported, got {error:?}"
    );
}