qmp_cursor_test.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env python3
  2. """Headless USB HID cursor regression test (QEMU + QMP + screendump).
  3. Boots the OS with a USB mouse, injects motion through QMP and verifies the
  4. on-screen cursor triangle lands where the injected deltas say it should.
  5. Modes:
  6. (default) OHCI controller, mouse on it
  7. --uhci UHCI (piix3) controller
  8. --uhci72 UHCI at PCI 00:01.2 (multifunction south-bridge layout)
  9. --multi three OHCI controllers, mouse on the third
  10. --xhci xHCI controller, mouse on it
  11. --ehci EHCI + UHCI companion, mouse on the companion (multi-HCD)
  12. --hotplug boot without the mouse, plug it in, move, unplug, replug
  13. Requires: floppy.img/root.img built (make all), python3, QEMU.
  14. Exit code 0 = pass.
  15. """
  16. import json
  17. import os
  18. import shutil
  19. import socket
  20. import subprocess
  21. import sys
  22. import tempfile
  23. import time
  24. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  25. # 16x16 cursor fill rows (same table as cursor_shape.h); bit 15 = column 0.
  26. CUR = [0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
  27. 0x7F80, 0x7E00, 0x7000, 0x4000, 0x0000, 0x0000, 0x0000, 0x0000]
  28. def qmp_connect(sock):
  29. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  30. s.connect(sock)
  31. qmp_recv(s) # greeting
  32. qmp_cmd(s, "qmp_capabilities")
  33. return s
  34. def qmp_recv(s):
  35. buf = b""
  36. while True:
  37. chunk = s.recv(4096)
  38. if not chunk:
  39. raise RuntimeError("QMP connection closed")
  40. buf += chunk
  41. while b"\n" in buf:
  42. line, buf = buf.split(b"\n", 1)
  43. line = line.strip()
  44. if line:
  45. return json.loads(line)
  46. def qmp_cmd(s, execute, arguments=None):
  47. req = {"execute": execute}
  48. if arguments is not None:
  49. req["arguments"] = arguments
  50. s.sendall((json.dumps(req) + "\n").encode())
  51. while True:
  52. msg = qmp_recv(s)
  53. if "return" in msg or "error" in msg:
  54. if "error" in msg:
  55. print("QMP error:", msg)
  56. return msg
  57. def read_ppm(path):
  58. data = open(path, "rb").read()
  59. parts = data.split(b"\n", 3)
  60. w, h = [int(x) for x in parts[1].split()]
  61. return w, h, parts[3]
  62. def pixel(w, pix, x, y):
  63. o = (y * w + x) * 3
  64. return pix[o], pix[o + 1], pix[o + 2]
  65. def cursor_matches(w, pix, x0, y0):
  66. for r in range(16):
  67. for c in range(16):
  68. want = (CUR[r] & (0x8000 >> c)) != 0
  69. got = pixel(w, pix, x0 + c, y0 + r) == (255, 255, 255)
  70. if want != got:
  71. return False
  72. return True
  73. def find_cursor(w, h, pix):
  74. for y in range(h - 16):
  75. for x in range(w - 16):
  76. if cursor_matches(w, pix, x, y):
  77. return (x, y)
  78. return None
  79. def screendump(s, ppm):
  80. if os.path.exists(ppm):
  81. os.unlink(ppm)
  82. qmp_cmd(s, "screendump", {"filename": ppm})
  83. time.sleep(0.5)
  84. return read_ppm(ppm)
  85. def move(s, dx, dy):
  86. """Inject relative motion, split so no 8-bit HID field wraps."""
  87. while dx or dy:
  88. ex = max(-100, min(100, dx))
  89. ey = max(-100, min(100, dy))
  90. qmp_cmd(s, "input-send-event", {"events": [
  91. {"type": "rel", "data": {"axis": "x", "value": ex}},
  92. {"type": "rel", "data": {"axis": "y", "value": ey}}]})
  93. dx -= ex
  94. dy -= ey
  95. time.sleep(0.3)
  96. def main():
  97. mode = "ohci"
  98. for m in ("uhci", "uhci72", "multi", "hotplug", "xhci", "ehci"):
  99. if "--" + m in sys.argv:
  100. mode = m
  101. if not (os.path.exists(os.path.join(ROOT, "floppy.img")) and
  102. os.path.exists(os.path.join(ROOT, "root.img"))):
  103. print("floppy.img/root.img missing - run `make all` first")
  104. return 1
  105. workdir = tempfile.mkdtemp(prefix="nebos-cursor-")
  106. sock = os.path.join(workdir, "qmp.sock")
  107. ppm = os.path.join(workdir, "screen.ppm")
  108. qemu = ["qemu-system-i386", "-M", "pc"]
  109. if mode == "uhci":
  110. qemu += ["-device", "piix3-usb-uhci", "-device", "usb-mouse"]
  111. elif mode == "uhci72":
  112. qemu += ["-device", "piix3-usb-uhci,addr=1.2", "-device", "usb-mouse"]
  113. elif mode == "multi":
  114. qemu += ["-device", "pci-ohci,id=o0", "-device", "pci-ohci,id=o1",
  115. "-device", "pci-ohci,id=o2", "-device", "usb-mouse,bus=o2.0"]
  116. elif mode == "hotplug":
  117. qemu += ["-device", "pci-ohci"]
  118. elif mode == "xhci":
  119. qemu += ["-device", "qemu-xhci", "-device", "usb-mouse"]
  120. elif mode == "ehci":
  121. qemu += ["-device", "usb-ehci,id=ehci0",
  122. "-device", "piix3-usb-uhci,id=uhci0",
  123. "-device", "usb-mouse,bus=uhci0.0"]
  124. else:
  125. qemu += ["-device", "pci-ohci", "-device", "usb-mouse"]
  126. qemu += ["-drive", "file=floppy.img,format=raw,if=floppy",
  127. "-drive", "file=root.img,format=raw,if=floppy",
  128. "-display", "none",
  129. "-qmp", "unix:%s,server,nowait" % sock]
  130. proc = subprocess.Popen(qemu, cwd=ROOT, stdout=subprocess.DEVNULL,
  131. stderr=subprocess.DEVNULL)
  132. ok = False
  133. try:
  134. for _ in range(100):
  135. if os.path.exists(sock):
  136. break
  137. time.sleep(0.1)
  138. s = qmp_connect(sock)
  139. time.sleep(8.0) # boot
  140. if mode == "hotplug":
  141. qmp_cmd(s, "device_add", {"driver": "usb-mouse", "id": "mouse"})
  142. time.sleep(2)
  143. w, h, pix = screendump(s, ppm)
  144. start = find_cursor(w, h, pix)
  145. move(s, 250, 150)
  146. time.sleep(0.5)
  147. w, h, pix = screendump(s, ppm)
  148. end = find_cursor(w, h, pix)
  149. if mode == "hotplug":
  150. qmp_cmd(s, "device_del", {"id": "mouse"})
  151. time.sleep(1)
  152. qmp_cmd(s, "device_add", {"driver": "usb-mouse", "id": "mouse"})
  153. time.sleep(2)
  154. move(s, -100, -60)
  155. time.sleep(0.5)
  156. w, h, pix = screendump(s, ppm)
  157. end2 = find_cursor(w, h, pix)
  158. ok = (start == (100, 100) and end == (350, 250) and
  159. end2 == (250, 190))
  160. print("mode=%s fb=%dx%d boot=%s plug=%s replug=%s -> %s"
  161. % (mode, w, h, start, end, end2,
  162. "ok" if ok else "MISMATCH"))
  163. else:
  164. ok = start == (100, 100) and end == (350, 250)
  165. print("mode=%s fb=%dx%d cursor %s -> %s : %s"
  166. % (mode, w, h, start, end, "ok" if ok else "MISMATCH"))
  167. qmp_cmd(s, "quit")
  168. try:
  169. proc.wait(timeout=10)
  170. except subprocess.TimeoutExpired:
  171. pass
  172. finally:
  173. if proc.poll() is None:
  174. proc.kill()
  175. proc.wait()
  176. shutil.rmtree(workdir, ignore_errors=True)
  177. return 0 if ok else 1
  178. if __name__ == "__main__":
  179. sys.exit(main())