qmp_keyboard_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. keys = ["h", "e", "l", "p", "ret"]
  52. if "--keys" in sys.argv:
  53. keys = sys.argv[sys.argv.index("--keys") + 1].split(",")
  54. if not (os.path.exists(os.path.join(ROOT, "floppy.img")) and
  55. os.path.exists(os.path.join(ROOT, "root.img"))):
  56. print("floppy.img/root.img missing - run `make all` first")
  57. return 1
  58. workdir = tempfile.mkdtemp(prefix="nebos-kbd-")
  59. sock = os.path.join(workdir, "qmp.sock")
  60. ppm = os.path.join(workdir, "screen.ppm")
  61. kbddev = [] if hotplug else ["-device", "usb-kbd"]
  62. qemu = ["qemu-system-i386", "-M", "pc",
  63. "-device", "pci-ohci", "-device", "usb-mouse"] + kbddev + [
  64. "-drive", "file=floppy.img,format=raw,if=floppy",
  65. "-drive", "file=root.img,format=raw,if=floppy",
  66. "-display", "none",
  67. "-qmp", "unix:%s,server,nowait" % sock]
  68. proc = subprocess.Popen(qemu, cwd=ROOT, stdout=subprocess.DEVNULL,
  69. stderr=subprocess.DEVNULL)
  70. ok = False
  71. try:
  72. for _ in range(100):
  73. if os.path.exists(sock):
  74. break
  75. time.sleep(0.1)
  76. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  77. s.connect(sock)
  78. qmp_recv(s) # greeting
  79. qmp_cmd(s, "qmp_capabilities")
  80. time.sleep(4) # boot
  81. if hotplug:
  82. qmp_cmd(s, "device_add", {"driver": "usb-kbd", "id": "kbd"})
  83. time.sleep(2)
  84. if os.path.exists(ppm):
  85. os.unlink(ppm)
  86. qmp_cmd(s, "screendump", {"filename": ppm})
  87. time.sleep(0.5)
  88. w, h, before = read_ppm(ppm)
  89. for k in keys:
  90. qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": k}]})
  91. time.sleep(0.25)
  92. time.sleep(1.0)
  93. if os.path.exists(ppm):
  94. os.unlink(ppm)
  95. qmp_cmd(s, "screendump", {"filename": ppm})
  96. time.sleep(0.5)
  97. w2, h2, after = read_ppm(ppm)
  98. changed = sum(1 for a, b in zip(before, after) if a != b)
  99. ok = changed > 3000 # the command output repaints the screen
  100. print("keys=%s screen %dx%d changed bytes: %d -> %s"
  101. % (",".join(keys), w, h, changed, "ok" if ok else "NO CHANGE"))
  102. qmp_cmd(s, "quit")
  103. try:
  104. proc.wait(timeout=10)
  105. except subprocess.TimeoutExpired:
  106. pass
  107. finally:
  108. if proc.poll() is None:
  109. proc.kill()
  110. proc.wait()
  111. shutil.rmtree(workdir, ignore_errors=True)
  112. return 0 if ok else 1
  113. if __name__ == "__main__":
  114. sys.exit(main())