Skip to content

Commit

Permalink
Add unit test for VersionMismatch (#1524)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mkeeter authored Oct 30, 2024
1 parent c2247df commit 134c114
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
);
}
Expand Down

0 comments on commit 134c114

Please sign in to comment.