refactor(proto): consolidate tests

This commit is contained in:
Nils Andreas Svee committed 2026-10-06 17:02:54 +07:00
1 parent 4c941a7d78
commit 5bc27ef894
13 files changed
+1000 -1802

No files matched your search

-73
View File
@@ -96,76 +96,3 @@ pub fn verify_packet_hmac(key: &[u8], packet: &[u8]) -> Result<()> {
pub(crate) fn hmac_offset() -> usize {
HEADER_SIZE
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
flags::{FLAG_HMAC, FLAG_OPEN},
MAGIC,
};
fn reference_copy_zero_hmac(
key: &[u8],
packet: &[u8],
hmac_offset: usize,
) -> Result<[u8; HMAC_SIZE]> {
if packet.len().saturating_sub(hmac_offset) < HMAC_SIZE {
return Err(ProtoError::InvalidHmacOffset);
}
let mut copy = packet.to_vec();
copy[hmac_offset..hmac_offset + HMAC_SIZE].fill(0);
let mut mac = HmacMd5::new_from_slice(key).expect("HMAC accepts keys of any size");
mac.update(&copy);
let bytes = mac.finalize().into_bytes();
let mut out = [0u8; HMAC_SIZE];
out.copy_from_slice(&bytes);
Ok(out)
}
fn packet_with_hmac_field(hmac_bytes: [u8; HMAC_SIZE]) -> Vec<u8> {
let mut packet = Vec::new();
packet.extend_from_slice(&MAGIC);
packet.push(FLAG_OPEN | FLAG_HMAC);
packet.extend_from_slice(&hmac_bytes);
packet.extend_from_slice(&[1, 2, 3, 4]);
packet
}
#[test]
fn compute_and_verify_hmac() {
let mut packet = packet_with_hmac_field([0; HMAC_SIZE]);
compute_hmac_in_place(b"testkey", &mut packet, hmac_offset()).unwrap();
verify_hmac(b"testkey", &packet, hmac_offset()).unwrap();
assert_eq!(
verify_hmac(b"wrong", &packet, hmac_offset()),
Err(ProtoError::BadHmac)
);
}
#[test]
fn compute_hmac_matches_copy_and_zero_semantics() {
let mut packet = packet_with_hmac_field([0xa5; HMAC_SIZE]);
packet.extend_from_slice(&[5, 6, 7, 8, 9]);
let digest = compute_hmac(b"testkey", &packet, hmac_offset()).unwrap();
let reference = reference_copy_zero_hmac(b"testkey", &packet, hmac_offset()).unwrap();
assert_eq!(digest, reference);
}
#[test]
fn verify_hmac_rejects_modified_authenticated_byte() {
let mut packet = packet_with_hmac_field([0; HMAC_SIZE]);
compute_hmac_in_place(b"testkey", &mut packet, hmac_offset()).unwrap();
verify_hmac(b"testkey", &packet, hmac_offset()).unwrap();
packet[hmac_offset() + HMAC_SIZE + 1] ^= 0x01;
assert_eq!(
verify_hmac(b"testkey", &packet, hmac_offset()),
Err(ProtoError::BadHmac)
);
}
}