diff --git a/programs/dtchat/xmpp_adapter.c b/programs/dtchat/xmpp_adapter.c index dcac4741..c3b8ec8f 100644 --- a/programs/dtchat/xmpp_adapter.c +++ b/programs/dtchat/xmpp_adapter.c @@ -660,8 +660,13 @@ conn_handler(xmpp_conn_t *c, xmpp_conn_event_t event, int error, send_error("network", "the XMPP connection failed", 0); } send_state("offline", NULL); - send_bye(error != 0 ? "failed" : "disconnected"); - _exit(0); + /* + * An unexpected end of the stream is not a goodbye: exit non-zero + * without a bye, and the core (DcAdapter) restarts the process with + * its backoff and sends hello/configure/connect again. A requested + * disconnect leaves through handle_command with quitting set. + */ + _exit(1); } static int diff --git a/tests/xmpp_drop.py b/tests/xmpp_drop.py new file mode 100755 index 00000000..23b171de --- /dev/null +++ b/tests/xmpp_drop.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 +"""tests/xmpp_drop.py -- an unexpected stream end is not a goodbye (T4.6). + +usage: tests/xmpp_drop.py [path-to-dtchat-xmpp] + +A minimal XMPP server answers the stream, SASL PLAIN and the bind and then +closes the socket without a closing stream tag. The adapter must report the +offline state, exit with status 1 and send no bye: the core's DcAdapter sees +the non-zero exit and restarts the process with its backoff (the restart +policy itself is covered by DtChat/test_adapter). +""" +import base64 +import json +import queue +import re +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-xmpp" +failures = [] + + +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(6) + buf = b"" + + def fill_until(marker): + nonlocal buf + while marker not in buf: + chunk = conn.recv(4096) + if not chunk: + return False + buf += chunk + return True + + if not fill_until(b"") + conn.sendall(b"" + b"PLAIN" + b"") + if not fill_until(b""): + return + conn.sendall(b"") + buf = b"" + if not fill_until(b"") + conn.sendall(b"" + b"") + if not fill_until(b""): + return + m = re.search(rb"]*id=['\"]([^'\"]+)", buf) + iqid = m.group(1).decode() if m else "1" + conn.sendall(("" + "tester@example.org/dtchat" % + iqid).encode()) + # let the client finish its hello/configure/connect and go online + time.sleep(1.0) + 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, env={"PATH": "/usr/bin:/bin", + "DTCHAT_XMPP_NO_TLS": "1"}) +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): + try: + proc.stdin.write(json.dumps(o) + "\n") + proc.stdin.flush() + except BrokenPipeError: + pass + + +hello = None +deadline = time.time() + 5 +while time.time() < deadline: + try: + hello = q.get(timeout=1) + except queue.Empty: + continue + if hello.get("type") == "hello": + break +check(hello is not None and hello.get("type") == "hello", "hello arrives") +send({"type": "configure", "account": "t", + "settings": {"jid": "tester@example.org", "resource": "dtchat", + "host": "127.0.0.1", "port": port}, + "secrets": {"password": "secret"}}) +send({"type": "connect"}) + +events = [] +online = False +deadline = time.time() + 8 +while time.time() < deadline: + try: + e = q.get(timeout=1) + except queue.Empty: + if proc.poll() is not None: + break + continue + events.append(e) + if e.get("type") == "state": + if e.get("state") == "online": + online = True + if e.get("state") == "offline" and online: + break +check(online, "online after SASL and bind") + +try: + rc = proc.wait(timeout=8) +except subprocess.TimeoutExpired: + proc.kill() + rc = None +check(rc == 1, "exit status 1 after the stream ended (%r)" % rc) +check(any(e.get("type") == "state" and e.get("state") == "offline" + for e in events), "offline state was reported") +check(not any(e.get("type") == "bye" for e in events), + "no bye for an unexpected end") +check(proc.stderr.read().strip() == "", "no stderr output") + +if failures: + print("test_xmpp_drop: FAIL (%d)" % len(failures)) + sys.exit(1) +print("test_xmpp_drop: all ok")