N1: Transport and connection handshake
ENet over UDP, used directly through ENetConnection, with a handshake before a connection counts as a player. - NetProtocol: HELLO, WELCOME and REJECT layouts, the protocol version, name cleaning, the reasons a connection ends and a sentence for the player about each. Decoders return nothing for malformed input and never raise an error. - NetServer: listens, takes each guest through the handshake, and reports joined, left and refused. It refuses a different protocol version, a full server, a malformed HELLO and a guest that stays silent, each with its own reason. Handles are never reused; duplicate names get a number. - NetClient: connects, introduces itself, and ends up joined or closed with a reason. Leaving says goodbye so the server knows at once; a client that vanishes is dropped by ENet's timeout. - Tests run a real server and clients over loopback UDP, with bare ENet peers sending malformed and random packets. The headless server doesn't listen yet; N2 connects this to the match. Closes #32 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
extends TestCase
|
||||
|
||||
## The handshake's messages (issue N1): they round-trip, and nothing malformed decodes.
|
||||
|
||||
|
||||
func test_hello_round_trips() -> void:
|
||||
var hello: Dictionary = NetProtocol.decode_hello(NetProtocol.encode_hello("Mara"))
|
||||
assert_eq(hello, {"version": NetProtocol.VERSION, "name": "Mara"})
|
||||
assert_eq(NetProtocol.decode_hello(NetProtocol.encode_hello("Ægir Þór", 7)), {"version": 7, "name": "Ægir Þór"})
|
||||
|
||||
|
||||
func test_welcome_and_reject_round_trip() -> void:
|
||||
var welcome: Dictionary = NetProtocol.decode_welcome(NetProtocol.encode_welcome(70000, 0x7FFFFFFFFFFFFFF1, 24, "Mara 2"))
|
||||
assert_eq(welcome["handle"], 70000)
|
||||
assert_eq(welcome["seed"], 0x7FFFFFFFFFFFFFF1)
|
||||
assert_eq(welcome["max_players"], 24)
|
||||
assert_eq(welcome["tick_rate"], Tuning.TICK_RATE_HZ)
|
||||
assert_eq(welcome["name"], "Mara 2")
|
||||
assert_eq(welcome["version"], NetProtocol.VERSION)
|
||||
assert_eq(NetProtocol.decode_reject(NetProtocol.encode_reject(NetProtocol.REASON_FULL)),
|
||||
{"reason": NetProtocol.REASON_FULL, "server_version": NetProtocol.VERSION})
|
||||
|
||||
|
||||
func test_names_are_cleaned() -> void:
|
||||
assert_eq(NetProtocol.clean_name(" Mara of the\tReef "), "Mara of the Reef")
|
||||
assert_eq(NetProtocol.clean_name(""), NetProtocol.DEFAULT_NAME)
|
||||
assert_eq(NetProtocol.clean_name(" \n\t "), NetProtocol.DEFAULT_NAME)
|
||||
assert_eq(NetProtocol.clean_name("a" + String.chr(1) + "b" + String.chr(0x1B) + "c" + String.chr(0x7F)), "abc", "control characters go")
|
||||
assert_eq(NetProtocol.clean_name("ab" + String.chr(0x202E) + "cd" + String.chr(0x200B)), "abcd", "direction overrides and zero-width marks go")
|
||||
assert_eq(NetProtocol.clean_name("x".repeat(40)).length(), NetProtocol.NAME_MAX_CHARS)
|
||||
assert_true(NetProtocol.clean_name("語".repeat(40)).to_utf8_buffer().size() <= NetProtocol.NAME_MAX_BYTES)
|
||||
assert_eq(NetProtocol.clean_name("Line\nBreak"), "LineBreak")
|
||||
|
||||
|
||||
func test_malformed_hellos_decode_to_nothing() -> void:
|
||||
var good: PackedByteArray = NetProtocol.encode_hello("Mara")
|
||||
assert_eq(NetProtocol.decode_hello(PackedByteArray()), {})
|
||||
for size: int in good.size():
|
||||
assert_eq(NetProtocol.decode_hello(good.slice(0, size)), {}, "cut to %d bytes" % size)
|
||||
var long: PackedByteArray = good.duplicate()
|
||||
long.append(0x41)
|
||||
assert_eq(NetProtocol.decode_hello(long), {}, "bytes after the name")
|
||||
var wrong_magic: PackedByteArray = good.duplicate()
|
||||
wrong_magic[2] = 0x58
|
||||
assert_eq(NetProtocol.decode_hello(wrong_magic), {})
|
||||
var wrong_type: PackedByteArray = good.duplicate()
|
||||
wrong_type[0] = NetProtocol.MSG_WELCOME
|
||||
assert_eq(NetProtocol.decode_hello(wrong_type), {})
|
||||
var huge: PackedByteArray = good.duplicate()
|
||||
huge.resize(4000)
|
||||
assert_eq(NetProtocol.decode_hello(huge), {})
|
||||
# Names that aren't UTF-8: a stray continuation byte, a cut sequence, an overlong
|
||||
# form, a surrogate. None of them may reach the engine's decoder.
|
||||
for bad: Array in [[0x80], [0xC3], [0xE2, 0x82], [0xC0, 0xAF], [0xE0, 0x80, 0xAF], [0xED, 0xA0, 0x80], [0xF5, 0x80, 0x80, 0x80], [0xFF]]:
|
||||
var bytes: PackedByteArray = good.slice(0, 7)
|
||||
bytes.append(bad.size())
|
||||
bytes.append_array(PackedByteArray(bad))
|
||||
assert_eq(NetProtocol.decode_hello(bytes), {}, "name bytes %s" % [bad])
|
||||
|
||||
|
||||
func test_random_bytes_never_raise_an_error() -> void:
|
||||
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
||||
rng.seed = 20261112
|
||||
var decoded: int = 0
|
||||
for _round: int in 4000:
|
||||
var bytes: PackedByteArray = PackedByteArray()
|
||||
bytes.resize(rng.randi_range(0, 120))
|
||||
for index: int in bytes.size():
|
||||
bytes[index] = rng.randi_range(0, 255)
|
||||
if rng.randf() < 0.5 and bytes.size() >= 8:
|
||||
# Past the cheap checks: a right type and magic, and often a right length.
|
||||
bytes[0] = NetProtocol.MSG_HELLO
|
||||
for index: int in 4:
|
||||
bytes[1 + index] = NetProtocol.MAGIC.to_ascii_buffer()[index]
|
||||
if rng.randf() < 0.7:
|
||||
bytes[7] = mini(bytes.size() - 8, 255)
|
||||
var hello: Dictionary = NetProtocol.decode_hello(bytes)
|
||||
if not hello.is_empty():
|
||||
decoded += 1
|
||||
assert_true(String(hello["name"]).length() >= 1 and String(hello["name"]).length() <= NetProtocol.NAME_MAX_CHARS)
|
||||
NetProtocol.decode_welcome(bytes)
|
||||
NetProtocol.decode_reject(bytes)
|
||||
assert_true(decoded > 0, "some random hellos have valid names, and those decode")
|
||||
|
||||
|
||||
func test_a_version_refusal_says_which_side_is_behind() -> void:
|
||||
var newer: String = NetProtocol.reason_text(NetProtocol.REASON_VERSION, NetProtocol.VERSION + 1)
|
||||
assert_true(newer.contains("newer version") and newer.contains("Update the game"), newer)
|
||||
var older: String = NetProtocol.reason_text(NetProtocol.REASON_VERSION, 3, 5)
|
||||
assert_true(older.contains("older version") and older.contains("protocol 3") and older.contains("speaks 5"), older)
|
||||
for reason: int in range(NetProtocol.REASON_NONE, NetProtocol.REASON_LOST + 1):
|
||||
assert_false(NetProtocol.reason_text(reason).is_empty())
|
||||
Reference in New Issue
Block a user