use super::super::super::support::vv;
use crate::metis::{DecodeError, VersionVector};
#[test]
fn test_vv_to_bytes_layout() {
let frame = vv(&[(1, 1), (2, 5)]).to_bytes();
assert_eq!(
frame,
[
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, ]
);
assert_eq!(VersionVector::new().to_bytes(), [0x01u8, 0, 0, 0, 0]);
}
#[test]
fn test_vv_from_bytes_rejects_unknown_version() {
let mut frame = vv(&[(1, 1)]).to_bytes();
frame[0] = 0x02;
assert_eq!(
VersionVector::from_bytes(&frame),
Err(DecodeError::UnknownVersion(0x02))
);
}
#[test]
fn test_vv_from_bytes_rejects_bad_length() {
let mut trailing = vv(&[(1, 1)]).to_bytes();
trailing.push(0xFF);
assert!(matches!(
VersionVector::from_bytes(&trailing),
Err(DecodeError::UnexpectedLength { .. })
));
assert!(matches!(
VersionVector::from_bytes(&[0x01, 0x00]),
Err(DecodeError::UnexpectedLength { .. })
));
assert!(matches!(
VersionVector::from_bytes(&[0x01, 0xFF, 0xFF, 0xFF, 0xFF]),
Err(DecodeError::UnexpectedLength { .. })
));
}
#[test]
fn test_vv_from_bytes_rejects_zero_counter() {
let frame = [
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ];
assert_eq!(
VersionVector::from_bytes(&frame),
Err(DecodeError::ZeroCounter { station: 7 })
);
}
#[test]
fn test_vv_from_bytes_rejects_unsorted_stations() {
let unsorted = [
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, ];
assert_eq!(
VersionVector::from_bytes(&unsorted),
Err(DecodeError::NonAscendingStations {
previous: 5,
found: 3
})
);
let duplicate = [
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, ];
assert_eq!(
VersionVector::from_bytes(&duplicate),
Err(DecodeError::NonAscendingStations {
previous: 4,
found: 4
})
);
}