Debian Codegraph @

test/common.c

raw
1 /*
2 * Copyright © 2009 Dan Nicholson <[email protected]>
3 * Copyright © 2012 Intel Corporation
4 * Copyright © 2012 Ran Benita <[email protected]>
5 * SPDX-License-Identifier: MIT-open-group
6 *
7 * Author: Dan Nicholson <[email protected]>
8 * Daniel Stone <[email protected]>
9 * Ran Benita <[email protected]>
10 */
11
12 #include "config.h"
13
14 #include <stdlib.h>
15 #include <limits.h>
16 #include <locale.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #ifdef _WIN32
23 #include <io.h>
24 #include <windows.h>
25 #else
26 #include <unistd.h>
27 #include <termios.h>
28 #endif
29
30 #include "xkbcommon/xkbcommon.h"
31
32 #include "darray.h"
33 #include "keymap.h"
34 #include "test.h"
35 #include "utils.h"
36 #include "utils-paths.h"
37 #include "src/keysym.h"
38 #include "src/xkbcomp/rules.h"
39 #include "src/utils-numbers.h"
40
41 #include "tools/tools-common.h"
42
43
44 /* Setup test */
45 void
46 test_init(void)
47 {
48 /* Make stdout always unbuffered, to ensure we always get it entirely */
49 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
50
51 /* Enable to use another locale than C or en_US, so we can catch
52 * locale-specific bugs */
53 setlocale(LC_ALL, "");
54 }
55
56 void
57 print_detailed_state(struct xkb_state *state)
58 {
59 fprintf(stderr,
60 " Layout: base: %"PRId32", latched: %"PRId32", locked: %"PRId32", "
61 "effective: %"PRIu32"\n",
62 xkb_state_serialize_layout(state, XKB_STATE_LAYOUT_DEPRESSED),
63 xkb_state_serialize_layout(state, XKB_STATE_LAYOUT_LATCHED),
64 xkb_state_serialize_layout(state, XKB_STATE_LAYOUT_LOCKED),
65 xkb_state_serialize_layout(state, XKB_STATE_LAYOUT_EFFECTIVE));
66 fprintf(stderr,
67 " Modifiers: base: %#"PRIx32", latched: %#"PRIx32", "
68 "locked: %#"PRIx32", effective: %#"PRIx32"\n",
69 xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED),
70 xkb_state_serialize_mods(state, XKB_STATE_MODS_LATCHED),
71 xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED),
72 xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE));
73
74 /* There is currently no xkb_state_serialize_leds */
75 struct xkb_keymap *keymap = xkb_state_get_keymap(state);
76 xkb_led_mask_t leds = 0;
77 for (xkb_led_index_t led = 0; led < xkb_keymap_num_leds(keymap); led++) {
78 if (xkb_state_led_index_is_active(state, led) > 0)
79 leds |= UINT32_C(1) << led;
80 }
81 fprintf(stderr, " LEDs: 0x%x\n", leds);
82 }
83
84 /*
85 * Test a sequence of keysyms, resulting from a sequence of key presses,
86 * against the keysyms they're supposed to generate.
87 *
88 * - Each test runs with a clean state.
89 * - Each line in the test is made up of:
90 * + A keycode, given as a KEY_* from linux/input.h.
91 * + A direction - DOWN for press, UP for release, BOTH for
92 * immediate press + release, REPEAT to just get the syms.
93 * + A sequence of keysyms that should result from this keypress.
94 *
95 * The vararg format is:
96 * <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
97 *
98 * See below for examples.
99 */
100 int
101 test_key_seq_va(struct xkb_keymap *keymap, va_list ap)
102 {
103 struct xkb_state *state;
104
105 xkb_keycode_t kc;
106 int op;
107 xkb_keysym_t keysym;
108
109 const xkb_keysym_t *syms;
110 xkb_keysym_t sym;
111 char ksbuf[XKB_KEYSYM_NAME_MAX_SIZE];
112 const char *opstr = NULL;
113
114 fprintf(stderr, "----\n");
115
116 state = xkb_state_new(keymap);
117 assert(state);
118
119 for (;;) {
120 kc = va_arg(ap, int) + EVDEV_OFFSET;
121 op = va_arg(ap, int);
122
123 switch (op) {
124 case DOWN: opstr = "DOWN"; break;
125 case REPEAT: opstr = "REPEAT"; break;
126 case UP: opstr = "UP"; break;
127 case BOTH: opstr = "BOTH"; break;
128 case NEXT: opstr = "NEXT"; break;
129 case FINISH: opstr = "FINISH"; break;
130 default:
131 fprintf(stderr, "ERROR: Unsupported operation: %d\n", op);
132 goto fail;
133 }
134
135 const int nsyms = xkb_state_key_get_syms(state, kc, &syms);
136 if (nsyms == 1) {
137 sym = xkb_state_key_get_one_sym(state, kc);
138 syms = &sym;
139 }
140
141 if (op == DOWN || op == BOTH)
142 xkb_state_update_key(state, kc, XKB_KEY_DOWN);
143 if (op == UP || op == BOTH)
144 xkb_state_update_key(state, kc, XKB_KEY_UP);
145
146 #if HAVE_TOOLS
147 tools_print_keycode_state("", state, NULL, kc,
148 (op == DOWN ? XKB_KEY_DOWN : XKB_KEY_UP),
149 XKB_CONSUMED_MODE_XKB,
150 PRINT_ALL_FIELDS | PRINT_UNILINE);
151 #endif
152 fprintf(stderr, "op %-6s got %d syms for keycode %3"PRIu32": [", opstr, nsyms, kc);
153
154 for (int i = 0; i < nsyms; i++) {
155 keysym = va_arg(ap, int);
156 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
157 fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
158
159 if (keysym == FINISH || keysym == NEXT) {
160 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
161 fprintf(stderr, "\nERROR: Did not expect keysym: %s.\n", ksbuf);
162 goto fail;
163 }
164
165 if (keysym != syms[i]) {
166 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
167 fprintf(stderr, "\nERROR: Expected keysym: %s. ", ksbuf);;
168 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
169 fprintf(stderr, " Got keysym: %s.\n", ksbuf);;
170 goto fail;
171 }
172 }
173
174 if (nsyms == 0) {
175 keysym = va_arg(ap, int);
176 if (keysym != XKB_KEY_NoSymbol) {
177 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
178 fprintf(stderr, "\nERROR: Expected %s, but got no keysyms.\n", ksbuf);
179 goto fail;
180 }
181 }
182
183 fprintf(stderr, "]\n");
184
185 keysym = va_arg(ap, int);
186 if (keysym == NEXT)
187 continue;
188 if (keysym == FINISH)
189 break;
190
191 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
192 fprintf(stderr, "\nERROR: Expected keysym: %s. Didn't get it.\n", ksbuf);
193 goto fail;
194 }
195
196 xkb_state_unref(state);
197 return 1;
198
199 fail:
200 fprintf(stderr, "Current state:\n");
201 print_detailed_state(state);
202 xkb_state_unref(state);
203 return 0;
204 }
205
206 int
207 test_key_seq(struct xkb_keymap *keymap, ...)
208 {
209 va_list ap;
210 int ret;
211
212 va_start(ap, keymap);
213 ret = test_key_seq_va(keymap, ap);
214 va_end(ap);
215
216 return ret;
217 }
218
219 char *
220 test_makedir(const char *parent, const char *path)
221 {
222 char *dirname;
223 int err;
224
225 dirname = asprintf_safe("%s/%s", parent, path);
226 assert(dirname);
227 #ifdef _WIN32
228 err = _mkdir(dirname);
229 #else
230 err = mkdir(dirname, 0777);
231 #endif
232 assert(err == 0);
233
234 return dirname;
235 }
236
237 char *
238 test_maketempdir(const char *template)
239 {
240 #ifdef _WIN32
241 const char *basetmp = getenv("TMP");
242 if (basetmp == NULL) {
243 basetmp = getenv("TEMP");
244 }
245 if (basetmp == NULL) {
246 basetmp = getenv("top_builddir");
247 }
248 assert(basetmp != NULL);
249 char *tmpdir = asprintf_safe("%s/%s", basetmp, template);
250 assert(tmpdir != NULL);
251 char *tmp = _mktemp(tmpdir);
252 assert(tmp == tmpdir);
253 int ret = _mkdir(tmp);
254 assert(ret == 0);
255 return tmpdir;
256 #else
257 char *tmpdir = asprintf_safe("/tmp/%s", template);
258 assert(tmpdir != NULL);
259 char *tmp = mkdtemp(tmpdir);
260 assert(tmp == tmpdir);
261 return tmpdir;
262 #endif
263 }
264
265 char *
266 test_get_path(const char *path_rel)
267 {
268 char *path;
269 const char *srcdir;
270
271 srcdir = getenv("top_srcdir");
272 if (!srcdir)
273 srcdir = ".";
274
275 if (is_absolute_path(path_rel))
276 return strdup(path_rel);
277
278 path = asprintf_safe("%s/test/data%s%s", srcdir,
279 path_rel[0] ? "/" : "", path_rel);
280 if (!path) {
281 fprintf(stderr, "Failed to allocate path for %s\n", path_rel);
282 return NULL;
283 }
284 return path;
285 }
286
287 char *
288 read_file(const char *path, FILE *file)
289 {
290 if (!file)
291 return NULL;
292
293 const int fd = fileno(file);
294 if (fd < 0)
295 return NULL;
296
297 struct stat info;
298 if (fstat(fd, &info) != 0) {
299 fclose(file);
300 return NULL;
301 }
302
303 char* ret = malloc(info.st_size + 1);
304 if (!ret) {
305 fclose(file);
306 return NULL;
307 }
308
309 const size_t size = info.st_size;
310 const size_t count = fread(ret, sizeof(*ret), size, file);
311 if (count != size) {
312 if (!feof(file))
313 printf("Error reading file %s: unexpected end of file\n", path);
314 else if (ferror(file))
315 perror("Error reading file");
316 fclose(file);
317 free(ret);
318 return NULL;
319 }
320 ret[count] = '\0';
321
322 return ret;
323 }
324
325 char *
326 test_read_file(const char *path_rel)
327 {
328 char *path = test_get_path(path_rel);
329 if (!path)
330 return NULL;
331
332 FILE* file = fopen(path, "rb");
333 char *ret = NULL;
334
335 if (!file)
336 goto out;
337
338 ret = read_file(path, file);
339
340 out:
341 if (file)
342 fclose(file);
343 free(path);
344 return ret;
345 }
346
347 struct xkb_context *
348 test_get_context(enum test_context_flags test_flags)
349 {
350 enum xkb_context_flags ctx_flags = XKB_CONTEXT_NO_DEFAULT_INCLUDES;
351 if (test_flags & CONTEXT_ALLOW_ENVIRONMENT_NAMES) {
352 unsetenv("XKB_DEFAULT_RULES");
353 unsetenv("XKB_DEFAULT_MODEL");
354 unsetenv("XKB_DEFAULT_LAYOUT");
355 unsetenv("XKB_DEFAULT_VARIANT");
356 unsetenv("XKB_DEFAULT_OPTIONS");
357 }
358 else {
359 ctx_flags |= XKB_CONTEXT_NO_ENVIRONMENT_NAMES;
360 }
361
362 struct xkb_context * const ctx = xkb_context_new(ctx_flags);
363 if (!ctx)
364 return NULL;
365
366 char * const path = test_get_path("");
367 if (!path) {
368 xkb_context_unref(ctx);
369 return NULL;
370 }
371
372 xkb_context_include_path_append(ctx, path);
373 free(path);
374
375 return ctx;
376 }
377
378 struct xkb_keymap *
379 test_compile_file(struct xkb_context *context, enum xkb_keymap_format format,
380 const char *path_rel)
381 {
382 struct xkb_keymap *keymap;
383 FILE *file;
384 char *path;
385
386 path = test_get_path(path_rel);
387 if (!path)
388 return NULL;
389
390 file = fopen(path, "rb");
391 if (!file) {
392 fprintf(stderr, "Failed to open path: %s\n", path);
393 free(path);
394 return NULL;
395 }
396 assert(file != NULL);
397
398 keymap = xkb_keymap_new_from_file(context, file, format,
399 XKB_KEYMAP_COMPILE_NO_FLAGS);
400 fclose(file);
401
402 if (!keymap) {
403 fprintf(stderr, "Failed to compile path: %s\n", path);
404 free(path);
405 return NULL;
406 }
407
408 fprintf(stderr, "Successfully compiled path: %s\n", path);
409 free(path);
410
411 return keymap;
412 }
413
414 struct xkb_keymap *
415 test_compile_string(struct xkb_context *context, enum xkb_keymap_format format,
416 const char *string)
417 {
418 struct xkb_keymap *keymap;
419
420 keymap = xkb_keymap_new_from_string(context, string, format,
421 XKB_KEYMAP_COMPILE_NO_FLAGS);
422 if (!keymap) {
423 fprintf(stderr, "Failed to compile string\n");
424 return NULL;
425 }
426
427 return keymap;
428 }
429
430 struct xkb_keymap *
431 test_compile_buffer(struct xkb_context *context, enum xkb_keymap_format format,
432 const char *buf, size_t len)
433 {
434 struct xkb_keymap *keymap;
435
436 keymap = xkb_keymap_new_from_buffer(context, buf, len, format,
437 XKB_KEYMAP_COMPILE_NO_FLAGS);
438 if (!keymap) {
439 fprintf(stderr, "Failed to compile keymap from memory buffer\n");
440 return NULL;
441 }
442
443 return keymap;
444 }
445
446 struct xkb_keymap *
447 test_compile_rules(struct xkb_context *context, enum xkb_keymap_format format,
448 const char *rules, const char *model, const char *layout,
449 const char *variant, const char *options)
450 {
451 struct xkb_keymap *keymap;
452 struct xkb_rule_names rmlvo = {
453 .rules = isempty(rules) ? NULL : rules,
454 .model = isempty(model) ? NULL : model,
455 .layout = isempty(layout) ? NULL : layout,
456 .variant = isempty(variant) ? NULL : variant,
457 .options = isempty(options) ? NULL : options
458 };
459
460 if (!rules && !model && !layout && !variant && !options)
461 keymap = xkb_keymap_new_from_names2(context, NULL, format,
462 XKB_KEYMAP_COMPILE_NO_FLAGS);
463 else
464 keymap = xkb_keymap_new_from_names2(context, &rmlvo, format,
465 XKB_KEYMAP_COMPILE_NO_FLAGS);
466
467 if (!keymap) {
468 fprintf(stderr,
469 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
470 rules, model, layout, variant, options);
471 return NULL;
472 }
473
474 return keymap;
475 }
476
477 static struct xkb_rmlvo_builder *
478 xkb_rules_names_to_rmlvo_builder(struct xkb_context *context,
479 const struct xkb_rule_names *names)
480 {
481 struct xkb_rmlvo_builder *rmlvo =
482 xkb_rmlvo_builder_new(context, names->rules, names->model,
483 XKB_RMLVO_BUILDER_NO_FLAGS);
484 if (!rmlvo) {
485 fprintf(stderr, "ERROR: xkb_rmlvo_builder_new() failed\n");
486 return NULL;
487 }
488
489 char buf[1024] = { 0 };
490
491 /* First parse options, and gather the layout-specific ones and
492 * append the others to the builder */
493 darray(darray_string) loptions = darray_new();
494 if (!isempty(names->options)) {
495 const char *o = names->options;
496 while (*o != '\0') {
497 /* Get option name */
498 const char *option = o;
499 while (*o != '\0' && *o != ',' &&
500 *o != OPTIONS_GROUP_SPECIFIER_PREFIX) {
501 o++;
502 };
503 const size_t len = o - option;
504 if (len >= sizeof(buf))
505 goto error;
506 memcpy(buf, option, len);
507 buf[len] = '\0';
508
509 /* Check if layout-specific option */
510 xkb_layout_index_t layout = XKB_LAYOUT_INVALID;
511 if (*o == OPTIONS_GROUP_SPECIFIER_PREFIX) {
512 o++;
513 int count = parse_dec_to_uint32_t(o, SIZE_MAX, &layout);
514 if (count > 0 && layout > 0 && layout <= XKB_MAX_GROUPS) {
515 o += count;
516 layout--;
517 } else {
518 layout = XKB_LAYOUT_INVALID;
519 }
520 const char* const layout_index_end = o;
521 while (*o != '\0' && *o != ',') { o++; }
522 if (count <= 0 || layout_index_end != o)
523 layout = XKB_LAYOUT_INVALID;
524 }
525
526 if (layout != XKB_LAYOUT_INVALID) {
527 /* Save as layout-specific option, to be added with layout */
528 char *opt = strdup(buf);
529 if (!opt)
530 goto error;
531 darray_resize0(loptions, layout + 1);
532 darray_append(darray_item(loptions, layout), opt);
533 } else {
534 /* Append layout-agnostic option */
535 if (!xkb_rmlvo_builder_append_option(rmlvo, buf))
536 goto error;
537 }
538
539 if (*o == ',')
540 o++;
541 }
542 }
543
544 if (!isempty(names->layout)) {
545 /* Process layout & variant together.
546 * Ignore variants without corresponding layout. */
547 const char *l = names->layout;
548 const char *v = names->variant;
549 if (!names->variant)
550 v = "";
551 xkb_layout_index_t layout_count = 0;
552 while (*l != '\0') {
553 const char *layout = l;
554 const char *variant = v;
555 char *start = buf;
556 size_t buf_size = sizeof(buf);
557 while (*l != '\0' && *l != ',') { l++; };
558 while (*v != '\0' && *v != ',') { v++; };
559
560 size_t len = l - layout;
561 if (len >= buf_size)
562 goto error;
563 memcpy(start, layout, len);
564 start[len] = '\0';
565 start += len;
566 buf_size -= len + 1;
567
568 len = v - variant;
569 if (len >= buf_size)
570 goto error;
571 ++start;
572 memcpy(start, variant, len);
573 start[len] = '\0';
574
575 /* Get layout-specific options */
576 char **opts = NULL;
577 size_t opts_count = 0;
578 if (layout_count < darray_size(loptions)) {
579 opts = darray_items(darray_item(loptions, layout_count));
580 opts_count = darray_size(darray_item(loptions, layout_count));
581 }
582
583 if (!xkb_rmlvo_builder_append_layout(rmlvo, buf, start,
584 (const char* *) opts,
585 opts_count))
586 goto error;
587
588 if (*l == ',')
589 l++;
590 if (*v == ',')
591 v++;
592
593 layout_count += 1;
594 }
595 }
596
597 exit:
598 {
599 darray_string *opts;
600 darray_foreach(opts, loptions) {
601 char **opt;
602 darray_foreach(opt, *opts)
603 free(*opt);
604 darray_free(*opts);
605 }
606 darray_free(loptions);
607 }
608
609 return rmlvo;
610
611 error:
612 fprintf(stderr, "ERROR: %s\n", __func__);
613 xkb_rmlvo_builder_unref(rmlvo);
614 rmlvo = NULL;
615 goto exit;
616 }
617
618 struct xkb_keymap *
619 test_compile_rmlvo(struct xkb_context *context, enum xkb_keymap_format format,
620 const char *rules, const char *model, const char *layout,
621 const char *variant, const char *options)
622 {
623 struct xkb_keymap *keymap = NULL;
624 const struct xkb_rule_names names = {
625 .rules = rules,
626 .model = model,
627 .layout = layout,
628 .variant = variant,
629 .options = options
630 };
631
632 struct xkb_rmlvo_builder *rmlvo = xkb_rules_names_to_rmlvo_builder(context,
633 &names);
634
635 if (!rmlvo) {
636 fprintf(stderr,
637 "Failed to create RMLVO builder: "
638 "'%s', '%s', '%s', '%s', '%s'\n",
639 rules, model, layout, variant, options);
640 return NULL;
641 }
642
643 keymap = xkb_keymap_new_from_rmlvo(rmlvo, format,
644 XKB_KEYMAP_COMPILE_NO_FLAGS);
645
646 xkb_rmlvo_builder_unref(rmlvo);
647
648 if (!keymap) {
649 fprintf(stderr,
650 "Failed to compile RMLVO from builder: "
651 "'%s', '%s', '%s', '%s', '%s'\n",
652 rules, model, layout, variant, options);
653 }
654
655 return keymap;
656 }
657
658 bool
659 test_compile_output(struct xkb_context *ctx, enum xkb_keymap_format input_format,
660 enum xkb_keymap_format output_format,
661 test_compile_buffer_t compile_buffer,
662 void *compile_buffer_private, const char *test_title,
663 const char *keymap_str, size_t keymap_len,
664 const char *rel_path, bool update_output_files)
665 {
666 return test_compile_output2(ctx, input_format, output_format,
667 TEST_KEYMAP_SERIALIZE_FLAGS,
668 compile_buffer, compile_buffer_private,
669 test_title, keymap_str, keymap_len, rel_path,
670 update_output_files);
671 }
672
673 bool
674 test_compile_output2(struct xkb_context *ctx,
675 enum xkb_keymap_format input_format,
676 enum xkb_keymap_format output_format,
677 enum xkb_keymap_serialize_flags serialize_flags,
678 test_compile_buffer_t compile_buffer,
679 void *compile_buffer_private, const char *test_title,
680 const char *keymap_str, size_t keymap_len,
681 const char *rel_path, bool update_output_files)
682 {
683 int success = true;
684 fprintf(stderr, "*** %s ***\n", test_title);
685
686 struct xkb_keymap *keymap = compile_buffer(
687 ctx, input_format, keymap_str, keymap_len, compile_buffer_private
688 );
689
690 if (!rel_path) {
691 /* No path given: expect compilation failure */
692 if (keymap) {
693 char *got = xkb_keymap_get_as_string2(keymap, output_format,
694 serialize_flags);
695 xkb_keymap_unref(keymap);
696 assert(got);
697 fprintf(stderr,
698 "Unexpected keymap compilation success:\n%s\n", got);
699 free(got);
700 }
701 return !keymap;
702 }
703
704 if (!keymap) {
705 fprintf(stderr, "Unexpected keymap compilation failure\n");
706 return false;
707 }
708
709 char *got = xkb_keymap_get_as_string2(keymap, output_format,
710 serialize_flags);
711 if (!got) {
712 fprintf(stderr, "Unexpected keymap serialization failure\n");
713 return false;
714 }
715
716 xkb_keymap_unref(keymap);
717
718 char* const path = test_get_path(rel_path);
719 assert(path);
720
721 if (update_output_files) {
722 fprintf(stderr, "Writing golden test output to: %s\n", path);
723 FILE *file = fopen(path, "wb");
724 assert(file);
725 fwrite(got, 1, strlen(got), file);
726 fclose(file);
727 } else {
728 fprintf(stderr, "Reading golden test output: %s\n", path);
729 char* const expected = test_read_file(rel_path);
730 assert(expected);
731 static const char *label[2] = {"Golden test", "Roundtrip"};
732 bool test_round_trip =
733 (output_format == input_format ||
734 output_format == XKB_KEYMAP_USE_ORIGINAL_FORMAT);
735 for (unsigned int k = 0; k < ARRAY_SIZE(label) && success; k++) {
736 if (streq(expected, got)) {
737 fprintf(stderr, "%s succeeded.\n", label[k]);
738 if (!test_round_trip)
739 break;
740 /* Test round trip */
741 keymap = compile_buffer(ctx, input_format,
742 expected, strlen(expected),
743 compile_buffer_private);
744 if (!keymap) {
745 fprintf(stderr,
746 "Unexpected keymap roundtrip compilation failure\n");
747 success = false;
748 break;
749 }
750 free(got);
751 got = xkb_keymap_get_as_string2(keymap, output_format,
752 serialize_flags);
753 if (!got) {
754 fprintf(stderr,
755 "Unexpected keymap roundtrip serialization failure\n");
756 success = false;
757 }
758 xkb_keymap_unref(keymap);
759 test_round_trip = false;
760 } else {
761 fprintf(stderr,
762 "%s failed: dumped map differs from expected.\n",
763 label[k]);
764 fprintf(stderr, "Path to expected file: %s\n", path);
765 fprintf(stderr, "Length: expected %zu, got: %zu\n",
766 strlen(expected), strlen(got));
767 fprintf(stderr, "Dumped map:\n");
768 fprintf(stderr, "%s\n", got);
769 success = false;
770 }
771 }
772 free(expected);
773 }
774 free(got);
775 free(path);
776 return success;
777 }
778
779 bool
780 test_third_pary_compile_output(test_third_party_compile_buffer_t compile_buffer,
781 void *compile_buffer_private,
782 const char *test_title,
783 const char *keymap_in, size_t keymap_in_size,
784 const char *rel_path, bool update_output_files)
785 {
786 int success = true;
787 fprintf(stderr, "*** %s ***\n", test_title);
788 char* got = NULL;
789 size_t got_size = 0;
790
791 int ret = compile_buffer(keymap_in, keymap_in_size, compile_buffer_private,
792 &got, &got_size);
793
794 if (!rel_path) {
795 /* No path given: expect compilation failure */
796 if (ret == EXIT_SUCCESS) {
797 fprintf(stderr,
798 "Unexpected keymap compilation success:\nstdout:\n%s\n",
799 got);
800 }
801 free(got);
802 return (ret != EXIT_SUCCESS);
803 }
804
805 if (ret != EXIT_SUCCESS || isempty(got)) {
806 fprintf(stderr, "Unexpected keymap compilation failure.\nstdout:\n%s\n",
807 got);
808 free(got);
809 return false;
810 }
811
812 char *path = test_get_path(rel_path);
813 assert(path);
814
815 if (update_output_files) {
816 fprintf(stderr, "Writing golden test output to: %s\n", path);
817 FILE *file = fopen(path, "wb");
818 assert(file);
819 fwrite(got, 1, got_size, file);
820 fclose(file);
821 } else {
822 fprintf(stderr, "Reading golden test output: %s\n", path);
823 char *expected = test_read_file(rel_path);
824 assert(expected);
825 const char *label[2] = {"Golden test", "Roundtrip"};
826 for (unsigned int k = 0; k < ARRAY_SIZE(label) && success; k++) {
827 if (streq(expected, got)) {
828 fprintf(stderr, "%s succeeded.\n", label[k]);
829 if (k > 0)
830 continue;
831 /* Test round trip */
832 // TODO
833 break;
834 }else {
835 fprintf(stderr,
836 "%s failed: dumped map differs from expected.\n",
837 label[k]);
838 fprintf(stderr, "Path to expected file: %s\n", path);
839 fprintf(stderr, "Length: expected %zu, got: %zu\n",
840 strlen(expected), strlen(got));
841 fprintf(stderr, "Dumped map:\n");
842 fprintf(stderr, "%s\n", got);
843 success = false;
844 }
845 }
846 free(expected);
847 }
848 free(got);
849 free(path);
850 return success;
851 }