buffercomp.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536
  1. /*
  2. * Copyright © 2009 Dan Nicholson
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #include "test-config.h"
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include "xkbcommon/xkbcommon.h"
  13. #include "darray.h"
  14. #include "keymap.h"
  15. #include "log.h"
  16. #include "messages-codes.h"
  17. #include "src/keysym.h"
  18. #include "test/keysym.h"
  19. #include "test.h"
  20. #include "utils.h"
  21. #include "utils-random.h"
  22. #define GOLDEN_TESTS_OUTPUTS "keymaps/"
  23. struct keymap_test_data {
  24. /** Keymap string to compile */
  25. const char * const keymap;
  26. /** Resulting file *path* to reference serialization,
  27. * or NULL if the keymap string should not compile. */
  28. const char * const expected;
  29. /** Optionally skip the test */
  30. bool skip;
  31. };
  32. /* Our keymap compiler is the xkbcommon buffer compiler */
  33. static struct xkb_keymap *
  34. compile_buffer(struct xkb_context *context, enum xkb_keymap_format format,
  35. const char *buf, size_t len, void *private)
  36. {
  37. const enum xkb_keymap_compile_flags flags
  38. = (private)
  39. ? *(enum xkb_keymap_compile_flags*)private
  40. : TEST_KEYMAP_COMPILE_FLAGS;
  41. return test_compile_buffer2(context, format, flags, buf, len);
  42. }
  43. static void
  44. test_encodings(struct xkb_context *ctx)
  45. {
  46. struct xkb_keymap *keymap;
  47. /* Accept UTF-8 encoded BOM (U+FEFF) */
  48. const char utf8_with_bom[] = "\xef\xbb\xbfxkb_keymap {};";
  49. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  50. utf8_with_bom, sizeof(utf8_with_bom));
  51. assert(keymap);
  52. xkb_keymap_unref(keymap);
  53. /* Reject UTF-16LE encoded string */
  54. const char utf16_le[] = "x\0k\0b\0_\0k\0e\0y\0m\0a\0p\0 \0{\0}\0;\0";
  55. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  56. utf16_le, sizeof(utf16_le));
  57. assert(!keymap);
  58. /* Reject UTF-16LE with BOM encoded string */
  59. const char utf16_le_with_bom[] =
  60. "\xff\xfex\0k\0b\0_\0k\0e\0y\0m\0a\0p\0 \0{\0}\0;\0";
  61. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  62. utf16_le_with_bom, sizeof(utf16_le_with_bom));
  63. assert(!keymap);
  64. /* Reject UTF-16BE encoded string */
  65. const char utf16_be[] = "\0x\0k\0b\0_\0k\0e\0y\0m\0a\0p\0 \0{\0}\0;";
  66. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  67. utf16_be, sizeof(utf16_be));
  68. assert(!keymap);
  69. }
  70. struct keymap_simple_test_data {
  71. const char * const keymap;
  72. bool valid;
  73. };
  74. static void
  75. test_floats(struct xkb_context *ctx)
  76. {
  77. const struct keymap_simple_test_data tests[] = {
  78. /* Valid floats */
  79. {
  80. .keymap =
  81. "xkb_keymap {\n"
  82. " xkb_geometry {\n"
  83. " width=123.456;\n"
  84. " width=123.0;\n"
  85. " width=123.;\n"
  86. " width=01.234;\n"
  87. " width=01.0;\n"
  88. " width=01.;\n"
  89. " width=001.234;\n"
  90. " width=001.0;\n"
  91. " width=001.;\n"
  92. " };"
  93. "};",
  94. .valid = true
  95. },
  96. /* Invalid: missing integer part */
  97. {
  98. .keymap = "xkb_keymap { xkb_geometry { width=.123; }; };",
  99. .valid = false
  100. },
  101. /* Invalid: comma decimal separator */
  102. {
  103. .keymap = "xkb_keymap { xkb_geometry { width=1,23; }; };",
  104. .valid = false
  105. },
  106. /* Invalid: exponent */
  107. {
  108. .keymap = "xkb_keymap { xkb_geometry { width=1.23e2; }; };",
  109. .valid = false
  110. },
  111. };
  112. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  113. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  114. struct xkb_keymap *keymap =
  115. test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  116. tests[k].keymap, strlen(tests[k].keymap));
  117. assert(tests[k].valid ^ !keymap);
  118. xkb_keymap_unref(keymap);
  119. }
  120. }
  121. static void
  122. test_component_syntax_error(struct xkb_context *ctx)
  123. {
  124. /* The following used to trigger memory leak */
  125. const char* const keymaps[] = {
  126. "xkb_keymap {"
  127. " xkb_keycodes {};"
  128. "};"
  129. "};", /* Syntax error, keymap “complete” */
  130. "xkb_keymap {"
  131. " xkb_keycodes {};"
  132. " xkb_types {};"
  133. " xkb_compat {};"
  134. " xkb_symbols {};"
  135. "};"
  136. "};"/* Syntax error, keymap complete */,
  137. "xkb_keymap {"
  138. " xkb_keycodes {};"
  139. "}" /* Syntax error, incomplete map */
  140. " xkb_types {};"
  141. " xkb_compat {};"
  142. " xkb_symbols {};"
  143. "};",
  144. "xkb_keymap {"
  145. " xkb_keycodes {};"
  146. ";" /* Syntax error, incomplete map */
  147. " xkb_types {};"
  148. " xkb_compat {};"
  149. " xkb_symbols {};"
  150. "};",
  151. /*
  152. * Syntax error, multiple keymaps
  153. *
  154. * If we allow multiple keymaps per file, then do re-enable the warning
  155. * in `parser.y` about implicit default section for *all* file types.
  156. */
  157. "xkb_keymap \"1\" {};\nxkb_keymap \"2\" {};",
  158. };
  159. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  160. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  161. const struct xkb_keymap *keymap =
  162. test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  163. keymaps[k], strlen(keymaps[k]));
  164. assert(!keymap);
  165. }
  166. }
  167. /** Test that any component is optional and can be empty */
  168. static void
  169. test_optional_components(struct xkb_context *ctx, bool update_output_files)
  170. {
  171. const struct keymap_test_data keymaps[] = {
  172. /*
  173. * Optional or empty
  174. */
  175. {
  176. .keymap = "xkb_keymap {};",
  177. .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
  178. },
  179. {
  180. .keymap = "xkb_keymap { xkb_keycodes {}; };",
  181. .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
  182. },
  183. {
  184. .keymap = "xkb_keymap { xkb_types {}; };",
  185. .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
  186. },
  187. {
  188. .keymap = "xkb_keymap { xkb_compat {}; };",
  189. .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
  190. },
  191. {
  192. .keymap = "xkb_keymap { xkb_symbols {}; };",
  193. .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
  194. },
  195. /*
  196. * Some content, to check we handle missing data correctly
  197. */
  198. {
  199. /* Indicator not defined in keycodes */
  200. .keymap =
  201. "xkb_keymap {\n"
  202. " xkb_compat { indicator \"XXX\" { modifiers=Lock; }; };\n"
  203. "};",
  204. .expected = GOLDEN_TESTS_OUTPUTS "optional-components-no-real-led.xkb"
  205. },
  206. {
  207. /* Key not defined */
  208. .keymap =
  209. "xkb_keymap {\n"
  210. " xkb_symbols { key <> { [a] }; };\n"
  211. "};",
  212. .expected = GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"
  213. },
  214. {
  215. /* Key type not defined */
  216. .keymap =
  217. "xkb_keymap {\n"
  218. " xkb_keycodes { <> = 1; };"
  219. " xkb_symbols { key <> { [a], type=\"XXX\" }; };\n"
  220. "};",
  221. .expected = GOLDEN_TESTS_OUTPUTS "optional-components-basic.xkb"
  222. },
  223. {
  224. /* Virtual modifier not defined */
  225. .keymap =
  226. "xkb_keymap {\n"
  227. " xkb_keycodes { <> = 1; };"
  228. " xkb_symbols { key <> { vmods=XXX, [a] }; };\n"
  229. "};",
  230. .expected = NULL
  231. },
  232. };
  233. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  234. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  235. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  236. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  237. compile_buffer, NULL, __func__,
  238. keymaps[k].keymap, strlen(keymaps[k].keymap),
  239. keymaps[k].expected, update_output_files));
  240. }
  241. }
  242. static void
  243. test_section_flags(void)
  244. {
  245. static const char deprecated[] =
  246. "warning: [XKB-021] (input string):4:29: deprecated section: \"(unnamed map)\"\n";
  247. static const char unknown_error[] =
  248. "error: [XKB-917] (input string):2:3: Unknown section flag \"foo\"\n"
  249. "error: [XKB-822] Failed to parse input xkb string\n";
  250. static const char unknown_warning[] =
  251. "warning: [XKB-917] (input string):2:3: Unknown section flag \"foo\"; ignored\n"
  252. "warning: [XKB-917] (input string):2:7: Unknown section flag \"bar\"; ignored\n";
  253. static const struct {
  254. const char* keymap;
  255. struct errors {
  256. struct result {
  257. const char* log;
  258. bool error;
  259. } strict;
  260. struct result lax;
  261. } v1;
  262. struct errors v2;
  263. } tests[] = {
  264. /* Known flags */
  265. {
  266. .keymap =
  267. "xkb_keymap {\n"
  268. " default partial hidden alphanumeric_keys \n"
  269. " modifier_keys keypad_keys function_keys alternate_group \n"
  270. " deprecated xkb_keycodes {};\n"
  271. "};",
  272. .v1 = {
  273. .strict = {
  274. .error = false,
  275. .log = deprecated
  276. },
  277. .lax = {
  278. .error = false,
  279. .log = deprecated
  280. },
  281. },
  282. .v2 = {
  283. .strict = {
  284. .error = false,
  285. .log = deprecated
  286. },
  287. .lax = {
  288. .error = false,
  289. .log = deprecated
  290. },
  291. },
  292. },
  293. /* Unknown flags */
  294. {
  295. .keymap =
  296. "xkb_keymap {\n"
  297. " foo bar xkb_keycodes {};\n"
  298. "};",
  299. .v1 = {
  300. .strict = {
  301. .error = true,
  302. .log = unknown_error
  303. },
  304. .lax = {
  305. .error = true,
  306. .log = unknown_error
  307. },
  308. },
  309. .v2 = {
  310. .strict = {
  311. .error = true,
  312. .log = unknown_error
  313. },
  314. .lax = {
  315. .error = false,
  316. .log = unknown_warning
  317. },
  318. },
  319. }
  320. };
  321. struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
  322. assert(ctx);
  323. darray_char log_string;
  324. darray_init(log_string);
  325. darray_append_lit(log_string, "");
  326. xkb_context_set_user_data(ctx, &log_string);
  327. xkb_context_set_log_fn(ctx, log_fn);
  328. xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_WARNING);
  329. xkb_context_set_log_verbosity(ctx, XKB_LOG_VERBOSITY_MINIMAL);
  330. for (unsigned int t = 0; t < ARRAY_SIZE(tests); t++) {
  331. const struct {
  332. const char* log;
  333. enum xkb_keymap_format format;
  334. enum xkb_keymap_compile_flags flags;
  335. bool error;
  336. } configs[] = {
  337. {
  338. .format = XKB_KEYMAP_FORMAT_TEXT_V1,
  339. .flags = TEST_KEYMAP_COMPILE_FLAGS & ~XKB_KEYMAP_COMPILE_STRICT_MODE,
  340. .error = tests[t].v1.lax.error,
  341. .log = tests[t].v1.lax.log
  342. },
  343. {
  344. .format = XKB_KEYMAP_FORMAT_TEXT_V1,
  345. .flags = TEST_KEYMAP_COMPILE_FLAGS | XKB_KEYMAP_COMPILE_STRICT_MODE,
  346. .error = tests[t].v1.strict.error,
  347. .log = tests[t].v1.strict.log
  348. },
  349. {
  350. .format = XKB_KEYMAP_FORMAT_TEXT_V2,
  351. .flags = TEST_KEYMAP_COMPILE_FLAGS & ~XKB_KEYMAP_COMPILE_STRICT_MODE,
  352. .error = tests[t].v2.lax.error,
  353. .log = tests[t].v2.lax.log
  354. },
  355. {
  356. .format = XKB_KEYMAP_FORMAT_TEXT_V2,
  357. .flags = TEST_KEYMAP_COMPILE_FLAGS | XKB_KEYMAP_COMPILE_STRICT_MODE,
  358. .error = tests[t].v2.strict.error,
  359. .log = tests[t].v2.strict.log
  360. },
  361. };
  362. for (unsigned int c = 0; c < ARRAY_SIZE(configs); c++) {
  363. fprintf(stderr, "------\n*** %s: #%u (format: %d, strict: %d) ***\n",
  364. __func__, t, configs[c].format, !!configs[c].flags);
  365. assert(test_compile_output(
  366. ctx, configs[c].format,
  367. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  368. compile_buffer, (void *)&configs[c].flags, __func__,
  369. tests[t].keymap, strlen(tests[t].keymap),
  370. (configs[c].error
  371. ? NULL
  372. : GOLDEN_TESTS_OUTPUTS "optional-components-none.xkb"),
  373. false
  374. ));
  375. assert_printf(streq_not_null(darray_items(log_string), configs[c].log),
  376. "Expected:\n%s\nGot:\n%s\n",
  377. configs[c].log, darray_items(log_string));
  378. darray_size(log_string) = 0;
  379. }
  380. }
  381. xkb_context_unref(ctx);
  382. darray_free(log_string);
  383. }
  384. static void
  385. test_bidi_chars(struct xkb_context *ctx, bool update_output_files)
  386. {
  387. const struct keymap_test_data keymaps[] = {
  388. /* Invalid: first char must be ASCII */
  389. {
  390. .keymap = u8"\u200Exkb_keymap {};",
  391. .expected = NULL
  392. },
  393. {
  394. .keymap = u8"\u200Fxkb_keymap {};",
  395. .expected = NULL
  396. },
  397. /* Valid */
  398. {
  399. .keymap =
  400. u8" \u200Fxkb_keymap\u200E\u200F\n\u200E{ "
  401. u8"\u200Exkb_keycodes \u200F{ "
  402. u8"<>\u200E= \u200F1\u200E;\u200F"
  403. u8"}\u200E ;"
  404. u8"};\u200E",
  405. .expected = GOLDEN_TESTS_OUTPUTS "bidi.xkb"
  406. }
  407. };
  408. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  409. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  410. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  411. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  412. compile_buffer, NULL, __func__,
  413. keymaps[k].keymap, strlen(keymaps[k].keymap),
  414. keymaps[k].expected, update_output_files));
  415. }
  416. }
  417. static void
  418. test_recursive_includes(struct xkb_context *ctx)
  419. {
  420. struct xkb_keymap *keymap;
  421. const char* const keymaps[] = {
  422. /* Recursive keycodes */
  423. "Keycodes: recursive",
  424. "xkb_keymap {"
  425. " xkb_keycodes { include \"evdev+recursive\" };"
  426. " xkb_types { include \"complete\" };"
  427. " xkb_compat { include \"complete\" };"
  428. " xkb_symbols { include \"pc\" };"
  429. "};",
  430. "Keycodes: recursive(bar)",
  431. "xkb_keymap {"
  432. " xkb_keycodes { include \"evdev+recursive(bar)\" };"
  433. " xkb_types { include \"complete\" };"
  434. " xkb_compat { include \"complete\" };"
  435. " xkb_symbols { include \"pc\" };"
  436. "};",
  437. /* Recursive key types */
  438. "Key types: recursive",
  439. "xkb_keymap {"
  440. " xkb_keycodes { include \"evdev\" };"
  441. " xkb_types { include \"recursive\" };"
  442. " xkb_compat { include \"complete\" };"
  443. " xkb_symbols { include \"pc\" };"
  444. "};",
  445. "Key types: recursive(bar)",
  446. "xkb_keymap {"
  447. " xkb_keycodes { include \"evdev\" };"
  448. " xkb_types { include \"recursive(bar)\" };"
  449. " xkb_compat { include \"complete\" };"
  450. " xkb_symbols { include \"pc\" };"
  451. "};",
  452. /* Recursive compat */
  453. "Compat: recursive",
  454. "xkb_keymap {"
  455. " xkb_keycodes { include \"evdev\" };"
  456. " xkb_types { include \"recursive\" };"
  457. " xkb_compat { include \"complete\" };"
  458. " xkb_symbols { include \"pc\" };"
  459. "};",
  460. "Compat: recursive(bar)",
  461. "xkb_keymap {"
  462. " xkb_keycodes { include \"evdev\" };"
  463. " xkb_types { include \"complete\" };"
  464. " xkb_compat { include \"recursive(bar)\" };"
  465. " xkb_symbols { include \"pc\" };"
  466. "};",
  467. /* Recursive symbols */
  468. "Symbols: recursive",
  469. "xkb_keymap {"
  470. " xkb_keycodes { include \"evdev\" };"
  471. " xkb_types { include \"complete\" };"
  472. " xkb_compat { include \"complete\" };"
  473. " xkb_symbols { include \"recursive\" };"
  474. "};",
  475. "Symbols: recursive(bar)",
  476. "xkb_keymap {"
  477. " xkb_keycodes { include \"evdev\" };"
  478. " xkb_types { include \"complete\" };"
  479. " xkb_compat { include \"complete\" };"
  480. " xkb_symbols { include \"recursive(bar)\" };"
  481. "};"
  482. };
  483. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  484. fprintf(stderr, "------\n*** %s: #%u %s ***\n", __func__, k, keymaps[k]);
  485. k++;
  486. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  487. keymaps[k], strlen(keymaps[k]));
  488. assert(!keymap);
  489. }
  490. }
  491. static void
  492. test_include_paths(struct xkb_context *ctx)
  493. {
  494. struct xkb_keymap *keymap;
  495. const char simple_str[] =
  496. "xkb_keymap {\n"
  497. " xkb_keycodes \"test\" { include \"evdev\" };\n"
  498. " xkb_types \"test\" { include \"complete\" };\n"
  499. " xkb_symbols \"test\" { include \"pc+us(basic)+"
  500. "level5\" };\n"
  501. "};";
  502. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  503. simple_str, sizeof(simple_str));
  504. char *expected =
  505. xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
  506. xkb_keymap_unref(keymap);
  507. static const char* keymaps[] = {
  508. "xkb_keymap {\n"
  509. " xkb_keycodes \"test\" { include \"%S/evdev\" };\n"
  510. " xkb_types \"test\" { include \"%S/complete\" };\n"
  511. " xkb_symbols \"test\" { include \"%S/pc+%S/us(basic)+"
  512. "%S/level5\" };\n"
  513. "};",
  514. /* Windows absolute path contains “:”, which has special meaning in XKB includes,
  515. * so skip it. */
  516. #ifndef _WIN32
  517. "xkb_keymap {\n"
  518. " xkb_keycodes \"test\" { include \"" TEST_XKB_CONFIG_ROOT "/keycodes/evdev\" };\n"
  519. " xkb_types \"test\" { include \"" TEST_XKB_CONFIG_ROOT "/types/complete\" };\n"
  520. " xkb_symbols \"test\" { include \"" TEST_XKB_CONFIG_ROOT "/symbols/pc+"
  521. TEST_XKB_CONFIG_ROOT "/symbols/us(basic)+"
  522. TEST_XKB_CONFIG_ROOT "/symbols/level5\" };\n"
  523. "};",
  524. #endif
  525. };
  526. /* Relative path, which does not work as expected, because it is
  527. * not resolved before %-expansion. */
  528. setenv("XKB_CONFIG_ROOT", "..", 1);
  529. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  530. keymaps[0], strlen(keymaps[0]));
  531. assert(!keymap);
  532. setenv("XKB_CONFIG_ROOT", TEST_XKB_CONFIG_ROOT, 1);
  533. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  534. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  535. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  536. keymaps[k], strlen(keymaps[k]));
  537. assert(keymap);
  538. char *got =
  539. xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
  540. assert_streq_not_null("test_include_paths", expected, got);
  541. free(got);
  542. xkb_keymap_unref(keymap);
  543. }
  544. free(expected);
  545. }
  546. static void
  547. test_include_default_maps(bool update_output_files)
  548. {
  549. struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
  550. assert(ctx);
  551. /* “User” config */
  552. char *include_path = test_get_path("extra");
  553. assert(include_path);
  554. assert(xkb_context_include_path_append(ctx, include_path));
  555. free(include_path);
  556. /* “System” config */
  557. include_path = test_get_path("");
  558. assert(include_path);
  559. assert(xkb_context_include_path_append(ctx, include_path));
  560. free(include_path);
  561. const struct keymap_test_data keymaps[] = {
  562. {
  563. /* Use system explicit default map */
  564. .keymap =
  565. "xkb_keymap {\n"
  566. " xkb_keycodes { <CAPS> = 66; };\n"
  567. " xkb_symbols { include \"capslock\" };\n"
  568. "};",
  569. .expected = GOLDEN_TESTS_OUTPUTS "include-capslock-system.xkb"
  570. },
  571. {
  572. /* Use custom named map */
  573. .keymap =
  574. "xkb_keymap {\n"
  575. " xkb_keycodes { <CAPS> = 66; };\n"
  576. " xkb_symbols { include \"capslock(custom)\" };\n"
  577. "};",
  578. .expected = GOLDEN_TESTS_OUTPUTS "include-capslock-custom.xkb"
  579. },
  580. {
  581. /* Use custom *explicit* default map */
  582. .keymap =
  583. "xkb_keymap {\n"
  584. " xkb_keycodes { <RALT> = 108; <LVL3> = 92; };\n"
  585. " xkb_types { include \"basic\" };\n"
  586. " xkb_symbols { include \"level3\" };\n"
  587. "};",
  588. .expected = GOLDEN_TESTS_OUTPUTS "include-level3-explicit-default.xkb"
  589. },
  590. {
  591. /* Use custom *implicit* default map */
  592. .keymap =
  593. "xkb_keymap {\n"
  594. " xkb_keycodes { <> = 1; };\n"
  595. " xkb_symbols { include \"banana\" };\n"
  596. "};",
  597. .expected = GOLDEN_TESTS_OUTPUTS "include-banana-implicit-default.xkb"
  598. },
  599. };
  600. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  601. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  602. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  603. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  604. compile_buffer, NULL, __func__,
  605. keymaps[k].keymap, strlen(keymaps[k].keymap),
  606. keymaps[k].expected, update_output_files));
  607. }
  608. xkb_context_unref(ctx);
  609. }
  610. static void
  611. test_include_group_indices(struct xkb_context *ctx, bool update_output_files)
  612. {
  613. static const struct keymap_test_data tests[] = {
  614. {
  615. .keymap =
  616. "xkb_keymap {\n"
  617. " xkb_keycodes { include \"evdev\" };\n"
  618. " xkb_types { include \"basic\" };\n"
  619. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(chr)\" };\n"
  620. "};",
  621. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  622. },
  623. /*
  624. * Next keymaps have an invalid group index,
  625. * which then default to the base one.
  626. */
  627. /* Empty group */
  628. {
  629. .keymap =
  630. "xkb_keymap {\n"
  631. " xkb_keycodes { include \"evdev\" };\n"
  632. " xkb_types { include \"basic\" };\n"
  633. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(basic)+us(chr):\" };\n"
  634. "};",
  635. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  636. },
  637. /* Spaces */
  638. {
  639. .keymap =
  640. "xkb_keymap {\n"
  641. " xkb_keycodes { include \"evdev\" };\n"
  642. " xkb_types { include \"basic\" };\n"
  643. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(basic)+us(chr): \" };\n"
  644. "};",
  645. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  646. },
  647. {
  648. .keymap =
  649. "xkb_keymap {\n"
  650. " xkb_keycodes { include \"evdev\" };\n"
  651. " xkb_types { include \"basic\" };\n"
  652. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(basic)+us(chr): 2\" };\n"
  653. "};",
  654. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  655. },
  656. {
  657. .keymap =
  658. "xkb_keymap {\n"
  659. " xkb_keycodes { include \"evdev\" };\n"
  660. " xkb_types { include \"basic\" };\n"
  661. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(basic)+us(chr):2 \" };\n"
  662. "};",
  663. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  664. },
  665. /* Group NaN */
  666. {
  667. .keymap =
  668. "xkb_keymap {\n"
  669. " xkb_keycodes { include \"evdev\" };\n"
  670. " xkb_types { include \"basic\" };\n"
  671. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(basic)+us(chr):x\" };\n"
  672. "};",
  673. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  674. },
  675. /* Group < 0 */
  676. {
  677. .keymap =
  678. "xkb_keymap {\n"
  679. " xkb_keycodes { include \"evdev\" };\n"
  680. " xkb_types { include \"basic\" };\n"
  681. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(basic)+us(chr):-1\" };\n"
  682. "};",
  683. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  684. },
  685. /* Group == 0 */
  686. {
  687. .keymap =
  688. "xkb_keymap {\n"
  689. " xkb_keycodes { include \"evdev\" };\n"
  690. " xkb_types { include \"basic\" };\n"
  691. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(basic)+us(chr):0\" };\n"
  692. "};",
  693. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  694. },
  695. /* Group > max */
  696. {
  697. .keymap =
  698. "xkb_keymap {\n"
  699. " xkb_keycodes { include \"evdev\" };\n"
  700. " xkb_types { include \"basic\" };\n"
  701. " xkb_symbols \"pc_us(chr)\" { include \"pc+us(basic)+us(chr):5\" };\n"
  702. "};",
  703. .expected = GOLDEN_TESTS_OUTPUTS "include-invalid-group-index.xkb"
  704. },
  705. };
  706. for (unsigned int t = 0; t < ARRAY_SIZE(tests); t++) {
  707. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, t);
  708. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  709. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  710. compile_buffer, NULL, __func__,
  711. tests[t].keymap, strlen(tests[t].keymap),
  712. tests[t].expected, update_output_files));
  713. }
  714. }
  715. /* Test some limits related to allocations */
  716. static void
  717. test_alloc_limits(struct xkb_context *ctx, bool update_output_files)
  718. {
  719. const struct keymap_test_data keymaps[] = {
  720. /* Keycodes */
  721. {
  722. .keymap =
  723. "xkb_keymap {\n"
  724. /* Valid & supported high keycode value */
  725. " xkb_keycodes { <> = 0xfffffffe; };\n"
  726. " xkb_symbols { key <> {[a]}; };\n"
  727. "};",
  728. .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-1.xkb"
  729. },
  730. {
  731. .keymap =
  732. "xkb_keymap {\n"
  733. " xkb_keycodes {\n"
  734. " <xxx> = 0xfffffffe;\n"
  735. " <> = 0xfffffffd;\n"
  736. " <> = 0xfffffffe;\n"
  737. " };\n"
  738. " xkb_symbols { key <> {[a]}; };\n"
  739. "};",
  740. .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-1.xkb"
  741. },
  742. {
  743. .keymap =
  744. "xkb_keymap {\n"
  745. " xkb_keycodes {\n"
  746. " <> = 7;\n"
  747. " <> = 8;\n"
  748. " <> = 0xfffffffd;\n"
  749. " <> = 0xfffffffe;\n"
  750. " };\n"
  751. " xkb_symbols { key <> {[a]}; };\n"
  752. "};",
  753. .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-1.xkb"
  754. },
  755. {
  756. .keymap =
  757. "xkb_keymap {\n"
  758. " xkb_keycodes {\n"
  759. " <> = 0xfffffffd;\n"
  760. " <> = 0xfffffffe;\n"
  761. " <> = 7;\n"
  762. " <> = 8;\n"
  763. " };\n"
  764. " xkb_symbols { key <> {[a]}; };\n"
  765. "};",
  766. .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-2.xkb"
  767. },
  768. {
  769. .keymap =
  770. "xkb_keymap {\n"
  771. " xkb_keycodes {\n"
  772. " <min> = 0xfffffffd;\n"
  773. " <max> = 0xfffffffe;\n"
  774. " };\n"
  775. " xkb_symbols {\n"
  776. " key <min> {[a]};\n"
  777. " key <max> {[b]};\n"
  778. " };\n"
  779. "};",
  780. .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-3.xkb"
  781. },
  782. {
  783. .keymap =
  784. "xkb_keymap {\n"
  785. " xkb_keycodes {\n"
  786. " <max> = 0xfffffffe;\n"
  787. " <min> = 0xfffffffd;\n"
  788. " };\n"
  789. " xkb_symbols {\n"
  790. " key <min> {[a]};\n"
  791. " key <max> {[b]};\n"
  792. " };\n"
  793. "};",
  794. .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-3.xkb"
  795. },
  796. {
  797. .keymap =
  798. "xkb_keymap {\n"
  799. " xkb_keycodes {\n"
  800. " <a> = 0xf;\n"
  801. " <d> = 0x4000;\n"
  802. " <e> = 0xfffffffe;\n"
  803. " <c> = 0x2000;\n"
  804. " <b> = 0xfff;\n"
  805. " };\n"
  806. " xkb_symbols {\n"
  807. " key <a> {[a]};\n"
  808. " key <b> {[b]};\n"
  809. " key <c> {[c]};\n"
  810. " key <d> {[d]};\n"
  811. " key <e> {[e]};\n"
  812. " };\n"
  813. "};",
  814. .expected = GOLDEN_TESTS_OUTPUTS "high-keycodes-4.xkb"
  815. },
  816. /* Key types */
  817. {
  818. .keymap =
  819. "xkb_keymap {\n"
  820. " xkb_types {\n"
  821. /* Invalid level index */
  822. " type \"X\" { map[none] = 0xfffffffe; };\n"
  823. " };\n"
  824. "};",
  825. .expected = NULL
  826. },
  827. {
  828. .keymap =
  829. "xkb_keymap {\n"
  830. " xkb_types {\n"
  831. /* Invalid level index */
  832. " type \"X\" {levelname[0xfffffffe]=\"x\";};\n"
  833. " };\n"
  834. "};",
  835. .expected = NULL
  836. }
  837. };
  838. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  839. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  840. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  841. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  842. compile_buffer, NULL, __func__,
  843. keymaps[k].keymap, strlen(keymaps[k].keymap),
  844. keymaps[k].expected, update_output_files));
  845. }
  846. }
  847. static void
  848. test_integers(struct xkb_context *ctx, bool update_output_files) {
  849. /* Using a buffer without a terminating NULL. The following is a obvious
  850. * syntax error, but it should still exit *cleanly*, not as previously where
  851. * the use of strtoll would trigger a memory violation. */
  852. const char not_null_terminated[] = { '1' };
  853. assert(!test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  854. not_null_terminated, sizeof(not_null_terminated)));
  855. const bool skipOverflow = sizeof(int64_t) >= sizeof (long long);
  856. if (skipOverflow) {
  857. fprintf(stderr,
  858. "[WARNING] %s: cannot use checked arithmetic\n", __func__);
  859. }
  860. const struct keymap_test_data keymaps[] = {
  861. {
  862. .keymap =
  863. "xkb_keymap {\n"
  864. " xkb_keycodes {\n"
  865. /* Out of range (expect 32 bits, got > 64 bits) */
  866. " <> = 0x10000000000000000;\n"
  867. " };\n"
  868. "};",
  869. .expected = NULL
  870. },
  871. {
  872. .keymap =
  873. "xkb_keymap {\n"
  874. " xkb_keycodes { <> = 1; };\n"
  875. " xkb_compat {\n"
  876. " };\n"
  877. " xkb_symbols {\n"
  878. " key <> {\n"
  879. /* Checked int64_t underflow */
  880. " [MovePointer(x=-0x7fffffffffffffff - 2,\n"
  881. " y=0)]\n"
  882. " };\n"
  883. " };\n"
  884. "};",
  885. .expected = GOLDEN_TESTS_OUTPUTS "integers-overflow.xkb",
  886. .skip = skipOverflow
  887. },
  888. {
  889. .keymap =
  890. "xkb_keymap {\n"
  891. " xkb_keycodes { <> = 1; };\n"
  892. " xkb_compat {\n"
  893. " };\n"
  894. " xkb_symbols {\n"
  895. " key <> {\n"
  896. /* Checked int64_t underflow */
  897. " [MovePointer(x=-0x7fffffffffffffff * 2,\n"
  898. " y=0)]\n"
  899. " };\n"
  900. " };\n"
  901. "};",
  902. .expected = GOLDEN_TESTS_OUTPUTS "integers-overflow.xkb",
  903. .skip = skipOverflow
  904. },
  905. {
  906. .keymap =
  907. "xkb_keymap {\n"
  908. " xkb_keycodes { <> = 1; };\n"
  909. " xkb_compat {\n"
  910. " };\n"
  911. " xkb_symbols {\n"
  912. " key <> {\n"
  913. " [MovePointer(x=0,\n"
  914. /* Checked int64_t overflow */
  915. " y=0x7fffffffffffffff + 1)]\n"
  916. " };\n"
  917. " };\n"
  918. "};",
  919. .expected = GOLDEN_TESTS_OUTPUTS "integers-overflow.xkb",
  920. .skip = skipOverflow
  921. },
  922. {
  923. .keymap =
  924. "xkb_keymap {\n"
  925. " xkb_keycodes { <> = 1; };\n"
  926. " xkb_compat {\n"
  927. " };\n"
  928. " xkb_symbols {\n"
  929. " key <> {\n"
  930. " [MovePointer(x=0,\n"
  931. /* Checked int64_t overflow */
  932. " y=0x7fffffffffffffff * 2)]\n"
  933. " };\n"
  934. " };\n"
  935. "};",
  936. .expected = GOLDEN_TESTS_OUTPUTS "integers-overflow.xkb",
  937. .skip = skipOverflow
  938. },
  939. {
  940. .keymap =
  941. "xkb_keymap {\n"
  942. " xkb_keycodes {\n"
  943. " <> = 1;\n"
  944. " indicator 32 = \"xxx\";\n"
  945. " };\n"
  946. " xkb_compat {\n"
  947. " group 0xffffffff = Mod5;\n"
  948. " };\n"
  949. " xkb_types {\n"
  950. " type \"ONE_LEVEL\" {};"
  951. " type \"TWO_LEVEL\" { modifiers = Shift; map[Shift] = 2;};\n"
  952. " };\n"
  953. " xkb_symbols {\n"
  954. /* Computations with 64 bit ints that then fit into 16 bits */
  955. " key <> {\n"
  956. " actions[1 + -~0x100000001 / 0x100000000]=\n"
  957. " [MovePointer(x=0x100000000 - 0xfffffffe,\n"
  958. " y=~-0x7fff * 0x30000 / 0x2ffff)],\n"
  959. /* Test (INT64_MIN + 1) and INT64_MAX */
  960. " [MovePointer(x=-9223372036854775807\n"
  961. " +9223372036854775807)],\n"
  962. /* Test +0 */
  963. " [SetGroup(group=+0), LockGroup(group=+(Last-4))],\n"
  964. " [MovePointer(x=+0, y=-(1-1))]\n"
  965. " };\n"
  966. " };\n"
  967. "};",
  968. .expected = GOLDEN_TESTS_OUTPUTS "integers.xkb"
  969. },
  970. {
  971. .keymap =
  972. "xkb_keymap {\n"
  973. " xkb_keycodes { <> = 1; };\n"
  974. " xkb_symbols {\n"
  975. /* We cannot parse INT64_MIN literal.
  976. * If we could, the following should fit into 16 bits. */
  977. " key <> {\n"
  978. " [MovePointer(x=-9223372036854775808\n"
  979. " +9223372036854775807)]\n"
  980. " };\n"
  981. " };\n"
  982. "};",
  983. .expected = NULL
  984. },
  985. {
  986. .keymap =
  987. "xkb_keymap {\n"
  988. " xkb_keycodes {\n"
  989. " <0> = 10;\n"
  990. " <1> = 11;\n"
  991. " <2> = 12;\n"
  992. " <a> = 20;\n"
  993. " <b> = 21;\n"
  994. " <c> = 22;\n"
  995. " };\n"
  996. " xkb_types {\n"
  997. " type \"FOUR_LEVEL\" {\n"
  998. " modifiers = Shift + Control;\n"
  999. " map[None] = 01;\n"
  1000. " map[Shift] = 2;\n"
  1001. " map[Control] = 3;\n"
  1002. " };\n"
  1003. " }\n;"
  1004. " xkb_compat {\n"
  1005. " interpret 0 { action = LockGroup(group=2); };\n"
  1006. " interpret 00 { action = LockGroup(group=3); };\n"
  1007. " interpret 0x0 { action = LockGroup(group=4); };\n"
  1008. " interpret 1 { action = LockGroup(group=-1); };\n"
  1009. " interpret 01 { action = LockGroup(group=1); };\n"
  1010. " interpret 2 { action = LockGroup(group=-2); };\n"
  1011. " interpret 02 { action = LockGroup(group=2); };\n"
  1012. " interpret 3 { action = LockGroup(group=-3); };\n"
  1013. " interpret 0x3 { action = LockGroup(group=3); };\n"
  1014. " };\n"
  1015. " xkb_symbols {\n"
  1016. /* Various numeric keysyms forms.
  1017. * Single digits have a special interpretation in symbols */
  1018. " key <0> { [ 0, 00, 0x0] };\n"
  1019. " key <1> { [ 1, 01, 0x1] };\n"
  1020. " key <2> { [ 2, 02, 0x2] };\n"
  1021. " key <a> { [ 3 ] };\n"
  1022. " key <b> { [ 04 ] };\n"
  1023. " key <c> { [ 0x5 ] };\n"
  1024. " modifier_map Shift { 0, 3 };\n"
  1025. " modifier_map Lock { 00, 04 };\n"
  1026. " modifier_map Control { 0x0, 0x5 };\n"
  1027. " };\n"
  1028. "};",
  1029. .expected = GOLDEN_TESTS_OUTPUTS "digits.xkb"
  1030. },
  1031. };
  1032. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  1033. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  1034. if (keymaps[k].skip) {
  1035. fprintf(stderr, "INFO: skip test\n");
  1036. continue;
  1037. }
  1038. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  1039. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  1040. compile_buffer, NULL, __func__,
  1041. keymaps[k].keymap, strlen(keymaps[k].keymap),
  1042. keymaps[k].expected, update_output_files));
  1043. }
  1044. }
  1045. static void
  1046. test_keycodes(struct xkb_context *ctx, bool update_output_files) {
  1047. const struct keymap_test_data keymaps[] = {
  1048. /*
  1049. * Ensure keycodes bound are correctly updated.
  1050. *
  1051. * Since expanding bounds is tested in the rest of the code, we test
  1052. * only shrinking here.
  1053. */
  1054. /* Single keycode */
  1055. {
  1056. .keymap =
  1057. "xkb_keymap {\n"
  1058. " xkb_keycodes {\n"
  1059. " <A> = 0;\n"
  1060. " override <A> = 1;\n"
  1061. " augment <A> = 300;\n"
  1062. " };\n"
  1063. "};",
  1064. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
  1065. },
  1066. {
  1067. .keymap =
  1068. "xkb_keymap {\n"
  1069. " xkb_keycodes {\n"
  1070. " <A> = 300;\n"
  1071. " override <A> = 1;\n"
  1072. " augment <A> = 0;\n"
  1073. " };\n"
  1074. "};",
  1075. /* NOTE: same as previous */
  1076. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
  1077. },
  1078. {
  1079. .keymap =
  1080. "xkb_keymap {\n"
  1081. " xkb_keycodes {\n"
  1082. " <A> = 0;\n"
  1083. " override <A> = 1;\n"
  1084. " override <A> = 301;\n"
  1085. " override <A> = 300;\n"
  1086. " };\n"
  1087. "};",
  1088. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-2.xkb"
  1089. },
  1090. /* Multiple keycodes to single keycode */
  1091. {
  1092. .keymap =
  1093. "xkb_keymap {\n"
  1094. " xkb_keycodes {\n"
  1095. " <A> = 300;\n"
  1096. " <B> = 1;\n"
  1097. " augment <B> = 301;\n"
  1098. " override <A> = 1;\n"
  1099. " };\n"
  1100. "};",
  1101. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
  1102. },
  1103. {
  1104. .keymap =
  1105. "xkb_keymap {\n"
  1106. " xkb_keycodes {\n"
  1107. " <A> = 0;\n"
  1108. " <B> = 1;\n"
  1109. " augment <B> = 300;\n"
  1110. " override <A> = 1;\n"
  1111. " };\n"
  1112. "};",
  1113. /* NOTE: same as previous */
  1114. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
  1115. },
  1116. /* Multiple keycodes to multiple keycodes */
  1117. {
  1118. .keymap =
  1119. "xkb_keymap {\n"
  1120. " xkb_keycodes {\n"
  1121. " <A> = 1;\n"
  1122. " <B> = 0;\n"
  1123. " override <B> = 300;\n"
  1124. " augment <A> = 0;\n"
  1125. " };\n"
  1126. "};",
  1127. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-1.xkb"
  1128. },
  1129. {
  1130. .keymap =
  1131. "xkb_keymap {\n"
  1132. " xkb_keycodes {\n"
  1133. " <A> = 301;\n"
  1134. " <B> = 300;\n"
  1135. " override <A> = 1;\n"
  1136. " augment <B> = 302;\n"
  1137. " };\n"
  1138. "};",
  1139. /* NOTE: same as previous */
  1140. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-1.xkb"
  1141. },
  1142. /* High keycodes */
  1143. {
  1144. .keymap =
  1145. "xkb_keymap {\n"
  1146. " xkb_keycodes {\n"
  1147. " <A> = 1;\n"
  1148. " include \"high\"\n"
  1149. " <B> = 2;\n"
  1150. " };\n"
  1151. "};",
  1152. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-2.xkb"
  1153. },
  1154. {
  1155. .keymap =
  1156. "xkb_keymap {\n"
  1157. " xkb_keycodes {\n"
  1158. " <A> = 0x10000;\n"
  1159. " <B> = 0x10001;\n"
  1160. " <A> = 1;\n"
  1161. " <B> = 0x10002;\n"
  1162. " };\n"
  1163. "};",
  1164. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-3.xkb"
  1165. },
  1166. /* Aliases */
  1167. {
  1168. .keymap =
  1169. "xkb_keymap {\n"
  1170. " xkb_keycodes {\n"
  1171. " <B> = 1;\n"
  1172. " <A> = 1;\n"
  1173. " alias <C> = <B>;\n" /* C target is invalid */
  1174. " };\n"
  1175. "};",
  1176. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
  1177. },
  1178. {
  1179. .keymap =
  1180. "xkb_keymap {\n"
  1181. " xkb_keycodes {\n"
  1182. " alias <C> = <B>;\n" /* C target is invalid */
  1183. " <B> = 1;\n"
  1184. " <A> = 1;\n"
  1185. " };\n"
  1186. "};",
  1187. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
  1188. },
  1189. {
  1190. .keymap =
  1191. "xkb_keymap {\n"
  1192. " xkb_keycodes {\n"
  1193. " <B> = 1;\n"
  1194. " <A> = 1;\n"
  1195. " alias <C> = <B>;\n" /* C target is valid */
  1196. " <B> = 2;\n"
  1197. " };\n"
  1198. "};",
  1199. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-1.xkb"
  1200. },
  1201. {
  1202. .keymap =
  1203. "xkb_keymap {\n"
  1204. " xkb_keycodes {\n"
  1205. " alias <C> = <B>;\n" /* C target is valid */
  1206. " <B> = 1;\n"
  1207. " <A> = 1;\n"
  1208. " <B> = 2;\n"
  1209. " };\n"
  1210. "};",
  1211. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-1.xkb"
  1212. },
  1213. {
  1214. .keymap =
  1215. "xkb_keymap {\n"
  1216. " xkb_keycodes {\n"
  1217. " alias <A> = <B>;\n"
  1218. " <A> = 1;\n" /* Override alias */
  1219. " <B> = 300;\n"
  1220. " };\n"
  1221. "};",
  1222. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-1.xkb"
  1223. },
  1224. {
  1225. .keymap =
  1226. "xkb_keymap {\n"
  1227. " xkb_keycodes {\n"
  1228. " alias <C> = <B>;\n"
  1229. " augment <C> = 3;\n" /* Do not override alias */
  1230. " <A> = 1;\n"
  1231. " <B> = 2;\n"
  1232. " };\n"
  1233. "};",
  1234. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-1.xkb"
  1235. },
  1236. {
  1237. .keymap =
  1238. "xkb_keymap {\n"
  1239. " xkb_keycodes {\n"
  1240. " <A> = 1;\n"
  1241. " <C> = 3;\n"
  1242. " alias <C> = <B>;\n" /* Override key */
  1243. " <B> = 2;\n"
  1244. " };\n"
  1245. "};",
  1246. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-1.xkb"
  1247. },
  1248. {
  1249. .keymap =
  1250. "xkb_keymap {\n"
  1251. " xkb_keycodes {\n"
  1252. " <A> = 1;\n"
  1253. " augment alias <A> = <B>;\n" /* Do not override key */
  1254. " <B> = 300;\n"
  1255. " };\n"
  1256. "};",
  1257. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-multiple-1.xkb"
  1258. },
  1259. {
  1260. .keymap =
  1261. "xkb_keymap {\n"
  1262. " xkb_keycodes {\n"
  1263. " <A> = 1;\n"
  1264. " <B> = 2;\n"
  1265. " alias <B> = <C>;\n" /* Override key, even if invalid entry */
  1266. " };\n"
  1267. "};",
  1268. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
  1269. },
  1270. {
  1271. .keymap =
  1272. "xkb_keymap {\n"
  1273. " xkb_keycodes {\n"
  1274. " <B> = 1;\n"
  1275. " alias <A> = <B>;\n"
  1276. " <A> = 1;\n" /* Override both alias and key */
  1277. " };\n"
  1278. "};",
  1279. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-bounds-single-1.xkb"
  1280. },
  1281. {
  1282. .keymap =
  1283. "xkb_keymap {\n"
  1284. " xkb_keycodes {\n"
  1285. /*
  1286. * Multiple aliases *before* names: check that the key name LUT
  1287. * is replaced with new allocation.
  1288. */
  1289. " alias <A> = <X>;\n"
  1290. " alias <B> = <X>;\n"
  1291. " alias <C> = <X>;\n"
  1292. " alias <D> = <X>;\n"
  1293. " alias <E> = <X>;\n"
  1294. " alias <F> = <X>;\n"
  1295. " alias <G> = <X>;\n"
  1296. " alias <H> = <X>;\n"
  1297. " alias <I> = <X>;\n"
  1298. " alias <J> = <X>;\n"
  1299. " alias <K> = <X>;\n"
  1300. " alias <L> = <X>;\n"
  1301. " alias <M> = <X>;\n"
  1302. " alias <N> = <X>;\n"
  1303. " alias <O> = <X>;\n"
  1304. " alias <P> = <X>;\n"
  1305. " <X> = 1;\n"
  1306. " };\n"
  1307. "};",
  1308. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-2.xkb"
  1309. },
  1310. {
  1311. .keymap =
  1312. "xkb_keymap {\n"
  1313. " xkb_keycodes {\n"
  1314. /*
  1315. * Multiple aliases *before* names: check that the key name LUT
  1316. * overwrite is done properly after the last alias.
  1317. */
  1318. " alias <A> = <1>;\n"
  1319. " alias <B> = <1>;\n"
  1320. " alias <C> = <1>;\n"
  1321. " alias <D> = <1>;\n"
  1322. " alias <E> = <1>;\n"
  1323. " <1> = 1;\n"
  1324. " <2> = 2;\n"
  1325. " <3> = 3;\n"
  1326. " <4> = 4;\n"
  1327. " <5> = 5;\n"
  1328. " <6> = 6;\n"
  1329. " <7> = 7;\n"
  1330. " <8> = 8;\n"
  1331. " <9> = 9;\n"
  1332. " <10> = 10;\n"
  1333. " <11> = 11;\n"
  1334. " <12> = 12;\n"
  1335. " };\n"
  1336. "};",
  1337. .expected = GOLDEN_TESTS_OUTPUTS "keycodes-aliases-3.xkb"
  1338. },
  1339. };
  1340. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  1341. fprintf(stderr, "------\n*** %s: key bounds #%u ***\n", __func__, k);
  1342. /*
  1343. * We use a new context because we want to check key name LUT is
  1344. * correctly implemented: it requires an empty atom table.
  1345. */
  1346. struct xkb_context * const ctx2 = test_get_context(CONTEXT_NO_FLAG);
  1347. assert(ctx2);
  1348. assert(test_compile_output(ctx2, XKB_KEYMAP_FORMAT_TEXT_V1,
  1349. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  1350. compile_buffer, NULL, __func__,
  1351. keymaps[k].keymap, strlen(keymaps[k].keymap),
  1352. keymaps[k].expected, update_output_files));
  1353. xkb_context_unref(ctx2);
  1354. }
  1355. /* Test random high keycodes do not trigger xkbcomp asserts */
  1356. static const unsigned int max_iterations = 1000;
  1357. char buffer[2048] = "";
  1358. for (unsigned int i = 0; i < max_iterations; i++) {
  1359. size_t available = sizeof(buffer);
  1360. char *buf = buffer;
  1361. int count = snprintf(buf, available,
  1362. "default xkb_keymap { xkb_keycodes {\n");
  1363. assert(count > 0 && (size_t) count < available);
  1364. available -= (size_t) count;
  1365. buf += count;
  1366. static const struct {
  1367. xkb_keycode_t min;
  1368. xkb_keycode_t max;
  1369. unsigned int max_count;
  1370. } bounds[] = {
  1371. {
  1372. .min = 10 << 3, /* Avoid issue with digits */
  1373. .max = XKB_KEYCODE_MAX_CONTIGUOUS,
  1374. .max_count = 3
  1375. },
  1376. {
  1377. .min = XKB_KEYCODE_MAX_CONTIGUOUS + 1,
  1378. .max = XKB_KEYCODE_MAX,
  1379. .max_count = 10
  1380. },
  1381. };
  1382. xkb_keycode_t keycodes[13] = {0};
  1383. unsigned int keycode_index = 0;
  1384. for (size_t b = 0; b < ARRAY_SIZE(bounds); b++) {
  1385. assert(bounds[b].min < bounds[b].max);
  1386. const unsigned int keycode_count = random() % (bounds[b].max_count + 1);
  1387. for (unsigned int k = 0; k < keycode_count; k++) {
  1388. /* Note: we do not care about keycode uniqueness */
  1389. const xkb_keycode_t kc =
  1390. bounds[b].min + (random() % (bounds[b].max - bounds[b].min + 1));
  1391. assert(keycode_index < ARRAY_SIZE(keycodes));
  1392. keycodes[keycode_index++] = kc;
  1393. count = snprintf(buf, available,
  1394. "<%"PRIu32"> = 0x%"PRIx32";\n", kc, kc);
  1395. assert(count > 0 && (size_t) count < available);
  1396. available -= (size_t) count;
  1397. buf += count;
  1398. }
  1399. }
  1400. count = snprintf(buf, available, "}; xkb_symbols {\n");
  1401. assert(count > 0 && (size_t) count < available);
  1402. available -= (size_t) count;
  1403. buf += count;
  1404. for (unsigned int k = 0; k < keycode_index; k++) {
  1405. const xkb_keycode_t kc = keycodes[k];
  1406. count = snprintf(buf, available,
  1407. "key <%"PRIu32"> { [ 0x%"PRIx32" ] };\n",
  1408. kc, (kc >> 3));
  1409. assert(count > 0 && (size_t) count < available);
  1410. available -= (size_t) count;
  1411. buf += count;
  1412. }
  1413. count = snprintf(buf, available, "}; };");
  1414. assert(count > 0 && (size_t) count < available);
  1415. struct xkb_keymap * const keymap = xkb_keymap_new_from_string(
  1416. ctx, buffer, XKB_KEYMAP_FORMAT_TEXT_V1, TEST_KEYMAP_COMPILE_FLAGS
  1417. );
  1418. assert(keymap);
  1419. for (unsigned int k = 0; k < keycode_index; k++) {
  1420. const xkb_keycode_t kc = keycodes[k];
  1421. const xkb_keysym_t *ks = NULL;
  1422. count = xkb_keymap_key_get_syms_by_level(keymap, kc, 0, 0, &ks);
  1423. assert_printf(count == 1, "%d\n", count);
  1424. const xkb_keysym_t expected = kc >> 3;
  1425. assert_printf(*ks == expected,
  1426. "<%"PRIu32"> 0x%"PRIx32" != 0x%"PRIx32"\n",
  1427. kc, *ks, expected);
  1428. }
  1429. xkb_keymap_unref(keymap);
  1430. }
  1431. /* Ensure long key names have a fallback in V1 format */
  1432. static const char long_names_1[] =
  1433. "xkb_keymap {\n"
  1434. " xkb_keycodes {\n"
  1435. " <0008> = 8;\n" /* name conflict (count = 4): prefix ‘0’ skipped */
  1436. " <!09> = 9;\n" /* no name conflict (count = 3): prefix ‘!’ available */
  1437. " <long-name-000axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = 0x000a;\n"
  1438. " <long-name-0fffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = 0x0fff;\n"
  1439. " <long-name-ffffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = 0xffff;\n"
  1440. " alias <long-alias-000axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = "
  1441. " <long-name-000axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>;\n"
  1442. " alias <long-alias-0fffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = "
  1443. " <long-name-0fffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>;\n"
  1444. " alias <long-alias-ffffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = "
  1445. " <long-name-ffffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>;\n"
  1446. " };\n"
  1447. " xkb_symbols {\n"
  1448. " key <0008> { [0x8] };\n"
  1449. " key <!09> { [0x9] };\n"
  1450. " key <long-name-000axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> {[0x000a]};\n"
  1451. " key <long-name-0fffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> {[0x0fff]};\n"
  1452. " key <long-name-ffffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> {[0xffff]};\n"
  1453. " modmap Shift { <0008>, <!09> };\n"
  1454. " modmap Lock {\n"
  1455. " <long-alias-000axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>,\n"
  1456. " <long-alias-0fffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>,\n"
  1457. " <long-alias-ffffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>\n"
  1458. " };\n"
  1459. " };\n"
  1460. "};";
  1461. static const char long_names_2[] =
  1462. "xkb_keymap {\n"
  1463. " xkb_keycodes {\n"
  1464. " <!008> = 8;\n"
  1465. " <#009> = 9;\n"
  1466. " <$010> = 10;\n"
  1467. " <%011> = 11;\n"
  1468. " <&012> = 12;\n"
  1469. " <'013> = 13;\n"
  1470. " <(014> = 14;\n"
  1471. " <)015> = 15;\n"
  1472. " <*016> = 16;\n"
  1473. " <+017> = 17;\n"
  1474. " <,018> = 18;\n"
  1475. " <-019> = 19;\n"
  1476. " <.020> = 20;\n"
  1477. " </021> = 21;\n"
  1478. " <0022> = 22;\n"
  1479. /* No prefix available: the following keys names will stay unchanged */
  1480. " <long-name-0100xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = 100;\n"
  1481. " <long-name-0101xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = 101;\n"
  1482. " alias <long-alias-0100xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = "
  1483. " <long-name-0100xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>;\n"
  1484. " alias <long-alias-0101xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> = "
  1485. " <long-name-0101xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>;\n"
  1486. " };\n"
  1487. "};";
  1488. static const struct {
  1489. const char* keymap;
  1490. const char* path;
  1491. enum xkb_keymap_format format;
  1492. } tests[] = {
  1493. {
  1494. .keymap = long_names_1,
  1495. .path = GOLDEN_TESTS_OUTPUTS "keycodes-long-names-1-v1.xkb",
  1496. .format = XKB_KEYMAP_FORMAT_TEXT_V1
  1497. },
  1498. {
  1499. .keymap = long_names_1,
  1500. .path = GOLDEN_TESTS_OUTPUTS "keycodes-long-names-1-v2.xkb",
  1501. .format = XKB_KEYMAP_FORMAT_TEXT_V2
  1502. },
  1503. {
  1504. .keymap = long_names_2,
  1505. .path = GOLDEN_TESTS_OUTPUTS "keycodes-long-names-2.xkb",
  1506. .format = XKB_KEYMAP_FORMAT_TEXT_V1
  1507. },
  1508. {
  1509. .keymap = long_names_2,
  1510. .path = GOLDEN_TESTS_OUTPUTS "keycodes-long-names-2.xkb",
  1511. .format = XKB_KEYMAP_FORMAT_TEXT_V2
  1512. },
  1513. };
  1514. for (unsigned int t = 0; t < ARRAY_SIZE(tests); t++) {
  1515. fprintf(stderr, "------\n*** %s: long key names #%u ***\n", __func__, t);
  1516. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  1517. tests[t].format,
  1518. compile_buffer, NULL, __func__,
  1519. tests[t].keymap, strlen(tests[t].keymap),
  1520. tests[t].path,
  1521. update_output_files));
  1522. }
  1523. }
  1524. static void
  1525. test_masks(struct xkb_context *ctx, bool update_output_files) {
  1526. const struct keymap_test_data keymaps[] = {
  1527. {
  1528. .keymap =
  1529. "xkb_keymap {\n"
  1530. " xkb_compat {\n"
  1531. /* Cannot be negative */
  1532. " virtual_modifiers Test1 = -1;\n"
  1533. " };\n"
  1534. "};",
  1535. .expected = NULL
  1536. },
  1537. {
  1538. .keymap =
  1539. "xkb_keymap {\n"
  1540. " xkb_compat {\n"
  1541. /* Out of range (expect 32bits) */
  1542. " virtual_modifiers Test1 = 0x100000000;\n"
  1543. " };\n"
  1544. "};",
  1545. .expected = NULL
  1546. },
  1547. {
  1548. .keymap =
  1549. "xkb_keymap {\n"
  1550. " xkb_compat {\n"
  1551. /* Out of range (expect 32bits) */
  1552. " virtual_modifiers Test1 = ~0x100000000;\n"
  1553. " };\n"
  1554. "};",
  1555. .expected = NULL
  1556. },
  1557. {
  1558. .keymap =
  1559. "xkb_keymap {\n"
  1560. " xkb_compat {\n"
  1561. /* Unsupported operator */
  1562. " virtual_modifiers Test1 = !Mod1;\n"
  1563. " };\n"
  1564. "};",
  1565. .expected = NULL
  1566. },
  1567. {
  1568. .keymap =
  1569. "xkb_keymap {\n"
  1570. " xkb_types {\n"
  1571. /* Try range */
  1572. " virtual_modifiers Test01 = 0;\n"
  1573. " virtual_modifiers Test02 = 0xffffffff;\n"
  1574. /* Try various operations on masks */
  1575. " virtual_modifiers Test11 = 0xf0 + 0x0f;\n"
  1576. " virtual_modifiers Test12 = 0xff - 0x0f;\n"
  1577. " virtual_modifiers Test13 = ~0xf;\n"
  1578. " virtual_modifiers Test14 = ~none;\n"
  1579. " virtual_modifiers Test15 = ~all;\n"
  1580. " virtual_modifiers Test16 = ~0xffffffff;\n"
  1581. " virtual_modifiers Test17 = all - ~Mod1 + Mod2;\n"
  1582. " type \"XXX\" {\n"
  1583. " modifiers = Test12;\n"
  1584. /* Masks mappings are not resolved here, so:
  1585. * map[Test12 - Mod5] <=> map[Test12] */
  1586. " map[Test12 - Mod5] = 2;\n"
  1587. " };\n"
  1588. " };\n"
  1589. "};",
  1590. .expected = GOLDEN_TESTS_OUTPUTS "masks.xkb"
  1591. },
  1592. {
  1593. .keymap =
  1594. "xkb_keymap {\n"
  1595. " xkb_keycodes { <a> = 38; };\n"
  1596. " xkb_symbols {\n"
  1597. " virtual_modifiers X = 0xf0000000;\n"
  1598. " key <a> { [ SetMods(mods = 0x00001100) ] };\n"
  1599. " };\n"
  1600. "};",
  1601. .expected = GOLDEN_TESTS_OUTPUTS "masks-extra-bits.xkb"
  1602. }
  1603. };
  1604. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  1605. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  1606. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  1607. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  1608. compile_buffer, NULL, __func__,
  1609. keymaps[k].keymap, strlen(keymaps[k].keymap),
  1610. keymaps[k].expected, update_output_files));
  1611. }
  1612. }
  1613. static void
  1614. test_interpret(struct xkb_context *ctx, bool update_output_files)
  1615. {
  1616. const struct keymap_test_data keymaps[] = {
  1617. /* Invalid: empty string */
  1618. {
  1619. .keymap =
  1620. "xkb_keymap {\n"
  1621. " xkb_compat { interpret \"\" { repeat = false; } };\n"
  1622. "};",
  1623. .expected = NULL
  1624. },
  1625. /* Invalid UTF-8 encoding */
  1626. {
  1627. .keymap =
  1628. "xkb_keymap {\n"
  1629. " xkb_compat { interpret \"\xff\" { repeat = false; }; };\n"
  1630. "};",
  1631. .expected = NULL
  1632. },
  1633. /* Invalid single Unicode code point */
  1634. {
  1635. .keymap =
  1636. "xkb_keymap {\n"
  1637. " xkb_compat { interpret \"\\u{0}\" { repeat = false; }; };\n"
  1638. "};",
  1639. .expected = NULL
  1640. },
  1641. /* Invalid multiple Unicode code points */
  1642. {
  1643. .keymap =
  1644. "xkb_keymap {\n"
  1645. " xkb_compat { interpret \"ab\" { repeat = false; }; };\n"
  1646. "};",
  1647. .expected = NULL
  1648. },
  1649. /* Valid */
  1650. {
  1651. .keymap =
  1652. u8"xkb_keymap {\n"
  1653. u8" xkb_compat {\n"
  1654. u8" interpret 0x1 { repeat = false; };\n"
  1655. u8" interpret 0xB { repeat = false; };\n"
  1656. u8" interpret Shift_L { repeat = false; };\n"
  1657. u8" interpret a { repeat = false; };\n"
  1658. u8" interpret \"ä\" { repeat = false; };\n"
  1659. u8" interpret \"✨\" { repeat = false; };\n"
  1660. u8" interpret \"🎺\" { repeat = false; };\n"
  1661. u8" };\n"
  1662. u8"};",
  1663. .expected = GOLDEN_TESTS_OUTPUTS "compat-interpret.xkb"
  1664. },
  1665. };
  1666. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  1667. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  1668. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  1669. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  1670. compile_buffer, NULL, __func__,
  1671. keymaps[k].keymap, strlen(keymaps[k].keymap),
  1672. keymaps[k].expected, update_output_files));
  1673. }
  1674. }
  1675. static void
  1676. test_group_indices_names(struct xkb_context *ctx, bool update_output_files)
  1677. {
  1678. const struct {
  1679. const char* keymap;
  1680. const char* expected_v1;
  1681. const char* expected_v2;
  1682. } keymaps[] = {
  1683. {
  1684. .keymap =
  1685. "xkb_keymap {\n"
  1686. " xkb_keycodes {\n"
  1687. " <> = 1;\n"
  1688. " indicator 1 = \"1\";\n"
  1689. " indicator 2 = \"2\";\n"
  1690. " };\n"
  1691. " xkb_compat {\n"
  1692. " interpret a { action = SetGroup(group=Group1); };\n"
  1693. " interpret b { action = SetGroup(group=-Group2); };\n"
  1694. " interpret c { action = SetGroup(group=+Group3); };\n"
  1695. " indicator \"1\" { groups = Group4; };\n"
  1696. " indicator \"2\" { groups = All-Group1; };\n"
  1697. " };\n"
  1698. " xkb_symbols {\n"
  1699. " name[Group1] = \"1\";\n"
  1700. " name[Group2] = \"2\";\n"
  1701. " name[Group3] = \"3\";\n"
  1702. " name[Group4] = \"4\";\n"
  1703. " key <> {\n"
  1704. " groupsredirect=Group4,\n"
  1705. " symbols[Group1]=[a],\n"
  1706. " symbols[Group2]=[b],\n"
  1707. " symbols[Group3]=[c],\n"
  1708. " symbols[Group4]=[d]\n"
  1709. " };\n"
  1710. " };\n"
  1711. "};",
  1712. .expected_v1 = GOLDEN_TESTS_OUTPUTS "group-index-names-1.xkb",
  1713. .expected_v2 = GOLDEN_TESTS_OUTPUTS "group-index-names-1.xkb"
  1714. },
  1715. {
  1716. .keymap =
  1717. "xkb_keymap {\n"
  1718. " xkb_keycodes {\n"
  1719. " <> = 1;\n"
  1720. " indicator 1 = \"1\";\n"
  1721. " indicator 2 = \"2\";\n"
  1722. " };\n"
  1723. " xkb_compat {\n"
  1724. " interpret a { action = SetGroup(group=Group32); };\n"
  1725. " interpret b { action = SetGroup(group=-Group32); };\n"
  1726. " interpret c { action = SetGroup(group=+Group32); };\n"
  1727. " indicator \"1\" { groups = Group32; };\n"
  1728. " indicator \"2\" { groups = All-Group32; };\n"
  1729. " };\n"
  1730. " xkb_symbols {\n"
  1731. " name[Group1] = \"1\";\n"
  1732. " name[Group4] = \"4\";\n"
  1733. " name[Group5] = \"5\";\n"
  1734. " name[Group8] = \"8\";\n"
  1735. " name[Group32] = \"32\";\n"
  1736. " key <> {\n"
  1737. " groupsredirect=Group32,\n"
  1738. " symbols[Group1]=[a],\n"
  1739. " symbols[Group4]=[b],\n"
  1740. " symbols[Group5]=[c],\n"
  1741. " symbols[Group8]=[d],\n"
  1742. " symbols[Group32]=[e]\n"
  1743. " };\n"
  1744. " };\n"
  1745. "};",
  1746. .expected_v1 = NULL,
  1747. .expected_v2 = GOLDEN_TESTS_OUTPUTS "group-index-names-2.xkb"
  1748. },
  1749. /* No names for groups above 32 */
  1750. {
  1751. .keymap =
  1752. "xkb_keymap {\n"
  1753. " xkb_compat { interpret a { action = SetGroup(group=Group33); }; };\n"
  1754. "};",
  1755. .expected_v1 = NULL,
  1756. .expected_v2 = NULL
  1757. },
  1758. {
  1759. .keymap =
  1760. "xkb_keymap {\n"
  1761. " xkb_keycodes { indicator 1 = \"1\"; };\n"
  1762. " xkb_compat { indicator \"1\" { groups = Group33; }; };\n"
  1763. "};",
  1764. .expected_v1 = NULL,
  1765. .expected_v2 = NULL
  1766. },
  1767. {
  1768. .keymap =
  1769. "xkb_keymap {\n"
  1770. " xkb_keycodes { <> = 1; };\n"
  1771. " xkb_symbols { name[Group33] = \"1\"; };\n"
  1772. "};",
  1773. .expected_v1 = NULL,
  1774. .expected_v2 = NULL
  1775. },
  1776. {
  1777. .keymap =
  1778. "xkb_keymap {\n"
  1779. " xkb_keycodes { <> = 1; };\n"
  1780. " xkb_symbols { key <> { groupsredirect=Group33 };\n"
  1781. "};",
  1782. .expected_v1 = NULL,
  1783. .expected_v2 = NULL
  1784. },
  1785. {
  1786. .keymap =
  1787. "xkb_keymap {\n"
  1788. " xkb_keycodes { <> = 1; };\n"
  1789. " xkb_symbols { key <> { symbols[Group33] = [a] };\n"
  1790. "};",
  1791. .expected_v1 = NULL,
  1792. .expected_v2 = NULL
  1793. },
  1794. /* First and Last: test default values */
  1795. {
  1796. .keymap =
  1797. "xkb_keymap {\n"
  1798. " xkb_compat {\n"
  1799. " interpret ISO_First_Group {\n"
  1800. " action= LockGroup(group=First);\n"
  1801. " };\n"
  1802. " interpret ISO_Last_Group {\n"
  1803. " action= LockGroup(group=Last);\n"
  1804. " };\n"
  1805. " indicator \"First group\" {\n"
  1806. " groups = First;\n"
  1807. " };\n"
  1808. " indicator \"Last group\" {\n"
  1809. " groups = Last;\n"
  1810. " };\n"
  1811. " };\n"
  1812. "};",
  1813. .expected_v1 = GOLDEN_TESTS_OUTPUTS "group-index-names-3.xkb",
  1814. .expected_v2 = GOLDEN_TESTS_OUTPUTS "group-index-names-3.xkb",
  1815. },
  1816. /* First and Last: test all properties */
  1817. {
  1818. .keymap =
  1819. "xkb_keymap {\n"
  1820. " xkb_keycodes { <> = 1; };\n"
  1821. " xkb_compat {\n"
  1822. " SetGroup.group = Last;\n"
  1823. " interpret ISO_First_Group {\n"
  1824. " action= LockGroup(group=First);\n"
  1825. " };\n"
  1826. " interpret ISO_Last_Group {\n"
  1827. " action= SetGroup();\n"
  1828. " };\n"
  1829. " indicator \"Later groups\" {\n"
  1830. " groups = All - First;\n"
  1831. " };\n"
  1832. " indicator \"Last group\" {\n"
  1833. " groups = Last;\n"
  1834. " };\n"
  1835. " indicator \"All but last group\" {\n"
  1836. " groups = All - Last;\n"
  1837. " };\n"
  1838. " indicator \"Y\" {\n"
  1839. " groups = Last + First;\n"
  1840. " };\n"
  1841. " };\n"
  1842. " xkb_types {\n"
  1843. " type \"ONE_LEVEL\" {};\n"
  1844. " type \"TWO_LEVEL\" {\n"
  1845. " modifiers = Shift;\n"
  1846. " map[Shift] = 2;\n"
  1847. " };\n"
  1848. " };"
  1849. " xkb_symbols {\n"
  1850. " name[first] = \"1\";\n"
  1851. " SetGroup.group = +(Last + 1);\n"
  1852. " key.groupsRedirect = Last;\n"
  1853. " key <> {\n"
  1854. " symbols[FIRST] = [1],\n"
  1855. " actions[First] = [SetGroup(group=Last), SetGroup(group=-Last)],\n"
  1856. " symbols[2] = [2],\n"
  1857. " actions[2] = [SetGroup(group=Last - First), SetGroup()],\n"
  1858. " actions[3] = [SetGroup(group=First), SetGroup(group=-First)]\n"
  1859. " };\n"
  1860. " };\n"
  1861. "};",
  1862. .expected_v1 = GOLDEN_TESTS_OUTPUTS "group-index-names-4.xkb",
  1863. .expected_v2 = GOLDEN_TESTS_OUTPUTS "group-index-names-4.xkb",
  1864. },
  1865. /* “Last” is not supported as a group index */
  1866. {
  1867. .keymap =
  1868. "xkb_keymap {\n"
  1869. " xkb_keycodes { <> = 1; };\n"
  1870. " xkb_symbols { key <> { symbols[Last] = [a] }; };\n"
  1871. "};",
  1872. .expected_v1 = NULL,
  1873. .expected_v2 = NULL
  1874. },
  1875. };
  1876. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  1877. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  1878. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  1879. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  1880. compile_buffer, NULL, __func__,
  1881. keymaps[k].keymap, strlen(keymaps[k].keymap),
  1882. keymaps[k].expected_v1, update_output_files));
  1883. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
  1884. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  1885. compile_buffer, NULL, __func__,
  1886. keymaps[k].keymap, strlen(keymaps[k].keymap),
  1887. keymaps[k].expected_v2, update_output_files));
  1888. }
  1889. }
  1890. static void
  1891. test_level_indices_names(struct xkb_context *ctx,
  1892. bool update_output_files)
  1893. {
  1894. const struct {
  1895. const char* keymap;
  1896. const char* expected;
  1897. } keymaps[] = {
  1898. {
  1899. .keymap =
  1900. "xkb_keymap {\n"
  1901. " xkb_types {\n"
  1902. " type \"X\" {\n"
  1903. " modifiers = Shift;\n"
  1904. " map[Shift] = Level2048;\n"
  1905. " level_name[Level2048] = \"x\";\n"
  1906. " };\n"
  1907. " };\n"
  1908. "};",
  1909. .expected = GOLDEN_TESTS_OUTPUTS "level-index-names.xkb"
  1910. },
  1911. /* No names above 2048 */
  1912. {
  1913. .keymap =
  1914. "xkb_keymap {\n"
  1915. " xkb_types {\n"
  1916. " type \"X\" {\n"
  1917. " modifiers = Shift;\n"
  1918. " map[Shift] = Level2049;\n"
  1919. " };\n"
  1920. " };\n"
  1921. "};",
  1922. .expected = NULL,
  1923. },
  1924. {
  1925. .keymap =
  1926. "xkb_keymap {\n"
  1927. " xkb_types {\n"
  1928. " type \"X\" {\n"
  1929. " modifiers = Shift;\n"
  1930. " level_name[Level2049] = \"x\";\n"
  1931. " };\n"
  1932. " };\n"
  1933. "};",
  1934. .expected = NULL,
  1935. },
  1936. };
  1937. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  1938. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  1939. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  1940. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  1941. compile_buffer, NULL, __func__,
  1942. keymaps[k].keymap, strlen(keymaps[k].keymap),
  1943. keymaps[k].expected, update_output_files));
  1944. }
  1945. }
  1946. /* Test various multi-{keysym,action} syntaxes */
  1947. static void
  1948. test_multi_keysyms_actions(struct xkb_context *ctx, bool update_output_files)
  1949. {
  1950. /* Macro to create keymap for failing tests */
  1951. #define make_keymap(xs) \
  1952. "xkb_keymap {\n" \
  1953. " xkb_keycodes {\n" \
  1954. " <AE01> = 10;\n" \
  1955. " };\n" \
  1956. " xkb_types { include \"basic\" };\n" \
  1957. " xkb_compat { include \"basic\" };\n" \
  1958. " xkb_symbols {\n" \
  1959. " key <AE01> { [" xs "] };\n" \
  1960. " };\n" \
  1961. "};"
  1962. #define make_keymaps_with(func, name, a, b, c, d) \
  1963. /* Test: valid keymaps */ \
  1964. { \
  1965. .keymap = \
  1966. "xkb_keymap {\n" \
  1967. " xkb_keycodes {\n" \
  1968. " <01> = 1;\n" \
  1969. " <02> = 2;\n" \
  1970. " <03> = 3;\n" \
  1971. " <04> = 4;\n" \
  1972. " <05> = 5;\n" \
  1973. " <06> = 6;\n" \
  1974. " <07> = 7;\n" \
  1975. " <08> = 8;\n" \
  1976. " <09> = 9;\n" \
  1977. " <10> = 10;\n" \
  1978. " <11> = 11;\n" \
  1979. " <12> = 12;\n" \
  1980. " <13> = 13;\n" \
  1981. " <14> = 14;\n" \
  1982. " <15> = 15;\n" \
  1983. " };\n" \
  1984. " xkb_types { include \"basic+extra\" };\n" \
  1985. " xkb_compat { include \"basic\" };\n" \
  1986. " xkb_symbols {\n" \
  1987. " key <01> { [ "a "] };\n" \
  1988. " key <02> { [ "a", "b" ] };\n" \
  1989. " key <03> { [ {} ] };\n" \
  1990. " key <04> { [ {}, "b" ] };\n" \
  1991. " key <05> { [ "a", {} ] };\n" \
  1992. " key <06> { [ {}, {} ] };\n" \
  1993. " key <07> { [ { "a" } ] };\n" \
  1994. " key <08> { [ { "a" }, { "b" } ] };\n" \
  1995. " key <09> { [ { "a", "b" } ] };\n" \
  1996. " key <10> { [ { "a", "b", "c" } ] };\n" \
  1997. " key <11> { [ "a", { "b", "c" } ] };\n" \
  1998. " key <12> { [ { "a", "b" }, "c" ] };\n" \
  1999. " key <13> { [ { "a", "b" }, { "c", "d" } ] };\n" \
  2000. " key <14> { [ { "a", "b" }, "c", { "d", "a" } ] };\n" \
  2001. " key <15> { [ { "a", "b" }, { "c", "d" }, "a" ] };\n" \
  2002. " };\n" \
  2003. "};", \
  2004. .expected = GOLDEN_TESTS_OUTPUTS "symbols-multi-" name ".xkb" \
  2005. }, \
  2006. /* Test: invalid keymaps */ \
  2007. { .keymap = func("{ {} }") , .expected = NULL }, \
  2008. { .keymap = func("{ "a", {} }") , .expected = NULL }, \
  2009. { .keymap = func("{ {}, "b" }") , .expected = NULL }, \
  2010. { .keymap = func("{ {}, {} }") , .expected = NULL }, \
  2011. { .keymap = func("{ "a", { "b" } }") , .expected = NULL }, \
  2012. { .keymap = func("{ { "a" }, "b" }") , .expected = NULL }, \
  2013. { .keymap = func("{ { "a", "b" }, "c" }") , .expected = NULL }, \
  2014. { .keymap = func("{ "a", { "b", "c" } }") , .expected = NULL }, \
  2015. { .keymap = func("{ "a", {}, "c" }") , .expected = NULL }, \
  2016. { .keymap = func("{ "a", "b", {} }") , .expected = NULL }, \
  2017. { .keymap = func("{ {}, "b", "c" }") , .expected = NULL }, \
  2018. { .keymap = func("{ { "a", "b" }, "c", "d" }") , .expected = NULL }, \
  2019. { .keymap = func("{ "a", { "b", "c" }, "d" }") , .expected = NULL }, \
  2020. { .keymap = func("{ "a", "b", { "c", "d" } }") , .expected = NULL }, \
  2021. { .keymap = func("{ { "a", "b" }, { "c", "d" } }"), .expected = NULL }
  2022. const struct keymap_test_data keymaps[] = {
  2023. make_keymaps_with(make_keymap, "keysyms",
  2024. "a", "b", "c", "d"),
  2025. make_keymaps_with(make_keymap, "actions",
  2026. "SetMods(modifiers=Control)",
  2027. "SetGroup(group=+1)",
  2028. "Private(data=\"foo\")",
  2029. "Private(data=\"bar\")"),
  2030. {
  2031. .keymap =
  2032. "xkb_keymap {\n"
  2033. " xkb_keycodes {\n"
  2034. " <10> = 10;\n"
  2035. " <11> = 11;\n"
  2036. " <12> = 12;\n"
  2037. " <13> = 13;\n"
  2038. " <14> = 14;\n"
  2039. " <15> = 15;\n"
  2040. " <16> = 16;\n"
  2041. " <17> = 17;\n"
  2042. " <18> = 18;\n"
  2043. " <19> = 19;\n"
  2044. " <20> = 20;\n"
  2045. " <21> = 21;\n"
  2046. " <22> = 22;\n"
  2047. " <23> = 23;\n"
  2048. " <30> = 30;\n"
  2049. " <31> = 31;\n"
  2050. " <32> = 32;\n"
  2051. " <33> = 33;\n"
  2052. " <34> = 34;\n"
  2053. " <35> = 35;\n"
  2054. " <36> = 36;\n"
  2055. " <37> = 37;\n"
  2056. " <38> = 38;\n"
  2057. " <39> = 39;\n"
  2058. " };\n"
  2059. " xkb_types { include \"basic+extra\" };\n"
  2060. " xkb_compat {\n"
  2061. " interpret 1 { action = {}; };\n"
  2062. " interpret 2 { action = {NoAction()}; };\n"
  2063. " interpret 3 { action = {SetMods()}; };\n"
  2064. " interpret 4 { action = {SetMods(), SetGroup(group=1)}; };\n"
  2065. " };\n"
  2066. " xkb_symbols {\n"
  2067. /* Empty keysyms */
  2068. " key <10> { [any, any ] };\n"
  2069. " key <11> { [{} , {} ] };\n"
  2070. " key <12> { [any, any ], [SetMods(modifiers=Shift)] };\n"
  2071. " key <13> { [{} , {} ], [SetMods(modifiers=Shift)] };\n"
  2072. " key <14> { [any, any ], type = \"TWO_LEVEL\" };\n"
  2073. " key <15> { [{} , {} ], type = \"TWO_LEVEL\" };\n"
  2074. " key <16> { [a, A, any] };\n"
  2075. " key <17> { [a, A, {} ] };\n"
  2076. " key <18> { [a, A, any], type = \"FOUR_LEVEL_SEMIALPHABETIC\" };\n"
  2077. " key <19> { [a, A, {} ], type = \"FOUR_LEVEL_SEMIALPHABETIC\" };\n"
  2078. " key <20> { [a, A, ae, any] };\n"
  2079. " key <21> { [a, A, ae, {} ] };\n"
  2080. " key <22> { [a, A, ae, AE, any] };\n"
  2081. " key <23> { [a, A, ae, AE, {} ] };\n"
  2082. /* Empty actions */
  2083. " key <30> { [NoAction(), NoAction() ] };\n"
  2084. " key <31> { actions=[{}, {} ] };\n"
  2085. " key <32> { [NoAction(), NoAction() ], [a] };\n"
  2086. " key <33> { actions=[{}, {} ], [a] };\n"
  2087. " key <34> { [NoAction(), NoAction() ], type = \"TWO_LEVEL\" };\n"
  2088. " key <35> { actions=[{}, {} ], type = \"TWO_LEVEL\" };\n"
  2089. " key <38> { [NoAction(), NoAction() ], type = \"FOUR_LEVEL_SEMIALPHABETIC\" };\n"
  2090. " key <39> { actions=[{}, {} ], type = \"FOUR_LEVEL_SEMIALPHABETIC\" };\n"
  2091. " };\n"
  2092. "};",
  2093. .expected = GOLDEN_TESTS_OUTPUTS "symbols-multi-keysyms-empty.xkb"
  2094. }
  2095. };
  2096. for (size_t k = 0; k < ARRAY_SIZE(keymaps); k++) {
  2097. fprintf(stderr, "------\n*** %s: #%zu ***\n", __func__, k);
  2098. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
  2099. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2100. compile_buffer, NULL, __func__,
  2101. keymaps[k].keymap,
  2102. strlen(keymaps[k].keymap),
  2103. keymaps[k].expected,
  2104. update_output_files));
  2105. }
  2106. #undef make_keymaps_with
  2107. #undef make_keymap
  2108. /* V1 serialization */
  2109. const char keymap_str[] =
  2110. "xkb_keymap {\n"
  2111. " xkb_keycodes { <> = 1; };\n"
  2112. " xkb_compat { interpret A { action = {SetMods(), SetGroup()}; }; };\n"
  2113. " xkb_symbols { key <> { [{SetMods(), SetGroup()}] }; };\n"
  2114. "};";
  2115. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2116. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2117. compile_buffer, NULL, __func__,
  2118. keymap_str, ARRAY_SIZE(keymap_str),
  2119. GOLDEN_TESTS_OUTPUTS "symbols-multi-actions-v1.xkb",
  2120. update_output_files));
  2121. }
  2122. /* Test keysyms as strings */
  2123. static void
  2124. test_key_keysyms_as_strings(struct xkb_context *ctx, bool update_output_files)
  2125. {
  2126. const struct keymap_test_data keymaps[] = {
  2127. /* Invalid UTF-8 encoding */
  2128. {
  2129. .keymap =
  2130. "xkb_keymap {\n"
  2131. " xkb_keycodes { <> = 10; };\n"
  2132. " xkb_symbols {\n"
  2133. /* Invalid byte at string index 2 */
  2134. " key <> { [\"\xC3\xBC\xff\"] };\n"
  2135. " };\n"
  2136. "};",
  2137. .expected = NULL
  2138. },
  2139. /* Valid */
  2140. {
  2141. .keymap =
  2142. u8"xkb_keymap {\n"
  2143. u8" xkb_keycodes {\n"
  2144. u8" <10> = 10;\n"
  2145. u8" <11> = 11;\n"
  2146. u8" <12> = 12;\n"
  2147. u8" <20> = 20;\n"
  2148. u8" <21> = 21;\n"
  2149. u8" <22> = 22;\n"
  2150. u8" <23> = 23;\n"
  2151. u8" <24> = 24;\n"
  2152. u8" <25> = 25;\n"
  2153. u8" <30> = 30;\n"
  2154. u8" <31> = 31;\n"
  2155. u8" <32> = 32;\n"
  2156. u8" <33> = 33;\n"
  2157. u8" <34> = 34;\n"
  2158. u8" <35> = 35;\n"
  2159. u8" <40> = 40;\n"
  2160. u8" <41> = 41;\n"
  2161. u8" <42> = 42;\n"
  2162. u8" <50> = 50;\n"
  2163. u8" <51> = 51;\n"
  2164. u8" <52> = 52;\n"
  2165. u8" <60> = 60;\n"
  2166. u8" <61> = 61;\n"
  2167. u8" <62> = 62;\n"
  2168. u8" <63> = 63;\n"
  2169. u8" <64> = 64;\n"
  2170. u8" <65> = 65;\n"
  2171. u8" <66> = 66;\n"
  2172. u8" <67> = 67;\n"
  2173. u8" <68> = 68;\n"
  2174. u8" <69> = 69;\n"
  2175. u8" <70> = 70;\n"
  2176. u8" <71> = 71;\n"
  2177. u8" <72> = 72;\n"
  2178. u8" <73> = 73;\n"
  2179. u8" <74> = 74;\n"
  2180. u8" <AD08> = 80;\n"
  2181. u8" <AC05> = 81;\n"
  2182. u8" <AB05> = 82;\n"
  2183. u8" <AD01> = 83;\n"
  2184. u8" };\n"
  2185. u8" xkb_types { include \"basic\" };\n"
  2186. u8" xkb_compat {\n"
  2187. u8" interpret.action = SetMods();\n"
  2188. u8" interpret \"ä\" {};\n"
  2189. u8" interpret \"✨\" {};\n"
  2190. u8" interpret \"🎺\" {};\n"
  2191. u8" interpret \"\\u{1F54A}\" {};\n"
  2192. u8" interpret \"\\u{1}\" {};\n"
  2193. u8" interpret \"\\u{a}\" {};\n"
  2194. u8" interpret \"\\u{1f}\" {};\n"
  2195. u8" interpret \"\\u{20}\" {};\n"
  2196. u8" interpret \"\\u{7e}\" {};\n"
  2197. u8" interpret \"\\u{7f}\" {};\n"
  2198. u8" interpret \"\\u{80}\" {};\n"
  2199. u8" interpret \"\\u{9f}\" {};\n"
  2200. u8" interpret \"\\u{a0}\" {};\n"
  2201. u8" interpret \"\\u{ff}\" {};\n"
  2202. // u8" interpret \"\\u{d800}\" {};\n"
  2203. // u8" interpret \"\\u{dfff}\" {};\n"
  2204. u8" interpret \"\\u{fdd0}\" {};\n"
  2205. u8" interpret \"\\u{fdef}\" {};\n"
  2206. u8" interpret \"\\u{fffe}\" {};\n"
  2207. u8" interpret \"\\u{ffff}\" {};\n"
  2208. u8" interpret \"\\u{10000}\" {};\n"
  2209. u8" interpret \"\\u{1ffff}\" {};\n"
  2210. u8" interpret \"\\u{10ffff}\" {};\n"
  2211. u8" };\n"
  2212. u8" xkb_symbols {\n"
  2213. /* Empty string */
  2214. u8" key <10> { [\"\", {b, \"\", c}] };\n"
  2215. u8" key <11> { [{a, \"\"}, {b, \"\"}] };\n"
  2216. u8" key <12> { [{\"\"}, {\"\", \"\"}] };\n"
  2217. /* Single string: Plain */
  2218. u8" key <20> { [\"a\", \"bc\"] };\n"
  2219. u8" key <23> { [\"✨\", \"🎺\"] };\n" // U+2728 ✨, U+1F3BA 🎺
  2220. u8" key <24> { [\"u\u0308\"] };\n" // u + U+0308 ◌̈ COMBINING DIAERESIS
  2221. u8" key <25> { [\"∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა\"] };\n"
  2222. /* Single string: Nested */
  2223. u8" key <30> { [{\"a\" }, {\"bc\" }] };\n"
  2224. u8" key <31> { [{\"a\", \"\"}, {\"bc\", \"\"}] };\n"
  2225. u8" key <32> { [{\"\", \"a\"}, {\"\", \"bc\"}] };\n"
  2226. u8" key <33> { [{\"\\u{1F54A}\"}, {\"\\u{1f3f3}\\u{fe0f}\"}] };\n" // U+1F54A 🕊️, 🏳️
  2227. u8" key <34> { [{\"u\\u{0308}\"}] };\n" // u + U+0308 ◌̈ COMBINING DIAERESIS
  2228. u8" key <35> { [{\"∀\\u{0}∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა\"}] };\n"
  2229. /* Multi: string, literal */
  2230. u8" key <40> { [{\"a\", b}, {\"cde\", f}] };\n"
  2231. u8" key <41> { [{\"a\", \"\", b}, {\"cde\", \"\", f}] };\n"
  2232. u8" key <42> { [{\"a\", b, \"\"}, {\"cde\", f, \"\"}] };\n"
  2233. /* Multi: literal, string */
  2234. u8" key <50> { [{a, \"b\"}, {c, \"def\"}] };\n"
  2235. u8" key <51> { [{a, \"\", \"b\"}, {c, \"\", \"def\"}] };\n"
  2236. u8" key <52> { [{a, \"b\", \"\"}, {c, \"def\", \"\"}] };\n"
  2237. /* Multi: string, string */
  2238. u8" key <60> { [{\"a\", \"b\"}, {\"cd\", \"ef\"}] };\n"
  2239. u8" key <61> { [{\"a\", \"\", \"b\"}, {\"cd\", \"\", \"ef\"}] };\n"
  2240. u8" key <63> { [{\"a\", \"bcd\"}, {\"efg\", \"h\"}] };\n"
  2241. u8" key <64> { [{\"a\", \"\", \"bcd\"}, {\"efg\", \"\", \"h\"}] };\n"
  2242. /* Some special Unicode code points */
  2243. u8" key <65> { [\"\\u{0}\", \"\\u{10ffff}\"] };\n"
  2244. u8" key <66> { [\"\\u{1}\", \"\\u{a}\"] };\n"
  2245. u8" key <67> { [\"\\u{1f}\", \"\\u{20}\"] };\n"
  2246. u8" key <68> { [\"\\u{7e}\", \"\\u{7f}\"] };\n"
  2247. u8" key <69> { [\"\\u{80}\", \"\\u{9f}\"] };\n"
  2248. u8" key <70> { [\"\\u{a0}\", \"\\u{ff}\"] };\n"
  2249. u8" key <71> { [\"\\u{d800}\", \"\\u{dfff}\"] };\n"
  2250. u8" key <72> { [\"\\u{fdd0}\", \"\\u{fdef}\"] };\n"
  2251. u8" key <73> { [\"\\u{fffe}\", \"\\u{ffff}\"] };\n"
  2252. u8" key <74> { [\"\\u{10000}\", \"\\u{1ffff}\"] };\n"
  2253. /* Example from the doc */
  2254. u8" key <AD08> { [ \"ij\" , \"IJ\" ] }; // IJ Dutch digraph\n"
  2255. u8" key <AC05> { [ \"g̃\" , \"G̃\" ] }; // G̃ Guarani letter\n"
  2256. /* NOTE: We use U+200E LEFT-TO-RIGHT MARK in order to display the strings in
  2257. * in the proper order. */
  2258. u8" key <AB05> { [ \"لا\"‎ , \"لآ\"‎ ] }; // ⁧لا⁩ ⁧لآ⁩ Arabic Lam-Alef ligatures decomposed\n"
  2259. u8" key <AD01> { [ \"c’h\", \"C’h\" ] }; // C’H Breton trigraph\n"
  2260. u8" modifier_map Mod1 { \"✨\", \"\\u{1F54A}\" };\n"
  2261. u8" };\n"
  2262. u8"};",
  2263. .expected = GOLDEN_TESTS_OUTPUTS "string-as-keysyms.xkb"
  2264. },
  2265. };
  2266. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  2267. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  2268. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2269. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2270. compile_buffer, NULL, __func__,
  2271. keymaps[k].keymap, strlen(keymaps[k].keymap),
  2272. keymaps[k].expected, update_output_files));
  2273. }
  2274. }
  2275. static void
  2276. test_invalid_symbols_fields(struct xkb_context *ctx)
  2277. {
  2278. /* Any of the following is invalid syntax, but also use to trigger
  2279. * a NULL pointer deference, thus this regression test */
  2280. const char * const keymaps[] = {
  2281. "xkb_keymap {\n"
  2282. " xkb_keycodes { <> = 9; };\n"
  2283. " xkb_symbols { key <> { vmods = [] }; };\n"
  2284. "};",
  2285. "xkb_keymap {\n"
  2286. " xkb_keycodes { <> = 9; };\n"
  2287. " xkb_symbols { key <> { repeat = [] }; };\n"
  2288. "};",
  2289. "xkb_keymap {\n"
  2290. " xkb_keycodes { <> = 9; };\n"
  2291. " xkb_symbols { key <> { type = [] }; };\n"
  2292. "};",
  2293. "xkb_keymap {\n"
  2294. " xkb_keycodes { <> = 9; };\n"
  2295. " xkb_symbols { key <> { groupswrap = [] }; };\n"
  2296. "};",
  2297. "xkb_keymap {\n"
  2298. " xkb_keycodes { <> = 9; };\n"
  2299. " xkb_symbols { key <> { groupsredirect = [] }; };\n"
  2300. "};",
  2301. /* Used to parse without error because the “repeats” key field is valid,
  2302. * but we should fail in the following 2 keymaps */
  2303. "xkb_keymap {\n"
  2304. " xkb_keycodes { <> = 9; };\n"
  2305. " xkb_symbols { key <> { vmods=[], repeats=false }; };\n"
  2306. "};",
  2307. "xkb_keymap {\n"
  2308. " xkb_keycodes { <> = 9; };\n"
  2309. " xkb_symbols { key <> { repeats=false, vmods=[] }; };\n"
  2310. "};"
  2311. };
  2312. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  2313. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  2314. const struct xkb_keymap *keymap =
  2315. test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2316. keymaps[k], strlen(keymaps[k]));
  2317. assert(!keymap);
  2318. }
  2319. }
  2320. static void
  2321. test_modifier_maps(struct xkb_context *ctx, bool update_output_files)
  2322. {
  2323. /* Only accept key and keysyms in the modifier_map list */
  2324. struct keymap_test_data keymaps[] = {
  2325. {
  2326. .keymap =
  2327. u8"xkb_keymap {\n"
  2328. u8" xkb_keycodes {\n"
  2329. u8" <CAPS> = 66;\n"
  2330. u8" alias <LOCK> = <CAPS>;\n"
  2331. u8" <0> = 0;"
  2332. u8" <1> = 1;"
  2333. u8" <2> = 2;"
  2334. u8" <3> = 3;"
  2335. u8" <4> = 4;"
  2336. u8" <5> = 5;"
  2337. u8" <6> = 6;"
  2338. u8" <7> = 7;"
  2339. u8" <any> = 10;"
  2340. u8" <none> = 11;"
  2341. u8" <a> = 61;"
  2342. u8" <b> = 62;"
  2343. u8" <c> = 63;"
  2344. u8" <100> = 100;"
  2345. u8" };\n"
  2346. u8" xkb_types { include \"basic\" };\n"
  2347. u8" xkb_symbols {\n"
  2348. u8" key <CAPS> { [Caps_Lock] };\n"
  2349. u8" key <any> { [any, A] };\n"
  2350. u8" key <none> { [none, N] };\n"
  2351. u8" key <0> { [0] };\n"
  2352. u8" key <1> { [1] };\n"
  2353. u8" key <2> { [2] };\n"
  2354. u8" key <a> { [a] };\n"
  2355. u8" key <b> { [b] };\n"
  2356. u8" key <c> { [\"🎺\"] };\n"
  2357. u8" key <3> { [NotAKeysym, 3] };\n"
  2358. u8" key <4> { [\"\\u{0000001F54A}\"]};\n"
  2359. u8" key <5> { [\"\\u{d800}\", \"\\u{dfff}\"]};\n"
  2360. u8" key <6> { [\"\\u{fdd0}\", \"\\u{fdef}\"]};\n"
  2361. u8" key <7> { [\"\\u{fffe}\", \"\\u{ffff}\"]};\n"
  2362. u8" key <100> { [C] };\n"
  2363. u8" modifier_map Lock {\n"
  2364. u8" <100>, <LOCK>, any, none,\n"
  2365. u8" 0, 1, 0x2, a, \"b\", \"🎺\", \"\\u{1F54A}\",\n"
  2366. u8" \"\\u{d800}\", \"\\u{dfff}\",\n"
  2367. u8" \"\\u{fdd0}\", \"\\u{fdef}\",\n"
  2368. u8" \"\\u{fffe}\", \"\\u{ffff}\",\n"
  2369. u8" NotAKeysym\n"
  2370. u8" };\n"
  2371. u8" };\n"
  2372. u8"};",
  2373. .expected = GOLDEN_TESTS_OUTPUTS "symbols-modifier_map.xkb"
  2374. },
  2375. /* Invalid: empty string */
  2376. {
  2377. .keymap =
  2378. "xkb_keymap {\n"
  2379. " xkb_symbols { modifier_map Lock { \"\" }; };\n"
  2380. "};",
  2381. .expected = NULL
  2382. },
  2383. /* Invalid Unicode encoding */
  2384. {
  2385. .keymap =
  2386. "xkb_keymap {\n"
  2387. " xkb_symbols { modifier_map Lock { \"\xff\" }; };\n"
  2388. "};",
  2389. .expected = NULL
  2390. },
  2391. /* Invalid single Unicode code point */
  2392. {
  2393. .keymap =
  2394. "xkb_keymap {\n"
  2395. " xkb_symbols { modifier_map Lock { \"\\u{0}\" }; };\n"
  2396. "};",
  2397. .expected = NULL
  2398. },
  2399. /* Invalid multiple Unicode code points */
  2400. {
  2401. .keymap =
  2402. "xkb_keymap {\n"
  2403. " xkb_symbols { modifier_map Lock { \"ab\" }; };\n"
  2404. "};",
  2405. .expected = NULL
  2406. },
  2407. /* Invalid type: list */
  2408. {
  2409. .keymap =
  2410. "xkb_keymap {\n"
  2411. " xkb_symbols { modifier_map Lock { [a] }; };\n"
  2412. "};",
  2413. .expected = NULL
  2414. },
  2415. /* Invalid type: list */
  2416. {
  2417. .keymap =
  2418. "xkb_keymap {\n"
  2419. " xkb_symbols { modifier_map Lock { {a, b} }; };\n"
  2420. "};",
  2421. .expected = NULL
  2422. },
  2423. };
  2424. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  2425. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  2426. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2427. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2428. compile_buffer, NULL, __func__,
  2429. keymaps[k].keymap, strlen(keymaps[k].keymap),
  2430. keymaps[k].expected, update_output_files));
  2431. }
  2432. }
  2433. static void
  2434. test_empty_compound_statements(struct xkb_context *ctx, bool update_output_files)
  2435. {
  2436. struct keymap_test_data keymaps[] = {
  2437. {
  2438. .keymap =
  2439. "xkb_keymap {\n"
  2440. " xkb_keycodes {\n"
  2441. " <Q> = 24;\n"
  2442. " <W> = 25;\n"
  2443. " <E> = 26;\n"
  2444. " <R> = 27;\n"
  2445. " <T> = 28;\n"
  2446. " <Y> = 29;\n"
  2447. " <U> = 30;\n"
  2448. " <I> = 31;\n"
  2449. " <O> = 32;\n"
  2450. " <P> = 33;\n"
  2451. " <A> = 38;\n"
  2452. " <S> = 39;\n"
  2453. " <D> = 40;\n"
  2454. " <F> = 41;\n"
  2455. " <G> = 42;\n"
  2456. " <H> = 43;\n"
  2457. " <Z> = 52;\n"
  2458. " <X> = 53;\n"
  2459. " <C> = 54;\n"
  2460. " <V> = 55;\n"
  2461. " <B> = 56;\n"
  2462. " <N> = 57;\n"
  2463. " <M> = 58;\n"
  2464. " };\n"
  2465. " xkb_types {\n"
  2466. " type \"t1\" {};\n"
  2467. " type \"t2\" { modifiers = Shift; map[Shift] = 2; };\n"
  2468. " };\n"
  2469. " xkb_compat {\n"
  2470. " virtual_modifiers M1, M2;\n"
  2471. " indicator \"xxx\" {};\n"
  2472. " indicator.modifiers = Shift;"
  2473. " indicator \"yyy\" {};\n"
  2474. "\n"
  2475. " interpret q {};\n"
  2476. " interpret.repeat = true;\n"
  2477. " interpret w {};\n"
  2478. " };\n"
  2479. " xkb_symbols {\n"
  2480. " key <Q> { [q] };\n"
  2481. " key <W> { [SetMods()] };\n"
  2482. " key <E> { [e], type = \"t1\" };\n"
  2483. /* Empty key */
  2484. " key <R> {};\n"
  2485. /* Empty key, same as previous */
  2486. " key <Y> { [] };\n"
  2487. /* Override empty key with another */
  2488. " key <T> {};\n"
  2489. " key <T> { [] };\n"
  2490. /* Override empty key with some symbols */
  2491. " key <U> {};\n"
  2492. " key <U> { [], [u] };\n"
  2493. /* Override non-empty key with an empty key */
  2494. " key <I> { [i] };\n"
  2495. " key <I> {};\n"
  2496. /* Empty symbols/actions */
  2497. " key <O> { [NoSymbol] };\n"
  2498. " key <P> { [NoAction()] };\n"
  2499. /* No symbols nor actions: other properties */
  2500. " key <A> { vmods = M1 };\n"
  2501. " key <S> { repeat = false };\n"
  2502. " key <D> { type = \"t2\" };\n"
  2503. " key <F> { [], type = \"t2\" };\n"
  2504. " key <G> { type[1] = \"t2\" };\n"
  2505. " key <H> { type[1] = \"t1\", type[2] = \"t2\" };\n"
  2506. /* Test key defaults / modmaps */
  2507. " key <Z> {};\n"
  2508. " key.vmods = M1;\n"
  2509. " key <X> {};\n"
  2510. " key <C> { vmods = M2 };\n"
  2511. " key.type = \"t2\";\n"
  2512. " key <V> { vmods = 0 };\n"
  2513. " key <B> { [], vmods = 0 };\n"
  2514. " key.type[1] = \"t1\";\n"
  2515. " key <N> { vmods = 0 };\n"
  2516. " key <M> { [], [], vmods = 0 };\n"
  2517. " modmap Shift { <Z> };\n"
  2518. " modmap Lock { <X> };\n"
  2519. " modmap Control { <C> };\n"
  2520. " };\n"
  2521. "};",
  2522. .expected = GOLDEN_TESTS_OUTPUTS "empty-compound-statements.xkb"
  2523. },
  2524. };
  2525. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  2526. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  2527. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2528. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2529. compile_buffer, NULL, __func__,
  2530. keymaps[k].keymap, strlen(keymaps[k].keymap),
  2531. keymaps[k].expected, update_output_files));
  2532. }
  2533. }
  2534. static void
  2535. test_escape_sequences(struct xkb_context *ctx, bool update_output_files)
  2536. {
  2537. /* Similarly to `test_integers`, test that escape sequences at the end of
  2538. * a buffer raise a syntax error but no memory violation */
  2539. const char bad_octal[] = { '"', '\\', '1' };
  2540. assert(!test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2541. bad_octal, sizeof(bad_octal)));
  2542. const char bad_unicode[] = { '"', '\\', 'u', '{', '1' };
  2543. assert(!test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2544. bad_unicode, sizeof(bad_unicode)));
  2545. const char keymap[] =
  2546. "default xkb_keymap \"\" {\n"
  2547. " xkb_keycodes "
  2548. "\"\\u{0}La paix est la seule\\tbataille "
  2549. "qui vaille la peine d\\u{02019}\\u{Ea}tre men\\303\\251e.\\n\" {\n"
  2550. " <> = 1;\n"
  2551. " indicator 1 = \"\\0\\n\\u{2328}\\u{fe0f}\";\n"
  2552. " indicator 2 = \"surrogates: \\u{d800} \\u{dfff}\";\n"
  2553. " indicator 3 = \"noncharacters: \\u{fdd0} \\u{fdef} \\u{fffe} \\u{ffff}\";\n"
  2554. " indicator 4 = \"noncharacters: \\u{1fffe} \\u{1ffff} \\u{10fffe} \\u{10ffff}\";\n"
  2555. " indicator 5 = \"out of range: \\u{0} a \\u{110000} \\u{ffffffffffff}\";\n"
  2556. " indicator 6 = \"invalid: \\u a \\uA b \\u{} c \\u{ d \\u} e \\u{1\";\n"
  2557. " indicator 7 = \"invalid: \\u{1234x\\\" y \";\n"
  2558. " indicator 8 = \"invalid: \\u{ 2} x \\u{3 } y\";\n"
  2559. " indicator 9 = \"\\u{+1} \\u{-1} \\u{x} \\u{#} \\u{\\0} \\u{\\}\";\n"
  2560. " };\n"
  2561. " xkb_types \"\\00001\\\\\\00427\\u{22}\\r\\n\" {\n"
  2562. " type \"\\0\\00451\\u{1F3BA}\\u{2728}\\u{01F54a}\\u{0fE0f}\\f\" {\n"
  2563. " modifiers = Shift;\n"
  2564. " map[Shift] = 2;\n"
  2565. " level_name[1] = "
  2566. "\"O\\u{f9} ils ont fait un \\u{22}d\\303\\251sert\\u{22}, \\e"
  2567. "ils disent qu\\u{002019}ils \\12ont fait la \\42paix\\042.\\b\\n\";\n"
  2568. " level_name[2] = "
  2569. "\"\\u{0000}Science \\u{73}\\141ns conscience "
  2570. "n\\u{2019}est que rui\\\\ne\\u{A} de l\\u{02019}\\u{E2}me.\\r\";\n"
  2571. " };\n"
  2572. " };\n"
  2573. " xkb_compat "
  2574. "\"Le c\\u{153}ur a \\v ses raisons "
  2575. "que la raison ne con\\u{5C}na\\u{EE}t point\" {\n"
  2576. " indicator \"\\n\\u{2328}\\0\\u{fe0f}\" { modifiers = Shift; };\n"
  2577. " };\n"
  2578. " xkb_symbols "
  2579. "\"La libert\\u{00e9} commence "
  2580. "o\\u{0000f9} l\\342\\200\\231ignorance finit.\" {\n"
  2581. " name[1] = \"\\n\\0377\\3760\";\n"
  2582. " name[2] = \"\\00427\";\n"
  2583. " key <> {\n"
  2584. " symbols[1]=[a, A],\n"
  2585. " type[1]=\"%1\\u{1F3BA}\\u{2728}\\u{00001F54a}\\u{0fE0f}\\u{0C}\",\n"
  2586. " actions[2]=[Private(type=123, data=\"abc\0def\")]"
  2587. " };\n"
  2588. " };\n"
  2589. "};";
  2590. const char expected[] = GOLDEN_TESTS_OUTPUTS "escape-sequences.xkb";
  2591. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2592. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2593. compile_buffer, NULL, __func__,
  2594. keymap, ARRAY_SIZE(keymap),
  2595. expected, update_output_files));
  2596. }
  2597. static void
  2598. test_unicode_keysyms(struct xkb_context *ctx, bool update_output_files)
  2599. {
  2600. struct keymap_test_data keymaps[] = {
  2601. {
  2602. .keymap =
  2603. "xkb_keymap {\n"
  2604. " xkb_keycodes { include \"evdev\" };\n"
  2605. " xkb_types { include \"basic\" };\n"
  2606. " xkb_symbols {\n"
  2607. /* C0 Control characters */
  2608. " key <AE01> { [U0000, 0x01000000 ] };\n"
  2609. " key <AE02> { [U0001, 0x01000001 ] };\n"
  2610. " key <AE03> { [U000A, 0x0100000a ] };\n"
  2611. " key <AE04> { [U001F, 0x0100001f ] };\n"
  2612. /* Printable ASCII characters */
  2613. " key <AE05> { [U0020, 0x01000020 ] };\n"
  2614. " key <AE06> { [U007E, 0x0100007e ] };\n"
  2615. /* C0/C1 control characters */
  2616. " key <AE07> { [U007F, 0x0100007f ] };\n"
  2617. " key <AE08> { [U0080, 0x01000080 ] };\n"
  2618. " key <AE09> { [U009F, 0x0100009f ] };\n"
  2619. /* Latin-1 printable characters */
  2620. " key <AE10> { [U00A0, 0x010000a0 ] };\n"
  2621. " key <AE11> { [U00FF, 0x010000ff ] };\n"
  2622. /* Misc: bounds */
  2623. " key <AD01> { [U0100, 0x01000100 ] };\n"
  2624. " key <AD02> { [UD7FF, 0x0100d7ff ] };\n"
  2625. /* Surrogates */
  2626. " key <AD03> { [UD800, 0x0100d800 ] };\n"
  2627. " key <AD04> { [UDFFF, 0x0100dfff ] };\n"
  2628. /* Misc: bounds */
  2629. " key <AD05> { [UE000, 0x0100e000 ] };\n"
  2630. " key <AD06> { [UFDCF, 0x0100fdcf ] };\n"
  2631. /* Noncharacters */
  2632. " key <AD07> { [UFDD0, 0x0100fdd0 ] };\n"
  2633. " key <AD08> { [UFDEF, 0x0100fdef ] };\n"
  2634. /* Misc: bounds */
  2635. " key <AD09> { [UFDF0, 0x0100fdf0 ] };\n"
  2636. " key <AD10> { [UFFFD, 0x0100fffd ] };\n"
  2637. /* Noncharacters */
  2638. " key <AD11> { [UFFFE, 0x0100fffe ] };\n"
  2639. " key <AD12> { [UFFFF, 0x0100ffff ] };\n"
  2640. /* Misc: bounds */
  2641. " key <AC01> { [U10000, 0x01010000 ] };\n"
  2642. /* Noncharacters */
  2643. " key <AC08> { [U1FFFE , 0x0101fffe ] };\n"
  2644. " key <AC09> { [U1FFFF , 0x0101ffff ] };\n"
  2645. " key <AC10> { [U10FFFE, 0x0110fffe ] };\n"
  2646. /* Max Unicode */
  2647. " key <AC11> { [U10FFFF, 0x0110ffff ] };\n"
  2648. /* Max Unicode + 1 */
  2649. " key <AC12> { [U110000, 0x01110000 ] };\n"
  2650. /* Misc */
  2651. " key <AB01> { [U0174, 0x01000174 ] };\n"
  2652. " };\n"
  2653. "};",
  2654. .expected = GOLDEN_TESTS_OUTPUTS "unicode-keysyms.xkb"
  2655. },
  2656. };
  2657. for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
  2658. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  2659. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2660. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2661. compile_buffer, NULL, __func__,
  2662. keymaps[k].keymap, strlen(keymaps[k].keymap),
  2663. keymaps[k].expected, update_output_files));
  2664. }
  2665. }
  2666. static void
  2667. test_no_action_void_action(struct xkb_context *ctx, bool update_output_files)
  2668. {
  2669. const char keymap_str[] =
  2670. "xkb_keymap {\n"
  2671. " xkb_keycodes { <1> = 1; <2> = 2; <3> = 3; };\n"
  2672. " xkb_symbols {\n"
  2673. /* These actions take no argument */
  2674. " key <1> { [x], [NoAction(mods=1)] };\n"
  2675. " key <2> { [y], [VoidAction(mods=1)] };\n"
  2676. /* Check that we can overwrite NoAction, but not the contrary */
  2677. " key <3> { [NoAction()] };\n"
  2678. " key <3> { [VoidAction()] };\n"
  2679. " key <3> { [NoAction()] };\n"
  2680. " };\n"
  2681. "};";
  2682. const enum xkb_keymap_compile_flags flags = TEST_KEYMAP_COMPILE_FLAGS
  2683. & ~XKB_KEYMAP_COMPILE_STRICT_MODE;
  2684. static const struct {
  2685. struct {
  2686. enum xkb_keymap_format input;
  2687. enum xkb_keymap_format output;
  2688. } format;
  2689. const char* output;
  2690. } tests[] = {
  2691. {
  2692. /* v1 → v1 */
  2693. { XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_FORMAT_TEXT_V1},
  2694. NULL,
  2695. },
  2696. {
  2697. /* v1 → v2 */
  2698. { XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_FORMAT_TEXT_V2},
  2699. NULL,
  2700. },
  2701. {
  2702. /* v2 → v2 */
  2703. { XKB_KEYMAP_FORMAT_TEXT_V2, XKB_KEYMAP_FORMAT_TEXT_V2},
  2704. GOLDEN_TESTS_OUTPUTS "no_void_action-v2.xkb",
  2705. },
  2706. {
  2707. /* v2 → v1 */
  2708. { XKB_KEYMAP_FORMAT_TEXT_V2, XKB_KEYMAP_FORMAT_TEXT_V1},
  2709. GOLDEN_TESTS_OUTPUTS "no_void_action-v1.xkb",
  2710. },
  2711. };
  2712. for (unsigned int t = 0; t < ARRAY_SIZE(tests); t++) {
  2713. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, t);
  2714. assert(test_compile_output(ctx, tests[t].format.input,
  2715. tests[t].format.output,
  2716. compile_buffer, (void*)&flags, NULL,
  2717. keymap_str, sizeof(keymap_str),
  2718. tests[t].output, update_output_files));
  2719. }
  2720. }
  2721. static void
  2722. test_prebuilt_keymap_roundtrip(struct xkb_context *ctx, bool update_output_files)
  2723. {
  2724. /* Load in a prebuilt keymap, make sure we can compile it from memory,
  2725. * then compare it to make sure we get the same result when dumping it
  2726. * to a string. */
  2727. static const struct {
  2728. const char* path;
  2729. enum xkb_keymap_format format;
  2730. enum xkb_keymap_serialize_flags serialize_flags;
  2731. } data[] = {
  2732. {
  2733. .path = "keymaps/stringcomp-v1.xkb",
  2734. .format = XKB_KEYMAP_FORMAT_TEXT_V1,
  2735. .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
  2736. },
  2737. {
  2738. .path = "keymaps/stringcomp-v1-no-prettyfied.xkb",
  2739. .format = XKB_KEYMAP_FORMAT_TEXT_V1,
  2740. .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
  2741. & ~XKB_KEYMAP_SERIALIZE_PRETTY
  2742. },
  2743. {
  2744. .path = "keymaps/stringcomp-v2.xkb",
  2745. .format = XKB_KEYMAP_FORMAT_TEXT_V2,
  2746. .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
  2747. },
  2748. };
  2749. for (unsigned int k = 0; k < ARRAY_SIZE(data); k++) {
  2750. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  2751. char *original = test_read_file(data[k].path);
  2752. assert(original);
  2753. /* Load a prebuild keymap, once without, once with the trailing \0 */
  2754. const size_t length = strlen(original);
  2755. for (unsigned int i = 0; i <= 1; i++) {
  2756. assert(test_compile_output2(ctx, data[k].format,
  2757. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2758. data[k].serialize_flags,
  2759. compile_buffer, NULL, "Round-trip",
  2760. original, length + i,
  2761. data[k].path, update_output_files));
  2762. }
  2763. free(original);
  2764. }
  2765. }
  2766. static void
  2767. test_keymap_from_rules(struct xkb_context *ctx)
  2768. {
  2769. /* Make sure we can recompile our output for a normal keymap from rules. */
  2770. fprintf(stderr, "------\n*** %s ***\n", __func__);
  2771. struct xkb_keymap *keymap = test_compile_rules(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2772. NULL, NULL,
  2773. "ru,ca,de,us",
  2774. ",multix,neo,intl", NULL);
  2775. assert(keymap);
  2776. char *dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
  2777. assert(dump);
  2778. xkb_keymap_unref(keymap);
  2779. keymap = test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2780. dump, strlen(dump));
  2781. assert(keymap);
  2782. xkb_keymap_unref(keymap);
  2783. free(dump);
  2784. }
  2785. static void
  2786. test_redirect_key(struct xkb_context *ctx, bool update_output_files)
  2787. {
  2788. const struct keymap_test_data tests[] = {
  2789. {
  2790. .keymap =
  2791. "xkb_keymap {\n"
  2792. " xkb_keycodes { <A> = 38; <S> = 39; <D> = 40; <F> = 41; <G> = 42;};\n"
  2793. " xkb_symbols {\n"
  2794. /* Inexistent key */
  2795. " key <A> { [a], [RedirectKey(key=<?>)] };\n"
  2796. /* Multiple RedirectKey() on the same level */
  2797. " key <S> { repeat=true, [s], [RedirectKey(key=<A>), RedirectKey(key=<D>)] };\n"
  2798. /* OK! */
  2799. " key <D> { repeat=true, [d], [RedirectKey(key=<S>,mods=Shift,clearMods=Control)] };\n"
  2800. /* No parameters (implicit auto) */
  2801. " key <F> { [f], [RedirectKey()] };\n"
  2802. /* Explicit auto keycode */
  2803. " key <G> { [g], [RedirectKey(keycode=auto)] };\n"
  2804. " };\n"
  2805. "};",
  2806. .expected = GOLDEN_TESTS_OUTPUTS "redirect-key-1.xkb"
  2807. },
  2808. };
  2809. const enum xkb_keymap_compile_flags flags = TEST_KEYMAP_COMPILE_FLAGS
  2810. & ~XKB_KEYMAP_COMPILE_STRICT_MODE;
  2811. for (unsigned int t = 0; t < ARRAY_SIZE(tests); t++) {
  2812. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, t);
  2813. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2814. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2815. compile_buffer, (void*)&flags, __func__,
  2816. tests[t].keymap, strlen(tests[t].keymap),
  2817. tests[t].expected, update_output_files));
  2818. }
  2819. }
  2820. static void
  2821. test_unsupported_legacy_x11_actions(struct xkb_context *ctx,
  2822. bool update_output_files)
  2823. {
  2824. const char keymap_str[] =
  2825. "xkb_keymap {\n"
  2826. " xkb_keycodes {\n"
  2827. " <1> = 1;\n"
  2828. " <2> = 2;\n"
  2829. " <3> = 3;\n"
  2830. " <4> = 4;\n"
  2831. " <5> = 5;\n"
  2832. " };\n"
  2833. " xkb_compat {\n"
  2834. " ISOLock.modifiers = modMapMods;\n"
  2835. " DeviceButton.data = <1>;\n" /* invalid field */
  2836. " LockDeviceButton.data = <1>;\n" /* invalid field */
  2837. " DeviceValuator.data = <1>;\n" /* invalid field */
  2838. " ActionMessage.data = <1>;\n" /* invalid field */
  2839. " interpret ISO_Lock {\n"
  2840. " action=ISOLock(affect=all);\n"
  2841. " };\n"
  2842. " interpret 0x10000 {\n"
  2843. " action=DeviceButton(data=all);\n" /* invalid field */
  2844. " };\n"
  2845. " interpret 0x10001 {\n"
  2846. " action=LockDeviceButton(data=all);\n" /* invalid field */
  2847. " };\n"
  2848. " interpret 0x10002 {\n"
  2849. " action=DeviceValuator(data=all);\n" /* invalid field */
  2850. " };\n"
  2851. " interpret 0x10003 {\n"
  2852. " action=ActionMessage(data=all);\n" /* invalid field */
  2853. " };\n"
  2854. " };\n"
  2855. " xkb_symbols {\n"
  2856. " key <1> { [ISOLock(affect=all)] };\n"
  2857. " key <2> { [DeviceButton(data=<1>)] };\n" /* invalid field */
  2858. " key <3> { [LockDeviceButton(data=<1>)] };\n" /* invalid field */
  2859. " key <4> { [DeviceValuator(data=<1>)] };\n" /* invalid field */
  2860. " key <5> { [ActionMessage(data=<1>)] };\n" /* invalid field */
  2861. " };\n"
  2862. "};";
  2863. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  2864. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  2865. compile_buffer, NULL, __func__,
  2866. keymap_str, sizeof(keymap_str),
  2867. GOLDEN_TESTS_OUTPUTS "unsupported-x11-actions",
  2868. update_output_files));
  2869. }
  2870. struct keymap_multi_versions_test_data {
  2871. const char* keymap;
  2872. bool no_output;
  2873. bool lenient;
  2874. union {
  2875. struct { bool compiles_v1; bool compiles_v2; };
  2876. struct {
  2877. const char* expected_v1_1;
  2878. const char* expected_v1_2;
  2879. const char* expected_v2_2;
  2880. const char* expected_v2_1;
  2881. };
  2882. };
  2883. };
  2884. static void
  2885. test_overlays(struct xkb_context *ctx, bool update_output_files)
  2886. {
  2887. static const struct keymap_multi_versions_test_data tests[] = {
  2888. /* Legacy */
  2889. {
  2890. .keymap =
  2891. "xkb_keymap {\n"
  2892. " xkb_keycodes {\n"
  2893. " <j> = 44;\n"
  2894. " };\n"
  2895. " xkb_symbols {\n"
  2896. " key <j> { [j], overlay1 = <xxx> };\n"
  2897. " };\n"
  2898. "};",
  2899. .lenient = false,
  2900. .no_output = true,
  2901. .compiles_v1 = false,
  2902. .compiles_v2 = false,
  2903. },
  2904. {
  2905. .keymap =
  2906. "xkb_keymap {\n"
  2907. " xkb_keycodes {\n"
  2908. " <j> = 44;\n"
  2909. " <kp1> = 87;\n"
  2910. " <left> = 113;\n"
  2911. " };\n"
  2912. " xkb_symbols {\n"
  2913. /* V1 does not support overlapping overlays */
  2914. " key <j> { [j], overlay1 = <kp1>, overlay2 = <left> };\n"
  2915. " };\n"
  2916. "};",
  2917. .lenient = false,
  2918. .no_output = true,
  2919. .compiles_v1 = false,
  2920. .compiles_v2 = true,
  2921. },
  2922. {
  2923. .keymap =
  2924. "xkb_keymap {\n"
  2925. " xkb_keycodes {\n"
  2926. " <j> = 44;\n"
  2927. " <kp1> = 87;\n"
  2928. " };\n"
  2929. " xkb_symbols {\n"
  2930. /* V1 does not support overlays > 2 */
  2931. " key <j> { [j], overlay3 = <kp1> };\n"
  2932. " };\n"
  2933. "};",
  2934. .lenient = false,
  2935. .no_output = true,
  2936. .compiles_v1 = false,
  2937. .compiles_v2 = true,
  2938. },
  2939. {
  2940. .keymap =
  2941. "xkb_keymap {\n"
  2942. " xkb_keycodes {\n"
  2943. " <j> = 44;\n"
  2944. " <kp1> = 87;\n"
  2945. " };\n"
  2946. " xkb_symbols {\n"
  2947. /* V1 does not support overlays > 2 */
  2948. " key <j> { [j], overlay3 = none };\n"
  2949. " };\n"
  2950. "};",
  2951. .lenient = false,
  2952. .no_output = true,
  2953. .compiles_v1 = false,
  2954. .compiles_v2 = true,
  2955. },
  2956. {
  2957. .keymap =
  2958. "xkb_keymap {\n"
  2959. " xkb_keycodes {\n"
  2960. " <j> = 44;\n"
  2961. " <kp1> = 87;\n"
  2962. " <left> = 113;\n"
  2963. " };\n"
  2964. " xkb_symbols {\n"
  2965. " key <j> { [j], overlay2 = <left> };\n"
  2966. /* overlay2 is discarded, therefore there is no overlap */
  2967. " key <j> { overlay1 = <kp1>, overlay2 = none };\n"
  2968. " key <kp1> { [KP_1], overlay1 = none, overlay1 = <j> };\n"
  2969. " key <left> { [Left], overlay1 = <kp1> };\n"
  2970. " key <left> { overlay1 = none };\n"
  2971. " };\n"
  2972. "};",
  2973. .lenient = false,
  2974. .expected_v1_1 = GOLDEN_TESTS_OUTPUTS "overlays-v1-1.xkb",
  2975. .expected_v1_2 = GOLDEN_TESTS_OUTPUTS "overlays-v1-1.xkb",
  2976. .expected_v2_2 = GOLDEN_TESTS_OUTPUTS "overlays-v1-1.xkb",
  2977. .expected_v2_1 = GOLDEN_TESTS_OUTPUTS "overlays-v1-1.xkb",
  2978. },
  2979. {
  2980. .keymap =
  2981. "xkb_keymap {\n"
  2982. " xkb_keycodes {\n"
  2983. " <j> = 44;\n"
  2984. " <kp1> = 87;\n"
  2985. " <left> = 113;\n"
  2986. " };\n"
  2987. " xkb_symbols {\n"
  2988. /* conflicting entry ignored */
  2989. " key <j> { [j], overlay1 = <kp1>, overlay1 = none };\n"
  2990. " key <kp1> { [KP_1], overlay1 = <j> };\n"
  2991. " key <kp1> { [KP_1], overlay2 = <left> };\n"
  2992. " key <kp1> { [KP_1], overlay1 = none, overlay2 = none };\n"
  2993. " key <left> { [Left], overlay1 = none, overlay2 = none };\n"
  2994. " augment key <left> { [Left], overlay1 = <j> };\n"
  2995. " };\n"
  2996. "};",
  2997. .lenient = false,
  2998. .expected_v1_1 = GOLDEN_TESTS_OUTPUTS "overlays-v1-1.xkb",
  2999. .expected_v1_2 = GOLDEN_TESTS_OUTPUTS "overlays-v1-1.xkb",
  3000. .expected_v2_2 = GOLDEN_TESTS_OUTPUTS "overlays-v1-1.xkb",
  3001. .expected_v2_1 = GOLDEN_TESTS_OUTPUTS "overlays-v1-1.xkb",
  3002. },
  3003. {
  3004. .keymap =
  3005. "xkb_keymap {\n"
  3006. " xkb_keycodes {\n"
  3007. " <1> = 10;\n"
  3008. " <2> = 11;\n"
  3009. " <3> = 12;\n"
  3010. " <4> = 13;\n"
  3011. " <j> = 44;\n"
  3012. " <k> = 45;\n"
  3013. " <kp1> = 87;\n"
  3014. " <kp2> = 88;\n"
  3015. " <f1> = 67;\n"
  3016. " <f2> = 68;\n"
  3017. " <f10> = 76;\n"
  3018. " <f11> = 95;\n"
  3019. " <left> = 113;\n"
  3020. " <down> = 116;\n"
  3021. " };\n"
  3022. " xkb_symbols {\n"
  3023. " key <1> { [LockControls(controls=Overlay1)] };\n"
  3024. " key <2> { [LockControls(controls=Overlay2)] };\n"
  3025. " key <3> { [LockControls(controls=none)] };\n"
  3026. " key <4> { [LockControls(controls=none)] };\n"
  3027. /* V1 will discard overlay2 */
  3028. " key <j> { [j], overlay1 = <kp1>, overlay2 = <left> };\n"
  3029. " key <k> { [k], overlay1 = <kp2>, overlay2 = <down> };\n"
  3030. " key <kp1> { [KP_1], overlay1 = none };\n"
  3031. " key <kp2> { [KP_2] };\n"
  3032. " key <left> { [Left], overlay1 = <kp1> };\n"
  3033. " key <left> { overlay1 = none };\n"
  3034. " key <down> { [Down] };\n"
  3035. " key <f1> { [F1] };\n"
  3036. " key <f2> { [F2] };\n"
  3037. " key <f10> { [F10] };\n"
  3038. " key <f11> { [F11] };\n"
  3039. " };\n"
  3040. "};",
  3041. .lenient = true,
  3042. .expected_v1_1 = GOLDEN_TESTS_OUTPUTS "overlays-v1-2.xkb",
  3043. .expected_v1_2 = GOLDEN_TESTS_OUTPUTS "overlays-v1-2.xkb",
  3044. .expected_v2_2 = GOLDEN_TESTS_OUTPUTS "overlays-v2-1.xkb",
  3045. .expected_v2_1 = GOLDEN_TESTS_OUTPUTS "overlays-v1-2.xkb",
  3046. },
  3047. /* Alternate array syntax is not supported (yet) */
  3048. {
  3049. .keymap =
  3050. "xkb_keymap {\n"
  3051. " xkb_keycodes {\n"
  3052. " <j> = 44;\n"
  3053. " <kp1> = 87;\n"
  3054. " <left> = 113;\n"
  3055. " };\n"
  3056. " xkb_symbols {\n"
  3057. " key <j> { [j], overlay[1] = <kp1>, overlay[2] = <left> };\n"
  3058. " key <kp1> { [KP_1] };\n"
  3059. " key <left> { [Left] };\n"
  3060. " };\n"
  3061. "};",
  3062. .lenient = false,
  3063. .no_output = true,
  3064. .compiles_v1 = false,
  3065. .compiles_v2 = false,
  3066. },
  3067. {
  3068. .keymap =
  3069. "xkb_keymap {\n"
  3070. " xkb_keycodes {\n"
  3071. " <1> = 10;\n"
  3072. " <2> = 11;\n"
  3073. " <3> = 12;\n"
  3074. " <4> = 13;\n"
  3075. " <j> = 44;\n"
  3076. " <k> = 45;\n"
  3077. " <kp1> = 87;\n"
  3078. " <kp2> = 88;\n"
  3079. " <f1> = 67;\n"
  3080. " <f2> = 68;\n"
  3081. " <f10> = 76;\n"
  3082. " <f11> = 95;\n"
  3083. " <left> = 113;\n"
  3084. " <down> = 116;\n"
  3085. " };\n"
  3086. " xkb_symbols {\n"
  3087. " key <1> { [LockControls(controls=Overlay1)] };\n"
  3088. " key <2> { [LockControls(controls=Overlay2)] };\n"
  3089. " key <3> { [LockControls(controls=Overlay3)] };\n"
  3090. " key <4> { [LockControls(controls=Overlay4)] };\n"
  3091. " key <j> { [j] };\n"
  3092. " key <j> {\n"
  3093. " overlay1 = <left>, overlay2 = <left>,\n"
  3094. " overlay6 = <j>, overlay7 = <kp1>\n"
  3095. " };\n"
  3096. " key <j> {\n"
  3097. " overlay1 = <kp1>, overlay7 = none,\n"
  3098. " overlay3 = <f1>, overlay4 = <f10>\n"
  3099. " };\n"
  3100. " augment key <j> { overlay4 = <f1> };\n"
  3101. " key <k> {\n"
  3102. " [k],\n"
  3103. " overlay1 = <kp2>,\n"
  3104. " overlay2 = <down>,\n"
  3105. " overlay3 = <f2>,\n"
  3106. " overlay4 = <f11>\n"
  3107. " };\n"
  3108. " key <kp1> { [KP_1], overlay2 = <j> };\n"
  3109. " key <kp1> { [KP_1], overlay1 = none, overlay2 = none, overlay3 = <f1> };\n"
  3110. " key <kp1> { [KP_1], overlay3 = none };\n"
  3111. " key <kp2> { [KP_2] };\n"
  3112. " key <left> { [Left], overlay3 = <f1>, overlay4 = <f10> };\n"
  3113. " key <down> { [Down] };\n"
  3114. " key <f1> { [F1] };\n"
  3115. " key <f2> { [F2] };\n"
  3116. " key <f10> { [F10] };\n"
  3117. " key <f11> { [F11] };\n"
  3118. " };\n"
  3119. "};",
  3120. .lenient = true,
  3121. .expected_v1_1 = NULL,
  3122. .expected_v1_2 = NULL,
  3123. .expected_v2_2 = GOLDEN_TESTS_OUTPUTS "overlays-v2-2.xkb",
  3124. /* Drop overlays 2-8 */
  3125. .expected_v2_1 = GOLDEN_TESTS_OUTPUTS "overlays-v1-2.xkb",
  3126. },
  3127. };
  3128. for (size_t t = 0; t < ARRAY_SIZE(tests); t++) {
  3129. fprintf(stderr, "------\n*** %s: #%zu ***\n", __func__, t);
  3130. const enum xkb_keymap_compile_flags flags
  3131. = TEST_KEYMAP_COMPILE_FLAGS
  3132. & ~(tests[t].lenient ? XKB_KEYMAP_COMPILE_STRICT_MODE : 0);
  3133. if (tests[t].no_output) {
  3134. struct xkb_keymap *keymap =
  3135. test_compile_buffer2(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, flags,
  3136. tests[t].keymap, strlen(tests[t].keymap));
  3137. assert(!!keymap == tests[t].compiles_v1);
  3138. xkb_keymap_unref(keymap);
  3139. keymap =
  3140. test_compile_buffer2(ctx, XKB_KEYMAP_FORMAT_TEXT_V2, flags,
  3141. tests[t].keymap, strlen(tests[t].keymap));
  3142. assert(!!keymap == tests[t].compiles_v2);
  3143. xkb_keymap_unref(keymap);
  3144. continue;
  3145. }
  3146. /* v1 → v1 */
  3147. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  3148. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  3149. compile_buffer, (void*)&flags, "v1 -> v1",
  3150. tests[t].keymap, strlen(tests[t].keymap),
  3151. tests[t].expected_v1_1, update_output_files));
  3152. /* v1 → v2 */
  3153. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  3154. XKB_KEYMAP_FORMAT_TEXT_V2,
  3155. compile_buffer, (void*)&flags, "v1 -> v2",
  3156. tests[t].keymap, strlen(tests[t].keymap),
  3157. tests[t].expected_v1_2, update_output_files));
  3158. /* v2 → v2 */
  3159. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
  3160. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  3161. compile_buffer, (void*)&flags, "v2 -> v2",
  3162. tests[t].keymap, strlen(tests[t].keymap),
  3163. tests[t].expected_v2_2, update_output_files));
  3164. /* v2 → v1 */
  3165. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
  3166. XKB_KEYMAP_FORMAT_TEXT_V1,
  3167. compile_buffer, (void*)&flags, "v2 -> v1",
  3168. tests[t].keymap, strlen(tests[t].keymap),
  3169. tests[t].expected_v2_1, update_output_files));
  3170. }
  3171. }
  3172. static void
  3173. test_extended_layout_indices(struct xkb_context *ctx,
  3174. bool update_output_files)
  3175. {
  3176. static const struct keymap_multi_versions_test_data tests[] = {
  3177. {
  3178. .keymap =
  3179. "xkb_keymap {\n"
  3180. " xkb_compat { interpret a { action = SetGroup(group=5); }; };\n"
  3181. "};",
  3182. .no_output = true,
  3183. .compiles_v1 = false,
  3184. .compiles_v2 = true,
  3185. },
  3186. {
  3187. .keymap =
  3188. "xkb_keymap {\n"
  3189. " xkb_keycodes { indicator 1 = \"a\"; };\n"
  3190. " xkb_compat { indicator \"a\" { groups=All-5; }; };\n"
  3191. "};",
  3192. .no_output = true,
  3193. .compiles_v1 = true,
  3194. .compiles_v2 = true,
  3195. },
  3196. {
  3197. .keymap =
  3198. "xkb_keymap {\n"
  3199. " xkb_keycodes { <> = 1; };\n"
  3200. " xkb_symbols { name[5] = \"5\"; };\n"
  3201. "};",
  3202. .no_output = true,
  3203. .compiles_v1 = false,
  3204. .compiles_v2 = true,
  3205. },
  3206. {
  3207. .keymap =
  3208. "xkb_keymap {\n"
  3209. " xkb_keycodes { <> = 1; };\n"
  3210. " xkb_symbols { key <> { [1], [2], [3], [4], [5] }; };\n"
  3211. "};",
  3212. .no_output = true,
  3213. .compiles_v1 = false,
  3214. .compiles_v2 = true,
  3215. },
  3216. {
  3217. .keymap =
  3218. "xkb_keymap {\n"
  3219. " xkb_keycodes {\n"
  3220. " <a> = 1;\n"
  3221. " <b> = 2;\n"
  3222. " <c> = 3;\n"
  3223. " indicator 1 = \"a\";\n"
  3224. " };\n"
  3225. " xkb_compat {\n"
  3226. " interpret a { action = SetGroup(group=5); };\n"
  3227. " indicator \"a\" { groups=All-5; };\n"
  3228. " };\n"
  3229. " xkb_symbols {\n"
  3230. " name[5] = \"G5\";\n"
  3231. " name[1] = \"G1\";\n"
  3232. " key <a> { symbols[1]=[a], symbols[5]=[Greek_alpha] };\n"
  3233. " key <b> { actions[1]=[Latchgroup(group=-5)], actions[5]=[LockGroup(group=5)], [SetGroup(group=+9)] };\n"
  3234. " key <c> { [1], [2], [3], [4], [5], [6], [7], [8], [9] };\n"
  3235. " };\n"
  3236. "};",
  3237. .expected_v1_1 = NULL,
  3238. .expected_v1_2 = NULL,
  3239. .expected_v2_2 = GOLDEN_TESTS_OUTPUTS "extended-layout-indices-v2.xkb",
  3240. .expected_v2_1 = GOLDEN_TESTS_OUTPUTS "extended-layout-indices-v1.xkb",
  3241. }
  3242. };
  3243. for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
  3244. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
  3245. if (tests[k].no_output) {
  3246. struct xkb_keymap *keymap =
  3247. test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  3248. tests[k].keymap, strlen(tests[k].keymap));
  3249. assert(!!keymap == tests[k].compiles_v1);
  3250. xkb_keymap_unref(keymap);
  3251. keymap =
  3252. test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
  3253. tests[k].keymap, strlen(tests[k].keymap));
  3254. assert(!!keymap == tests[k].compiles_v2);
  3255. xkb_keymap_unref(keymap);
  3256. continue;
  3257. }
  3258. /* v1 → v1 */
  3259. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  3260. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  3261. compile_buffer, NULL, __func__,
  3262. tests[k].keymap, strlen(tests[k].keymap),
  3263. tests[k].expected_v1_1, update_output_files));
  3264. /* v1 → v2 */
  3265. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  3266. XKB_KEYMAP_FORMAT_TEXT_V2,
  3267. compile_buffer, NULL, __func__,
  3268. tests[k].keymap, strlen(tests[k].keymap),
  3269. tests[k].expected_v1_2, update_output_files));
  3270. /* v2 → v2 */
  3271. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
  3272. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  3273. compile_buffer, NULL, __func__,
  3274. tests[k].keymap, strlen(tests[k].keymap),
  3275. tests[k].expected_v2_2, update_output_files));
  3276. /* v2 → v1 */
  3277. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V2,
  3278. XKB_KEYMAP_FORMAT_TEXT_V1,
  3279. compile_buffer, NULL, __func__,
  3280. tests[k].keymap, strlen(tests[k].keymap),
  3281. tests[k].expected_v2_1, update_output_files));
  3282. }
  3283. }
  3284. static void
  3285. test_compound_statement_errors(struct xkb_context *ctx)
  3286. {
  3287. /* Check that any error invalidates the whole compound statement */
  3288. const struct keymap_test_data tests[] = {
  3289. {
  3290. .keymap =
  3291. "xkb_keymap {\n"
  3292. " xkb_types {\n"
  3293. " type \"1\" {\n"
  3294. " modifiers[0] = Shift;\n" /* error on field */
  3295. " map[Shift] = 2;\n"
  3296. " };\n"
  3297. " };\n"
  3298. "};",
  3299. .expected = NULL
  3300. },
  3301. {
  3302. .keymap =
  3303. "xkb_keymap {\n"
  3304. " xkb_types {\n"
  3305. " type \"1\" {\n"
  3306. " modifiers = \"xxx\";\n" /* error on value */
  3307. " map[Shift] = 2;\n"
  3308. " };\n"
  3309. " };\n"
  3310. "};",
  3311. .expected = NULL
  3312. },
  3313. {
  3314. .keymap =
  3315. "xkb_keymap {\n"
  3316. " xkb_compat {\n"
  3317. " interpret a {\n"
  3318. " action[0] = NoAction();\n" /* error on field */
  3319. " repeat = true;\n"
  3320. " };\n"
  3321. " };\n"
  3322. "};",
  3323. .expected = NULL
  3324. },
  3325. {
  3326. .keymap =
  3327. "xkb_keymap {\n"
  3328. " xkb_compat {\n"
  3329. " interpret a {\n"
  3330. " action = \"xxx\";\n" /* error on value */
  3331. " repeat = true;\n"
  3332. " };\n"
  3333. " };\n"
  3334. "};",
  3335. .expected = NULL
  3336. },
  3337. };
  3338. for (unsigned int t = 0; t < ARRAY_SIZE(tests); t++) {
  3339. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, t);
  3340. assert(test_compile_output(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  3341. XKB_KEYMAP_USE_ORIGINAL_FORMAT,
  3342. compile_buffer, NULL, __func__,
  3343. tests[t].keymap, strlen(tests[t].keymap),
  3344. tests[t].expected, false));
  3345. }
  3346. }
  3347. int
  3348. main(int argc, char *argv[])
  3349. {
  3350. test_init();
  3351. bool update_output_files = false;
  3352. /* Default seed for pseudo-random generator */
  3353. unsigned int seed = (unsigned int) time(NULL);
  3354. int arg_index = 0;
  3355. while (++arg_index < argc) {
  3356. if (streq(argv[arg_index], "update")) {
  3357. /* Update files with *obtained* results */
  3358. update_output_files = true;
  3359. } else if (streq(argv[arg_index], "--seed") && arg_index + 1 < argc) {
  3360. arg_index++;
  3361. char *endp = argv[arg_index];
  3362. errno = 0;
  3363. const unsigned long raw = strtoul(argv[arg_index], &endp, 10);
  3364. if (errno || endp == argv[arg_index] || *endp != '\0' || raw > UINT_MAX) {
  3365. fprintf(stderr, "ERROR: Invalid seed: \"%s\"\n", argv[arg_index]);
  3366. exit(TEST_SETUP_FAILURE);
  3367. }
  3368. seed = (unsigned int) raw;
  3369. } else {
  3370. fprintf(stderr, "ERROR: unsupported argument: \"%s\".\n",
  3371. argv[arg_index]);
  3372. exit(EXIT_INVALID_USAGE);
  3373. }
  3374. }
  3375. /* Initialize pseudo-random generator with program arg or current time */
  3376. fprintf(stderr, "Seed for the pseudo-random generator: %u\n", seed);
  3377. srand(seed);
  3378. struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
  3379. assert(ctx);
  3380. /* Reject unsupported flags */
  3381. static const char buf[] = "xkb_keymap {};";
  3382. assert(!xkb_keymap_new_from_buffer(ctx, buf, sizeof(buf),
  3383. XKB_KEYMAP_FORMAT_TEXT_V1, -1));
  3384. assert(!xkb_keymap_new_from_buffer(ctx, buf, sizeof(buf),
  3385. XKB_KEYMAP_FORMAT_TEXT_V1, 0xffff));
  3386. /* Make sure we can't (falsely claim to) compile an empty string. */
  3387. struct xkb_keymap *keymap =
  3388. test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, "", 0);
  3389. assert(!keymap);
  3390. test_encodings(ctx);
  3391. test_floats(ctx);
  3392. test_component_syntax_error(ctx);
  3393. test_optional_components(ctx, update_output_files);
  3394. test_section_flags();
  3395. test_bidi_chars(ctx, update_output_files);
  3396. test_recursive_includes(ctx);
  3397. test_include_paths(ctx);
  3398. test_include_default_maps(update_output_files);
  3399. test_include_group_indices(ctx, update_output_files);
  3400. test_alloc_limits(ctx, update_output_files);
  3401. test_integers(ctx, update_output_files);
  3402. test_keycodes(ctx, update_output_files);
  3403. test_masks(ctx, update_output_files);
  3404. test_interpret(ctx, update_output_files);
  3405. test_group_indices_names(ctx, update_output_files);
  3406. test_level_indices_names(ctx, update_output_files);
  3407. test_multi_keysyms_actions(ctx, update_output_files);
  3408. test_key_keysyms_as_strings(ctx, update_output_files);
  3409. test_invalid_symbols_fields(ctx);
  3410. test_modifier_maps(ctx, update_output_files);
  3411. test_empty_compound_statements(ctx, update_output_files);
  3412. test_escape_sequences(ctx, update_output_files);
  3413. test_unicode_keysyms(ctx, update_output_files);
  3414. test_no_action_void_action(ctx, update_output_files);
  3415. test_prebuilt_keymap_roundtrip(ctx, update_output_files);
  3416. test_keymap_from_rules(ctx);
  3417. test_redirect_key(ctx, update_output_files);
  3418. test_unsupported_legacy_x11_actions(ctx, update_output_files);
  3419. test_overlays(ctx, update_output_files);
  3420. test_extended_layout_indices(ctx, update_output_files);
  3421. test_compound_statement_errors(ctx);
  3422. xkb_context_unref(ctx);
  3423. return EXIT_SUCCESS;
  3424. }