use crate::codec::{Error, ErrorKind, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct InvocationCounter(u32);
impl InvocationCounter {
#[must_use]
pub const fn new(v: u32) -> Self {
Self(v)
}
#[must_use]
pub const fn get(self) -> u32 {
self.0
}
#[allow(
clippy::should_implement_trait,
reason = "a counter is not an iterator; `next` is the domain's word"
)]
pub fn next(&mut self) -> Result<u32> {
let next = self.0.checked_add(1).ok_or(Error::new(ErrorKind::Unsupported, 0))?;
self.0 = next;
Ok(next)
}
}
pub const REPLAY_WINDOW_MAX: u32 = 64;
pub const REPLAY_WIDTH_MAX: u32 = REPLAY_WINDOW_MAX - 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ReplayWindow {
highest: Option<u32>,
seen: u64,
width: u32,
}
impl ReplayWindow {
#[must_use]
pub const fn strict() -> Self {
Self { highest: None, seen: 0, width: 0 }
}
#[must_use]
pub const fn new(width: u32) -> Self {
Self {
highest: None,
seen: 0,
width: if width > REPLAY_WIDTH_MAX { REPLAY_WIDTH_MAX } else { width },
}
}
#[must_use]
pub const fn width(&self) -> u32 {
self.width
}
#[must_use]
pub const fn resumed(highest: u32, width: u32) -> Self {
let mut w = Self::new(width);
w.highest = Some(highest);
w.seen = u64::MAX;
w
}
#[must_use]
pub const fn highest(&self) -> Option<u32> {
self.highest
}
#[must_use]
pub const fn expected_next(&self) -> u32 {
match self.highest {
Some(highest) => highest.saturating_add(1),
None => 0,
}
}
pub fn check(&self, received: u32) -> Result<()> {
let replay = || Error::new(ErrorKind::Replay, 0);
let Some(highest) = self.highest else {
return Ok(());
};
if received > highest {
return Ok(());
}
let back = highest - received;
if back > self.width || back >= REPLAY_WINDOW_MAX {
return Err(replay());
}
if self.seen & (1u64 << back) != 0 {
return Err(replay());
}
Ok(())
}
pub fn accept(&mut self, received: u32) -> Result<()> {
self.check(received)?;
match self.highest {
None => {
self.highest = Some(received);
self.seen = 1;
}
Some(highest) if received > highest => {
let advance = received - highest;
self.seen = if advance >= REPLAY_WINDOW_MAX { 0 } else { self.seen << advance };
self.seen |= 1;
self.highest = Some(received);
}
Some(highest) => {
let back = highest - received;
self.seen |= 1u64 << back;
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_first_value_sent_is_one_past_the_stored_one() {
let mut c = InvocationCounter::new(0);
assert_eq!(c.next().unwrap(), 1);
assert_eq!(c.next().unwrap(), 2);
assert_eq!(c.get(), 2);
}
#[test]
fn exhaustion_refuses_rather_than_wrapping() {
let mut c = InvocationCounter::new(u32::MAX);
assert_eq!(c.next().unwrap_err().kind, ErrorKind::Unsupported);
assert_eq!(c.get(), u32::MAX, "and does not move");
}
#[test]
fn a_strict_window_accepts_only_what_is_higher() {
let mut w = ReplayWindow::strict();
assert!(w.accept(5).is_ok(), "the first message has nothing to beat");
assert!(w.accept(6).is_ok());
assert_eq!(w.accept(6).unwrap_err().kind, ErrorKind::Replay, "equal is a replay");
assert_eq!(w.accept(5).unwrap_err().kind, ErrorKind::Replay, "lower is a replay");
assert!(w.accept(7).is_ok());
assert_eq!(w.highest(), Some(7));
}
#[test]
fn a_widened_window_still_accepts_each_value_exactly_once() {
let mut w = ReplayWindow::new(16);
assert!(w.accept(100).is_ok());
for v in [99u32, 95, 88, 97, 90] {
assert!(w.accept(v).is_ok(), "{v} has not been seen");
assert_eq!(w.accept(v).unwrap_err().kind, ErrorKind::Replay, "{v} twice is a replay");
}
assert_eq!(w.accept(100).unwrap_err().kind, ErrorKind::Replay);
}
#[test]
fn a_value_older_than_the_window_is_refused_however_wide_it_is() {
let mut w = ReplayWindow::new(REPLAY_WINDOW_MAX * 4);
assert!(w.accept(1000).is_ok());
assert_eq!(w.accept(1000 - REPLAY_WINDOW_MAX).unwrap_err().kind, ErrorKind::Replay);
assert!(w.accept(1000 - REPLAY_WINDOW_MAX + 1).is_ok(), "the oldest value still inside");
}
#[test]
fn a_large_jump_forward_empties_the_window_rather_than_shifting_out_of_range() {
let mut w = ReplayWindow::new(32);
assert!(w.accept(1).is_ok());
assert!(w.accept(u32::MAX).is_ok(), "a jump of four billion must not panic");
assert_eq!(w.highest(), Some(u32::MAX));
assert_eq!(w.accept(u32::MAX).unwrap_err().kind, ErrorKind::Replay);
assert_eq!(w.accept(1).unwrap_err().kind, ErrorKind::Replay, "long since out of the window");
}
#[test]
fn checking_does_not_record() {
let mut w = ReplayWindow::strict();
assert!(w.accept(10).is_ok());
assert!(w.check(11).is_ok());
assert!(w.check(11).is_ok(), "checking twice is still fine: nothing was recorded");
assert_eq!(w.highest(), Some(10), "a check must not advance the window");
assert!(w.accept(11).is_ok());
}
#[test]
fn the_widest_window_accepts_exactly_as_far_back_as_it_claims() {
let w = ReplayWindow::new(u32::MAX);
assert_eq!(w.width(), REPLAY_WIDTH_MAX);
for width in [0u32, 1, 7, REPLAY_WIDTH_MAX] {
let mut w = ReplayWindow::new(width);
assert!(w.accept(10_000).is_ok());
if width > 0 {
assert!(w.accept(10_000 - width).is_ok(), "width {width} refuses its own furthest value");
}
assert_eq!(
w.accept(10_000 - width - 1).unwrap_err().kind,
ErrorKind::Replay,
"width {width} accepts one past its own bound"
);
}
}
#[test]
fn a_resumed_window_does_not_reaccept_what_was_stored() {
let mut w = ReplayWindow::resumed(500, 8);
assert_eq!(w.accept(500).unwrap_err().kind, ErrorKind::Replay);
assert_eq!(w.accept(495).unwrap_err().kind, ErrorKind::Replay);
assert!(w.accept(501).is_ok());
}
}