test.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright © 2012 Intel Corporation
  3. * SPDX-License-Identifier: MIT
  4. *
  5. * Author: Daniel Stone <daniel@fooishbar.org>
  6. */
  7. #pragma once
  8. #include "config.h"
  9. #include <assert.h>
  10. #include <stdbool.h>
  11. /* Don't use compat names in internal code. */
  12. #define _XKBCOMMON_COMPAT_H
  13. #include "xkbcommon/xkbcommon.h"
  14. #include "xkbcommon/xkbcommon-compose.h"
  15. #include "src/messages-codes.h"
  16. #include "utils.h"
  17. /* Automake test exit code to signify SKIP (à la PASS, FAIL, etc).
  18. * See: https://www.gnu.org/software/automake/manual/html_node/Scripts_002dbased-Testsuites.html */
  19. #define SKIP_TEST 77
  20. #define TEST_SETUP_FAILURE 99
  21. #define TEST_ASSERT_FAILURE -1
  22. #define assert_printf(cond, ...) do { \
  23. const bool __cond = (cond); \
  24. if (!__cond) { \
  25. fprintf(stderr, "Assertion failure: " __VA_ARGS__); \
  26. assert(__cond); \
  27. }} while (0)
  28. #define assert_streq(test_name, expected, got) \
  29. assert_printf(streq_null(expected, got), \
  30. test_name ". Expected \"%s\", got: \"%s\"\n", expected, got)
  31. #define assert_streq_not_null(test_name, expected, got) \
  32. assert_printf(streq_not_null(expected, got), \
  33. test_name ". Expected \"%s\", got: \"%s\"\n", expected, got)
  34. #define assert_eq(test_name, expected, got, format, ...) \
  35. assert_printf(expected == got, \
  36. test_name ". Expected " format ", got: " format "\n", \
  37. ##__VA_ARGS__, expected, got)
  38. /* Enable testing assertion failures */
  39. #if defined(__unix__) || defined(__APPLE__)
  40. #include <sys/wait.h>
  41. #include <sys/mman.h>
  42. #include <unistd.h>
  43. #define RUN_ISOLATED(code, ret, ...) do { \
  44. memset(&(ret), 0, sizeof(ret)); \
  45. __typeof__(ret) *_result = mmap( \
  46. NULL, sizeof(ret), \
  47. PROT_READ | PROT_WRITE, \
  48. MAP_SHARED | MAP_ANONYMOUS, -1, 0 \
  49. ); \
  50. if (_result == MAP_FAILED) { \
  51. (code) = TEST_SETUP_FAILURE; \
  52. break; \
  53. } \
  54. pid_t _pid = fork(); \
  55. if (_pid == -1) { \
  56. (code) = TEST_SETUP_FAILURE; \
  57. munmap(_result, sizeof(ret)); \
  58. } else if (_pid == 0) { \
  59. __VA_ARGS__; \
  60. *_result = (ret); \
  61. _exit(EXIT_SUCCESS); \
  62. } else { \
  63. int _status; \
  64. waitpid(_pid, &_status, 0); \
  65. if (WIFSIGNALED(_status) && \
  66. WTERMSIG(_status) == SIGABRT) { \
  67. /* assert failure */ \
  68. (code) = TEST_ASSERT_FAILURE; \
  69. } else if (WIFEXITED(_status)) { \
  70. (code) = WEXITSTATUS(_status); \
  71. /* propagate child's own return code */ \
  72. (ret) = *_result; \
  73. } else { \
  74. /* killed by other signal */ \
  75. (code) = EXIT_FAILURE; \
  76. } \
  77. munmap(_result, sizeof(ret)); \
  78. } \
  79. } while (0)
  80. #else
  81. /* Skip assert-death tests or use a stub */
  82. #define RUN_ISOLATED(code, ret, ...) do { \
  83. (code) = SKIP_TEST; \
  84. memset(&(ret), 0, sizeof(ret)); \
  85. } while (0)
  86. #endif
  87. #define TEST_KEYMAP_COMPILE_FLAGS XKB_KEYMAP_COMPILE_STRICT_MODE
  88. #define TEST_KEYMAP_SERIALIZE_FLAGS ( \
  89. XKB_KEYMAP_SERIALIZE_PRETTY | \
  90. XKB_KEYMAP_SERIALIZE_KEEP_UNUSED \
  91. )
  92. #define TEST_KEYMAP_SERIALIZE_EXPLICIT ( \
  93. XKB_KEYMAP_SERIALIZE_EXPLICIT_DEFAULT_VALUES | \
  94. XKB_KEYMAP_SERIALIZE_EXPLICIT_VMODS | \
  95. XKB_KEYMAP_SERIALIZE_EXPLICIT_KEY_VALUES \
  96. )
  97. void
  98. test_init(void);
  99. void
  100. print_detailed_state(struct xkb_state *state);
  101. /* The offset between KEY_* numbering, and keycodes in the XKB evdev
  102. * dataset. */
  103. #define EVDEV_OFFSET 8
  104. enum key_seq_state {
  105. DOWN,
  106. REPEAT,
  107. UP,
  108. BOTH,
  109. NEXT,
  110. FINISH,
  111. };
  112. int
  113. test_key_seq(struct xkb_keymap *keymap, ...);
  114. int
  115. test_key_seq2(struct xkb_keymap *keymap, struct xkb_machine *sm,
  116. struct xkb_events *events, ...);
  117. int
  118. test_key_seq_va(struct xkb_keymap *keymap, struct xkb_machine * sm,
  119. struct xkb_events *events, va_list args);
  120. char *
  121. test_makedir(const char *parent, const char *path);
  122. char *
  123. test_maketempdir(const char *template);
  124. char *
  125. test_get_path(const char *path_rel);
  126. char *
  127. read_file(const char *path, FILE *file);
  128. char *
  129. test_read_file(const char *path_rel);
  130. enum test_context_flags {
  131. CONTEXT_NO_FLAG = 0,
  132. CONTEXT_ALLOW_ENVIRONMENT_NAMES = (1 << 0),
  133. };
  134. struct xkb_context *
  135. test_get_context(enum test_context_flags flags);
  136. struct xkb_keymap *
  137. test_compile_file(struct xkb_context *context, enum xkb_keymap_format format,
  138. const char *path_rel);
  139. struct xkb_keymap *
  140. test_compile_string(struct xkb_context *context, enum xkb_keymap_format format,
  141. const char *string);
  142. struct xkb_keymap *
  143. test_compile_buffer(struct xkb_context *context, enum xkb_keymap_format format,
  144. const char *buf, size_t len);
  145. struct xkb_keymap *
  146. test_compile_buffer2(struct xkb_context *context, enum xkb_keymap_format format,
  147. enum xkb_keymap_compile_flags flags,
  148. const char *buf, size_t len);
  149. typedef struct xkb_keymap * (*test_compile_buffer_t)(struct xkb_context *context,
  150. enum xkb_keymap_format format,
  151. const char *buf, size_t len,
  152. void *private);
  153. bool
  154. test_compile_output(struct xkb_context *ctx, enum xkb_keymap_format input_format,
  155. enum xkb_keymap_format output_format,
  156. test_compile_buffer_t compile_buffer,
  157. void *compile_buffer_private, const char *test_title,
  158. const char *keymap_str, size_t keymap_len,
  159. const char *rel_path, bool update_output_files);
  160. bool
  161. test_compile_output2(struct xkb_context *ctx,
  162. enum xkb_keymap_format input_format,
  163. enum xkb_keymap_format output_format,
  164. enum xkb_keymap_serialize_flags serialize_flags,
  165. test_compile_buffer_t compile_buffer,
  166. void *compile_buffer_private, const char *test_title,
  167. const char *keymap_str, size_t keymap_len,
  168. const char *rel_path, bool update_output_files);
  169. typedef int (*test_third_party_compile_buffer_t)(const char *buf, size_t len,
  170. void *private,
  171. char **out, size_t *size_out);
  172. bool
  173. test_third_pary_compile_output(test_third_party_compile_buffer_t compile_buffer,
  174. void *compile_buffer_private,
  175. const char *test_title,
  176. const char *keymap_str, size_t keymap_len,
  177. const char *rel_path, bool update_output_files);
  178. struct xkb_keymap *
  179. test_compile_rmlvo(struct xkb_context *context, enum xkb_keymap_format format,
  180. const char *rules, const char *model, const char *layout,
  181. const char *variant, const char *options);
  182. struct xkb_keymap *
  183. test_compile_rules(struct xkb_context *context, enum xkb_keymap_format format,
  184. const char *rules, const char *model, const char *layout,
  185. const char *variant, const char *options);
  186. static inline void
  187. xkb_enable_quiet_logging(struct xkb_context *ctx)
  188. {
  189. xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
  190. xkb_context_set_log_verbosity(ctx, XKB_LOG_VERBOSITY_MINIMAL);
  191. }
  192. #ifdef _WIN32
  193. #define setenv(varname, value, overwrite) _putenv_s((varname), (value))
  194. #define unsetenv(varname) _putenv_s(varname, "")
  195. #endif