wire_format_tests.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #![allow(clippy::arithmetic_side_effects)]
  2. #[cfg(test)]
  3. mod tests {
  4. use {
  5. crate::protocol::Protocol,
  6. serde::Serialize,
  7. solana_net_utils::tooling_for_tests::{hexdump, validate_packet_format},
  8. solana_sanitize::Sanitize,
  9. std::path::PathBuf,
  10. };
  11. fn parse_gossip(bytes: &[u8]) -> anyhow::Result<Protocol> {
  12. let pkt: Protocol = solana_perf::packet::deserialize_from_with_limit(bytes)?;
  13. pkt.sanitize()?;
  14. Ok(pkt)
  15. }
  16. fn serialize<T: Serialize>(pkt: T) -> Vec<u8> {
  17. bincode::serialize(&pkt).unwrap()
  18. }
  19. fn find_differences(a: &[u8], b: &[u8]) -> Option<usize> {
  20. if a.len() != b.len() {
  21. return Some(a.len().min(b.len()));
  22. }
  23. for (idx, (e1, e2)) in a.iter().zip(b).enumerate() {
  24. if e1 != e2 {
  25. return Some(idx);
  26. }
  27. }
  28. None
  29. }
  30. /// Test the ability of gossip parsers to understand and re-serialize a corpus of
  31. /// packets captured from mainnet.
  32. ///
  33. /// This test requires external files and is not run by default.
  34. /// Export the "GOSSIP_WIRE_FORMAT_PACKETS" variable to run this test
  35. #[test]
  36. fn test_gossip_wire_format() {
  37. agave_logger::setup();
  38. let path_base = match std::env::var_os("GOSSIP_WIRE_FORMAT_PACKETS") {
  39. Some(p) => PathBuf::from(p),
  40. None => {
  41. eprintln!("Test requires GOSSIP_WIRE_FORMAT_PACKETS env variable, skipping!");
  42. return;
  43. }
  44. };
  45. for entry in
  46. std::fs::read_dir(path_base).expect("Expecting env var to point to a directory")
  47. {
  48. let entry = entry.expect("Expecting a readable file");
  49. validate_packet_format(
  50. &entry.path(),
  51. parse_gossip,
  52. serialize,
  53. hexdump,
  54. find_differences,
  55. )
  56. .unwrap();
  57. }
  58. }
  59. }