qmp_chat_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #!/usr/bin/env python3
  2. """Headless end-to-end test of the chat client against tools/mock_chat.py.
  3. Boots floppy.img/root.img in QEMU with an NE2000 on slirp plus a USB
  4. keyboard and mouse, starts the mock Chat + OIDC server on the host
  5. (reachable from the guest as 10.0.2.2), then drives the chat app with QMP
  6. send-key / input-send-event.
  7. Scenarios (one per run):
  8. full (default) native login, send, live poll + read marker, PgUp, older
  9. paging (Home), conversation switch (Esc, Down), browse +
  10. join (Ctrl+B), new DM (Ctrl+N), sign out (click) and a
  11. relaunch that resumes the saved /share/chat.cfg session
  12. --totp TOTP account: wrong code first, then the right one
  13. --consent login followed by the consent interaction
  14. --passkey passkey-only account: must land on the cookie screen
  15. --wrong wrong password first, then the right one
  16. Screenshots go to --out (PNG). Exit code 0 = pass.
  17. tools/qmp_chat_test.py [--out DIR] [--totp|--consent|--passkey|--wrong]
  18. """
  19. import json
  20. import os
  21. import shutil
  22. import socket
  23. import struct
  24. import subprocess
  25. import sys
  26. import tempfile
  27. import time
  28. import urllib.request
  29. import zlib
  30. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  31. CHAT_PORT = 18080
  32. LOGIN_PORT = 18081
  33. def qmp_recv(s):
  34. buf = b""
  35. while True:
  36. chunk = s.recv(65536)
  37. if not chunk:
  38. raise RuntimeError("QMP connection closed")
  39. buf += chunk
  40. while b"\n" in buf:
  41. line, buf = buf.split(b"\n", 1)
  42. line = line.strip()
  43. if line:
  44. msg = json.loads(line)
  45. if "event" in msg:
  46. continue
  47. return msg
  48. def qmp_cmd(s, execute, arguments=None):
  49. req = {"execute": execute}
  50. if arguments is not None:
  51. req["arguments"] = arguments
  52. s.sendall((json.dumps(req) + "\n").encode())
  53. msg = qmp_recv(s)
  54. if "error" in msg:
  55. print("QMP error:", msg)
  56. return msg
  57. KEYMAP = {" ": ["spc"], ":": ["shift", "semicolon"], "/": ["slash"],
  58. ".": ["dot"], "-": ["minus"], "_": ["shift", "minus"],
  59. "\n": ["ret"], "\t": ["tab"], ",": ["comma"], "!": ["shift", "1"],
  60. "@": ["shift", "2"], "?": ["shift", "slash"], "'": ["apostrophe"]}
  61. def key_list(ch):
  62. if ch in KEYMAP:
  63. return KEYMAP[ch]
  64. if ch.isdigit() or ("a" <= ch <= "z"):
  65. return [ch]
  66. if "A" <= ch <= "Z":
  67. return ["shift", ch.lower()]
  68. raise ValueError("no qcode for %r" % ch)
  69. def send_keys(s, keys, delay=0.12):
  70. qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": k} for k in keys]})
  71. time.sleep(delay)
  72. def type_text(s, text):
  73. for ch in text:
  74. send_keys(s, key_list(ch))
  75. def ppm_to_png(ppm, png):
  76. data = open(ppm, "rb").read()
  77. parts = data.split(b"\n", 3)
  78. w, h = [int(x) for x in parts[1].split()]
  79. px = parts[3]
  80. raw = b"".join(b"\x00" + px[y * w * 3:(y + 1) * w * 3] for y in range(h))
  81. def chunk(tag, body):
  82. return (struct.pack(">I", len(body)) + tag + body +
  83. struct.pack(">I", zlib.crc32(tag + body) & 0xffffffff))
  84. with open(png, "wb") as f:
  85. f.write(b"\x89PNG\r\n\x1a\n")
  86. f.write(chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0)))
  87. f.write(chunk(b"IDAT", zlib.compress(raw, 6)))
  88. f.write(chunk(b"IEND", b""))
  89. def move(s, dx, dy):
  90. while dx or dy:
  91. ex = max(-100, min(100, dx))
  92. ey = max(-100, min(100, dy))
  93. qmp_cmd(s, "input-send-event", {"events": [
  94. {"type": "rel", "data": {"axis": "x", "value": ex}},
  95. {"type": "rel", "data": {"axis": "y", "value": ey}}]})
  96. dx -= ex
  97. dy -= ey
  98. time.sleep(0.05)
  99. def click(s, x, y):
  100. """Move to the top-left corner (the cursor clamps), then to x,y."""
  101. move(s, -3000, -3000)
  102. time.sleep(0.2)
  103. move(s, x, y)
  104. time.sleep(0.3)
  105. for down in (True, False):
  106. qmp_cmd(s, "input-send-event", {"events": [
  107. {"type": "btn", "data": {"down": down, "button": "left"}}]})
  108. time.sleep(0.2)
  109. def shot(s, workdir, out, name):
  110. ppm = os.path.join(workdir, "s.ppm")
  111. if os.path.exists(ppm):
  112. os.unlink(ppm)
  113. qmp_cmd(s, "screendump", {"filename": ppm})
  114. for _ in range(50):
  115. if os.path.exists(ppm) and os.path.getsize(ppm) > 100:
  116. break
  117. time.sleep(0.1)
  118. time.sleep(0.3)
  119. png = os.path.join(out, name + ".png")
  120. ppm_to_png(ppm, png)
  121. print("screenshot:", png)
  122. def mock_get(path):
  123. with urllib.request.urlopen("http://127.0.0.1:%d%s" % (CHAT_PORT, path),
  124. timeout=5) as r:
  125. return json.loads(r.read().decode())
  126. def mock_post(path, body):
  127. req = urllib.request.Request("http://127.0.0.1:%d%s" % (CHAT_PORT, path),
  128. data=json.dumps(body).encode(),
  129. headers={"Content-Type": "application/json"})
  130. with urllib.request.urlopen(req, timeout=5) as r:
  131. return json.loads(r.read().decode())
  132. def wait_for(pred, timeout, what):
  133. end = time.time() + timeout
  134. while time.time() < end:
  135. try:
  136. if pred():
  137. return True
  138. except Exception:
  139. pass
  140. time.sleep(0.5)
  141. print("TIMEOUT waiting for", what)
  142. return False
  143. def main():
  144. out = os.path.join(ROOT, "chat-test-out")
  145. if "--out" in sys.argv:
  146. out = sys.argv[sys.argv.index("--out") + 1]
  147. os.makedirs(out, exist_ok=True)
  148. totp = "--totp" in sys.argv
  149. consent = "--consent" in sys.argv
  150. passkey = "--passkey" in sys.argv
  151. wrong = "--wrong" in sys.argv
  152. full = not (totp or consent or passkey or wrong)
  153. keep = "--keep" in sys.argv
  154. mocklog = os.path.join(out, "mock.log")
  155. def logged(sub):
  156. return any(sub in l for l in open(mocklog))
  157. for f in ("floppy.img", "root.img"):
  158. if not os.path.exists(os.path.join(ROOT, f)):
  159. print("%s missing - run `make all` first" % f)
  160. return 1
  161. workdir = tempfile.mkdtemp(prefix="nebos-chat-")
  162. sock = os.path.join(workdir, "qmp.sock")
  163. serial = os.path.join(out, "serial.log")
  164. mock = subprocess.Popen([sys.executable, os.path.join(ROOT, "tools", "mock_chat.py"),
  165. "--chat-port", str(CHAT_PORT),
  166. "--login-port", str(LOGIN_PORT), "-v"],
  167. stdout=open(os.path.join(out, "mock.log"), "w"),
  168. stderr=subprocess.STDOUT)
  169. qemu = ["qemu-system-i386", "-M", "pc", "-m", "512",
  170. "-device", "pci-ohci", "-device", "usb-mouse", "-device", "usb-kbd",
  171. "-device", "ne2k_pci,netdev=n0", "-netdev", "user,id=n0",
  172. "-drive", "file=floppy.img,format=raw,if=floppy",
  173. "-drive", "file=root.img,format=raw,if=floppy",
  174. "-display", "none", "-serial", "file:" + serial,
  175. "-qmp", "unix:%s,server,nowait" % sock]
  176. proc = subprocess.Popen(qemu, cwd=ROOT, stdout=subprocess.DEVNULL,
  177. stderr=subprocess.DEVNULL)
  178. results = []
  179. def check(name, ok):
  180. results.append((name, ok))
  181. print("%-40s %s" % (name, "ok" if ok else "FAIL"), flush=True)
  182. return ok
  183. s = None
  184. try:
  185. for _ in range(100):
  186. if os.path.exists(sock):
  187. break
  188. time.sleep(0.1)
  189. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  190. s.connect(sock)
  191. qmp_recv(s)
  192. qmp_cmd(s, "qmp_capabilities")
  193. wait_for(lambda: b"booted" in open(serial, "rb").read(), 60, "boot line")
  194. time.sleep(12.0) # WM, terminal, DHCP
  195. shot(s, workdir, out, "0-desktop")
  196. type_text(s, "chat\n")
  197. time.sleep(6.0)
  198. shot(s, workdir, out, "1-server")
  199. send_keys(s, ["ctrl", "u"])
  200. type_text(s, "http://10.0.2.2:%d\n" % CHAT_PORT)
  201. check("login form fetched",
  202. wait_for(lambda: any("interaction" in l and '" 200' in l
  203. for l in open(os.path.join(out, "mock.log"))),
  204. 30, "login page"))
  205. time.sleep(2.0)
  206. shot(s, workdir, out, "2-credentials")
  207. user = "totp" if totp else "consent" if consent else \
  208. "passkey" if passkey else "test"
  209. if wrong:
  210. type_text(s, user + "\t" + "nope\n")
  211. check("wrong password rejected",
  212. wait_for(lambda: logged("login attempt user='test'") and
  213. logged('/login HTTP/1.1" 401'), 30, "401"))
  214. time.sleep(2.0)
  215. shot(s, workdir, out, "2a-wrong")
  216. # the password field is cleared and focused
  217. type_text(s, "test\n")
  218. else:
  219. type_text(s, user + "\t" + "test" + "\n")
  220. if passkey:
  221. check("passkey falls back to cookie screen",
  222. wait_for(lambda: logged("login attempt user='passkey'"), 30,
  223. "passkey attempt"))
  224. time.sleep(3.0)
  225. shot(s, workdir, out, "2c-passkey")
  226. check("no session for passkey account",
  227. "u-passkey" not in mock_get("/debug/state")["sessions"].values())
  228. raise StopIteration
  229. if totp:
  230. time.sleep(5.0)
  231. shot(s, workdir, out, "2b-totp")
  232. type_text(s, "654321\n")
  233. check("wrong TOTP rejected",
  234. wait_for(lambda: logged("totp='654321'"), 30, "bad totp"))
  235. time.sleep(3.0)
  236. shot(s, workdir, out, "2b-totp-wrong")
  237. type_text(s, "123456\n")
  238. check("session established",
  239. wait_for(lambda: ("u-" + user) in mock_get("/debug/state")["sessions"].values(),
  240. 40, "session"))
  241. if consent:
  242. check("consent confirmed", logged("/confirm HTTP/1.1\" 303"))
  243. check("history loaded",
  244. wait_for(lambda: logged("/api/channels/c-general/messages?limit=50"),
  245. 30, "history"))
  246. time.sleep(4.0)
  247. shot(s, workdir, out, "3-main")
  248. if not full:
  249. raise StopIteration
  250. type_text(s, "Hello from NebOS, it works!\n")
  251. check("message sent",
  252. wait_for(lambda: any(m["body"] == "Hello from NebOS, it works!" and
  253. m["userId"] == "u-" + user and
  254. (m["clientId"] or "").startswith("nebos-")
  255. for m in mock_get("/debug/state")["messages"]["c-general"]),
  256. 30, "sent message"))
  257. time.sleep(3.0)
  258. shot(s, workdir, out, "4-sent")
  259. injected = mock_post("/debug/inject", {"channel": "c-general", "user": "u-alice",
  260. "body": "Live reply from Alice \u2013 polled"})
  261. check("live message read",
  262. wait_for(lambda: mock_get("/debug/state")["reads"].get(
  263. "c-general|u-" + user) == injected["id"], 30, "read marker"))
  264. time.sleep(1.5)
  265. shot(s, workdir, out, "5-live")
  266. for _ in range(4):
  267. send_keys(s, ["pgup"], 0.4)
  268. time.sleep(2.0)
  269. shot(s, workdir, out, "6-scrolled")
  270. send_keys(s, ["esc"])
  271. send_keys(s, ["home"])
  272. check("older page loaded",
  273. wait_for(lambda: logged("messages?limit=50&before="), 30, "older"))
  274. time.sleep(3.0)
  275. shot(s, workdir, out, "7-older")
  276. send_keys(s, ["end"])
  277. send_keys(s, ["down"])
  278. check("switched to #secret",
  279. wait_for(lambda: logged("/api/channels/c-secret/messages?limit=50") and
  280. logged("POST /api/channels/c-secret/read"), 30, "secret"))
  281. time.sleep(2.0)
  282. shot(s, workdir, out, "8-secret")
  283. send_keys(s, ["ctrl", "b"])
  284. check("public channels listed",
  285. wait_for(lambda: logged("GET /api/channels/public"), 20, "browse"))
  286. time.sleep(2.0)
  287. shot(s, workdir, out, "9-browse")
  288. send_keys(s, ["down"]) # sorted: design, random
  289. send_keys(s, ["ret"])
  290. check("joined #random",
  291. wait_for(lambda: "u-test" in mock_get("/debug/state")["channels"]
  292. ["c-random"]["members"], 20, "join"))
  293. time.sleep(3.0)
  294. shot(s, workdir, out, "10-joined")
  295. send_keys(s, ["ctrl", "n"])
  296. check("users listed",
  297. wait_for(lambda: logged("GET /api/users"), 20, "users"))
  298. type_text(s, "Bob")
  299. time.sleep(1.0)
  300. shot(s, workdir, out, "11-newdm")
  301. send_keys(s, ["ret"])
  302. check("DM with Bob created",
  303. wait_for(lambda: any(c["type"] == "dm" and
  304. sorted(c["members"]) == ["u-bob", "u-test"]
  305. for c in mock_get("/debug/state")["channels"].values()),
  306. 20, "dm"))
  307. time.sleep(3.0)
  308. shot(s, workdir, out, "12-dm")
  309. type_text(s, "Hi Bob\n")
  310. check("DM message sent",
  311. wait_for(lambda: any(m["body"] == "Hi Bob"
  312. for cid, ms in mock_get("/debug/state")["messages"].items()
  313. if cid.startswith("d-") for m in ms), 20, "dm msg"))
  314. # Close the window (title-bar x), relaunch: the saved session in
  315. # /share/chat.cfg must log in without the password.
  316. click(s, 1028, 81)
  317. time.sleep(2.0)
  318. click(s, 200, 300) # focus the terminal
  319. time.sleep(1.0)
  320. n_me = sum(1 for l in open(mocklog) if "GET /api/me" in l)
  321. type_text(s, "chat\n")
  322. check("saved session resumed",
  323. wait_for(lambda: sum(1 for l in open(mocklog) if "GET /api/me" in l) > n_me and
  324. sum(1 for l in open(mocklog) if "GET /api/channels HTTP" in l) >= 2,
  325. 40, "resume"))
  326. time.sleep(4.0)
  327. shot(s, workdir, out, "13-resumed")
  328. click(s, 987, 112) # Sign out
  329. check("signed out",
  330. wait_for(lambda: logged("GET /auth/logout"), 20, "logout"))
  331. time.sleep(2.0)
  332. shot(s, workdir, out, "14-signed-out")
  333. except StopIteration:
  334. pass # scenario finished early
  335. finally:
  336. try:
  337. qmp_cmd(s, "quit")
  338. proc.wait(timeout=10)
  339. except Exception:
  340. pass
  341. if proc.poll() is None:
  342. proc.kill()
  343. proc.wait()
  344. mock.terminate()
  345. mock.wait()
  346. if not keep:
  347. shutil.rmtree(workdir, ignore_errors=True)
  348. failed = [n for n, ok in results if not ok]
  349. print("chat e2e: %s" % ("PASS" if results and not failed else "FAIL " + ", ".join(failed)))
  350. return 0 if results and not failed else 1
  351. if __name__ == "__main__":
  352. sys.exit(main())