After the bind the adapter enables XEP-0280 carbons and unwraps sent/received forwarded messages: a message another of our resources sent becomes a normal message event with self=true and the contact as conv, a received one self=false. XEP-0085 chat states are handled before the body: composing emits typing on, paused/active/inactive/gone off. The typing command sends composing or paused to the conversation, and dc_adapter_send_typing() puts it on the core side. tests/xmpp_fake.py checks carbons were enabled, the forwarded message (self, conv bob, text), typing on/off from bob, and that a typing command reaches the server as <composing> addressed to bob. Standalone adapter: all checks ok. The UI does not send typing for adapter accounts yet (adapter account wiring is still open), which the commit documents.
313 lines
11 KiB
Python
Executable File
313 lines
11 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""tests/xmpp_fake.py -- drive dtchat-xmpp against a minimal XMPP server.
|
|
|
|
Usage: tests/xmpp_fake.py [path-to-dtchat-xmpp]
|
|
|
|
The fake server speaks the stream header, SASL PLAIN, resource binding and
|
|
one message in each direction, with TLS turned off (DTCHAT_XMPP_NO_TLS); it
|
|
checks the adapter's events and the stanza that reaches the server.
|
|
"""
|
|
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]
|
|
received = []
|
|
sasl_payload = []
|
|
roster_request_seen = []
|
|
presence_seen = []
|
|
carbons_seen = []
|
|
typing_seen = []
|
|
done = threading.Event()
|
|
|
|
|
|
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
|
|
|
|
def take(marker):
|
|
nonlocal buf
|
|
i = buf.index(marker) + len(marker)
|
|
out, buf = buf[:i], buf[i:]
|
|
return out
|
|
|
|
if not fill_until(b"<stream:stream"):
|
|
return
|
|
take(b"<stream:stream")
|
|
# the header may continue to the end of the tag; send our own stream
|
|
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
|
|
auth = take(b"</auth>")
|
|
m = re.search(rb">([A-Za-z0-9+/=]+)</auth>", auth)
|
|
if m:
|
|
sasl_payload.append(base64.b64decode(m.group(1)))
|
|
conn.sendall(b"<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>")
|
|
buf = b""
|
|
if not fill_until(b"<stream:stream"):
|
|
return
|
|
take(b"<stream:stream")
|
|
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
|
|
iq = take(b"</bind></iq>")
|
|
m = re.search(rb"<iq[^>]*id=['\"]([^'\"]+)", iq)
|
|
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())
|
|
# the client announces presence and asks for the roster; collect both
|
|
# (some of it may already sit in buf behind the bind iq)
|
|
try:
|
|
data = buf
|
|
deadline = time.time() + 3
|
|
while (b"jabber:iq:roster" not in data or b"<presence" not in data) \
|
|
and time.time() < deadline:
|
|
chunk = conn.recv(4096)
|
|
if not chunk:
|
|
break
|
|
data += chunk
|
|
roster_request_seen.append(b"jabber:iq:roster" in data)
|
|
presence_seen.append(b"<presence" in data)
|
|
carbons_seen.append(b"urn:xmpp:carbons:2" in data)
|
|
buf = b""
|
|
except socket.timeout:
|
|
pass
|
|
|
|
# the roster answer and two presence changes for bob
|
|
conn.sendall(b"<iq type='result' id='roster1'>"
|
|
b"<query xmlns='jabber:iq:roster'>"
|
|
b"<item jid='bob@example.org' name='Bob' "
|
|
b"subscription='both'><group>Friends</group></item>"
|
|
b"<item jid='carol@example.org' "
|
|
b"subscription='none'/></query></iq>")
|
|
conn.sendall(b"<presence from='bob@example.org/laptop'>"
|
|
b"<show>away</show><status>at lunch</status></presence>")
|
|
conn.sendall(b"<presence from='bob@example.org/laptop' "
|
|
b"type='unavailable'/>")
|
|
|
|
# another resource sent a message (XEP-0280) ...
|
|
conn.sendall(b"<message from='tester@example.org/phone' "
|
|
b"to='bob@example.org' type='chat'>"
|
|
b"<sent xmlns='urn:xmpp:carbons:2'>"
|
|
b"<forwarded xmlns='urn:xmpp:forward:0'>"
|
|
b"<message from='tester@example.org/phone' "
|
|
b"to='bob@example.org' type='chat'>"
|
|
b"<body>from my phone</body></message>"
|
|
b"</forwarded></sent></message>")
|
|
# ... and bob started and stopped typing (XEP-0085)
|
|
conn.sendall(b"<message from='bob@example.org/laptop' "
|
|
b"to='tester@example.org/dtchat' type='chat'>"
|
|
b"<composing "
|
|
b"xmlns='http://jabber.org/protocol/chatstates'/></message>")
|
|
conn.sendall(b"<message from='bob@example.org/laptop' "
|
|
b"to='tester@example.org/dtchat' type='chat'>"
|
|
b"<paused "
|
|
b"xmlns='http://jabber.org/protocol/chatstates'/></message>")
|
|
|
|
# an incoming message from a contact (after the client is connected)
|
|
time.sleep(0.5)
|
|
conn.sendall(b"<message from='bob@example.org/laptop' type='chat' "
|
|
b"to='tester@example.org/dtchat'>"
|
|
b"<body>hello back</body></message>")
|
|
# wait for our outgoing message, then for the typing command
|
|
try:
|
|
if fill_until(b"</message>"):
|
|
received.append(take(b"</message>").decode("utf-8", "replace"))
|
|
if fill_until(b"</message>"):
|
|
typing_seen.append(take(b"</message>").decode("utf-8",
|
|
"replace"))
|
|
except socket.timeout:
|
|
pass
|
|
done.set()
|
|
time.sleep(0.5)
|
|
try:
|
|
conn.sendall(b"</stream:stream>")
|
|
conn.close()
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
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):
|
|
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 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"})
|
|
|
|
online = False
|
|
message = None
|
|
roster = {}
|
|
presence = []
|
|
typing = []
|
|
carbon_msg = None
|
|
deadline = time.time() + 10
|
|
while time.time() < deadline:
|
|
e = ev(1.0)
|
|
if e is None:
|
|
if roster.get("bob@example.org") and roster.get("carol@example.org") \
|
|
and any(p.get("show") == "offline" for p in presence):
|
|
break
|
|
continue
|
|
if e.get("type") == "state" and e.get("state") == "online":
|
|
online = True
|
|
elif e.get("type") == "message":
|
|
message = e
|
|
elif e.get("type") == "roster":
|
|
contact = e.get("contact", {})
|
|
roster[contact.get("id")] = contact
|
|
elif e.get("type") == "presence":
|
|
presence.append(e)
|
|
elif e.get("type") == "typing":
|
|
typing.append(e)
|
|
if e.get("type") == "message" and e.get("self"):
|
|
carbon_msg = e
|
|
check(online, "online after SASL and bind")
|
|
check(roster_request_seen and roster_request_seen[0],
|
|
"the client asked for the roster")
|
|
check(presence_seen and presence_seen[0],
|
|
"the client announced itself")
|
|
check(roster.get("bob@example.org", {}).get("name") == "Bob",
|
|
"roster item with its name")
|
|
check(roster.get("bob@example.org", {}).get("subscription") == "both",
|
|
"roster item with its subscription")
|
|
check(roster.get("carol@example.org", {}).get("subscription") == "none",
|
|
"second roster item")
|
|
check(roster.get("bob@example.org", {}).get("online") is False,
|
|
"roster starts offline")
|
|
online_ev = [p for p in presence
|
|
if p.get("show") == "online" and p.get("away") == "away"]
|
|
check(bool(online_ev), "presence online with show")
|
|
check(online_ev and online_ev[0].get("status") == "at lunch",
|
|
"presence status")
|
|
check(any(p.get("show") == "offline" and
|
|
p.get("buddy") == "bob@example.org" for p in presence),
|
|
"presence offline")
|
|
check(carbons_seen and carbons_seen[0],
|
|
"the client enabled message carbons")
|
|
check(carbon_msg is not None and
|
|
carbon_msg.get("text") == "from my phone", "carbons message event")
|
|
check(carbon_msg is not None and carbon_msg.get("conv") == "bob@example.org",
|
|
"carbons message is filed under the contact")
|
|
check(any(t.get("on") for t in typing), "incoming typing on")
|
|
check(any(not t.get("on") for t in typing), "incoming typing off")
|
|
check(message is not None, "incoming message event")
|
|
if message is not None:
|
|
check(message.get("conv") == "bob@example.org",
|
|
"conversation is the bare JID")
|
|
check(message.get("text") == "hello back", "message text")
|
|
|
|
send({"type": "send", "conv": "bob@example.org", "text": "hello xmpp",
|
|
"client_id": "c1"})
|
|
sent = None
|
|
deadline = time.time() + 5
|
|
while time.time() < deadline and sent is None:
|
|
e = ev(1.0)
|
|
if e and e.get("type") == "sent":
|
|
sent = e
|
|
check(sent is not None, "sent event")
|
|
check(sasl_payload and sasl_payload[0].startswith(b"\0tester\0"),
|
|
"SASL PLAIN carries the user and password")
|
|
send({"type": "typing", "conv": "bob@example.org", "on": True})
|
|
check(done.wait(5), "server saw the outgoing message and typing")
|
|
check(typing_seen and "composing" in typing_seen[-1] and
|
|
"bob@example.org" in typing_seen[-1],
|
|
"outgoing typing reached the server")
|
|
joined = "\n".join(received)
|
|
check("hello xmpp" in joined, "outgoing stanza reached the server")
|
|
check("to='bob@example.org'" in joined or 'to="bob@example.org"' in joined,
|
|
"stanza is addressed to the contact")
|
|
|
|
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")
|
|
check(proc.wait(timeout=3) == 0, "exit 0")
|
|
check(proc.stderr.read().strip() == "", "no stderr output")
|
|
|
|
if failures:
|
|
print("test_xmpp_live: FAIL (%d)" % len(failures))
|
|
sys.exit(1)
|
|
print("test_xmpp_live: all ok")
|