]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/test-keymap-util.c
fa26a0294ebd0174270b9945065650a8a4dd7666
[thirdparty/systemd.git] / src / locale / test-keymap-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2016 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "keymap-util.h"
23 #include "log.h"
24 #include "string-util.h"
25
26 static void test_find_language_fallback(void) {
27 _cleanup_free_ char *ans = NULL, *ans2 = NULL;
28
29 log_info("/*** %s ***/", __func__);
30
31 assert_se(find_language_fallback("foobar", &ans) == 0);
32 assert_se(ans == NULL);
33
34 assert_se(find_language_fallback("csb", &ans) == 0);
35 assert_se(ans == NULL);
36
37 assert_se(find_language_fallback("csb_PL", &ans) == 1);
38 assert_se(streq(ans, "csb:pl"));
39
40 assert_se(find_language_fallback("szl_PL", &ans2) == 1);
41 assert_se(streq(ans2, "szl:pl"));
42 }
43
44 static void test_find_converted_keymap(void) {
45 _cleanup_free_ char *ans = NULL, *ans2 = NULL;
46 int r;
47
48 log_info("/*** %s ***/", __func__);
49
50 assert_se(find_converted_keymap("pl", "foobar", &ans) == 0);
51 assert_se(ans == NULL);
52
53 r = find_converted_keymap("pl", NULL, &ans);
54 if (r == 0) {
55 log_info("Skipping rest of %s: keymaps are not installed", __func__);
56 return;
57 }
58
59 assert_se(r == 1);
60 assert_se(streq(ans, "pl"));
61
62 assert_se(find_converted_keymap("pl", "dvorak", &ans2) == 1);
63 assert_se(streq(ans2, "pl-dvorak"));
64 }
65
66 static void test_find_legacy_keymap(void) {
67 Context c = {};
68 _cleanup_free_ char *ans = NULL, *ans2 = NULL;
69
70 log_info("/*** %s ***/", __func__);
71
72 c.x11_layout = (char*) "foobar";
73 assert_se(find_legacy_keymap(&c, &ans) == 0);
74 assert_se(ans == NULL);
75
76 c.x11_layout = (char*) "pl";
77 assert_se(find_legacy_keymap(&c, &ans) == 1);
78 assert_se(streq(ans, "pl2"));
79
80 c.x11_layout = (char*) "pl,ru";
81 assert_se(find_legacy_keymap(&c, &ans2) == 1);
82 assert_se(streq(ans, "pl2"));
83 }
84
85 static void test_vconsole_convert_to_x11(void) {
86 _cleanup_(context_free) Context c = {};
87
88 log_info("/*** %s ***/", __func__);
89
90 log_info("/* test emptying first (:) */");
91 assert_se(free_and_strdup(&c.x11_layout, "foo") >= 0);
92 assert_se(free_and_strdup(&c.x11_variant, "bar") >= 0);
93 assert_se(vconsole_convert_to_x11(&c) == 1);
94 assert_se(c.x11_layout == NULL);
95 assert_se(c.x11_variant == NULL);
96
97 log_info("/* test emptying second (:) */");
98
99 assert_se(vconsole_convert_to_x11(&c) == 0);
100 assert_se(c.x11_layout == NULL);
101 assert_se(c.x11_variant == NULL);
102
103 log_info("/* test without variant, new mapping (es:) */");
104 assert_se(free_and_strdup(&c.vc_keymap, "es") >= 0);
105
106 assert_se(vconsole_convert_to_x11(&c) == 1);
107 assert_se(streq(c.x11_layout, "es"));
108 assert_se(c.x11_variant == NULL);
109
110 log_info("/* test with known variant, new mapping (es:dvorak) */");
111 assert_se(free_and_strdup(&c.vc_keymap, "es-dvorak") >= 0);
112
113 assert_se(vconsole_convert_to_x11(&c) == 0); // FIXME
114 assert_se(streq(c.x11_layout, "es"));
115 assert_se(c.x11_variant == NULL); // FIXME: "dvorak"
116
117 log_info("/* test with old mapping (fr:latin9) */");
118 assert_se(free_and_strdup(&c.vc_keymap, "fr-latin9") >= 0);
119
120 assert_se(vconsole_convert_to_x11(&c) == 1);
121 assert_se(streq(c.x11_layout, "fr"));
122 assert_se(streq(c.x11_variant, "latin9"));
123
124 log_info("/* test with a compound mapping (ru,us) */");
125 assert_se(free_and_strdup(&c.vc_keymap, "ru") >= 0);
126
127 assert_se(vconsole_convert_to_x11(&c) == 1);
128 assert_se(streq(c.x11_layout, "ru,us"));
129 assert_se(c.x11_variant == NULL);
130
131 log_info("/* test with a simple mapping (us) */");
132 assert_se(free_and_strdup(&c.vc_keymap, "us") >= 0);
133
134 assert_se(vconsole_convert_to_x11(&c) == 1);
135 assert_se(streq(c.x11_layout, "us"));
136 assert_se(c.x11_variant == NULL);
137 }
138
139 static void test_x11_convert_to_vconsole(void) {
140 _cleanup_(context_free) Context c = {};
141 int r;
142
143 log_info("/*** %s ***/", __func__);
144
145 log_info("/* test emptying first (:) */");
146 assert_se(free_and_strdup(&c.vc_keymap, "foobar") >= 0);
147 assert_se(x11_convert_to_vconsole(&c) == 1);
148 assert_se(c.vc_keymap == NULL);
149
150 log_info("/* test emptying second (:) */");
151
152 assert_se(x11_convert_to_vconsole(&c) == 0);
153 assert_se(c.vc_keymap == NULL);
154
155 log_info("/* test without variant, new mapping (es:) */");
156 assert_se(free_and_strdup(&c.x11_layout, "es") >= 0);
157
158 assert_se(x11_convert_to_vconsole(&c) == 1);
159 assert_se(streq(c.vc_keymap, "es"));
160
161 log_info("/* test with unknown variant, new mapping (es:foobar) */");
162 assert_se(free_and_strdup(&c.x11_variant, "foobar") >= 0);
163
164 assert_se(x11_convert_to_vconsole(&c) == 0);
165 assert_se(streq(c.vc_keymap, "es"));
166
167 log_info("/* test with known variant, new mapping (es:dvorak) */");
168 assert_se(free_and_strdup(&c.x11_variant, "dvorak") >= 0);
169
170 r = x11_convert_to_vconsole(&c);
171 if (r == 0) {
172 log_info("Skipping rest of %s: keymaps are not installed", __func__);
173 return;
174 }
175
176 assert_se(r == 1);
177 assert_se(streq(c.vc_keymap, "es-dvorak"));
178
179 log_info("/* test with old mapping (fr:latin9) */");
180 assert_se(free_and_strdup(&c.x11_layout, "fr") >= 0);
181 assert_se(free_and_strdup(&c.x11_variant, "latin9") >= 0);
182
183 assert_se(x11_convert_to_vconsole(&c) == 1);
184 assert_se(streq(c.vc_keymap, "fr-latin9"));
185
186 log_info("/* test with a compound mapping (us,ru:) */");
187 assert_se(free_and_strdup(&c.x11_layout, "us,ru") >= 0);
188 assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
189
190 assert_se(x11_convert_to_vconsole(&c) == 1);
191 assert_se(streq(c.vc_keymap, "us"));
192
193 log_info("/* test with a compound mapping (ru,us:) */");
194 assert_se(free_and_strdup(&c.x11_layout, "ru,us") >= 0);
195 assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
196
197 assert_se(x11_convert_to_vconsole(&c) == 1);
198 assert_se(streq(c.vc_keymap, "ru"));
199
200 /* https://bugzilla.redhat.com/show_bug.cgi?id=1333998 */
201 log_info("/* test with a simple new mapping (ru:) */");
202 assert_se(free_and_strdup(&c.x11_layout, "ru") >= 0);
203 assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
204
205 assert_se(x11_convert_to_vconsole(&c) == 0);
206 assert_se(streq(c.vc_keymap, "ru"));
207 }
208
209 int main(int argc, char **argv) {
210 log_set_max_level(LOG_DEBUG);
211 log_parse_environment();
212
213 test_find_language_fallback();
214 test_find_converted_keymap();
215 test_find_legacy_keymap();
216
217 test_vconsole_convert_to_x11();
218 test_x11_convert_to_vconsole();
219
220 return 0;
221 }