#!/usr/bin/env python3 """tests/irc_join.py -- the join/part commands (T3.3/T4.4). usage: tests/irc_join.py [path-to-dtchat-irc] The server answers 001, expects JOIN #new from the adapter, sends the self JOIN back (the adapter must report the conversation) and expects PART. """ import json import queue import socket import subprocess import sys import threading import time ADAPTER = sys.argv[1] if len(sys.argv) > 1 else \ "build/mcde-host/programs/dtchat/dtchat-irc" failures = [] joined = [] parted = [] done = threading.Event() def check(cond, what): print(("ok " if cond else "FAIL ") + what, flush=True) if not cond: failures.append(what) srv = socket.socket() srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srv.bind(("127.0.0.1", 0)) srv.listen(1) port = srv.getsockname()[1] def server(): conn, _ = srv.accept() conn.settimeout(0.2) buf = b"" def wait_line(pred, timeout=8): nonlocal buf deadline = time.time() + timeout while time.time() < deadline: while b"\r\n" in buf: line, buf = buf.split(b"\r\n", 1) text = line.decode("utf-8", "replace") if pred(text): return text try: chunk = conn.recv(4096) except socket.timeout: continue if not chunk: return None buf += chunk return None line = wait_line(lambda s: "USER " in s) if line is None: return conn.sendall(b":srv 001 tester :Welcome\r\n") line = wait_line(lambda s: s.startswith("JOIN #new")) joined.append(line is not None) conn.sendall(b":tester!t@h JOIN #new\r\n") line = wait_line(lambda s: s.startswith("PART #new")) parted.append(line is not None) done.set() time.sleep(0.2) conn.close() threading.Thread(target=server, daemon=True).start() proc = subprocess.Popen([ADAPTER, "--stdio"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) q = queue.Queue() def reader(): for line in proc.stdout: line = line.strip() if line: q.put(json.loads(line)) threading.Thread(target=reader, daemon=True).start() def send(o): proc.stdin.write(json.dumps(o) + "\n") proc.stdin.flush() def ev(t=5.0): try: return q.get(timeout=t) except queue.Empty: return None hello = ev() check(hello is not None and hello.get("type") == "hello", "hello arrives") send({"type": "configure", "account": "t", "settings": {"host": "127.0.0.1", "port": port, "tls": False, "nick": "tester", "channels": ""}, "secrets": {}}) send({"type": "connect"}) online = False conversation = None deadline = time.time() + 8 while time.time() < deadline and (not online or conversation is None): e = ev(1.0) if e is None: continue if e.get("type") == "state" and e.get("state") == "online": online = True send({"type": "join", "conv": "#new"}) elif e.get("type") == "conversation": conversation = e check(online, "online") check(joined and joined[0], "the adapter sent JOIN #new") check(conversation is not None and conversation.get("conv") == "#new", "conversation event for the channel") check(conversation is not None and conversation.get("kind") == "channel", "the channel kind") send({"type": "part", "conv": "#new", "reason": "bye"}) check(done.wait(6), "the server saw the part") check(parted and parted[0], "the adapter sent PART #new") send({"type": "disconnect"}) bye = None deadline = time.time() + 3 while time.time() < deadline: e = ev(1.0) if e and e.get("type") == "bye": bye = e break check(bye is not None, "bye on disconnect") try: check(proc.wait(timeout=3) == 0, "exit 0") except subprocess.TimeoutExpired: proc.kill() check(False, "exit 0") check(proc.stderr.read().strip() == "", "no stderr output") if failures: print("test_irc_join: FAIL (%d)" % len(failures)) sys.exit(1) print("test_irc_join: all ok")