"""tools/coop_relay.py and the co-op wire format: the secret alphabet, the derived keys, HMAC tags, the byte vectors of tests/coop_vectors.txt (which runtime/coop_probe.c checks the C codec against), the relay's rooms (create, join, forward, roster, rejoin with its token, expiry, limits per member, per network and for the relay, bad tags, a full room, a protocol mismatch, removal by the maker) on a fake clock, hostile clients (a stolen client id, challenge floods, room floods, fan-out, garbage from a member), the same over real UDP sockets, and, when NX2NAVI_EXE names a built game, `--probe-coop` against a relay started here (with and without receive loss, and through a relay restart). No game files; stdlib only. Run: python tests/coop_relay_test.py (NX2NAVI_EXE=/ocarina also runs the probe) python tests/coop_relay_test.py --write-vectors rewrites tests/coop_vectors.txt from the Python codec """ import hashlib import hmac import math import os import random import socket import struct import subprocess import sys import threading import time import unittest from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / 'tools')) import coop_relay as cr # noqa: E402 VECTORS = ROOT / 'tests' / 'coop_vectors.txt' RELAY_PY = ROOT / 'tools' / 'coop_relay.py' SECRET = 'H7K3WMBCDFGH234567' # Sealed player messages (XChaCha20-Poly1305 under the message key of SECRET), made once by runtime/coop_proto.c: # Python's standard library has no XChaCha20-Poly1305, so this side checks their clear fields and the probe checks that # they open, and that sealing again gives the same bytes. (name, src, dst, session, counter, kind, message, payload) SEALED = [ ('test', 3, 255, 0x0123456789abcdef, 0x00000001, 0xf0, '68656c6c6f', '01efcdab896745230101000000d2244219967816dfefab11d765441dbffe22c6b5b160'), ('empty', 1, 2, 0xfedcba9876543210, 0xffffffff, 0x03, '-', '011032547698badcfeffffffffe5632ae3fe1043b0359b661003ee21dbb9'), ] def hx(b): return b.hex() if b else '-' def unhex(s): return b'' if s == '-' else bytes.fromhex(s) # ---- POS (player message kind 2, sealed as every player message): where a player is and what Link plays, for the co-op # other players in the world (no sender in this build). The relay forwards it unread, so its codec is here and in runtime/coop_proto.c only. # Little endian: u8 version (2), u32 pseq (the sender's counter), u32 t_ms (the sender's clock), u8 flags (POS_ADULT, # POS_IN_WORLD), u8 area length (< 48) and the area's name, f32 x y z, f32 yaw (radians), u8 slot count (<= 4), per AS # slot u8 node name length (< 48), the name, f32 frame. Names are printable ASCII and numbers finite, else the message # is refused. Version 1 had a u16 pseq. POS_VERSION = 2 POS_ADULT, POS_IN_WORLD = 1, 2 POS_TEXT_MAX, POS_SLOTS = 47, 4 POS_HEAD = 11 def pack_pos(pseq, t_ms, flags, area, xyz, yaw, slots): out = struct.pack(' POS_TEXT_MAX or len(b) < POS_HEAD + b[10] + 17: return None pseq, t_ms, flags, alen = struct.unpack_from(' POS_SLOTS or not _printable(area) or not all(map(math.isfinite, (x, y, z, yaw))): return None slots = [] for _ in range(n): if at >= len(b) or b[at] > POS_TEXT_MAX or at + 1 + b[at] + 4 > len(b): return None node = b[at + 1:at + 1 + b[at]] frame = struct.unpack_from(' RFC 4231 cases and a locator as the key', '# secret normalisation: the secret a text means, - for none', '# keys BLAKE2b keyed with the secret (coop_proto.h)', '# dgram parse, verify the tag with the', '# secret\'s locator (- : a zero tag), pack again', '# badtag parses, and the tag does not verify', '# connect|challenge|roster|joined|error|kick ... one message payload: parse, check the fields, pack again', '# sealed opens to kind and message, seals', '# to the same bytes', '# pos ... a POS (the message after its', '# kind byte): parse, check the fields, pack again', '# reject parse must fail'] def line(*tokens): out.append(' '.join(str(t) for t in tokens)) loc = cr.room_locator(SECRET) for name, key, msg in ( ('rfc4231-1', bytes([0x0b] * 20), b'Hi There'), ('rfc4231-2', b'Jefe', b'what do ya want for nothing?'), ('rfc4231-6', bytes([0xaa] * 131), b'Test Using Larger Than Block-Size Key - Hash Key First'), ('locator', loc, bytes(range(26))), ('empty', cr.room_locator('23BCDF' * 3), b'')): line('hmac', name, hx(key), hx(msg), hmac.new(key, msg, hashlib.sha256).hexdigest()) for canon, text in ((SECRET, 'h7k3wm-bcdfgh-234567'), (SECRET, ' H7K 3WM BCD FGH 234 567 '), (SECRET, SECRET), ('-', 'H7K3WMBCDFGH23456'), ('-', 'H7K3WMBCDFGH2345678'), ('-', 'H7K3WMBCDFGH23456E'), ('-', 'H7K3WMBCDFGH23456Q'), ('-', 'H7K3WMBCDFGH234560'), ('-', 'H7K3WM.BCDFGH234567'), ('-', '')): line('secret', canon, text) for s in (SECRET, '23BCDF' * 3, 'Z' * 18): line('keys', s, hx(cr.room_locator(s)), hx(cr.message_key(s))) cookie = bytes(range(0xA0, 0xB0)) token = bytes(range(0x30, 0x40)) cid, session, nonce = 0x0123456789ABCDEF, 0x1122334455667788, 0x99AABBCCDDEEFF00 roster = cr.pack_roster([(1, cr.FLAG_MAKER, session, b'Alice'), (2, cr.FLAG_AWAY, 7, 'Zoë'.encode())]) sealed = unhex(SEALED[0][7]) dgrams = [ ('connect-create', SECRET, cr.T_CONNECT, 0, 0, 0, 7, cr.pack_connect(cr.INTENT_CREATE, 0, loc, cid, session, nonce, b'Alice')), ('connect-join', SECRET, cr.T_CONNECT, 0, 0, 0, 7, cr.pack_connect(cr.INTENT_JOIN, 0, loc, cid, session, nonce, b'Bob', cookie, token)), ('challenge', SECRET, cr.T_CHALLENGE, 0, 0, 0, 1, cr.pack_challenge(nonce, cookie)), ('joined', SECRET, cr.T_JOINED, 0xCAFE0001, 0, 2, 2, cr.pack_joined(2, nonce, token, roster)), ('error', SECRET, cr.T_ERROR, 0, 0, 0, 3, cr.pack_error(cr.E_NO_ROOM, nonce, 'no such room')), ('roster', SECRET, cr.T_ROSTER, 0xCAFE0001, 0, 1, 4, roster), ('ping', SECRET, cr.T_PING, 0xCAFE0001, 3, 0, 99, struct.pack(' 2 else ''])) else: parts = raw.split() rows.append((parts[0], parts[1:])) return rows def roster_entries(tokens): return tuple((int(e.split(':')[0]), int(e.split(':')[1]), int(e.split(':')[2], 16), bytes.fromhex(e.split(':')[3])) for e in tokens) class CodecTest(unittest.TestCase): def test_the_alphabet_has_27_symbols_and_no_lookalikes(self): self.assertEqual(len(cr.ALPHABET), 27) self.assertEqual(len(set(cr.ALPHABET)), 27) for c in '01OILEQAU': self.assertNotIn(c, cr.ALPHABET) def test_a_secret_has_more_than_80_bits(self): import math self.assertGreater(cr.SECRET_LEN * math.log2(len(cr.ALPHABET)), 85) def test_normalize(self): self.assertEqual(cr.normalize_secret('h7k3wm-bcdfgh-234567'), SECRET) for bad in ('', SECRET[:-1], SECRET + 'X', SECRET[:-1] + '0', SECRET[:-1] + 'E', SECRET[:-1] + '\u017f'): self.assertIsNone(cr.normalize_secret(bad), bad) self.assertEqual(cr.format_secret(SECRET), 'H7K3WM-BCDFGH-234567') def test_random_secrets_are_valid(self): for _ in range(200): self.assertIsNotNone(cr.normalize_secret(cr.random_secret())) def test_keys_are_the_documented_blake2b(self): self.assertEqual(cr.room_locator(SECRET), hashlib.blake2b(b'nx2navi-coop/2 room', key=SECRET.encode(), digest_size=16).digest()) self.assertEqual(cr.message_key(SECRET), hashlib.blake2b(b'nx2navi-coop/2 messages', key=SECRET.encode(), digest_size=32).digest()) self.assertNotEqual(cr.room_locator(SECRET), cr.message_key(SECRET)[:16]) def test_tag_covers_header_and_payload(self): key = cr.room_locator(SECRET) d = cr.pack(cr.T_DATA, 5, 1, 2, 3, b'payload', key) dg = cr.parse(d) self.assertTrue(dg.verify(key)) for i in range(len(d)): if 16 <= i < 24 or i in (10, 11): continue # the tag itself; the length (covered by parse) p = cr.parse(d[:i] + bytes([d[i] ^ 0x40]) + d[i + 1:]) if p is not None: self.assertFalse(p.verify(key), f'byte {i}') self.assertFalse(dg.verify(cr.room_locator('23BCDF' * 3))) def test_clean_name(self): self.assertEqual(cr.clean_name(b'Alice'), b'Alice') self.assertEqual(cr.clean_name(b'A\x01B\x7fC'), b'A?B?C') self.assertEqual(cr.clean_name(b'ab\xffcd'), b'ab?cd') self.assertEqual(cr.clean_name('a\u0085b\u202ec\u2066d'.encode()), b'a?b?c?d') # C1 and bidi controls self.assertEqual(len(cr.clean_name(('\u00e9' * 30).encode())), 24) self.assertEqual(cr.clean_name(b'x' * 30), b'x' * 24) def test_vectors(self): """The file matches what this codec writes now, and every line checks out.""" self.assertEqual(VECTORS.read_text(encoding='utf-8'), '\n'.join(build_vectors()) + '\n', 'tests/coop_vectors.txt is stale: python tests/coop_relay_test.py --write-vectors') count = 0 for kind, t in read_vectors(): count += 1 if kind == 'hmac': self.assertEqual(hmac.new(unhex(t[1]), unhex(t[2]), hashlib.sha256).hexdigest(), t[3], t[0]) elif kind == 'secret': self.assertEqual(cr.normalize_secret(t[1]), None if t[0] == '-' else t[0], t[1]) elif kind == 'keys': self.assertEqual((cr.room_locator(t[0]).hex(), cr.message_key(t[0]).hex()), (t[1], t[2])) elif kind == 'dgram': name, s, ver, type_, room, src, dst, seq, payload, data = t d = unhex(data) p = cr.parse(d) self.assertEqual((p.ver, p.type, p.room, p.src, p.dst, p.seq, p.payload), (int(ver), int(type_), int(room), int(src), int(dst), int(seq), unhex(payload)), name) key = cr.room_locator(s) if s != '-' else None self.assertTrue(p.verify(key) if key else p.tag == bytes(8), name) self.assertEqual(cr.pack(p.type, p.room, p.src, p.dst, p.seq, p.payload, key, ver=p.ver), d, name) elif kind == 'badtag': p = cr.parse(unhex(t[2])) self.assertIsNotNone(p, t[0]) self.assertFalse(p.verify(cr.room_locator(t[1])), t[0]) elif kind == 'connect': name, intent, flags, loc, cid, sess, nonce, nm, cookie, token, payload = t c = cr.parse_connect(unhex(payload)) self.assertEqual(c, (int(intent), int(flags), unhex(loc), int(cid, 16), int(sess, 16), int(nonce, 16), unhex(nm), unhex(cookie), unhex(token)), name) self.assertEqual(cr.pack_connect(*c), unhex(payload), name) elif kind == 'challenge': self.assertEqual(cr.parse_challenge(unhex(t[1])), (int(t[2], 16), unhex(t[3])), t[0]) elif kind == 'roster': payload, want = unhex(t[1]), roster_entries(t[2:]) self.assertEqual(cr.parse_roster(payload), want, t[0]) self.assertEqual(cr.pack_roster(want), payload, t[0]) elif kind == 'joined': payload = unhex(t[1]) self.assertEqual(cr.parse_joined(payload), (int(t[2]), int(t[3], 16), unhex(t[4]), roster_entries(t[5:])), t[0]) elif kind == 'error': self.assertEqual(cr.parse_error(unhex(t[1])), (int(t[2]), int(t[3], 16), unhex(t[4]).decode()), t[0]) elif kind == 'kick': self.assertEqual(cr.parse_kick(unhex(t[1])), (int(t[2]), int(t[3], 16)), t[0]) elif kind == 'sealed': name, s, src, dst, sess, ctr, kind_byte, msg, payload = t p = unhex(payload) # the clear fields; the probe opens it self.assertEqual((p[0], struct.unpack_from('> 8 & 255}.{i & 255}.9' relay.handle(Client(ip=ip).connect(cr.INTENT_CREATE), (ip, 5), cr.SILENT_DROP + 2) a.addr = (a.addr[0], a.addr[1] + 1000) # back from a new port (a new NAT mapping): a cookie first a.seq, a.nonce = a.seq + 1, 12 out = parsed(relay.handle(a.connect(cr.INTENT_JOIN), a.addr, cr.SILENT_DROP + 2)) self.assertEqual([dg.type for to, dg in out], [cr.T_CHALLENGE]) flood = parsed(relay.handle(Client(ip='10.200.0.1').connect(cr.INTENT_CREATE), ('10.200.0.1', 5), cr.SILENT_DROP + 2)) self.assertEqual(flood, []) # the flood's own share is spent def test_a_flood_of_rejoins_with_a_members_token_takes_neither_its_packets_nor_its_own_rejoin(self): """Someone who saw a member's join (its client id and token travel in the clear) sends 250 CONNECTs a second with them and no cookie from another network, for ten seconds. The member's own messages still pass, its rejoin in a new session from its own network still gets a challenge, and the flood gets a few challenges, not hundreds.""" (a, b), secret = self.room_of(2) flood = Client(ip='10.66.0.9', cid=a.cid, secret=secret) flood.token = a.token challenged = passed = sent = 0 for i in range(2500): now = 1 + i / 250 flood.seq, flood.nonce = flood.seq + 1, i challenged += len(self.relay.handle(flood.connect(cr.INTENT_JOIN), flood.addr, now)) if i % 25 == 0 and i >= 2000: # the member's messages in the last two seconds, ten a second out, _ = a.send(self.relay, cr.T_DATA, b'\x01sealed', dst=cr.DST_ALL, now=now) sent, passed = sent + 1, passed + any(to == b.addr for to, d in out) a.session = random.getrandbits(64) # the member joins again in a new session, from its own network a.seq, a.nonce = a.seq + 1, 77 out = parsed(self.relay.handle(a.connect(cr.INTENT_JOIN), a.addr, 11)) self.assertEqual((passed, [dg.type for to, dg in out]), (sent, [cr.T_CHALLENGE])) self.assertLessEqual(challenged, 8 + 2 * 11) def test_the_rejoins_of_rooms_someone_made_to_flood_with_leave_other_rooms_their_share(self): """Someone makes four rooms of eight members and, every half second for four seconds, sends a CONNECT with each of the 32 tokens from the member's own network and one from another, in new sessions: 64 at once, as many as each member's own challenges allow, against a relay that sets 40 a second aside for rejoins. A member of another room who comes back from a new port right after the last of them still gets its challenge: each room has its own share of those set aside.""" relay = cr.Relay(rejoin_challenge_rate=40) (a, b), secret = self.room_of(2, relay=relay) flooders = [] for r in range(4): maker = Client(f'M{r}', ip=f'10.66.{r}.1') maker.join(relay, 0, cr.INTENT_CREATE) flooders.append(maker) for i in range(1, 8): c = Client(f'M{r}{i}', ip=f'10.66.{r}.{i + 1}', secret=maker.secret) c.join(relay, 0, cr.INTENT_JOIN) flooders.append(c) challenged = 0 for step in range(9): now = 1 + step / 2 for m in flooders: for addr in (m.addr, ('10.77.0.9', m.addr[1])): # its own network, and another m.seq, m.nonce, m.session = m.seq + 1, random.getrandbits(64), random.getrandbits(64) challenged += len(relay.handle(m.connect(cr.INTENT_JOIN), addr, now)) a.addr = (a.addr[0], a.addr[1] + 1000) # back from a new port (a new NAT mapping): a cookie first a.seq, a.nonce = a.seq + 1, 99 out = parsed(relay.handle(a.connect(cr.INTENT_JOIN), a.addr, 5)) self.assertEqual(([dg.type for to, dg in out], challenged >= 9 * 8), ([cr.T_CHALLENGE], True)) def full_relay(self, members, rooms=1000): """A relay with rooms open rooms: ours, with members members, each from its own /24, and rooms - 1 others, each with its maker in it, eight to a /24 (the per-network limit). (relay, our members)""" relay = cr.Relay() a = Client('A', ip='10.0.0.1') a.join(relay, 0, cr.INTENT_CREATE) ours = [a] for i in range(1, members): c = Client(f'P{i}', ip=f'10.0.{i}.1') c.join(relay, 0, cr.INTENT_JOIN, a.secret) ours.append(c) for i in range(rooms - 1): net = 100 + i // 8 Client(f'R{i}', ip=f'10.{net >> 8}.{net & 255}.{i % 8 + 1}').join(relay, 0, cr.INTENT_CREATE) self.assertEqual(len(relay.rooms), rooms) return relay, ours def test_a_flood_of_rejoins_with_a_members_token_on_a_full_relay_keeps_neither_its_messages_nor_its_own_rejoin_out(self): """The flood of test_a_flood_of_rejoins_with_a_members_token_takes_neither_its_packets_nor_its_own_rejoin (250 CONNECTs a second with the member's seen token from another network, for ten seconds) on a relay that holds its full 1000 rooms: the member's messages pass, and its rejoin in a new session from its own network gets its challenge.""" relay, (a, b) = self.full_relay(2) flood = Client(ip='10.66.0.9', cid=a.cid, secret=a.secret) flood.token = a.token passed = sent = 0 for i in range(2500): now = 1 + i / 250 flood.seq, flood.nonce = flood.seq + 1, i relay.handle(flood.connect(cr.INTENT_JOIN), flood.addr, now) if i % 25 == 0 and i >= 2000: out, _ = a.send(relay, cr.T_DATA, b'\x01sealed', dst=cr.DST_ALL, now=now) sent, passed = sent + 1, passed + any(to == b.addr for to, d in out) a.session = random.getrandbits(64) a.seq, a.nonce = a.seq + 1, 77 out = parsed(relay.handle(a.connect(cr.INTENT_JOIN), a.addr, 11)) self.assertEqual((passed, [dg.type for to, dg in out]), (sent, [cr.T_CHALLENGE])) def test_a_paced_flood_with_every_token_of_a_room_keeps_no_members_own_rejoin_out_also_on_a_full_relay(self): """Someone who saw the CONNECTs of all eight members of a room sends CONNECTs with each token, in new sessions, from another network at the 2 a second each member's own challenges for other networks allow; and one member of the room sends CONNECTs with its own token from its own network at the 2 a second its own challenges allow. For a minute, on a relay with one room and on one with its full 1000. Member A joins again in a new session from its own network as coop.c does (a burst of four CONNECTs 250 ms apart, then waits of 1, 2, 4, 8 and 15 s) and answers the first challenge: its first CONNECT gets one.""" for rooms in (1, 1000): relay, ours = self.full_relay(8, rooms) a, b = ours[0], ours[1] events = [] # (time, what, index): 0 a seen token from another network, 1 b's own from its network, 2 a for k, m in enumerate(ours): f = Client(ip=f'10.66.{k}.9', cid=m.cid, secret=a.secret) f.token = m.token ours[k] = (m, f) events += [(0.01 + k * 1e-3, 0, k)] * 8 + [(0.5 + k / 16 + j / 2, 0, k) for j in range(120)] own = Client(ip=b.addr[0], cid=b.cid, secret=a.secret) # b's own network, another port own.token = b.token events += [(0.02, 1, 0)] * 8 + [(0.53 + j / 2, 1, 0) for j in range(120)] at, wait = 1.37, 1.0 while at < 60: events += [(at + k * 0.25, 2, 0) for k in range(4)] at += 0.75 + wait wait = min(wait * 2, 15) events.sort() a.session = random.getrandbits(64) first = None for i, (now, what, k) in enumerate(events): if what == 0: f = ours[k][1] f.seq, f.nonce, f.session = f.seq + 1, i, random.getrandbits(64) relay.handle(f.connect(cr.INTENT_JOIN), f.addr, now) elif what == 1: own.seq, own.nonce, own.session = own.seq + 1, i, random.getrandbits(64) relay.handle(own.connect(cr.INTENT_JOIN), own.addr, now) elif first is None: a.seq, a.nonce = a.seq + 1, 1000 + i if [dg.type for to, dg in parsed(relay.handle(a.connect(cr.INTENT_JOIN), a.addr, now))] == [cr.T_CHALLENGE]: first = now self.assertEqual((rooms, first), (rooms, 1.37)) def test_challenges_are_limited_per_network_so_one_cannot_lock_out_the_rest(self): relay = cr.Relay(challenge_rate=100, prefix_challenge_rate=10, prefix_challenge_burst=10) flood = sum(len(relay.handle(Client(ip=f'10.1.0.{i}').connect(cr.INTENT_CREATE), (f'10.1.0.{i}', 5), 0)) for i in range(200)) self.assertEqual(flood, 10) # one /24 gets its share, no more other = Client(ip='10.2.0.1') other.seq = 1 self.assertEqual(len(relay.handle(other.connect(cr.INTENT_CREATE), other.addr, 0)), 1) def test_ipv6_addresses_of_one_48_share_their_limits(self): self.assertEqual(cr.prefix_of(('2001:db8:1:2::1', 5)), cr.prefix_of(('2001:db8:1:2:ffff::9', 6))) self.assertEqual(cr.prefix_of(('2001:db8:1:2::1', 5)), cr.prefix_of(('2001:db8:1:ff03::1', 5))) self.assertNotEqual(cr.prefix_of(('2001:db8:1:2::1', 5)), cr.prefix_of(('2001:db8:2:2::1', 5))) self.assertEqual(cr.prefix_of(('::ffff:10.0.0.7', 5)), cr.prefix_of(('10.0.0.9', 6))) relay = cr.Relay(prefix_challenge_rate=5, prefix_challenge_burst=5) n = sum(len(relay.handle(Client(ip=f'2001:db8::{i:x}').connect(cr.INTENT_CREATE), (f'2001:db8::{i:x}', 7, 0, 0), 0)) for i in range(1, 50)) self.assertEqual(n, 5) def test_rooms_per_network_are_limited(self): relay = cr.Relay(rooms_per_prefix=3, join_burst=100) results = [Client(f'P{i}', ip=f'10.0.0.{i + 1}').join(relay, 0, cr.INTENT_CREATE)[0][0] for i in range(5)] self.assertEqual(results, [1, 1, 1, cr.E_RATE, cr.E_RATE]) self.assertEqual(Client(ip='10.0.1.1').join(relay, 0, cr.INTENT_CREATE)[0][0], 1) def test_one_ipv6_48_opens_no_more_rooms_than_one_network(self): relay = cr.Relay(rooms_per_prefix=3, join_burst=100) results = [Client(f'P{i}', ip=f'2001:db8:7:{i:x}::1').join(relay, 0, cr.INTENT_CREATE)[0][0] for i in range(5)] self.assertEqual(results, [1, 1, 1, cr.E_RATE, cr.E_RATE]) # five /64s of one /48 def test_a_flood_from_many_networks_keeps_the_relay_state_small(self): relay = cr.Relay(challenge_rate=1000) for i in range(20000): # spoofed CONNECTs, each from another /24, over two seconds ip = f'10.{i >> 8 & 255}.{i & 255}.1' c = Client(ip=ip) relay.handle(c.connect(cr.INTENT_CREATE), (ip, 5), i / 10000) self.assertLessEqual(len(relay.challenge_buckets) + len(relay.join_buckets), 4096) relay.tick(2.5) other = Client(ip='192.0.2.1') # and a new player still gets a challenge a moment later self.assertEqual(len(relay.handle(other.connect(cr.INTENT_CREATE), other.addr, 4)), 1) def test_one_room_cannot_take_the_forwarding_budget_of_another(self): relay = cr.Relay(egress_rate=40000, pkt_rate=10000, pkt_burst=10000, byte_rate=1 << 22, byte_burst=1 << 22) busy, _ = self.room_of(8, relay=relay) q1, q2 = Client('Q1', ip='10.9.0.1'), Client('Q2', ip='10.9.1.1') q1.join(relay, 0, cr.INTENT_CREATE) q2.join(relay, 0, cr.INTENT_JOIN, q1.secret) relay.tick(1) got = 0 for t in range(100): # one second: the busy room sends small datagrams as fast as it can, the quiet one 200 bytes every 100 ms now = 2 + t / 100 for c in busy: for _ in range(50): c.send(relay, cr.T_DATA, b'x', dst=busy[0].id if c is not busy[0] else busy[1].id, now=now) if t % 10 == 0: got += len(q1.send(relay, cr.T_DATA, bytes(200), dst=q2.id, now=now)[0]) if t % 25 == 0: relay.tick(now) self.assertEqual(got, 10) def test_idle_rooms_do_not_shrink_the_forwarding_share_of_a_busy_one(self): relay = cr.Relay(egress_rate=80000, max_rooms=2000) for i in range(300): # rooms of one member each that send nothing, each from a network of its own Client(f'I{i}', ip=f'10.{100 + i // 250}.{i % 250}.1').join(relay, 0, cr.INTENT_CREATE) q1, q2 = Client('Q1', ip='10.9.0.1'), Client('Q2', ip='10.9.1.1') q1.join(relay, 0, cr.INTENT_CREATE) q2.join(relay, 0, cr.INTENT_JOIN, q1.secret) relay.tick(1) got = 0 for t in range(100): # one second of 200-byte datagrams every 10 ms: 20 KB/s, a quarter of the relay's budget now = 2 + t / 100 got += len(q1.send(relay, cr.T_DATA, bytes(200), dst=q2.id, now=now)[0]) if t % 25 == 0: relay.tick(now) self.assertGreaterEqual(got, 95, f'{got} of 100 forwarded beside 300 idle rooms') def test_rooms_that_forward_to_nobody_do_not_shrink_the_share_of_a_busy_one(self): relay = cr.Relay(egress_rate=80000, max_rooms=2000) lonely = [] for i in range(300): # rooms of one member each that send DATA nobody receives, each from a network of its own c = Client(f'L{i}', ip=f'10.{100 + i // 250}.{i % 250}.1') c.join(relay, 0, cr.INTENT_CREATE) lonely.append(c) q1, q2 = Client('Q1', ip='10.9.0.1'), Client('Q2', ip='10.9.1.1') q1.join(relay, 0, cr.INTENT_CREATE) q2.join(relay, 0, cr.INTENT_JOIN, q1.secret) relay.tick(1) got = 0 for t in range(100): # one second: each lonely room sends every second, the busy one 200 bytes every 10 ms now = 2 + t / 100 for c in lonely[t::100]: for _ in range(3): c.send(relay, cr.T_DATA, bytes(100), dst=cr.DST_ALL, now=now) got += len(q1.send(relay, cr.T_DATA, bytes(200), dst=q2.id, now=now)[0]) if t % 25 == 0: relay.tick(now) self.assertGreaterEqual(got, 95, f'{got} of 100 forwarded beside 300 rooms that send to nobody') def test_garbage_is_ignored(self): rnd = random.Random(7) for _ in range(3000): data = bytes(rnd.getrandbits(8) for _ in range(rnd.randrange(0, 80))) self.assertEqual(self.relay.handle(data, ('10.0.0.9', 1), 0), []) base = Client().connect(cr.INTENT_CREATE) for _ in range(3000): # a CONNECT with bytes changed: never an exception d = bytearray(base) for _ in range(rnd.randrange(1, 4)): d[rnd.randrange(len(d))] = rnd.getrandbits(8) self.relay.handle(bytes(d), ('10.0.0.9', 1), 0) self.relay.handle(bytes(d[:rnd.randrange(len(d))]), ('10.0.0.9', 1), 0) def test_garbage_from_a_member_is_ignored(self): """DATA, LEAVE, KICK, PING with random payloads and fields, from a member's own address with the room's tag.""" (a, b, c), _ = self.room_of(3) rnd = random.Random(11) for i in range(3000): type_ = rnd.choice([cr.T_DATA, cr.T_KICK, cr.T_PING, cr.T_PONG, cr.T_ROSTER, 0x77]) payload = bytes(rnd.getrandbits(8) for _ in range(rnd.randrange(0, 40))) a.send(self.relay, type_, payload, dst=rnd.choice([0, 1, 2, 3, 9, cr.DST_ALL]), now=1 + i / 1000) self.assertEqual(sorted(m.id for m in self.relay.rooms[a.room].live()), [1, 2, 3]) # a random KICK hit no session def test_log_has_no_secret_locator_or_address(self): a = Client('Alice') a.join(self.relay, 0, cr.INTENT_CREATE) text = '\n'.join(self.log) self.assertIn('Alice joined as member 1', text) for leak in (a.secret, a.secret[:6], cr.room_locator(a.secret).hex()[:8], '10.0.0.1'): self.assertNotIn(leak, text) relay = cr.Relay(log=self.log.append, log_addrs=True) Client('Bob').join(relay, 0, cr.INTENT_CREATE) self.assertIn('10.0.0.1', self.log[-1]) # ---- over real sockets class UdpTest(unittest.TestCase): def setUp(self): self.relay = cr.Relay() self.thread, self.stop, self.port = cr.run_in_thread(self.relay) self.addCleanup(self.thread.join, 2) self.addCleanup(self.stop.set) def sock(self): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(2) s.bind(('127.0.0.1', 0)) self.addCleanup(s.close) return s def exchange(self, s, data): s.sendto(data, ('127.0.0.1', self.port)) return cr.parse(s.recvfrom(2048)[0]) def handshake(self, s, intent, secret, name): loc, cid, sess = cr.room_locator(secret), random.getrandbits(64), random.getrandbits(64) ch = self.exchange(s, cr.pack(cr.T_CONNECT, 0, 0, 0, 1, cr.pack_connect(intent, 0, loc, cid, sess, 1, name), loc)) self.assertEqual(ch.type, cr.T_CHALLENGE) cookie = cr.parse_challenge(ch.payload)[1] return self.exchange(s, cr.pack(cr.T_CONNECT, 0, 0, 0, 1, cr.pack_connect(intent, 0, loc, cid, sess, 1, name, cookie), loc)) def test_two_clients_over_udp(self): a, b = self.sock(), self.sock() secret = cr.random_secret() key = cr.room_locator(secret) ja = self.handshake(a, cr.INTENT_CREATE, secret, b'A') self.assertEqual((ja.type, ja.payload[0]), (cr.T_JOINED, 1)) jb = self.handshake(b, cr.INTENT_JOIN, secret, b'B') self.assertEqual((jb.type, jb.payload[0]), (cr.T_JOINED, 2)) self.assertEqual(cr.parse(a.recvfrom(2048)[0]).type, cr.T_ROSTER) d = cr.pack(cr.T_DATA, ja.room, 1, cr.DST_ALL, 10, b'\x01to everyone', key) a.sendto(d, ('127.0.0.1', self.port)) self.assertEqual(b.recvfrom(2048)[0], d) pong = self.exchange(a, cr.pack(cr.T_PING, ja.room, 1, 0, 12, b'\x01\x02\x03\x04', key)) self.assertEqual((pong.type, pong.payload), (cr.T_PONG, b'\x01\x02\x03\x04')) def test_the_command_line_prints_where_it_listens(self): p = subprocess.Popen([sys.executable, str(RELAY_PY), '--host', '127.0.0.1', '--port', '0'], stdout=subprocess.PIPE, text=True) self.addCleanup(p.stdout.close) self.addCleanup(p.wait, 5) self.addCleanup(p.kill) line = p.stdout.readline() self.assertIn('listening on 127.0.0.1:', line) port = int(line.split('listening on 127.0.0.1:')[1].split()[0]) s = self.sock() loc = cr.room_locator(cr.random_secret()) s.sendto(cr.pack(cr.T_CONNECT, 0, 0, 0, 1, cr.pack_connect(cr.INTENT_CREATE, 0, loc, 1, 2, 3, b'A'), loc), ('127.0.0.1', port)) self.assertEqual(cr.parse(s.recvfrom(2048)[0]).type, cr.T_CHALLENGE) # ---- the C client: --probe-coop against a relay started here EXE = os.environ.get('NX2NAVI_EXE') def free_udp_port(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('127.0.0.1', 0)) port = s.getsockname()[1] s.close() return port class Relay_proc: """tools/coop_relay.py as a process on a fixed port, so that it can be stopped and started again. Its log (with where each network's joins went) is kept across restarts, for a failure's message.""" def __init__(self, port): self.port, self.p, self.log = port, None, [] def _read(self, out): try: for line in out: self.log.append(line.rstrip()) except (OSError, ValueError): pass # closed by stop() def start(self): self.p = subprocess.Popen([sys.executable, str(RELAY_PY), '--host', '127.0.0.1', '--port', str(self.port), '--log-joins', '--log-addrs'], stdout=subprocess.PIPE, text=True) line = self.p.stdout.readline() assert 'listening' in line self.log.append(line.rstrip()) self.reader = threading.Thread(target=self._read, args=(self.p.stdout,), daemon=True) self.reader.start() def stop(self): if self.p: self.p.kill() self.p.wait() self.reader.join(5) self.p.stdout.close() self.p = None self.log.append('(the relay stopped)') @unittest.skipUnless(EXE and Path(EXE).is_file(), 'set NX2NAVI_EXE to a built game executable to run --probe-coop') class ProbeTest(unittest.TestCase): """The probe starts two sessions in one process and joins them through the relay at NX2NAVI_COOP_RELAY. Between its phases it asks for a relay restart on its output and waits for a line on its input.""" def run_probe(self, drop): port = free_udp_port() relay = Relay_proc(port) relay.start() self.addCleanup(relay.stop) env = dict(os.environ, NX2NAVI_COOP_RELAY=f'127.0.0.1:{port}', NX2NAVI_COOP_DROP=str(drop), NX2NAVI_COOP_VECTORS=str(VECTORS), NX2NAVI_COOP_RESTART='stdin') p = subprocess.Popen([EXE, '--probe-coop'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=env) lines = [] deadline = time.monotonic() + 240 for line in p.stdout: lines.append(line.rstrip()) if 'coop-probe: restart the relay' in line: relay.stop() time.sleep(0.3) relay.start() p.stdin.write('restarted\n') p.stdin.flush() if time.monotonic() > deadline: p.kill() break p.stdin.close() p.stdout.close() code = p.wait() text = '\n'.join(lines) if os.environ.get('COOP_TEST_VERBOSE'): print(text) relay_log = "\n--- the relay's log ---\n" + '\n'.join(relay.log[-300:]) self.assertEqual(code, 0, text + relay_log) self.assertIn('coop-probe: passed', text, relay_log) self.assertIn('restart', text) def test_probe_without_loss(self): self.run_probe(0) def test_probe_with_30_percent_loss(self): self.run_probe(30) if __name__ == '__main__': if '--write-vectors' in sys.argv: write_vectors() print(f'wrote {VECTORS}') else: unittest.main()