Skip to content

Commit 134c114

Browse files
authored
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.
1 parent c2247df commit 134c114

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

protocol/src/lib.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,10 +1328,47 @@ mod tests {
13281328
};
13291329
let encoded = bincode::serialize(&m).unwrap();
13301330
assert_eq!(
1331-
&encoded[0..8],
1331+
encoded,
13321332
[
13331333
1, 0, 0, 0, // tag
1334-
123, 0, 0, 0 // version
1334+
123, 0, 0, 0, // version
1335+
0, 0, 0, 0, // SocketAddr::SocketAddrV4
1336+
127, 0, 0, 1, // ip: Ipv4Addr
1337+
123, 0, // port: u16
1338+
]
1339+
);
1340+
1341+
let m = Message::YesItsMe {
1342+
version: 123,
1343+
repair_addr: "[ff1d::12e]:456".parse().unwrap(),
1344+
};
1345+
let encoded = bincode::serialize(&m).unwrap();
1346+
assert_eq!(
1347+
encoded,
1348+
[
1349+
1, 0, 0, 0, // tag
1350+
123, 0, 0, 0, // version
1351+
1, 0, 0, 0, // SocketAddr::SocketAddrV6
1352+
0xff, 0x1d, 0, 0, 0, 0, 0, 0, // ip: Ipv6Addr
1353+
0, 0, 0, 0, 0, 0, 0x1, 0x2e, // ip (cont)
1354+
200, 1 // port
1355+
]
1356+
);
1357+
}
1358+
1359+
/// Test that the `Message::VersionMismatch { version }` encoding is stable
1360+
///
1361+
/// This encoding is used to check for compatibility, so it cannot change
1362+
/// even upon protocol version bumps.
1363+
#[test]
1364+
fn version_mismatch() {
1365+
let m = Message::VersionMismatch { version: 0x3456 };
1366+
let encoded = bincode::serialize(&m).unwrap();
1367+
assert_eq!(
1368+
encoded,
1369+
[
1370+
2, 0, 0, 0, // tag
1371+
0x56, 0x34, 0, 0 // version
13351372
]
13361373
);
13371374
}

0 commit comments

Comments
 (0)