qmp_cursor_test.py 6.2 KB

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