Files
benilla-octo/crates/benilla-protocol/tests/handshake.rs
T
gromandClaude Opus 5 81d96af35d Everwood: the HD/OctoWoW line of benilla, imported onto upstream
This checkout had no git history at all, so this one commit carries the whole
of the Everwood work as a single import on top of chairmanpow/benilla-octo's
`main` (706cd71). The two trees have genuinely diverged — each has files the
other does not — so this is a fork point, not a catch-up.

Today's work, which is the reason a baseline exists at all:

- Booth previews no longer cast world shadows (`NO_BOOTH_SHADOW`). Portrait
  characters were entering the sun's shadow cascades, so every skinned-rig
  shadow specialization compiled live mid-frame; the hitch detector logged
  "main thread blocked 1316 ms" beside a `compiled LIVE` warning. This was
  the Fashion/Tmog stutter.
- Per-pane dress-up: each `DressUpModel` pane assembles and stages its own
  tried-on look instead of every pane mirroring the live player.
- Item template cache is append-only JSONL written on the IO pool, replacing
  an 11.6 MB whole-map JSON rewrite on the main thread every 10 s.
- `WOW_SYSTEM_TIMING` rebuilt per-thread: it took 2 mutexes per system enter
  and 4 per exit, costing 15 ms a frame (590 fps -> 60) and corrupting the
  measurements it was taken to produce.
- The visibility-churn sweep moved to its own `WOW_FLASH_CHURN` switch, so a
  frame-cost reading no longer pays 2.7 ms a frame for it.

Local state (`benilla-config/`, `logs/`, `target/`) is excluded by the
existing .gitignore; the account credential it holds is not in this commit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SgstFWtn1RxEJ5kVrpgYwn
2026-10-15 15:01:14 -06:00

97 lines
3.5 KiB
Rust

//! The world handshake over a real socket against a fake server: packets may precede
//! `SMSG_AUTH_RESPONSE`. Deviation: `SMSG_WARDEN_DATA` refuses the login, because benilla cannot
//! answer Warden and the server kicks a client that stays silent about 30 s later.
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::thread;
use benilla_protocol::messages::opcode;
use benilla_protocol::{messages, WardenRequired, WorldSession};
use benilla_srp::vanilla_header::HeaderCrypto;
use benilla_srp::SESSION_KEY_LENGTH;
const SESSION_KEY: [u8; SESSION_KEY_LENGTH] = [7u8; SESSION_KEY_LENGTH];
const SERVER_SEED: u32 = 0xDEAD_BEEF;
/// Write one server packet: `u16` BE size (opcode plus body) and `u16` LE opcode, encrypted once
/// `crypto` is set, then the plaintext body.
fn send(stream: &mut TcpStream, crypto: Option<&mut HeaderCrypto>, opcode: u16, body: &[u8]) {
let size = (body.len() + 2) as u16;
let s = size.to_be_bytes();
let o = opcode.to_le_bytes();
let mut header = [s[0], s[1], o[0], o[1]];
if let Some(c) = crypto {
c.encrypter().encrypt(&mut header);
}
stream.write_all(&header).unwrap();
stream.write_all(body).unwrap();
}
/// Read the client's unencrypted `CMSG_AUTH_SESSION` (6-byte header: BE size + LE u32 opcode).
fn read_auth_session(stream: &mut TcpStream) {
let mut header = [0u8; 6];
stream.read_exact(&mut header).unwrap();
let size = u16::from_be_bytes([header[0], header[1]]) as usize;
let mut body = vec![0u8; size - 4];
stream.read_exact(&mut body).unwrap();
}
/// A fake world server that sends the `pre` packets, encrypted and in order, before a successful
/// `SMSG_AUTH_RESPONSE`; returns its address.
fn fake_server(pre: Vec<(u16, Vec<u8>)>) -> String {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap().to_string();
thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
send(
&mut stream,
None,
opcode::SMSG_AUTH_CHALLENGE,
&SERVER_SEED.to_le_bytes(),
);
read_auth_session(&mut stream);
let mut crypto = HeaderCrypto::from_session_key(SESSION_KEY);
for (op, body) in pre {
send(&mut stream, Some(&mut crypto), op, &body);
}
send(
&mut stream,
Some(&mut crypto),
opcode::SMSG_AUTH_RESPONSE,
&[messages::AUTH_OK],
);
// Hold the socket open so the client's reads never see a premature EOF.
thread::sleep(std::time::Duration::from_secs(2));
});
addr
}
#[test]
fn auth_response_alone_completes_the_handshake() {
let addr = fake_server(vec![]);
assert!(WorldSession::connect(&addr, "one", SESSION_KEY).is_ok());
}
/// The server interleaves its own packets ahead of the auth response.
#[test]
fn packets_ahead_of_the_auth_response_are_skipped() {
let addr = fake_server(vec![
(opcode::SMSG_LOGIN_VERIFY_WORLD, vec![0u8; 20]),
(opcode::SMSG_SET_FACTION_STANDING, vec![0u8; 12]),
]);
assert!(WorldSession::connect(&addr, "one", SESSION_KEY).is_ok());
}
#[test]
fn a_warden_server_is_refused_at_the_handshake() {
let addr = fake_server(vec![(opcode::SMSG_WARDEN_DATA, vec![0u8; 16])]);
let Err(err) = WorldSession::connect(&addr, "one", SESSION_KEY) else {
panic!("a Warden server must not yield a session");
};
assert!(
err.downcast_ref::<WardenRequired>().is_some(),
"expected WardenRequired, got: {err:#}"
);
}