use crate::error::{SigError, SigResult};
pub const MAX_CONTEXT_LEN: usize = 255;
pub(crate) fn check_ctx(ctx: &[u8]) -> SigResult<()> {
if ctx.len() > MAX_CONTEXT_LEN {
return Err(SigError::ContextTooLong { len: ctx.len() });
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
extern crate alloc;
use alloc::vec;
#[test]
fn accepts_empty_and_max_len() {
assert!(check_ctx(&[]).is_ok());
assert!(check_ctx(&[0u8; MAX_CONTEXT_LEN]).is_ok());
}
#[test]
fn rejects_over_max_len() {
let ctx = vec![0u8; MAX_CONTEXT_LEN + 1];
match check_ctx(&ctx) {
Err(SigError::ContextTooLong { len }) => assert_eq!(len, MAX_CONTEXT_LEN + 1),
other => panic!("expected ContextTooLong, got {other:?}"),
}
}
}