test_xkb.c revision 1b5d61b8
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#include <assert.h>
50
51#include "tests-common.h"
52
53/**
54 * Initialize an empty XkbRMLVOSet.
55 * Call XkbGetRulesDflts to obtain the default ruleset.
56 * Compare obtained ruleset with the built-in defaults.
57 *
58 * Result: RMLVO defaults are the same as obtained.
59 */
60static void
61xkb_get_rules_test(void)
62{
63    XkbRMLVOSet rmlvo = { NULL };
64    XkbGetRulesDflts(&rmlvo);
65
66    assert(rmlvo.rules);
67    assert(rmlvo.model);
68    assert(rmlvo.layout);
69    assert(rmlvo.variant);
70    assert(rmlvo.options);
71    assert(strcmp(rmlvo.rules, XKB_DFLT_RULES) == 0);
72    assert(strcmp(rmlvo.model, XKB_DFLT_MODEL) == 0);
73    assert(strcmp(rmlvo.layout, XKB_DFLT_LAYOUT) == 0);
74    assert(strcmp(rmlvo.variant, XKB_DFLT_VARIANT) == 0);
75    assert(strcmp(rmlvo.options, XKB_DFLT_OPTIONS) == 0);
76}
77
78/**
79 * Initialize an random XkbRMLVOSet.
80 * Call XkbGetRulesDflts to obtain the default ruleset.
81 * Compare obtained ruleset with the built-in defaults.
82 * Result: RMLVO defaults are the same as obtained.
83 */
84static void
85xkb_set_rules_test(void)
86{
87    XkbRMLVOSet rmlvo;
88    XkbRMLVOSet rmlvo_new = { NULL };
89
90    XkbInitRules(&rmlvo, "test-rules", "test-model", "test-layout",
91                         "test-variant", "test-options");
92    assert(rmlvo.rules);
93    assert(rmlvo.model);
94    assert(rmlvo.layout);
95    assert(rmlvo.variant);
96    assert(rmlvo.options);
97
98    XkbSetRulesDflts(&rmlvo);
99    XkbGetRulesDflts(&rmlvo_new);
100
101    /* XkbGetRulesDflts strdups the values */
102    assert(rmlvo.rules != rmlvo_new.rules);
103    assert(rmlvo.model != rmlvo_new.model);
104    assert(rmlvo.layout != rmlvo_new.layout);
105    assert(rmlvo.variant != rmlvo_new.variant);
106    assert(rmlvo.options != rmlvo_new.options);
107
108    assert(strcmp(rmlvo.rules, rmlvo_new.rules) == 0);
109    assert(strcmp(rmlvo.model, rmlvo_new.model) == 0);
110    assert(strcmp(rmlvo.layout, rmlvo_new.layout) == 0);
111    assert(strcmp(rmlvo.variant, rmlvo_new.variant) == 0);
112    assert(strcmp(rmlvo.options, rmlvo_new.options) == 0);
113
114    XkbFreeRMLVOSet(&rmlvo, FALSE);
115}
116
117/**
118 * Get the default RMLVO set.
119 * Set the default RMLVO set.
120 * Get the default RMLVO set.
121 * Repeat the last two steps.
122 *
123 * Result: RMLVO set obtained is the same as previously set.
124 */
125static void
126xkb_set_get_rules_test(void)
127{
128/* This test failed before XkbGetRulesDftlts changed to strdup.
129   We test this twice because the first time using XkbGetRulesDflts we obtain
130   the built-in defaults. The unexpected free isn't triggered until the second
131   XkbSetRulesDefaults.
132 */
133    XkbRMLVOSet rmlvo = { NULL };
134    XkbRMLVOSet rmlvo_backup;
135
136    XkbGetRulesDflts(&rmlvo);
137
138    /* pass 1 */
139    XkbSetRulesDflts(&rmlvo);
140    XkbGetRulesDflts(&rmlvo);
141
142    /* Make a backup copy */
143    rmlvo_backup.rules = strdup(rmlvo.rules);
144    rmlvo_backup.layout = strdup(rmlvo.layout);
145    rmlvo_backup.model = strdup(rmlvo.model);
146    rmlvo_backup.variant = strdup(rmlvo.variant);
147    rmlvo_backup.options = strdup(rmlvo.options);
148
149    /* pass 2 */
150    XkbSetRulesDflts(&rmlvo);
151
152    /* This test is iffy, because strictly we may be comparing against already
153     * freed memory */
154    assert(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0);
155    assert(strcmp(rmlvo.model, rmlvo_backup.model) == 0);
156    assert(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0);
157    assert(strcmp(rmlvo.variant, rmlvo_backup.variant) == 0);
158    assert(strcmp(rmlvo.options, rmlvo_backup.options) == 0);
159
160    XkbGetRulesDflts(&rmlvo);
161    assert(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0);
162    assert(strcmp(rmlvo.model, rmlvo_backup.model) == 0);
163    assert(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0);
164    assert(strcmp(rmlvo.variant, rmlvo_backup.variant) == 0);
165    assert(strcmp(rmlvo.options, rmlvo_backup.options) == 0);
166}
167
168int
169xkb_test(void)
170{
171    xkb_set_get_rules_test();
172    xkb_get_rules_test();
173    xkb_set_rules_test();
174
175    return 0;
176}
177