const NIA_PREFERENCE: &[(u8, u8)] = &[
(0x20, 0x02), (0x40, 0x01), (0x10, 0x03), ];
const NEA_PREFERENCE: &[(u8, u8)] = &[
(0x20, 0x02), (0x40, 0x01), (0x10, 0x03), (0x80, 0x00), ];
pub fn select_integrity_algo(nia_capability: u8) -> Option<u8> {
for &(mask, algo) in NIA_PREFERENCE {
if nia_capability & mask != 0 {
return Some(algo);
}
}
None
}
pub fn select_ciphering_algo(nea_capability: u8) -> Option<u8> {
for &(mask, algo) in NEA_PREFERENCE {
if nea_capability & mask != 0 {
return Some(algo);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn integrity_prefers_nia2() {
assert_eq!(select_integrity_algo(0xF0), Some(0x02));
}
#[test]
fn integrity_nia1_when_no_nia2() {
assert_eq!(select_integrity_algo(0xD0), Some(0x01)); }
#[test]
fn integrity_nia3_when_only_nia3_nia0() {
assert_eq!(select_integrity_algo(0x90), Some(0x03)); }
#[test]
fn integrity_rejects_nia0_only() {
assert_eq!(select_integrity_algo(0x80), None);
}
#[test]
fn integrity_fallback_nia2() {
assert_eq!(select_integrity_algo(0x00), None);
}
#[test]
fn ciphering_prefers_nea2() {
assert_eq!(select_ciphering_algo(0xF0), Some(0x02));
}
#[test]
fn ciphering_nea1_when_no_nea2() {
assert_eq!(select_ciphering_algo(0xC0), Some(0x01)); }
#[test]
fn ciphering_nea3_when_only_nea3() {
assert_eq!(select_ciphering_algo(0x10), Some(0x03));
}
#[test]
fn ciphering_fallback_nea0() {
assert_eq!(select_ciphering_algo(0x80), Some(0x00));
}
#[test]
fn ciphering_no_bits_nea0() {
assert_eq!(select_ciphering_algo(0x00), None);
}
}