1/**
2 * Copyright © 2009 Red Hat, Inc.
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#ifdef HAVE_DIX_CONFIG_H
25#include <dix-config.h>
26#endif
27
28#include <xkb-config.h>
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <ctype.h>
33#include <unistd.h>
34#include <math.h>
35#include <X11/X.h>
36#include <X11/Xproto.h>
37#include <X11/keysym.h>
38#include <X11/Xatom.h>
39#include "misc.h"
40#include "inputstr.h"
41#include "opaque.h"
42#include "property.h"
43#define	XKBSRV_NEED_FILE_FUNCS
44#include <xkbsrv.h>
45#include "../xkb/xkbgeom.h"
46#include <X11/extensions/XKMformat.h>
47#include "xkbfile.h"
48#include "../xkb/xkb.h"
49
50#include <glib.h>
51
52/**
53 * Initialize an empty XkbRMLVOSet.
54 * Call XkbGetRulesDflts to obtain the default ruleset.
55 * Compare obtained ruleset with the built-in defaults.
56 *
57 * Result: RMLVO defaults are the same as obtained.
58 */
59static void xkb_get_rules_test(void)
60{
61    XkbRMLVOSet rmlvo = { NULL};
62    XkbGetRulesDflts(&rmlvo);
63
64
65    g_assert(rmlvo.rules);
66    g_assert(rmlvo.model);
67    g_assert(rmlvo.layout);
68    g_assert(rmlvo.variant);
69    g_assert(rmlvo.options);
70    g_assert(strcmp(rmlvo.rules, XKB_DFLT_RULES) == 0);
71    g_assert(strcmp(rmlvo.model, XKB_DFLT_MODEL) == 0);
72    g_assert(strcmp(rmlvo.layout, XKB_DFLT_LAYOUT) == 0);
73    g_assert(strcmp(rmlvo.variant, XKB_DFLT_VARIANT) == 0);
74    g_assert(strcmp(rmlvo.options, XKB_DFLT_OPTIONS) == 0);
75}
76
77/**
78 * Initialize an random XkbRMLVOSet.
79 * Call XkbGetRulesDflts to obtain the default ruleset.
80 * Compare obtained ruleset with the built-in defaults.
81 * Result: RMLVO defaults are the same as obtained.
82 */
83static void xkb_set_rules_test(void)
84{
85    XkbRMLVOSet rmlvo = {
86        .rules = "test-rules",
87        .model = "test-model",
88        .layout = "test-layout",
89        .variant = "test-variant",
90        .options = "test-options"
91    };
92    XkbRMLVOSet rmlvo_new = { NULL };
93
94    XkbSetRulesDflts(&rmlvo);
95    XkbGetRulesDflts(&rmlvo_new);
96
97    /* XkbGetRulesDflts strdups the values */
98    g_assert(rmlvo.rules != rmlvo_new.rules);
99    g_assert(rmlvo.model != rmlvo_new.model);
100    g_assert(rmlvo.layout != rmlvo_new.layout);
101    g_assert(rmlvo.variant != rmlvo_new.variant);
102    g_assert(rmlvo.options != rmlvo_new.options);
103
104    g_assert(strcmp(rmlvo.rules, rmlvo_new.rules) == 0);
105    g_assert(strcmp(rmlvo.model, rmlvo_new.model) == 0);
106    g_assert(strcmp(rmlvo.layout, rmlvo_new.layout) == 0);
107    g_assert(strcmp(rmlvo.variant, rmlvo_new.variant) == 0);
108    g_assert(strcmp(rmlvo.options, rmlvo_new.options) == 0);
109}
110
111
112/**
113 * Get the default RMLVO set.
114 * Set the default RMLVO set.
115 * Get the default RMLVO set.
116 * Repeat the last two steps.
117 *
118 * Result: RMLVO set obtained is the same as previously set.
119 */
120static void xkb_set_get_rules_test(void)
121{
122/* This test failed before XkbGetRulesDftlts changed to strdup.
123   We test this twice because the first time using XkbGetRulesDflts we obtain
124   the built-in defaults. The unexpected free isn't triggered until the second
125   XkbSetRulesDefaults.
126 */
127    XkbRMLVOSet rmlvo = { NULL };
128    XkbRMLVOSet rmlvo_backup;
129
130    XkbGetRulesDflts(&rmlvo);
131
132    /* pass 1 */
133    XkbSetRulesDflts(&rmlvo);
134    XkbGetRulesDflts(&rmlvo);
135
136    /* Make a backup copy */
137    rmlvo_backup.rules = strdup(rmlvo.rules);
138    rmlvo_backup.layout = strdup(rmlvo.layout);
139    rmlvo_backup.model = strdup(rmlvo.model);
140    rmlvo_backup.variant = strdup(rmlvo.variant);
141    rmlvo_backup.options = strdup(rmlvo.options);
142
143    /* pass 2 */
144    XkbSetRulesDflts(&rmlvo);
145
146    /* This test is iffy, because strictly we may be comparing against already
147     * freed memory */
148    g_assert(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0);
149    g_assert(strcmp(rmlvo.model, rmlvo_backup.model) == 0);
150    g_assert(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0);
151    g_assert(strcmp(rmlvo.variant, rmlvo_backup.variant) == 0);
152    g_assert(strcmp(rmlvo.options, rmlvo_backup.options) == 0);
153
154    XkbGetRulesDflts(&rmlvo);
155    g_assert(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0);
156    g_assert(strcmp(rmlvo.model, rmlvo_backup.model) == 0);
157    g_assert(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0);
158    g_assert(strcmp(rmlvo.variant, rmlvo_backup.variant) == 0);
159    g_assert(strcmp(rmlvo.options, rmlvo_backup.options) == 0);
160}
161
162
163int main(int argc, char** argv)
164{
165    g_test_init(&argc, &argv,NULL);
166    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
167
168    g_test_add_func("/xkb/set-get-rules", xkb_set_get_rules_test);
169    g_test_add_func("/xkb/get-rules", xkb_get_rules_test);
170    g_test_add_func("/xkb/set-rules", xkb_set_rules_test);
171
172    return g_test_run();
173}
174