Debian Codegraph @

test/compose.c

raw
1 /*
2 * Copyright © 2014 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <stdlib.h>
9 #include <time.h>
10 #include <errno.h>
11 #include <locale.h>
12 #include <stdio.h>
13
14 #include "xkbcommon/xkbcommon-compose.h"
15 #include "xkbcommon/xkbcommon-keysyms.h"
16
17 #include "test.h"
18 #include "src/utf8.h"
19 #include "src/keysym.h"
20 #include "src/compose/constants.h"
21 #include "src/compose/parser.h"
22 #include "src/compose/escape.h"
23 #include "src/compose/dump.h"
24 #include "test/compose-iter.h"
25 #include "test/utils-text.h"
26
27 static const char *
28 compose_status_string(enum xkb_compose_status status)
29 {
30 switch (status) {
31 case XKB_COMPOSE_NOTHING:
32 return "nothing";
33 case XKB_COMPOSE_COMPOSING:
34 return "composing";
35 case XKB_COMPOSE_COMPOSED:
36 return "composed";
37 case XKB_COMPOSE_CANCELLED:
38 return "cancelled";
39 }
40
41 return "<invalid-status>";
42 }
43
44 static const char *
45 feed_result_string(enum xkb_compose_feed_result result)
46 {
47 switch (result) {
48 case XKB_COMPOSE_FEED_IGNORED:
49 return "ignored";
50 case XKB_COMPOSE_FEED_ACCEPTED:
51 return "accepted";
52 }
53
54 return "<invalid-result>";
55 }
56
57 /*
58 * Feed a sequence of keysyms to a fresh compose state and test the outcome.
59 *
60 * The varargs consists of lines in the following format:
61 * <input keysym> <expected feed result> <expected status> <expected string> <expected keysym>
62 * Terminated by a line consisting only of XKB_KEY_NoSymbol.
63 */
64 static bool
65 test_compose_seq_va(struct xkb_compose_table *table, va_list ap)
66 {
67 int ret;
68 struct xkb_compose_state *state;
69 char buffer[MAX(XKB_COMPOSE_MAX_STRING_SIZE, XKB_KEYSYM_NAME_MAX_SIZE)];
70
71 state = xkb_compose_state_new(table, XKB_COMPOSE_STATE_NO_FLAGS);
72 assert(state);
73
74 for (int i = 1; ; i++) {
75 xkb_keysym_t input_keysym;
76 enum xkb_compose_feed_result result, expected_result;
77 enum xkb_compose_status status, expected_status;
78 const char *expected_string;
79 xkb_keysym_t keysym, expected_keysym;
80
81 input_keysym = va_arg(ap, xkb_keysym_t);
82 if (input_keysym == XKB_KEY_NoSymbol)
83 break;
84
85 expected_result = va_arg(ap, enum xkb_compose_feed_result);
86 expected_status = va_arg(ap, enum xkb_compose_status);
87 expected_string = va_arg(ap, const char *);
88 expected_keysym = va_arg(ap, xkb_keysym_t);
89
90 result = xkb_compose_state_feed(state, input_keysym);
91
92 if (result != expected_result) {
93 fprintf(stderr, "after feeding %d keysyms:\n", i);
94 fprintf(stderr, "expected feed result: %s\n",
95 feed_result_string(expected_result));
96 fprintf(stderr, "got feed result: %s\n",
97 feed_result_string(result));
98 goto fail;
99 }
100
101 status = xkb_compose_state_get_status(state);
102 if (status != expected_status) {
103 fprintf(stderr, "after feeding %d keysyms:\n", i);
104 fprintf(stderr, "expected status: %s\n",
105 compose_status_string(expected_status));
106 fprintf(stderr, "got status: %s\n",
107 compose_status_string(status));
108 goto fail;
109 }
110
111 ret = xkb_compose_state_get_utf8(state, buffer, sizeof(buffer));
112 if (ret < 0 || (size_t) ret >= sizeof(buffer)) {
113 fprintf(stderr, "after feeding %d keysyms:\n", i);
114 fprintf(stderr, "expected string: %s\n", expected_string);
115 fprintf(stderr, "got error: %d\n", ret);
116 goto fail;
117 }
118 if (!streq(buffer, expected_string)) {
119 fprintf(stderr, "after feeding %d keysyms:\n", i);
120 fprintf(stderr, "expected string: %s\n", strempty(expected_string));
121 fprintf(stderr, "got string: %s\n", buffer);
122 goto fail;
123 }
124
125 keysym = xkb_compose_state_get_one_sym(state);
126 if (keysym != expected_keysym) {
127 fprintf(stderr, "after feeding %d keysyms:\n", i);
128 xkb_keysym_get_name(expected_keysym, buffer, sizeof(buffer));
129 fprintf(stderr, "expected keysym: %s\n", buffer);
130 xkb_keysym_get_name(keysym, buffer, sizeof(buffer));
131 fprintf(stderr, "got keysym (%#06"PRIx32"): %s\n", keysym, buffer);
132 goto fail;
133 }
134 }
135
136 xkb_compose_state_unref(state);
137 return true;
138
139 fail:
140 xkb_compose_state_unref(state);
141 return false;
142 }
143
144 static bool
145 test_compose_seq(struct xkb_compose_table *table, ...)
146 {
147 va_list ap;
148 bool ok;
149 va_start(ap, table);
150 ok = test_compose_seq_va(table, ap);
151 va_end(ap);
152 return ok;
153 }
154
155 static bool
156 test_compose_seq_buffer(struct xkb_context *ctx, const char *buffer, ...)
157 {
158 va_list ap;
159 bool ok;
160 struct xkb_compose_table *table;
161 table = xkb_compose_table_new_from_buffer(ctx, buffer, strlen(buffer), "",
162 XKB_COMPOSE_FORMAT_TEXT_V1,
163 XKB_COMPOSE_COMPILE_NO_FLAGS);
164 assert(table);
165 va_start(ap, buffer);
166 ok = test_compose_seq_va(table, ap);
167 va_end(ap);
168 xkb_compose_table_unref(table);
169 return ok;
170 }
171
172 static void
173 test_compose_utf8_bom(struct xkb_context *ctx)
174 {
175 const char buffer[] = "\xef\xbb\xbf<A> : X";
176 assert(test_compose_seq_buffer(ctx, buffer,
177 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "X", XKB_KEY_X,
178 XKB_KEY_NoSymbol));
179 }
180
181 static void
182 test_invalid_encodings(struct xkb_context *ctx)
183 {
184 struct xkb_compose_table *table;
185
186 /* ISO 8859-1 (latin1) */
187 const char iso_8859_1[] = "<A> : \"\xe1\" acute";
188 assert(!test_compose_seq_buffer(ctx, iso_8859_1,
189 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "\xc3\xa1", XKB_KEY_acute,
190 XKB_KEY_NoSymbol));
191
192 /* UTF-16LE */
193 const char utf_16_le[] =
194 "<\0A\0>\0 \0:\0 \0X\0\n\0"
195 "<\0B\0>\0 \0:\0 \0Y\0";
196 table = xkb_compose_table_new_from_buffer(ctx,
197 utf_16_le, sizeof(utf_16_le), "",
198 XKB_COMPOSE_FORMAT_TEXT_V1,
199 XKB_COMPOSE_COMPILE_NO_FLAGS);
200 assert(!table);
201
202 /* UTF-16BE */
203 const char utf_16_be[] =
204 "\0<\0A\0>\0 \0:\0 \0X\0\n"
205 "\0<\0B\0>\0 \0:\0 \0Y";
206 table = xkb_compose_table_new_from_buffer(ctx,
207 utf_16_be, sizeof(utf_16_be), "",
208 XKB_COMPOSE_FORMAT_TEXT_V1,
209 XKB_COMPOSE_COMPILE_NO_FLAGS);
210 assert(!table);
211
212 /* UTF-16BE with BOM */
213 const char utf_16_be_bom[] =
214 "\xfe\xff"
215 "\0<\0A\0>\0 \0:\0 \0X\0\n"
216 "\0<\0B\0>\0 \0:\0 \0Y";
217 table = xkb_compose_table_new_from_buffer(ctx,
218 utf_16_be_bom, sizeof(utf_16_be_bom), "",
219 XKB_COMPOSE_FORMAT_TEXT_V1,
220 XKB_COMPOSE_COMPILE_NO_FLAGS);
221 assert(!table);
222
223 /* UTF-32LE */
224 const char utf_32_le[] =
225 "<\0\0\0A\0\0\0>\0\0\0 \0\0\0:\0\0\0 \0\0\0X\0\0\0\n\0\0\0"
226 "<\0\0\0B\0\0\0>\0\0\0 \0\0\0:\0\0\0 \0\0\0Y\0\0\0";
227 table = xkb_compose_table_new_from_buffer(ctx,
228 utf_32_le, sizeof(utf_32_le), "",
229 XKB_COMPOSE_FORMAT_TEXT_V1,
230 XKB_COMPOSE_COMPILE_NO_FLAGS);
231 assert(!table);
232
233 /* UTF-32LE with BOM */
234 const char utf_32_le_bom[] =
235 "\xff\xfe\0\0"
236 "<\0\0\0A\0\0\0>\0\0\0 \0\0\0:\0\0\0 \0\0\0X\0\0\0\n\0\0\0"
237 "<\0\0\0B\0\0\0>\0\0\0 \0\0\0:\0\0\0 \0\0\0Y\0\0\0";
238 table = xkb_compose_table_new_from_buffer(ctx,
239 utf_32_le_bom, sizeof(utf_32_le_bom), "",
240 XKB_COMPOSE_FORMAT_TEXT_V1,
241 XKB_COMPOSE_COMPILE_NO_FLAGS);
242 assert(!table);
243
244 /* UTF-32BE */
245 const char utf_32_be[] =
246 "\0\0\0<\0\0\0A\0\0\0>\0\0\0 \0\0\0:\0\0\0 \0\0\0X\0\0\0\n\0\0\0"
247 "<\0\0\0B\0\0\0>\0\0\0 \0\0\0:\0\0\0 \0\0\0Y";
248 table = xkb_compose_table_new_from_buffer(ctx,
249 utf_32_be, sizeof(utf_32_be), "",
250 XKB_COMPOSE_FORMAT_TEXT_V1,
251 XKB_COMPOSE_COMPILE_NO_FLAGS);
252 assert(!table);
253 }
254
255
256 static void
257 test_seqs(struct xkb_context *ctx)
258 {
259 struct xkb_compose_table *table;
260 char *path;
261 FILE *file;
262
263 path = test_get_path("locale/en_US.UTF-8/Compose");
264 file = fopen(path, "rb");
265 assert(file);
266 free(path);
267
268 table = xkb_compose_table_new_from_file(ctx, file, "",
269 XKB_COMPOSE_FORMAT_TEXT_V1,
270 XKB_COMPOSE_COMPILE_NO_FLAGS);
271 assert(table);
272 fclose(file);
273
274 assert(test_compose_seq(table,
275 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
276 XKB_KEY_space, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "~", XKB_KEY_asciitilde,
277 XKB_KEY_NoSymbol));
278
279 assert(test_compose_seq(table,
280 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
281 XKB_KEY_space, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "~", XKB_KEY_asciitilde,
282 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
283 XKB_KEY_space, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "~", XKB_KEY_asciitilde,
284 XKB_KEY_NoSymbol));
285
286 assert(test_compose_seq(table,
287 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
288 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "~", XKB_KEY_asciitilde,
289 XKB_KEY_NoSymbol));
290
291 assert(test_compose_seq(table,
292 XKB_KEY_dead_acute, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
293 XKB_KEY_space, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "'", XKB_KEY_apostrophe,
294 XKB_KEY_Caps_Lock, XKB_COMPOSE_FEED_IGNORED, XKB_COMPOSE_COMPOSED, "'", XKB_KEY_apostrophe,
295 XKB_KEY_NoSymbol));
296
297 assert(test_compose_seq(table,
298 XKB_KEY_dead_acute, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
299 XKB_KEY_dead_acute, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "´", XKB_KEY_acute,
300 XKB_KEY_NoSymbol));
301
302 assert(test_compose_seq(table,
303 XKB_KEY_Multi_key, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
304 XKB_KEY_Shift_L, XKB_COMPOSE_FEED_IGNORED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
305 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
306 XKB_KEY_Caps_Lock, XKB_COMPOSE_FEED_IGNORED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
307 XKB_KEY_Control_L, XKB_COMPOSE_FEED_IGNORED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
308 XKB_KEY_T, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "@", XKB_KEY_at,
309 XKB_KEY_NoSymbol));
310
311 assert(test_compose_seq(table,
312 XKB_KEY_7, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
313 XKB_KEY_a, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
314 XKB_KEY_b, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
315 XKB_KEY_NoSymbol));
316
317 assert(test_compose_seq(table,
318 XKB_KEY_Multi_key, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
319 XKB_KEY_apostrophe, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
320 XKB_KEY_7, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_CANCELLED, "", XKB_KEY_NoSymbol,
321 XKB_KEY_7, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
322 XKB_KEY_Caps_Lock, XKB_COMPOSE_FEED_IGNORED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
323 XKB_KEY_NoSymbol));
324
325 xkb_compose_table_unref(table);
326
327 /* Make sure one-keysym sequences work. */
328 assert(test_compose_seq_buffer(ctx,
329 "<A> : \"foo\" X \n"
330 "<B> <A> : \"baz\" Y \n",
331 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "foo", XKB_KEY_X,
332 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "foo", XKB_KEY_X,
333 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
334 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
335 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "baz", XKB_KEY_Y,
336 XKB_KEY_NoSymbol));
337
338 /* No sequences at all. */
339 assert(test_compose_seq_buffer(ctx,
340 "",
341 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
342 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
343 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
344 XKB_KEY_Multi_key, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
345 XKB_KEY_dead_acute, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
346 XKB_KEY_NoSymbol));
347
348 /* Only keysym - string derived from keysym. */
349 assert(test_compose_seq_buffer(ctx,
350 "<A> <B> : X \n"
351 "<B> <A> : dollar \n"
352 "<C> : dead_acute \n",
353 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
354 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "X", XKB_KEY_X,
355 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
356 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "$", XKB_KEY_dollar,
357 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "", XKB_KEY_dead_acute,
358 XKB_KEY_NoSymbol));
359
360 /* Make sure a cancelling keysym doesn't start a new sequence. */
361 assert(test_compose_seq_buffer(ctx,
362 "<A> <B> : X \n"
363 "<C> <D> : Y \n",
364 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
365 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_CANCELLED, "", XKB_KEY_NoSymbol,
366 XKB_KEY_D, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
367 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
368 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_CANCELLED, "", XKB_KEY_NoSymbol,
369 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
370 XKB_KEY_D, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "Y", XKB_KEY_Y,
371 XKB_KEY_NoSymbol));
372 }
373
374 static void
375 test_conflicting(struct xkb_context *ctx)
376 {
377 // new is prefix of old
378 assert(test_compose_seq_buffer(ctx,
379 "<A> <B> <C> : \"foo\" A \n"
380 "<A> <B> : \"bar\" B \n",
381 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
382 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "bar", XKB_KEY_B,
383 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
384 XKB_KEY_NoSymbol));
385
386 // old is a prefix of new
387 assert(test_compose_seq_buffer(ctx,
388 "<A> <B> : \"bar\" B \n"
389 "<A> <B> <C> : \"foo\" A \n",
390 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
391 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
392 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "foo", XKB_KEY_A,
393 XKB_KEY_NoSymbol));
394
395 // new duplicate of old
396 assert(test_compose_seq_buffer(ctx,
397 "<A> <B> : \"bar\" B \n"
398 "<A> <B> : \"bar\" B \n",
399 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
400 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "bar", XKB_KEY_B,
401 XKB_KEY_C, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_NOTHING, "", XKB_KEY_NoSymbol,
402 XKB_KEY_NoSymbol));
403
404 // new same length as old #1
405 assert(test_compose_seq_buffer(ctx,
406 "<A> <B> : \"foo\" A \n"
407 "<A> <B> : \"bar\" B \n",
408 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
409 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "bar", XKB_KEY_B,
410 XKB_KEY_NoSymbol));
411
412 // new same length as old #2
413 assert(test_compose_seq_buffer(ctx,
414 "<A> <B> : \"foo\" A \n"
415 "<A> <B> : \"foo\" B \n",
416 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
417 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "foo", XKB_KEY_B,
418 XKB_KEY_NoSymbol));
419
420 // new same length as old #3: overwritable string: do not allocate
421 assert(test_compose_seq_buffer(ctx,
422 "<A> <B> : \"foo\" A \n"
423 "<A> <B> : \"qu\" A \n",
424 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
425 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "qu", XKB_KEY_A,
426 XKB_KEY_NoSymbol));
427
428 // new same length as old #4: no-overwritable string: allocate
429 assert(test_compose_seq_buffer(ctx,
430 "<A> <B> : \"foo\" A \n"
431 "<A> <B> : \"quux\" A \n",
432 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
433 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "quux", XKB_KEY_A,
434 XKB_KEY_NoSymbol));
435
436 // new same length as old #5: ensure string is reset
437 assert(test_compose_seq_buffer(ctx,
438 "<A> <B> : \"foo\" A \n"
439 "<A> <B> : B \n",
440 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
441 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "B", XKB_KEY_B,
442 XKB_KEY_NoSymbol));
443
444 // new same length as old #6: ensure keysym is reset
445 assert(test_compose_seq_buffer(ctx,
446 "<A> <B> : \"foo\" A \n"
447 "<A> <B> : \"bar\" \n",
448 XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
449 XKB_KEY_B, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "bar", XKB_KEY_NoSymbol,
450 XKB_KEY_NoSymbol));
451 }
452
453 static void
454 test_state(struct xkb_context *ctx)
455 {
456 struct xkb_compose_table *table;
457 struct xkb_compose_state *state;
458 char *path;
459 FILE *file;
460
461 path = test_get_path("locale/en_US.UTF-8/Compose");
462 file = fopen(path, "rb");
463 assert(file);
464 free(path);
465
466 table = xkb_compose_table_new_from_file(ctx, file, "",
467 XKB_COMPOSE_FORMAT_TEXT_V1,
468 XKB_COMPOSE_COMPILE_NO_FLAGS);
469 assert(table);
470 fclose(file);
471
472 state = xkb_compose_state_new(table, XKB_COMPOSE_STATE_NO_FLAGS);
473 assert(state);
474
475 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_NOTHING);
476 xkb_compose_state_reset(state);
477 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_NOTHING);
478 xkb_compose_state_feed(state, XKB_KEY_NoSymbol);
479 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_NOTHING);
480 xkb_compose_state_feed(state, XKB_KEY_Multi_key);
481 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_COMPOSING);
482 xkb_compose_state_reset(state);
483 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_NOTHING);
484 xkb_compose_state_feed(state, XKB_KEY_Multi_key);
485 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_COMPOSING);
486 xkb_compose_state_feed(state, XKB_KEY_Multi_key);
487 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_CANCELLED);
488 xkb_compose_state_feed(state, XKB_KEY_Multi_key);
489 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_COMPOSING);
490 xkb_compose_state_feed(state, XKB_KEY_Multi_key);
491 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_CANCELLED);
492 xkb_compose_state_reset(state);
493 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_NOTHING);
494 xkb_compose_state_feed(state, XKB_KEY_dead_acute);
495 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_COMPOSING);
496 xkb_compose_state_feed(state, XKB_KEY_A);
497 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_COMPOSED);
498 xkb_compose_state_reset(state);
499 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_NOTHING);
500 xkb_compose_state_feed(state, XKB_KEY_dead_acute);
501 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_COMPOSING);
502 xkb_compose_state_feed(state, XKB_KEY_A);
503 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_COMPOSED);
504 xkb_compose_state_reset(state);
505 xkb_compose_state_feed(state, XKB_KEY_NoSymbol);
506 assert(xkb_compose_state_get_status(state) == XKB_COMPOSE_NOTHING);
507
508 xkb_compose_state_unref(state);
509 xkb_compose_table_unref(table);
510 }
511
512 static void
513 test_XCOMPOSEFILE(struct xkb_context *ctx)
514 {
515 struct xkb_compose_table *table;
516 char *path;
517
518 /* Error: directory */
519 path = test_get_path("locale/en_US.UTF-8");
520 setenv("XCOMPOSEFILE", path, 1);
521 free(path);
522
523 table = xkb_compose_table_new_from_locale(ctx, "blabla",
524 XKB_COMPOSE_COMPILE_NO_FLAGS);
525 assert_printf(errno != ENODEV && errno != EISDIR,
526 "Should not be an error from `map_file`\n");
527 assert(!table);
528
529 /* OK: regular file */
530 path = test_get_path("locale/en_US.UTF-8/Compose");
531 setenv("XCOMPOSEFILE", path, 1);
532 free(path);
533
534 table = xkb_compose_table_new_from_locale(ctx, "blabla",
535 XKB_COMPOSE_COMPILE_NO_FLAGS);
536 assert(table);
537
538 unsetenv("XCOMPOSEFILE");
539
540 assert(test_compose_seq(table,
541 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
542 XKB_KEY_space, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "~", XKB_KEY_asciitilde,
543 XKB_KEY_NoSymbol));
544
545 xkb_compose_table_unref(table);
546 }
547
548 static void
549 test_from_locale(struct xkb_context *ctx)
550 {
551 struct xkb_compose_table *table;
552 char *path;
553
554 path = test_get_path("locale");
555 setenv("XLOCALEDIR", path, 1);
556 free(path);
557
558 /* Direct directory name match. */
559 table = xkb_compose_table_new_from_locale(ctx, "en_US.UTF-8",
560 XKB_COMPOSE_COMPILE_NO_FLAGS);
561 assert(table);
562 xkb_compose_table_unref(table);
563
564 /* Direct locale name match. */
565 table = xkb_compose_table_new_from_locale(ctx, "C.UTF-8",
566 XKB_COMPOSE_COMPILE_NO_FLAGS);
567 assert(table);
568 xkb_compose_table_unref(table);
569
570 /* Alias. */
571 table = xkb_compose_table_new_from_locale(ctx, "univ.utf8",
572 XKB_COMPOSE_COMPILE_NO_FLAGS);
573 assert(table);
574 xkb_compose_table_unref(table);
575
576 /* Special case - C. */
577 table = xkb_compose_table_new_from_locale(ctx, "C",
578 XKB_COMPOSE_COMPILE_NO_FLAGS);
579 assert(table);
580 xkb_compose_table_unref(table);
581
582 #ifdef HAVE_NEWLOCALE
583 /* Test custom locale. Require installing it system-wide */
584 /* NOTE: Keep the locale name in sync with the localedef call in our CI */
585 static const char * const custom_locale = "xx_YY.UTF-8";
586 locale_t loc = newlocale(LC_ALL, custom_locale, (locale_t) 0);
587 table = xkb_compose_table_new_from_locale(ctx, custom_locale,
588 XKB_COMPOSE_COMPILE_NO_FLAGS);
589 if (loc == (locale_t) 0) {
590 /* Locale is not installed: no fallback */
591 assert(!table);
592 } else {
593 /* Locale is installed */
594 freelocale(loc);
595 assert(table);
596 xkb_compose_table_unref(table);
597 }
598 #endif
599
600 /* Bogus - not found. */
601 table = xkb_compose_table_new_from_locale(ctx, "blabla",
602 XKB_COMPOSE_COMPILE_NO_FLAGS);
603 assert(!table);
604
605 unsetenv("XLOCALEDIR");
606 }
607
608
609 static void
610 test_modifier_syntax(struct xkb_context *ctx)
611 {
612 const char *table_string;
613
614 /* We don't do anything with the modifiers, but make sure we can parse
615 * them. */
616
617 assert(test_compose_seq_buffer(ctx,
618 "None <A> : X \n"
619 "Shift <B> : Y \n"
620 "Ctrl <C> : Y \n"
621 "Alt <D> : Y \n"
622 "Caps <E> : Y \n"
623 "Lock <F> : Y \n"
624 "Shift Ctrl <G> : Y \n"
625 "~Shift <H> : Y \n"
626 "~Shift Ctrl <I> : Y \n"
627 "Shift ~Ctrl <J> : Y \n"
628 "Shift ~Ctrl ~Alt <K> : Y \n"
629 "! Shift <B> : Y \n"
630 "! Ctrl <C> : Y \n"
631 "! Alt <D> : Y \n"
632 "! Caps <E> : Y \n"
633 "! Lock <F> : Y \n"
634 "! Shift Ctrl <G> : Y \n"
635 "! ~Shift <H> : Y \n"
636 "! ~Shift Ctrl <I> : Y \n"
637 "! Shift ~Ctrl <J> : Y \n"
638 "! Shift ~Ctrl ~Alt <K> : Y \n"
639 "<L> ! Shift <M> : Y \n"
640 "None <N> ! Shift <O> : Y \n"
641 "None <P> ! Shift <Q> : Y \n",
642 XKB_KEY_NoSymbol));
643
644 fprintf(stderr, "<START bad input string>\n");
645 table_string =
646 "! None <A> : X \n"
647 "! Foo <B> : X \n"
648 "None ! Shift <C> : X \n"
649 "! ! <D> : X \n"
650 "! ~ <E> : X \n"
651 "! ! <F> : X \n"
652 "! Ctrl ! Ctrl <G> : X \n"
653 "<H> ! : X \n"
654 "<I> None : X \n"
655 "None None <J> : X \n"
656 "<K> : !Shift X \n";
657 assert(!xkb_compose_table_new_from_buffer(ctx, table_string,
658 strlen(table_string), "C",
659 XKB_COMPOSE_FORMAT_TEXT_V1,
660 XKB_COMPOSE_COMPILE_NO_FLAGS));
661 fprintf(stderr, "<END bad input string>\n");
662 }
663
664 static void
665 test_include(struct xkb_context *ctx)
666 {
667 char *path, *table_string;
668
669 path = test_get_path("locale/en_US.UTF-8/Compose");
670 assert(path);
671
672 /* We don't have a mechanism to change the include paths like we
673 * have for keymaps. So we must include the full path. */
674 table_string = asprintf_safe("<dead_tilde> <space> : \"foo\" X\n"
675 "include \"%s\"\n"
676 "<dead_tilde> <dead_tilde> : \"bar\" Y\n", path);
677 assert(table_string);
678
679 assert(test_compose_seq_buffer(ctx, table_string,
680 /* No conflict. */
681 XKB_KEY_dead_acute, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
682 XKB_KEY_dead_acute, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "´", XKB_KEY_acute,
683
684 /* Comes before - doesn't override. */
685 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
686 XKB_KEY_space, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "~", XKB_KEY_asciitilde,
687
688 /* Comes after - does override. */
689 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
690 XKB_KEY_dead_tilde, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "bar", XKB_KEY_Y,
691
692 XKB_KEY_NoSymbol));
693
694 free(path);
695 free(table_string);
696 }
697
698 static void
699 test_override(struct xkb_context *ctx)
700 {
701 const char *table_string = "<dead_circumflex> <dead_circumflex> : \"foo\" X\n"
702 "<dead_circumflex> <e> : \"bar\" Y\n"
703 "<dead_circumflex> <dead_circumflex> <e> : \"baz\" Z\n";
704
705 assert(test_compose_seq_buffer(ctx, table_string,
706 /* Comes after - does override. */
707 XKB_KEY_dead_circumflex, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
708 XKB_KEY_dead_circumflex, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
709 XKB_KEY_e, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "baz", XKB_KEY_Z,
710
711 /* Override does not affect sibling nodes */
712 XKB_KEY_dead_circumflex, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
713 XKB_KEY_e, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "bar", XKB_KEY_Y,
714
715 XKB_KEY_NoSymbol));
716 }
717
718 static bool
719 test_eq_entry_va(struct xkb_compose_table_entry *entry, xkb_keysym_t keysym_ref, const char *utf8_ref, va_list ap)
720 {
721 assert (entry != NULL);
722
723 assert (xkb_compose_table_entry_keysym(entry) == keysym_ref);
724
725 const char *utf8 = xkb_compose_table_entry_utf8(entry);
726 assert (utf8 && utf8_ref && strcmp(utf8, utf8_ref) == 0);
727
728 size_t nsyms;
729 const xkb_keysym_t *sequence = xkb_compose_table_entry_sequence(entry, &nsyms);
730
731 xkb_keysym_t keysym;
732 for (unsigned int k = 0; ; k++) {
733 keysym = va_arg(ap, xkb_keysym_t);
734 if (keysym == XKB_KEY_NoSymbol) {
735 return (k == nsyms - 1);
736 }
737 assert (k < nsyms);
738 assert (keysym == sequence[k]);
739 }
740 }
741
742 static bool
743 test_eq_entry(struct xkb_compose_table_entry *entry, xkb_keysym_t keysym, const char *utf8, ...)
744 {
745 va_list ap;
746 bool ok;
747 va_start(ap, utf8);
748 ok = test_eq_entry_va(entry, keysym, utf8, ap);
749 va_end(ap);
750 return ok;
751 }
752
753 static bool
754 test_eq_entries(struct xkb_compose_table_entry *entry1, struct xkb_compose_table_entry *entry2)
755 {
756 if (!entry1 || !entry2)
757 goto error;
758 bool ok = true;
759 if (entry1->keysym != entry2->keysym ||
760 !streq_null(entry1->utf8, entry2->utf8) ||
761 entry1->sequence_length != entry2->sequence_length)
762 ok = false;
763 for (size_t k = 0; k < entry1->sequence_length; k++) {
764 if (entry1->sequence[k] != entry2->sequence[k])
765 ok = false;
766 }
767 if (ok)
768 return true;
769 error:
770 #define print_entry(msg, entry) \
771 fprintf(stderr, msg); \
772 if (entry) \
773 print_compose_table_entry(stderr, entry); \
774 else \
775 fprintf(stderr, "\n");
776 print_entry("Expected: ", entry1);
777 print_entry("Got: ", entry2);
778 #undef print_entry
779 return false;
780 }
781
782 static void
783 compose_traverse_fn(struct xkb_compose_table_entry *entry_ref, void *data)
784 {
785 struct xkb_compose_table_iterator *iter = (struct xkb_compose_table_iterator *)data;
786 struct xkb_compose_table_entry *entry = xkb_compose_table_iterator_next(iter);
787 assert(test_eq_entries(entry_ref, entry));
788 }
789
790 static void
791 test_traverse(struct xkb_context *ctx, size_t quickcheck_loops)
792 {
793 struct xkb_compose_table *table;
794 struct xkb_compose_table_iterator *iter;
795
796 /* Empty table */
797 table = xkb_compose_table_new_from_buffer(ctx, "", 0, "",
798 XKB_COMPOSE_FORMAT_TEXT_V1,
799 XKB_COMPOSE_COMPILE_NO_FLAGS);
800 assert(table);
801 iter = xkb_compose_table_iterator_new(table);
802 assert (xkb_compose_table_iterator_next(iter) == NULL);
803 xkb_compose_table_iterator_free(iter);
804 xkb_compose_table_unref(table);
805
806 /* Non-empty table */
807 const char *buffer = "<dead_circumflex> <dead_circumflex> : \"foo\" X\n"
808 "<Ahook> <x> : \"foobar\"\n"
809 "<Multi_key> <o> <e> : oe\n"
810 "<dead_circumflex> <e> : \"bar\" Y\n"
811 "<Multi_key> <a> <e> : \"æ\" ae\n"
812 "<dead_circumflex> <a> : \"baz\" Z\n"
813 "<dead_acute> <e> : \"é\" eacute\n"
814 "<Multi_key> <a> <a> <c>: \"aac\"\n"
815 "<Multi_key> <a> <a> <b>: \"aab\"\n"
816 "<Multi_key> <a> <a> <a>: \"aaa\"\n";
817
818 table = xkb_compose_table_new_from_buffer(ctx, buffer, strlen(buffer), "",
819 XKB_COMPOSE_FORMAT_TEXT_V1,
820 XKB_COMPOSE_COMPILE_NO_FLAGS);
821 assert(table);
822
823 iter = xkb_compose_table_iterator_new(table);
824
825 test_eq_entry(xkb_compose_table_iterator_next(iter),
826 XKB_KEY_eacute, "é",
827 XKB_KEY_dead_acute, XKB_KEY_e, XKB_KEY_NoSymbol);
828
829 test_eq_entry(xkb_compose_table_iterator_next(iter),
830 XKB_KEY_Z, "baz",
831 XKB_KEY_dead_circumflex, XKB_KEY_a, XKB_KEY_NoSymbol);
832
833 test_eq_entry(xkb_compose_table_iterator_next(iter),
834 XKB_KEY_Y, "bar",
835 XKB_KEY_dead_circumflex, XKB_KEY_e, XKB_KEY_NoSymbol);
836
837 test_eq_entry(xkb_compose_table_iterator_next(iter),
838 XKB_KEY_X, "foo",
839 XKB_KEY_dead_circumflex, XKB_KEY_dead_circumflex, XKB_KEY_NoSymbol);
840
841 test_eq_entry(xkb_compose_table_iterator_next(iter),
842 XKB_KEY_NoSymbol, "aaa",
843 XKB_KEY_Multi_key, XKB_KEY_a, XKB_KEY_a, XKB_KEY_a, XKB_KEY_NoSymbol);
844
845 test_eq_entry(xkb_compose_table_iterator_next(iter),
846 XKB_KEY_NoSymbol, "aab",
847 XKB_KEY_Multi_key, XKB_KEY_a, XKB_KEY_a, XKB_KEY_b, XKB_KEY_NoSymbol);
848
849 test_eq_entry(xkb_compose_table_iterator_next(iter),
850 XKB_KEY_NoSymbol, "aac",
851 XKB_KEY_Multi_key, XKB_KEY_a, XKB_KEY_a, XKB_KEY_c, XKB_KEY_NoSymbol);
852
853 test_eq_entry(xkb_compose_table_iterator_next(iter),
854 XKB_KEY_ae, "æ",
855 XKB_KEY_Multi_key, XKB_KEY_a, XKB_KEY_e, XKB_KEY_NoSymbol);
856
857 test_eq_entry(xkb_compose_table_iterator_next(iter),
858 XKB_KEY_oe, "",
859 XKB_KEY_Multi_key, XKB_KEY_o, XKB_KEY_e, XKB_KEY_NoSymbol);
860
861 test_eq_entry(xkb_compose_table_iterator_next(iter),
862 XKB_KEY_NoSymbol, "foobar",
863 XKB_KEY_Ahook, XKB_KEY_x, XKB_KEY_NoSymbol);
864
865 assert (xkb_compose_table_iterator_next(iter) == NULL);
866
867 xkb_compose_table_iterator_free(iter);
868 xkb_compose_table_unref(table);
869
870 /* QuickCheck: shuffle compose file lines and compare against
871 * reference implementation */
872 char *input = test_read_file("locale/en_US.UTF-8/Compose");
873 assert(input);
874 struct text_line lines[6000];
875 size_t input_length = strlen(input);
876 size_t lines_count = split_lines(input, input_length, lines, ARRAY_SIZE(lines));
877 /* Note: we may add additional new line char */
878 char *shuffled = calloc(input_length + 1, sizeof(char));
879 assert(shuffled);
880 for (size_t k = 0; k < quickcheck_loops; k++) {
881 size_t shuffled_length = shuffle_lines(lines, lines_count, shuffled);
882 table = xkb_compose_table_new_from_buffer(ctx, shuffled, shuffled_length, "",
883 XKB_COMPOSE_FORMAT_TEXT_V1,
884 XKB_COMPOSE_COMPILE_NO_FLAGS);
885 assert(table);
886
887 iter = xkb_compose_table_iterator_new(table);
888 assert(iter);
889 xkb_compose_table_for_each(table, compose_traverse_fn, iter);
890 assert(xkb_compose_table_iterator_next(iter) == NULL);
891 xkb_compose_table_iterator_free(iter);
892
893 xkb_compose_table_unref(table);
894 }
895 free(shuffled);
896 free(input);
897 }
898
899 static void
900 test_string_length(struct xkb_context *ctx)
901 {
902 // Invalid: empty string
903 const char table_string_1[] = "<a> <b> : \"\" X\n";
904 assert(test_compose_seq_buffer(ctx, table_string_1,
905 XKB_KEY_a, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
906 XKB_KEY_b, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "", XKB_KEY_X,
907 XKB_KEY_NoSymbol));
908
909 char long_string[XKB_COMPOSE_MAX_STRING_SIZE] = { 0 };
910 memset(long_string, 0x61, XKB_COMPOSE_MAX_STRING_SIZE - 1);
911 char table_string_2[XKB_COMPOSE_MAX_STRING_SIZE + sizeof(table_string_1) - 1];
912 assert(snprintf_safe(table_string_2, sizeof(table_string_2),
913 "<a> <b> : \"%s\" X\n", long_string));
914 assert(test_compose_seq_buffer(ctx, table_string_2,
915 XKB_KEY_a, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
916 XKB_KEY_b, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, long_string, XKB_KEY_X,
917 XKB_KEY_NoSymbol));
918 }
919
920 static void
921 test_decode_escape_sequences(struct xkb_context *ctx)
922 {
923 /* The following escape sequences should be ignored:
924 * • \401 overflows
925 * • \0 and \x0 produce NULL
926 */
927 const char table_string_1[] = "<o> <e> : \"\\401f\\x0o\\0o\" X\n";
928
929 assert(test_compose_seq_buffer(ctx, table_string_1,
930 XKB_KEY_o, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSING, "", XKB_KEY_NoSymbol,
931 XKB_KEY_e, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "foo", XKB_KEY_X,
932 XKB_KEY_NoSymbol));
933
934 /* Test various cases */
935 const char table_string_2[] =
936 "<a> : \"\\x0abcg\\\"x\" A\n" /* hexadecimal sequence has max 2 chars */
937 "<b> : \"éxyz\" B\n" /* non-ASCII (2 bytes) */
938 "<c> : \"€xyz\" C\n" /* non-ASCII (3 bytes) */
939 "<d> : \"✨xyz\" D\n" /* non-ASCII (4 bytes) */
940 "<e> : \"✨\\x0aé\\x0a€x\\\"\" E\n"
941 "<f> : \"\" F\n";
942
943 assert(test_compose_seq_buffer(ctx, table_string_2,
944 XKB_KEY_a, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "\x0a""bcg\"x", XKB_KEY_A,
945 XKB_KEY_b, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "éxyz", XKB_KEY_B,
946 XKB_KEY_c, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "€xyz", XKB_KEY_C,
947 XKB_KEY_d, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "✨xyz", XKB_KEY_D,
948 XKB_KEY_e, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "✨\x0aé\x0a€x\"", XKB_KEY_E,
949 XKB_KEY_f, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "", XKB_KEY_F,
950 XKB_KEY_NoSymbol));
951 }
952
953 static uint32_t
954 random_non_null_unicode_char(bool ascii)
955 {
956 if (ascii)
957 return 0x01 + (rand() % 0x80);
958 switch (rand() % 5) {
959 case 0:
960 /* U+0080..U+07FF: 2 bytes in UTF-8 */
961 return 0x80 + (rand() % 0x800);
962 case 1:
963 /* U+0800..U+FFFF: 3 bytes in UTF-8 */
964 return 0x800 + (rand() % 0x10000);
965 case 2:
966 /* U+10000..U+10FFFF: 4 bytes in UTF-8 */
967 return 0x10000 + (rand() % 0x110000);
968 default:
969 /* NOTE: Higher probability for ASCII */
970 /* U+0001..U+007F: 1 byte in UTF-8 */
971 return 0x01 + (rand() % 0x80);
972 }
973 }
974
975 static void
976 test_encode_escape_sequences(struct xkb_context *ctx)
977 {
978 char *escaped;
979
980 /* Test empty string */
981 escaped = escape_utf8_string_literal("");
982 assert_streq_not_null("Empty string", "", escaped);
983 free(escaped);
984
985 /* Test specific ASCII characters: ", \ */
986 escaped = escape_utf8_string_literal("\"\\");
987 assert_streq_not_null("Quote and backslash", "\\\"\\\\", escaped);
988 free(escaped);
989
990 /* Test round-trip of random strings */
991 # define SAMPLE_SIZE 1000
992 # define MIN_CODE_POINT 0x0001
993 # define MAX_CODE_POINTS_COUNT 15
994 char buf[1 + MAX_CODE_POINTS_COUNT * 4];
995 for (int ascii = 1; ascii >= 0; ascii--) {
996 for (size_t s = 0; s < SAMPLE_SIZE; s++) {
997 memset(buf, 0xab, sizeof(buf));
998 /* Create the string */
999 size_t length = 1 + (rand() % MAX_CODE_POINTS_COUNT);
1000 size_t c = 0;
1001 for (size_t idx = 0; idx < length; idx++) {
1002 int nbytes;
1003 /* Get a random Unicode code point and encode it in UTF-8 */
1004 do {
1005 const uint32_t cp = random_non_null_unicode_char(ascii);
1006 nbytes = utf32_to_utf8(cp, &buf[c]);
1007 } while (!nbytes); /* Handle invalid code point in UTF-8 */
1008 c += nbytes - 1;
1009 assert(c <= sizeof(buf) - 1);
1010 }
1011 assert_printf(buf[c] == '\0', "NULL-terminated string\n");
1012 assert_printf(strlen(buf) == c, "Contains no NULL char\n");
1013 assert_printf(is_valid_utf8(buf, c),
1014 "Invalid input UTF-8 string: \"%s\"\n", buf);
1015 /* Escape the string */
1016 escaped = escape_utf8_string_literal(buf);
1017 if (!escaped)
1018 break;
1019 assert_printf(is_valid_utf8(escaped, strlen(escaped)),
1020 "Invalid input UTF-8 string: %s\n", escaped);
1021 char *string_literal = asprintf_safe("\"%s\"", escaped);
1022 if (!string_literal) {
1023 free(escaped);
1024 break;
1025 }
1026 /* Unescape the string */
1027 char *unescaped = parse_string_literal(ctx, string_literal);
1028 assert_streq_not_null("Escaped string", buf, unescaped);
1029 free(unescaped);
1030 free(string_literal);
1031 free(escaped);
1032 }
1033 }
1034 # undef SAMPLE_SIZE
1035 # undef MIN_CODE_POINT
1036 # undef MAX_CODE_POINTS_COUNT
1037 }
1038
1039 /* Roundtrip check: check that a table parsed from a file and the table parsed
1040 * from the dump of the previous table are identical */
1041 static void
1042 test_roundtrip(struct xkb_context *ctx)
1043 {
1044 /* TODO: add support for systems without open_memstream */
1045 #if HAVE_OPEN_MEMSTREAM
1046 bool ok = false;
1047
1048 /* Parse reference file */
1049 char *input = test_read_file("locale/en_US.UTF-8/Compose");
1050 assert(input);
1051 size_t input_length = strlen(input);
1052 struct xkb_compose_table *ref_table = xkb_compose_table_new_from_buffer(
1053 ctx, input, input_length, "",
1054 XKB_COMPOSE_FORMAT_TEXT_V1,
1055 XKB_COMPOSE_COMPILE_NO_FLAGS
1056 );
1057 free(input);
1058 assert(ref_table);
1059
1060 /* Dump reference Compose table */
1061 char *output;
1062 size_t output_length = 0;
1063 FILE *output_file = open_memstream(&output, &output_length);
1064 assert(output_file);
1065
1066 ok = xkb_compose_table_dump(output_file, ref_table);
1067 fclose(output_file);
1068 assert(input);
1069
1070 if (!ok) {
1071 free(output);
1072 xkb_compose_table_unref(ref_table);
1073 exit(TEST_SETUP_FAILURE);
1074 }
1075
1076 /* Parse dumped table */
1077 struct xkb_compose_table *table = xkb_compose_table_new_from_buffer(
1078 ctx, output, output_length, "",
1079 XKB_COMPOSE_FORMAT_TEXT_V1,
1080 XKB_COMPOSE_COMPILE_NO_FLAGS
1081 );
1082 free(output);
1083 assert(table);
1084
1085 /* Check roundtrip by comparing table entries */
1086 struct xkb_compose_table_iterator *iter = xkb_compose_table_iterator_new(table);
1087 xkb_compose_table_for_each(ref_table, compose_traverse_fn, iter);
1088 assert(xkb_compose_table_iterator_next(iter) == NULL);
1089
1090 xkb_compose_table_iterator_free(iter);
1091 xkb_compose_table_unref(table);
1092 xkb_compose_table_unref(ref_table);
1093 #endif
1094 }
1095
1096 /* CLI positional arguments:
1097 * 1. Seed for the pseudo-random generator:
1098 * - Leave it unset or set it to “-” to use current time.
1099 * - Use an integer to set it explicitly.
1100 * 2. Number of quickcheck loops:
1101 * - Leave it unset to use the default. It depends if the `RUNNING_VALGRIND`
1102 * environment variable is set.
1103 * - Use an integer to set it explicitly.
1104 */
1105 int
1106 main(int argc, char *argv[])
1107 {
1108 struct xkb_context *ctx;
1109
1110 test_init();
1111
1112 ctx = test_get_context(CONTEXT_NO_FLAG);
1113 assert(ctx);
1114
1115 /* Initialize pseudo-random generator with program arg or current time */
1116 unsigned int seed;
1117 if (argc >= 2 && !streq(argv[1], "-")) {
1118 seed = (unsigned int) atoi(argv[1]);
1119 } else {
1120 seed = (unsigned int) time(NULL);
1121 }
1122 fprintf(stderr, "Seed for the pseudo-random generator: %u\n", seed);
1123 srand(seed);
1124
1125 /* Determine number of loops for quickchecks */
1126 size_t quickcheck_loops = 50; /* Default */
1127 if (argc > 2) {
1128 /* From command-line */
1129 quickcheck_loops = (size_t)atoi(argv[2]);
1130 } else if (getenv("RUNNING_VALGRIND") != NULL) {
1131 /* Reduce if running Valgrind */
1132 quickcheck_loops = quickcheck_loops / 20;
1133 }
1134
1135 /*
1136 * Ensure no environment variables but “top_srcdir” is set. This ensures
1137 * that user Compose file paths are unset before the tests and set
1138 * explicitly when necessary.
1139 */
1140 #ifdef __linux__
1141 const char *srcdir = getenv("top_srcdir");
1142 clearenv();
1143 if (srcdir)
1144 setenv("top_srcdir", srcdir, 1);
1145 #else
1146 unsetenv("XCOMPOSEFILE");
1147 unsetenv("XDG_CONFIG_HOME");
1148 unsetenv("HOME");
1149 unsetenv("XLOCALEDIR");
1150 #endif
1151
1152 test_compose_utf8_bom(ctx);
1153 test_invalid_encodings(ctx);
1154 test_seqs(ctx);
1155 test_conflicting(ctx);
1156 test_XCOMPOSEFILE(ctx);
1157 test_from_locale(ctx);
1158 test_state(ctx);
1159 test_modifier_syntax(ctx);
1160 test_include(ctx);
1161 test_override(ctx);
1162 test_traverse(ctx, quickcheck_loops);
1163 test_string_length(ctx);
1164 test_decode_escape_sequences(ctx);
1165 test_encode_escape_sequences(ctx);
1166 test_roundtrip(ctx);
1167
1168 xkb_context_unref(ctx);
1169 return EXIT_SUCCESS;
1170 }