| 1 |
/* |
| 2 |
* Copyright © 2012 Ran Benita <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include <locale.h> |
| 9 |
|
| 10 |
#include "test.h" |
| 11 |
#include "context.h" |
| 12 |
#include "messages-codes.h" |
| 13 |
|
| 14 |
#ifdef __GNUC__ |
| 15 |
#pragma GCC diagnostic ignored "-Wmissing-format-attribute" |
| 16 |
#endif |
| 17 |
|
| 18 |
static const char * |
| 19 |
log_level_to_string(enum xkb_log_level level) |
| 20 |
{ |
| 21 |
switch (level) { |
| 22 |
case XKB_LOG_LEVEL_CRITICAL: |
| 23 |
return "critical"; |
| 24 |
case XKB_LOG_LEVEL_ERROR: |
| 25 |
return "error"; |
| 26 |
case XKB_LOG_LEVEL_WARNING: |
| 27 |
return "warning"; |
| 28 |
case XKB_LOG_LEVEL_INFO: |
| 29 |
return "info"; |
| 30 |
case XKB_LOG_LEVEL_DEBUG: |
| 31 |
return "debug"; |
| 32 |
} |
| 33 |
|
| 34 |
return "unknown"; |
| 35 |
} |
| 36 |
|
| 37 |
ATTR_PRINTF(3, 0) static void |
| 38 |
log_fn(struct xkb_context *ctx, enum xkb_log_level level, |
| 39 |
const char *fmt, va_list args) |
| 40 |
{ |
| 41 |
char *s; |
| 42 |
int size; |
| 43 |
darray_char *ls = xkb_context_get_user_data(ctx); |
| 44 |
assert(ls); |
| 45 |
|
| 46 |
size = vasprintf(&s, fmt, args); |
| 47 |
assert(size != -1); |
| 48 |
|
| 49 |
darray_append_string(*ls, log_level_to_string(level)); |
| 50 |
darray_append_lit(*ls, ": "); |
| 51 |
darray_append_string(*ls, s); |
| 52 |
free(s); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
test_basic(void) |
| 57 |
{ |
| 58 |
int ret; |
| 59 |
ret = setenv("XKB_LOG_LEVEL", "warn", 1); |
| 60 |
assert(ret == 0); |
| 61 |
ret = setenv("XKB_LOG_VERBOSITY", "5", 1); |
| 62 |
assert(ret == 0); |
| 63 |
|
| 64 |
struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG); |
| 65 |
assert(ctx); |
| 66 |
|
| 67 |
darray_char log_string; |
| 68 |
darray_init(log_string); |
| 69 |
xkb_context_set_user_data(ctx, &log_string); |
| 70 |
xkb_context_set_log_fn(ctx, log_fn); |
| 71 |
|
| 72 |
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "first warning: %d\n", 87); |
| 73 |
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "first info\n"); |
| 74 |
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "first debug: %s\n", "hello"); |
| 75 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "first error: %lu\n", 115415UL); |
| 76 |
log_vrb(ctx, 5, XKB_LOG_MESSAGE_NO_ID, "first verbose 5\n"); |
| 77 |
|
| 78 |
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_DEBUG); |
| 79 |
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "second warning: %d\n", 87); |
| 80 |
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "second debug: %s %s\n", "hello", "world"); |
| 81 |
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "second info\n"); |
| 82 |
log_err(ctx, XKB_ERROR_MALFORMED_NUMBER_LITERAL, "second error: %lu\n", 115415UL); |
| 83 |
log_vrb(ctx, 6, XKB_LOG_MESSAGE_NO_ID, "second verbose 6\n"); |
| 84 |
|
| 85 |
xkb_context_set_log_verbosity(ctx, XKB_LOG_VERBOSITY_MINIMAL); |
| 86 |
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL); |
| 87 |
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "third warning: %d\n", 87); |
| 88 |
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "third debug: %s %s\n", "hello", "world"); |
| 89 |
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "third info\n"); |
| 90 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "third error: %lu\n", 115415UL); |
| 91 |
log_vrb(ctx, 0, XKB_LOG_MESSAGE_NO_ID, "third verbose 0\n"); |
| 92 |
|
| 93 |
printf("%s", darray_items(log_string)); |
| 94 |
|
| 95 |
assert(streq(darray_items(log_string), |
| 96 |
"warning: first warning: 87\n" |
| 97 |
"error: first error: 115415\n" |
| 98 |
"warning: first verbose 5\n" |
| 99 |
"warning: second warning: 87\n" |
| 100 |
"debug: second debug: hello world\n" |
| 101 |
"info: second info\n" |
| 102 |
"error: [XKB-034] second error: 115415\n")); |
| 103 |
|
| 104 |
xkb_context_unref(ctx); |
| 105 |
darray_free(log_string); |
| 106 |
} |
| 107 |
|
| 108 |
struct test_data { |
| 109 |
const char * const input; |
| 110 |
const char * const log; |
| 111 |
bool error; |
| 112 |
}; |
| 113 |
|
| 114 |
static void |
| 115 |
test_keymaps(void) |
| 116 |
{ |
| 117 |
struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG); |
| 118 |
assert(ctx); |
| 119 |
|
| 120 |
darray_char log_string; |
| 121 |
darray_init(log_string); |
| 122 |
xkb_context_set_user_data(ctx, &log_string); |
| 123 |
xkb_context_set_log_fn(ctx, log_fn); |
| 124 |
|
| 125 |
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_WARNING); |
| 126 |
xkb_context_set_log_verbosity(ctx, XKB_LOG_VERBOSITY_COMPREHENSIVE); |
| 127 |
|
| 128 |
const struct test_data keymaps[] = { |
| 129 |
{ |
| 130 |
.input = "", |
| 131 |
.log = "error: [XKB-822] Failed to parse input xkb string\n", |
| 132 |
.error = true |
| 133 |
}, |
| 134 |
{ |
| 135 |
.input = " ", |
| 136 |
.log = "error: [XKB-822] Failed to parse input xkb string\n", |
| 137 |
.error = true |
| 138 |
}, |
| 139 |
{ |
| 140 |
.input = "\n", |
| 141 |
.log = "error: [XKB-822] Failed to parse input xkb string\n", |
| 142 |
.error = true |
| 143 |
}, |
| 144 |
{ |
| 145 |
.input = "xkb_keymap {\n", |
| 146 |
.log = |
| 147 |
"error: [XKB-769] (input string):1:12: syntax error, unexpected end of file\n" |
| 148 |
"error: [XKB-822] Failed to parse input xkb string\n", |
| 149 |
.error = true |
| 150 |
}, |
| 151 |
{ |
| 152 |
.input = |
| 153 |
"xkb_keymap \"\\j\"\n" |
| 154 |
" { xkb_symbols = {};\n" |
| 155 |
"};", |
| 156 |
.log = |
| 157 |
"warning: [XKB-645] (input string):1:12: unknown escape sequence \"\\j\" in string literal\n" |
| 158 |
"error: [XKB-769] (input string):2:16: syntax error, unexpected =, expecting {\n" |
| 159 |
"error: [XKB-822] Failed to parse input xkb string\n", |
| 160 |
.error = true |
| 161 |
}, |
| 162 |
{ |
| 163 |
.input = |
| 164 |
"xkb_keymap {\n" |
| 165 |
" xkb_keycodes { <> = 1; };\n" |
| 166 |
" xkb_symbols { key <> { [\"\xC3\xBC\xff\"] }; };\n" |
| 167 |
"};", |
| 168 |
.log = |
| 169 |
"error: [XKB-542] (input string):3:26: Cannot convert string to keysyms: Invalid UTF-8 encoding starting at byte position 3 (code point position: 2).\n" |
| 170 |
"error: [XKB-822] Failed to parse input xkb string\n", |
| 171 |
.error = true |
| 172 |
}, |
| 173 |
/* Extra lines & spaces intented to check the file positions */ |
| 174 |
{ |
| 175 |
.input = |
| 176 |
"xkb_keymap {\n" |
| 177 |
" xkb_keycodes {\n" |
| 178 |
" <> = 1;\n" |
| 179 |
"\n" |
| 180 |
" alias <1> = <>;\n" |
| 181 |
" alias <1> =\n" |
| 182 |
" <>;\n" |
| 183 |
" };\n" |
| 184 |
" xkb_types \"\\400x\\j\" { };\n" |
| 185 |
" xkb_compat {\n" |
| 186 |
" interpret invalidKeysym +\n" |
| 187 |
" Any { repeat = true; };\n" |
| 188 |
" };\n" |
| 189 |
" xkb_symbols { key <> {[0x30, leftshoe]}; };\n" |
| 190 |
"};", |
| 191 |
.log = |
| 192 |
"warning: [XKB-193] (input string):9:13: invalid octal escape sequence \"\\400\" in string literal\n" |
| 193 |
"warning: [XKB-645] (input string):9:13: unknown escape sequence \"\\j\" in string literal\n" |
| 194 |
"warning: [XKB-107] (input string):11:15: unrecognized keysym \"invalidKeysym\"\n" |
| 195 |
"warning: [XKB-489] (input string):14:26: numeric keysym \"0x0030\" (48)\n" |
| 196 |
"warning: [XKB-301] (input string):14:32: deprecated keysym \"leftshoe\".\n" |
| 197 |
"warning: [XKB-433] No map in include statement, but \"(input string)\" contains several; Using first defined map, \"(unnamed map)\"\n" |
| 198 |
"warning: [XKB-523] Alias of <1> for <> declared more than once; First definition ignored\n" |
| 199 |
"warning: [XKB-286] The type \"TWO_LEVEL\" for key '<>' group 1 was not previously defined; Using the default type\n" |
| 200 |
"warning: [XKB-516] Type \"ONE_LEVEL\" has 1 levels, but <> has 2 levels; Ignoring extra symbols\n", |
| 201 |
.error = false |
| 202 |
}, |
| 203 |
/* Invalid action fields */ |
| 204 |
{ |
| 205 |
.input = |
| 206 |
"default xkb_keymap {\n" |
| 207 |
" xkb_compat {\n" |
| 208 |
" NoAction.data = 1;\n" |
| 209 |
" interpret VoidSymbol { action= VoidAction(data=<garbage>); };\n" |
| 210 |
" };\n" |
| 211 |
"};", |
| 212 |
.log = |
| 213 |
"error: [XKB-563] The \"NoAction\" action takes no argument, but got \"data\" field; Action definition ignored\n" |
| 214 |
"error: [XKB-563] The \"VoidAction\" action takes no argument, but got \"data\" field; Action definition ignored\n" |
| 215 |
"error: Failed to compile xkb_compatibility\n" |
| 216 |
"error: [XKB-822] Failed to compile keymap\n", |
| 217 |
.error = true |
| 218 |
}, |
| 219 |
/* Invalid action fields */ |
| 220 |
{ |
| 221 |
.input = |
| 222 |
"default xkb_keymap {\n" |
| 223 |
" xkb_keycodes { <> = 1; };\n" |
| 224 |
" xkb_symbols {\n" |
| 225 |
" NoAction.data = 1;\n" |
| 226 |
" key <> { [TerminateServer(data=<garbage>)] };\n" |
| 227 |
" };\n" |
| 228 |
"};", |
| 229 |
.log = |
| 230 |
"error: [XKB-563] The \"NoAction\" action takes no argument, but got \"data\" field; Action definition ignored\n" |
| 231 |
"error: [XKB-563] The \"Terminate\" action takes no argument, but got \"data\" field; Action definition ignored\n" |
| 232 |
"error: [XKB-796] Illegal action definition for <>; Action for group 1/level 1 ignored\n" |
| 233 |
"error: Failed to compile xkb_symbols\n" |
| 234 |
"error: [XKB-822] Failed to compile keymap\n", |
| 235 |
.error = true |
| 236 |
}, |
| 237 |
/* Unsupported legacy X11 actions */ |
| 238 |
{ |
| 239 |
.input = |
| 240 |
"default xkb_keymap {\n" |
| 241 |
" xkb_compat {\n" |
| 242 |
/* This is a valid entry */ |
| 243 |
" interpret ISO_Lock+AnyOf(all) {\n" |
| 244 |
" action= ISOLock(modifiers=modMapMods,affect=all);\n" |
| 245 |
" };\n" |
| 246 |
/* This uses invalid field, but we do not check fields of |
| 247 |
* legacy actions */ |
| 248 |
" interpret VoidSymbol+AnyOf(all) {\n" |
| 249 |
" action= RedirectKey(data=<garbage>);\n" |
| 250 |
" };\n" |
| 251 |
" };\n" |
| 252 |
"};", |
| 253 |
.log = |
| 254 |
"warning: [XKB-362] Unsupported legacy action type \"ISOLock\".\n" |
| 255 |
"warning: [XKB-362] Unsupported legacy action type \"RedirectKey\".\n", |
| 256 |
.error = false |
| 257 |
}, |
| 258 |
/* Invalid global var */ |
| 259 |
{ |
| 260 |
.input = |
| 261 |
"default xkb_keymap {\n" |
| 262 |
" xkb_keycodes { foobar; };\n" |
| 263 |
"};", |
| 264 |
.log = |
| 265 |
"error: [XKB-639] Default defined for unknown field \"foobar\"; Ignored\n" |
| 266 |
"error: Failed to compile xkb_keycodes\n" |
| 267 |
"error: [XKB-822] Failed to compile keymap\n", |
| 268 |
.error = true |
| 269 |
}, |
| 270 |
{ |
| 271 |
.input = |
| 272 |
"default xkb_keymap {\n" |
| 273 |
" xkb_types { foobar; };\n" |
| 274 |
"};", |
| 275 |
.log = |
| 276 |
"error: [XKB-639] Default defined for unknown field \"foobar\"; Ignored\n" |
| 277 |
"error: Failed to compile xkb_types\n" |
| 278 |
"error: [XKB-822] Failed to compile keymap\n", |
| 279 |
.error = true |
| 280 |
}, |
| 281 |
{ |
| 282 |
.input = |
| 283 |
"default xkb_keymap {\n" |
| 284 |
" xkb_compat { foobar; };\n" |
| 285 |
"};", |
| 286 |
.log = |
| 287 |
"error: [XKB-639] Default defined for unknown field \"foobar\"; Ignored\n" |
| 288 |
"error: Failed to compile xkb_compatibility\n" |
| 289 |
"error: [XKB-822] Failed to compile keymap\n", |
| 290 |
.error = true |
| 291 |
}, |
| 292 |
{ |
| 293 |
.input = |
| 294 |
"default xkb_keymap {\n" |
| 295 |
" xkb_symbols { foobar; };\n" |
| 296 |
"};", |
| 297 |
.log = |
| 298 |
"error: [XKB-639] Default defined for unknown field \"foobar\"; Ignored\n" |
| 299 |
"error: Failed to compile xkb_symbols\n" |
| 300 |
"error: [XKB-822] Failed to compile keymap\n", |
| 301 |
.error = true |
| 302 |
}, |
| 303 |
}; |
| 304 |
|
| 305 |
for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) { |
| 306 |
fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k); |
| 307 |
struct xkb_keymap *keymap = |
| 308 |
test_compile_buffer(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, |
| 309 |
keymaps[k].input, strlen(keymaps[k].input)); |
| 310 |
assert_printf(keymaps[k].error ^ !!keymap, |
| 311 |
"%s\n", darray_items(log_string)); |
| 312 |
xkb_keymap_unref(keymap); |
| 313 |
assert_printf(streq_not_null(darray_items(log_string), keymaps[k].log), |
| 314 |
"Expected:\n%s\nGot:\n%s\n", |
| 315 |
keymaps[k].log, darray_items(log_string)); |
| 316 |
darray_free(log_string); |
| 317 |
} |
| 318 |
|
| 319 |
xkb_context_unref(ctx); |
| 320 |
} |
| 321 |
|
| 322 |
static void |
| 323 |
test_compose(void) |
| 324 |
{ |
| 325 |
struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG); |
| 326 |
assert(ctx); |
| 327 |
|
| 328 |
darray_char log_string; |
| 329 |
darray_init(log_string); |
| 330 |
xkb_context_set_user_data(ctx, &log_string); |
| 331 |
xkb_context_set_log_fn(ctx, log_fn); |
| 332 |
|
| 333 |
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_WARNING); |
| 334 |
xkb_context_set_log_verbosity(ctx, XKB_LOG_VERBOSITY_VERBOSE); |
| 335 |
|
| 336 |
const struct test_data composes[] = { |
| 337 |
{ |
| 338 |
.input = "", |
| 339 |
.log = NULL, |
| 340 |
.error = false |
| 341 |
}, |
| 342 |
{ |
| 343 |
.input = "\n", |
| 344 |
.log = NULL, |
| 345 |
.error = false |
| 346 |
}, |
| 347 |
{ |
| 348 |
.input = "\xff\n", |
| 349 |
.log = |
| 350 |
"error: [XKB-542] (input string):1:1: unexpected non-ASCII character.\n" |
| 351 |
"error: [XKB-542] (input string):1:1: This could be a file encoding issue. Supported file encodings are ASCII and UTF-8.\n" |
| 352 |
"error: (input string):1:1: failed to parse file\n", |
| 353 |
.error = true |
| 354 |
}, |
| 355 |
{ |
| 356 |
.input = |
| 357 |
"<leftshoe> : x\n" |
| 358 |
"include \"x\"\n", |
| 359 |
.log = |
| 360 |
"warning: [XKB-301] (input string):1:1: deprecated keysym \"leftshoe\".\n" |
| 361 |
"error: (input string):2:9: failed to open included Compose file \"x\": No such file or directory\n" |
| 362 |
"error: (input string):2:9: failed to parse file\n", |
| 363 |
.error = true |
| 364 |
}, |
| 365 |
{ |
| 366 |
.input = |
| 367 |
"<a> : \"a\"\n" |
| 368 |
"\n" |
| 369 |
"<b> : \"i\\j\\xk\n" |
| 370 |
"<0x30> : \"\\400\" invalidKeysym\n" |
| 371 |
"<0> <1> <2> <3> <4> <5> <6> <7> <8> <9> <leftshoe> : \"\"\n", |
| 372 |
.log = |
| 373 |
"warning: [XKB-645] (input string):3:7: unknown escape sequence \"\\j\" in string literal\n" |
| 374 |
"warning: [XKB-193] (input string):3:7: illegal hexadecimal escape sequence \"\\x\" in string literal\n" |
| 375 |
"error: [XKB-685] (input string):3:7: unterminated string literal\n" |
| 376 |
"warning: [XKB-193] (input string):4:10: illegal octal escape sequence \"\\400\" in string literal\n" |
| 377 |
"error: (input string):4:17: unrecognized keysym \"invalidKeysym\" on right-hand side\n" |
| 378 |
"warning: [XKB-301] (input string):5:41: deprecated keysym \"leftshoe\".\n" |
| 379 |
"warning: [XKB-685] (input string):5:41: too many keysyms (11) on left-hand side; skipping line\n", |
| 380 |
.error = false |
| 381 |
}, |
| 382 |
{ |
| 383 |
.input = |
| 384 |
":\n" |
| 385 |
"<a> :\n" |
| 386 |
"#\n" |
| 387 |
"<c> : \"a\" \"b\"\n" |
| 388 |
"<d> : a b\n", |
| 389 |
.log = |
| 390 |
"warning: (input string):1:1: expected at least one keysym on left-hand side; skipping line\n" |
| 391 |
"warning: [XKB-685] (input string):2:5: right-hand side must have at least one of string or keysym; skipping line\n" |
| 392 |
"warning: (input string):4:11: right-hand side can have at most one string; skipping line\n" |
| 393 |
"error: [XKB-685] (input string):5:9: unrecognized modifier \"b\"\n", |
| 394 |
.error = false |
| 395 |
}, |
| 396 |
{ |
| 397 |
.input = |
| 398 |
"<a> : a\n" |
| 399 |
"<a> : a\n" |
| 400 |
"<b> : b\n" |
| 401 |
"<b> <c> : x\n" |
| 402 |
"<c> <d> : y\n" |
| 403 |
"<c> : c\n", |
| 404 |
.log = |
| 405 |
"warning: (input string):2:7: this compose sequence is a duplicate of another; skipping line\n" |
| 406 |
"warning: (input string):4:11: a sequence already exists which is a prefix of this sequence; overriding\n" |
| 407 |
"warning: (input string):6:11: this compose sequence is a prefix of another; overriding\n", |
| 408 |
.error = false |
| 409 |
} |
| 410 |
}; |
| 411 |
|
| 412 |
for (unsigned int k = 0; k < ARRAY_SIZE(composes); k++) { |
| 413 |
fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k); |
| 414 |
struct xkb_compose_table *table = xkb_compose_table_new_from_buffer( |
| 415 |
ctx, composes[k].input, strlen(composes[k].input), "", |
| 416 |
XKB_COMPOSE_FORMAT_TEXT_V1, |
| 417 |
XKB_COMPOSE_COMPILE_NO_FLAGS |
| 418 |
); |
| 419 |
assert(composes[k].error ^ !!table); |
| 420 |
xkb_compose_table_unref(table); |
| 421 |
assert_printf(streq_null(darray_items(log_string), composes[k].log), |
| 422 |
"Expected:\n%s\nGot:\n%s\n", |
| 423 |
darray_items(log_string), composes[k].log); |
| 424 |
darray_free(log_string); |
| 425 |
} |
| 426 |
|
| 427 |
xkb_context_unref(ctx); |
| 428 |
} |
| 429 |
|
| 430 |
int |
| 431 |
main(void) |
| 432 |
{ |
| 433 |
test_init(); |
| 434 |
|
| 435 |
/* We really need to be locale-independent here */ |
| 436 |
setlocale(LC_ALL, "C"); |
| 437 |
|
| 438 |
test_basic(); |
| 439 |
test_keymaps(); |
| 440 |
test_compose(); |
| 441 |
return EXIT_SUCCESS; |
| 442 |
} |