1
0

rules-file.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * Copyright © 2012 Ran Benita <ran234@gmail.com>
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #include "test-config.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "xkbcommon/xkbcommon.h"
  10. #include "context.h"
  11. #include "keymap.h"
  12. #include "utils.h"
  13. #include "xkbcomp/rules.h"
  14. #include "test.h"
  15. struct test_data {
  16. /* Rules file */
  17. const char *rules;
  18. /* Input */
  19. const char *model;
  20. const char *layout;
  21. const char *variant;
  22. const char *options;
  23. /* Expected output */
  24. const char *keycodes;
  25. const char *types;
  26. const char *compat;
  27. const char *symbols;
  28. const char *geometry;
  29. xkb_layout_index_t explicit_layouts;
  30. /* Or set this if xkb_components_from_rules() should fail. */
  31. bool should_fail;
  32. };
  33. static bool
  34. test_rules(struct xkb_context *ctx, const struct test_data *data)
  35. {
  36. fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
  37. data->model, data->layout, data->variant, data->options);
  38. if (data->should_fail)
  39. fprintf(stderr, "Expecting: FAILURE\n");
  40. else
  41. fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\t%s\t%"PRIu32"\n",
  42. data->keycodes, data->types, data->compat, data->symbols,
  43. data->geometry, data->explicit_layouts);
  44. bool passed = true;
  45. xkb_layout_index_t explicit_layouts = 0;
  46. enum api_type {
  47. API_PRIVATE = 0,
  48. API_PUBLIC
  49. };
  50. for (enum api_type api = API_PRIVATE; api <= API_PUBLIC; api++) {
  51. bool ok;
  52. const struct xkb_rule_names rmlvo = {
  53. .rules = data->rules,
  54. .model = data->model,
  55. .layout = data->layout,
  56. .variant = data->variant,
  57. .options = data->options
  58. };
  59. struct xkb_component_names kccgst;
  60. if (api == API_PRIVATE) {
  61. /* Private API */
  62. ok = xkb_components_from_rules_names(ctx, &rmlvo, &kccgst,
  63. &explicit_layouts);
  64. } else {
  65. /* Public API */
  66. ok = xkb_components_names_from_rules(ctx, &rmlvo, NULL, &kccgst);
  67. }
  68. if (ok) {
  69. fprintf(stderr, "Received : %s\t%s\t%s\t%s\t%s\t%"PRIu32"\n",
  70. kccgst.keycodes, kccgst.types, kccgst.compatibility,
  71. kccgst.symbols, kccgst.geometry, explicit_layouts);
  72. } else {
  73. fprintf(stderr, "Received : FAILURE\n");
  74. return data->should_fail;
  75. }
  76. passed &= streq_not_null(kccgst.keycodes, data->keycodes) &&
  77. streq_not_null(kccgst.types, data->types) &&
  78. streq_not_null(kccgst.compatibility, data->compat) &&
  79. streq_not_null(kccgst.symbols, data->symbols) &&
  80. streq_null(kccgst.geometry, data->geometry) &&
  81. explicit_layouts == data->explicit_layouts;
  82. free(kccgst.keycodes);
  83. free(kccgst.types);
  84. free(kccgst.compatibility);
  85. free(kccgst.symbols);
  86. free(kccgst.geometry);
  87. }
  88. return passed;
  89. }
  90. static void
  91. test_encodings(struct xkb_context *ctx)
  92. {
  93. static const struct test_data tests[] = {
  94. {
  95. .rules = "utf-8_with_bom",
  96. .model = "my_model", .layout = "my_layout", .variant = "my_variant",
  97. .options = "my_option",
  98. .keycodes = "my_keycodes", .types = "my_types",
  99. .compat = "my_compat|some:compat",
  100. .symbols = "my_symbols+extra_variant",
  101. .explicit_layouts = 1,
  102. },
  103. {
  104. .rules = "utf-16le_with_bom",
  105. .model = "my_model", .layout = "my_layout", .variant = "my_variant",
  106. .options = "my_option",
  107. .keycodes = "my_keycodes", .types = "my_types",
  108. .compat = "my_compat|some:compat",
  109. .symbols = "my_symbols+extra_variant",
  110. .explicit_layouts = 1,
  111. .should_fail = true,
  112. },
  113. {
  114. .rules = "utf-16be_with_bom",
  115. .model = "my_model", .layout = "my_layout", .variant = "my_variant",
  116. .options = "my_option",
  117. .keycodes = "my_keycodes", .types = "my_types",
  118. .compat = "my_compat|some:compat",
  119. .symbols = "my_symbols+extra_variant",
  120. .explicit_layouts = 1,
  121. .should_fail = true,
  122. },
  123. {
  124. .rules = "utf-32be",
  125. .model = "my_model", .layout = "my_layout", .variant = "my_variant",
  126. .options = "my_option",
  127. .keycodes = "my_keycodes", .types = "my_types",
  128. .compat = "my_compat|some:compat",
  129. .symbols = "my_symbols+extra_variant",
  130. .explicit_layouts = 1,
  131. .should_fail = true,
  132. }
  133. };
  134. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  135. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  136. assert(test_rules(ctx, &tests[k]));
  137. }
  138. }
  139. /* Only parse strict decimal groups */
  140. static void
  141. test_strict_decimal_groups(struct xkb_context *ctx)
  142. {
  143. static const struct test_data tests[] = {
  144. {
  145. .rules = "invalid-group-index-1",
  146. .model = "m", .layout = "1,2", .variant = NULL,
  147. .options = NULL,
  148. .should_fail = true,
  149. },
  150. {
  151. .rules = "invalid-group-index-2",
  152. .model = "m", .layout = "1", .variant = NULL,
  153. .options = NULL,
  154. .should_fail = true,
  155. },
  156. {
  157. .rules = "invalid-group-index-3",
  158. .model = "m", .layout = "1", .variant = NULL,
  159. .options = NULL,
  160. .should_fail = true,
  161. },
  162. {
  163. .rules = "invalid-group-qualifier",
  164. .model = "m", .layout = "1,2", .variant = NULL,
  165. .options = NULL,
  166. .keycodes = "default_keycodes", .types = "default_types",
  167. .compat = "default_compat",
  168. .symbols = "default_symbols+default_symbols:+2",
  169. .explicit_layouts = 1,
  170. }
  171. };
  172. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  173. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  174. assert(test_rules(ctx, &tests[k]));
  175. }
  176. }
  177. static void
  178. test_simple(struct xkb_context *ctx)
  179. {
  180. static const struct test_data tests[] = {
  181. {
  182. .rules = "simple",
  183. .model = "my_model", .layout = "my_layout", .variant = "my_variant",
  184. .options = "my_option",
  185. .keycodes = "my_keycodes", .types = "my_types",
  186. .compat = "my_compat|some:compat",
  187. .symbols = "my_symbols+extra_variant",
  188. .explicit_layouts = 1,
  189. },
  190. {
  191. .rules = "simple",
  192. .model = "", .layout = "foo", .variant = "", .options = "",
  193. .keycodes = "default_keycodes", .types = "default_types",
  194. .compat = "default_compat", .symbols = "default_symbols",
  195. .explicit_layouts = 1,
  196. },
  197. {
  198. .rules = "groups",
  199. .model = "pc104", .layout = "foo", .variant = "", .options = "",
  200. .keycodes = "something(pc104)", .types = "default_types",
  201. .compat = "default_compat", .symbols = "default_symbols",
  202. .explicit_layouts = 1,
  203. },
  204. {
  205. .rules = "groups",
  206. .model = "foo", .layout = "ar", .variant = "bar", .options = "",
  207. .keycodes = "default_keycodes", .types = "default_types",
  208. .compat = "default_compat", .symbols = "my_symbols+(bar)",
  209. .explicit_layouts = 1,
  210. },
  211. {
  212. .rules = "simple",
  213. .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
  214. .options = "my_option",
  215. .should_fail = true
  216. },
  217. {
  218. .rules = "index",
  219. .model = "", .layout = "br,al,cn,az", .variant = "",
  220. .options = "some:opt",
  221. .keycodes = "default_keycodes", .types = "default_types",
  222. .compat = "default_compat",
  223. .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
  224. .explicit_layouts = 4,
  225. },
  226. {
  227. .rules = "multiple-options",
  228. .model = "my_model", .layout = "my_layout", .variant = "my_variant",
  229. .options = "option3,option1,colon:opt,option11",
  230. .keycodes = "my_keycodes", .types = "my_types",
  231. .compat = "my_compat+some:compat+group(bla)",
  232. .symbols = "my_symbols+extra_variant+compose(foo)+keypad(bar)+altwin(menu)",
  233. .explicit_layouts = 1,
  234. },
  235. /* “replace” merge mode */
  236. {
  237. .rules = "merge-mode-replace",
  238. .model = "my_model", .layout = "us,de", .variant = "",
  239. .options = "replace:first",
  240. .keycodes = "evdev", .types = "complete",
  241. .compat = "complete",
  242. .symbols = "pc+us+de:2^level3(ralt_alt)|empty",
  243. .explicit_layouts = 2,
  244. }
  245. };
  246. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  247. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  248. assert(test_rules(ctx, &tests[k]));
  249. }
  250. }
  251. static void
  252. test_wild_card(struct xkb_context *ctx)
  253. {
  254. /* Wild card does not match empty entries for layouts and variants */
  255. #define ENTRY(_model, _layout, _variant, _options, _symbols, _layouts, _fail) \
  256. { .rules = "wildcard", .model = (_model), \
  257. .layout = (_layout), .variant = (_variant), .options = (_options), \
  258. .keycodes = "evdev", .types = "complete", .compat = "complete", \
  259. .symbols = (_symbols) , .geometry = "pc(pc104)", \
  260. .explicit_layouts = (_layouts), .should_fail = (_fail) }
  261. static const struct test_data tests[] = {
  262. /* OK: empty model and options and at least one layout+variant combo */
  263. ENTRY(NULL, "a" , "1" , NULL, "pc+a(1)", 1, false),
  264. ENTRY("" , "a" , "1" , "" , "pc+a(1)", 1, false),
  265. ENTRY("" , "a," , "1," , "" , "pc+a(1)", 1, false),
  266. ENTRY("" , ",b" , ",2" , "" , "+b(2):2", 2, false),
  267. ENTRY("" , "a,b", "1," , "" , "pc+a(1)", 1, false),
  268. ENTRY("" , "a,b", ",2" , "" , "+b(2):2", 2, false),
  269. /* Fails: empty layout or variant */
  270. ENTRY(NULL, NULL , NULL , NULL, "", 1, true),
  271. ENTRY(NULL, "" , "" , NULL, "", 1, true),
  272. ENTRY(NULL, NULL , "1" , NULL, "", 1, true),
  273. ENTRY(NULL, "" , "1" , NULL, "", 1, true),
  274. ENTRY(NULL, "," , "1,2", NULL, "", 2, true),
  275. ENTRY(NULL, "a" , NULL , NULL, "", 1, true),
  276. ENTRY(NULL, "a" , "" , NULL, "", 1, true),
  277. ENTRY(NULL, "a,b", NULL , NULL, "", 2, true),
  278. ENTRY(NULL, "a,b", "" , NULL, "", 2, true),
  279. ENTRY(NULL, "a,b", "," , NULL, "", 2, true)
  280. };
  281. #undef ENTRY
  282. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  283. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  284. assert(test_rules(ctx, &tests[k]));
  285. }
  286. }
  287. /* Test extended wild cards: <none>, <some> and <any> */
  288. static void
  289. test_extended_wilcards(struct xkb_context *ctx)
  290. {
  291. #define ENTRY(_model, _layout, _variant, _options, _symbols, _layouts, _fail) {\
  292. .rules = "extended-wild-cards", .model = (_model), \
  293. .layout = (_layout), .variant = (_variant), .options = (_options), \
  294. .keycodes = "evdev", .types = "complete", .compat = "complete", \
  295. .symbols = (_symbols) , .explicit_layouts = (_layouts), \
  296. .should_fail = (_fail) \
  297. }
  298. static const struct test_data tests[] = {
  299. /*
  300. * Model
  301. */
  302. ENTRY(NULL, "l1", NULL, NULL, "pc+l10:1+o1+o2+o4+o6:1+o8:1", 1, false),
  303. ENTRY("", "l1", NULL, NULL, "pc+l10:1+o1+o2+o4+o6:1+o8:1", 1, false),
  304. ENTRY("m", "l1", NULL, NULL, "pc+l10:1+o1+o2+o4+o6:1+o8:1", 1, false),
  305. /*
  306. * Layout / variant
  307. */
  308. ENTRY("m", "l1", NULL, NULL, "pc+l10:1+o1+o2+o4+o6:1+o8:1", 1, false),
  309. ENTRY("m", "l1", "v1", NULL, "pc+l20:1+o1+o2+o4+o6:1+o8:1", 1, false),
  310. ENTRY("m", "l1", "v2", NULL, "pc+l30(v2):1+o1+o2+o4+o6:1+o8:1", 1, false),
  311. /* legacy wild card * does not catch empty variant */
  312. ENTRY("m", "l2", NULL, NULL, "pc+l2:1+o1+o2+o4+o6:1+o8:1", 1, false),
  313. ENTRY("m", "l2", "v1", NULL, "pc+l40(v1):1+o1+o2+o4+o6:1+o8:1", 1, false),
  314. ENTRY("m", "l2", "v2", NULL, "pc+l40(v2):1+o1+o2+o4+o6:1+o8:1", 1, false),
  315. ENTRY("m", "l3", NULL, NULL, "pc+l50:1+o1+o2+o4+o6:1+o8:1", 1, false),
  316. ENTRY("m", "l3", "v1", NULL, "pc+l50(v1):1+o1+o2+o4+o6:1+o8:1", 1, false),
  317. ENTRY("m", "l3", "v2", NULL, "pc+l50(v2):1+o1+o2+o4+o6:1+o8:1", 1, false),
  318. /* ? wild card does catch empty variant */
  319. ENTRY("m", "l4", NULL, NULL, "pc+l4:1+o1+o2+o4+o6:1+o8:1", 1, false),
  320. ENTRY("m", "l4", "v1", NULL, "pc+l4(v1):1+o1+o2+o4+o6:1+o8:1", 1, false),
  321. ENTRY("m", "l4", "v2", NULL, "pc+l4(v20):1+o1+o2+o4+o6:1+o8:1", 1, false),
  322. ENTRY("m", "l1,l1,l1,l2", ",v1,v2,", NULL,
  323. "pc+l10:1+l20:2+l30(v2):3+l2:4"
  324. "+o1+o2+o4+o6:1+o8:1+o6:2+o8:2+o6:3+o8:3+o6:4+o8:4", 4, false),
  325. ENTRY("m", "l2,l2,l3,l3", "v1,v2,,v1", NULL,
  326. "pc+l40(v1):1+l40(v2):2+l50:3+l50(v1):4"
  327. "+o1+o2+o4+o6:1+o8:1+o6:2+o8:2+o6:3+o8:3+o6:4+o8:4", 4, false),
  328. ENTRY("m", "l3,l4,l4,l4", "v2,,v1,v2", NULL,
  329. "pc+l50(v2):1+l4:2+l4(v1):3+l4(v20):4"
  330. "+o1+o2+o4+o6:1+o8:1+o6:2+o8:2+o6:3+o8:3+o6:4+o8:4", 4, false),
  331. /*
  332. * Options
  333. */
  334. /* Empty list: check `*`, `<none>`, `<any>` wild cards */
  335. ENTRY("m", "l1", NULL, NULL, "pc+l10:1+o1+o2+o4+o6:1+o8:1", 1, false),
  336. ENTRY("m", "l1", NULL, "", "pc+l10:1+o1+o2+o4+o6:1+o8:1", 1, false),
  337. ENTRY("m", "l1", NULL, ",", "pc+l10:1+o1+o2+o4+o6:1+o8:1", 1, false),
  338. ENTRY("m", "l1", NULL, "!", "pc+l10:1+o1+o2+o4+o6:1+o8:1", 1, false), /* discarded */
  339. /* Some values */
  340. ENTRY("m", "l1", NULL, "*", "pc+l10:1+o1+o3+o4+o7:1+o8:1", 1, false),
  341. ENTRY("m", "l1", NULL, ":", "pc+l10:1+o1+o3+o4+o7:1+o8:1", 1, false),
  342. ENTRY("m", "l1", NULL, "=", "pc+l10:1+o1+o3+o4+o7:1+o8:1", 1, false),
  343. ENTRY("m", "l1", NULL, "opt0", "pc+l10:1+o0+o1+o3+o4+o7:1+o8:1", 1, false),
  344. };
  345. #undef ENTRY
  346. for (size_t t = 0; t < ARRAY_SIZE(tests); t++) {
  347. fprintf(stderr, "------\n*** %s: #%zu ***\n", __func__, t);
  348. assert(test_rules(ctx, &tests[t]));
  349. }
  350. }
  351. static void
  352. test_layout_index_ranges(struct xkb_context *ctx, const char *too_much_layouts,
  353. const char *too_much_symbols)
  354. {
  355. #define ENTRY2(_rules, _model, _layout, _variant, _options, \
  356. _keycodes, _types, _compat, _symbols, _count, _should_fail) \
  357. { .rules = (_rules), .model = (_model), \
  358. .layout = (_layout), .variant = (_variant), .options = (_options), \
  359. .keycodes = (_keycodes), .types = (_types), .compat = (_compat), \
  360. .symbols = (_symbols), \
  361. .geometry = (strncmp((_rules), "evdev", 5) == 0) ? "pc(pc104)" : NULL,\
  362. .explicit_layouts = (_count), .should_fail = (_should_fail) }
  363. #define ENTRY(layout, variant, options, symbols, count, should_fail) \
  364. ENTRY2("special_indices", NULL, layout, variant, options, \
  365. "default_keycodes", "default_types", "default_compat", symbols, \
  366. count, should_fail)
  367. const struct test_data tests[] = {
  368. /* Test index ranges: layout vs layout[first] */
  369. ENTRY("layout_a", NULL, NULL, "A", 1, false),
  370. ENTRY("layout_e", NULL, NULL, "E+layout_e", 1, false),
  371. ENTRY("a", NULL, NULL, "a", 1, false),
  372. ENTRY("a", "1", NULL, "a(1)", 1, false),
  373. /* Test index ranges: invalid layout qualifier */
  374. ENTRY("layout_c", NULL, NULL, "C:1+z:1", 1, false),
  375. /* Test index ranges: invalid layout[first] qualifier */
  376. ENTRY("layout_d", NULL, NULL, "D", 1, false),
  377. /* Test index ranges: multiple layouts */
  378. ENTRY("a,b", NULL, NULL, "a+b:2", 2, false),
  379. ENTRY("a,b", ",c", NULL, "a+b(c):2", 2, false),
  380. ENTRY("layout_e,layout_a", NULL, NULL, "e:1+x:2+m:2", 2, false),
  381. ENTRY("layout_a,layout_b,layout_c,layout_d", NULL, NULL,
  382. "a:1+y:2+layout_c:3+layout_d:4+m:1+z:3", 4, false),
  383. ENTRY("layout_a,layout_b,layout_c,layout_d",
  384. "extra,,,extra", NULL,
  385. "a:1+y:2+layout_c:3+layout_d(extra):4+m:1+z:3"
  386. "+foo:1|bar:1+foo:4|bar:4", 4, false),
  387. ENTRY("layout_a,layout_b,layout_c,layout_d,layout_e", NULL, NULL,
  388. "a:1+y:2+layout_c:3+layout_d:4+layout_e:5+m:1+z:3", 5, false),
  389. /* Check that special indices merge the KcCGST values in the expected order */
  390. ENTRY("layout_a,layout_b,layout_c", NULL, "option_3,option_2,option_1",
  391. "a:1+y:2+layout_c:3+m:1+z:3+III:2+JJJ:2+HHH:3+KKK:3+LLL+OOO:2+MMM:3+NNN:3",
  392. 3, false),
  393. #undef ENTRY
  394. /* Test index ranges: too much layouts */
  395. ENTRY2("special_indices-limit", NULL, too_much_layouts, NULL, NULL,
  396. "default_keycodes", "default_types", "default_compat", too_much_symbols,
  397. XKB_MAX_GROUPS, false),
  398. #define ENTRY(model, layout, variant, options, compat, symbols, count, should_fail) \
  399. ENTRY2("evdev-modern", model, layout, variant, options, \
  400. "evdev+aliases(qwerty)", "complete", compat, symbols, count, should_fail)
  401. /* evdev-modern: 1 layout */
  402. ENTRY("whatever", "ar", NULL, NULL, "complete", "pc+ara+inet(evdev)", 1, false),
  403. ENTRY("whatever", "ben", "probhat", NULL, "complete", "pc+in(ben_probhat)+inet(evdev)", 1, false),
  404. ENTRY("ataritt", "es", NULL, NULL, "complete", "xfree68_vndr/ataritt(us)+es+inet(evdev)", 1, false),
  405. ENTRY("ataritt", "jp", NULL, NULL, "complete+japan", "xfree68_vndr/ataritt(us)+jp+inet(evdev)", 1, false),
  406. ENTRY2("evdev-modern", "olpc", "us", NULL, NULL,
  407. "evdev+olpc(olpc)+aliases(qwerty)", "complete", "olpc", "olpc+us(olpc)+inet(evdev)", 1, false),
  408. ENTRY2("evdev-modern", "olpc", "jp", NULL, NULL,
  409. "evdev+olpc(olpc)+aliases(qwerty)", "complete", "complete+japan", "olpc+jp+inet(evdev)", 1, false),
  410. ENTRY("pc104", "jp", NULL, NULL, "complete+japan", "pc+jp+inet(evdev)", 1, false),
  411. ENTRY("pc104", "jp", "xxx", NULL, "complete+japan", "pc+jp(xxx)+inet(evdev)", 1, false),
  412. ENTRY("pc104", "es", NULL, NULL, "complete", "pc+es+inet(evdev)", 1, false),
  413. ENTRY("pc104", "es", "xxx", NULL, "complete", "pc+es(xxx)+inet(evdev)", 1, false),
  414. ENTRY2("evdev-modern", "pc104", "de", "neo", NULL,
  415. "evdev+aliases(qwertz)", "complete",
  416. "complete+caps(caps_lock):1+misc(assign_shift_left_action):1+level5(level5_lock):1",
  417. "pc+de(neo)+inet(evdev)", 1, false),
  418. ENTRY("pc104", "br", NULL, "misc:typo,misc:apl", "complete",
  419. "pc+br+inet(evdev)+apl(level3):1+typo(base):1", 1, false),
  420. /* evdev-modern: 2 layouts */
  421. ENTRY("whatever", "ar,pt", NULL, NULL, "complete", "pc+ara+pt:2+inet(evdev)", 2, false),
  422. ENTRY("whatever", "pt,ar", NULL, NULL, "complete", "pc+pt+ara:2+inet(evdev)", 2, false),
  423. ENTRY("whatever", "ben,gb", "probhat,", NULL, "complete",
  424. "pc+in(ben_probhat)+gb:2+inet(evdev)", 2, false),
  425. ENTRY("whatever", "gb,ben", ",probhat", NULL, "complete",
  426. "pc+gb+in(ben):2+in(ben_probhat):2+inet(evdev)", 2, false),
  427. ENTRY("whatever", "ben,ar", "probhat,", NULL, "complete",
  428. "pc+in(ben_probhat)+ara:2+inet(evdev)", 2, false),
  429. ENTRY("ataritt", "jp,es", NULL, NULL, "complete", "pc+jp+es:2+inet(evdev)", 2, false),
  430. ENTRY("ataritt", "es,jp", NULL, NULL, "complete", "pc+es+jp:2+inet(evdev)", 2, false),
  431. ENTRY2("evdev-modern", "olpc", "jp,es", NULL, NULL,
  432. "evdev+olpc(olpc)+aliases(qwerty)", "complete", "complete", "pc+jp+es:2+inet(evdev)", 2, false),
  433. ENTRY2("evdev-modern", "olpc", "es,jp", NULL, NULL,
  434. "evdev+olpc(olpc)+aliases(qwerty)", "complete", "complete", "pc+es+jp:2+inet(evdev)", 2, false),
  435. ENTRY("pc104", "jp,es", NULL, NULL, "complete", "pc+jp+es:2+inet(evdev)", 2, false),
  436. ENTRY("pc104", "jp,es", "xxx,yyy", NULL, "complete", "pc+jp(xxx)+es(yyy):2+inet(evdev)", 2, false),
  437. ENTRY("pc104", "latin,jp", NULL, NULL, "complete", "pc+latin+jp:2+inet(evdev)", 2, false),
  438. ENTRY("pc104", "latin,jp", "xxx,yyy", NULL, "complete", "pc+latin(xxx)+jp(yyy):2+inet(evdev)", 2, false),
  439. ENTRY2("evdev-modern", "pc104", "gb,de", ",neo", NULL,
  440. "evdev+aliases(qwerty)", "complete",
  441. "complete+caps(caps_lock):2+misc(assign_shift_left_action):2+level5(level5_lock):2",
  442. "pc+gb+de(neo):2+inet(evdev)", 2, false),
  443. ENTRY("pc104", "ca,br", NULL, "misc:typo,misc:apl", "complete",
  444. "pc+ca+br:2+inet(evdev)+apl(level3):1+apl(level3):2+typo(base):1+typo(base):2", 2, false),
  445. };
  446. #undef ENTRY
  447. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  448. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  449. assert(test_rules(ctx, &tests[k]));
  450. }
  451. }
  452. static void
  453. test_extended_layout_indices(struct xkb_context *ctx)
  454. {
  455. char layouts[(2 + MAX_LAYOUT_INDEX_STR_LENGTH) * XKB_MAX_GROUPS] = { 0 };
  456. size_t count = 0;
  457. for (xkb_layout_index_t l = 0; l < XKB_MAX_GROUPS; l++) {
  458. count += snprintf(&layouts[count], sizeof(layouts) - count,
  459. "x%"PRIu32",", l + 1);
  460. }
  461. layouts[--count] = '\0';
  462. static const char symbols_prefix[] = "pc+x1+";
  463. static const char symbols_suffix[] = "+inet(evdev)";
  464. char symbols[
  465. (3 + 2 * MAX_LAYOUT_INDEX_STR_LENGTH) * XKB_MAX_GROUPS +
  466. sizeof(symbols_prefix) + sizeof(symbols_suffix)
  467. ] = { 0 };
  468. memcpy(symbols, symbols_prefix, sizeof(symbols_prefix));
  469. count = sizeof(symbols_prefix) - 1;
  470. for (xkb_layout_index_t l = 1; l < XKB_MAX_GROUPS; l++) {
  471. count += snprintf(&symbols[count], sizeof(symbols) - count,
  472. "x%"PRIu32":%"PRIu32"+", l + 1, l + 1);
  473. }
  474. memcpy(symbols + count - 1, symbols_suffix, sizeof(symbols_suffix));
  475. const struct test_data test = {
  476. .rules = "evdev-modern",
  477. .model = "pc104",
  478. .layout = layouts, .variant = "", .options = "",
  479. .keycodes = "evdev+aliases(qwerty)",
  480. .compat = "complete",
  481. .types = "complete",
  482. .symbols = symbols,
  483. .geometry = "pc(pc104)",
  484. .explicit_layouts = XKB_MAX_GROUPS
  485. };
  486. assert(test_rules(ctx, &test));
  487. }
  488. static void
  489. test_all_qualifier(struct xkb_context *ctx,
  490. const char *too_much_layouts, const char *too_much_symbols)
  491. {
  492. const struct test_data tests[] = {
  493. /* Test :all qualifier without special indices, with option */
  494. {
  495. .rules = "all_qualifier",
  496. .model = "my_model",
  497. .layout = "layout_a,layout_b,layout_a,layout_b,layout_c",
  498. .variant = "",
  499. .options = "my_option",
  500. .keycodes = "my_keycodes", .types = "my_types",
  501. .compat = "my_compat",
  502. .symbols = "symbols_a:1+symbols_b:2+symbols_a:3+symbols_b:4+symbols_c:5"
  503. "+extra_option:1+extra_option:2+extra_option:3+extra_option:4"
  504. "+extra_option:5",
  505. .explicit_layouts = 5,
  506. },
  507. /* Test :all qualifier without special indices, base for all layout */
  508. {
  509. .rules = "all_qualifier",
  510. .model = "my_model",
  511. .layout = "layout_x,layout_a,layout_b,layout_c,layout_d",
  512. .variant = "",
  513. .options = "",
  514. .keycodes = "my_keycodes", .types = "my_types",
  515. .compat = "my_compat",
  516. .symbols = "base:1+base:2+base:3+base:4+base:5"
  517. "+symbols_a:2+symbols_b:3+default_symbols:4+default_symbols:5",
  518. .explicit_layouts = 5,
  519. },
  520. /* Test :all qualifier without special indices, with option, too much layouts */
  521. {
  522. .rules = "all_qualifier-limit",
  523. .model = "my_model",
  524. .layout = too_much_layouts,
  525. .variant = "",
  526. .options = "",
  527. .keycodes = "default_keycodes", .types = "default_types",
  528. .compat = "default_compat",
  529. .symbols = too_much_symbols,
  530. .explicit_layouts = XKB_MAX_GROUPS,
  531. },
  532. /* Test :all qualifier with special indices */
  533. {
  534. .rules = "all_qualifier",
  535. .model = "my_model",
  536. .layout = "layout_a,layout_b,layout_a,layout_b,layout_c",
  537. .variant = "extra1,,,,",
  538. .options = "my_option",
  539. .keycodes = "my_keycodes", .types = "my_types",
  540. .compat = "my_compat",
  541. .symbols = "symbols_a:1+symbols_b:2+symbols_a:3+symbols_b:4+symbols_c:5"
  542. "+extra_symbols:1+extra_symbols:2+extra_symbols:3+extra_symbols:4+extra_symbols:5"
  543. "+extra_option:1+extra_option:2+extra_option:3+extra_option:4+extra_option:5",
  544. .explicit_layouts = 5,
  545. },
  546. /* Test :all qualifier with special indices
  547. * It uses :all combined with layout[any], which is valid but
  548. * :%i was probably the intended qualified, so raises warning */
  549. {
  550. .rules = "all_qualifier",
  551. .model = "my_model",
  552. .layout = "layout_a,layout_b,layout_a,layout_b,layout_c",
  553. .variant = "extra2,,extra3,,",
  554. .options = "my_option",
  555. .keycodes = "my_keycodes", .types = "my_types",
  556. .compat = "my_compat",
  557. .symbols = "symbols_a:1+symbols_b:2+symbols_a:3+symbols_b:4+symbols_c:5"
  558. "+extra_symbols1:1+extra_symbols2:1+extra_symbols2:2+extra_symbols2:3+extra_symbols2:4+extra_symbols2:5"
  559. "+extra_symbols2:1+extra_symbols2:2+extra_symbols2:3+extra_symbols2:4+extra_symbols2:5"
  560. "+extra_symbols1:3"
  561. "+extra_option:1"
  562. "+extra_option:2+extra_option:3+extra_option:4+extra_option:5",
  563. .explicit_layouts = 5,
  564. }
  565. };
  566. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  567. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  568. assert(test_rules(ctx, &tests[k]));
  569. }
  570. }
  571. static void
  572. test_layout_specific_options(struct xkb_context *ctx)
  573. {
  574. const struct test_data tests[] = {
  575. /*
  576. * 1 layout, no layout index
  577. */
  578. {
  579. .rules = "layout-specific-options",
  580. .model = "pc104", .layout = "l1", .variant = NULL,
  581. .options = "opt1,opt2,opt3,opt4,opt5,opt6,opt7",
  582. .keycodes = "evdev", .compat = "complete", .types = "complete",
  583. .symbols = "pc+l1:1+s1:1+s3:1+s7",
  584. .explicit_layouts = 1
  585. },
  586. {
  587. .rules = "layout-specific-options",
  588. .model = "pc104", .layout = "l2", .variant = NULL,
  589. .options = "opt1,opt2,opt3,opt4,opt5,opt6,opt7",
  590. .keycodes = "evdev", .compat = "complete", .types = "complete",
  591. .symbols = "pc+l2:1+s1:1+s2:1+s3:1+s4:1+s7",
  592. .explicit_layouts = 1
  593. },
  594. /*
  595. * 1 layout, invalid layout index
  596. */
  597. {
  598. .rules = "layout-specific-options",
  599. .model = "pc104", .layout = "l1", .variant = NULL,
  600. .options = "opt1!,opt2!1x,opt3!x,opt4!x1,opt5!!,opt6!+,opt7!|",
  601. .keycodes = "evdev", .compat = "complete", .types = "complete",
  602. .symbols = "pc+l1:1+s1:1+s3:1+s7",
  603. .explicit_layouts = 1
  604. },
  605. /*
  606. * 1 layout, matching layout index
  607. */
  608. {
  609. .rules = "layout-specific-options",
  610. .model = "pc104", .layout = "l1", .variant = NULL,
  611. .options = "opt1!1,opt2!1,opt3!1,opt4!1,opt5!1,opt6!1,opt7!1",
  612. .keycodes = "evdev", .compat = "complete", .types = "complete",
  613. .symbols = "pc+l1:1+s1:1+s3:1",
  614. .explicit_layouts = 1
  615. },
  616. {
  617. .rules = "layout-specific-options",
  618. .model = "pc104", .layout = "l2", .variant = NULL,
  619. .options = "opt1!1,opt2!1,opt3!1,opt4!1,opt5!1,opt6!1,opt7!1",
  620. .keycodes = "evdev", .compat = "complete", .types = "complete",
  621. .symbols = "pc+l2:1+s1:1+s2:1+s3:1+s4:1",
  622. .explicit_layouts = 1
  623. },
  624. /*
  625. * 1 layout, non-matching layout index
  626. */
  627. {
  628. .rules = "layout-specific-options",
  629. .model = "pc104", .layout = "l1", .variant = NULL,
  630. .options = "opt1!2,opt2!2,opt3!2,opt4!2,opt5!2,opt6!2,opt7!2",
  631. .keycodes = "evdev", .compat = "complete", .types = "complete",
  632. .symbols = "pc+l1:1",
  633. .explicit_layouts = 1
  634. },
  635. {
  636. .rules = "layout-specific-options",
  637. .model = "pc104", .layout = "l2", .variant = NULL,
  638. .options = "opt1!2,opt2!2,opt3!2,opt4!2,opt5!2,opt6!2,opt7!2",
  639. .keycodes = "evdev", .compat = "complete", .types = "complete",
  640. .symbols = "pc+l2:1",
  641. .explicit_layouts = 1
  642. },
  643. /*
  644. * 2 layouts: no specifier
  645. */
  646. {
  647. .rules = "layout-specific-options",
  648. .model = "pc104", .layout = "l1,l2", .variant = NULL,
  649. .options = "opt1,opt2,opt3,opt4,opt5,opt6,opt7",
  650. .keycodes = "evdev", .compat = "complete", .types = "complete",
  651. .symbols = "pc+l1:1+l2:2+s1:1+s3:1+s3:2+s4:2+s5:1+s6:2+s7",
  652. .explicit_layouts = 2
  653. },
  654. /* 2 layout: non-matching index */
  655. {
  656. .rules = "layout-specific-options",
  657. .model = "pc104", .layout = "l1,l2", .variant = NULL,
  658. .options = "opt1!3,opt2!3,opt3!3,opt4!3,opt5!3,opt6!3,opt7!3",
  659. .keycodes = "evdev", .compat = "complete", .types = "complete",
  660. .symbols = "pc+l1:1+l2:2",
  661. .explicit_layouts = 2
  662. },
  663. /*
  664. * 2 layouts: specify only 1 layout for each option
  665. */
  666. {
  667. .rules = "layout-specific-options",
  668. .model = "pc104", .layout = "l1,l2", .variant = NULL,
  669. .options = "opt1!1,opt2!1,opt3!1,opt4!1,opt5!1,opt6!1,opt7!1",
  670. .keycodes = "evdev", .compat = "complete", .types = "complete",
  671. .symbols = "pc+l1:1+l2:2+s1:1+s3:1+s5:1",
  672. .explicit_layouts = 2
  673. },
  674. {
  675. .rules = "layout-specific-options",
  676. .model = "pc104", .layout = "l1,l2", .variant = NULL,
  677. .options = "opt1!2,opt2!2,opt3!2,opt4!2,opt5!2,opt6!2,opt7!2",
  678. .keycodes = "evdev", .compat = "complete", .types = "complete",
  679. .symbols = "pc+l1:1+l2:2+s3:2+s4:2+s6:2",
  680. .explicit_layouts = 2
  681. },
  682. /*
  683. * 2 layouts: specify 2 layouts for each option
  684. */
  685. {
  686. .rules = "layout-specific-options",
  687. .model = "pc104", .layout = "l1,l2", .variant = NULL,
  688. .options = "opt1!1,opt1!2,opt2!1,opt2!2,opt3!1,opt3!2,"
  689. "opt4!1,opt4!2,opt5!1,opt5!2,opt6!1,opt6!2,opt7!1,opt7!2",
  690. .keycodes = "evdev", .compat = "complete", .types = "complete",
  691. .symbols = "pc+l1:1+l2:2+s1:1+s3:1+s3:2+s4:2+s5:1+s6:2",
  692. .explicit_layouts = 2
  693. },
  694. };
  695. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  696. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  697. assert(test_rules(ctx, &tests[k]));
  698. }
  699. }
  700. static void
  701. test_partial_rules(struct xkb_context *ctx)
  702. {
  703. /*
  704. * 1 include path
  705. */
  706. const struct test_data tests_1[] = {
  707. /* Does not trigger partial rules */
  708. {
  709. .rules = "partial",
  710. .model = "pc104", .layout = "l1", .variant = NULL,
  711. .options = NULL,
  712. .keycodes = "evdev", .compat = "complete", .types = "complete",
  713. .symbols = "pc+l1:1",
  714. .explicit_layouts = 1
  715. },
  716. /* Does trigger pre-partial rules */
  717. {
  718. .rules = "partial",
  719. .model = "m1", .layout = "l1", .variant = NULL,
  720. .options = NULL,
  721. .keycodes = "m1", .compat = "complete", .types = "complete",
  722. .symbols = "m1+l1:1",
  723. .explicit_layouts = 1
  724. },
  725. /* Does trigger post-partial rules */
  726. {
  727. .rules = "partial",
  728. .model = "pc104", .layout = "l1,l2", .variant = NULL,
  729. .options = "opt:a!1",
  730. .keycodes = "evdev", .compat = "complete", .types = "complete",
  731. .symbols = "pc+l1:1+l2:2+extra:2+a:1",
  732. .explicit_layouts = 2
  733. },
  734. /* Does trigger both pre- and post-partial rules */
  735. {
  736. .rules = "partial",
  737. .model = "m1", .layout = "l1,l2", .variant = NULL,
  738. .options = "opt:a!1",
  739. .keycodes = "m1", .compat = "complete", .types = "complete",
  740. .symbols = "m1+l1:1+l2:2+extra:2+a:1",
  741. .explicit_layouts = 2
  742. },
  743. };
  744. for (unsigned int k = 0; k < ARRAY_SIZE(tests_1); k++) {
  745. fprintf(stderr, "------\n*** %s: #%u (1 include path) ***\n", __func__, k);
  746. assert(test_rules(ctx, &tests_1[k]));
  747. }
  748. /*
  749. * 2 include paths
  750. */
  751. ctx = test_get_context(CONTEXT_NO_FLAG);
  752. assert(ctx);
  753. char *extra = test_get_path("extra");
  754. assert(extra);
  755. xkb_context_include_path_append(ctx, extra);
  756. free(extra);
  757. const struct test_data tests_2[] = {
  758. /* Does not trigger partial rules */
  759. {
  760. .rules = "partial",
  761. .model = "pc104", .layout = "l1", .variant = NULL,
  762. .options = NULL,
  763. .keycodes = "evdev", .compat = "complete", .types = "complete",
  764. .symbols = "pc+l1:1",
  765. .explicit_layouts = 1
  766. },
  767. /* Does trigger pre-partial rules */
  768. {
  769. .rules = "partial",
  770. .model = "m1", .layout = "l1", .variant = NULL,
  771. .options = NULL,
  772. .keycodes = "m1+m1-ter", .compat = "complete", .types = "complete",
  773. .symbols = "m1+l1:1",
  774. .explicit_layouts = 1
  775. },
  776. /* Does trigger post-partial rules */
  777. {
  778. .rules = "partial",
  779. .model = "pc104", .layout = "l1,l2", .variant = NULL,
  780. .options = "opt:a!1",
  781. .keycodes = "evdev", .compat = "complete", .types = "complete",
  782. .symbols = "pc+l1:1+l2:2+extra:2+a:1+extra-bis:2+a-bis:1",
  783. .explicit_layouts = 2
  784. },
  785. /* Does trigger both pre- and post-partial rules */
  786. {
  787. .rules = "partial",
  788. .model = "m1", .layout = "l1,l2", .variant = NULL,
  789. .options = "opt:a!1",
  790. .keycodes = "m1+m1-ter", .compat = "complete", .types = "complete",
  791. .symbols = "m1+l1:1+l2:2+extra:2+a:1+extra-bis:2+a-bis:1",
  792. .explicit_layouts = 2
  793. },
  794. };
  795. for (unsigned int k = 0; k < ARRAY_SIZE(tests_2); k++) {
  796. fprintf(stderr, "------\n*** %s: #%u (2 include paths) ***\n", __func__, k);
  797. assert(test_rules(ctx, &tests_2[k]));
  798. }
  799. xkb_context_unref(ctx);
  800. }
  801. int
  802. main(int argc, char *argv[])
  803. {
  804. struct xkb_context *ctx;
  805. test_init();
  806. ctx = test_get_context(CONTEXT_NO_FLAG);
  807. assert(ctx);
  808. test_encodings(ctx);
  809. test_strict_decimal_groups(ctx);
  810. test_simple(ctx);
  811. test_wild_card(ctx);
  812. test_extended_wilcards(ctx);
  813. /* Prepare data with too much layouts */
  814. char too_much_layouts[(2 + MAX_LAYOUT_INDEX_STR_LENGTH) * (XKB_MAX_GROUPS + 1)] = { 0 };
  815. char too_much_symbols[(3 + MAX_LAYOUT_INDEX_STR_LENGTH) * XKB_MAX_GROUPS] = { 0 };
  816. size_t i = 0;
  817. for (xkb_layout_index_t l = 0; l <= XKB_MAX_GROUPS; l++) {
  818. i += snprintf(&too_much_layouts[i], sizeof(too_much_layouts) - i,
  819. "x%"PRIu32",", l + 1);
  820. }
  821. too_much_layouts[--i] = '\0';
  822. i = 0;
  823. for (xkb_layout_index_t l = 0; l < XKB_MAX_GROUPS; l++) {
  824. i += snprintf(&too_much_symbols[i], sizeof(too_much_symbols) - i,
  825. "x:%"PRIu32"+", l + 1);
  826. }
  827. too_much_symbols[--i] = '\0';
  828. test_layout_index_ranges(ctx, too_much_layouts, too_much_symbols);
  829. test_extended_layout_indices(ctx);
  830. test_all_qualifier(ctx, too_much_layouts, too_much_symbols);
  831. test_layout_specific_options(ctx);
  832. test_partial_rules(ctx);
  833. xkb_context_unref(ctx);
  834. return EXIT_SUCCESS;
  835. }