Debian Codegraph @

test/buffercomp.c

raw
1 /*
2 * Copyright © 2009 Dan Nicholson
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13
14 #include "keymap.h"
15 #include "src/keysym.h"
16 #include "test/keysym.h"
17 #include "test.h"
18 #include "utils.h"
19 #include "xkbcommon/xkbcommon.h"
20 #include "test-config.h"
21
22 #define GOLDEN_TESTS_OUTPUTS "keymaps/"
23
24 struct keymap_test_data {
25 /** Keymap string to compile */
26 const char * const keymap;
27 /** Resulting file *path* to reference serialization,
28 * or NULL if the keymap string should not compile. */
29 const char * const expected;
30 /** Optionally skip the test */
31 bool skip;
32 };
33
34 /* Our keymap compiler is the xkbcommon buffer compiler */
35 static struct xkb_keymap *
36 compile_buffer(struct xkb_context *context, enum xkb_keymap_format format,
37 const char *buf, size_t len, void *private)
38 {
39 return test_compile_buffer(context, format, buf, len);
40 }
41
42 static void
43 test_encodings(struct xkb_context *ctx)
44 {
45 struct xkb_keymap *keymap;
46
47 /* Accept UTF-8 encoded BOM (U+FEFF) */
48 const char utf8_with_bom[] = "\xef\xbb\xbfxkb_keymap {};";
49 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
50 utf8_with_bom, sizeof(utf8_with_bom));
51 assert(keymap);
52 xkb_keymap_unref(keymap);
53
54 /* Reject UTF-16LE encoded string */
55 const char utf16_le[] = "x\0k\0b\0_\0k\0e\0y\0m\0a\0p\0 \0{\0}\0;\0";
56 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
57 utf16_le, sizeof(utf16_le));
58 assert(!keymap);
59
60 /* Reject UTF-16LE with BOM encoded string */
61 const char utf16_le_with_bom[] =
62 "\xff\xfex\0k\0b\0_\0k\0e\0y\0m\0a\0p\0 \0{\0}\0;\0";
63 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
64 utf16_le_with_bom, sizeof(utf16_le_with_bom));
65 assert(!keymap);
66
67 /* Reject UTF-16BE encoded string */
68 const char utf16_be[] = "\0x\0k\0b\0_\0k\0e\0y\0m\0a\0p\0 \0{\0}\0;";
69 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
70 utf16_be, sizeof(utf16_be));
71 assert(!keymap);
72 }
73
74 struct keymap_simple_test_data {
75 const char * const keymap;
76 bool valid;
77 };
78
79 static void
80 test_floats(struct xkb_context *ctx)
81 {
82 const struct keymap_simple_test_data tests[] = {
83 /* Valid floats */
84 {
85 .keymap =
86 "xkb_keymap {\n"
87 " xkb_geometry {\n"
88 " width=123.456;\n"
89 " width=123.0;\n"
90 " width=123.;\n"
91 " width=01.234;\n"
92 " width=01.0;\n"
93 " width=01.;\n"
94 " width=001.234;\n"
95 " width=001.0;\n"
96 " width=001.;\n"
97 " };"
98 "};",
99 .valid = true
100 },
101 /* Invalid: missing integer part */
102 {
103 .keymap = "xkb_keymap { xkb_geometry { width=.123; }; };",
104 .valid = false
105 },
106 /* Invalid: comma decimal separator */
107 {
108 .keymap = "xkb_keymap { xkb_geometry { width=1,23; }; };",
109 .valid = false
110 },
111 /* Invalid: exponent */
112 {
113 .keymap = "xkb_keymap { xkb_geometry { width=1.23e2; }; };",
114 .valid = false
115 },
116 };
117
118 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
119 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
120 struct xkb_keymap *keymap =
121 test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
122 tests[k].keymap, strlen(tests[k].keymap));
123 assert(tests[k].valid ^ !keymap);
124 xkb_keymap_unref(keymap);
125 }
126 }
127
128 static void
129 test_component_syntax_error(struct xkb_context *ctx)
130 {
131 /* The following used to trigger memory leak */
132 const char* const keymaps[] = {
133 "xkb_keymap {"
134 " xkb_keycodes {};"
135 "};"
136 "};", /* Syntax error, keymap “complete” */
137 "xkb_keymap {"
138 " xkb_keycodes {};"
139 " xkb_types {};"
140 " xkb_compat {};"
141 " xkb_symbols {};"
142 "};"
143 "};"/* Syntax error, keymap complete */,
144 "xkb_keymap {"
145 " xkb_keycodes {};"
146 "}" /* Syntax error, incomplete map */
147 " xkb_types {};"
148 " xkb_compat {};"
149 " xkb_symbols {};"
150 "};",
151 "xkb_keymap {"
152 " xkb_keycodes {};"
153 ";" /* Syntax error, incomplete map */
154 " xkb_types {};"
155 " xkb_compat {};"
156 " xkb_symbols {};"
157 "};",
158 };
159
160 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
161 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
162 const struct xkb_keymap *keymap =
163 test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
164 keymaps[k], strlen(keymaps[k]));
165 assert(!keymap);
166 }
167 }
168
169 /** Test that any component is optional and can be empty */
170 static void
171 test_optional_components(struct xkb_context *ctx, bool update_output_files)
172 {
173 const struct keymap_test_data keymaps[] = {
174 /*
175 * Optional or empty
176 */
177 {
178 .keymap = "xkb_keymap {};",
179 .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
180 },
181 {
182 .keymap = "xkb_keymap { xkb_keycodes {}; };",
183 .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
184 },
185 {
186 .keymap = "xkb_keymap { xkb_types {}; };",
187 .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
188 },
189 {
190 .keymap = "xkb_keymap { xkb_compat {}; };",
191 .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
192 },
193 {
194 .keymap = "xkb_keymap { xkb_symbols {}; };",
195 .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
196 },
197 /*
198 * Some content, to check we handle missing data correctly
199 */
200 {
201 /* Indicator not defined in keycodes */
202 .keymap =
203 "xkb_keymap {\n"
204 " xkb_compat { indicator \"XXX\" { modifiers=Lock; }; };\n"
205 "};",
206 .expected = GOLDEN_TESTS_OUTPUTS "optional-components-no-real-led.xkb"
207 },
208 {
209 /* Key not defined */
210 .keymap =
211 "xkb_keymap {\n"
212 " xkb_symbols { key <> { [a] }; };\n"
213 "};",
214 .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
215 },
216 {
217 /* Key type not defined */
218 .keymap =
219 "xkb_keymap {\n"
220 " xkb_keycodes { <> = 1; };"
221 " xkb_symbols { key <> { [a], type=\"XXX\" }; };\n"
222 "};",
223 .expected = GOLDEN_TESTS_OUTPUTS "optional-components-basic.xkb"
224 },
225 {
226 /* Virtual modifier not defined */
227 .keymap =
228 "xkb_keymap {\n"
229 " xkb_keycodes { <> = 1; };"
230 " xkb_symbols { key <> { vmods=XXX, [a] }; };\n"
231 "};",
232 .expected = NULL
233 },
234 };
235 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
236 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
237 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
238 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
239 compile_buffer, NULL, __func__,
240 keymaps[k].keymap, strlen(keymaps[k].keymap),
241 keymaps[k].expected, update_output_files));
242 }
243 }
244
245 static void
246 test_bidi_chars(struct xkb_context *ctx, bool update_output_files)
247 {
248 const struct keymap_test_data keymaps[] = {
249 /* Invalid: first char must be ASCII */
250 {
251 .keymap = u8"\u200Exkb_keymap {};",
252 .expected = NULL
253 },
254 {
255 .keymap = u8"\u200Fxkb_keymap {};",
256 .expected = NULL
257 },
258 /* Valid */
259 {
260 .keymap =
261 u8" \u200Fxkb_keymap\u200E\u200F\n\u200E{ "
262 u8"\u200Exkb_keycodes \u200F{ "
263 u8"<>\u200E= \u200F1\u200E;\u200F"
264 u8"}\u200E ;"
265 u8"};\u200E",
266 .expected = GOLDEN_TESTS_OUTPUTS "bidi.xkb"
267 }
268 };
269 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
270 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
271 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
272 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
273 compile_buffer, NULL, __func__,
274 keymaps[k].keymap, strlen(keymaps[k].keymap),
275 keymaps[k].expected, update_output_files));
276 }
277 }
278
279 static void
280 test_recursive_includes(struct xkb_context *ctx)
281 {
282 struct xkb_keymap *keymap;
283
284 const char* const keymaps[] = {
285 /* Recursive keycodes */
286 "Keycodes: recursive",
287 "xkb_keymap {"
288 " xkb_keycodes { include \"evdev+recursive\" };"
289 " xkb_types { include \"complete\" };"
290 " xkb_compat { include \"complete\" };"
291 " xkb_symbols { include \"pc\" };"
292 "};",
293 "Keycodes: recursive(bar)",
294 "xkb_keymap {"
295 " xkb_keycodes { include \"evdev+recursive(bar)\" };"
296 " xkb_types { include \"complete\" };"
297 " xkb_compat { include \"complete\" };"
298 " xkb_symbols { include \"pc\" };"
299 "};",
300 /* Recursive key types */
301 "Key types: recursive",
302 "xkb_keymap {"
303 " xkb_keycodes { include \"evdev\" };"
304 " xkb_types { include \"recursive\" };"
305 " xkb_compat { include \"complete\" };"
306 " xkb_symbols { include \"pc\" };"
307 "};",
308 "Key types: recursive(bar)",
309 "xkb_keymap {"
310 " xkb_keycodes { include \"evdev\" };"
311 " xkb_types { include \"recursive(bar)\" };"
312 " xkb_compat { include \"complete\" };"
313 " xkb_symbols { include \"pc\" };"
314 "};",
315 /* Recursive compat */
316 "Compat: recursive",
317 "xkb_keymap {"
318 " xkb_keycodes { include \"evdev\" };"
319 " xkb_types { include \"recursive\" };"
320 " xkb_compat { include \"complete\" };"
321 " xkb_symbols { include \"pc\" };"
322 "};",
323 "Compat: recursive(bar)",
324 "xkb_keymap {"
325 " xkb_keycodes { include \"evdev\" };"
326 " xkb_types { include \"complete\" };"
327 " xkb_compat { include \"recursive(bar)\" };"
328 " xkb_symbols { include \"pc\" };"
329 "};",
330 /* Recursive symbols */
331 "Symbols: recursive",
332 "xkb_keymap {"
333 " xkb_keycodes { include \"evdev\" };"
334 " xkb_types { include \"complete\" };"
335 " xkb_compat { include \"complete\" };"
336 " xkb_symbols { include \"recursive\" };"
337 "};",
338 "Symbols: recursive(bar)",
339 "xkb_keymap {"
340 " xkb_keycodes { include \"evdev\" };"
341 " xkb_types { include \"complete\" };"
342 " xkb_compat { include \"complete\" };"
343 " xkb_symbols { include \"recursive(bar)\" };"
344 "};"
345 };
346
347 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
348 fprintf(stderr, "------\n*** %s: #%u %s ***\n", __func__, k, keymaps[k]);
349 k++;
350 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
351 keymaps[k], strlen(keymaps[k]));
352 assert(!keymap);
353 }
354 }
355
356 static void
357 test_include_paths(struct xkb_context *ctx)
358 {
359 struct xkb_keymap *keymap;
360 const char simple_str[] =
361 "xkb_keymap {\n"
362 " xkb_keycodes \"test\" { include \"evdev\" };\n"
363 " xkb_types \"test\" { include \"complete\" };\n"
364 " xkb_symbols \"test\" { include \"pc+us(basic)+"
365 "level5\" };\n"
366 "};";
367 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
368 simple_str, sizeof(simple_str));
369 char *expected =
370 xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
371 xkb_keymap_unref(keymap);
372
373 static const char* keymaps[] = {
374 "xkb_keymap {\n"
375 " xkb_keycodes \"test\" { include \"%S/evdev\" };\n"
376 " xkb_types \"test\" { include \"%S/complete\" };\n"
377 " xkb_symbols \"test\" { include \"%S/pc+%S/us(basic)+"
378 "%S/level5\" };\n"
379 "};",
380 /* Windows absolute path contains “:”, which has special meaning in XKB includes,
381 * so skip it. */
382 #ifndef _WIN32
383 "xkb_keymap {\n"
384 " xkb_keycodes \"test\" { include \"" TEST_XKB_CONFIG_ROOT "/keycodes/evdev\" };\n"
385 " xkb_types \"test\" { include \"" TEST_XKB_CONFIG_ROOT "/types/complete\" };\n"
386 " xkb_symbols \"test\" { include \"" TEST_XKB_CONFIG_ROOT "/symbols/pc+"
387 TEST_XKB_CONFIG_ROOT "/symbols/us(basic)+"
388 TEST_XKB_CONFIG_ROOT "/symbols/level5\" };\n"
389 "};",
390 #endif
391 };
392
393 /* Relative path, which does not work as expected, because it is
394 * not resolved before %-expansion. */
395 setenv("XKB_CONFIG_ROOT", "..", 1);
396 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
397 keymaps[0], strlen(keymaps[0]));
398 assert(!keymap);
399
400 setenv("XKB_CONFIG_ROOT", TEST_XKB_CONFIG_ROOT, 1);
401
402 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
403 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
404 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
405 keymaps[k], strlen(keymaps[k]));
406 assert(keymap);
407 char *got =
408 xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
409 assert_streq_not_null("test_include_paths", expected, got);
410 free(got);
411 xkb_keymap_unref(keymap);
412 }
413
414 free(expected);
415 }
416
417 static void
418 test_include_default_maps(bool update_output_files)
419 {
420 struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
421 assert(ctx);
422 /* “User” config */
423 char *include_path = test_get_path("extra");
424 assert(include_path);
425 assert(xkb_context_include_path_append(ctx, include_path));
426 free(include_path);
427 /* “System” config */
428 include_path = test_get_path("");
429 assert(include_path);
430 assert(xkb_context_include_path_append(ctx, include_path));
431 free(include_path);
432
433 const struct keymap_test_data keymaps[] = {
434 {
435 /* Use system explicit default map */
436 .keymap =
437 "xkb_keymap {\n"
438 " xkb_keycodes { <CAPS> = 66; };\n"
439 " xkb_symbols { include \"capslock\" };\n"
440 "};",
441 .expected = GOLDEN_TESTS_OUTPUTS "include-capslock-system.xkb"
442 },
443 {
444 /* Use custom named map */
445 .keymap =
446 "xkb_keymap {\n"
447 " xkb_keycodes { <CAPS> = 66; };\n"
448 " xkb_symbols { include \"capslock(custom)\" };\n"
449 "};",
450 .expected = GOLDEN_TESTS_OUTPUTS "include-capslock-custom.xkb"
451 },
452 {
453 /* Use custom *explicit* default map */
454 .keymap =
455 "xkb_keymap {\n"
456 " xkb_keycodes { <RALT> = 108; <LVL3> = 92; };\n"
457 " xkb_types { include \"basic\" };\n"
458 " xkb_symbols { include \"level3\" };\n"
459 "};",
460 .expected = GOLDEN_TESTS_OUTPUTS "include-level3-explicit-default.xkb"
461 },
462 {
463 /* Use custom *implicit* default map */
464 .keymap =
465 "xkb_keymap {\n"
466 " xkb_keycodes { <> = 1; };\n"
467 " xkb_symbols { include \"banana\" };\n"
468 "};",
469 .expected = GOLDEN_TESTS_OUTPUTS "include-banana-implicit-default.xkb"
470 },
471 };
472
473 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
474 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
475 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
476 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
477 compile_buffer, NULL, __func__,
478 keymaps[k].keymap, strlen(keymaps[k].keymap),
479 keymaps[k].expected, update_output_files));
480 }
481
482 xkb_context_unref(ctx);
483 }
484
485 /* Test some limits related to allocations */
486 static void
487 test_alloc_limits(struct xkb_context *ctx, bool update_output_files)
488 {
489 const struct keymap_test_data keymaps[] = {
490 /* Keycodes */
491 {
492 .keymap =
493 "xkb_keymap {\n"
494 /* Valid & supported high keycode value */
495 " xkb_keycodes { <> = 0xfffffffe; };\n"
496 " xkb_symbols { key <> {[a]}; };\n"
497 "};",
498 .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-1.xkb"
499 },
500 {
501 .keymap =
502 "xkb_keymap {\n"
503 " xkb_keycodes {\n"
504 " <xxx> = 0xfffffffe;\n"
505 " <> = 0xfffffffd;\n"
506 " <> = 0xfffffffe;\n"
507 " };\n"
508 " xkb_symbols { key <> {[a]}; };\n"
509 "};",
510 .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-1.xkb"
511 },
512 {
513 .keymap =
514 "xkb_keymap {\n"
515 " xkb_keycodes {\n"
516 " <> = 7;\n"
517 " <> = 8;\n"
518 " <> = 0xfffffffd;\n"
519 " <> = 0xfffffffe;\n"
520 " };\n"
521 " xkb_symbols { key <> {[a]}; };\n"
522 "};",
523 .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-1.xkb"
524 },
525 {
526 .keymap =
527 "xkb_keymap {\n"
528 " xkb_keycodes {\n"
529 " <> = 0xfffffffd;\n"
530 " <> = 0xfffffffe;\n"
531 " <> = 7;\n"
532 " <> = 8;\n"
533 " };\n"
534 " xkb_symbols { key <> {[a]}; };\n"
535 "};",
536 .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-2.xkb"
537 },
538 {
539 .keymap =
540 "xkb_keymap {\n"
541 " xkb_keycodes {\n"
542 " <min> = 0xfffffffd;\n"
543 " <max> = 0xfffffffe;\n"
544 " };\n"
545 " xkb_symbols {\n"
546 " key <min> {[a]};\n"
547 " key <max> {[b]};\n"
548 " };\n"
549 "};",
550 .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-3.xkb"
551 },
552 {
553 .keymap =
554 "xkb_keymap {\n"
555 " xkb_keycodes {\n"
556 " <max> = 0xfffffffe;\n"
557 " <min> = 0xfffffffd;\n"
558 " };\n"
559 " xkb_symbols {\n"
560 " key <min> {[a]};\n"
561 " key <max> {[b]};\n"
562 " };\n"
563 "};",
564 .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-3.xkb"
565 },
566 {
567 .keymap =
568 "xkb_keymap {\n"
569 " xkb_keycodes {\n"
570 " <a> = 0xf;\n"
571 " <d> = 0x4000;\n"
572 " <e> = 0xfffffffe;\n"
573 " <c> = 0x2000;\n"
574 " <b> = 0xfff;\n"
575 " };\n"
576 " xkb_symbols {\n"
577 " key <a> {[a]};\n"
578 " key <b> {[b]};\n"
579 " key <c> {[c]};\n"
580 " key <d> {[d]};\n"
581 " key <e> {[e]};\n"
582 " };\n"
583 "};",
584 .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-4.xkb"
585 },
586 /* Key types */
587 {
588 .keymap =
589 "xkb_keymap {\n"
590 " xkb_types {\n"
591 /* Invalid level index */
592 " type \"X\" { map[none] = 0xfffffffe; };\n"
593 " };\n"
594 "};",
595 .expected = NULL
596 },
597 {
598 .keymap =
599 "xkb_keymap {\n"
600 " xkb_types {\n"
601 /* Invalid level index */
602 " type \"X\" {levelname[0xfffffffe]=\"x\";};\n"
603 " };\n"
604 "};",
605 .expected = NULL
606 }
607 };
608 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
609 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
610 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
611 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
612 compile_buffer, NULL, __func__,
613 keymaps[k].keymap, strlen(keymaps[k].keymap),
614 keymaps[k].expected, update_output_files));
615 }
616 }
617
618 static void
619 test_integers(struct xkb_context *ctx, bool update_output_files) {
620 /* Using a buffer without a terminating NULL. The following is a obvious
621 * syntax error, but it should still exit *cleanly*, not as previously where
622 * the use of strtoll would trigger a memory violation. */
623 const char not_null_terminated[] = { '1' };
624 assert(!test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
625 not_null_terminated, sizeof(not_null_terminated)));
626 const bool skipOverflow = sizeof(int64_t) >= sizeof (long long);
627 if (skipOverflow) {
628 fprintf(stderr,
629 "[WARNING] %s: cannot use checked arithmetic\n", __func__);
630 }
631
632 const struct keymap_test_data keymaps[] = {
633 {
634 .keymap =
635 "xkb_keymap {\n"
636 " xkb_keycodes {\n"
637 /* Out of range (expect 32 bits, got > 64 bits) */
638 " <> = 0x10000000000000000;\n"
639 " };\n"
640 "};",
641 .expected = NULL
642 },
643 {
644 .keymap =
645 "xkb_keymap {\n"
646 " xkb_keycodes { <> = 1; };\n"
647 " xkb_compat {\n"
648 " };\n"
649 " xkb_symbols {\n"
650 " key <> {\n"
651 /* Checked int64_t underflow */
652 " [MovePointer(x=-0x7fffffffffffffff - 2,\n"
653 " y=0)]\n"
654 " };\n"
655 " };\n"
656 "};",
657 .expected = GOLDEN_TESTS_OUTPUTS "integers-overflow.xkb",
658 .skip = skipOverflow
659 },
660 {
661 .keymap =
662 "xkb_keymap {\n"
663 " xkb_keycodes { <> = 1; };\n"
664 " xkb_compat {\n"
665 " };\n"
666 " xkb_symbols {\n"
667 " key <> {\n"
668 /* Checked int64_t underflow */
669 " [MovePointer(x=-0x7fffffffffffffff * 2,\n"
670 " y=0)]\n"
671 " };\n"
672 " };\n"
673 "};",
674 .expected = GOLDEN_TESTS_OUTPUTS "integers-overflow.xkb",
675 .skip = skipOverflow
676 },
677 {
678 .keymap =
679 "xkb_keymap {\n"
680 " xkb_keycodes { <> = 1; };\n"
681 " xkb_compat {\n"
682 " };\n"
683 " xkb_symbols {\n"
684 " key <> {\n"
685 " [MovePointer(x=0,\n"
686 /* Checked int64_t overflow */
687 " y=0x7fffffffffffffff + 1)]\n"
688 " };\n"
689 " };\n"
690 "};",
691 .expected = GOLDEN_TESTS_OUTPUTS "integers-overflow.xkb",
692 .skip = skipOverflow
693 },
694 {
695 .keymap =
696 "xkb_keymap {\n"
697 " xkb_keycodes { <> = 1; };\n"
698 " xkb_compat {\n"
699 " };\n"
700 " xkb_symbols {\n"
701 " key <> {\n"
702 " [MovePointer(x=0,\n"
703 /* Checked int64_t overflow */
704 " y=0x7fffffffffffffff * 2)]\n"
705 " };\n"
706 " };\n"
707 "};",
708 .expected = GOLDEN_TESTS_OUTPUTS "integers-overflow.xkb",
709 .skip = skipOverflow
710 },
711 {
712 .keymap =
713 "xkb_keymap {\n"
714 " xkb_keycodes {\n"
715 " <> = 1;\n"
716 " indicator 32 = \"xxx\";\n"
717 " };\n"
718 " xkb_compat {\n"
719 " group 0xffffffff = Mod5;\n"
720 " };\n"
721 " xkb_symbols {\n"
722 /* Computations with 64 bit ints that then fit into 16 bits */
723 " key <> {\n"
724 " actions[1 + -~0x100000001 / 0x100000000]=\n"
725 " [MovePointer(x=0x100000000 - 0xfffffffe,\n"
726 " y=~-0x7fff * 0x30000 / 0x2ffff)],\n"
727 /* Test (INT64_MIN + 1) and INT64_MAX */
728 " [MovePointer(x=-9223372036854775807\n"
729 " +9223372036854775807)]\n"
730 " };\n"
731 " };\n"
732 "};",
733 .expected = GOLDEN_TESTS_OUTPUTS "integers.xkb"
734 },
735 {
736 .keymap =
737 "xkb_keymap {\n"
738 " xkb_keycodes { <> = 1; };\n"
739 " xkb_symbols {\n"
740 /* We cannot parse INT64_MIN literal.
741 * If we could, the following should fit into 16 bits. */
742 " key <> {\n"
743 " [MovePointer(x=-9223372036854775808\n"
744 " +9223372036854775807)]\n"
745 " };\n"
746 " };\n"
747 "};",
748 .expected = NULL
749 },
750 {
751 .keymap =
752 "xkb_keymap {\n"
753 " xkb_keycodes {\n"
754 " <0> = 10;\n"
755 " <1> = 11;\n"
756 " <2> = 12;\n"
757 " <a> = 20;\n"
758 " <b> = 21;\n"
759 " <c> = 22;\n"
760 " };\n"
761 " xkb_types {\n"
762 " type \"FOUR_LEVEL\" {\n"
763 " modifiers = Shift + Control;\n"
764 " map[None] = 01;\n"
765 " map[Shift] = 2;\n"
766 " map[Control] = 3;\n"
767 " };\n"
768 " }\n;"
769 " xkb_compat {\n"
770 " interpret 0 { action = LockGroup(group=2); };\n"
771 " interpret 00 { action = LockGroup(group=3); };\n"
772 " interpret 0x0 { action = LockGroup(group=4); };\n"
773 " interpret 1 { action = LockGroup(group=-1); };\n"
774 " interpret 01 { action = LockGroup(group=1); };\n"
775 " interpret 2 { action = LockGroup(group=-2); };\n"
776 " interpret 02 { action = LockGroup(group=2); };\n"
777 " interpret 3 { action = LockGroup(group=-3); };\n"
778 " interpret 0x3 { action = LockGroup(group=3); };\n"
779 " };\n"
780 " xkb_symbols {\n"
781 /* Various numeric keysyms forms.
782 * Single digits have a special interpretation in symbols */
783 " key <0> { [ 0, 00, 0x0] };\n"
784 " key <1> { [ 1, 01, 0x1] };\n"
785 " key <2> { [ 2, 02, 0x2] };\n"
786 " key <a> { [ 3 ] };\n"
787 " key <b> { [ 04 ] };\n"
788 " key <c> { [ 0x5 ] };\n"
789 " modifier_map Shift { 0, 3 };\n"
790 " modifier_map Lock { 00, 04 };\n"
791 " modifier_map Control { 0x0, 0x5 };\n"
792 " };\n"
793 "};",
794 .expected = GOLDEN_TESTS_OUTPUTS "digits.xkb"
795 },
796 };
797 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
798 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
799 if (keymaps[k].skip) {
800 fprintf(stderr, "INFO: skip test\n");
801 continue;
802 }
803 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
804 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
805 compile_buffer, NULL, __func__,
806 keymaps[k].keymap, strlen(keymaps[k].keymap),
807 keymaps[k].expected, update_output_files));
808 }
809 }
810
811 static void
812 test_keycodes(struct xkb_context *ctx, bool update_output_files) {
813 const struct keymap_test_data keymaps[] = {
814 /*
815 * Ensure keycodes bound are correctly updated.
816 *
817 * Since expanding bounds is tested in the rest of the code, we test
818 * only shrinking here.
819 */
820
821 /* Single keycode */
822 {
823 .keymap =
824 "xkb_keymap {\n"
825 " xkb_keycodes {\n"
826 " <A> = 0;\n"
827 " override <A> = 1;\n"
828 " augment <A> = 300;\n"
829 " };\n"
830 "};",
831 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
832 },
833 {
834 .keymap =
835 "xkb_keymap {\n"
836 " xkb_keycodes {\n"
837 " <A> = 300;\n"
838 " override <A> = 1;\n"
839 " augment <A> = 0;\n"
840 " };\n"
841 "};",
842 /* NOTE: same as previous */
843 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
844 },
845 {
846 .keymap =
847 "xkb_keymap {\n"
848 " xkb_keycodes {\n"
849 " <A> = 0;\n"
850 " override <A> = 1;\n"
851 " override <A> = 301;\n"
852 " override <A> = 300;\n"
853 " };\n"
854 "};",
855 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-2.xkb"
856 },
857
858 /* Multiple keycodes to single keycode */
859 {
860 .keymap =
861 "xkb_keymap {\n"
862 " xkb_keycodes {\n"
863 " <A> = 300;\n"
864 " <B> = 1;\n"
865 " augment <B> = 301;\n"
866 " override <A> = 1;\n"
867 " };\n"
868 "};",
869 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
870 },
871 {
872 .keymap =
873 "xkb_keymap {\n"
874 " xkb_keycodes {\n"
875 " <A> = 0;\n"
876 " <B> = 1;\n"
877 " augment <B> = 300;\n"
878 " override <A> = 1;\n"
879 " };\n"
880 "};",
881 /* NOTE: same as previous */
882 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
883 },
884
885 /* Multiple keycodes to multiple keycodes */
886 {
887 .keymap =
888 "xkb_keymap {\n"
889 " xkb_keycodes {\n"
890 " <A> = 1;\n"
891 " <B> = 0;\n"
892 " override <B> = 300;\n"
893 " augment <A> = 0;\n"
894 " };\n"
895 "};",
896 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-1.xkb"
897 },
898 {
899 .keymap =
900 "xkb_keymap {\n"
901 " xkb_keycodes {\n"
902 " <A> = 301;\n"
903 " <B> = 300;\n"
904 " override <A> = 1;\n"
905 " augment <B> = 302;\n"
906 " };\n"
907 "};",
908 /* NOTE: same as previous */
909 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-1.xkb"
910 },
911
912 /* High keycodes */
913 {
914 .keymap =
915 "xkb_keymap {\n"
916 " xkb_keycodes {\n"
917 " <A> = 1;\n"
918 " include \"high\"\n"
919 " <B> = 2;\n"
920 " };\n"
921 "};",
922 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-2.xkb"
923 },
924 {
925 .keymap =
926 "xkb_keymap {\n"
927 " xkb_keycodes {\n"
928 " <A> = 0x10000;\n"
929 " <B> = 0x10001;\n"
930 " <A> = 1;\n"
931 " <B> = 0x10002;\n"
932 " };\n"
933 "};",
934 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-3.xkb"
935 },
936
937 /* Aliases */
938 {
939 .keymap =
940 "xkb_keymap {\n"
941 " xkb_keycodes {\n"
942 " <B> = 1;\n"
943 " <A> = 1;\n"
944 " alias <C> = <B>;\n" /* C target is invalid */
945 " };\n"
946 "};",
947 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
948 },
949 {
950 .keymap =
951 "xkb_keymap {\n"
952 " xkb_keycodes {\n"
953 " alias <C> = <B>;\n" /* C target is invalid */
954 " <B> = 1;\n"
955 " <A> = 1;\n"
956 " };\n"
957 "};",
958 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
959 },
960 {
961 .keymap =
962 "xkb_keymap {\n"
963 " xkb_keycodes {\n"
964 " <B> = 1;\n"
965 " <A> = 1;\n"
966 " alias <C> = <B>;\n" /* C target is valid */
967 " <B> = 2;\n"
968 " };\n"
969 "};",
970 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-1.xkb"
971 },
972 {
973 .keymap =
974 "xkb_keymap {\n"
975 " xkb_keycodes {\n"
976 " alias <C> = <B>;\n" /* C target is valid */
977 " <B> = 1;\n"
978 " <A> = 1;\n"
979 " <B> = 2;\n"
980 " };\n"
981 "};",
982 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-1.xkb"
983 },
984 {
985 .keymap =
986 "xkb_keymap {\n"
987 " xkb_keycodes {\n"
988 " alias <A> = <B>;\n"
989 " <A> = 1;\n" /* Override alias */
990 " <B> = 300;\n"
991 " };\n"
992 "};",
993 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-1.xkb"
994 },
995 {
996 .keymap =
997 "xkb_keymap {\n"
998 " xkb_keycodes {\n"
999 " alias <C> = <B>;\n"
1000 " augment <C> = 3;\n" /* Do not override alias */
1001 " <A> = 1;\n"
1002 " <B> = 2;\n"
1003 " };\n"
1004 "};",
1005 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-1.xkb"
1006 },
1007 {
1008 .keymap =
1009 "xkb_keymap {\n"
1010 " xkb_keycodes {\n"
1011 " <A> = 1;\n"
1012 " <C> = 3;\n"
1013 " alias <C> = <B>;\n" /* Override key */
1014 " <B> = 2;\n"
1015 " };\n"
1016 "};",
1017 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-1.xkb"
1018 },
1019 {
1020 .keymap =
1021 "xkb_keymap {\n"
1022 " xkb_keycodes {\n"
1023 " <A> = 1;\n"
1024 " augment alias <A> = <B>;\n" /* Do not override key */
1025 " <B> = 300;\n"
1026 " };\n"
1027 "};",
1028 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-1.xkb"
1029 },
1030 {
1031 .keymap =
1032 "xkb_keymap {\n"
1033 " xkb_keycodes {\n"
1034 " <A> = 1;\n"
1035 " <B> = 2;\n"
1036 " alias <B> = <C>;\n" /* Override key, even if invalid entry */
1037 " };\n"
1038 "};",
1039 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
1040 },
1041 {
1042 .keymap =
1043 "xkb_keymap {\n"
1044 " xkb_keycodes {\n"
1045 " <B> = 1;\n"
1046 " alias <A> = <B>;\n"
1047 " <A> = 1;\n" /* Override both alias and key */
1048 " };\n"
1049 "};",
1050 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
1051 },
1052 {
1053 .keymap =
1054 "xkb_keymap {\n"
1055 " xkb_keycodes {\n"
1056 /*
1057 * Multiple aliases *before* names: check that the key name LUT
1058 * is replaced with new allocation.
1059 */
1060 " alias <A> = <X>;\n"
1061 " alias <B> = <X>;\n"
1062 " alias <C> = <X>;\n"
1063 " alias <D> = <X>;\n"
1064 " alias <E> = <X>;\n"
1065 " alias <F> = <X>;\n"
1066 " alias <G> = <X>;\n"
1067 " alias <H> = <X>;\n"
1068 " alias <I> = <X>;\n"
1069 " alias <J> = <X>;\n"
1070 " alias <K> = <X>;\n"
1071 " alias <L> = <X>;\n"
1072 " alias <M> = <X>;\n"
1073 " alias <N> = <X>;\n"
1074 " alias <O> = <X>;\n"
1075 " alias <P> = <X>;\n"
1076 " <X> = 1;\n"
1077 " };\n"
1078 "};",
1079 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-2.xkb"
1080 },
1081 {
1082 .keymap =
1083 "xkb_keymap {\n"
1084 " xkb_keycodes {\n"
1085 /*
1086 * Multiple aliases *before* names: check that the key name LUT
1087 * overwrite is done properly after the last alias.
1088 */
1089 " alias <A> = <1>;\n"
1090 " alias <B> = <1>;\n"
1091 " alias <C> = <1>;\n"
1092 " alias <D> = <1>;\n"
1093 " alias <E> = <1>;\n"
1094 " <1> = 1;\n"
1095 " <2> = 2;\n"
1096 " <3> = 3;\n"
1097 " <4> = 4;\n"
1098 " <5> = 5;\n"
1099 " <6> = 6;\n"
1100 " <7> = 7;\n"
1101 " <8> = 8;\n"
1102 " <9> = 9;\n"
1103 " <10> = 10;\n"
1104 " <11> = 11;\n"
1105 " <12> = 12;\n"
1106 " };\n"
1107 "};",
1108 .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-3.xkb"
1109 },
1110 };
1111
1112 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
1113 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
1114 /*
1115 * We use a new context because we want to check key name LUT is
1116 * correctly implemented: it requires an empty atom table.
1117 */
1118 struct xkb_context * const ctx2 = test_get_context(CONTEXT_NO_FLAG);
1119 assert(ctx2);
1120 assert(test_compile_output(ctx2, XKB_KEYMAP_FORMAT_TEXT_V1,
1121 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1122 compile_buffer, NULL, __func__,
1123 keymaps[k].keymap, strlen(keymaps[k].keymap),
1124 keymaps[k].expected, update_output_files));
1125 xkb_context_unref(ctx2);
1126 }
1127
1128 /* Test random high keycodes do not trigger xkbcomp asserts */
1129 static const unsigned int max_iterations = 1000;
1130 char buffer[2048] = "";
1131 for (unsigned int i = 0; i < max_iterations; i++) {
1132 size_t available = sizeof(buffer);
1133 char *buf = buffer;
1134 int count = snprintf(buf, available,
1135 "default xkb_keymap { xkb_keycodes {\n");
1136 assert(count > 0 && (size_t) count < available);
1137 available -= (size_t) count;
1138 buf += count;
1139
1140 static const struct {
1141 xkb_keycode_t min;
1142 xkb_keycode_t max;
1143 unsigned int max_count;
1144 } bounds[] = {
1145 {
1146 .min = 10 << 3, /* Avoid issue with digits */
1147 .max = XKB_KEYCODE_MAX_CONTIGUOUS,
1148 .max_count = 3
1149 },
1150 {
1151 .min = XKB_KEYCODE_MAX_CONTIGUOUS + 1,
1152 .max = XKB_KEYCODE_MAX,
1153 .max_count = 10
1154 },
1155 };
1156
1157 xkb_keycode_t keycodes[13] = {0};
1158 unsigned int keycode_index = 0;
1159
1160 for (size_t b = 0; b < ARRAY_SIZE(bounds); b++) {
1161 assert(bounds[b].min < bounds[b].max);
1162 const unsigned int keycode_count = rand() % (bounds[b].max_count + 1);
1163 for (unsigned int k = 0; k < keycode_count; k++) {
1164 /* Note: we do not care about keycode uniqueness */
1165 const xkb_keycode_t kc =
1166 bounds[b].min + (rand() % (bounds[b].max - bounds[b].min + 1));
1167 assert(keycode_index < ARRAY_SIZE(keycodes));
1168 keycodes[keycode_index++] = kc;
1169 count = snprintf(buf, available,
1170 "<%"PRIu32"> = 0x%"PRIx32";\n", kc, kc);
1171 assert(count > 0 && (size_t) count < available);
1172 available -= (size_t) count;
1173 buf += count;
1174 }
1175 }
1176
1177 count = snprintf(buf, available, "}; xkb_symbols {\n");
1178 assert(count > 0 && (size_t) count < available);
1179 available -= (size_t) count;
1180 buf += count;
1181
1182 for (unsigned int k = 0; k < keycode_index; k++) {
1183 const xkb_keycode_t kc = keycodes[k];
1184 count = snprintf(buf, available,
1185 "key <%"PRIu32"> { [ 0x%"PRIx32" ] };\n",
1186 kc, (kc >> 3));
1187 assert(count > 0 && (size_t) count < available);
1188 available -= (size_t) count;
1189 buf += count;
1190 }
1191
1192 count = snprintf(buf, available, "}; };");
1193 assert(count > 0 && (size_t) count < available);
1194
1195 struct xkb_keymap * const keymap = xkb_keymap_new_from_string(
1196 ctx, buffer, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS
1197 );
1198 assert(keymap);
1199 for (unsigned int k = 0; k < keycode_index; k++) {
1200 const xkb_keycode_t kc = keycodes[k];
1201 const xkb_keysym_t *ks = NULL;
1202 count = xkb_keymap_key_get_syms_by_level(keymap, kc, 0, 0, &ks);
1203 assert_printf(count == 1, "%d\n", count);
1204 const xkb_keysym_t expected = kc >> 3;
1205 assert_printf(*ks == expected,
1206 "<%"PRIu32"> 0x%"PRIx32" != 0x%"PRIx32"\n",
1207 kc, *ks, expected);
1208 }
1209 xkb_keymap_unref(keymap);
1210 }
1211 }
1212
1213 static void
1214 test_masks(struct xkb_context *ctx, bool update_output_files) {
1215 const struct keymap_test_data keymaps[] = {
1216 {
1217 .keymap =
1218 "xkb_keymap {\n"
1219 " xkb_compat {\n"
1220 /* Cannot be negative */
1221 " virtual_modifiers Test1 = -1;\n"
1222 " };\n"
1223 "};",
1224 .expected = NULL
1225 },
1226 {
1227 .keymap =
1228 "xkb_keymap {\n"
1229 " xkb_compat {\n"
1230 /* Out of range (expect 32bits) */
1231 " virtual_modifiers Test1 = 0x100000000;\n"
1232 " };\n"
1233 "};",
1234 .expected = NULL
1235 },
1236 {
1237 .keymap =
1238 "xkb_keymap {\n"
1239 " xkb_compat {\n"
1240 /* Out of range (expect 32bits) */
1241 " virtual_modifiers Test1 = ~0x100000000;\n"
1242 " };\n"
1243 "};",
1244 .expected = NULL
1245 },
1246 {
1247 .keymap =
1248 "xkb_keymap {\n"
1249 " xkb_compat {\n"
1250 /* Unsupported operator */
1251 " virtual_modifiers Test1 = !Mod1;\n"
1252 " };\n"
1253 "};",
1254 .expected = NULL
1255 },
1256 {
1257 .keymap =
1258 "xkb_keymap {\n"
1259 " xkb_types {\n"
1260 /* Try range */
1261 " virtual_modifiers Test01 = 0;\n"
1262 " virtual_modifiers Test02 = 0xffffffff;\n"
1263 /* Try various operations on masks */
1264 " virtual_modifiers Test11 = 0xf0 + 0x0f;\n"
1265 " virtual_modifiers Test12 = 0xff - 0x0f;\n"
1266 " virtual_modifiers Test13 = ~0xf;\n"
1267 " virtual_modifiers Test14 = ~none;\n"
1268 " virtual_modifiers Test15 = ~all;\n"
1269 " virtual_modifiers Test16 = ~0xffffffff;\n"
1270 " virtual_modifiers Test17 = all - ~Mod1 + Mod2;\n"
1271 " type \"XXX\" {\n"
1272 " modifiers = Test12;\n"
1273 /* Masks mappings are not resolved here, so:
1274 * map[Test12 - Mod5] <=> map[Test12] */
1275 " map[Test12 - Mod5] = 2;\n"
1276 " };\n"
1277 " };\n"
1278 "};",
1279 .expected = GOLDEN_TESTS_OUTPUTS "masks.xkb"
1280 },
1281 {
1282 .keymap =
1283 "xkb_keymap {\n"
1284 " xkb_keycodes { <a> = 38; };\n"
1285 " xkb_symbols {\n"
1286 " virtual_modifiers X = 0xf0000000;\n"
1287 " key <a> { [ SetMods(mods = 0x00001100) ] };\n"
1288 " };\n"
1289 "};",
1290 .expected = GOLDEN_TESTS_OUTPUTS "masks-extra-bits.xkb"
1291 }
1292 };
1293 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
1294 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
1295 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
1296 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1297 compile_buffer, NULL, __func__,
1298 keymaps[k].keymap, strlen(keymaps[k].keymap),
1299 keymaps[k].expected, update_output_files));
1300 }
1301 }
1302
1303 static void
1304 test_interpret(struct xkb_context *ctx, bool update_output_files)
1305 {
1306 const struct keymap_test_data keymaps[] = {
1307 /* Invalid: empty string */
1308 {
1309 .keymap =
1310 "xkb_keymap {\n"
1311 " xkb_compat { interpret \"\" { repeat = false; } };\n"
1312 "};",
1313 .expected = NULL
1314 },
1315 /* Invalid UTF-8 encoding */
1316 {
1317 .keymap =
1318 "xkb_keymap {\n"
1319 " xkb_compat { interpret \"\xff\" { repeat = false; }; };\n"
1320 "};",
1321 .expected = NULL
1322 },
1323 /* Invalid single Unicode code point */
1324 {
1325 .keymap =
1326 "xkb_keymap {\n"
1327 " xkb_compat { interpret \"\\u{0}\" { repeat = false; }; };\n"
1328 "};",
1329 .expected = NULL
1330 },
1331 /* Invalid multiple Unicode code points */
1332 {
1333 .keymap =
1334 "xkb_keymap {\n"
1335 " xkb_compat { interpret \"ab\" { repeat = false; }; };\n"
1336 "};",
1337 .expected = NULL
1338 },
1339 /* Valid */
1340 {
1341 .keymap =
1342 u8"xkb_keymap {\n"
1343 u8" xkb_compat {\n"
1344 u8" interpret 0x1 { repeat = false; };\n"
1345 u8" interpret 0xB { repeat = false; };\n"
1346 u8" interpret Shift_L { repeat = false; };\n"
1347 u8" interpret a { repeat = false; };\n"
1348 u8" interpret \"ä\" { repeat = false; };\n"
1349 u8" interpret \"✨\" { repeat = false; };\n"
1350 u8" interpret \"🎺\" { repeat = false; };\n"
1351 u8" };\n"
1352 u8"};",
1353 .expected = GOLDEN_TESTS_OUTPUTS "compat-interpret.xkb"
1354 },
1355 };
1356
1357 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
1358 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
1359 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
1360 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1361 compile_buffer, NULL, __func__,
1362 keymaps[k].keymap, strlen(keymaps[k].keymap),
1363 keymaps[k].expected, update_output_files));
1364 }
1365 }
1366
1367 static void
1368 test_group_indices_names(struct xkb_context *ctx, bool update_output_files)
1369 {
1370 const struct {
1371 const char* keymap;
1372 const char* expected_v1;
1373 const char* expected_v2;
1374 } keymaps[] = {
1375 {
1376 .keymap =
1377 "xkb_keymap {\n"
1378 " xkb_keycodes {\n"
1379 " <> = 1;\n"
1380 " indicator 1 = \"1\";\n"
1381 " indicator 2 = \"2\";\n"
1382 " };\n"
1383 " xkb_compat {\n"
1384 " interpret a { action = SetGroup(group=Group1); };\n"
1385 " interpret b { action = SetGroup(group=-Group2); };\n"
1386 " interpret c { action = SetGroup(group=+Group3); };\n"
1387 " indicator \"1\" { groups = Group4; };\n"
1388 " indicator \"2\" { groups = All-Group1; };\n"
1389 " };\n"
1390 " xkb_symbols {\n"
1391 " name[Group1] = \"1\";\n"
1392 " name[Group2] = \"2\";\n"
1393 " name[Group3] = \"3\";\n"
1394 " name[Group4] = \"4\";\n"
1395 " key <> {\n"
1396 " groupsredirect=Group4,\n"
1397 " symbols[Group1]=[a],\n"
1398 " symbols[Group2]=[b],\n"
1399 " symbols[Group3]=[c],\n"
1400 " symbols[Group4]=[d]\n"
1401 " };\n"
1402 " };\n"
1403 "};",
1404 .expected_v1 = GOLDEN_TESTS_OUTPUTS "group-index-names-1.xkb",
1405 .expected_v2 = GOLDEN_TESTS_OUTPUTS "group-index-names-1.xkb"
1406 },
1407 {
1408 .keymap =
1409 "xkb_keymap {\n"
1410 " xkb_keycodes {\n"
1411 " <> = 1;\n"
1412 " indicator 1 = \"1\";\n"
1413 " indicator 2 = \"2\";\n"
1414 " };\n"
1415 " xkb_compat {\n"
1416 " interpret a { action = SetGroup(group=Group32); };\n"
1417 " interpret b { action = SetGroup(group=-Group32); };\n"
1418 " interpret c { action = SetGroup(group=+Group32); };\n"
1419 " indicator \"1\" { groups = Group32; };\n"
1420 " indicator \"2\" { groups = All-Group32; };\n"
1421 " };\n"
1422 " xkb_symbols {\n"
1423 " name[Group1] = \"1\";\n"
1424 " name[Group4] = \"4\";\n"
1425 " name[Group5] = \"5\";\n"
1426 " name[Group8] = \"8\";\n"
1427 " name[Group32] = \"32\";\n"
1428 " key <> {\n"
1429 " groupsredirect=Group32,\n"
1430 " symbols[Group1]=[a],\n"
1431 " symbols[Group4]=[b],\n"
1432 " symbols[Group5]=[c],\n"
1433 " symbols[Group8]=[d],\n"
1434 " symbols[Group32]=[e]\n"
1435 " };\n"
1436 " };\n"
1437 "};",
1438 .expected_v1 = NULL,
1439 .expected_v2 = GOLDEN_TESTS_OUTPUTS "group-index-names-2.xkb"
1440 },
1441 /* No names for groups above 32 */
1442 {
1443 .keymap =
1444 "xkb_keymap {\n"
1445 " xkb_compat { interpret a { action = SetGroup(group=Group33); }; };\n"
1446 "};",
1447 .expected_v1 = NULL,
1448 .expected_v2 = NULL
1449 },
1450 {
1451 .keymap =
1452 "xkb_keymap {\n"
1453 " xkb_keycodes { indicator 1 = \"1\"; };\n"
1454 " xkb_compat { indicator \"1\" { groups = Group33; }; };\n"
1455 "};",
1456 .expected_v1 = NULL,
1457 .expected_v2 = NULL
1458 },
1459 {
1460 .keymap =
1461 "xkb_keymap {\n"
1462 " xkb_keycodes { <> = 1; };\n"
1463 " xkb_symbols { name[Group33] = \"1\"; };\n"
1464 "};",
1465 .expected_v1 = NULL,
1466 .expected_v2 = NULL
1467 },
1468 {
1469 .keymap =
1470 "xkb_keymap {\n"
1471 " xkb_keycodes { <> = 1; };\n"
1472 " xkb_symbols { key <> { groupsredirect=Group33 };\n"
1473 "};",
1474 .expected_v1 = NULL,
1475 .expected_v2 = NULL
1476 },
1477 {
1478 .keymap =
1479 "xkb_keymap {\n"
1480 " xkb_keycodes { <> = 1; };\n"
1481 " xkb_symbols { key <> { symbols[Group33] = [a] };\n"
1482 "};",
1483 .expected_v1 = NULL,
1484 .expected_v2 = NULL
1485 }
1486 };
1487
1488 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
1489 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
1490 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
1491 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1492 compile_buffer, NULL, __func__,
1493 keymaps[k].keymap, strlen(keymaps[k].keymap),
1494 keymaps[k].expected_v1, update_output_files));
1495 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
1496 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1497 compile_buffer, NULL, __func__,
1498 keymaps[k].keymap, strlen(keymaps[k].keymap),
1499 keymaps[k].expected_v2, update_output_files));
1500 }
1501 }
1502
1503 static void
1504 test_level_indices_names(struct xkb_context *ctx,
1505 bool update_output_files)
1506 {
1507 const struct {
1508 const char* keymap;
1509 const char* expected;
1510 } keymaps[] = {
1511 {
1512 .keymap =
1513 "xkb_keymap {\n"
1514 " xkb_types {\n"
1515 " type \"X\" {\n"
1516 " modifiers = Shift;\n"
1517 " map[Shift] = Level2048;\n"
1518 " level_name[Level2048] = \"x\";\n"
1519 " };\n"
1520 " };\n"
1521 "};",
1522 .expected = GOLDEN_TESTS_OUTPUTS "level-index-names.xkb"
1523 },
1524 /* No names above 2048 */
1525 {
1526 .keymap =
1527 "xkb_keymap {\n"
1528 " xkb_types {\n"
1529 " type \"X\" {\n"
1530 " modifiers = Shift;\n"
1531 " map[Shift] = Level2049;\n"
1532 " };\n"
1533 " };\n"
1534 "};",
1535 .expected = NULL,
1536 },
1537 {
1538 .keymap =
1539 "xkb_keymap {\n"
1540 " xkb_types {\n"
1541 " type \"X\" {\n"
1542 " modifiers = Shift;\n"
1543 " level_name[Level2049] = \"x\";\n"
1544 " };\n"
1545 " };\n"
1546 "};",
1547 .expected = NULL,
1548 },
1549 };
1550
1551 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
1552 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
1553 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
1554 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1555 compile_buffer, NULL, __func__,
1556 keymaps[k].keymap, strlen(keymaps[k].keymap),
1557 keymaps[k].expected, update_output_files));
1558 }
1559 }
1560
1561 /* Test various multi-{keysym,action} syntaxes */
1562 static void
1563 test_multi_keysyms_actions(struct xkb_context *ctx, bool update_output_files)
1564 {
1565 /* Macro to create keymap for failing tests */
1566 #define make_keymap(xs) \
1567 "xkb_keymap {\n" \
1568 " xkb_keycodes {\n" \
1569 " <AE01> = 10;\n" \
1570 " };\n" \
1571 " xkb_types { include \"basic\" };\n" \
1572 " xkb_compat { include \"basic\" };\n" \
1573 " xkb_symbols {\n" \
1574 " key <AE01> { [" xs "] };\n" \
1575 " };\n" \
1576 "};"
1577
1578 #define make_keymaps_with(func, name, a, b, c, d) \
1579 /* Test: valid keymaps */ \
1580 { \
1581 .keymap = \
1582 "xkb_keymap {\n" \
1583 " xkb_keycodes {\n" \
1584 " <01> = 1;\n" \
1585 " <02> = 2;\n" \
1586 " <03> = 3;\n" \
1587 " <04> = 4;\n" \
1588 " <05> = 5;\n" \
1589 " <06> = 6;\n" \
1590 " <07> = 7;\n" \
1591 " <08> = 8;\n" \
1592 " <09> = 9;\n" \
1593 " <10> = 10;\n" \
1594 " <11> = 11;\n" \
1595 " <12> = 12;\n" \
1596 " <13> = 13;\n" \
1597 " <14> = 14;\n" \
1598 " <15> = 15;\n" \
1599 " };\n" \
1600 " xkb_types { include \"basic+extra\" };\n" \
1601 " xkb_compat { include \"basic\" };\n" \
1602 " xkb_symbols {\n" \
1603 " key <01> { [ "a "] };\n" \
1604 " key <02> { [ "a", "b" ] };\n" \
1605 " key <03> { [ {} ] };\n" \
1606 " key <04> { [ {}, "b" ] };\n" \
1607 " key <05> { [ "a", {} ] };\n" \
1608 " key <06> { [ {}, {} ] };\n" \
1609 " key <07> { [ { "a" } ] };\n" \
1610 " key <08> { [ { "a" }, { "b" } ] };\n" \
1611 " key <09> { [ { "a", "b" } ] };\n" \
1612 " key <10> { [ { "a", "b", "c" } ] };\n" \
1613 " key <11> { [ "a", { "b", "c" } ] };\n" \
1614 " key <12> { [ { "a", "b" }, "c" ] };\n" \
1615 " key <13> { [ { "a", "b" }, { "c", "d" } ] };\n" \
1616 " key <14> { [ { "a", "b" }, "c", { "d", "a" } ] };\n" \
1617 " key <15> { [ { "a", "b" }, { "c", "d" }, "a" ] };\n" \
1618 " };\n" \
1619 "};", \
1620 .expected = GOLDEN_TESTS_OUTPUTS "symbols-multi-" name ".xkb" \
1621 }, \
1622 /* Test: invalid keymaps */ \
1623 { .keymap = func("{ {} }") , .expected = NULL }, \
1624 { .keymap = func("{ "a", {} }") , .expected = NULL }, \
1625 { .keymap = func("{ {}, "b" }") , .expected = NULL }, \
1626 { .keymap = func("{ {}, {} }") , .expected = NULL }, \
1627 { .keymap = func("{ "a", { "b" } }") , .expected = NULL }, \
1628 { .keymap = func("{ { "a" }, "b" }") , .expected = NULL }, \
1629 { .keymap = func("{ { "a", "b" }, "c" }") , .expected = NULL }, \
1630 { .keymap = func("{ "a", { "b", "c" } }") , .expected = NULL }, \
1631 { .keymap = func("{ "a", {}, "c" }") , .expected = NULL }, \
1632 { .keymap = func("{ "a", "b", {} }") , .expected = NULL }, \
1633 { .keymap = func("{ {}, "b", "c" }") , .expected = NULL }, \
1634 { .keymap = func("{ { "a", "b" }, "c", "d" }") , .expected = NULL }, \
1635 { .keymap = func("{ "a", { "b", "c" }, "d" }") , .expected = NULL }, \
1636 { .keymap = func("{ "a", "b", { "c", "d" } }") , .expected = NULL }, \
1637 { .keymap = func("{ { "a", "b" }, { "c", "d" } }"), .expected = NULL }
1638
1639 const struct keymap_test_data keymaps[] = {
1640 make_keymaps_with(make_keymap, "keysyms",
1641 "a", "b", "c", "d"),
1642 make_keymaps_with(make_keymap, "actions",
1643 "SetMods(modifiers=Control)",
1644 "SetGroup(group=+1)",
1645 "Private(data=\"foo\")",
1646 "Private(data=\"bar\")"),
1647 {
1648 .keymap =
1649 "xkb_keymap {\n"
1650 " xkb_keycodes {\n"
1651 " <10> = 10;\n"
1652 " <11> = 11;\n"
1653 " <12> = 12;\n"
1654 " <13> = 13;\n"
1655 " <14> = 14;\n"
1656 " <15> = 15;\n"
1657 " <16> = 16;\n"
1658 " <17> = 17;\n"
1659 " <18> = 18;\n"
1660 " <19> = 19;\n"
1661 " <20> = 20;\n"
1662 " <21> = 21;\n"
1663 " <22> = 22;\n"
1664 " <23> = 23;\n"
1665 " <30> = 30;\n"
1666 " <31> = 31;\n"
1667 " <32> = 32;\n"
1668 " <33> = 33;\n"
1669 " <34> = 34;\n"
1670 " <35> = 35;\n"
1671 " <36> = 36;\n"
1672 " <37> = 37;\n"
1673 " <38> = 38;\n"
1674 " <39> = 39;\n"
1675 " };\n"
1676 " xkb_types { include \"basic+extra\" };\n"
1677 " xkb_compat {\n"
1678 " interpret 1 { action = {}; };\n"
1679 " interpret 2 { action = {NoAction()}; };\n"
1680 " interpret 3 { action = {SetMods()}; };\n"
1681 " interpret 4 { action = {SetMods(), SetGroup(group=1)}; };\n"
1682 " };\n"
1683 " xkb_symbols {\n"
1684 /* Empty keysyms */
1685 " key <10> { [any, any ] };\n"
1686 " key <11> { [{} , {} ] };\n"
1687 " key <12> { [any, any ], [SetMods(modifiers=Shift)] };\n"
1688 " key <13> { [{} , {} ], [SetMods(modifiers=Shift)] };\n"
1689 " key <14> { [any, any ], type = \"TWO_LEVEL\" };\n"
1690 " key <15> { [{} , {} ], type = \"TWO_LEVEL\" };\n"
1691 " key <16> { [a, A, any] };\n"
1692 " key <17> { [a, A, {} ] };\n"
1693 " key <18> { [a, A, any], type = \"FOUR_LEVEL_SEMIALPHABETIC\" };\n"
1694 " key <19> { [a, A, {} ], type = \"FOUR_LEVEL_SEMIALPHABETIC\" };\n"
1695 " key <20> { [a, A, ae, any] };\n"
1696 " key <21> { [a, A, ae, {} ] };\n"
1697 " key <22> { [a, A, ae, AE, any] };\n"
1698 " key <23> { [a, A, ae, AE, {} ] };\n"
1699 /* Empty actions */
1700 " key <30> { [NoAction(), NoAction() ] };\n"
1701 " key <31> { actions=[{}, {} ] };\n"
1702 " key <32> { [NoAction(), NoAction() ], [a] };\n"
1703 " key <33> { actions=[{}, {} ], [a] };\n"
1704 " key <34> { [NoAction(), NoAction() ], type = \"TWO_LEVEL\" };\n"
1705 " key <35> { actions=[{}, {} ], type = \"TWO_LEVEL\" };\n"
1706 " key <38> { [NoAction(), NoAction() ], type = \"FOUR_LEVEL_SEMIALPHABETIC\" };\n"
1707 " key <39> { actions=[{}, {} ], type = \"FOUR_LEVEL_SEMIALPHABETIC\" };\n"
1708 " };\n"
1709 "};",
1710 .expected = GOLDEN_TESTS_OUTPUTS "symbols-multi-keysyms-empty.xkb"
1711 }
1712 };
1713 for (size_t k = 0; k < ARRAY_SIZE(keymaps); k++) {
1714 fprintf(stderr, "------\n*** %s: #%zu ***\n", __func__, k);
1715 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
1716 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1717 compile_buffer, NULL, __func__,
1718 keymaps[k].keymap,
1719 strlen(keymaps[k].keymap),
1720 keymaps[k].expected,
1721 update_output_files));
1722 }
1723 #undef make_keymaps_with
1724 #undef make_keymap
1725
1726 /* V1 serialization */
1727 const char keymap_str[] =
1728 "xkb_keymap {\n"
1729 " xkb_keycodes { <> = 1; };\n"
1730 " xkb_compat { interpret A { action = {SetMods(), SetGroup()}; }; };\n"
1731 " xkb_symbols { key <> { [{SetMods(), SetGroup()}] }; };\n"
1732 "};";
1733 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
1734 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1735 compile_buffer, NULL, __func__,
1736 keymap_str, ARRAY_SIZE(keymap_str),
1737 GOLDEN_TESTS_OUTPUTS "symbols-multi-actions-v1.xkb",
1738 update_output_files));
1739 }
1740
1741 /* Test keysyms as strings */
1742 static void
1743 test_key_keysyms_as_strings(struct xkb_context *ctx, bool update_output_files)
1744 {
1745 const struct keymap_test_data keymaps[] = {
1746 /* Invalid UTF-8 encoding */
1747 {
1748 .keymap =
1749 "xkb_keymap {\n"
1750 " xkb_keycodes { <> = 10; };\n"
1751 " xkb_symbols {\n"
1752 /* Invalid byte at string index 2 */
1753 " key <> { [\"\xC3\xBC\xff\"] };\n"
1754 " };\n"
1755 "};",
1756 .expected = NULL
1757 },
1758 /* Valid */
1759 {
1760 .keymap =
1761 u8"xkb_keymap {\n"
1762 u8" xkb_keycodes {\n"
1763 u8" <10> = 10;\n"
1764 u8" <11> = 11;\n"
1765 u8" <12> = 12;\n"
1766 u8" <20> = 20;\n"
1767 u8" <21> = 21;\n"
1768 u8" <22> = 22;\n"
1769 u8" <23> = 23;\n"
1770 u8" <24> = 24;\n"
1771 u8" <25> = 25;\n"
1772 u8" <30> = 30;\n"
1773 u8" <31> = 31;\n"
1774 u8" <32> = 32;\n"
1775 u8" <33> = 33;\n"
1776 u8" <34> = 34;\n"
1777 u8" <35> = 35;\n"
1778 u8" <40> = 40;\n"
1779 u8" <41> = 41;\n"
1780 u8" <42> = 42;\n"
1781 u8" <50> = 50;\n"
1782 u8" <51> = 51;\n"
1783 u8" <52> = 52;\n"
1784 u8" <60> = 60;\n"
1785 u8" <61> = 61;\n"
1786 u8" <62> = 62;\n"
1787 u8" <63> = 63;\n"
1788 u8" <64> = 64;\n"
1789 u8" <65> = 65;\n"
1790 u8" <66> = 66;\n"
1791 u8" <67> = 67;\n"
1792 u8" <68> = 68;\n"
1793 u8" <69> = 69;\n"
1794 u8" <70> = 70;\n"
1795 u8" <71> = 71;\n"
1796 u8" <72> = 72;\n"
1797 u8" <73> = 73;\n"
1798 u8" <74> = 74;\n"
1799 u8" <AD08> = 80;\n"
1800 u8" <AC05> = 81;\n"
1801 u8" <AB05> = 82;\n"
1802 u8" <AD01> = 83;\n"
1803 u8" };\n"
1804 u8" xkb_types { include \"basic\" };\n"
1805 u8" xkb_compat {\n"
1806 u8" interpret.action = SetMods();\n"
1807 u8" interpret \"ä\" {};\n"
1808 u8" interpret \"✨\" {};\n"
1809 u8" interpret \"🎺\" {};\n"
1810 u8" interpret \"\\u{1F54A}\" {};\n"
1811 u8" interpret \"\\u{1}\" {};\n"
1812 u8" interpret \"\\u{a}\" {};\n"
1813 u8" interpret \"\\u{1f}\" {};\n"
1814 u8" interpret \"\\u{20}\" {};\n"
1815 u8" interpret \"\\u{7e}\" {};\n"
1816 u8" interpret \"\\u{7f}\" {};\n"
1817 u8" interpret \"\\u{80}\" {};\n"
1818 u8" interpret \"\\u{9f}\" {};\n"
1819 u8" interpret \"\\u{a0}\" {};\n"
1820 u8" interpret \"\\u{ff}\" {};\n"
1821 // u8" interpret \"\\u{d800}\" {};\n"
1822 // u8" interpret \"\\u{dfff}\" {};\n"
1823 u8" interpret \"\\u{fdd0}\" {};\n"
1824 u8" interpret \"\\u{fdef}\" {};\n"
1825 u8" interpret \"\\u{fffe}\" {};\n"
1826 u8" interpret \"\\u{ffff}\" {};\n"
1827 u8" interpret \"\\u{10000}\" {};\n"
1828 u8" interpret \"\\u{1ffff}\" {};\n"
1829 u8" interpret \"\\u{10ffff}\" {};\n"
1830 u8" };\n"
1831 u8" xkb_symbols {\n"
1832 /* Empty string */
1833 u8" key <10> { [\"\", {b, \"\", c}] };\n"
1834 u8" key <11> { [{a, \"\"}, {b, \"\"}] };\n"
1835 u8" key <12> { [{\"\"}, {\"\", \"\"}] };\n"
1836 /* Single string: Plain */
1837 u8" key <20> { [\"a\", \"bc\"] };\n"
1838 u8" key <23> { [\"✨\", \"🎺\"] };\n" // U+2728 ✨, U+1F3BA 🎺
1839 u8" key <24> { [\"u\u0308\"] };\n" // u + U+0308 ◌̈ COMBINING DIAERESIS
1840 u8" key <25> { [\"∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა\"] };\n"
1841 /* Single string: Nested */
1842 u8" key <30> { [{\"a\" }, {\"bc\" }] };\n"
1843 u8" key <31> { [{\"a\", \"\"}, {\"bc\", \"\"}] };\n"
1844 u8" key <32> { [{\"\", \"a\"}, {\"\", \"bc\"}] };\n"
1845 u8" key <33> { [{\"\\u{1F54A}\"}, {\"\\u{1f3f3}\\u{fe0f}\"}] };\n" // U+1F54A 🕊️, 🏳️
1846 u8" key <34> { [{\"u\\u{0308}\"}] };\n" // u + U+0308 ◌̈ COMBINING DIAERESIS
1847 u8" key <35> { [{\"∀\\u{0}∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა\"}] };\n"
1848 /* Multi: string, literal */
1849 u8" key <40> { [{\"a\", b}, {\"cde\", f}] };\n"
1850 u8" key <41> { [{\"a\", \"\", b}, {\"cde\", \"\", f}] };\n"
1851 u8" key <42> { [{\"a\", b, \"\"}, {\"cde\", f, \"\"}] };\n"
1852 /* Multi: literal, string */
1853 u8" key <50> { [{a, \"b\"}, {c, \"def\"}] };\n"
1854 u8" key <51> { [{a, \"\", \"b\"}, {c, \"\", \"def\"}] };\n"
1855 u8" key <52> { [{a, \"b\", \"\"}, {c, \"def\", \"\"}] };\n"
1856 /* Multi: string, string */
1857 u8" key <60> { [{\"a\", \"b\"}, {\"cd\", \"ef\"}] };\n"
1858 u8" key <61> { [{\"a\", \"\", \"b\"}, {\"cd\", \"\", \"ef\"}] };\n"
1859 u8" key <63> { [{\"a\", \"bcd\"}, {\"efg\", \"h\"}] };\n"
1860 u8" key <64> { [{\"a\", \"\", \"bcd\"}, {\"efg\", \"\", \"h\"}] };\n"
1861 /* Some special Unicode code points */
1862 u8" key <65> { [\"\\u{0}\", \"\\u{10ffff}\"] };\n"
1863 u8" key <66> { [\"\\u{1}\", \"\\u{a}\"] };\n"
1864 u8" key <67> { [\"\\u{1f}\", \"\\u{20}\"] };\n"
1865 u8" key <68> { [\"\\u{7e}\", \"\\u{7f}\"] };\n"
1866 u8" key <69> { [\"\\u{80}\", \"\\u{9f}\"] };\n"
1867 u8" key <70> { [\"\\u{a0}\", \"\\u{ff}\"] };\n"
1868 u8" key <71> { [\"\\u{d800}\", \"\\u{dfff}\"] };\n"
1869 u8" key <72> { [\"\\u{fdd0}\", \"\\u{fdef}\"] };\n"
1870 u8" key <73> { [\"\\u{fffe}\", \"\\u{ffff}\"] };\n"
1871 u8" key <74> { [\"\\u{10000}\", \"\\u{1ffff}\"] };\n"
1872 /* Example from the doc */
1873 u8" key <AD08> { [ \"ij\" , \"IJ\" ] }; // IJ Dutch digraph\n"
1874 u8" key <AC05> { [ \"g̃\" , \"G̃\" ] }; // G̃ Guarani letter\n"
1875 /* NOTE: We use U+200E LEFT-TO-RIGHT MARK in order to display the strings in
1876 * in the proper order. */
1877 u8" key <AB05> { [ \"لا\"‎ , \"لآ\"‎ ] }; // ⁧لا⁩ ⁧لآ⁩ Arabic Lam-Alef ligatures decomposed\n"
1878 u8" key <AD01> { [ \"c’h\", \"C’h\" ] }; // C’H Breton trigraph\n"
1879 u8" modifier_map Mod1 { \"✨\", \"\\u{1F54A}\" };\n"
1880 u8" };\n"
1881 u8"};",
1882 .expected = GOLDEN_TESTS_OUTPUTS "string-as-keysyms.xkb"
1883 },
1884 };
1885
1886 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
1887 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
1888 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
1889 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
1890 compile_buffer, NULL, __func__,
1891 keymaps[k].keymap, strlen(keymaps[k].keymap),
1892 keymaps[k].expected, update_output_files));
1893 }
1894 }
1895
1896 static void
1897 test_invalid_symbols_fields(struct xkb_context *ctx)
1898 {
1899 /* Any of the following is invalid syntax, but also use to trigger
1900 * a NULL pointer deference, thus this regression test */
1901 const char * const keymaps[] = {
1902 "xkb_keymap {\n"
1903 " xkb_keycodes { <> = 9; };\n"
1904 " xkb_symbols { key <> { vmods = [] }; };\n"
1905 "};",
1906 "xkb_keymap {\n"
1907 " xkb_keycodes { <> = 9; };\n"
1908 " xkb_symbols { key <> { repeat = [] }; };\n"
1909 "};",
1910 "xkb_keymap {\n"
1911 " xkb_keycodes { <> = 9; };\n"
1912 " xkb_symbols { key <> { type = [] }; };\n"
1913 "};",
1914 "xkb_keymap {\n"
1915 " xkb_keycodes { <> = 9; };\n"
1916 " xkb_symbols { key <> { groupswrap = [] }; };\n"
1917 "};",
1918 "xkb_keymap {\n"
1919 " xkb_keycodes { <> = 9; };\n"
1920 " xkb_symbols { key <> { groupsredirect = [] }; };\n"
1921 "};",
1922 /* Used to parse without error because the “repeats” key field is valid,
1923 * but we should fail in the following 2 keymaps */
1924 "xkb_keymap {\n"
1925 " xkb_keycodes { <> = 9; };\n"
1926 " xkb_symbols { key <> { vmods=[], repeats=false }; };\n"
1927 "};",
1928 "xkb_keymap {\n"
1929 " xkb_keycodes { <> = 9; };\n"
1930 " xkb_symbols { key <> { repeats=false, vmods=[] }; };\n"
1931 "};"
1932 };
1933 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
1934 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
1935 const struct xkb_keymap *keymap =
1936 test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
1937 keymaps[k], strlen(keymaps[k]));
1938 assert(!keymap);
1939 }
1940 }
1941
1942 static void
1943 test_modifier_maps(struct xkb_context *ctx, bool update_output_files)
1944 {
1945 /* Only accept key and keysyms in the modifier_map list */
1946 struct keymap_test_data keymaps[] = {
1947 {
1948 .keymap =
1949 u8"xkb_keymap {\n"
1950 u8" xkb_keycodes {\n"
1951 u8" <CAPS> = 66;\n"
1952 u8" alias <LOCK> = <CAPS>;\n"
1953 u8" <0> = 0;"
1954 u8" <1> = 1;"
1955 u8" <2> = 2;"
1956 u8" <3> = 3;"
1957 u8" <4> = 4;"
1958 u8" <5> = 5;"
1959 u8" <6> = 6;"
1960 u8" <7> = 7;"
1961 u8" <any> = 10;"
1962 u8" <none> = 11;"
1963 u8" <a> = 61;"
1964 u8" <b> = 62;"
1965 u8" <c> = 63;"
1966 u8" <100> = 100;"
1967 u8" };\n"
1968 u8" xkb_types { include \"basic\" };\n"
1969 u8" xkb_symbols {\n"
1970 u8" key <CAPS> { [Caps_Lock] };\n"
1971 u8" key <any> { [any, A] };\n"
1972 u8" key <none> { [none, N] };\n"
1973 u8" key <0> { [0] };\n"
1974 u8" key <1> { [1] };\n"
1975 u8" key <2> { [2] };\n"
1976 u8" key <a> { [a] };\n"
1977 u8" key <b> { [b] };\n"
1978 u8" key <c> { [\"🎺\"] };\n"
1979 u8" key <3> { [NotAKeysym, 3] };\n"
1980 u8" key <4> { [\"\\u{0000001F54A}\"]};\n"
1981 u8" key <5> { [\"\\u{d800}\", \"\\u{dfff}\"]};\n"
1982 u8" key <6> { [\"\\u{fdd0}\", \"\\u{fdef}\"]};\n"
1983 u8" key <7> { [\"\\u{fffe}\", \"\\u{ffff}\"]};\n"
1984 u8" key <100> { [C] };\n"
1985 u8" modifier_map Lock {\n"
1986 u8" <100>, <LOCK>, any, none,\n"
1987 u8" 0, 1, 0x2, a, \"b\", \"🎺\", \"\\u{1F54A}\",\n"
1988 u8" \"\\u{d800}\", \"\\u{dfff}\",\n"
1989 u8" \"\\u{fdd0}\", \"\\u{fdef}\",\n"
1990 u8" \"\\u{fffe}\", \"\\u{ffff}\",\n"
1991 u8" NotAKeysym\n"
1992 u8" };\n"
1993 u8" };\n"
1994 u8"};",
1995 .expected = GOLDEN_TESTS_OUTPUTS "symbols-modifier_map.xkb"
1996 },
1997 /* Invalid: empty string */
1998 {
1999 .keymap =
2000 "xkb_keymap {\n"
2001 " xkb_symbols { modifier_map Lock { \"\" }; };\n"
2002 "};",
2003 .expected = NULL
2004 },
2005 /* Invalid Unicode encoding */
2006 {
2007 .keymap =
2008 "xkb_keymap {\n"
2009 " xkb_symbols { modifier_map Lock { \"\xff\" }; };\n"
2010 "};",
2011 .expected = NULL
2012 },
2013 /* Invalid single Unicode code point */
2014 {
2015 .keymap =
2016 "xkb_keymap {\n"
2017 " xkb_symbols { modifier_map Lock { \"\\u{0}\" }; };\n"
2018 "};",
2019 .expected = NULL
2020 },
2021 /* Invalid multiple Unicode code points */
2022 {
2023 .keymap =
2024 "xkb_keymap {\n"
2025 " xkb_symbols { modifier_map Lock { \"ab\" }; };\n"
2026 "};",
2027 .expected = NULL
2028 },
2029 /* Invalid type: list */
2030 {
2031 .keymap =
2032 "xkb_keymap {\n"
2033 " xkb_symbols { modifier_map Lock { [a] }; };\n"
2034 "};",
2035 .expected = NULL
2036 },
2037 /* Invalid type: list */
2038 {
2039 .keymap =
2040 "xkb_keymap {\n"
2041 " xkb_symbols { modifier_map Lock { {a, b} }; };\n"
2042 "};",
2043 .expected = NULL
2044 },
2045 };
2046
2047 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
2048 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
2049 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2050 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2051 compile_buffer, NULL, __func__,
2052 keymaps[k].keymap, strlen(keymaps[k].keymap),
2053 keymaps[k].expected, update_output_files));
2054 }
2055 }
2056
2057 static void
2058 test_empty_compound_statements(struct xkb_context *ctx, bool update_output_files)
2059 {
2060 struct keymap_test_data keymaps[] = {
2061 {
2062 .keymap =
2063 "xkb_keymap {\n"
2064 " xkb_keycodes {\n"
2065 " <Q> = 24;\n"
2066 " <W> = 25;\n"
2067 " <E> = 26;\n"
2068 " <R> = 27;\n"
2069 " <T> = 28;\n"
2070 " <Y> = 29;\n"
2071 " <U> = 30;\n"
2072 " <I> = 31;\n"
2073 " <O> = 32;\n"
2074 " <P> = 33;\n"
2075 " <A> = 38;\n"
2076 " <S> = 39;\n"
2077 " <D> = 40;\n"
2078 " <F> = 41;\n"
2079 " <G> = 42;\n"
2080 " <H> = 43;\n"
2081 " <Z> = 52;\n"
2082 " <X> = 53;\n"
2083 " <C> = 54;\n"
2084 " <V> = 55;\n"
2085 " <B> = 56;\n"
2086 " <N> = 57;\n"
2087 " <M> = 58;\n"
2088 " };\n"
2089 " xkb_types {\n"
2090 " type \"t1\" {};\n"
2091 " type \"t2\" { modifiers = Shift; map[Shift] = 2; };\n"
2092 " };\n"
2093 " xkb_compat {\n"
2094 " virtual_modifiers M1, M2;\n"
2095 " indicator \"xxx\" {};\n"
2096 " indicator.modifiers = Shift;"
2097 " indicator \"yyy\" {};\n"
2098 "\n"
2099 " interpret q {};\n"
2100 " interpret.repeat = true;\n"
2101 " interpret w {};\n"
2102 " };\n"
2103 " xkb_symbols {\n"
2104 " key <Q> { [q] };\n"
2105 " key <W> { [SetMods()] };\n"
2106 " key <E> { [e], type = \"t1\" };\n"
2107 /* Empty key */
2108 " key <R> {};\n"
2109 /* Empty key, same as previous */
2110 " key <Y> { [] };\n"
2111 /* Override empty key with another */
2112 " key <T> {};\n"
2113 " key <T> { [] };\n"
2114 /* Override empty key with some symbols */
2115 " key <U> {};\n"
2116 " key <U> { [], [u] };\n"
2117 /* Override non-empty key with an empty key */
2118 " key <I> { [i] };\n"
2119 " key <I> {};\n"
2120 /* Empty symbols/actions */
2121 " key <O> { [NoSymbol] };\n"
2122 " key <P> { [NoAction()] };\n"
2123 /* No symbols nor actions: other properties */
2124 " key <A> { vmods = M1 };\n"
2125 " key <S> { repeat = false };\n"
2126 " key <D> { type = \"t2\" };\n"
2127 " key <F> { [], type = \"t2\" };\n"
2128 " key <G> { type[1] = \"t2\" };\n"
2129 " key <H> { type[1] = \"t1\", type[2] = \"t2\" };\n"
2130 /* Test key defaults / modmaps */
2131 " key <Z> {};\n"
2132 " key.vmods = M1;\n"
2133 " key <X> {};\n"
2134 " key <C> { vmods = M2 };\n"
2135 " key.type = \"t2\";\n"
2136 " key <V> { vmods = 0 };\n"
2137 " key <B> { [], vmods = 0 };\n"
2138 " key.type[1] = \"t1\";\n"
2139 " key <N> { vmods = 0 };\n"
2140 " key <M> { [], [], vmods = 0 };\n"
2141 " modmap Shift { <Z> };\n"
2142 " modmap Lock { <X> };\n"
2143 " modmap Control { <C> };\n"
2144 " };\n"
2145 "};",
2146 .expected = GOLDEN_TESTS_OUTPUTS "empty-compound-statements.xkb"
2147 },
2148 };
2149
2150 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
2151 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
2152 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2153 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2154 compile_buffer, NULL, __func__,
2155 keymaps[k].keymap, strlen(keymaps[k].keymap),
2156 keymaps[k].expected, update_output_files));
2157 }
2158 }
2159
2160 static void
2161 test_escape_sequences(struct xkb_context *ctx, bool update_output_files)
2162 {
2163 /* Similarly to `test_integers`, test that escape sequences at the end of
2164 * a buffer raise a syntax error but no memory violation */
2165 const char bad_octal[] = { '"', '\\', '1' };
2166 assert(!test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2167 bad_octal, sizeof(bad_octal)));
2168 const char bad_unicode[] = { '"', '\\', 'u', '{', '1' };
2169 assert(!test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2170 bad_unicode, sizeof(bad_unicode)));
2171
2172 const char keymap[] =
2173 "default xkb_keymap \"\" {\n"
2174 " xkb_keycodes "
2175 "\"\\u{0}La paix est la seule\\tbataille "
2176 "qui vaille la peine d\\u{02019}\\u{Ea}tre men\\303\\251e.\\n\" {\n"
2177 " <> = 1;\n"
2178 " indicator 1 = \"\\0\\n\\u{2328}\\u{fe0f}\";\n"
2179 " indicator 2 = \"surrogates: \\u{d800} \\u{dfff}\";\n"
2180 " indicator 3 = \"noncharacters: \\u{fdd0} \\u{fdef} \\u{fffe} \\u{ffff}\";\n"
2181 " indicator 4 = \"noncharacters: \\u{1fffe} \\u{1ffff} \\u{10fffe} \\u{10ffff}\";\n"
2182 " indicator 5 = \"out of range: \\u{0} a \\u{110000} \\u{ffffffffffff}\";\n"
2183 " indicator 6 = \"invalid: \\u a \\uA b \\u{} c \\u{ d \\u} e \\u{1\";\n"
2184 " indicator 7 = \"invalid: \\u{1234x\\\" y \";\n"
2185 " indicator 8 = \"invalid: \\u{ 2} x \\u{3 } y\";\n"
2186 " indicator 9 = \"\\u{+1} \\u{-1} \\u{x} \\u{#} \\u{\\0} \\u{\\}\";\n"
2187 " };\n"
2188 " xkb_types \"\\00001\\\\\\00427\\u{22}\\r\\n\" {\n"
2189 " type \"\\0\\00451\\u{1F3BA}\\u{2728}\\u{01F54a}\\u{0fE0f}\\f\" {\n"
2190 " modifiers = Shift;\n"
2191 " map[Shift] = 2;\n"
2192 " level_name[1] = "
2193 "\"O\\u{f9} ils ont fait un \\u{22}d\\303\\251sert\\u{22}, \\e"
2194 "ils disent qu\\u{002019}ils \\12ont fait la \\42paix\\042.\\b\\n\";\n"
2195 " level_name[2] = "
2196 "\"\\u{0000}Science \\u{73}\\141ns conscience "
2197 "n\\u{2019}est que rui\\\\ne\\u{A} de l\\u{02019}\\u{E2}me.\\r\";\n"
2198 " };\n"
2199 " };\n"
2200 " xkb_compat "
2201 "\"Le c\\u{153}ur a \\v ses raisons "
2202 "que la raison ne con\\u{5C}na\\u{EE}t point\" {\n"
2203 " indicator \"\\n\\u{2328}\\0\\u{fe0f}\" { modifiers = Shift; };\n"
2204 " };\n"
2205 " xkb_symbols "
2206 "\"La libert\\u{00e9} commence "
2207 "o\\u{0000f9} l\\342\\200\\231ignorance finit.\" {\n"
2208 " name[1] = \"\\n\\0377\\3760\";\n"
2209 " name[2] = \"\\00427\";\n"
2210 " key <> {\n"
2211 " symbols[1]=[a, A],\n"
2212 " type[1]=\"%1\\u{1F3BA}\\u{2728}\\u{00001F54a}\\u{0fE0f}\\u{0C}\",\n"
2213 " actions[2]=[Private(type=123, data=\"\0abcdefg\")]"
2214 " };\n"
2215 " };\n"
2216 "};";
2217 const char expected[] = GOLDEN_TESTS_OUTPUTS "escape-sequences.xkb";
2218 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2219 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2220 compile_buffer, NULL, __func__,
2221 keymap, ARRAY_SIZE(keymap),
2222 expected, update_output_files));
2223 }
2224
2225 static void
2226 test_unicode_keysyms(struct xkb_context *ctx, bool update_output_files)
2227 {
2228 struct keymap_test_data keymaps[] = {
2229 {
2230 .keymap =
2231 "xkb_keymap {\n"
2232 " xkb_keycodes { include \"evdev\" };\n"
2233 " xkb_types { include \"basic\" };\n"
2234 " xkb_symbols {\n"
2235 /* C0 Control characters */
2236 " key <AE01> { [U0000, 0x01000000 ] };\n"
2237 " key <AE02> { [U0001, 0x01000001 ] };\n"
2238 " key <AE03> { [U000A, 0x0100000a ] };\n"
2239 " key <AE04> { [U001F, 0x0100001f ] };\n"
2240 /* Printable ASCII characters */
2241 " key <AE05> { [U0020, 0x01000020 ] };\n"
2242 " key <AE06> { [U007E, 0x0100007e ] };\n"
2243 /* C0/C1 control characters */
2244 " key <AE07> { [U007F, 0x0100007f ] };\n"
2245 " key <AE08> { [U0080, 0x01000080 ] };\n"
2246 " key <AE09> { [U009F, 0x0100009f ] };\n"
2247 /* Latin-1 printable characters */
2248 " key <AE10> { [U00A0, 0x010000a0 ] };\n"
2249 " key <AE11> { [U00FF, 0x010000ff ] };\n"
2250 /* Misc: bounds */
2251 " key <AD01> { [U0100, 0x01000100 ] };\n"
2252 " key <AD02> { [UD7FF, 0x0100d7ff ] };\n"
2253 /* Surrogates */
2254 " key <AD03> { [UD800, 0x0100d800 ] };\n"
2255 " key <AD04> { [UDFFF, 0x0100dfff ] };\n"
2256 /* Misc: bounds */
2257 " key <AD05> { [UE000, 0x0100e000 ] };\n"
2258 " key <AD06> { [UFDCF, 0x0100fdcf ] };\n"
2259 /* Noncharacters */
2260 " key <AD07> { [UFDD0, 0x0100fdd0 ] };\n"
2261 " key <AD08> { [UFDEF, 0x0100fdef ] };\n"
2262 /* Misc: bounds */
2263 " key <AD09> { [UFDF0, 0x0100fdf0 ] };\n"
2264 " key <AD10> { [UFFFD, 0x0100fffd ] };\n"
2265 /* Noncharacters */
2266 " key <AD11> { [UFFFE, 0x0100fffe ] };\n"
2267 " key <AD12> { [UFFFF, 0x0100ffff ] };\n"
2268 /* Misc: bounds */
2269 " key <AC01> { [U10000, 0x01010000 ] };\n"
2270 /* Noncharacters */
2271 " key <AC08> { [U1FFFE , 0x0101fffe ] };\n"
2272 " key <AC09> { [U1FFFF , 0x0101ffff ] };\n"
2273 " key <AC10> { [U10FFFE, 0x0110fffe ] };\n"
2274 /* Max Unicode */
2275 " key <AC11> { [U10FFFF, 0x0110ffff ] };\n"
2276 /* Max Unicode + 1 */
2277 " key <AC12> { [U110000, 0x01110000 ] };\n"
2278 /* Misc */
2279 " key <AB01> { [U0174, 0x01000174 ] };\n"
2280 " };\n"
2281 "};",
2282 .expected = GOLDEN_TESTS_OUTPUTS "unicode-keysyms.xkb"
2283 },
2284 };
2285
2286 for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
2287 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
2288 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2289 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2290 compile_buffer, NULL, __func__,
2291 keymaps[k].keymap, strlen(keymaps[k].keymap),
2292 keymaps[k].expected, update_output_files));
2293 }
2294 }
2295
2296 static void
2297 test_no_action_void_action(struct xkb_context *ctx, bool update_output_files)
2298 {
2299 const char keymap_str[] =
2300 "xkb_keymap {\n"
2301 " xkb_keycodes { <1> = 1; <2> = 2; <3> = 3; };\n"
2302 " xkb_symbols {\n"
2303 /* These actions take no argument */
2304 " key <1> { [x], [NoAction(mods=1)] };\n"
2305 " key <2> { [y], [VoidAction(mods=1)] };\n"
2306 /* Check that we can overwrite NoAction, but not the contrary */
2307 " key <3> { [NoAction()] };\n"
2308 " key <3> { [VoidAction()] };\n"
2309 " key <3> { [NoAction()] };\n"
2310 " };\n"
2311 "};";
2312 /* v1 → v1 */
2313 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2314 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2315 compile_buffer, NULL, __func__,
2316 keymap_str, sizeof(keymap_str),
2317 GOLDEN_TESTS_OUTPUTS "no_void_action-v1.xkb",
2318 update_output_files));
2319 /* v1 → v2 */
2320 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2321 XKB_KEYMAP_FORMAT_TEXT_V2,
2322 compile_buffer, NULL, __func__,
2323 keymap_str, sizeof(keymap_str),
2324 GOLDEN_TESTS_OUTPUTS "no_void_action-v2.xkb",
2325 update_output_files));
2326 /* v2 → v2 */
2327 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
2328 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2329 compile_buffer, NULL, __func__,
2330 keymap_str, sizeof(keymap_str),
2331 GOLDEN_TESTS_OUTPUTS "no_void_action-v2.xkb",
2332 update_output_files));
2333 /* v2 → v1 */
2334 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
2335 XKB_KEYMAP_FORMAT_TEXT_V1,
2336 compile_buffer, NULL, __func__,
2337 keymap_str, sizeof(keymap_str),
2338 GOLDEN_TESTS_OUTPUTS "no_void_action-v1.xkb",
2339 update_output_files));
2340 }
2341
2342 static void
2343 test_prebuilt_keymap_roundtrip(struct xkb_context *ctx, bool update_output_files)
2344 {
2345 /* Load in a prebuilt keymap, make sure we can compile it from memory,
2346 * then compare it to make sure we get the same result when dumping it
2347 * to a string. */
2348 static const struct {
2349 const char* path;
2350 enum xkb_keymap_format format;
2351 enum xkb_keymap_serialize_flags serialize_flags;
2352 } data[] = {
2353 {
2354 .path = "keymaps/stringcomp-v1.xkb",
2355 .format = XKB_KEYMAP_FORMAT_TEXT_V1,
2356 .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
2357 },
2358 {
2359 .path = "keymaps/stringcomp-v1-no-prettyfied.xkb",
2360 .format = XKB_KEYMAP_FORMAT_TEXT_V1,
2361 .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
2362 & ~XKB_KEYMAP_SERIALIZE_PRETTY
2363 },
2364 {
2365 .path = "keymaps/stringcomp-v2.xkb",
2366 .format = XKB_KEYMAP_FORMAT_TEXT_V2,
2367 .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
2368 },
2369 };
2370 for (unsigned int k = 0; k < ARRAY_SIZE(data); k++) {
2371 char *original = test_read_file(data[k].path);
2372 assert(original);
2373 /* Load a prebuild keymap, once without, once with the trailing \0 */
2374 for (unsigned int i = 0; i <= 1; i++) {
2375 assert(test_compile_output2(ctx, data[k].format,
2376 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2377 data[k].serialize_flags,
2378 compile_buffer, NULL, "Round-trip",
2379 original, strlen(original) + i,
2380 data[k].path, update_output_files));
2381 }
2382 free(original);
2383 }
2384 }
2385
2386 static void
2387 test_keymap_from_rules(struct xkb_context *ctx)
2388 {
2389 /* Make sure we can recompile our output for a normal keymap from rules. */
2390 fprintf(stderr, "------\n*** %s ***\n", __func__);
2391 struct xkb_keymap *keymap = test_compile_rules(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2392 NULL, NULL,
2393 "ru,ca,de,us",
2394 ",multix,neo,intl", NULL);
2395 assert(keymap);
2396 char *dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
2397 assert(dump);
2398 xkb_keymap_unref(keymap);
2399 keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2400 dump, strlen(dump));
2401 assert(keymap);
2402 xkb_keymap_unref(keymap);
2403 free(dump);
2404 }
2405
2406 static void
2407 test_unsupported_legacy_x11_actions(struct xkb_context *ctx,
2408 bool update_output_files)
2409 {
2410 const char keymap_str[] =
2411 "xkb_keymap {\n"
2412 " xkb_keycodes {\n"
2413 " <1> = 1;\n"
2414 " <2> = 2;\n"
2415 " <3> = 3;\n"
2416 " <4> = 4;\n"
2417 " <5> = 5;\n"
2418 " <6> = 6;\n"
2419 " };\n"
2420 " xkb_compat {\n"
2421 " RedirectKey.key = <1>;\n"
2422 " RedirectKey.data = <1>;\n" /* invalid field */
2423 " ISOLock.modifiers = modMapMods;\n"
2424 " DeviceButton.data = <1>;\n" /* invalid field */
2425 " LockDeviceButton.data = <1>;\n" /* invalid field */
2426 " DeviceValuator.data = <1>;\n" /* invalid field */
2427 " ActionMessage.data = <1>;\n" /* invalid field */
2428 " interpret ISO_Lock {\n"
2429 " action=ISOLock(affect=all);\n"
2430 " };\n"
2431 " interpret VoidSymbol {\n"
2432 " action=RedirectKey(data=<1>);\n" /* invalid field */
2433 " };\n"
2434 " };\n"
2435 " xkb_symbols {\n"
2436 " key <1> { [ISOLock(affect=all)] };\n"
2437 " key <2> { [RedirectKey(data=<1>)] };\n" /* invalid field */
2438 " key <3> { [DeviceButton(data=<1>)] };\n" /* invalid field */
2439 " key <4> { [LockDeviceButton(data=<1>)] };\n" /* invalid field */
2440 " key <5> { [DeviceValuator(data=<1>)] };\n" /* invalid field */
2441 " key <6> { [ActionMessage(data=<1>)] };\n" /* invalid field */
2442 " };\n"
2443 "};";
2444 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2445 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2446 compile_buffer, NULL, __func__,
2447 keymap_str, sizeof(keymap_str),
2448 GOLDEN_TESTS_OUTPUTS "unsupported-x11-actions",
2449 update_output_files));
2450 }
2451
2452 static void
2453 test_extended_layout_indices(struct xkb_context *ctx,
2454 bool update_output_files)
2455 {
2456 const struct {
2457 const char* keymap;
2458 bool no_output;
2459 union {
2460 struct { bool compiles_v1; bool compiles_v2; };
2461 struct {
2462 const char* expected_v1;
2463 const char* expected_v1_2;
2464 const char* expected_v2;
2465 const char* expected_v2_1;
2466 };
2467 };
2468 } tests[] = {
2469 {
2470 .keymap =
2471 "xkb_keymap {\n"
2472 " xkb_compat { interpret a { action = SetGroup(group=5); }; };\n"
2473 "};",
2474 .no_output = true,
2475 .compiles_v1 = false,
2476 .compiles_v2 = true,
2477 },
2478 {
2479 .keymap =
2480 "xkb_keymap {\n"
2481 " xkb_keycodes { indicator 1 = \"a\"; };\n"
2482 " xkb_compat { indicator \"a\" { groups=All-5; }; };\n"
2483 "};",
2484 .no_output = true,
2485 .compiles_v1 = true,
2486 .compiles_v2 = true,
2487 },
2488 {
2489 .keymap =
2490 "xkb_keymap {\n"
2491 " xkb_keycodes { <> = 1; };\n"
2492 " xkb_symbols { name[5] = \"5\"; };\n"
2493 "};",
2494 .no_output = true,
2495 .compiles_v1 = false,
2496 .compiles_v2 = true,
2497 },
2498 {
2499 .keymap =
2500 "xkb_keymap {\n"
2501 " xkb_keycodes { <> = 1; };\n"
2502 " xkb_symbols { key <> { [1], [2], [3], [4], [5] }; };\n"
2503 "};",
2504 .no_output = true,
2505 .compiles_v1 = false,
2506 .compiles_v2 = true,
2507 },
2508 {
2509 .keymap =
2510 "xkb_keymap {\n"
2511 " xkb_keycodes {\n"
2512 " <a> = 1;\n"
2513 " <b> = 2;\n"
2514 " <c> = 3;\n"
2515 " indicator 1 = \"a\";\n"
2516 " };\n"
2517 " xkb_compat {\n"
2518 " interpret a { action = SetGroup(group=5); };\n"
2519 " indicator \"a\" { groups=All-5; };\n"
2520 " };\n"
2521 " xkb_symbols {\n"
2522 " name[5] = \"G5\";\n"
2523 " name[1] = \"G1\";\n"
2524 " key <a> { symbols[1]=[a], symbols[5]=[Greek_alpha] };\n"
2525 " key <b> { actions[1]=[Latchgroup(group=-5)], actions[5]=[LockGroup(group=5)], [SetGroup(+9)] };\n"
2526 " key <c> { [1], [2], [3], [4], [5], [6], [7], [8], [9] };\n"
2527 " };\n"
2528 "};",
2529 .expected_v1 = NULL,
2530 .expected_v1_2 = NULL,
2531 .expected_v2 = GOLDEN_TESTS_OUTPUTS "extended-layout-indices-v2.xkb",
2532 .expected_v2_1 = GOLDEN_TESTS_OUTPUTS "extended-layout-indices-v1.xkb",
2533 }
2534 };
2535
2536 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
2537 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
2538 if (tests[k].no_output) {
2539 struct xkb_keymap *keymap =
2540 test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2541 tests[k].keymap, strlen(tests[k].keymap));
2542 assert(!!keymap == tests[k].compiles_v1);
2543 xkb_keymap_unref(keymap);
2544 keymap =
2545 test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
2546 tests[k].keymap, strlen(tests[k].keymap));
2547 assert(!!keymap == tests[k].compiles_v2);
2548 xkb_keymap_unref(keymap);
2549 continue;
2550 }
2551 /* v1 → v1 */
2552 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2553 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2554 compile_buffer, NULL, __func__,
2555 tests[k].keymap, strlen(tests[k].keymap),
2556 tests[k].expected_v1, update_output_files));
2557 /* v1 → v2 */
2558 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
2559 XKB_KEYMAP_FORMAT_TEXT_V2,
2560 compile_buffer, NULL, __func__,
2561 tests[k].keymap, strlen(tests[k].keymap),
2562 tests[k].expected_v1_2, update_output_files));
2563 /* v2 → v2 */
2564 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
2565 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
2566 compile_buffer, NULL, __func__,
2567 tests[k].keymap, strlen(tests[k].keymap),
2568 tests[k].expected_v2, update_output_files));
2569 /* v2 → v1 */
2570 assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
2571 XKB_KEYMAP_FORMAT_TEXT_V1,
2572 compile_buffer, NULL, __func__,
2573 tests[k].keymap, strlen(tests[k].keymap),
2574 tests[k].expected_v2_1, update_output_files));
2575 }
2576 }
2577
2578 int
2579 main(int argc, char *argv[])
2580 {
2581 test_init();
2582
2583 bool update_output_files = false;
2584 /* Default seed for pseudo-random generator */
2585 unsigned int seed = (unsigned int) time(NULL);
2586
2587 int arg_index = 0;
2588 while (++arg_index < argc) {
2589 if (streq(argv[arg_index], "update")) {
2590 /* Update files with *obtained* results */
2591 update_output_files = true;
2592 } else if (streq(argv[arg_index], "--seed") && arg_index + 1 < argc) {
2593 seed = (unsigned int) atoi(argv[arg_index + 1]);
2594 } else {
2595 fprintf(stderr, "ERROR: unsupported argument: \"%s\".\n",
2596 argv[arg_index]);
2597 exit(EXIT_INVALID_USAGE);
2598 }
2599 }
2600
2601 /* Initialize pseudo-random generator with program arg or current time */
2602 fprintf(stderr, "Seed for the pseudo-random generator: %u\n", seed);
2603 srand(seed);
2604
2605 struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
2606 assert(ctx);
2607
2608 /* Make sure we can't (falsely claim to) compile an empty string. */
2609 struct xkb_keymap *keymap =
2610 test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, "", 0);
2611 assert(!keymap);
2612
2613 test_encodings(ctx);
2614 test_floats(ctx);
2615 test_component_syntax_error(ctx);
2616 test_optional_components(ctx, update_output_files);
2617 test_bidi_chars(ctx, update_output_files);
2618 test_recursive_includes(ctx);
2619 test_include_paths(ctx);
2620 test_include_default_maps(update_output_files);
2621 test_alloc_limits(ctx, update_output_files);
2622 test_integers(ctx, update_output_files);
2623 test_keycodes(ctx, update_output_files);
2624 test_masks(ctx, update_output_files);
2625 test_interpret(ctx, update_output_files);
2626 test_group_indices_names(ctx, update_output_files);
2627 test_level_indices_names(ctx, update_output_files);
2628 test_multi_keysyms_actions(ctx, update_output_files);
2629 test_key_keysyms_as_strings(ctx, update_output_files);
2630 test_invalid_symbols_fields(ctx);
2631 test_modifier_maps(ctx, update_output_files);
2632 test_empty_compound_statements(ctx, update_output_files);
2633 test_escape_sequences(ctx, update_output_files);
2634 test_unicode_keysyms(ctx, update_output_files);
2635 test_no_action_void_action(ctx, update_output_files);
2636 test_prebuilt_keymap_roundtrip(ctx, update_output_files);
2637 test_keymap_from_rules(ctx);
2638 test_unsupported_legacy_x11_actions(ctx, update_output_files);
2639 test_extended_layout_indices(ctx, update_output_files);
2640
2641 xkb_context_unref(ctx);
2642
2643 return EXIT_SUCCESS;
2644 }