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