From 134c1146ece76ddd04f172f2f1859e2d36b654f5 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Wed, 30 Oct 2024 15:27:26 -0400 Subject: [PATCH] Add unit test for VersionMismatch (#1524) While looking at negotiation, I noticed that `VersionMismatch` is load-bearing: the Downstairs will send it if it receives a `HereIAm` with an incorrect version. As such, we should be unit-testing that its representation doesn't change (same as `HereIAm` and `YesItsMe`). I also made the `YesItsMe` test more exhaustive, testing the full encoding of both IPv4 and IPv6 addresses. --- protocol/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 22746933d..fe3fc077f 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -1328,10 +1328,47 @@ mod tests { }; let encoded = bincode::serialize(&m).unwrap(); assert_eq!( - &encoded[0..8], + encoded, [ 1, 0, 0, 0, // tag - 123, 0, 0, 0 // version + 123, 0, 0, 0, // version + 0, 0, 0, 0, // SocketAddr::SocketAddrV4 + 127, 0, 0, 1, // ip: Ipv4Addr + 123, 0, // port: u16 + ] + ); + + let m = Message::YesItsMe { + version: 123, + repair_addr: "[ff1d::12e]:456".parse().unwrap(), + }; + let encoded = bincode::serialize(&m).unwrap(); + assert_eq!( + encoded, + [ + 1, 0, 0, 0, // tag + 123, 0, 0, 0, // version + 1, 0, 0, 0, // SocketAddr::SocketAddrV6 + 0xff, 0x1d, 0, 0, 0, 0, 0, 0, // ip: Ipv6Addr + 0, 0, 0, 0, 0, 0, 0x1, 0x2e, // ip (cont) + 200, 1 // port + ] + ); + } + + /// Test that the `Message::VersionMismatch { version }` encoding is stable + /// + /// This encoding is used to check for compatibility, so it cannot change + /// even upon protocol version bumps. + #[test] + fn version_mismatch() { + let m = Message::VersionMismatch { version: 0x3456 }; + let encoded = bincode::serialize(&m).unwrap(); + assert_eq!( + encoded, + [ + 2, 0, 0, 0, // tag + 0x56, 0x34, 0, 0 // version ] ); }