use super::PushError;
#[derive(Debug)]
pub(crate) struct FrameLimit<F> {
pub(crate) max: usize,
pub(crate) payload_len: fn(&F) -> usize,
}
impl<F> Clone for FrameLimit<F> {
fn clone(&self) -> Self { *self }
}
impl<F> Copy for FrameLimit<F> {}
impl<F> FrameLimit<F> {
pub(crate) fn check(&self, frame: &F) -> Result<(), PushError> {
let size = (self.payload_len)(frame);
if size > self.max {
return Err(PushError::FrameTooLarge {
size,
max: self.max,
});
}
Ok(())
}
}
#[derive(Debug)]
pub(crate) struct FrameLimits<F> {
template: Option<FrameLimit<F>>,
codec: Option<FrameLimit<F>>,
}
impl<F> Default for FrameLimits<F> {
fn default() -> Self {
Self {
template: None,
codec: None,
}
}
}
impl<F> Clone for FrameLimits<F> {
fn clone(&self) -> Self { *self }
}
impl<F> Copy for FrameLimits<F> {}
impl<F> FrameLimits<F> {
pub(crate) const fn with_template(mut self, limit: FrameLimit<F>) -> Self {
self.template = Some(limit);
self
}
pub(crate) const fn with_codec(mut self, limit: FrameLimit<F>) -> Self {
self.codec = Some(limit);
self
}
pub(crate) fn check(&self, frame: &F) -> Result<(), PushError> {
if let Some(limit) = &self.template {
limit.check(frame)?;
}
self.codec
.as_ref()
.map_or(Ok(()), |limit| limit.check(frame))
}
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::{FrameLimit, FrameLimits};
use crate::push::PushError;
#[derive(Debug)]
struct Body(usize);
fn exact(frame: &Body) -> usize { frame.0 }
fn doubled(frame: &Body) -> usize { frame.0.saturating_mul(2) }
fn limit(max: usize) -> FrameLimit<Body> {
FrameLimit {
max,
payload_len: exact,
}
}
#[test]
fn a_frame_at_the_limit_is_accepted() {
let limits = FrameLimits::default().with_template(limit(4));
assert_eq!(limits.check(&Body(4)), Ok(()));
}
#[test]
fn a_frame_above_the_limit_reports_its_measurement() {
let limits = FrameLimits::default().with_template(limit(4));
assert_eq!(
limits.check(&Body(5)),
Err(PushError::FrameTooLarge { size: 5, max: 4 })
);
}
proptest! {
#[test]
fn a_template_limit_admits_exactly_frames_within_it(
size in 0usize..2048,
max in 0usize..2048,
) {
let limits = FrameLimits::default().with_template(limit(max));
prop_assert_eq!(limits.check(&Body(size)).is_ok(), size <= max);
}
}
proptest! {
#[test]
fn a_codec_limit_admits_exactly_frames_within_it(
size in 0usize..2048,
max in 0usize..2048,
) {
let limits = FrameLimits::default().with_codec(limit(max));
prop_assert_eq!(limits.check(&Body(size)).is_ok(), size <= max);
}
}
proptest! {
#[test]
fn combined_limits_admit_only_frames_both_accept(
size in 0usize..2048,
template_max in 0usize..2048,
codec_max in 0usize..2048,
) {
let limits = FrameLimits::default()
.with_template(limit(template_max))
.with_codec(limit(codec_max));
let outcome = limits.check(&Body(size));
prop_assert_eq!(
outcome.is_ok(),
size <= template_max && size <= codec_max
);
if size > template_max {
prop_assert_eq!(
outcome,
Err(PushError::FrameTooLarge {
size,
max: template_max,
})
);
} else if size > codec_max {
prop_assert_eq!(
outcome,
Err(PushError::FrameTooLarge {
size,
max: codec_max,
})
);
}
}
}
proptest! {
#[test]
fn each_limit_applies_its_own_measure(
size in 0usize..1024,
template_max in 0usize..1024,
codec_max in 0usize..2048,
) {
let limits = FrameLimits::default()
.with_template(limit(template_max))
.with_codec(FrameLimit {
max: codec_max,
payload_len: doubled,
});
prop_assert_eq!(
limits.check(&Body(size)).is_ok(),
size <= template_max && size.saturating_mul(2) <= codec_max
);
}
}
}