An incoming messageDocument is remembered with its TDLib file id; file-accept sends downloadFile (priority 1, whole file) and updateFile is followed until local.is_downloading_completed, then the local file is read in 4 KiB blocks and sent as file-data with a file-end carrying the SHA-256 computed with dc_file_sha256. Unknown offers answer file-error. The fake mode can offer a real local file through DTCHAT_TELEGRAM_FAKE_FILE and exercises the same read/hash/chunk/event path; tests/telegram_fake.py now uses a 9000-byte file, checks the offer size and SHA-256, reassembles the chunks byte-for-byte and verifies the file-end hash (25 checks, green stand-alone). The adapter links CRYPTOLIB for EVP/dc_file_sha256. The TDLib downloadFile/updateFile branch is compiled but not exercised without an account. Full build and harness follow.
207 lines
6.3 KiB
Python
Executable File
207 lines
6.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""tests/telegram_fake.py -- the Telegram adapter skeleton (T8.1).
|
|
|
|
usage: tests/telegram_fake.py [path-to-dtchat-telegram]
|
|
|
|
DTCHAT_TELEGRAM_FAKE=1 replaces TDLib with the authorization request and an
|
|
echo, so the IPC path (hello, configure, connect, request/answer, send,
|
|
disconnect) is tested without credentials or network.
|
|
"""
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import queue
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
ADAPTER = sys.argv[1] if len(sys.argv) > 1 else \
|
|
"build/mcde-host/programs/dtchat/dtchat-telegram"
|
|
failures = []
|
|
|
|
|
|
def check(cond, what):
|
|
print(("ok " if cond else "FAIL ") + what, flush=True)
|
|
if not cond:
|
|
failures.append(what)
|
|
|
|
|
|
PAYLOAD = bytes((i * 37 + 11) & 0xff for i in range(9000))
|
|
FILE = "/tmp/dtchat-telegram-file.bin"
|
|
with open(FILE, "wb") as f:
|
|
f.write(PAYLOAD)
|
|
proc = subprocess.Popen([ADAPTER, "--stdio"], stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
text=True,
|
|
env={"PATH": "/usr/bin:/bin",
|
|
"DTCHAT_TELEGRAM_FAKE": "1",
|
|
"DTCHAT_TELEGRAM_FAKE_FILE": FILE})
|
|
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")
|
|
check(hello.get("ipc") == 1, "ipc is 1")
|
|
check(hello.get("protocol") == "telegram", "protocol is telegram")
|
|
keys = [f.get("key") for f in (hello or {}).get("settings", [])]
|
|
check(keys == ["api_id", "api_hash", "phone", "database"],
|
|
"settings schema")
|
|
check("qr" in (hello or {}).get("caps", []), "caps name the QR flow")
|
|
|
|
send({"type": "configure", "account": "t",
|
|
"settings": {"api_id": 12345, "phone": "+15550001",
|
|
"database": "/tmp/dtchat-telegram-test"},
|
|
"secrets": {"api_hash": "secret"}})
|
|
send({"type": "connect"})
|
|
|
|
state = None
|
|
request = None
|
|
deadline = time.time() + 5
|
|
while time.time() < deadline and (state != "connecting" or request is None):
|
|
e = ev(1.0)
|
|
if e is None:
|
|
continue
|
|
if e.get("type") == "state":
|
|
state = e.get("state")
|
|
elif e.get("type") == "request":
|
|
request = e
|
|
check(state == "connecting", "state connecting")
|
|
check(request is not None and request.get("kind") == "code",
|
|
"a code is requested")
|
|
if request is not None:
|
|
check(request.get("secret") is True, "the code is secret")
|
|
send({"type": "answer", "id": request.get("id"), "value": "12345"})
|
|
|
|
online = False
|
|
deadline = time.time() + 5
|
|
while time.time() < deadline and not online:
|
|
e = ev(1.0)
|
|
if e and e.get("type") == "state" and e.get("state") == "online":
|
|
online = True
|
|
check(online, "online after the code")
|
|
|
|
roster = {}
|
|
offer = None
|
|
deadline = time.time() + 5
|
|
while time.time() < deadline and (len(roster) < 2 or offer is None):
|
|
e = ev(1.0)
|
|
if e is None:
|
|
continue
|
|
if e.get("type") == "roster":
|
|
roster[e.get("contact", {}).get("id")] = e["contact"]
|
|
elif e.get("type") == "file-offer":
|
|
offer = e
|
|
check(roster.get("1", {}).get("name") == "Alice", "roster has Alice")
|
|
check(roster.get("2", {}).get("name") == "Bob", "roster has Bob")
|
|
check(roster.get("1", {}).get("online") is True, "Alice is online")
|
|
check(offer is not None and offer.get("name") == "dtchat-telegram-file.bin",
|
|
"a file offer arrives")
|
|
if offer is not None:
|
|
check(offer.get("size") == len(PAYLOAD), "the offer has the size")
|
|
check(offer.get("sha256") == hashlib.sha256(PAYLOAD).hexdigest(),
|
|
"the offer has the SHA-256")
|
|
send({"type": "file-accept", "id": offer.get("id")})
|
|
chunks = []
|
|
end = None
|
|
deadline = time.time() + 5
|
|
while time.time() < deadline:
|
|
e = ev(1.0)
|
|
if e is None:
|
|
continue
|
|
if e.get("type") == "file-data":
|
|
chunks.append(base64.b64decode(e.get("data", "")))
|
|
elif e.get("type") == "file-end":
|
|
end = e
|
|
break
|
|
received = b"".join(chunks)
|
|
check(received == PAYLOAD, "the downloaded bytes arrive unchanged")
|
|
check(end is not None and end.get("size") == len(PAYLOAD), "file end size")
|
|
check(end is not None and
|
|
end.get("sha256") == hashlib.sha256(PAYLOAD).hexdigest(),
|
|
"file end SHA-256")
|
|
|
|
send({"type": "typing", "conv": "1", "on": True})
|
|
typing = None
|
|
deadline = time.time() + 3
|
|
while time.time() < deadline and typing is None:
|
|
e = ev(1.0)
|
|
if e and e.get("type") == "typing":
|
|
typing = e
|
|
check(typing is not None and typing.get("on") is True,
|
|
"typing event from the conversation")
|
|
|
|
send({"type": "file-send", "conv": "1", "id": "f2",
|
|
"path": "/tmp/opencode/sendme.txt", "name": "sendme.txt"})
|
|
sending = None
|
|
deadline = time.time() + 3
|
|
while time.time() < deadline and sending is None:
|
|
e = ev(1.0)
|
|
if e and e.get("type") == "file-sent" and e.get("id") == "f2":
|
|
sending = e
|
|
check(sending is not None, "file-sent for an outgoing file")
|
|
|
|
send({"type": "send", "conv": "42", "text": "hallo telegram",
|
|
"client_id": "c1"})
|
|
sent = message = None
|
|
deadline = time.time() + 5
|
|
while time.time() < deadline and (sent is None or message is None):
|
|
e = ev(1.0)
|
|
if e is None:
|
|
continue
|
|
if e.get("type") == "sent":
|
|
sent = e
|
|
elif e.get("type") == "message":
|
|
message = e
|
|
check(sent is not None and sent.get("client_id") == "c1",
|
|
"sent event with the client id")
|
|
check(message is not None and message.get("conv") == "42",
|
|
"the message is filed under the conversation")
|
|
check(message is not None and message.get("text") == "hallo telegram",
|
|
"the message text")
|
|
|
|
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")
|
|
os.unlink(FILE)
|
|
|
|
if failures:
|
|
print("test_telegram_fake: FAIL (%d)" % len(failures))
|
|
sys.exit(1)
|
|
print("test_telegram_fake: all ok")
|