refactor(proto): consolidate tests
This commit is contained in:
13 files changed
+1000
-1802
No files matched your search
@@ -304,172 +304,3 @@ fn read_i64(packet: &[u8], pos: &mut usize) -> i64 {
|
||||
*pos += TIMESTAMP_SIZE;
|
||||
value
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
hmac,
|
||||
params::{Clock, ReceivedStats, StampAt},
|
||||
write_header, FLAG_HMAC, HMAC_SIZE, RECV_COUNT_SIZE, RECV_WINDOW_SIZE,
|
||||
};
|
||||
|
||||
fn default_params() -> Params {
|
||||
Params {
|
||||
received_stats: ReceivedStats::Both,
|
||||
stamp_at: StampAt::Both,
|
||||
clock: Clock::Both,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn params_with_payload_space(payload_space: usize) -> Params {
|
||||
let header_len = PacketLayout::echo(false, &Params::default()).header_len();
|
||||
Params {
|
||||
length: (header_len + payload_space) as i64,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-fills every negotiated reply field, matching what a peer that
|
||||
/// reports no optional values would put on the wire.
|
||||
fn push_zeroed_layout_tail(layout: PacketLayout, out: &mut Vec<u8>) {
|
||||
if layout.recv_count {
|
||||
out.extend_from_slice(&[0; RECV_COUNT_SIZE]);
|
||||
}
|
||||
if layout.recv_window {
|
||||
out.extend_from_slice(&[0; RECV_WINDOW_SIZE]);
|
||||
}
|
||||
for _ in 0..layout.timestamp_count() {
|
||||
out.extend_from_slice(&[0; TIMESTAMP_SIZE]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_decodes_default_fields() {
|
||||
let packet = [
|
||||
0x14, 0xa7, 0x5b, 0x02, 0x13, 0x52, 0x71, 0x87, 0xab, 0xb6, 0x96, 0x78, 0x02, 0x00,
|
||||
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0xb8, 0x1a, 0x33, 0x0c, 0x86, 0x6d, 0xaa, 0x18, 0xde, 0x26, 0x35, 0x95, 0x00, 0x00,
|
||||
0x00, 0x00, 0x80, 0x4d, 0x33, 0x0c, 0x86, 0x6d, 0xaa, 0x18, 0xb2, 0x57, 0x35, 0x95,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let reply = decode_echo_reply(&packet, &default_params(), None).unwrap();
|
||||
assert_eq!(reply.token, 0x7896_b6ab_8771_5213);
|
||||
assert_eq!(reply.sequence, 2);
|
||||
assert_eq!(reply.recv_count, Some(3));
|
||||
assert_eq!(reply.recv_window, Some(7));
|
||||
assert!(reply.timestamps.recv_wall.is_some());
|
||||
assert!(reply.timestamps.midpoint_wall.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_decodes_exact_negotiated_length_with_payload() {
|
||||
let params = params_with_payload_space(4);
|
||||
let mut packet = Vec::new();
|
||||
write_header(&mut packet, FLAG_REPLY);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
packet.extend_from_slice(&[1, 2, 3, 4]);
|
||||
|
||||
let reply = decode_echo_reply(&packet, ¶ms, None).unwrap();
|
||||
assert_eq!(reply.payload, vec![1, 2, 3, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_rejects_shorter_than_negotiated_length() {
|
||||
let params = params_with_payload_space(4);
|
||||
let mut packet = Vec::new();
|
||||
write_header(&mut packet, FLAG_REPLY);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, None),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: 20,
|
||||
actual: 16,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_reply_rejects_longer_than_negotiated_length() {
|
||||
let params = Params::default();
|
||||
let mut packet = Vec::new();
|
||||
write_header(&mut packet, FLAG_REPLY);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
packet.push(0);
|
||||
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, None),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: 16,
|
||||
actual: 17,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmac_echo_reply_decodes_default_fields_after_hmac() {
|
||||
let params = default_params();
|
||||
let layout = PacketLayout::echo(true, ¶ms);
|
||||
let mut packet = Vec::with_capacity(layout.header_len());
|
||||
write_header(&mut packet, FLAG_REPLY | FLAG_HMAC);
|
||||
packet.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
push_zeroed_layout_tail(layout, &mut packet);
|
||||
hmac::compute_hmac_in_place(b"testkey", &mut packet, hmac::hmac_offset()).unwrap();
|
||||
|
||||
assert_eq!(packet.len(), 76);
|
||||
let reply = decode_echo_reply(&packet, ¶ms, Some(b"testkey")).unwrap();
|
||||
assert_eq!(reply.token, 0x7896_b6ab_8771_5213);
|
||||
assert_eq!(reply.sequence, 2);
|
||||
assert_eq!(reply.recv_count, Some(0));
|
||||
assert_eq!(reply.recv_window, Some(0));
|
||||
assert_eq!(reply.payload.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmac_echo_reply_decodes_exact_negotiated_length_with_payload() {
|
||||
let mut params = Params {
|
||||
length: 48,
|
||||
..Params::default()
|
||||
};
|
||||
params.received_stats = ReceivedStats::Both;
|
||||
let layout = PacketLayout::echo(true, ¶ms);
|
||||
let mut packet = Vec::with_capacity(echo_packet_len(true, ¶ms).unwrap());
|
||||
write_header(&mut packet, FLAG_REPLY | FLAG_HMAC);
|
||||
packet.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
push_zeroed_layout_tail(layout, &mut packet);
|
||||
packet.extend_from_slice(&[1, 2, 3, 4]);
|
||||
hmac::compute_hmac_in_place(b"testkey", &mut packet, hmac::hmac_offset()).unwrap();
|
||||
|
||||
let reply = decode_echo_reply(&packet, ¶ms, Some(b"testkey")).unwrap();
|
||||
assert_eq!(reply.payload, vec![1, 2, 3, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmac_echo_reply_rejects_length_mismatch_before_hmac_verification() {
|
||||
let params = Params::default();
|
||||
let mut packet = Vec::new();
|
||||
write_header(&mut packet, FLAG_REPLY | FLAG_HMAC);
|
||||
packet.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&2_u32.to_le_bytes());
|
||||
hmac::compute_hmac_in_place(b"testkey", &mut packet, hmac::hmac_offset()).unwrap();
|
||||
packet.push(0);
|
||||
|
||||
assert_eq!(
|
||||
decode_echo_reply(&packet, ¶ms, Some(b"testkey")),
|
||||
Err(ProtoError::PacketLengthMismatch {
|
||||
expected: 32,
|
||||
actual: 33,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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(©);
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -162,194 +162,3 @@ pub fn echo_packet_len(hmac: bool, params: &Params) -> Result<usize> {
|
||||
};
|
||||
Ok(header_len.max(requested))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::params::{Clock, ReceivedStats, StampAt};
|
||||
|
||||
fn params(stats: ReceivedStats, stamp_at: StampAt, clock: Clock) -> Params {
|
||||
Params {
|
||||
received_stats: stats,
|
||||
stamp_at,
|
||||
clock,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn expected_optional_len(stats: ReceivedStats, stamp_at: StampAt, clock: Clock) -> usize {
|
||||
let stats_len = match stats {
|
||||
ReceivedStats::None => 0,
|
||||
ReceivedStats::Count => RECV_COUNT_SIZE,
|
||||
ReceivedStats::Window => RECV_WINDOW_SIZE,
|
||||
ReceivedStats::Both => RECV_COUNT_SIZE + RECV_WINDOW_SIZE,
|
||||
};
|
||||
let clock_count = match clock {
|
||||
Clock::Unspecified => 0,
|
||||
Clock::Wall | Clock::Monotonic => 1,
|
||||
Clock::Both => 2,
|
||||
};
|
||||
let timestamp_groups = match stamp_at {
|
||||
StampAt::None => 0,
|
||||
StampAt::Send | StampAt::Receive | StampAt::Midpoint => 1,
|
||||
StampAt::Both => 2,
|
||||
};
|
||||
stats_len + timestamp_groups * clock_count * TIMESTAMP_SIZE
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verified_layout_lengths() {
|
||||
assert_eq!(PacketLayout::open_request(false).header_len(), 4);
|
||||
assert_eq!(PacketLayout::open_request(true).header_len(), 20);
|
||||
assert_eq!(PacketLayout::open_reply(false).header_len(), 12);
|
||||
assert_eq!(PacketLayout::open_reply(true).header_len(), 28);
|
||||
assert_eq!(PacketLayout::close_request(false).header_len(), 12);
|
||||
assert_eq!(PacketLayout::close_request(true).header_len(), 28);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::None, StampAt::None, Clock::Both)
|
||||
),
|
||||
16
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::Count, StampAt::Send, Clock::Wall)
|
||||
),
|
||||
28
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::Window, StampAt::Receive, Clock::Monotonic)
|
||||
),
|
||||
32
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::Both, StampAt::Midpoint, Clock::Both)
|
||||
),
|
||||
44
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
false,
|
||||
¶ms(ReceivedStats::Both, StampAt::Both, Clock::Both)
|
||||
),
|
||||
60
|
||||
);
|
||||
assert_eq!(
|
||||
echo_header_len(
|
||||
true,
|
||||
¶ms(ReceivedStats::Both, StampAt::Both, Clock::Both)
|
||||
),
|
||||
76
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layout_matrix_matches_stats_timestamps_clock_and_hmac_rules() {
|
||||
for stats in [
|
||||
ReceivedStats::None,
|
||||
ReceivedStats::Count,
|
||||
ReceivedStats::Window,
|
||||
ReceivedStats::Both,
|
||||
] {
|
||||
for stamp_at in [
|
||||
StampAt::None,
|
||||
StampAt::Send,
|
||||
StampAt::Receive,
|
||||
StampAt::Both,
|
||||
StampAt::Midpoint,
|
||||
] {
|
||||
for clock in [Clock::Wall, Clock::Monotonic, Clock::Both] {
|
||||
for hmac in [false, true] {
|
||||
let params = params(stats, stamp_at, clock);
|
||||
let layout = PacketLayout::echo(hmac, ¶ms);
|
||||
let expected_len = HEADER_SIZE
|
||||
+ TOKEN_SIZE
|
||||
+ SEQ_SIZE
|
||||
+ if hmac { HMAC_SIZE } else { 0 }
|
||||
+ expected_optional_len(stats, stamp_at, clock);
|
||||
|
||||
assert_eq!(
|
||||
layout.header_len(),
|
||||
expected_len,
|
||||
"unexpected length for stats={stats:?} stamp_at={stamp_at:?} clock={clock:?} hmac={hmac}"
|
||||
);
|
||||
assert_eq!(layout.recv_count, stats.has_count());
|
||||
assert_eq!(layout.recv_window, stats.has_window());
|
||||
assert_eq!(
|
||||
layout.recv_wall,
|
||||
matches!(stamp_at, StampAt::Receive | StampAt::Both)
|
||||
&& clock.has_wall()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.recv_mono,
|
||||
matches!(stamp_at, StampAt::Receive | StampAt::Both)
|
||||
&& clock.has_mono()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.midpoint_wall,
|
||||
matches!(stamp_at, StampAt::Midpoint) && clock.has_wall()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.midpoint_mono,
|
||||
matches!(stamp_at, StampAt::Midpoint) && clock.has_mono()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.send_wall,
|
||||
matches!(stamp_at, StampAt::Send | StampAt::Both) && clock.has_wall()
|
||||
);
|
||||
assert_eq!(
|
||||
layout.send_mono,
|
||||
matches!(stamp_at, StampAt::Send | StampAt::Both) && clock.has_mono()
|
||||
);
|
||||
|
||||
if hmac {
|
||||
assert_eq!(
|
||||
echo_header_len(true, ¶ms) - echo_header_len(false, ¶ms),
|
||||
HMAC_SIZE
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiated_length_is_floored_at_the_required_field_block() {
|
||||
// The 60-byte header of this layout is the floor. A negative negotiated
|
||||
// length is not an error: it asks for nothing beyond the mandatory
|
||||
// fields, exactly as zero does.
|
||||
let mut p = params(ReceivedStats::Both, StampAt::Both, Clock::Both);
|
||||
for (length, expected) in [(-4096, 60), (-1, 60), (0, 60), (20, 60), (92, 92)] {
|
||||
p.length = length;
|
||||
assert_eq!(
|
||||
echo_packet_len(false, &p),
|
||||
Ok(expected),
|
||||
"unexpected packet length for negotiated length {length}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A positive length wider than `usize` must stay an error rather than
|
||||
/// becoming a saturated buffer size. Only reachable where `usize` is
|
||||
/// narrower than `i64`; on a 64-bit target every positive `i64` converts.
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[test]
|
||||
fn a_length_wider_than_usize_is_rejected_rather_than_saturated() {
|
||||
let mut p = params(ReceivedStats::Both, StampAt::Both, Clock::Both);
|
||||
p.length = 5_000_000_000;
|
||||
|
||||
assert_eq!(
|
||||
echo_packet_len(false, &p),
|
||||
Err(ProtoError::PacketLengthUnrepresentable {
|
||||
length: 5_000_000_000
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -98,53 +98,3 @@ pub fn decode_open_reply(packet: &[u8], hmac_key: Option<&[u8]>) -> Result<OpenR
|
||||
params: Params::decode(&packet[pos..])?,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
hmac,
|
||||
params::{Clock, ReceivedStats, StampAt},
|
||||
FLAG_HMAC, HMAC_SIZE,
|
||||
};
|
||||
|
||||
fn default_params() -> Params {
|
||||
Params {
|
||||
protocol_version: 1,
|
||||
duration_ns: 3_000_000_000,
|
||||
interval_ns: 1_000_000_000,
|
||||
received_stats: ReceivedStats::Both,
|
||||
stamp_at: StampAt::Both,
|
||||
clock: Clock::Both,
|
||||
..Params::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_reply_decodes_token() {
|
||||
let mut packet = vec![0x14, 0xa7, 0x5b, 0x03];
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&default_params().encode());
|
||||
|
||||
let reply = decode_open_reply(&packet, None).unwrap();
|
||||
assert_eq!(reply.token, 0x7896_b6ab_8771_5213);
|
||||
assert_eq!(reply.params, default_params());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hmac_open_reply_decodes_token_after_hmac_field() {
|
||||
let mut packet = vec![0x14, 0xa7, 0x5b, FLAG_OPEN | FLAG_REPLY | FLAG_HMAC];
|
||||
packet.extend_from_slice(&[0; HMAC_SIZE]);
|
||||
packet.extend_from_slice(&0x7896_b6ab_8771_5213u64.to_le_bytes());
|
||||
packet.extend_from_slice(&default_params().encode());
|
||||
hmac::compute_hmac_in_place(b"testkey", &mut packet, hmac::hmac_offset()).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
packet.len(),
|
||||
4 + HMAC_SIZE + TOKEN_SIZE + default_params().encode().len()
|
||||
);
|
||||
let reply = decode_open_reply(&packet, Some(b"testkey")).unwrap();
|
||||
assert_eq!(reply.token, 0x7896_b6ab_8771_5213);
|
||||
assert_eq!(reply.params, default_params());
|
||||
}
|
||||
}
|
||||
@@ -368,773 +368,3 @@ fn read_int(input: &[u8], pos: &mut usize) -> Result<i64> {
|
||||
*pos += used;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn assert_round_trip(params: Params) {
|
||||
assert_eq!(Params::decode(¶ms.encode()), Ok(params));
|
||||
}
|
||||
|
||||
fn encode_server_fill_value(value: &[u8]) -> Vec<u8> {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(9, &mut encoded);
|
||||
varint::encode_uvarint(value.len() as u64, &mut encoded);
|
||||
encoded.extend_from_slice(value);
|
||||
encoded
|
||||
}
|
||||
|
||||
fn encode_int(tag: u64, value: i64) -> Vec<u8> {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(tag, &mut encoded);
|
||||
varint::encode_varint(value, &mut encoded);
|
||||
encoded
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_round_trip() {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
duration_ns: 3_000_000_000,
|
||||
interval_ns: 1_000_000_000,
|
||||
length: 1472,
|
||||
received_stats: ReceivedStats::Both,
|
||||
stamp_at: StampAt::Both,
|
||||
clock: Clock::Both,
|
||||
dscp: 184,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "pattern:abc".to_owned(),
|
||||
}),
|
||||
};
|
||||
assert_round_trip(params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_round_trip_negotiated_option_modes() {
|
||||
for received_stats in [
|
||||
ReceivedStats::None,
|
||||
ReceivedStats::Count,
|
||||
ReceivedStats::Window,
|
||||
ReceivedStats::Both,
|
||||
] {
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
received_stats,
|
||||
..Params::default()
|
||||
});
|
||||
}
|
||||
|
||||
for stamp_at in [
|
||||
StampAt::None,
|
||||
StampAt::Send,
|
||||
StampAt::Receive,
|
||||
StampAt::Both,
|
||||
StampAt::Midpoint,
|
||||
] {
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
stamp_at,
|
||||
clock: Clock::Both,
|
||||
..Params::default()
|
||||
});
|
||||
}
|
||||
|
||||
for clock in [Clock::Wall, Clock::Monotonic, Clock::Both] {
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
stamp_at: StampAt::Both,
|
||||
clock,
|
||||
..Params::default()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_round_trip_dscp_values_are_encoded_without_shifting() {
|
||||
// `Params::dscp` is a raw wire byte; encode/decode carry whatever
|
||||
// value is set without interpreting or shifting it, regardless of
|
||||
// whether it happens to look like a codepoint (46) or a raw TOS byte
|
||||
// (184).
|
||||
for dscp in [0, 46, 63, 64, 184, -1] {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
dscp,
|
||||
..Params::default()
|
||||
};
|
||||
assert_round_trip(params);
|
||||
}
|
||||
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
dscp: 46,
|
||||
..Params::default()
|
||||
};
|
||||
let encoded = params.encode();
|
||||
assert!(
|
||||
encoded.windows(2).any(|bytes| bytes == [8, 92]),
|
||||
"dscp value 46 must be encoded as param value 46"
|
||||
);
|
||||
assert!(
|
||||
!encoded.windows(3).any(|bytes| bytes == [8, 0xf0, 0x02]),
|
||||
"Params::encode must not shift dscp 46 to 184; any codepoint shift is a caller concern"
|
||||
);
|
||||
assert_eq!(Params::decode(&encoded).unwrap().dscp, 46);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_absent_short_and_max_length_round_trip() {
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
server_fill: None,
|
||||
..Params::default()
|
||||
});
|
||||
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "rand".to_owned(),
|
||||
}),
|
||||
..Params::default()
|
||||
});
|
||||
|
||||
assert_round_trip(Params {
|
||||
protocol_version: 1,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "0123456789abcdef0123456789abcdef".to_owned(),
|
||||
}),
|
||||
..Params::default()
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_decode_accepts_max_length() {
|
||||
let value = b"0123456789abcdef0123456789abcdef";
|
||||
let params = Params::decode(&encode_server_fill_value(value)).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
params.server_fill,
|
||||
Some(ServerFill {
|
||||
value: "0123456789abcdef0123456789abcdef".to_owned(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_decode_rejects_oversized_length() {
|
||||
let value = b"0123456789abcdef0123456789abcdefx";
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encode_server_fill_value(value)),
|
||||
Err(ProtoError::ParameterLengthTooLarge { tag: 9, length: 33 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_fill_tag_and_length_are_encoded_before_utf8_bytes() {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "rand".to_owned(),
|
||||
}),
|
||||
..Params::default()
|
||||
};
|
||||
let encoded = params.encode();
|
||||
assert!(encoded
|
||||
.windows(6)
|
||||
.any(|bytes| bytes == [9, 4, b'r', b'a', b'n', b'd']));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_tags_are_ignored() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(99, &mut encoded);
|
||||
varint::encode_varint(123, &mut encoded);
|
||||
varint::encode_uvarint(1, &mut encoded);
|
||||
varint::encode_varint(1, &mut encoded);
|
||||
|
||||
let params = Params::decode(&encoded).unwrap();
|
||||
assert_eq!(params.protocol_version, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_received_stats_value_is_rejected() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(5, &mut encoded);
|
||||
varint::encode_varint(4, &mut encoded);
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encoded),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "ReceivedStats",
|
||||
value: 4,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_timestamp_value_is_rejected() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(6, &mut encoded);
|
||||
varint::encode_varint(5, &mut encoded);
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encoded),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "StampAt",
|
||||
value: 5,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_clock_zero_is_rejected() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(7, &mut encoded);
|
||||
varint::encode_varint(0, &mut encoded);
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encoded),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "Clock",
|
||||
value: 0,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
Params::decode_with_presence(&encoded),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "Clock",
|
||||
value: 0,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_clock_values_one_to_three_are_accepted() {
|
||||
for (value, expected) in [(1, Clock::Wall), (2, Clock::Monotonic), (3, Clock::Both)] {
|
||||
let decoded = Params::decode_with_presence(&encode_int(7, value)).unwrap();
|
||||
assert_eq!(decoded.params.clock, expected);
|
||||
assert!(decoded.presence.clock);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_payload_decodes_to_wire_defaults_with_nothing_present() {
|
||||
let decoded = Params::decode_with_presence(&[]).unwrap();
|
||||
|
||||
assert_eq!(decoded.params, Params::default());
|
||||
assert_eq!(decoded.params.protocol_version, 0);
|
||||
assert_eq!(decoded.params.duration_ns, 0);
|
||||
assert_eq!(decoded.params.interval_ns, 0);
|
||||
assert_eq!(decoded.params.length, 0);
|
||||
assert_eq!(decoded.params.dscp, 0);
|
||||
assert_eq!(decoded.params.received_stats, ReceivedStats::None);
|
||||
assert_eq!(decoded.params.stamp_at, StampAt::None);
|
||||
// The wire default clock is zero, not `Both`.
|
||||
assert_eq!(decoded.params.clock, Clock::Unspecified);
|
||||
assert!(!decoded.params.clock.has_wall());
|
||||
assert!(!decoded.params.clock.has_mono());
|
||||
assert_eq!(decoded.params.server_fill, None);
|
||||
|
||||
assert_eq!(decoded.presence, ParamPresence::default());
|
||||
assert_eq!(
|
||||
decoded.presence,
|
||||
ParamPresence {
|
||||
protocol_version: false,
|
||||
duration_ns: false,
|
||||
interval_ns: false,
|
||||
length: false,
|
||||
received_stats: false,
|
||||
stamp_at: false,
|
||||
clock: false,
|
||||
dscp: false,
|
||||
server_fill: false,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absent_and_explicitly_zero_parameters_share_a_value_but_not_a_presence() {
|
||||
// Duration and Interval are the pair a server must tell apart: absent is
|
||||
// accepted, explicitly zero is not.
|
||||
let absent = Params::decode_with_presence(&encode_int(1, 1)).unwrap();
|
||||
assert_eq!(absent.params.duration_ns, 0);
|
||||
assert!(!absent.presence.duration_ns);
|
||||
assert_eq!(absent.params.interval_ns, 0);
|
||||
assert!(!absent.presence.interval_ns);
|
||||
assert_eq!(absent.params.clock, Clock::Unspecified);
|
||||
assert!(!absent.presence.clock);
|
||||
|
||||
let explicit_duration = Params::decode_with_presence(&encode_int(2, 0)).unwrap();
|
||||
assert_eq!(explicit_duration.params.duration_ns, 0);
|
||||
assert!(explicit_duration.presence.duration_ns);
|
||||
|
||||
let explicit_interval = Params::decode_with_presence(&encode_int(3, 0)).unwrap();
|
||||
assert_eq!(explicit_interval.params.interval_ns, 0);
|
||||
assert!(explicit_interval.presence.interval_ns);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_known_tag_reports_its_own_presence() {
|
||||
let mut encoded = Vec::new();
|
||||
for (tag, value) in [
|
||||
(1, 1),
|
||||
(2, 5),
|
||||
(3, 6),
|
||||
(4, 7),
|
||||
(5, 3),
|
||||
(6, 4),
|
||||
(7, 3),
|
||||
(8, 184),
|
||||
] {
|
||||
encoded.extend_from_slice(&encode_int(tag, value));
|
||||
}
|
||||
encoded.extend_from_slice(&encode_server_fill_value(b"rand"));
|
||||
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
assert_eq!(
|
||||
decoded.presence,
|
||||
ParamPresence {
|
||||
protocol_version: true,
|
||||
duration_ns: true,
|
||||
interval_ns: true,
|
||||
length: true,
|
||||
received_stats: true,
|
||||
stamp_at: true,
|
||||
clock: true,
|
||||
dscp: true,
|
||||
server_fill: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unspecified_clock_is_encoded_by_omission() {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
clock: Clock::Unspecified,
|
||||
..Params::default()
|
||||
};
|
||||
let encoded = params.encode();
|
||||
|
||||
assert_eq!(encoded, encode_int(1, 1));
|
||||
assert!(
|
||||
!encoded.windows(2).any(|bytes| bytes[0] == 7),
|
||||
"an unspecified clock must omit the tag rather than encode an explicit zero"
|
||||
);
|
||||
assert_round_trip(params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_and_decode_with_presence_agree() {
|
||||
let mut payloads = vec![
|
||||
Vec::new(),
|
||||
encode_int(1, 1),
|
||||
encode_int(2, 0),
|
||||
encode_server_fill_value(b"rand"),
|
||||
];
|
||||
payloads.push(
|
||||
Params {
|
||||
protocol_version: 1,
|
||||
duration_ns: 3_000_000_000,
|
||||
interval_ns: 1_000_000_000,
|
||||
length: 1472,
|
||||
received_stats: ReceivedStats::Both,
|
||||
stamp_at: StampAt::Both,
|
||||
clock: Clock::Both,
|
||||
dscp: 184,
|
||||
server_fill: Some(ServerFill {
|
||||
value: "rand".to_owned(),
|
||||
}),
|
||||
}
|
||||
.encode(),
|
||||
);
|
||||
// Malformed payloads must fail identically through both entry points.
|
||||
payloads.push(vec![1, 0x80]);
|
||||
payloads.push(encode_int(7, 0));
|
||||
|
||||
for payload in payloads {
|
||||
assert_eq!(
|
||||
Params::decode(&payload),
|
||||
Params::decode_with_presence(&payload).map(|decoded| decoded.params),
|
||||
"decode and decode_with_presence disagreed on {payload:02x?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_server_fill_is_rejected() {
|
||||
let encoded = [9, 4, b'a', b'b'];
|
||||
assert_eq!(Params::decode(&encoded), Err(ProtoError::MalformedParams));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_utf8_server_fill_is_rejected() {
|
||||
let encoded = [9, 1, 0xff];
|
||||
assert_eq!(Params::decode(&encoded), Err(ProtoError::InvalidUtf8));
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[test]
|
||||
fn server_fill_length_too_large_for_usize_is_rejected() {
|
||||
let mut encoded = Vec::new();
|
||||
varint::encode_uvarint(9, &mut encoded);
|
||||
varint::encode_uvarint(u64::from(u32::MAX) + 1, &mut encoded);
|
||||
|
||||
assert_eq!(
|
||||
Params::decode(&encoded),
|
||||
Err(ProtoError::ParameterLengthTooLarge {
|
||||
tag: 9,
|
||||
length: u64::from(u32::MAX) + 1,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_varint_parameter_is_rejected() {
|
||||
let encoded = [1, 0x80];
|
||||
assert_eq!(Params::decode(&encoded), Err(ProtoError::TruncatedVarint));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_unknown_parameter_value_is_rejected() {
|
||||
let encoded = [99];
|
||||
assert_eq!(Params::decode(&encoded), Err(ProtoError::TruncatedVarint));
|
||||
}
|
||||
|
||||
mod properties {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
|
||||
fn received_stats_strategy() -> impl Strategy<Value = ReceivedStats> {
|
||||
prop_oneof![
|
||||
Just(ReceivedStats::None),
|
||||
Just(ReceivedStats::Count),
|
||||
Just(ReceivedStats::Window),
|
||||
Just(ReceivedStats::Both),
|
||||
]
|
||||
}
|
||||
|
||||
fn stamp_at_strategy() -> impl Strategy<Value = StampAt> {
|
||||
prop_oneof![
|
||||
Just(StampAt::None),
|
||||
Just(StampAt::Send),
|
||||
Just(StampAt::Receive),
|
||||
Just(StampAt::Both),
|
||||
Just(StampAt::Midpoint),
|
||||
]
|
||||
}
|
||||
|
||||
/// A `Clock` valid for direct construction, including `Unspecified`
|
||||
/// (wire omission). Never produces an explicit zero on the wire; see
|
||||
/// `explicit_clock_strategy` for that.
|
||||
fn clock_strategy() -> impl Strategy<Value = Clock> {
|
||||
prop_oneof![
|
||||
Just(Clock::Unspecified),
|
||||
Just(Clock::Wall),
|
||||
Just(Clock::Monotonic),
|
||||
Just(Clock::Both),
|
||||
]
|
||||
}
|
||||
|
||||
/// A `Clock` value valid to encode *explicitly* on the wire (tag 7).
|
||||
/// Excludes `Unspecified`/zero, which is only ever reached by omission.
|
||||
fn explicit_clock_value_strategy() -> impl Strategy<Value = i64> {
|
||||
1_i64..=3
|
||||
}
|
||||
|
||||
fn server_fill_strategy() -> impl Strategy<Value = Option<ServerFill>> {
|
||||
prop_oneof![
|
||||
Just(None),
|
||||
proptest::collection::vec(proptest::char::any(), 0..=8).prop_map(|chars| Some(
|
||||
ServerFill {
|
||||
value: chars.into_iter().collect(),
|
||||
}
|
||||
)),
|
||||
]
|
||||
}
|
||||
|
||||
fn params_strategy() -> impl Strategy<Value = Params> {
|
||||
(
|
||||
any::<i64>(),
|
||||
any::<i64>(),
|
||||
any::<i64>(),
|
||||
any::<i64>(),
|
||||
any::<i64>(),
|
||||
received_stats_strategy(),
|
||||
stamp_at_strategy(),
|
||||
clock_strategy(),
|
||||
server_fill_strategy(),
|
||||
)
|
||||
.prop_map(
|
||||
|(
|
||||
protocol_version,
|
||||
duration_ns,
|
||||
interval_ns,
|
||||
length,
|
||||
dscp,
|
||||
received_stats,
|
||||
stamp_at,
|
||||
clock,
|
||||
server_fill,
|
||||
)| Params {
|
||||
protocol_version,
|
||||
duration_ns,
|
||||
interval_ns,
|
||||
length,
|
||||
received_stats,
|
||||
stamp_at,
|
||||
clock,
|
||||
dscp,
|
||||
server_fill,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn encode_tagged_int(tag: u64, value: i64, out: &mut Vec<u8>) {
|
||||
varint::encode_uvarint(tag, out);
|
||||
varint::encode_varint(value, out);
|
||||
}
|
||||
|
||||
fn encode_tagged_server_fill(value: &str, out: &mut Vec<u8>) {
|
||||
varint::encode_uvarint(9, out);
|
||||
varint::encode_uvarint(value.len() as u64, out);
|
||||
out.extend_from_slice(value.as_bytes());
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn valid_params_round_trip(params in params_strategy()) {
|
||||
let encoded = params.encode();
|
||||
let decoded = Params::decode(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded, params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoded_param_presence_matches_omission_rules(params in params_strategy()) {
|
||||
let encoded = params.encode();
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(&decoded.params, ¶ms);
|
||||
|
||||
prop_assert_eq!(decoded.presence.protocol_version, params.protocol_version != 0);
|
||||
prop_assert_eq!(decoded.presence.duration_ns, params.duration_ns != 0);
|
||||
prop_assert_eq!(decoded.presence.interval_ns, params.interval_ns != 0);
|
||||
prop_assert_eq!(decoded.presence.length, params.length != 0);
|
||||
prop_assert_eq!(decoded.presence.dscp, params.dscp != 0);
|
||||
prop_assert_eq!(
|
||||
decoded.presence.received_stats,
|
||||
params.received_stats != ReceivedStats::None
|
||||
);
|
||||
prop_assert_eq!(decoded.presence.stamp_at, params.stamp_at != StampAt::None);
|
||||
prop_assert_eq!(decoded.presence.clock, params.clock != Clock::Unspecified);
|
||||
prop_assert_eq!(decoded.presence.server_fill, params.server_fill.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_protocol_version_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(1, first, &mut encoded);
|
||||
encode_tagged_int(1, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.protocol_version, second);
|
||||
prop_assert!(decoded.presence.protocol_version);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_duration_ns_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(2, first, &mut encoded);
|
||||
encode_tagged_int(2, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.duration_ns, second);
|
||||
prop_assert!(decoded.presence.duration_ns);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_interval_ns_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(3, first, &mut encoded);
|
||||
encode_tagged_int(3, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.interval_ns, second);
|
||||
prop_assert!(decoded.presence.interval_ns);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_length_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(4, first, &mut encoded);
|
||||
encode_tagged_int(4, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.length, second);
|
||||
prop_assert!(decoded.presence.length);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_received_stats_is_last_wins(first in 0_i64..=3, second in 0_i64..=3) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(5, first, &mut encoded);
|
||||
encode_tagged_int(5, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.received_stats, ReceivedStats::try_from(second).unwrap());
|
||||
prop_assert!(decoded.presence.received_stats);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_stamp_at_is_last_wins(first in 0_i64..=4, second in 0_i64..=4) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(6, first, &mut encoded);
|
||||
encode_tagged_int(6, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.stamp_at, StampAt::try_from(second).unwrap());
|
||||
prop_assert!(decoded.presence.stamp_at);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_clock_is_last_wins(
|
||||
first in explicit_clock_value_strategy(),
|
||||
second in explicit_clock_value_strategy(),
|
||||
) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(7, first, &mut encoded);
|
||||
encode_tagged_int(7, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.clock, Clock::try_from(second).unwrap());
|
||||
prop_assert!(decoded.presence.clock);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_dscp_is_last_wins(first: i64, second: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_int(8, first, &mut encoded);
|
||||
encode_tagged_int(8, second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded.params.dscp, second);
|
||||
prop_assert!(decoded.presence.dscp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_server_fill_is_last_wins(
|
||||
first in proptest::collection::vec(proptest::char::any(), 0..=8)
|
||||
.prop_map(|chars| chars.into_iter().collect::<String>()),
|
||||
second in proptest::collection::vec(proptest::char::any(), 0..=8)
|
||||
.prop_map(|chars| chars.into_iter().collect::<String>()),
|
||||
) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_tagged_server_fill(&first, &mut encoded);
|
||||
encode_tagged_server_fill(&second, &mut encoded);
|
||||
let decoded = Params::decode_with_presence(&encoded).unwrap();
|
||||
prop_assert_eq!(
|
||||
decoded.params.server_fill,
|
||||
Some(ServerFill { value: second })
|
||||
);
|
||||
prop_assert!(decoded.presence.server_fill);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_scalar_tag_prefix_does_not_disturb_known_params(
|
||||
params in params_strategy(),
|
||||
unknown_tag in 10_u64..=u16::MAX as u64,
|
||||
unknown_value: u64,
|
||||
) {
|
||||
let mut prefixed = Vec::new();
|
||||
varint::encode_uvarint(unknown_tag, &mut prefixed);
|
||||
varint::encode_uvarint(unknown_value, &mut prefixed);
|
||||
prefixed.extend_from_slice(¶ms.encode());
|
||||
|
||||
let decoded = Params::decode(&prefixed).unwrap();
|
||||
prop_assert_eq!(decoded, params.clone());
|
||||
|
||||
let decoded_with_presence = Params::decode_with_presence(&prefixed).unwrap();
|
||||
let expected_presence = Params::decode_with_presence(¶ms.encode()).unwrap().presence;
|
||||
prop_assert_eq!(decoded_with_presence.presence, expected_presence);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_scalar_tag_suffix_does_not_disturb_known_params(
|
||||
params in params_strategy(),
|
||||
unknown_tag in 10_u64..=u16::MAX as u64,
|
||||
unknown_value: u64,
|
||||
) {
|
||||
let mut suffixed = params.encode();
|
||||
varint::encode_uvarint(unknown_tag, &mut suffixed);
|
||||
varint::encode_uvarint(unknown_value, &mut suffixed);
|
||||
|
||||
let decoded = Params::decode(&suffixed).unwrap();
|
||||
prop_assert_eq!(decoded, params.clone());
|
||||
|
||||
let decoded_with_presence = Params::decode_with_presence(&suffixed).unwrap();
|
||||
let expected_presence = Params::decode_with_presence(¶ms.encode()).unwrap().presence;
|
||||
prop_assert_eq!(decoded_with_presence.presence, expected_presence);
|
||||
}
|
||||
}
|
||||
|
||||
/// Omitted-vs-explicit-zero presence, for the fields where explicit
|
||||
/// zero is valid on the wire. `Clock` is deliberately excluded: an
|
||||
/// explicit zero there is rejected outright, not merely "present with
|
||||
/// the default value" (see `explicit_clock_zero_is_rejected`).
|
||||
#[test]
|
||||
fn omitted_vs_explicit_zero_presence_for_zero_valid_fields() {
|
||||
let scalar_tags = [1_u64, 2, 3, 4, 8];
|
||||
for tag in scalar_tags {
|
||||
let omitted = Params::decode_with_presence(&[]).unwrap();
|
||||
let mut explicit_zero = Vec::new();
|
||||
encode_tagged_int(tag, 0, &mut explicit_zero);
|
||||
let explicit = Params::decode_with_presence(&explicit_zero).unwrap();
|
||||
|
||||
assert_eq!(omitted.params, explicit.params);
|
||||
assert_eq!(explicit.params, Params::default());
|
||||
|
||||
let (omitted_presence, explicit_presence) = match tag {
|
||||
1 => (
|
||||
omitted.presence.protocol_version,
|
||||
explicit.presence.protocol_version,
|
||||
),
|
||||
2 => (omitted.presence.duration_ns, explicit.presence.duration_ns),
|
||||
3 => (omitted.presence.interval_ns, explicit.presence.interval_ns),
|
||||
4 => (omitted.presence.length, explicit.presence.length),
|
||||
8 => (omitted.presence.dscp, explicit.presence.dscp),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert!(!omitted_presence);
|
||||
assert!(explicit_presence);
|
||||
}
|
||||
|
||||
let enum_tags = [5_u64, 6];
|
||||
for tag in enum_tags {
|
||||
let omitted = Params::decode_with_presence(&[]).unwrap();
|
||||
let mut explicit_zero = Vec::new();
|
||||
encode_tagged_int(tag, 0, &mut explicit_zero);
|
||||
let explicit = Params::decode_with_presence(&explicit_zero).unwrap();
|
||||
|
||||
assert_eq!(omitted.params, explicit.params);
|
||||
|
||||
let (omitted_presence, explicit_presence) = match tag {
|
||||
5 => (
|
||||
omitted.presence.received_stats,
|
||||
explicit.presence.received_stats,
|
||||
),
|
||||
6 => (omitted.presence.stamp_at, explicit.presence.stamp_at),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert!(!omitted_presence);
|
||||
assert!(explicit_presence);
|
||||
}
|
||||
|
||||
// `Clock` deliberately breaks the pattern above: explicit zero is
|
||||
// rejected rather than accepted-but-present.
|
||||
let omitted = Params::decode_with_presence(&[]).unwrap();
|
||||
assert_eq!(omitted.params.clock, Clock::Unspecified);
|
||||
assert!(!omitted.presence.clock);
|
||||
let mut explicit_clock_zero = Vec::new();
|
||||
encode_tagged_int(7, 0, &mut explicit_clock_zero);
|
||||
assert_eq!(
|
||||
Params::decode_with_presence(&explicit_clock_zero),
|
||||
Err(ProtoError::InvalidEnum {
|
||||
name: "Clock",
|
||||
value: 0,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,112 +59,3 @@ pub fn decode_varint(input: &[u8]) -> Result<(i64, usize)> {
|
||||
let (value, used) = decode_uvarint(input)?;
|
||||
Ok((zigzag_decode(value), used))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn uvarint_round_trips() {
|
||||
for value in [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
127,
|
||||
128,
|
||||
255,
|
||||
16_384,
|
||||
u32::MAX as u64,
|
||||
i64::MAX as u64,
|
||||
u64::MAX,
|
||||
] {
|
||||
let mut encoded = Vec::new();
|
||||
encode_uvarint(value, &mut encoded);
|
||||
assert_eq!(decode_uvarint(&encoded), Ok((value, encoded.len())));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_varint_round_trips() {
|
||||
for value in [
|
||||
i64::MIN,
|
||||
-9_223_372_036,
|
||||
-2,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
1_000_000_000,
|
||||
3_000_000_000,
|
||||
i64::MAX,
|
||||
] {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(value, &mut encoded);
|
||||
assert_eq!(decode_varint(&encoded), Ok((value, encoded.len())));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verified_byte_examples() {
|
||||
let cases: &[(i64, &[u8])] = &[
|
||||
(1, &[0x02]),
|
||||
(3_000_000_000, &[0x80, 0xf8, 0x82, 0xad, 0x16]),
|
||||
(1_000_000_000, &[0x80, 0xa8, 0xd6, 0xb9, 0x07]),
|
||||
(1472, &[0x80, 0x17]),
|
||||
(3, &[0x06]),
|
||||
(184, &[0xf0, 0x02]),
|
||||
];
|
||||
for (value, expected) in cases {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(*value, &mut encoded);
|
||||
assert_eq!(&encoded, expected);
|
||||
assert_eq!(decode_varint(expected), Ok((*value, expected.len())));
|
||||
}
|
||||
|
||||
let mut tag = Vec::new();
|
||||
encode_uvarint(9, &mut tag);
|
||||
assert_eq!(tag, [0x09]);
|
||||
tag.clear();
|
||||
encode_uvarint(24, &mut tag);
|
||||
assert_eq!(tag, [0x18]);
|
||||
}
|
||||
|
||||
mod properties {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn uvarint_round_trips(value: u64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_uvarint(value, &mut encoded);
|
||||
prop_assert!(!encoded.is_empty());
|
||||
prop_assert!(encoded.len() <= 10);
|
||||
let (decoded, used) = decode_uvarint(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded, value);
|
||||
prop_assert_eq!(used, encoded.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_varint_round_trips(value: i64) {
|
||||
let mut encoded = Vec::new();
|
||||
encode_varint(value, &mut encoded);
|
||||
prop_assert!(!encoded.is_empty());
|
||||
prop_assert!(encoded.len() <= 10);
|
||||
let (decoded, used) = decode_varint(&encoded).unwrap();
|
||||
prop_assert_eq!(decoded, value);
|
||||
prop_assert_eq!(used, encoded.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zigzag_round_trips_from_signed(value: i64) {
|
||||
prop_assert_eq!(zigzag_decode(zigzag_encode(value)), value);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zigzag_round_trips_from_unsigned(value: u64) {
|
||||
prop_assert_eq!(zigzag_encode(zigzag_decode(value)), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user