From c06fec4be5f5ac44b625d0e72e6829ece8f19151 Mon Sep 17 00:00:00 2001 From: mnovak Date: Fri, 4 Dec 2026 12:56:26 +0100 Subject: [PATCH] [domistream] ENet: test that the client takes the verify's session ids as stock ENet does (fails: they are swapped) A host that answers VERIFY_CONNECT with different incoming and outgoing session ids (stock ENet after a reconnect, PROTOCOL.md 3.1) must see the client send with the verify's outgoing id and accept datagrams carrying the verify's incoming id. --- crates/domistream/tests/enet.rs | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/crates/domistream/tests/enet.rs b/crates/domistream/tests/enet.rs index a6d340b..a616bd8 100644 --- a/crates/domistream/tests/enet.rs +++ b/crates/domistream/tests/enet.rs @@ -497,6 +497,53 @@ fn verify_with_wrong_connect_id_does_not_connect() { assert!(link.events().is_empty()); } +#[test] +fn verify_session_ids_are_taken_as_stock_enet_takes_them() { + // PROTOCOL.md 3.1 [E]: the verify carries both session ids from the client's point of view. Its incoming id is + // the host's outgoing one (what the host puts in its headers, so what the client must accept) and its outgoing id + // is the host's incoming one (what the client must put in its headers). A fresh host slot answers 0/0, which hides + // a swap; after a reconnect stock ENet answers with two different ids, as here. + let (mut client, d, params, _, connect_seq) = start(); + let host = |session_id: u8, sent_time: u16, commands: Vec| { + encode_datagram(&Datagram { + peer_id: params.outgoing_peer_id, + session_id, + sent_time: Some(sent_time), + commands, + }) + .unwrap() + }; + let verify = ConnectParams { outgoing_peer_id: 7, incoming_session_id: 1, outgoing_session_id: 2, ..params }; + let ack = CommandBody::Acknowledge { + received_reliable_sequence: connect_seq, + received_sent_time: d.sent_time.unwrap_or(0), + }; + client + .handle_datagram( + at(1), + &host(1, 1, vec![cmd(false, 0xFF, 0, ack), cmd(true, 0xFF, 1, CommandBody::VerifyConnect(verify))]), + ) + .unwrap(); + assert_eq!(client.poll_event(), Some(EnetEvent::Connected)); + + // The client's datagrams carry its outgoing session id: the verify's outgoing id, 2. + let sent: Vec = + core::iter::from_fn(|| client.poll_transmit(at(1))).map(|d| parse_datagram(&d).unwrap()).collect(); + assert!(!sent.is_empty(), "the verify is acknowledged"); + for d in &sent { + assert_eq!((d.peer_id, d.session_id), (7, 2), "{d:?}"); + } + + // The host's datagrams carry the host's outgoing id (the verify's incoming id, 1): taken. Id 2 is not the + // client's incoming id: dropped. + let wrong = cmd(true, 0, 1, CommandBody::SendReliable(b"wrong session".to_vec())); + assert_eq!(client.handle_datagram(at(2), &host(2, 2, vec![wrong])), Ok(())); + assert_eq!(client.poll_event(), None, "a datagram with the client's outgoing id is not for it"); + let right = cmd(true, 0, 1, CommandBody::SendReliable(b"right session".to_vec())); + client.handle_datagram(at(3), &host(1, 3, vec![right])).unwrap(); + assert_eq!(client.poll_event(), Some(EnetEvent::Received { channel: 0, data: b"right session".to_vec() })); +} + #[test] fn garbage_never_changes_state() { let mut link = connected();