mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
60 lines
3.2 KiB
GDScript
60 lines
3.2 KiB
GDScript
extends "res://tests/test_case.gd"
|
|
|
|
const MatchNet = preload("res://scripts/match_net.gd")
|
|
|
|
# Adversarial-review regression: _hello's player_name used to be broadcast
|
|
# to every peer completely unvalidated — a multi-MB name head-of-line-
|
|
# blocked the reliable control channel hard enough that a concurrently-
|
|
# joining client's own _welcome never arrived. _sanitize_player_name() is
|
|
# the fix; these are pure-function tests for it, independent of the live
|
|
# two-process rejection test in tests/match_net_smoke.gd (--role=client-longname).
|
|
|
|
func test_normal_name_unchanged() -> void:
|
|
assert_eq(MatchNet._sanitize_player_name("Alice"), "Alice", "a normal name passes through unchanged")
|
|
|
|
|
|
func test_strips_control_characters() -> void:
|
|
var bell := String.chr(7) # a control char with no named GDScript escape
|
|
var raw := "Bad\nName\twith\rcontrol" + bell + "chars"
|
|
var clean := MatchNet._sanitize_player_name(raw)
|
|
assert_true(not clean.contains("\n"), "no newline")
|
|
assert_true(not clean.contains("\t"), "no tab")
|
|
assert_true(not clean.contains("\r"), "no carriage return")
|
|
assert_true(not clean.contains(bell), "no bell/control char")
|
|
|
|
|
|
func test_clamps_to_max_display_length() -> void:
|
|
var raw := "X".repeat(1000)
|
|
var clean := MatchNet._sanitize_player_name(raw)
|
|
assert_eq(clean.length(), MatchNet.MAX_PLAYER_NAME_LENGTH, "clamped to MAX_PLAYER_NAME_LENGTH")
|
|
|
|
|
|
func test_empty_or_whitespace_only_falls_back_to_default() -> void:
|
|
assert_eq(MatchNet._sanitize_player_name(""), "Player", "empty string falls back")
|
|
assert_eq(MatchNet._sanitize_player_name(" "), "Player", "whitespace-only falls back")
|
|
assert_eq(MatchNet._sanitize_player_name("\n\t\r"), "Player", "control-characters-only falls back")
|
|
|
|
|
|
func test_leading_trailing_whitespace_trimmed() -> void:
|
|
assert_eq(MatchNet._sanitize_player_name(" Bob "), "Bob", "surrounding whitespace trimmed")
|
|
|
|
|
|
func test_allocated_join_authorisation_is_allowlisted_and_bound_to_server() -> void:
|
|
var claims := {
|
|
"MatchID": "match-1", "ServerID": "server-1", "PlayerID": "player-1",
|
|
"SteamID": "steam-1", "Slot": 2, "Team": 1, "Protocol": "1",
|
|
"Generation": 1, "ExpiresAt": "2099-08-31T12:00:00Z",
|
|
}
|
|
var token := Marshalls.raw_to_base64(JSON.stringify({"Authorisation": claims, "Signature": "trusted-signature"}).to_utf8_buffer())
|
|
var match_net := MatchNet.new()
|
|
assert_true(match_net.configure_join_authorisations([token], {"match_id": "match-1", "server_id": "server-1", "protocol": "1", "protocol_version": 1}), "valid roster configures")
|
|
assert_true(match_net._valid_join_authorisation(token), "allowlisted matching token is accepted")
|
|
assert_true(not match_net._valid_join_authorisation(token + "tampered"), "token mutation is rejected")
|
|
var wrong_claims := claims.duplicate()
|
|
wrong_claims["ServerID"] = "other-server"
|
|
var wrong_token := Marshalls.raw_to_base64(JSON.stringify({"Authorisation": wrong_claims, "Signature": "trusted-signature"}).to_utf8_buffer())
|
|
assert_true(not match_net._valid_join_authorisation(wrong_token), "wrong server claim is rejected")
|
|
assert_true(not match_net.is_join_authorisation_active(token), "validated token is not active before admission")
|
|
match_net._active_join_peers[token] = 42
|
|
assert_true(match_net.is_join_authorisation_active(token), "active token is visible to the duplicate-admission guard")
|