#![allow(clippy::arithmetic_side_effects)]
#[cfg(test)]
mod tests {
use {
crate::{contact_info::ContactInfo, crds_data::CrdsData},
crate::protocol::Protocol,
serde::Serialize,
solana_net_utils::tooling_for_tests::{hexdump, validate_packet_format},
solana_pubkey::Pubkey,
solana_sanitize::Sanitize,
std::path::PathBuf,
};
fn parse_gossip(bytes: &[u8]) -> anyhow::Result<Protocol> {
let pkt: Protocol = solana_perf::packet::deserialize_from_with_limit(bytes)?;
pkt.sanitize()?;
Ok(pkt)
}
fn serialize<T: Serialize>(pkt: T) -> Vec<u8> {
bincode::serialize(&pkt).unwrap()
}
fn find_differences(a: &[u8], b: &[u8]) -> Option<usize> {
if a.len() != b.len() {
return Some(a.len().min(b.len()));
}
for (idx, (e1, e2)) in a.iter().zip(b).enumerate() {
if e1 != e2 {
return Some(idx);
}
}
None
}
#[test]
fn test_gossip_wire_format() {
agave_logger::setup();
let path_base = match std::env::var_os("GOSSIP_WIRE_FORMAT_PACKETS") {
Some(p) => PathBuf::from(p),
None => {
eprintln!("Test requires GOSSIP_WIRE_FORMAT_PACKETS env variable, skipping!");
return;
}
};
for entry in
std::fs::read_dir(path_base).expect("Expecting env var to point to a directory")
{
let entry = entry.expect("Expecting a readable file");
validate_packet_format(
&entry.path(),
parse_gossip,
serialize,
hexdump,
find_differences,
)
.unwrap();
}
}
#[test]
fn test_crds_contact_info_variant_offset_is_stable() {
let value = CrdsData::from(ContactInfo::new_localhost(
&Pubkey::new_unique(),
123456789,
));
let bytes = bincode::serialize(&value).unwrap();
assert!(bytes.len() >= 4);
assert_eq!(u32::from_le_bytes(bytes[0..4].try_into().unwrap()), 11);
}
}