Debian Codegraph @

test/registry.c

raw
1 /*
2 * Copyright © 2020 Red Hat, Inc.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #if HAVE_UNISTD_H
10 #include <unistd.h>
11 #endif
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <libxml/parser.h>
17 #include <libxml/tree.h>
18
19 #include "xkbcommon/xkbregistry.h"
20
21 #include "utils.h"
22 #include "test.h"
23
24 #define NO_VARIANT NULL
25
26 enum {
27 MODEL = 78,
28 LAYOUT,
29 VARIANT,
30 OPTION,
31 };
32
33 enum popularity {
34 POPULARITY_UNDEFINED = 0, /* Not member of enum rxkb_popularity */
35 POPULARITY_STANDARD = RXKB_POPULARITY_STANDARD,
36 POPULARITY_EXOTIC = RXKB_POPULARITY_EXOTIC,
37 };
38
39 struct test_model {
40 const char *name; /* required */
41 const char *vendor;
42 const char *description;
43 };
44
45 struct test_layout {
46 const char *name; /* required */
47 const char *variant;
48 const char *brief;
49 const char *description;
50 const char *iso639[3]; /* language list (iso639 three letter codes), 3 is enough for our test */
51 const char *iso3166[3]; /* country list (iso3166 two letter codes), 3 is enough for our tests */
52 enum popularity popularity;
53 };
54
55 struct test_option {
56 const char *name;
57 const char *description;
58 };
59
60 struct test_option_group {
61 const char *name;
62 const char *description;
63 bool allow_multiple_selection;
64
65 struct test_option options[10];
66 };
67
68 static const char *
69 popularity_attr(enum popularity popularity)
70 {
71 switch (popularity) {
72 case POPULARITY_UNDEFINED:
73 return "";
74 case RXKB_POPULARITY_STANDARD:
75 return " popularity=\"standard\"";
76 case RXKB_POPULARITY_EXOTIC:
77 return " popularity=\"exotic\"";
78 default:
79 fprintf(stderr, "ERROR: unsupported popularity: %d\n", popularity);
80 assert(false);
81 }
82 }
83
84 static void
85 fprint_config_item(FILE *fp,
86 const char *name,
87 const char *vendor,
88 const char *brief,
89 const char *description,
90 const char * const iso639[3],
91 const char * const iso3166[3],
92 enum popularity popularity)
93 {
94 fprintf(fp, " <configItem%s>\n"
95 " <name>%s</name>\n", popularity_attr(popularity), name);
96 if (brief)
97 fprintf(fp, " <shortDescription>%s</shortDescription>\n", brief);
98 if (description)
99 fprintf(fp, " <description>%s</description>\n", description);
100 if (vendor)
101 fprintf(fp, " <vendor>%s</vendor>\n", vendor);
102 if (iso3166 && iso3166[0]) {
103 fprintf(fp, " <countryList>\n");
104 for (int i = 0; i < 3; i++) {
105 const char *iso = iso3166[i];
106 if (!iso)
107 break;
108 fprintf(fp, " <iso3166Id>%s</iso3166Id>\n", iso);
109 }
110 fprintf(fp, " </countryList>\n");
111 }
112 if (iso639 && iso639[0]) {
113 fprintf(fp, " <languageList>\n");
114 for (int i = 0; i < 3; i++) {
115 const char *iso = iso639[i];
116 if (!iso)
117 break;
118 fprintf(fp, " <iso639Id>%s</iso639Id>\n", iso);
119 }
120 fprintf(fp, " </languageList>\n");
121 }
122
123 fprintf(fp, " </configItem>\n");
124 }
125
126 /**
127 * Create a directory populated with a rules/<ruleset>.xml that contains the
128 * given items.
129 *
130 * @return the XKB base directory
131 */
132 static char *
133 test_create_rules(const char *ruleset,
134 const struct test_model *test_models,
135 const struct test_layout *test_layouts,
136 const struct test_option_group *test_groups)
137 {
138 static int iteration;
139 char *tmpdir;
140 char buf[PATH_MAX];
141 int rc;
142 FILE *fp;
143
144 char *template = asprintf_safe("%s.%d.XXXXXX", ruleset, iteration++);
145 assert(template != NULL);
146 tmpdir = test_maketempdir(template);
147 free(template);
148
149 free(test_makedir(tmpdir, "rules"));
150
151 rc = snprintf_safe(buf, sizeof(buf), "%s/rules/%s.xml", tmpdir, ruleset);
152 assert(rc);
153
154 fp = fopen(buf, "w");
155 assert(fp);
156
157 fprintf(fp,
158 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
159 "<!DOCTYPE xkbConfigRegistry SYSTEM \"xkb.dtd\">\n"
160 "<xkbConfigRegistry version=\"1.1\">\n");
161
162 if (test_models) {
163 fprintf(fp, "<modelList>\n");
164
165 for (const struct test_model *m = test_models; m->name; m++) {
166 fprintf(fp, "<model>\n");
167 fprint_config_item(fp, m->name, m->vendor, NULL, m->description,
168 NULL, NULL, POPULARITY_UNDEFINED);
169 fprintf(fp, "</model>\n");
170 }
171 fprintf(fp, "</modelList>\n");
172 }
173
174 if (test_layouts) {
175 const struct test_layout *l, *next;
176
177 fprintf(fp, "<layoutList>\n");
178
179 l = test_layouts;
180 next = l + 1;
181
182 assert(l->variant == NULL);
183
184 while (l->name) {
185 fprintf(fp, "<layout>\n");
186 fprint_config_item(fp, l->name, NULL, l->brief, l->description, l->iso639, l->iso3166, l->popularity);
187
188 if (next->name && streq(next->name, l->name)) {
189 fprintf(fp, "<variantList>\n");
190 do {
191 fprintf(fp, "<variant>\n");
192 fprint_config_item(fp, next->variant, NULL, next->brief, next->description, next->iso639, next->iso3166, next->popularity);
193 fprintf(fp, "</variant>\n");
194 l = next;
195 next++;
196 } while (next->name && streq(next->name, l->name));
197 fprintf(fp, "</variantList>\n");
198 }
199 fprintf(fp, "</layout>\n");
200 l++;
201 next = l + 1;
202 }
203 fprintf(fp, "</layoutList>\n");
204 }
205
206 if (test_groups) {
207 fprintf(fp, "<optionList>\n");
208
209 for (const struct test_option_group *g = test_groups; g->name; g++) {
210 fprintf(fp, "<group allowMultipleSelection=\"%s\">\n",
211 g->allow_multiple_selection ? "true" : "false");
212 fprint_config_item(fp, g->name, NULL, NULL, g->description,
213 NULL, NULL, POPULARITY_UNDEFINED);
214 for (const struct test_option *o = g->options; o->name; o++) {
215 fprintf(fp, " <option>\n");
216 fprint_config_item(fp, o->name, NULL, NULL, o->description,
217 NULL, NULL, POPULARITY_UNDEFINED);
218 fprintf(fp, "</option>\n");
219 }
220 fprintf(fp, "</group>\n");
221 }
222 fprintf(fp, "</optionList>\n");
223 }
224
225 fprintf(fp, "</xkbConfigRegistry>\n");
226 fclose(fp);
227
228 return tmpdir;
229 }
230
231 static void
232 test_remove_rules(char *basedir, const char *ruleset)
233 {
234 char path[PATH_MAX];
235 int rc;
236
237 rc = snprintf_safe(path, sizeof(path), "%s/rules/%s.xml", basedir,
238 ruleset);
239 assert(rc);
240 unlink(path);
241 rc = snprintf_safe(path, sizeof(path), "%s/xkb/rules", basedir);
242 assert(rc);
243 rmdir(path);
244 rmdir(basedir);
245 free(basedir);
246 }
247
248 static struct rxkb_context *
249 test_setup_context_for(const char *ruleset,
250 struct test_model *system_models,
251 struct test_model *user_models,
252 struct test_layout *system_layouts,
253 struct test_layout *user_layouts,
254 struct test_option_group *system_groups,
255 struct test_option_group *user_groups)
256 {
257 char *sysdir = NULL, *userdir = NULL;
258 struct rxkb_context *ctx;
259
260 sysdir = test_create_rules(ruleset, system_models, system_layouts,
261 system_groups);
262 if (user_models || user_layouts || user_groups)
263 userdir = test_create_rules(ruleset, user_models, user_layouts,
264 user_groups);
265
266 ctx = rxkb_context_new(RXKB_CONTEXT_NO_DEFAULT_INCLUDES);
267 assert(ctx);
268 if (userdir)
269 assert(rxkb_context_include_path_append(ctx, userdir));
270 assert(rxkb_context_include_path_append(ctx, sysdir));
271 assert(rxkb_context_parse(ctx, ruleset));
272
273 test_remove_rules(sysdir, ruleset);
274 if (userdir)
275 test_remove_rules(userdir, ruleset);
276
277 return ctx;
278 }
279
280 static struct rxkb_context *
281 test_setup_context(struct test_model *system_models,
282 struct test_model *user_models,
283 struct test_layout *system_layouts,
284 struct test_layout *user_layouts,
285 struct test_option_group *system_groups,
286 struct test_option_group *user_groups)
287 {
288 const char *ruleset = "xkbtests";
289 return test_setup_context_for(ruleset, system_models,
290 user_models, system_layouts,
291 user_layouts, system_groups,
292 user_groups);
293 }
294
295 static struct rxkb_model *
296 fetch_model(struct rxkb_context *ctx, const char *model)
297 {
298 struct rxkb_model *m = rxkb_model_first(ctx);
299 while (m) {
300 if (streq(rxkb_model_get_name(m), model))
301 return rxkb_model_ref(m);
302 m = rxkb_model_next(m);
303 }
304 return NULL;
305 }
306
307 static bool
308 find_model(struct rxkb_context *ctx, const char *model)
309 {
310 struct rxkb_model *m = fetch_model(ctx, model);
311 rxkb_model_unref(m);
312 return m != NULL;
313 }
314
315 static bool
316 find_models(struct rxkb_context *ctx, ...)
317 {
318 va_list args;
319 const char *name;
320 int idx = 0;
321 bool rc = false;
322
323 va_start(args, ctx);
324 name = va_arg(args, const char *);
325 while(name) {
326 ++idx;
327 assert(idx < 20); /* safety guard */
328 if (!find_model(ctx, name))
329 goto out;
330 name = va_arg(args, const char *);
331 };
332
333 rc = true;
334 out:
335 va_end(args);
336 return rc;
337 }
338
339 static struct rxkb_layout *
340 fetch_layout(struct rxkb_context *ctx, const char *layout, const char *variant)
341 {
342 struct rxkb_layout *l = rxkb_layout_first(ctx);
343 while (l) {
344 const char *v = rxkb_layout_get_variant(l);
345
346 if (streq(rxkb_layout_get_name(l), layout) &&
347 ((v == NULL && variant == NULL) ||
348 (v != NULL && variant != NULL && streq(v, variant))))
349 return rxkb_layout_ref(l);
350 l = rxkb_layout_next(l);
351 }
352 return NULL;
353 }
354
355 static bool
356 find_layout(struct rxkb_context *ctx, const char *layout, const char *variant)
357 {
358 struct rxkb_layout *l = fetch_layout(ctx, layout, variant);
359 rxkb_layout_unref(l);
360 return l != NULL;
361 }
362
363 static bool
364 find_layouts(struct rxkb_context *ctx, ...)
365 {
366 va_list args;
367 const char *name, *variant;
368 int idx = 0;
369 bool rc = false;
370
371 va_start(args, ctx);
372 name = va_arg(args, const char *);
373 variant = va_arg(args, const char *);
374 while(name) {
375 ++idx;
376 assert(idx < 20); /* safety guard */
377 if (!find_layout(ctx, name, variant))
378 goto out;
379 name = va_arg(args, const char *);
380 if (name)
381 variant = va_arg(args, const char *);
382 };
383
384 rc = true;
385 out:
386 va_end(args);
387 return rc;
388 }
389
390 static bool
391 check_layouts_order(struct rxkb_context *ctx, ...)
392 {
393 va_list args;
394 const char *layout, *variant;
395 int idx = 0;
396 bool rc = false;
397 struct rxkb_layout *l = rxkb_layout_first(ctx);
398
399 va_start(args, ctx);
400 layout = va_arg(args, const char *);
401 variant = va_arg(args, const char *);
402 while(layout) {
403 ++idx;
404 assert(idx < 20); /* safety guard */
405 if (!l) {
406 fprintf(stderr, "ERROR: expected layout #%d \"%s(%s)\", got none\n",
407 idx, layout, variant ? variant : "-");
408 goto out;
409 }
410 const char *v = rxkb_layout_get_variant(l);
411 if (!streq(rxkb_layout_get_name(l), layout) || !streq_null(v, variant)) {
412 fprintf(stderr, "ERROR: expected layout #%d \"%s(%s)\", got \"%s(%s)\"\n",
413 idx, layout, variant ? variant : "-",
414 rxkb_layout_get_name(l), v ? v : "-");
415 goto out;
416 }
417 layout = va_arg(args, const char *);
418 if (layout)
419 variant = va_arg(args, const char *);
420 l = rxkb_layout_next(l);
421 };
422 if (l) {
423 const char *v = rxkb_layout_get_variant(l);
424 fprintf(stderr, "ERROR: unexpected layout at #%d: \"%s(%s)\"\n",
425 idx + 1, rxkb_layout_get_name(l), v ? v : "-");
426 }
427 rc = l == NULL;
428 out:
429 va_end(args);
430 return rc;
431 }
432
433 static struct rxkb_option_group *
434 fetch_option_group(struct rxkb_context *ctx, const char *grp)
435 {
436 struct rxkb_option_group *g = rxkb_option_group_first(ctx);
437 while (g) {
438 if (streq(grp, rxkb_option_group_get_name(g)))
439 return rxkb_option_group_ref(g);
440 g = rxkb_option_group_next(g);
441 }
442 return NULL;
443 }
444
445 static struct rxkb_option *
446 fetch_option(struct rxkb_context *ctx, const char *grp, const char *opt)
447 {
448 struct rxkb_option_group *g = rxkb_option_group_first(ctx);
449 while (g) {
450 if (streq(grp, rxkb_option_group_get_name(g))) {
451 struct rxkb_option *o = rxkb_option_first(g);
452
453 while (o) {
454 if (streq(opt, rxkb_option_get_name(o)))
455 return rxkb_option_ref(o);
456 o = rxkb_option_next(o);
457 }
458 }
459 g = rxkb_option_group_next(g);
460 }
461 return NULL;
462 }
463
464 static bool
465 find_option(struct rxkb_context *ctx, const char *grp, const char *opt)
466 {
467 struct rxkb_option *o = fetch_option(ctx, grp, opt);
468 rxkb_option_unref(o);
469 return o != NULL;
470 }
471
472 static bool
473 find_options(struct rxkb_context *ctx, ...)
474 {
475 va_list args;
476 const char *grp, *opt;
477 int idx = 0;
478 bool rc = false;
479
480 va_start(args, ctx);
481 grp = va_arg(args, const char *);
482 opt = va_arg(args, const char *);
483 while(grp) {
484 ++idx;
485 assert(idx < 20); /* safety guard */
486 if (!find_option(ctx, grp, opt))
487 goto out;
488 grp = va_arg(args, const char *);
489 if (grp)
490 opt = va_arg(args, const char *);
491 };
492
493 rc = true;
494 out:
495 va_end(args);
496 return rc;
497 }
498
499 static bool
500 cmp_models(struct test_model *tm, struct rxkb_model *m)
501 {
502 if (!tm || !m)
503 return false;
504
505 if (!streq(tm->name, rxkb_model_get_name(m)))
506 return false;
507
508 if (!streq_null(tm->vendor, rxkb_model_get_vendor(m)))
509 return false;
510
511 if (!streq_null(tm->description, rxkb_model_get_description(m)))
512 return false;
513
514 return true;
515 }
516
517 static bool
518 cmp_layouts(struct test_layout *tl, struct rxkb_layout *l)
519 {
520 struct rxkb_iso3166_code *iso3166 = NULL;
521 struct rxkb_iso639_code *iso639 = NULL;
522
523 if (!tl || !l)
524 return false;
525
526 if (!streq(tl->name, rxkb_layout_get_name(l)))
527 return false;
528
529 if (!streq_null(tl->variant, rxkb_layout_get_variant(l)))
530 return false;
531
532 if (!streq_null(tl->brief, rxkb_layout_get_brief(l)))
533 return false;
534
535 if (!streq_null(tl->description, rxkb_layout_get_description(l)))
536 return false;
537
538 iso3166 = rxkb_layout_get_iso3166_first(l);
539 for (size_t i = 0; i < ARRAY_SIZE(tl->iso3166); i++) {
540 const char *iso = tl->iso3166[i];
541 if (iso == NULL && iso3166 == NULL)
542 break;
543
544 if (!streq_null(iso, rxkb_iso3166_code_get_code(iso3166)))
545 return false;
546
547 iso3166 = rxkb_iso3166_code_next(iso3166);
548 }
549
550 if (iso3166 != NULL)
551 return false;
552
553 iso639 = rxkb_layout_get_iso639_first(l);
554 for (size_t i = 0; i < ARRAY_SIZE(tl->iso639); i++) {
555 const char *iso = tl->iso639[i];
556 if (iso == NULL && iso639 == NULL)
557 break;
558
559 if (!streq_null(iso, rxkb_iso639_code_get_code(iso639)))
560 return false;
561
562 iso639 = rxkb_iso639_code_next(iso639);
563 }
564
565 if (iso639 != NULL)
566 return false;
567
568 return true;
569 }
570
571 static bool
572 cmp_options(struct test_option *to, struct rxkb_option *o)
573 {
574 if (!to || !o)
575 return false;
576
577 if (!streq(to->name, rxkb_option_get_name(o)))
578 return false;
579
580 if (!streq_null(to->description, rxkb_option_get_description(o)))
581 return false;
582
583 return true;
584 }
585
586 enum cmp_type {
587 CMP_EXACT,
588 CMP_MATCHING_ONLY,
589 };
590
591 static bool
592 cmp_option_groups(struct test_option_group *tg, struct rxkb_option_group *g,
593 enum cmp_type cmp)
594 {
595 struct rxkb_option *o;
596 struct test_option *to;
597
598 if (!tg || !g)
599 return false;
600
601 if (!streq(tg->name, rxkb_option_group_get_name(g)))
602 return false;
603
604 if (!streq_null(tg->description, rxkb_option_group_get_description(g)))
605 return false;
606
607 if (tg->allow_multiple_selection != rxkb_option_group_allows_multiple(g))
608 return false;
609
610 to = tg->options;
611 o = rxkb_option_first(g);
612
613 while (o && to->name) {
614 if (!cmp_options(to, o))
615 return false;
616 to++;
617 o = rxkb_option_next(o);
618 }
619
620 if (cmp == CMP_EXACT && (o || to->name))
621 return false;
622
623 return true;
624 }
625
626 static void
627 test_load_basic(void)
628 {
629 struct test_model system_models[] = {
630 {"m1"},
631 {"m2"},
632 {NULL},
633 };
634 struct test_layout system_layouts[] = {
635 {"l1"},
636 {"l1", "v1"},
637 {NULL},
638 };
639 struct test_option_group system_groups[] = {
640 {"grp1", NULL, true,
641 { {"grp1:1"}, {"grp1:2"} } },
642 {"grp2", NULL, false,
643 { {"grp2:1"}, {"grp2:2"} } },
644 { NULL },
645 };
646 struct rxkb_context *ctx;
647
648 ctx = test_setup_context(system_models, NULL,
649 system_layouts, NULL,
650 system_groups, NULL);
651
652 assert(find_models(ctx, "m1", "m2", NULL));
653 assert(find_layouts(ctx, "l1", NO_VARIANT,
654 "l1", "v1", NULL));
655 assert(find_options(ctx, "grp1", "grp1:1",
656 "grp1", "grp1:2",
657 "grp2", "grp2:1",
658 "grp2", "grp2:2", NULL));
659 rxkb_context_unref(ctx);
660 }
661
662 static void
663 test_load_full(void)
664 {
665 struct test_model system_models[] = {
666 {"m1", "vendor1", "desc1"},
667 {"m2", "vendor2", "desc2"},
668 {NULL},
669 };
670 struct test_layout system_layouts[] = {
671 {"l1", NO_VARIANT, "lbrief1", "ldesc1"},
672 {"l1", "v1", "vbrief1", "vdesc1"},
673 {"l1", "v2", NULL, "vdesc2"},
674 {NULL},
675 };
676 struct test_option_group system_groups[] = {
677 {"grp1", "gdesc1", true,
678 { {"grp1:1", "odesc11"}, {"grp1:2", "odesc12"} } },
679 {"grp2", "gdesc2", false,
680 { {"grp2:1", "odesc21"}, {"grp2:2", "odesc22"} } },
681 { NULL },
682 };
683 struct rxkb_context *ctx;
684 struct rxkb_model *m;
685 struct rxkb_layout *l;
686 struct rxkb_option_group *g;
687
688 ctx = test_setup_context(system_models, NULL,
689 system_layouts, NULL,
690 system_groups, NULL);
691
692 m = fetch_model(ctx, "m1");
693 assert(cmp_models(&system_models[0], m));
694 rxkb_model_unref(m);
695
696 m = fetch_model(ctx, "m2");
697 assert(cmp_models(&system_models[1], m));
698 rxkb_model_unref(m);
699
700 l = fetch_layout(ctx, "l1", NO_VARIANT);
701 assert(cmp_layouts(&system_layouts[0], l));
702 rxkb_layout_unref(l);
703
704 l = fetch_layout(ctx, "l1", "v1");
705 assert(cmp_layouts(&system_layouts[1], l));
706 rxkb_layout_unref(l);
707
708 l = fetch_layout(ctx, "l1", "v2");
709 struct test_layout expected = {"l1", "v2", "lbrief1", "vdesc2"};
710 assert(cmp_layouts(&expected, l));
711 rxkb_layout_unref(l);
712
713 g = fetch_option_group(ctx, "grp1");
714 assert(cmp_option_groups(&system_groups[0], g, CMP_EXACT));
715 rxkb_option_group_unref(g);
716
717 g = fetch_option_group(ctx, "grp2");
718 assert(cmp_option_groups(&system_groups[1], g, CMP_EXACT));
719 rxkb_option_group_unref(g);
720
721 rxkb_context_unref(ctx);
722 }
723
724 static void
725 test_load_languages(void)
726 {
727 struct test_model system_models[] = {
728 {"m1", "vendor1", "desc1"},
729 {NULL},
730 };
731 struct test_layout system_layouts[] = {
732 {"l1", NO_VARIANT, "lbrief1", "ldesc1",
733 .iso639 = { "abc", "def" },
734 .iso3166 = { "uv", "wx" }},
735 {"l1", "v1", "vbrief1", "vdesc1",
736 .iso639 = {"efg"},
737 .iso3166 = {"yz"}},
738 {"l2", NO_VARIANT, "lbrief1", "ldesc1",
739 .iso639 = { "hij", "klm" },
740 .iso3166 = { "op", "qr" }},
741 {"l2", "v2", "lbrief1", "ldesc1",
742 .iso639 = { NULL }, /* inherit from parent */
743 .iso3166 = { NULL }}, /* inherit from parent */
744 {NULL},
745 };
746 struct test_option_group system_groups[] = {
747 {"grp1", "gdesc1", true,
748 { {"grp1:1", "odesc11"}, {"grp1:2", "odesc12"} } },
749 { NULL },
750 };
751 struct rxkb_context *ctx;
752 struct rxkb_layout *l;
753 struct rxkb_iso3166_code *iso3166;
754 struct rxkb_iso639_code *iso639;
755
756 ctx = test_setup_context(system_models, NULL,
757 system_layouts, NULL,
758 system_groups, NULL);
759
760 l = fetch_layout(ctx, "l1", NO_VARIANT);
761 assert(cmp_layouts(&system_layouts[0], l));
762 rxkb_layout_unref(l);
763
764 l = fetch_layout(ctx, "l1", "v1");
765 assert(cmp_layouts(&system_layouts[1], l));
766 rxkb_layout_unref(l);
767
768 l = fetch_layout(ctx, "l2", "v2");
769 iso3166 = rxkb_layout_get_iso3166_first(l);
770 assert(streq(rxkb_iso3166_code_get_code(iso3166), "op"));
771 iso3166 = rxkb_iso3166_code_next(iso3166);
772 assert(streq(rxkb_iso3166_code_get_code(iso3166), "qr"));
773
774 iso639 = rxkb_layout_get_iso639_first(l);
775 assert(streq(rxkb_iso639_code_get_code(iso639), "hij"));
776 iso639 = rxkb_iso639_code_next(iso639);
777 assert(streq(rxkb_iso639_code_get_code(iso639), "klm"));
778
779 rxkb_layout_unref(l);
780 rxkb_context_unref(ctx);
781 }
782
783 static void
784 test_load_invalid_languages(void)
785 {
786 struct test_model system_models[] = {
787 {"m1", "vendor1", "desc1"},
788 {NULL},
789 };
790 struct test_layout system_layouts[] = {
791 {"l1", NO_VARIANT, "lbrief1", "ldesc1",
792 .iso639 = { "ab", "def" },
793 .iso3166 = { "uvw", "xz" }},
794 {NULL},
795 };
796 struct test_option_group system_groups[] = {
797 {"grp1", "gdesc1", true,
798 { {"grp1:1", "odesc11"}, {"grp1:2", "odesc12"} } },
799 { NULL },
800 };
801 struct rxkb_context *ctx;
802 struct rxkb_layout *l;
803 struct rxkb_iso3166_code *iso3166;
804 struct rxkb_iso639_code *iso639;
805
806 ctx = test_setup_context(system_models, NULL,
807 system_layouts, NULL,
808 system_groups, NULL);
809
810 l = fetch_layout(ctx, "l1", NO_VARIANT);
811 /* uvw is invalid, we expect 2 letters, verify it was ignored */
812 iso3166 = rxkb_layout_get_iso3166_first(l);
813 assert(streq(rxkb_iso3166_code_get_code(iso3166), "xz"));
814 assert(rxkb_iso3166_code_next(iso3166) == NULL);
815
816 /* ab is invalid, we expect 3 letters, verify it was ignored */
817 iso639 = rxkb_layout_get_iso639_first(l);
818 assert(streq(rxkb_iso639_code_get_code(iso639), "def"));
819 assert(rxkb_iso639_code_next(iso639) == NULL);
820 rxkb_layout_unref(l);
821
822 rxkb_context_unref(ctx);
823 }
824
825 static void
826 test_popularity(void)
827 {
828 assert(POPULARITY_UNDEFINED != POPULARITY_STANDARD);
829 assert(POPULARITY_UNDEFINED != POPULARITY_EXOTIC);
830
831 struct test_layout system_layouts[] = {
832 {.name = "l1", .variant = NO_VARIANT }, /* Default popularity */
833 {.name = "l1", .variant = "v1" }, /* Default popularity */
834 {.name = "l2", .popularity = POPULARITY_STANDARD },
835 {.name = "l3", .popularity = POPULARITY_EXOTIC },
836 {NULL},
837 };
838 struct rxkb_context *ctx;
839 struct rxkb_layout *l;
840 struct ruleset_conf {
841 const char *ruleset;
842 enum rxkb_context_flags flags;
843 enum rxkb_popularity popularity; /* Default popularity */
844 };
845 struct ruleset_conf rulesets[] = {
846 { /* Rules with “standard” popularity */
847 .ruleset="xkbtests",
848 .flags=RXKB_CONTEXT_NO_DEFAULT_INCLUDES,
849 .popularity=RXKB_POPULARITY_STANDARD
850 },
851 { /* Rules with “exotic” popularity (hack, see below) */
852 .ruleset="xkbtests.extras",
853 .flags=RXKB_CONTEXT_NO_DEFAULT_INCLUDES |
854 RXKB_CONTEXT_LOAD_EXOTIC_RULES,
855 .popularity=RXKB_POPULARITY_EXOTIC
856 }
857 };
858 for (size_t k = 0; k < ARRAY_SIZE(rulesets); k++) {
859 struct ruleset_conf *conf = &rulesets[k];
860 char *dir = NULL;
861
862 dir = test_create_rules(conf->ruleset, NULL, system_layouts, NULL);
863 ctx = rxkb_context_new(conf->flags);
864 assert(ctx);
865 assert(rxkb_context_include_path_append(ctx, dir));
866 /* Hack: ruleset "xkbtests.extras" above generates xkbtests.extras.xml,
867 * loading "xkbtests" means the extras file counts as exotic */
868 assert(rxkb_context_parse(ctx, "xkbtests"));
869
870 /* Test implicit popularity */
871 l = fetch_layout(ctx, "l1", NO_VARIANT);
872 assert(rxkb_layout_get_popularity(l) == conf->popularity);
873 rxkb_layout_unref(l);
874
875 l = fetch_layout(ctx, "l1", "v1");
876 assert(rxkb_layout_get_popularity(l) == conf->popularity);
877 rxkb_layout_unref(l);
878
879 /* Test explicit popularity */
880 l = fetch_layout(ctx, "l2", NO_VARIANT);
881 assert(rxkb_layout_get_popularity(l) == RXKB_POPULARITY_STANDARD);
882 rxkb_layout_unref(l);
883
884 l = fetch_layout(ctx, "l3", NO_VARIANT);
885 assert(rxkb_layout_get_popularity(l) == RXKB_POPULARITY_EXOTIC);
886 rxkb_layout_unref(l);
887
888 test_remove_rules(dir, conf->ruleset);
889 rxkb_context_unref(ctx);
890 }
891 }
892
893
894 static void
895 test_load_merge(void)
896 {
897 struct test_model system_models[] = {
898 {"m1", "vendor1", "desc1"},
899 {"m2", "vendor2", "desc2"},
900 {NULL},
901 };
902 struct test_model user_models[] = {
903 {"m3", "vendor3", "desc3"},
904 {"m4", "vendor4", "desc4"},
905 {NULL},
906 };
907 struct test_layout system_layouts[] = {
908 {"l1", NO_VARIANT, "lbrief1", "ldesc1"},
909 {"l1", "v1", "vbrief1", "vdesc1"},
910 {NULL},
911 };
912 struct test_layout user_layouts[] = {
913 {"l2", NO_VARIANT, "lbrief2", "ldesc2"},
914 {"l2", "v2", "vbrief2", "vdesc2"},
915 // Duplicate with system
916 {"l1", NO_VARIANT, "lbrief1bis", "ldesc1bis"},
917 {"l1", "v1", "vbrief1bis", "vdesc1bis"},
918 {NULL},
919 };
920 struct test_option_group system_groups[] = {
921 {"grp1", NULL, true,
922 { {"grp1:1"}, {"grp1:2"} } },
923 {"grp2", NULL, false,
924 { {"grp2:1"}, {"grp2:2"} } },
925 { NULL },
926 };
927 struct test_option_group user_groups[] = {
928 {"grp3", NULL, true,
929 { {"grp3:1"}, {"grp3:2"} } },
930 {"grp4", NULL, false,
931 { {"grp4:1"}, {"grp4:2"} } },
932 { NULL },
933 };
934 struct rxkb_context *ctx;
935 struct rxkb_model *m;
936 struct rxkb_layout *l;
937 struct rxkb_option_group *g;
938
939 ctx = test_setup_context(system_models, user_models,
940 system_layouts, user_layouts,
941 system_groups, user_groups);
942
943 assert(find_models(ctx, "m1", "m2", "m3", "m4", NULL));
944 assert(find_layouts(ctx, "l1", NO_VARIANT,
945 "l1", "v1",
946 "l2", NO_VARIANT,
947 "l2", "v2", NULL));
948
949 m = fetch_model(ctx, "m1");
950 assert(cmp_models(&system_models[0], m));
951 rxkb_model_unref(m);
952
953 m = fetch_model(ctx, "m2");
954 assert(cmp_models(&system_models[1], m));
955 rxkb_model_unref(m);
956
957 m = fetch_model(ctx, "m3");
958 assert(cmp_models(&user_models[0], m));
959 rxkb_model_unref(m);
960
961 m = fetch_model(ctx, "m4");
962 assert(cmp_models(&user_models[1], m));
963 rxkb_model_unref(m);
964
965 l = fetch_layout(ctx, "l1", NO_VARIANT);
966 assert(cmp_layouts(&system_layouts[0], l));
967 rxkb_layout_unref(l);
968
969 l = fetch_layout(ctx, "l1", "v1");
970 assert(cmp_layouts(&system_layouts[1], l));
971 rxkb_layout_unref(l);
972
973 l = fetch_layout(ctx, "l2", NO_VARIANT);
974 assert(cmp_layouts(&user_layouts[0], l));
975 rxkb_layout_unref(l);
976
977 l = fetch_layout(ctx, "l2", "v2");
978 assert(cmp_layouts(&user_layouts[1], l));
979 rxkb_layout_unref(l);
980
981 /* Check that the duplicate layouts are skipped */
982 assert(check_layouts_order(ctx,
983 "l1", NO_VARIANT, "l1", "v1",
984 "l2", NO_VARIANT, "l2", "v2",
985 NULL));
986
987 g = fetch_option_group(ctx, "grp1");
988 assert(cmp_option_groups(&system_groups[0], g, CMP_EXACT));
989 rxkb_option_group_unref(g);
990
991 g = fetch_option_group(ctx, "grp2");
992 assert(cmp_option_groups(&system_groups[1], g, CMP_EXACT));
993 rxkb_option_group_unref(g);
994
995 g = fetch_option_group(ctx, "grp3");
996 assert(cmp_option_groups(&user_groups[0], g, CMP_EXACT));
997 rxkb_option_group_unref(g);
998
999 g = fetch_option_group(ctx, "grp4");
1000 assert(cmp_option_groups(&user_groups[1], g, CMP_EXACT));
1001 rxkb_option_group_unref(g);
1002
1003 rxkb_context_unref(ctx);
1004 }
1005
1006 static void
1007 test_load_merge_no_overwrite(void)
1008 {
1009 struct test_model system_models[] = {
1010 {"m1", "vendor1", "desc1"},
1011 {"m2", "vendor2", "desc2"},
1012 {NULL},
1013 };
1014 struct test_model user_models[] = {
1015 {"m1", "vendor3", "desc3"}, /* must not overwrite */
1016 {"m4", "vendor4", "desc4"},
1017 {NULL},
1018 };
1019 struct test_layout system_layouts[] = {
1020 {"l1", NO_VARIANT, "lbrief1", "ldesc1"},
1021 {"l1", "v1", "vbrief1", "vdesc1"},
1022 {NULL},
1023 };
1024 struct test_layout user_layouts[] = {
1025 {"l2", NO_VARIANT, "lbrief2", "ldesc2"},
1026 {"l2", "v2", "vbrief2", "vdesc2"},
1027 {"l1", NO_VARIANT, "lbrief3", "ldesc3"}, /* must not overwrite */
1028 {"l1", "v2", "vbrief3", "vdesc3"}, /* must not overwrite */
1029 {NULL},
1030 };
1031 struct test_option_group system_groups[] = {
1032 {"grp1", "gdesc1", true,
1033 { {"grp1:1", "odesc11"}, {"grp1:2", "odesc12"} } },
1034 {"grp2", "gdesc2", false,
1035 { {"grp2:1", "odesc21"}, {"grp2:2", "odesc22"} } },
1036 { NULL },
1037 };
1038 struct test_option_group user_groups[] = {
1039 {"grp1", "XXXXX", false, /* must not overwrite */
1040 { {"grp1:1", "YYYYYYY"}, /* must not overwrite */
1041 {"grp1:3", "ZZZZZZ"} } }, /* append */
1042 {"grp4", "gdesc4", false,
1043 { {"grp4:1", "odesc41"}, {"grp4:2", "odesc42"} } },
1044 { NULL },
1045 };
1046 struct rxkb_context *ctx;
1047 struct rxkb_model *m;
1048 struct rxkb_layout *l;
1049 struct rxkb_option_group *g;
1050
1051 ctx = test_setup_context(system_models, user_models,
1052 system_layouts, user_layouts,
1053 system_groups, user_groups);
1054
1055 m = fetch_model(ctx, "m1");
1056 assert(cmp_models(&system_models[0], m));
1057 rxkb_model_unref(m);
1058
1059 l = fetch_layout(ctx, "l1", NO_VARIANT);
1060 assert(cmp_layouts(&system_layouts[0], l));
1061 rxkb_layout_unref(l);
1062
1063 l = fetch_layout(ctx, "l1", "v1");
1064 assert(cmp_layouts(&system_layouts[1], l));
1065 rxkb_layout_unref(l);
1066
1067 assert(find_option(ctx, "grp1", "grp1:3"));
1068 g = fetch_option_group(ctx, "grp1");
1069 assert(cmp_option_groups(&system_groups[0], g, CMP_MATCHING_ONLY));
1070 rxkb_option_group_unref(g);
1071
1072 rxkb_context_unref(ctx);
1073 }
1074
1075 static void
1076 test_no_include_paths(void)
1077 {
1078 struct rxkb_context *ctx;
1079
1080 ctx = rxkb_context_new(RXKB_CONTEXT_NO_DEFAULT_INCLUDES);
1081 assert(ctx);
1082 assert(!rxkb_context_parse_default_ruleset(ctx));
1083
1084 rxkb_context_unref(ctx);
1085 }
1086
1087 static void
1088 test_invalid_include(void)
1089 {
1090 struct rxkb_context *ctx;
1091
1092 ctx = rxkb_context_new(RXKB_CONTEXT_NO_DEFAULT_INCLUDES);
1093 assert(ctx);
1094 assert(!rxkb_context_include_path_append(ctx, "/foo/bar/baz/bat"));
1095 assert(!rxkb_context_parse_default_ruleset(ctx));
1096
1097 rxkb_context_unref(ctx);
1098 }
1099
1100 /* Check that libxml2 error handler is reset after parsing */
1101 static void
1102 test_xml_error_handler(void)
1103 {
1104 struct test_model system_models[] = { {NULL} };
1105 struct test_layout system_layouts[] = { {NULL} };
1106 struct test_option_group system_groups[] = { { NULL } };
1107 struct rxkb_context *ctx;
1108
1109 ctx = test_setup_context(system_models, NULL,
1110 system_layouts, NULL,
1111 system_groups, NULL);
1112 assert(ctx);
1113 rxkb_context_unref(ctx);
1114
1115 const char invalid_xml[] = "<test";
1116 /* This should trigger a segfault if error handler is not reset, because
1117 * else it would use our error handler with an invalid (freed) context. */
1118 xmlDocPtr doc = xmlParseMemory (invalid_xml, strlen(invalid_xml));
1119 assert(!doc);
1120 xmlFreeDoc (doc);
1121 xmlCleanupParser();
1122 }
1123
1124 int
1125 main(void)
1126 {
1127 test_init();
1128
1129 test_xml_error_handler();
1130 test_no_include_paths();
1131 test_invalid_include();
1132 test_load_basic();
1133 test_load_full();
1134 test_load_merge();
1135 test_load_merge_no_overwrite();
1136 test_load_languages();
1137 test_load_invalid_languages();
1138 test_popularity();
1139
1140 return 0;
1141 }