refactor(proto): consolidate tests
This commit is contained in:
13 files changed
+1000
-1802
No files matched your search
@@ -136,101 +136,3 @@ fn with_hmac_flag(flags: u8, authenticated: bool) -> u8 {
|
||||
flags & !FLAG_HMAC
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
flags::{FLAG_OPEN, FLAG_REPLY},
|
||||
MAGIC,
|
||||
};
|
||||
|
||||
const KEY: &[u8] = b"testkey";
|
||||
const OPEN_REQUEST_RULES: &[FlagRule] =
|
||||
&[FlagRule::Require(FLAG_OPEN), FlagRule::Reject(FLAG_REPLY)];
|
||||
|
||||
fn header(flags: u8) -> Vec<u8> {
|
||||
let mut packet = MAGIC.to_vec();
|
||||
packet.push(flags);
|
||||
packet
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rejects_bad_magic_and_reserved_flags() {
|
||||
assert_eq!(
|
||||
decode(&[0, MAGIC[1], MAGIC[2], FLAG_OPEN], None, &[]),
|
||||
Err(ProtoError::BadMagic)
|
||||
);
|
||||
assert_eq!(
|
||||
decode(&header(FLAG_OPEN | 0x10), None, &[]),
|
||||
Err(ProtoError::ReservedFlags(0x10))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_enforces_required_and_rejected_flags() {
|
||||
assert_eq!(
|
||||
decode(&header(0), None, OPEN_REQUEST_RULES),
|
||||
Err(ProtoError::MissingFlag(FLAG_OPEN))
|
||||
);
|
||||
assert_eq!(
|
||||
decode(&header(FLAG_OPEN | FLAG_REPLY), None, OPEN_REQUEST_RULES),
|
||||
Err(ProtoError::UnexpectedFlag(FLAG_REPLY))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rejects_hmac_presence_mismatch() {
|
||||
let mut authenticated = header(FLAG_OPEN | FLAG_HMAC);
|
||||
authenticated.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
assert_eq!(
|
||||
decode(&authenticated, None, OPEN_REQUEST_RULES),
|
||||
Err(ProtoError::HmacPresenceMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
decode(&header(FLAG_OPEN), Some(KEY), OPEN_REQUEST_RULES),
|
||||
Err(ProtoError::HmacPresenceMismatch)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structural_decode_reports_hmac_presence_without_a_key() {
|
||||
let mut authenticated = header(FLAG_OPEN | FLAG_HMAC);
|
||||
authenticated.extend_from_slice(&[0xff; HMAC_SIZE]);
|
||||
|
||||
let envelope = decode_structural(&authenticated).unwrap();
|
||||
assert!(envelope.hmac_present);
|
||||
assert_eq!(envelope.body_offset, HEADER_SIZE + HMAC_SIZE);
|
||||
|
||||
let envelope = decode_structural(&header(FLAG_OPEN)).unwrap();
|
||||
assert!(!envelope.hmac_present);
|
||||
assert_eq!(envelope.body_offset, HEADER_SIZE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_rejects_bad_hmac() {
|
||||
let mut packet = begin_checked(FLAG_OPEN, Some(KEY), OPEN_REQUEST_RULES, 22).unwrap();
|
||||
packet.extend_from_slice(&[1, 2]);
|
||||
let mut packet = finish(packet, Some(KEY)).unwrap();
|
||||
*packet.last_mut().unwrap() ^= 0x80;
|
||||
|
||||
decode(&packet, Some(KEY), OPEN_REQUEST_RULES).unwrap();
|
||||
assert_eq!(verify(&packet, Some(KEY)), Err(ProtoError::BadHmac));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoder_key_authoritatively_normalizes_hmac_flag() {
|
||||
let plain =
|
||||
begin_checked(FLAG_OPEN | FLAG_HMAC, None, OPEN_REQUEST_RULES, HEADER_SIZE).unwrap();
|
||||
assert_eq!(plain[3], FLAG_OPEN);
|
||||
|
||||
let authenticated = begin_checked(
|
||||
FLAG_OPEN,
|
||||
Some(KEY),
|
||||
OPEN_REQUEST_RULES,
|
||||
HEADER_SIZE + HMAC_SIZE,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(authenticated[3], FLAG_OPEN | FLAG_HMAC);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user