Files
lotherk d063dc0d7b dtchat: restart the XMPP adapter after an unexpected stream end (T4.6)
conn_handler used to send a bye and exit 0 on any disconnect, which the core reads as an intentional stop. Now an unexpected end of the stream (or a failed connection) reports offline, sends no bye and exits 1: DcAdapter's existing restart policy starts the process again with backoff and the hello/configure/connect handshake repeats (the policy is covered by DtChat/test_adapter). A requested disconnect still leaves through handle_command with quitting set, bye and status 0. tests/xmpp_drop.py closes the stream after the bind and checks status 1, the offline state and no bye; xmpp_fake.py and xmpp_muc.py still pass, all with the standalone adapter. In-tree build/tests follow.
2026-11-17 10:34:28 +00:00

166 lines
5.0 KiB
Python
Executable File

#!/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"<stream:stream"):
return
conn.sendall(b"<?xml version='1.0'?><stream:stream "
b"xmlns='jabber:client' "
b"xmlns:stream='http://etherx.jabber.org/streams' "
b"id='s1' from='example.org' version='1.0'>")
conn.sendall(b"<stream:features><mechanisms "
b"xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
b"<mechanism>PLAIN</mechanism></mechanisms>"
b"</stream:features>")
if not fill_until(b"</auth>"):
return
conn.sendall(b"<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>")
buf = b""
if not fill_until(b"<stream:stream"):
return
conn.sendall(b"<?xml version='1.0'?><stream:stream "
b"xmlns='jabber:client' "
b"xmlns:stream='http://etherx.jabber.org/streams' "
b"id='s2' from='example.org' version='1.0'>")
conn.sendall(b"<stream:features><bind "
b"xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
b"</stream:features>")
if not fill_until(b"</bind></iq>"):
return
m = re.search(rb"<iq[^>]*id=['\"]([^'\"]+)", buf)
iqid = m.group(1).decode() if m else "1"
conn.sendall(("<iq type='result' id='%s'><bind "
"xmlns='urn:ietf:params:xml:ns:xmpp-bind'>"
"<jid>tester@example.org/dtchat</jid></bind></iq>" %
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")