use std::time::Instant;
use proptest::prelude::*;
use rstest::rstest;
use super::{admit_first, continuation_header, first_header, state_with_limit};
use crate::message_assembler::{
MessageAssemblyError,
MessageAssemblyState,
MessageKey,
StreamingFailure,
};
#[derive(Clone, Copy, Debug)]
enum DeclaredTotal {
Exceeded,
Mismatch,
}
impl DeclaredTotal {
fn matches(self, error: &MessageAssemblyError) -> bool {
match self {
Self::Exceeded => matches!(error, MessageAssemblyError::DeclaredTotalExceeded { .. }),
Self::Mismatch => matches!(error, MessageAssemblyError::DeclaredTotalMismatch { .. }),
}
}
}
struct DeclaredTotalCase {
key: u64,
declared_total: usize,
continuation: (u32, usize, bool),
body: &'static [u8],
expected: DeclaredTotal,
}
const DECLARED_TOTAL_OVERSHOOT: DeclaredTotalCase = DeclaredTotalCase {
key: 3,
declared_total: 6,
continuation: (1, 4, false),
body: b"bbbb",
expected: DeclaredTotal::Exceeded,
};
const DECLARED_TOTAL_SHORT_FINAL: DeclaredTotalCase = DeclaredTotalCase {
key: 4,
declared_total: 8,
continuation: (1, 2, true),
body: b"bb",
expected: DeclaredTotal::Mismatch,
};
fn reject_declared_total_continuation(
case: &DeclaredTotalCase,
) -> (MessageAssemblyState, StreamingFailure) {
let mut state = state_with_limit(1024);
let start = Instant::now();
admit_first(
&mut state,
&first_header(case.key, 4, Some(case.declared_total), false),
b"aaaa",
start,
);
let (sequence, body_len, is_last) = case.continuation;
let header = continuation_header(case.key, sequence, body_len, is_last);
let failure = state
.accept_streaming_continuation_at(&header, case.body, start)
.expect_err("the continuation cannot satisfy the declared total");
(state, failure)
}
#[rstest]
#[case::overshoot(&DECLARED_TOTAL_OVERSHOOT)]
#[case::short_final(&DECLARED_TOTAL_SHORT_FINAL)]
fn a_continuation_that_breaks_the_declared_total_is_terminal(#[case] case: &DeclaredTotalCase) {
let (state, failure) = reject_declared_total_continuation(case);
assert!(
case.expected.matches(failure.source_error()),
"expected {:?}, got {:?}",
case.expected,
failure.source_error()
);
assert!(
failure.is_terminal(),
"a rejected declaration closes the message"
);
assert_eq!(failure.key(), MessageKey(case.key));
assert!(
!state.is_streaming_key(MessageKey(case.key)),
"the partial is removed"
);
}
proptest! {
#[test]
fn continuation_outcomes_follow_the_declared_total(
declared in 4_usize..=64,
continuation_len in 0_usize..=64,
is_last in any::<bool>(),
) {
const FIRST: usize = 4;
let key = MessageKey(1);
let mut state = state_with_limit(1024);
let start = Instant::now();
admit_first(
&mut state,
&first_header(1, FIRST, Some(declared), false),
b"aaaa",
start,
);
let header = continuation_header(1, 1, continuation_len, is_last);
let body = vec![b'b'; continuation_len];
let outcome = state.accept_streaming_continuation_at(&header, &body, start);
let attempted = FIRST + continuation_len;
match outcome {
Ok(_) => {
prop_assert!(attempted <= declared, "an overrun must not be admitted");
prop_assert!(
!is_last || attempted == declared,
"a completing frame must match the declaration exactly"
);
prop_assert_eq!(
state.is_streaming_key(key),
!is_last,
"only a completed message leaves no partial behind"
);
}
Err(failure) => {
prop_assert!(
attempted > declared || (is_last && attempted != declared),
"a continuation that honours the declaration is not terminal"
);
let source = failure.source_error();
match source {
MessageAssemblyError::DeclaredTotalExceeded { .. } => {
prop_assert!(attempted > declared, "exceeded means the total was overrun");
}
MessageAssemblyError::DeclaredTotalMismatch { .. } => {
prop_assert!(
is_last && attempted != declared,
"a mismatch is only possible when the frame completes the body"
);
}
other => prop_assert!(false, "unexpected failure: {other:?}"),
}
prop_assert!(failure.is_terminal(), "a broken declaration closes the message");
prop_assert!(
!state.is_streaming_key(key),
"a terminal violation removes the partial"
);
}
}
}
}