Files
tidewall/test/server/test_list_server.gd
claudeandClaude Opus 5.5 f795ee3fa5 N6: Joining games: direct connect, LAN and server list
- LAN discovery: a listening server answers QUERY packets on its game port
  plus one (ServerBeacon, off with --no-lan). ServerBrowser scans eight game
  ports from 24650 by broadcast.
- A self-hostable list server: --list-server [--port=N] (ListServer,
  default UDP 24660). Game servers register with --list=HOST[:PORT] and
  --name=NAME; they are listed under their source address only after
  echoing a CHALLENGE token. Entries expire after 45 s, 8 per address,
  256 in all; every source is rate-limited (SourceLimiter).
- NetDiscovery messages: every QUERY is padded to 512 bytes and no answer
  is bigger, so neither service amplifies spoofed requests.
- tidewall --browse [--list=...] [--lan-target=IP] [--lan-port=N] prints
  the servers found as JSON until menus (A10) show them.
- People who connect mid-match or during results watch a living player
  (Match.watch) and take places ahead of bots in the next lobby, instead
  of being told the server is full. Bots fill only the places left when
  the match starts.
- Tests: codec round trips, size limits and a 40,000-message fuzz; list
  server with fake servers (challenge, stolen and wrong tokens, renewal,
  expiry, caps, flood); LAN scan over loopback; watchers over UDP; lobby
  backfill. make discovery-check runs it all as real processes.

Closes #37

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 17:33:32 -05:00

186 lines
7.8 KiB
GDScript

extends TestCase
## Issue N6: the list server, LAN discovery and the client's browser, over loopback
## UDP. The game servers here are fake: ServerBeacons, or raw sockets that misbehave.
const HOST: String = "127.0.0.1"
var list: ListServer
var browser: ServerBrowser
var beacons: Array[ServerBeacon] = []
var sockets: Array[PacketPeerUDP] = []
var clock: int = 1000
func before_each() -> void:
list = ListServer.new()
assert_eq(list.listen(0, HOST), OK)
browser = ServerBrowser.new()
assert_eq(browser.open(), OK)
beacons.clear()
sockets.clear()
clock = 1000
func after_each() -> void:
list.close()
browser.close()
for beacon: ServerBeacon in beacons:
beacon.close()
for socket: PacketPeerUDP in sockets:
socket.close()
func _beacon(name: String, game_port: int, players: int = 0, lan_port: int = 0) -> ServerBeacon:
var beacon: ServerBeacon = ServerBeacon.new()
beacon.server_name = name
beacon.game_port = game_port
beacon.players = players
assert_eq(beacon.open(lan_port if lan_port > 0 else ServerBeacon.ANY_PORT, HOST), OK)
beacons.append(beacon)
return beacon
## Polls everything until `done`, or real time runs out. The list server and beacons
## run on `clock`, which only moves when a test says so.
func _pump(done: Callable, limit_msec: int = 3000) -> bool:
var started: int = Time.get_ticks_msec()
while Time.get_ticks_msec() - started < limit_msec:
for beacon: ServerBeacon in beacons:
beacon.poll(clock)
list.poll(clock)
browser.poll()
if done.call():
return true
OS.delay_msec(2)
return bool(done.call())
func test_fake_servers_register_after_a_challenge_and_are_listed() -> void:
var names: Array[String] = []
# All from one address, so at most MAX_PER_ADDRESS of them.
var count: int = ListServer.MAX_PER_ADDRESS
for index: int in count + 1:
var beacon: ServerBeacon = _beacon("Fake %02d" % index, 30000 + index, index)
beacon.add_list(HOST, list.port())
if index < count:
names.append("Fake %02d" % index)
assert_true(_pump(func() -> bool: return list.entry_count() == index + 1), "%d listed" % list.entry_count())
_pump(func() -> bool: return false, 300)
assert_eq(list.entry_count(), count, "one more from the same address is refused")
browser.ask_list(HOST, list.port())
assert_true(_pump(func() -> bool: return browser.servers.size() == count), "%d found" % browser.servers.size())
var found: Array[String] = []
for server: Dictionary in browser.sorted():
found.append(String(server["name"]))
assert_eq(server["address"], HOST)
assert_eq(server["source"], "list")
assert_true(server["compatible"])
assert_eq(found[0], "Fake %02d" % (count - 1), "the busiest first")
found.sort()
assert_eq(found, names)
assert_true(list.queries_answered >= 2, "two pages of four")
func test_registrations_renew_and_silent_servers_expire() -> void:
var staying: ServerBeacon = _beacon("Staying", 30100)
staying.add_list(HOST, list.port())
var going: ServerBeacon = _beacon("Going", 30102)
going.add_list(HOST, list.port())
assert_true(_pump(func() -> bool: return list.entry_count() == 2))
going.close()
beacons.erase(going)
# Heartbeats keep the one still running listed well past the time limit.
staying.players = 5
for step: int in 4:
clock += ServerBeacon.HEARTBEAT_MSEC
var expected: int = staying.registrations + 1
assert_true(_pump(func() -> bool: return staying.registrations >= expected))
assert_true(_pump(func() -> bool:
var listed: Array[Dictionary] = list.servers()
return listed.size() >= 1 and int(listed[0]["players"]) == 5))
list.expire(clock)
assert_eq(list.entry_count(), 1)
assert_eq(String(list.servers()[0]["name"]), "Staying", "updates replace the entry")
func test_a_wrong_or_missing_token_is_never_listed() -> void:
var socket: PacketPeerUDP = PacketPeerUDP.new()
assert_eq(socket.bind(0, HOST), OK)
sockets.append(socket)
socket.set_dest_address(HOST, list.port())
# A server claiming a token it never got, and one ignoring its challenge.
socket.put_packet(NetDiscovery.encode_register(12345, NetDiscovery.info(30200, 0, 24, 0, "Liar")))
socket.put_packet(NetDiscovery.encode_register(0, NetDiscovery.info(30200, 0, 24, 0, "Liar")))
assert_true(_pump(func() -> bool: return socket.get_available_packet_count() >= 2))
assert_eq(list.entry_count(), 0)
var challenge: Dictionary = NetDiscovery.decode_challenge(socket.get_packet())
assert_false(challenge.is_empty())
# A token is bound to the address and port it was sent to.
var other: PacketPeerUDP = PacketPeerUDP.new()
assert_eq(other.bind(0, HOST), OK)
sockets.append(other)
other.set_dest_address(HOST, list.port())
other.put_packet(NetDiscovery.encode_register(int(challenge["token"]), NetDiscovery.info(30200, 0, 24, 0, "Thief")))
assert_true(_pump(func() -> bool: return other.get_available_packet_count() >= 1))
assert_eq(list.entry_count(), 0, "a token stolen by another socket doesn't work")
# The right socket echoing it is listed.
socket.put_packet(NetDiscovery.encode_register(int(challenge["token"]), NetDiscovery.info(30200, 0, 24, 0, "Honest")))
assert_true(_pump(func() -> bool: return list.entry_count() == 1))
assert_eq(String(list.servers()[0]["name"]), "Honest")
func test_limits_per_address_overall_and_per_second() -> void:
# Many fake servers from one address: only MAX_PER_ADDRESS are listed.
for index: int in ListServer.MAX_PER_ADDRESS + 4:
var port: int = 41000 + index
var token: int = list.token_for("192.0.2.9", port)
list.handle(NetDiscovery.encode_register(token, NetDiscovery.info(port, 0, 24, 0, "Crowd")), "192.0.2.9", port, clock + index * 100)
assert_eq(list.entry_count(), ListServer.MAX_PER_ADDRESS)
# Overall cap, from many addresses.
for index: int in ListServer.MAX_ENTRIES + 20:
var address: String = "198.51.%d.%d" % [index / 200, index % 200 + 1]
var token: int = list.token_for(address, 5000)
list.handle(NetDiscovery.encode_register(token, NetDiscovery.info(5000, 0, 24, 0, "Many")), address, 5000, clock + 5000 + index * 100)
assert_eq(list.entry_count(), ListServer.MAX_ENTRIES)
# A flood from one address within a second is mostly ignored. (Loopback, so
# the answers that do go out stay on this machine.)
var before: int = list.queries_answered
for _index: int in 500:
list.handle(NetDiscovery.encode_query(), "127.0.0.50", 9, clock + 100000)
assert_eq(list.queries_answered - before, list.limiter.per_window)
# Everything expires once nothing renews it.
list.expire(clock + 1000000)
assert_eq(list.entry_count(), 0)
func test_lan_scan_finds_servers_on_neighbouring_ports() -> void:
# Two servers on one machine, on the default port and the next one a scan asks.
var first_lan: int = 46001
_beacon("Quay", first_lan - 1, 2, first_lan)
_beacon("Breakwater", first_lan + 1, 7, first_lan + 2)
var targets: Array[String] = [HOST]
browser.scan_lan(targets, first_lan - 1)
assert_true(_pump(func() -> bool: return browser.servers.size() == 2), "%d found" % browser.servers.size())
var found: Array[Dictionary] = browser.sorted()
assert_eq(String(found[0]["name"]), "Breakwater")
assert_eq(found[0]["port"], first_lan + 1, "the game port, not the discovery port")
assert_eq(found[0]["players"], 7)
assert_eq(found[0]["source"], "lan")
assert_eq(String(found[1]["name"]), "Quay")
func test_a_beacon_ignores_challenges_it_did_not_ask_for_and_junk() -> void:
var beacon: ServerBeacon = _beacon("Quiet", 30300)
var socket: PacketPeerUDP = PacketPeerUDP.new()
assert_eq(socket.bind(0, HOST), OK)
sockets.append(socket)
socket.set_dest_address(HOST, beacon.port())
socket.put_packet(NetDiscovery.encode_challenge(77))
socket.put_packet(PackedByteArray([1, 2, 3]))
socket.put_packet(NetDiscovery.encode_query().slice(0, 40))
assert_true(_pump(func() -> bool: return beacon.ignored >= 3))
assert_eq(beacon.answered, 0)
assert_eq(beacon.registrations, 0)
assert_eq(socket.get_available_packet_count(), 0, "nothing was sent back")