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