summaryrefslogtreecommitdiffstats
path: root/scripts/tests/sendkeys.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/tests/sendkeys.py')
-rwxr-xr-xscripts/tests/sendkeys.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/scripts/tests/sendkeys.py b/scripts/tests/sendkeys.py
new file mode 100755
index 0000000..9300242
--- /dev/null
+++ b/scripts/tests/sendkeys.py
@@ -0,0 +1,105 @@
1#!/usr/bin/env python3
2"""Type lines into a QEMU guest via the monitor's `sendkey` command.
3
4Each positional argument is one line: it is typed character by character
5(US layout) followed by Enter. Use it to drive the rs-dos shell from a
6test script without a display.
7
8Used by scripts/tests/test-*.sh, and manually against an interactive
9session started with `./scripts/run.sh --monitor`:
10
11 ./scripts/tests/sendkeys.py "ls /disk" "date" "poweroff"
12 ./scripts/tests/sendkeys.py --sock /tmp/rsdos-monitor.sock "date"
13
14Environment: QMON_SOCK (default: /tmp/rsdos-monitor.sock)
15"""
16import os
17import socket
18import sys
19import time
20
21SOCK = os.environ.get("QMON_SOCK", "/tmp/rsdos-monitor.sock")
22
23# Unshifted punctuation needs QEMU's *named* keys — bare '-'/'.'/'/' are
24# not valid sendkey names (and '-' is the modifier separator).
25BASE = {
26 "-": "minus", "=": "equal", "[": "bracketleft", "]": "bracketright",
27 "\\": "backslash", ";": "semicolon", "'": "apostrophe",
28 ",": "comma", ".": "dot", "/": "slash", "`": "grave",
29}
30
31# Characters that need a shift key on US layout, and the key QEMU's
32# `sendkey` understands for them.
33SHIFT = {
34 "~": "grave", "!": "1", "@": "2", "#": "3", "$": "4", "%": "5",
35 "^": "6", "&": "7", "*": "8", "(": "9", ")": "0",
36 "_": "minus", "+": "equal", "{": "bracketleft", "}": "bracketright",
37 "|": "backslash", ":": "semicolon", '"': "apostrophe",
38 "<": "comma", ">": "dot", "?": "slash",
39}
40
41ALNUM = set("abcdefghijklmnopqrstuvwxyz0123456789")
42
43
44def connect(path, timeout=60):
45 """Connect to the monitor socket, retrying until QEMU is up."""
46 deadline = time.time() + timeout
47 while True:
48 try:
49 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
50 s.connect(path)
51 return s
52 except OSError:
53 if time.time() > deadline:
54 raise
55 time.sleep(0.2)
56
57
58def sendkey(sock, keys):
59 sock.sendall(("sendkey %s\n" % keys).encode())
60
61
62def type_line(sock, line):
63 for ch in line:
64 if ch == " ":
65 sendkey(sock, "spc")
66 elif ch in BASE:
67 sendkey(sock, BASE[ch])
68 elif ch in SHIFT:
69 sendkey(sock, "shift-" + SHIFT[ch])
70 elif ch.isupper():
71 sendkey(sock, "shift-" + ch.lower())
72 elif ch in ALNUM:
73 sendkey(sock, ch)
74 else:
75 sys.stderr.write("warning: no sendkey mapping for %r\n" % ch)
76 continue
77 time.sleep(0.25)
78 sendkey(sock, "ret")
79 time.sleep(1.5)
80
81
82def main():
83 args = [a for a in sys.argv[1:] if a != "--sock"]
84 sock_path = SOCK
85 if "--sock" in sys.argv:
86 i = sys.argv.index("--sock")
87 sock_path = sys.argv[i + 1]
88 args = sys.argv[i + 2:]
89 if not args:
90 sys.stderr.write(__doc__ or "")
91 return 1
92 sock = connect(sock_path)
93 # Let the monitor drain any startup banner before typing.
94 time.sleep(0.5)
95 for line in args:
96 try:
97 type_line(sock, line)
98 except OSError:
99 # Guest powered off (last line was usually "poweroff").
100 break
101 return 0
102
103
104if __name__ == "__main__":
105 sys.exit(main())