Debian Codegraph @

test/introspection.c

raw
1 /*
2 * Copyright © 2025 Pierre Le Marre
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
12 #include "darray.h"
13 #include "utils-paths.h"
14 #include "xkbcommon/xkbcommon.h"
15 #include "src/xkbcomp/keymap-file-iterator.h"
16 #include "src/xkbcomp/ast.h"
17 #include "test.h"
18 #include "utils.h"
19
20 struct file_include_test_data {
21 bool valid:1;
22 bool explicit_section:1;
23 enum merge_mode merge;
24 const char *path; /* relative to test data */
25 const char *file;
26 const char *section;
27 const char *modifier;
28 enum xkb_map_flags flags;
29 };
30
31 struct section_test_data {
32 const char *name;
33 enum xkb_file_type file_type;
34 enum xkb_map_flags flags;
35 struct xkb_file_include_group include_groups[3];
36 struct file_include_test_data includes[3];
37 darray_size_t num_include_groups;
38 darray_size_t num_includes;
39 };
40
41 static void
42 test_resolve_file(struct xkb_context *ctx)
43 {
44 char *path;
45 char resolved_path[PATH_MAX] = {0};
46 char resolved_section[1024] = {0};
47 FILE *file;
48
49 /* Invalid paths */
50 static const char invalid[] = "---invalid---";
51 path = test_get_path(invalid);
52 assert(path);
53 assert(is_absolute_path(path));
54 const char * paths[] = { path, invalid };
55 static const char * sections[] = { NULL, "invalid" };
56 /* Note: inferior or equal to _FILE_TYPE_NUM_ENTRIES is correct here */
57 for (enum xkb_file_type t = 0; t <= _FILE_TYPE_NUM_ENTRIES; t++) {
58 for (size_t p = 0; p < ARRAY_SIZE(paths); p++) {
59 for (size_t s = 0; s < ARRAY_SIZE(sections); s++) {
60 file = xkb_resolve_file(ctx, t, paths[p], sections[s],
61 resolved_path, sizeof(resolved_path),
62 resolved_section, sizeof(resolved_section));
63 assert(!file);
64 }
65 }
66 }
67 free(path);
68
69 /* Valid absolute path */
70 path = test_get_path("types/numpad");
71 assert(path);
72 assert(is_absolute_path(path));
73
74 const struct {
75 const char *path;
76 const char *section;
77 const char *resolved_path;
78 const char *resolved_section;
79 enum xkb_file_type type;
80 bool failure;
81 } tests[] = {
82 /* Absolute path */
83 {
84 .failure = false,
85 .path = path,
86 .section = NULL,
87 .type = FILE_TYPE_INVALID,
88 .resolved_path = path,
89 .resolved_section = "pc" /* default */
90 },
91 {
92 .failure = false,
93 .path = path,
94 .section = "shift3",
95 .type = FILE_TYPE_INVALID,
96 .resolved_path = path,
97 .resolved_section = "shift3" /* non-default */
98 },
99 {
100 .failure = true,
101 .path = path,
102 .section = NULL,
103 .type = FILE_TYPE_SYMBOLS, /* invalid type */
104 .resolved_path = NULL,
105 .resolved_section = NULL
106 },
107 {
108 .failure = false,
109 .path = path,
110 .section = NULL,
111 .type = FILE_TYPE_TYPES, /* valid type */
112 .resolved_path = path,
113 .resolved_section = "pc" /* default */
114 },
115 {
116 .failure = false,
117 .path = path,
118 .section = "shift3",
119 .type = FILE_TYPE_TYPES, /* valid type */
120 .resolved_path = path,
121 .resolved_section = "shift3" /* non default */
122 },
123
124 /* Relative path */
125 {
126 .failure = true,
127 .path = "numpad",
128 .section = NULL,
129 .type = FILE_TYPE_INVALID, /* type required */
130 .resolved_path = NULL,
131 .resolved_section = NULL
132 },
133 {
134 .failure = true,
135 .path = "numpad",
136 .section = "shift3",
137 .type = FILE_TYPE_INVALID, /* type required */
138 .resolved_path = NULL,
139 .resolved_section = NULL
140 },
141 {
142 .failure = true,
143 .path = "numpad",
144 .section = NULL,
145 .type = FILE_TYPE_KEYCODES, /* invalid type */
146 .resolved_path = NULL,
147 .resolved_section = NULL
148 },
149 {
150 .failure = false,
151 .path = "numpad",
152 .section = NULL,
153 .type = FILE_TYPE_TYPES, /* valid type */
154 .resolved_path = path,
155 .resolved_section = "pc" /* default */
156 },
157 {
158 .failure = false,
159 .path = "numpad",
160 .section = "shift3",
161 .type = FILE_TYPE_TYPES, /* valid type */
162 .resolved_path = path,
163 .resolved_section = "shift3" /* non default */
164 },
165 };
166 for (size_t t = 0; t < ARRAY_SIZE(tests); t++) {
167 fprintf(stderr, "------\n*** %s: #%zu ***\n", __func__, t);
168 file = xkb_resolve_file(ctx, tests[t].type,
169 tests[t].path, tests[t].section,
170 resolved_path, sizeof(resolved_path),
171 resolved_section, sizeof(resolved_section));
172 if (tests[t].failure) {
173 assert(!file);
174 } else {
175 assert(file);
176 assert_streq_not_null("resolved path",
177 resolved_path, tests[t].resolved_path);
178 assert_streq_not_null("resolved section",
179 resolved_section, tests[t].resolved_section);
180 }
181 }
182 free(path);
183 }
184
185 static bool
186 test_section(const struct section_test_data *data,
187 const struct xkb_file_section *section)
188 {
189 assert(section->file_type == data->file_type);
190 assert_streq_not_null("", data->name,
191 xkb_file_section_get_string(section, section->name));
192 assert_eq("Section flags", data->flags, section->flags, "%#x");
193 assert_eq("Num include groups", data->num_include_groups,
194 darray_size(section->include_groups), "%u");
195 assert_eq("Num includes", data->num_includes,
196 darray_size(section->includes), "%u");
197 for (darray_size_t k = 0; k < data->num_include_groups; k++) {
198 fprintf(stderr, "... %s: include group #%u ...\n", __func__, k);
199 const struct xkb_file_include_group * const expected = &data->include_groups[k];
200 const struct xkb_file_include_group * const got = &darray_item(section->include_groups, k);
201 assert_eq("Start", expected->start, got->start, "%d");
202 assert_eq("End", expected->end, got->end, "%d");
203 }
204 for (darray_size_t k = 0; k < data->num_includes; k++) {
205 fprintf(stderr, "... %s: include #%u ...\n", __func__, k);
206 const struct file_include_test_data * const expected = &data->includes[k];
207 const struct xkb_file_include * const got = &darray_item(section->includes, k);
208 assert_eq("Valid", expected->valid, got->valid, "%d");
209 assert_eq("Merge mode", expected->merge, got->merge, "%d");
210 char *path = test_get_path(expected->path);
211 assert(path);
212 assert_streq_not_null("File", path,
213 xkb_file_section_get_string(section, got->path));
214 free(path);
215 assert_streq_not_null("File", expected->file,
216 xkb_file_section_get_string(section, got->file));
217 assert_streq_not_null("Section", expected->section,
218 xkb_file_section_get_string(section, got->section));
219 assert_eq("Explicit section",
220 expected->explicit_section, got->explicit_section, "%d");
221 assert_streq_not_null("Modifier", expected->modifier,
222 xkb_file_section_get_string(section, got->modifier));
223 assert_eq("Section flags", expected->flags, got->flags, "%#x");
224 }
225 return true;
226 }
227
228 static void
229 test_file_section_parse(struct xkb_context *ctx)
230 {
231 char *path = test_get_path("symbols/pc");
232 assert(path);
233
234 struct xkb_file_section section = {0};
235
236 struct section_test_data tests[] = {
237 {
238 .name = "editing",
239 .file_type = FILE_TYPE_SYMBOLS,
240 .flags = MAP_IS_HIDDEN | MAP_IS_PARTIAL | MAP_HAS_ALPHANUMERIC,
241 .includes = {},
242 .num_includes = 0
243 },
244 {
245 .name = "pc105",
246 .file_type = FILE_TYPE_SYMBOLS,
247 .flags = MAP_IS_DEFAULT | MAP_IS_PARTIAL | MAP_HAS_ALPHANUMERIC
248 | MAP_HAS_MODIFIER,
249 .num_include_groups = 1,
250 .include_groups = {
251 { .start = 0, .end = 0 },
252 },
253 .num_includes = 1,
254 .includes = {
255 {
256 .valid = true,
257 .merge = MERGE_DEFAULT,
258 .path = "symbols/pc",
259 .file = "pc",
260 .section = "pc105-pure-virtual-modifiers",
261 .explicit_section = true,
262 .modifier = "",
263 .flags = 0
264 }
265 },
266 },
267 {
268 .name = "pc105-pure-virtual-modifiers",
269 .file_type = FILE_TYPE_SYMBOLS,
270 .flags = 0,
271 .num_include_groups = 3,
272 .include_groups = {
273 { .start = 0, .end = 0 },
274 { .start = 1, .end = 1 },
275 { .start = 2, .end = 2 },
276 },
277 .num_includes = 3,
278 .includes = {
279 {
280 .valid = true,
281 .merge = MERGE_DEFAULT,
282 .path = "symbols/srvr_ctrl",
283 .file = "srvr_ctrl",
284 .section = "fkey2vt",
285 .explicit_section = true,
286 .modifier = "",
287 .flags = MAP_IS_PARTIAL | MAP_HAS_FN,
288 },
289 {
290 .valid = true,
291 .merge = MERGE_DEFAULT,
292 .path = "symbols/pc",
293 .file = "pc",
294 .section = "editing",
295 .explicit_section = true,
296 .modifier = "",
297 .flags = MAP_IS_HIDDEN | MAP_IS_PARTIAL
298 | MAP_HAS_ALPHANUMERIC,
299 },
300 {
301 .valid = true,
302 .merge = MERGE_DEFAULT,
303 .path = "symbols/keypad",
304 .file = "keypad",
305 .section = "x11",
306 .explicit_section = true,
307 .modifier = "",
308 .flags = MAP_IS_DEFAULT | MAP_IS_HIDDEN | MAP_IS_PARTIAL
309 | MAP_HAS_KEYPAD,
310 }
311 },
312 }
313 };
314
315 for (size_t k = 0; k < ARRAY_SIZE(tests); k++) {
316 fprintf(stderr, "------\n*** %s: #%zu ***\n", __func__, k);
317 xkb_file_section_init(&section);
318 const bool is_default = tests[k].flags & MAP_IS_DEFAULT;
319 assert(xkb_file_section_parse(
320 ctx, XKB_FILE_ITERATOR_FAIL_ON_INCLUDE_ERROR,
321 XKB_KEYMAP_FORMAT_TEXT_V2, XKB_KEYMAP_COMPILE_NO_FLAGS,
322 0, path, (is_default ? NULL : tests[k].name), &section
323 ));
324 test_section(&tests[k], &section);
325 xkb_file_section_free(&section);
326 }
327 free(path);
328 }
329
330 struct iterator_test_data {
331 const char *string;
332 const char *map;
333 struct section_test_data sections[3];
334 unsigned int num_sections;
335 bool error;
336 };
337
338 static void
339 test_file_iterator(struct xkb_context *ctx)
340 {
341 static const struct iterator_test_data tests[] = {
342 {
343 .string = "",
344 .map = NULL,
345 .num_sections = 0,
346 .sections = {},
347 .error = false,
348 },
349 {
350 .string = "xkb_symbols \"1\" {};",
351 .map = NULL,
352 .num_sections = 1,
353 .sections = {
354 {
355 .name = "1",
356 .file_type = FILE_TYPE_SYMBOLS,
357 .flags = 0,
358 .includes = {},
359 .num_includes = 0,
360 }
361 },
362 .error = false,
363 },
364 {
365 .string =
366 "xkb_symbols \"1\" {\n"
367 " include \"pc\"\n"
368 " replace \"+de:1|cz:2\"\n"
369 "};",
370 .map = NULL,
371 .num_sections = 1,
372 .sections = {
373 {
374 .name = "1",
375 .file_type = FILE_TYPE_SYMBOLS,
376 .flags = 0,
377 .num_include_groups = 2,
378 .include_groups = {
379 { .start = 0, .end = 0 },
380 { .start = 1, .end = 2 },
381 },
382 .num_includes = 3,
383 .includes = {
384 {
385 .valid = true,
386 .merge = MERGE_DEFAULT,
387 .path = "symbols/pc",
388 .file = "pc",
389 .section = "pc105",
390 .explicit_section = false,
391 .modifier = "",
392 .flags = MAP_IS_DEFAULT | MAP_IS_PARTIAL
393 | MAP_HAS_ALPHANUMERIC | MAP_HAS_MODIFIER,
394 },
395 {
396 .valid = true,
397 /* First include uses the merge mode of the statement */
398 .merge = MERGE_REPLACE,
399 .path = "symbols/de",
400 .file = "de",
401 .section = "basic",
402 .explicit_section = false,
403 .modifier = "1",
404 .flags = MAP_IS_DEFAULT
405 },
406 {
407 .valid = true,
408 .merge = MERGE_AUGMENT,
409 .path = "symbols/cz",
410 .file = "cz",
411 .section = "basic",
412 .explicit_section = false,
413 .modifier = "2",
414 .flags = MAP_IS_DEFAULT | MAP_IS_PARTIAL
415 | MAP_HAS_ALPHANUMERIC,
416 }
417 },
418 }
419 },
420 .error = false,
421 },
422 {
423 .string =
424 "xkb_symbols \"1\" {};\n"
425 "xkb_symbols \"2\" {};\n",
426 .map = NULL,
427 .num_sections = 2,
428 .sections = {
429 {
430 .name = "1",
431 .file_type = FILE_TYPE_SYMBOLS,
432 .flags = 0,
433 .num_include_groups = 0,
434 .include_groups = {},
435 .num_includes = 0,
436 .includes = {},
437 },
438 {
439 .name = "2",
440 .file_type = FILE_TYPE_SYMBOLS,
441 .flags = 0,
442 .num_include_groups = 0,
443 .include_groups = {},
444 .num_includes = 0,
445 .includes = {},
446 },
447 },
448 .error = false,
449 },
450 {
451 .string =
452 "xkb_keymap \"1\" {\n"
453 " xkb_types \"2\" {};\n"
454 " xkb_symbols \"3\" {};\n"
455 "};",
456 .map = NULL,
457 .num_sections = 3,
458 .sections = {
459 {
460 .name = "1",
461 .file_type = FILE_TYPE_KEYMAP,
462 .flags = 0,
463 .num_include_groups = 0,
464 .include_groups = {},
465 .num_includes = 0,
466 .includes = {},
467 },
468 {
469 .name = "2",
470 .file_type = FILE_TYPE_TYPES,
471 .flags = 0,
472 .num_include_groups = 0,
473 .include_groups = {},
474 .num_includes = 0,
475 .includes = {},
476 },
477 {
478 .name = "3",
479 .file_type = FILE_TYPE_SYMBOLS,
480 .flags = 0,
481 .num_include_groups = 0,
482 .include_groups = {},
483 .num_includes = 0,
484 .includes = {},
485 },
486 },
487 .error = false,
488 },
489 {
490 .string =
491 "xkb_keymap \"10\" {\n"
492 " xkb_types \"11\" {};\n"
493 " xkb_symbols \"12\" {};\n"
494 "};\n"
495 "default xkb_keymap \"20\" {\n"
496 " xkb_types \"21\" {};\n"
497 " xkb_symbols \"22\" {};\n"
498 "};",
499 .map = NULL,
500 .sections = {},
501 .num_sections = 0,
502 .error = true,
503 // TODO: enable multiple keymap per file
504 // .num_sections = 6,
505 // .sections = {
506 // {
507 // .path = NULL,
508 // .name = "10",
509 // .file_type = FILE_TYPE_KEYMAP,
510 // .flags = 0,
511 // .includes = {},
512 // .num_include = 0
513 // },
514 // {
515 // .path = NULL,
516 // .name = "11",
517 // .file_type = FILE_TYPE_TYPES,
518 // .flags = 0,
519 // .includes = {},
520 // .num_include = 0
521 // },
522 // {
523 // .path = NULL,
524 // .name = "12",
525 // .file_type = FILE_TYPE_SYMBOLS,
526 // .flags = 0,
527 // .includes = {},
528 // .num_include = 0
529 // },
530 // {
531 // .path = NULL,
532 // .name = "20",
533 // .file_type = FILE_TYPE_KEYMAP,
534 // .flags = MAP_IS_DEFAULT,
535 // .includes = {},
536 // .num_include = 0
537 // },
538 // {
539 // .path = NULL,
540 // .name = "21",
541 // .file_type = FILE_TYPE_TYPES,
542 // .flags = 0,
543 // .includes = {},
544 // .num_include = 0
545 // },
546 // {
547 // .path = NULL,
548 // .name = "22",
549 // .file_type = FILE_TYPE_SYMBOLS,
550 // .flags = 0,
551 // .includes = {},
552 // .num_include = 0
553 // },
554 // },
555 // .error = false,
556 },
557 };
558 for (size_t k = 0; k < ARRAY_SIZE(tests); k++) {
559 fprintf(stderr, "------\n*** %s: #%zu ***\n", __func__, k);
560 struct xkb_file_iterator * const iter =
561 xkb_file_iterator_new_from_buffer(
562 ctx, XKB_FILE_ITERATOR_FAIL_ON_INCLUDE_ERROR,
563 XKB_KEYMAP_FORMAT_TEXT_V2, XKB_KEYMAP_COMPILE_NO_FLAGS,
564 "(string)", tests[k].map, FILE_TYPE_INVALID,
565 tests[k].string, strlen(tests[k].string)
566 );
567 assert(iter);
568
569 const struct xkb_file_section *section;
570 unsigned int s = 0;
571 bool ok = true;
572 while ((ok = xkb_file_iterator_next(iter, &section)) && section) {
573 fprintf(stderr, "section #%u\n", s);
574 if (s >= tests[k].num_sections) {
575 assert_eq("Section count", tests[k].num_sections, s, "%u");
576 break;
577 }
578 assert(test_section(&tests[k].sections[s], section));
579 s++;
580 }
581 assert_eq("Error", tests[k].error, !ok, "%d");
582 assert_eq("Section count", tests[k].num_sections, s, "%u");
583 xkb_file_iterator_free(iter);
584 }
585 }
586
587 int
588 main(void)
589 {
590 test_init();
591
592 struct xkb_context * const context = test_get_context(CONTEXT_NO_FLAG);
593
594 test_resolve_file(context);
595 test_file_section_parse(context);
596 test_file_iterator(context);
597
598 xkb_context_unref(context);
599
600 return EXIT_SUCCESS;
601 }