qmp_keyboard_test.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python3
  2. """Headless USB keyboard regression test (QEMU + QMP + screendump).
  3. Boots the OS, types a shell command on a USB keyboard through QMP and
  4. verifies the screen content changed (the command output).
  5. Modes:
  6. (default) OHCI + usb-mouse + usb-kbd; types "help"
  7. --hotplug boots without the keyboard, plugs it in via QMP, then types
  8. --xhci xHCI controller, keyboard on it
  9. --ehci EHCI + UHCI companion, keyboard on the companion (multi-HCD)
  10. --keys a,b,ret type a custom key list (QEMU qcodes)
  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. def qmp_recv(s):
  24. buf = b""
  25. while True:
  26. chunk = s.recv(4096)
  27. if not chunk:
  28. raise RuntimeError("QMP connection closed")
  29. buf += chunk
  30. while b"\n" in buf:
  31. line, buf = buf.split(b"\n", 1)
  32. line = line.strip()
  33. if line:
  34. return json.loads(line)
  35. def qmp_cmd(s, execute, arguments=None):
  36. req = {"execute": execute}
  37. if arguments is not None:
  38. req["arguments"] = arguments
  39. s.sendall((json.dumps(req) + "\n").encode())
  40. while True:
  41. msg = qmp_recv(s)
  42. if "return" in msg or "error" in msg:
  43. if "error" in msg:
  44. print("QMP error:", msg)
  45. return msg
  46. def read_ppm(path):
  47. data = open(path, "rb").read()
  48. parts = data.split(b"\n", 3)
  49. w, h = [int(x) for x in parts[1].split()]
  50. return w, h, parts[3]
  51. def main():
  52. hotplug = "--hotplug" in sys.argv
  53. xhci = "--xhci" in sys.argv
  54. ehci = "--ehci" in sys.argv
  55. keys = ["h", "e", "l", "p", "ret"]
  56. if "--keys" in sys.argv:
  57. keys = sys.argv[sys.argv.index("--keys") + 1].split(",")
  58. if not (os.path.exists(os.path.join(ROOT, "floppy.img")) and
  59. os.path.exists(os.path.join(ROOT, "root.img"))):
  60. print("floppy.img/root.img missing - run `make all` first")
  61. return 1
  62. workdir = tempfile.mkdtemp(prefix="nebos-kbd-")
  63. sock = os.path.join(workdir, "qmp.sock")
  64. ppm = os.path.join(workdir, "screen.ppm")
  65. kbddev = [] if hotplug else ["-device", "usb-kbd"]
  66. if ehci and not hotplug:
  67. kbddev = ["-device", "usb-kbd,bus=uhci0.0"]
  68. qemu = ["qemu-system-i386", "-M", "pc"]
  69. if xhci:
  70. qemu += ["-device", "qemu-xhci", "-device", "usb-mouse"]
  71. elif ehci:
  72. qemu += ["-device", "usb-ehci,id=ehci0",
  73. "-device", "piix3-usb-uhci,id=uhci0",
  74. "-device", "usb-mouse,bus=uhci0.0"]
  75. else:
  76. qemu += ["-device", "pci-ohci", "-device", "usb-mouse"]
  77. qemu += kbddev + [
  78. "-drive", "file=floppy.img,format=raw,if=floppy",
  79. "-drive", "file=root.img,format=raw,if=floppy",
  80. "-display", "none",
  81. "-qmp", "unix:%s,server,nowait" % sock]
  82. proc = subprocess.Popen(qemu, cwd=ROOT, stdout=subprocess.DEVNULL,
  83. stderr=subprocess.DEVNULL)
  84. ok = False
  85. try:
  86. for _ in range(100):
  87. if os.path.exists(sock):
  88. break
  89. time.sleep(0.1)
  90. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  91. s.connect(sock)
  92. qmp_recv(s) # greeting
  93. qmp_cmd(s, "qmp_capabilities")
  94. time.sleep(5) # boot
  95. if hotplug:
  96. qmp_cmd(s, "device_add", {"driver": "usb-kbd", "id": "kbd"})
  97. time.sleep(2)
  98. if os.path.exists(ppm):
  99. os.unlink(ppm)
  100. qmp_cmd(s, "screendump", {"filename": ppm})
  101. time.sleep(0.5)
  102. w, h, before = read_ppm(ppm)
  103. for k in keys:
  104. qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": k}]})
  105. time.sleep(0.25)
  106. time.sleep(4.0)
  107. if os.path.exists(ppm):
  108. os.unlink(ppm)
  109. qmp_cmd(s, "screendump", {"filename": ppm})
  110. time.sleep(0.5)
  111. w2, h2, after = read_ppm(ppm)
  112. changed = sum(1 for a, b in zip(before, after) if a != b)
  113. ok = changed > 3000 # the command output repaints the screen
  114. print("keys=%s screen %dx%d changed bytes: %d -> %s"
  115. % (",".join(keys), w, h, changed, "ok" if ok else "NO CHANGE"))
  116. qmp_cmd(s, "quit")
  117. try:
  118. proc.wait(timeout=10)
  119. except subprocess.TimeoutExpired:
  120. pass
  121. finally:
  122. if proc.poll() is None:
  123. proc.kill()
  124. proc.wait()
  125. shutil.rmtree(workdir, ignore_errors=True)
  126. return 0 if ok else 1
  127. if __name__ == "__main__":
  128. sys.exit(main())