3 # Copyright © 2020 Red Hat, Inc.
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 # DEALINGS IN THE SOFTWARE.
34 top_builddir = os.environ['top_builddir']
35 top_srcdir = os.environ['top_srcdir']
37 logging.basicConfig(level=logging.DEBUG)
38 logger = logging.getLogger('test')
39 logger.setLevel(logging.DEBUG)
41 # Permutation of RMLVO that we use in multiple tests
42 rmlvos = [list(x) for x in itertools.permutations(
43 ['--rules=evdev', '--model=pc104',
44 '--layout=ch', '--options=eurosign:5']
48 def _disable_coredump():
49 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
52 def run_command(args):
53 logger.debug('run command: {}'.format(' '.join(args)))
56 p = subprocess.run(args, preexec_fn=_disable_coredump,
57 capture_output=True, text=True,
59 return p.returncode, p.stdout, p.stderr
60 except subprocess.TimeoutExpired as e:
61 return 0, e.stdout, e.stderr
65 xkbcli_tool = 'xkbcli'
68 def __init__(self, subtool=None, skipIf=()):
69 self.tool_path = top_builddir
70 self.subtool = subtool
73 def run_command(self, args):
74 for condition, reason in self.skipIf:
76 raise unittest.SkipTest(reason)
77 if self.subtool is not None:
78 tool = '{}-{}'.format(self.xkbcli_tool, self.subtool)
80 tool = self.xkbcli_tool
81 args = [os.path.join(self.tool_path, tool)] + args
83 return run_command(args)
85 def run_command_success(self, args):
86 rc, stdout, stderr = self.run_command(args)
87 assert rc == 0, (stdout, stderr)
90 def run_command_invalid(self, args):
91 rc, stdout, stderr = self.run_command(args)
92 assert rc == 2, (rc, stdout, stderr)
93 return rc, stdout, stderr
95 def run_command_unrecognized_option(self, args):
96 rc, stdout, stderr = self.run_command(args)
97 assert rc == 2, (rc, stdout, stderr)
98 assert stdout.startswith('Usage') or stdout == ''
99 assert 'unrecognized option' in stderr
101 def run_command_missing_arg(self, args):
102 rc, stdout, stderr = self.run_command(args)
103 assert rc == 2, (rc, stdout, stderr)
104 assert stdout.startswith('Usage') or stdout == ''
105 assert 'requires an argument' in stderr
108 return str(self.subtool)
111 class TestXkbcli(unittest.TestCase):
114 cls.xkbcli = XkbcliTool()
115 cls.xkbcli_list = XkbcliTool('list', skipIf=(
116 (not int(os.getenv('HAVE_XKBCLI_LIST', '1')), 'xkbregistory not enabled'),
118 cls.xkbcli_how_to_type = XkbcliTool('how-to-type')
119 cls.xkbcli_compile_keymap = XkbcliTool('compile-keymap')
120 cls.xkbcli_interactive_evdev = XkbcliTool('interactive-evdev', skipIf=(
121 (not int(os.getenv('HAVE_XKBCLI_INTERACTIVE_EVDEV', '1')), 'evdev not enabled'),
122 (not os.path.exists('/dev/input/event0'), 'event node required'),
123 (not os.access('/dev/input/event0', os.R_OK), 'insufficient permissions'),
125 cls.xkbcli_interactive_x11 = XkbcliTool('interactive-x11', skipIf=(
126 (not int(os.getenv('HAVE_XKBCLI_INTERACTIVE_X11', '1')), 'x11 not enabled'),
127 (not os.getenv('DISPLAY'), 'DISPLAY not set'),
129 cls.xkbcli_interactive_wayland = XkbcliTool('interactive-wayland', skipIf=(
130 (not int(os.getenv('HAVE_XKBCLI_INTERACTIVE_WAYLAND', '1')), 'wayland not enabled'),
131 (not os.getenv('WAYLAND_DISPLAY'), 'WAYLAND_DISPLAY not set'),
136 cls.xkbcli_how_to_type,
137 cls.xkbcli_compile_keymap,
138 cls.xkbcli_interactive_evdev,
139 cls.xkbcli_interactive_x11,
140 cls.xkbcli_interactive_wayland,
144 # --help is supported by all tools
145 for tool in self.all_tools:
146 with self.subTest(tool=tool):
147 stdout, stderr = tool.run_command_success(['--help'])
148 assert stdout.startswith('Usage:')
151 def test_invalid_option(self):
152 # --foobar generates "Usage:" for all tools
153 for tool in self.all_tools:
154 with self.subTest(tool=tool):
155 tool.run_command_unrecognized_option(['--foobar'])
157 def test_xkbcli_version(self):
159 stdout, stderr = self.xkbcli.run_command_success(['--version'])
160 assert stdout.startswith('1')
163 def test_xkbcli_too_many_args(self):
164 self.xkbcli.run_command_invalid(['a'] * 64)
166 def test_compile_keymap_args(self):
171 ['--verbose', '--rmlvo'],
172 # ['--verbose', '--kccgst'],
174 with self.subTest(args=args):
175 self.xkbcli_compile_keymap.run_command_success(args)
177 def test_compile_keymap_rmlvo(self):
179 with self.subTest(rmlvo=rmlvo):
180 self.xkbcli_compile_keymap.run_command_success(rmlvo)
182 def test_compile_keymap_include(self):
184 ['--include', '.', '--include-defaults'],
185 ['--include', '/tmp', '--include-defaults'],
187 with self.subTest(args=args):
188 # Succeeds thanks to include-defaults
189 self.xkbcli_compile_keymap.run_command_success(args)
191 def test_compile_keymap_include_invalid(self):
192 # A non-directory is rejected by default
193 args = ['--include', '/proc/version']
194 rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
195 assert rc == 1, (stdout, stderr)
196 assert "There are no include paths to search" in stderr
198 # A non-existing directory is rejected by default
199 args = ['--include', '/tmp/does/not/exist']
200 rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
201 assert rc == 1, (stdout, stderr)
202 assert "There are no include paths to search" in stderr
204 # Valid dir, but missing files
205 args = ['--include', '/tmp']
206 rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
207 assert rc == 1, (stdout, stderr)
208 assert "Couldn't look up rules" in stderr
210 def test_how_to_type(self):
211 # Unicode codepoint conversions, we support whatever strtol does
212 for args in (['123'], ['0x123'], ['0123']):
213 with self.subTest(args=args):
214 self.xkbcli_how_to_type.run_command_success(args)
216 def test_how_to_type_rmlvo(self):
218 with self.subTest(rmlvo=rmlvo):
219 args = rmlvo + ['0x1234']
220 self.xkbcli_how_to_type.run_command_success(args)
222 def test_list_rmlvo(self):
226 ['--verbose', '--load-exotic'],
231 with self.subTest(args=args):
232 self.xkbcli_list.run_command_success(args)
234 def test_list_rmlvo_includes(self):
236 self.xkbcli_list.run_command_success(args)
238 def test_list_rmlvo_includes_invalid(self):
239 args = ['/proc/version']
240 rc, stdout, stderr = self.xkbcli_list.run_command(args)
242 assert "Failed to append include path" in stderr
244 def test_list_rmlvo_includes_no_defaults(self):
245 args = ['--skip-default-paths', '/tmp']
246 rc, stdout, stderr = self.xkbcli_list.run_command(args)
248 assert "Failed to parse XKB description" in stderr
250 def test_interactive_evdev_rmlvo(self):
252 with self.subTest(rmlvo=rmlvo):
253 self.xkbcli_interactive_evdev.run_command_success(rmlvos)
255 def test_interactive_evdev(self):
256 # Note: --enable-compose fails if $prefix doesn't have the compose tables
259 ['--report-state-changes'],
260 ['--enable-compose'],
261 ['--consumed-mode=xkb'],
262 ['--consumed-mode=gtk'],
263 ['--without-x11-offset'],
265 with self.subTest(args=args):
266 self.xkbcli_interactive_evdev.run_command_success(args)
268 def test_interactive_x11(self):
269 # To be filled in if we handle something other than --help
272 def test_interactive_wayland(self):
273 # To be filled in if we handle something other than --help
277 if __name__ == '__main__':
278 with tempfile.TemporaryDirectory() as tmpdir:
279 # Use our own test xkeyboard-config copy.
280 os.environ['XKB_CONFIG_ROOT'] = top_srcdir + '/test/data'
281 # libxkbcommon has fallbacks when XDG_CONFIG_HOME isn't set so we need
282 # to override it with a known (empty) directory. Otherwise our test
283 # behavior depends on the system the test is run on.
284 os.environ['XDG_CONFIG_HOME'] = tmpdir
285 # Prevent the legacy $HOME/.xkb from kicking in.
286 del os.environ['HOME']
287 # This needs to be separated if we do specific extra path testing
288 os.environ['XKB_CONFIG_EXTRA_PATH'] = tmpdir