qmp_cursor_test.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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", "xhci2",
  99. "xhci2hub", "ehci", "hub"):
  100. if "--" + m in sys.argv:
  101. mode = m
  102. if not (os.path.exists(os.path.join(ROOT, "floppy.img")) and
  103. os.path.exists(os.path.join(ROOT, "root.img"))):
  104. print("floppy.img/root.img missing - run `make all` first")
  105. return 1
  106. workdir = tempfile.mkdtemp(prefix="nebos-cursor-")
  107. sock = os.path.join(workdir, "qmp.sock")
  108. ppm = os.path.join(workdir, "screen.ppm")
  109. qemu = ["qemu-system-i386", "-M", "pc"]
  110. if mode == "uhci":
  111. qemu += ["-device", "piix3-usb-uhci", "-device", "usb-mouse"]
  112. elif mode == "uhci72":
  113. qemu += ["-device", "piix3-usb-uhci,addr=1.2", "-device", "usb-mouse"]
  114. elif mode == "multi":
  115. qemu += ["-device", "pci-ohci,id=o0", "-device", "pci-ohci,id=o1",
  116. "-device", "pci-ohci,id=o2", "-device", "usb-mouse,bus=o2.0"]
  117. elif mode == "hotplug":
  118. qemu += ["-device", "pci-ohci"]
  119. elif mode == "xhci":
  120. qemu += ["-device", "qemu-xhci", "-device", "usb-mouse"]
  121. elif mode == "xhci2":
  122. qemu += ["-device", "qemu-xhci,id=xhci0",
  123. "-device", "qemu-xhci,id=xhci1",
  124. "-device", "usb-mouse,bus=xhci1.0"]
  125. elif mode == "xhci2hub":
  126. qemu += ["-device", "qemu-xhci,id=xhci0",
  127. "-device", "qemu-xhci,id=xhci1",
  128. "-device", "usb-hub,bus=xhci1.0,port=1,id=hub1",
  129. "-device", "usb-mouse,bus=xhci1.0,port=1.2"]
  130. elif mode == "hub":
  131. qemu += ["-device", "qemu-xhci", "-device", "usb-hub,id=hub0",
  132. "-device", "usb-mouse,bus=usb-bus.0,port=1.2"]
  133. elif mode == "ehci":
  134. qemu += ["-device", "usb-ehci,id=ehci0",
  135. "-device", "piix3-usb-uhci,id=uhci0",
  136. "-device", "usb-mouse,bus=uhci0.0"]
  137. else:
  138. qemu += ["-device", "pci-ohci", "-device", "usb-mouse"]
  139. qemu += ["-drive", "file=floppy.img,format=raw,if=floppy",
  140. "-drive", "file=root.img,format=raw,if=floppy",
  141. "-display", "none",
  142. "-qmp", "unix:%s,server,nowait" % sock]
  143. proc = subprocess.Popen(qemu, cwd=ROOT, stdout=subprocess.DEVNULL,
  144. stderr=subprocess.DEVNULL)
  145. ok = False
  146. try:
  147. for _ in range(100):
  148. if os.path.exists(sock):
  149. break
  150. time.sleep(0.1)
  151. s = qmp_connect(sock)
  152. time.sleep(8.0) # boot
  153. if mode == "hotplug":
  154. qmp_cmd(s, "device_add", {"driver": "usb-mouse", "id": "mouse"})
  155. time.sleep(4)
  156. # Move to the empty desktop first: (100,100) sits over the terminal,
  157. # whose I-beam/theme cursor is not the built-in arrow this test looks
  158. # for. The cursor movement itself is what is under test.
  159. move(s, 1500, 500)
  160. time.sleep(0.5)
  161. w, h, pix = screendump(s, ppm)
  162. start = find_cursor(w, h, pix)
  163. move(s, -250, -150)
  164. time.sleep(0.5)
  165. w, h, pix = screendump(s, ppm)
  166. end = find_cursor(w, h, pix)
  167. if mode == "hotplug":
  168. qmp_cmd(s, "device_del", {"id": "mouse"})
  169. time.sleep(1)
  170. qmp_cmd(s, "device_add", {"driver": "usb-mouse", "id": "mouse"})
  171. time.sleep(4)
  172. move(s, -100, -60)
  173. time.sleep(0.5)
  174. w, h, pix = screendump(s, ppm)
  175. end2 = find_cursor(w, h, pix)
  176. ok = (start == (1600, 600) and end == (1350, 450) and
  177. end2 == (1250, 390))
  178. print("mode=%s fb=%dx%d boot=%s plug=%s replug=%s -> %s"
  179. % (mode, w, h, start, end, end2,
  180. "ok" if ok else "MISMATCH"))
  181. else:
  182. ok = start == (1600, 600) and end == (1350, 450)
  183. print("mode=%s fb=%dx%d cursor %s -> %s : %s"
  184. % (mode, w, h, start, end, "ok" if ok else "MISMATCH"))
  185. qmp_cmd(s, "quit")
  186. try:
  187. proc.wait(timeout=10)
  188. except subprocess.TimeoutExpired:
  189. pass
  190. finally:
  191. if proc.poll() is None:
  192. proc.kill()
  193. proc.wait()
  194. shutil.rmtree(workdir, ignore_errors=True)
  195. return 0 if ok else 1
  196. if __name__ == "__main__":
  197. sys.exit(main())