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