qmp_keyboard_test.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. --keys a,b,ret type a custom key list (QEMU qcodes)
  9. Requires: floppy.img/root.img built (make all), python3, QEMU.
  10. Exit code 0 = pass.
  11. """
  12. import json
  13. import os
  14. import shutil
  15. import socket
  16. import subprocess
  17. import sys
  18. import tempfile
  19. import time
  20. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  21. def qmp_recv(s):
  22. buf = b""
  23. while True:
  24. chunk = s.recv(4096)
  25. if not chunk:
  26. raise RuntimeError("QMP connection closed")
  27. buf += chunk
  28. while b"\n" in buf:
  29. line, buf = buf.split(b"\n", 1)
  30. line = line.strip()
  31. if line:
  32. return json.loads(line)
  33. def qmp_cmd(s, execute, arguments=None):
  34. req = {"execute": execute}
  35. if arguments is not None:
  36. req["arguments"] = arguments
  37. s.sendall((json.dumps(req) + "\n").encode())
  38. while True:
  39. msg = qmp_recv(s)
  40. if "return" in msg or "error" in msg:
  41. if "error" in msg:
  42. print("QMP error:", msg)
  43. return msg
  44. def read_ppm(path):
  45. data = open(path, "rb").read()
  46. parts = data.split(b"\n", 3)
  47. w, h = [int(x) for x in parts[1].split()]
  48. return w, h, parts[3]
  49. def main():
  50. hotplug = "--hotplug" in sys.argv
  51. xhci = "--xhci" in sys.argv
  52. keys = ["h", "e", "l", "p", "ret"]
  53. if "--keys" in sys.argv:
  54. keys = sys.argv[sys.argv.index("--keys") + 1].split(",")
  55. if not (os.path.exists(os.path.join(ROOT, "floppy.img")) and
  56. os.path.exists(os.path.join(ROOT, "root.img"))):
  57. print("floppy.img/root.img missing - run `make all` first")
  58. return 1
  59. workdir = tempfile.mkdtemp(prefix="nebos-kbd-")
  60. sock = os.path.join(workdir, "qmp.sock")
  61. ppm = os.path.join(workdir, "screen.ppm")
  62. kbddev = [] if hotplug else ["-device", "usb-kbd"]
  63. qemu = ["qemu-system-i386", "-M", "pc"]
  64. if xhci:
  65. qemu += ["-device", "qemu-xhci", "-device", "usb-mouse"]
  66. else:
  67. qemu += ["-device", "pci-ohci", "-device", "usb-mouse"]
  68. qemu += kbddev + [
  69. "-drive", "file=floppy.img,format=raw,if=floppy",
  70. "-drive", "file=root.img,format=raw,if=floppy",
  71. "-display", "none",
  72. "-qmp", "unix:%s,server,nowait" % sock]
  73. proc = subprocess.Popen(qemu, cwd=ROOT, stdout=subprocess.DEVNULL,
  74. stderr=subprocess.DEVNULL)
  75. ok = False
  76. try:
  77. for _ in range(100):
  78. if os.path.exists(sock):
  79. break
  80. time.sleep(0.1)
  81. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  82. s.connect(sock)
  83. qmp_recv(s) # greeting
  84. qmp_cmd(s, "qmp_capabilities")
  85. time.sleep(4) # boot
  86. if hotplug:
  87. qmp_cmd(s, "device_add", {"driver": "usb-kbd", "id": "kbd"})
  88. time.sleep(2)
  89. if os.path.exists(ppm):
  90. os.unlink(ppm)
  91. qmp_cmd(s, "screendump", {"filename": ppm})
  92. time.sleep(0.5)
  93. w, h, before = read_ppm(ppm)
  94. for k in keys:
  95. qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": k}]})
  96. time.sleep(0.25)
  97. time.sleep(1.0)
  98. if os.path.exists(ppm):
  99. os.unlink(ppm)
  100. qmp_cmd(s, "screendump", {"filename": ppm})
  101. time.sleep(0.5)
  102. w2, h2, after = read_ppm(ppm)
  103. changed = sum(1 for a, b in zip(before, after) if a != b)
  104. ok = changed > 3000 # the command output repaints the screen
  105. print("keys=%s screen %dx%d changed bytes: %d -> %s"
  106. % (",".join(keys), w, h, changed, "ok" if ok else "NO CHANGE"))
  107. qmp_cmd(s, "quit")
  108. try:
  109. proc.wait(timeout=10)
  110. except subprocess.TimeoutExpired:
  111. pass
  112. finally:
  113. if proc.poll() is None:
  114. proc.kill()
  115. proc.wait()
  116. shutil.rmtree(workdir, ignore_errors=True)
  117. return 0 if ok else 1
  118. if __name__ == "__main__":
  119. sys.exit(main())