text.c: use strncpy instead of strcpy for better security
[platform/upstream/libxkbcommon.git] / test / test-keysym.py
1 #!/usr/bin/env python3
2 #
3 # This script creates a custom layout, overriding the TDLE key with the first
4 # argument given.
5
6 import argparse
7 import tempfile
8 from pathlib import Path
9 import subprocess
10 import os
11 import re
12 import sys
13
14 # Template to force our key to TLDE
15 template = """
16 default
17 xkb_symbols "basic" {{
18     include "us(basic)"
19     replace key <TLDE> {{ [ {} ] }};
20 }};
21 """
22
23 parser = argparse.ArgumentParser(
24     description="Tool to verify whether a keysym is resolved"
25 )
26 parser.add_argument("keysym", type=str, help="XKB keysym")
27 parser.add_argument(
28     "--tool",
29     type=str,
30     nargs=1,
31     default=["xkbcli", "compile-keymap"],
32     help="Full path to the xkbcli-compile-keymap tool",
33 )
34 args = parser.parse_args()
35
36 with tempfile.TemporaryDirectory() as tmpdir:
37     symfile = Path(tmpdir) / "symbols" / "keytest"
38     symfile.parent.mkdir()
39     with symfile.open(mode="w") as f:
40         f.write(template.format(args.keysym))
41
42     try:
43         cmd = [
44             *args.tool,
45             "--layout",
46             "keytest",
47         ]
48
49         env = os.environ.copy()
50         env["XKB_CONFIG_EXTRA_PATH"] = tmpdir
51
52         result = subprocess.run(
53             cmd, env=env, capture_output=True, universal_newlines=True
54         )
55         if result.returncode != 0:
56             print("ERROR: Failed to compile:")
57             print(result.stderr)
58             sys.exit(1)
59
60         # grep for TLDE actually being remapped
61         for l in result.stdout.split("\n"):
62             match = re.match(r"\s+key \<TLDE\>\s+{\s+\[\s+(?P<keysym>\w+)\s+\]\s+}", l)
63             if match:
64                 if args.keysym == match.group("keysym"):
65                     sys.exit(0)
66                 elif match.group("keysym") == "NoSymbol":
67                     print("ERROR: key {} not resolved:".format(args.keysym), l)
68                 else:
69                     print("ERROR: key {} mapped to wrong key:".format(args.keysym), l)
70                 sys.exit(1)
71
72         print(result.stdout)
73         print("ERROR: above keymap is missing key mapping for {}".format(args.keysym))
74         sys.exit(1)
75     except FileNotFoundError as err:
76         print("ERROR: invalid or missing tool: {}".format(err))
77         sys.exit(1)