2020-07-12 23:38:50 -06:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright © 2020 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
# copy of this software and associated documentation files (the "Software"),
|
|
|
|
# to deal in the Software without restriction, including without limitation
|
|
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
# Software is furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice (including the next
|
|
|
|
# paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
# Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
# DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
import itertools
|
|
|
|
import os
|
|
|
|
import resource
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import logging
|
2020-07-26 19:26:51 -06:00
|
|
|
import tempfile
|
2020-09-07 01:36:18 -06:00
|
|
|
import unittest
|
2020-07-12 23:38:50 -06:00
|
|
|
|
|
|
|
|
2021-01-21 15:05:09 -07:00
|
|
|
try:
|
2023-09-28 01:50:43 -06:00
|
|
|
top_builddir = os.environ["top_builddir"]
|
|
|
|
top_srcdir = os.environ["top_srcdir"]
|
2021-01-21 15:05:09 -07:00
|
|
|
except KeyError:
|
2023-09-28 01:50:43 -06:00
|
|
|
print(
|
|
|
|
"Required environment variables not found: top_srcdir/top_builddir",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2021-01-21 15:05:09 -07:00
|
|
|
from pathlib import Path
|
2023-09-28 01:50:43 -06:00
|
|
|
|
|
|
|
top_srcdir = "."
|
2021-01-21 15:05:09 -07:00
|
|
|
try:
|
2023-09-28 01:50:43 -06:00
|
|
|
top_builddir = next(Path(".").glob("**/meson-logs/")).parent
|
2021-01-21 15:05:09 -07:00
|
|
|
except StopIteration:
|
|
|
|
sys.exit(1)
|
2023-09-28 01:50:43 -06:00
|
|
|
print(
|
|
|
|
'Using srcdir "{}", builddir "{}"'.format(top_srcdir, top_builddir),
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2021-01-21 15:05:09 -07:00
|
|
|
|
2020-07-25 07:30:39 -06:00
|
|
|
|
2020-07-12 23:38:50 -06:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2023-09-28 01:50:43 -06:00
|
|
|
logger = logging.getLogger("test")
|
2020-07-12 23:38:50 -06:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
# Permutation of RMLVO that we use in multiple tests
|
2023-09-28 01:50:43 -06:00
|
|
|
rmlvos = [
|
|
|
|
list(x)
|
|
|
|
for x in itertools.permutations(
|
|
|
|
["--rules=evdev", "--model=pc104", "--layout=ch", "--options=eurosign:5"]
|
|
|
|
)
|
|
|
|
]
|
2020-07-12 23:38:50 -06:00
|
|
|
|
|
|
|
|
|
|
|
def _disable_coredump():
|
|
|
|
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(args):
|
2023-09-28 01:50:43 -06:00
|
|
|
logger.debug("run command: {}".format(" ".join(args)))
|
2020-07-12 23:38:50 -06:00
|
|
|
|
|
|
|
try:
|
2023-09-28 01:50:43 -06:00
|
|
|
p = subprocess.run(
|
|
|
|
args,
|
|
|
|
preexec_fn=_disable_coredump,
|
|
|
|
capture_output=True,
|
|
|
|
text=True,
|
|
|
|
timeout=0.7,
|
|
|
|
)
|
2020-07-12 23:38:50 -06:00
|
|
|
return p.returncode, p.stdout, p.stderr
|
|
|
|
except subprocess.TimeoutExpired as e:
|
|
|
|
return 0, e.stdout, e.stderr
|
|
|
|
|
|
|
|
|
|
|
|
class XkbcliTool:
|
2023-09-28 01:50:43 -06:00
|
|
|
xkbcli_tool = "xkbcli"
|
2020-07-12 23:38:50 -06:00
|
|
|
subtool = None
|
|
|
|
|
2021-04-20 17:02:40 -06:00
|
|
|
def __init__(self, subtool=None, skipIf=(), skipError=()):
|
2020-07-25 07:30:39 -06:00
|
|
|
self.tool_path = top_builddir
|
2020-07-12 23:38:50 -06:00
|
|
|
self.subtool = subtool
|
2020-09-07 03:27:26 -06:00
|
|
|
self.skipIf = skipIf
|
2021-04-20 17:02:40 -06:00
|
|
|
self.skipError = skipError
|
2020-07-12 23:38:50 -06:00
|
|
|
|
|
|
|
def run_command(self, args):
|
2020-09-07 03:27:26 -06:00
|
|
|
for condition, reason in self.skipIf:
|
|
|
|
if condition:
|
|
|
|
raise unittest.SkipTest(reason)
|
2020-07-12 23:38:50 -06:00
|
|
|
if self.subtool is not None:
|
2023-09-28 01:50:43 -06:00
|
|
|
tool = "{}-{}".format(self.xkbcli_tool, self.subtool)
|
2020-07-12 23:38:50 -06:00
|
|
|
else:
|
|
|
|
tool = self.xkbcli_tool
|
|
|
|
args = [os.path.join(self.tool_path, tool)] + args
|
|
|
|
|
|
|
|
return run_command(args)
|
|
|
|
|
|
|
|
def run_command_success(self, args):
|
|
|
|
rc, stdout, stderr = self.run_command(args)
|
2021-04-20 17:02:40 -06:00
|
|
|
if rc != 0:
|
|
|
|
for testfunc, reason in self.skipError:
|
|
|
|
if testfunc(rc, stdout, stderr):
|
|
|
|
raise unittest.SkipTest(reason)
|
2022-01-19 21:17:50 -07:00
|
|
|
assert rc == 0, (rc, stdout, stderr)
|
2020-07-12 23:38:50 -06:00
|
|
|
return stdout, stderr
|
|
|
|
|
|
|
|
def run_command_invalid(self, args):
|
|
|
|
rc, stdout, stderr = self.run_command(args)
|
|
|
|
assert rc == 2, (rc, stdout, stderr)
|
|
|
|
return rc, stdout, stderr
|
|
|
|
|
|
|
|
def run_command_unrecognized_option(self, args):
|
|
|
|
rc, stdout, stderr = self.run_command(args)
|
|
|
|
assert rc == 2, (rc, stdout, stderr)
|
2023-09-28 01:50:43 -06:00
|
|
|
assert stdout.startswith("Usage") or stdout == ""
|
|
|
|
assert "unrecognized option" in stderr
|
2020-07-12 23:38:50 -06:00
|
|
|
|
|
|
|
def run_command_missing_arg(self, args):
|
|
|
|
rc, stdout, stderr = self.run_command(args)
|
|
|
|
assert rc == 2, (rc, stdout, stderr)
|
2023-09-28 01:50:43 -06:00
|
|
|
assert stdout.startswith("Usage") or stdout == ""
|
|
|
|
assert "requires an argument" in stderr
|
2020-07-12 23:38:50 -06:00
|
|
|
|
2020-09-07 01:36:18 -06:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.subtool)
|
|
|
|
|
2020-07-12 23:38:50 -06:00
|
|
|
|
2020-09-07 01:36:18 -06:00
|
|
|
class TestXkbcli(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-09-07 03:27:26 -06:00
|
|
|
cls.xkbcli = XkbcliTool()
|
2023-09-28 01:50:43 -06:00
|
|
|
cls.xkbcli_list = XkbcliTool(
|
|
|
|
"list",
|
|
|
|
skipIf=(
|
|
|
|
(
|
|
|
|
not int(os.getenv("HAVE_XKBCLI_LIST", "1")),
|
|
|
|
"xkbregistory not enabled",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
cls.xkbcli_how_to_type = XkbcliTool("how-to-type")
|
|
|
|
cls.xkbcli_compile_keymap = XkbcliTool("compile-keymap")
|
2023-11-07 04:58:20 -07:00
|
|
|
cls.xkbcli_compile_compose = XkbcliTool("compile-compose")
|
2023-09-28 01:50:43 -06:00
|
|
|
cls.xkbcli_interactive_evdev = XkbcliTool(
|
|
|
|
"interactive-evdev",
|
|
|
|
skipIf=(
|
|
|
|
(
|
|
|
|
not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_EVDEV", "1")),
|
|
|
|
"evdev not enabled",
|
|
|
|
),
|
|
|
|
(not os.path.exists("/dev/input/event0"), "event node required"),
|
|
|
|
(
|
|
|
|
not os.access("/dev/input/event0", os.R_OK),
|
|
|
|
"insufficient permissions",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
skipError=(
|
|
|
|
(
|
|
|
|
lambda rc, stdout, stderr: "Couldn't find any keyboards" in stderr,
|
|
|
|
"No keyboards available",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
cls.xkbcli_interactive_x11 = XkbcliTool(
|
|
|
|
"interactive-x11",
|
|
|
|
skipIf=(
|
|
|
|
(
|
|
|
|
not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_X11", "1")),
|
|
|
|
"x11 not enabled",
|
|
|
|
),
|
|
|
|
(not os.getenv("DISPLAY"), "DISPLAY not set"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
cls.xkbcli_interactive_wayland = XkbcliTool(
|
|
|
|
"interactive-wayland",
|
|
|
|
skipIf=(
|
|
|
|
(
|
|
|
|
not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_WAYLAND", "1")),
|
|
|
|
"wayland not enabled",
|
|
|
|
),
|
|
|
|
(not os.getenv("WAYLAND_DISPLAY"), "WAYLAND_DISPLAY not set"),
|
|
|
|
),
|
2021-04-20 17:02:40 -06:00
|
|
|
)
|
2020-09-07 01:36:18 -06:00
|
|
|
cls.all_tools = [
|
|
|
|
cls.xkbcli,
|
|
|
|
cls.xkbcli_list,
|
|
|
|
cls.xkbcli_how_to_type,
|
|
|
|
cls.xkbcli_compile_keymap,
|
2023-11-07 04:58:20 -07:00
|
|
|
cls.xkbcli_compile_compose,
|
2020-09-07 01:36:18 -06:00
|
|
|
cls.xkbcli_interactive_evdev,
|
|
|
|
cls.xkbcli_interactive_x11,
|
|
|
|
cls.xkbcli_interactive_wayland,
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_help(self):
|
|
|
|
# --help is supported by all tools
|
|
|
|
for tool in self.all_tools:
|
|
|
|
with self.subTest(tool=tool):
|
2023-09-28 01:50:43 -06:00
|
|
|
stdout, stderr = tool.run_command_success(["--help"])
|
|
|
|
assert stdout.startswith("Usage:")
|
|
|
|
assert stderr == ""
|
2020-09-07 01:36:18 -06:00
|
|
|
|
|
|
|
def test_invalid_option(self):
|
|
|
|
# --foobar generates "Usage:" for all tools
|
|
|
|
for tool in self.all_tools:
|
|
|
|
with self.subTest(tool=tool):
|
2023-09-28 01:50:43 -06:00
|
|
|
tool.run_command_unrecognized_option(["--foobar"])
|
2020-09-07 01:36:18 -06:00
|
|
|
|
|
|
|
def test_xkbcli_version(self):
|
|
|
|
# xkbcli --version
|
2023-09-28 01:50:43 -06:00
|
|
|
stdout, stderr = self.xkbcli.run_command_success(["--version"])
|
|
|
|
assert stdout.startswith("1")
|
|
|
|
assert stderr == ""
|
2020-09-07 01:36:18 -06:00
|
|
|
|
|
|
|
def test_xkbcli_too_many_args(self):
|
2023-09-28 01:50:43 -06:00
|
|
|
self.xkbcli.run_command_invalid(["a"] * 64)
|
2020-09-07 01:36:18 -06:00
|
|
|
|
|
|
|
def test_compile_keymap_args(self):
|
|
|
|
for args in (
|
2023-09-28 01:50:43 -06:00
|
|
|
["--verbose"],
|
|
|
|
["--rmlvo"],
|
2020-09-07 01:36:18 -06:00
|
|
|
# ['--kccgst'],
|
2023-09-28 01:50:43 -06:00
|
|
|
["--verbose", "--rmlvo"],
|
2020-09-07 01:36:18 -06:00
|
|
|
# ['--verbose', '--kccgst'],
|
|
|
|
):
|
|
|
|
with self.subTest(args=args):
|
|
|
|
self.xkbcli_compile_keymap.run_command_success(args)
|
|
|
|
|
|
|
|
def test_compile_keymap_rmlvo(self):
|
|
|
|
for rmlvo in rmlvos:
|
|
|
|
with self.subTest(rmlvo=rmlvo):
|
|
|
|
self.xkbcli_compile_keymap.run_command_success(rmlvo)
|
|
|
|
|
|
|
|
def test_compile_keymap_include(self):
|
|
|
|
for args in (
|
2023-09-28 01:50:43 -06:00
|
|
|
["--include", ".", "--include-defaults"],
|
|
|
|
["--include", "/tmp", "--include-defaults"],
|
2020-09-07 01:36:18 -06:00
|
|
|
):
|
|
|
|
with self.subTest(args=args):
|
|
|
|
# Succeeds thanks to include-defaults
|
|
|
|
self.xkbcli_compile_keymap.run_command_success(args)
|
|
|
|
|
|
|
|
def test_compile_keymap_include_invalid(self):
|
|
|
|
# A non-directory is rejected by default
|
2023-09-28 01:50:43 -06:00
|
|
|
args = ["--include", "/proc/version"]
|
2020-09-07 01:36:18 -06:00
|
|
|
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
|
|
|
|
assert rc == 1, (stdout, stderr)
|
|
|
|
assert "There are no include paths to search" in stderr
|
|
|
|
|
|
|
|
# A non-existing directory is rejected by default
|
2023-09-28 01:50:43 -06:00
|
|
|
args = ["--include", "/tmp/does/not/exist"]
|
2020-09-07 01:36:18 -06:00
|
|
|
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
|
|
|
|
assert rc == 1, (stdout, stderr)
|
|
|
|
assert "There are no include paths to search" in stderr
|
|
|
|
|
|
|
|
# Valid dir, but missing files
|
2023-09-28 01:50:43 -06:00
|
|
|
args = ["--include", "/tmp"]
|
2020-09-07 01:36:18 -06:00
|
|
|
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
|
|
|
|
assert rc == 1, (stdout, stderr)
|
|
|
|
assert "Couldn't look up rules" in stderr
|
|
|
|
|
|
|
|
def test_how_to_type(self):
|
|
|
|
# Unicode codepoint conversions, we support whatever strtol does
|
2023-09-28 01:50:43 -06:00
|
|
|
for args in (["123"], ["0x123"], ["0123"]):
|
2020-09-07 01:36:18 -06:00
|
|
|
with self.subTest(args=args):
|
|
|
|
self.xkbcli_how_to_type.run_command_success(args)
|
|
|
|
|
|
|
|
def test_how_to_type_rmlvo(self):
|
|
|
|
for rmlvo in rmlvos:
|
|
|
|
with self.subTest(rmlvo=rmlvo):
|
2023-09-28 01:50:43 -06:00
|
|
|
args = rmlvo + ["0x1234"]
|
2020-09-07 01:36:18 -06:00
|
|
|
self.xkbcli_how_to_type.run_command_success(args)
|
|
|
|
|
|
|
|
def test_list_rmlvo(self):
|
|
|
|
for args in (
|
2023-09-28 01:50:43 -06:00
|
|
|
["--verbose"],
|
|
|
|
["-v"],
|
|
|
|
["--verbose", "--load-exotic"],
|
|
|
|
["--load-exotic"],
|
|
|
|
["--ruleset=evdev"],
|
|
|
|
["--ruleset=base"],
|
2020-09-07 01:36:18 -06:00
|
|
|
):
|
|
|
|
with self.subTest(args=args):
|
|
|
|
self.xkbcli_list.run_command_success(args)
|
|
|
|
|
|
|
|
def test_list_rmlvo_includes(self):
|
2023-09-28 01:50:43 -06:00
|
|
|
args = ["/tmp/"]
|
2020-09-07 01:36:18 -06:00
|
|
|
self.xkbcli_list.run_command_success(args)
|
|
|
|
|
|
|
|
def test_list_rmlvo_includes_invalid(self):
|
2023-09-28 01:50:43 -06:00
|
|
|
args = ["/proc/version"]
|
2020-09-07 01:36:18 -06:00
|
|
|
rc, stdout, stderr = self.xkbcli_list.run_command(args)
|
|
|
|
assert rc == 1
|
|
|
|
assert "Failed to append include path" in stderr
|
|
|
|
|
|
|
|
def test_list_rmlvo_includes_no_defaults(self):
|
2023-09-28 01:50:43 -06:00
|
|
|
args = ["--skip-default-paths", "/tmp"]
|
2020-09-07 01:36:18 -06:00
|
|
|
rc, stdout, stderr = self.xkbcli_list.run_command(args)
|
|
|
|
assert rc == 1
|
|
|
|
assert "Failed to parse XKB description" in stderr
|
|
|
|
|
|
|
|
def test_interactive_evdev_rmlvo(self):
|
|
|
|
for rmlvo in rmlvos:
|
|
|
|
with self.subTest(rmlvo=rmlvo):
|
2021-01-21 15:18:13 -07:00
|
|
|
self.xkbcli_interactive_evdev.run_command_success(rmlvo)
|
2020-09-07 01:36:18 -06:00
|
|
|
|
|
|
|
def test_interactive_evdev(self):
|
|
|
|
# Note: --enable-compose fails if $prefix doesn't have the compose tables
|
|
|
|
# installed
|
|
|
|
for args in (
|
2023-09-28 01:50:43 -06:00
|
|
|
["--report-state-changes"],
|
|
|
|
["--enable-compose"],
|
|
|
|
["--consumed-mode=xkb"],
|
|
|
|
["--consumed-mode=gtk"],
|
|
|
|
["--without-x11-offset"],
|
2020-09-07 01:36:18 -06:00
|
|
|
):
|
|
|
|
with self.subTest(args=args):
|
|
|
|
self.xkbcli_interactive_evdev.run_command_success(args)
|
|
|
|
|
|
|
|
def test_interactive_x11(self):
|
|
|
|
# To be filled in if we handle something other than --help
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_interactive_wayland(self):
|
|
|
|
# To be filled in if we handle something other than --help
|
|
|
|
pass
|
2020-07-12 23:38:50 -06:00
|
|
|
|
|
|
|
|
2023-09-28 01:50:43 -06:00
|
|
|
if __name__ == "__main__":
|
2020-07-26 19:26:51 -06:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
2020-09-07 02:10:19 -06:00
|
|
|
# Use our own test xkeyboard-config copy.
|
2023-09-28 01:50:43 -06:00
|
|
|
os.environ["XKB_CONFIG_ROOT"] = top_srcdir + "/test/data"
|
2021-05-22 11:11:10 -06:00
|
|
|
# Use our own X11 locale copy.
|
2023-09-28 01:50:43 -06:00
|
|
|
os.environ["XLOCALEDIR"] = top_srcdir + "/test/data/locale"
|
2021-05-22 11:11:10 -06:00
|
|
|
# Use our own locale.
|
2023-09-28 01:50:43 -06:00
|
|
|
os.environ["LC_CTYPE"] = "en_US.UTF-8"
|
2020-07-26 19:26:51 -06:00
|
|
|
# libxkbcommon has fallbacks when XDG_CONFIG_HOME isn't set so we need
|
|
|
|
# to override it with a known (empty) directory. Otherwise our test
|
|
|
|
# behavior depends on the system the test is run on.
|
2023-09-28 01:50:43 -06:00
|
|
|
os.environ["XDG_CONFIG_HOME"] = tmpdir
|
2020-09-07 02:10:19 -06:00
|
|
|
# Prevent the legacy $HOME/.xkb from kicking in.
|
2023-09-28 01:50:43 -06:00
|
|
|
del os.environ["HOME"]
|
2020-07-09 16:50:02 -06:00
|
|
|
# This needs to be separated if we do specific extra path testing
|
2023-09-28 01:50:43 -06:00
|
|
|
os.environ["XKB_CONFIG_EXTRA_PATH"] = tmpdir
|
2020-07-26 19:26:51 -06:00
|
|
|
|
2020-09-07 01:36:18 -06:00
|
|
|
unittest.main()
|