xkbcomp: escape the section names before storing them in the keymap
[platform/upstream/libxkbcommon.git] / test / stringcomp.c
1 /*
2  * Copyright © 2009 Dan Nicholson
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "test.h"
29
30 #define DATA_PATH "keymaps/stringcomp.data"
31
32 int
33 main(int argc, char *argv[])
34 {
35     struct xkb_context *ctx = test_get_context(0);
36     struct xkb_keymap *keymap;
37     char *original, *dump, *dump2;
38
39     assert(ctx);
40
41     /* Load in a prebuilt keymap, make sure we can compile it from a string,
42      * then compare it to make sure we get the same result when dumping it
43      * to a string. */
44     original = test_read_file(DATA_PATH);
45     assert(original);
46
47     keymap = test_compile_string(ctx, original);
48     assert(keymap);
49
50     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
51     assert(dump);
52
53     if (!streq(original, dump)) {
54         fprintf(stderr,
55                 "round-trip test failed: dumped map differs from original\n");
56         fprintf(stderr, "path to original file: %s\n",
57                 test_get_path(DATA_PATH));
58         fprintf(stderr, "length: dumped %lu, original %lu\n",
59                 (unsigned long) strlen(dump),
60                 (unsigned long) strlen(original));
61         fprintf(stderr, "dumped map:\n");
62         fprintf(stderr, "%s\n", dump);
63         fflush(stderr);
64         assert(0);
65     }
66
67     free(original);
68     free(dump);
69     xkb_keymap_unref(keymap);
70
71     /* Make sure we can't (falsely claim to) compile an empty string. */
72     keymap = test_compile_string(ctx, "");
73     assert(!keymap);
74
75     /* Make sure we can recompile our output for a normal keymap from rules. */
76     keymap = test_compile_rules(ctx, NULL, NULL,
77                                 "ru,ca,de,us", ",multix,neo,intl", NULL);
78     assert(keymap);
79     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
80     assert(dump);
81     xkb_keymap_unref(keymap);
82     keymap = test_compile_string(ctx, dump);
83     assert(keymap);
84     /* Now test that the dump of the dump is equal to the dump! */
85     dump2 = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
86     assert(dump2);
87     assert(streq(dump, dump2));
88
89     /* Test response to invalid formats and flags. */
90     assert(!xkb_keymap_new_from_string(ctx, dump, 0, 0));
91     assert(!xkb_keymap_new_from_string(ctx, dump, -1, 0));
92     assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V1+1, 0));
93     assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V1, -1));
94     assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V1, 1414));
95     assert(!xkb_keymap_get_as_string(keymap, 0));
96     assert(!xkb_keymap_get_as_string(keymap, 4893));
97
98     xkb_keymap_unref(keymap);
99     free(dump);
100     free(dump2);
101
102     xkb_context_unref(ctx);
103
104     return 0;
105 }