]> git.proxmox.com Git - systemd.git/blob - src/locale/test-keymap-util.c
Imported Upstream version 231
[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
28 log_info("/*** %s ***/", __func__);
29
30 assert_se(find_language_fallback("foobar", &ans) == 0);
31 assert_se(ans == NULL);
32
33 assert_se(find_language_fallback("csb", &ans) == 0);
34 assert_se(ans == NULL);
35
36 assert_se(find_language_fallback("csb_PL", &ans) == 1);
37 assert_se(streq(ans, "csb:pl"));
38
39 assert_se(find_language_fallback("szl_PL", &ans2) == 1);
40 assert_se(streq(ans2, "szl:pl"));
41 }
42
43 static void test_find_converted_keymap(void) {
44 _cleanup_free_ char *ans = NULL, *ans2 = NULL;
45 int r;
46
47 log_info("/*** %s ***/", __func__);
48
49 assert_se(find_converted_keymap("pl", "foobar", &ans) == 0);
50 assert_se(ans == NULL);
51
52 r = find_converted_keymap("pl", NULL, &ans);
53 if (r == 0) {
54 log_info("Skipping rest of %s: keymaps are not installed", __func__);
55 return;
56 }
57
58 assert_se(r == 1);
59 assert_se(streq(ans, "pl"));
60
61 assert_se(find_converted_keymap("pl", "dvorak", &ans2) == 1);
62 assert_se(streq(ans2, "pl-dvorak"));
63 }
64
65 static void test_find_legacy_keymap(void) {
66 Context c = {};
67 _cleanup_free_ char *ans = NULL, *ans2 = NULL;
68
69 log_info("/*** %s ***/", __func__);
70
71 c.x11_layout = (char*) "foobar";
72 assert_se(find_legacy_keymap(&c, &ans) == 0);
73 assert_se(ans == NULL);
74
75 c.x11_layout = (char*) "pl";
76 assert_se(find_legacy_keymap(&c, &ans) == 1);
77 assert_se(streq(ans, "pl2"));
78
79 c.x11_layout = (char*) "pl,ru";
80 assert_se(find_legacy_keymap(&c, &ans2) == 1);
81 assert_se(streq(ans, "pl2"));
82 }
83
84 static void test_vconsole_convert_to_x11(void) {
85 _cleanup_(context_free) Context c = {};
86
87 log_info("/*** %s ***/", __func__);
88
89 log_info("/* test emptying first (:) */");
90 assert_se(free_and_strdup(&c.x11_layout, "foo") >= 0);
91 assert_se(free_and_strdup(&c.x11_variant, "bar") >= 0);
92 assert_se(vconsole_convert_to_x11(&c) == 1);
93 assert_se(c.x11_layout == NULL);
94 assert_se(c.x11_variant == NULL);
95
96 log_info("/* test emptying second (:) */");
97
98 assert_se(vconsole_convert_to_x11(&c) == 0);
99 assert_se(c.x11_layout == NULL);
100 assert_se(c.x11_variant == NULL);
101
102 log_info("/* test without variant, new mapping (es:) */");
103 assert_se(free_and_strdup(&c.vc_keymap, "es") >= 0);
104
105 assert_se(vconsole_convert_to_x11(&c) == 1);
106 assert_se(streq(c.x11_layout, "es"));
107 assert_se(c.x11_variant == NULL);
108
109 log_info("/* test with known variant, new mapping (es:dvorak) */");
110 assert_se(free_and_strdup(&c.vc_keymap, "es-dvorak") >= 0);
111
112 assert_se(vconsole_convert_to_x11(&c) == 0); // FIXME
113 assert_se(streq(c.x11_layout, "es"));
114 assert_se(c.x11_variant == NULL); // FIXME: "dvorak"
115
116 log_info("/* test with old mapping (fr:latin9) */");
117 assert_se(free_and_strdup(&c.vc_keymap, "fr-latin9") >= 0);
118
119 assert_se(vconsole_convert_to_x11(&c) == 1);
120 assert_se(streq(c.x11_layout, "fr"));
121 assert_se(streq(c.x11_variant, "latin9"));
122
123 log_info("/* test with a compound mapping (ru,us) */");
124 assert_se(free_and_strdup(&c.vc_keymap, "ru") >= 0);
125
126 assert_se(vconsole_convert_to_x11(&c) == 1);
127 assert_se(streq(c.x11_layout, "ru,us"));
128 assert_se(c.x11_variant == NULL);
129
130 log_info("/* test with a simple mapping (us) */");
131 assert_se(free_and_strdup(&c.vc_keymap, "us") >= 0);
132
133 assert_se(vconsole_convert_to_x11(&c) == 1);
134 assert_se(streq(c.x11_layout, "us"));
135 assert_se(c.x11_variant == NULL);
136 }
137
138 static void test_x11_convert_to_vconsole(void) {
139 _cleanup_(context_free) Context c = {};
140 int r;
141
142 log_info("/*** %s ***/", __func__);
143
144 log_info("/* test emptying first (:) */");
145 assert_se(free_and_strdup(&c.vc_keymap, "foobar") >= 0);
146 assert_se(x11_convert_to_vconsole(&c) == 1);
147 assert_se(c.vc_keymap == NULL);
148
149 log_info("/* test emptying second (:) */");
150
151 assert_se(x11_convert_to_vconsole(&c) == 0);
152 assert_se(c.vc_keymap == NULL);
153
154 log_info("/* test without variant, new mapping (es:) */");
155 assert_se(free_and_strdup(&c.x11_layout, "es") >= 0);
156
157 assert_se(x11_convert_to_vconsole(&c) == 1);
158 assert_se(streq(c.vc_keymap, "es"));
159
160 log_info("/* test with unknown variant, new mapping (es:foobar) */");
161 assert_se(free_and_strdup(&c.x11_variant, "foobar") >= 0);
162
163 assert_se(x11_convert_to_vconsole(&c) == 0);
164 assert_se(streq(c.vc_keymap, "es"));
165
166 log_info("/* test with known variant, new mapping (es:dvorak) */");
167 assert_se(free_and_strdup(&c.x11_variant, "dvorak") >= 0);
168
169 r = x11_convert_to_vconsole(&c);
170 if (r == 0) {
171 log_info("Skipping rest of %s: keymaps are not installed", __func__);
172 return;
173 }
174
175 assert_se(r == 1);
176 assert_se(streq(c.vc_keymap, "es-dvorak"));
177
178 log_info("/* test with old mapping (fr:latin9) */");
179 assert_se(free_and_strdup(&c.x11_layout, "fr") >= 0);
180 assert_se(free_and_strdup(&c.x11_variant, "latin9") >= 0);
181
182 assert_se(x11_convert_to_vconsole(&c) == 1);
183 assert_se(streq(c.vc_keymap, "fr-latin9"));
184
185 log_info("/* test with a compound mapping (us,ru:) */");
186 assert_se(free_and_strdup(&c.x11_layout, "us,ru") >= 0);
187 assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
188
189 assert_se(x11_convert_to_vconsole(&c) == 1);
190 assert_se(streq(c.vc_keymap, "us"));
191
192 log_info("/* test with a compound mapping (ru,us:) */");
193 assert_se(free_and_strdup(&c.x11_layout, "ru,us") >= 0);
194 assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
195
196 assert_se(x11_convert_to_vconsole(&c) == 1);
197 assert_se(streq(c.vc_keymap, "ru"));
198
199 /* https://bugzilla.redhat.com/show_bug.cgi?id=1333998 */
200 log_info("/* test with a simple new mapping (ru:) */");
201 assert_se(free_and_strdup(&c.x11_layout, "ru") >= 0);
202 assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
203
204 assert_se(x11_convert_to_vconsole(&c) == 0);
205 assert_se(streq(c.vc_keymap, "ru"));
206 }
207
208 int main(int argc, char **argv) {
209 log_set_max_level(LOG_DEBUG);
210 log_parse_environment();
211
212 test_find_language_fallback();
213 test_find_converted_keymap();
214 test_find_legacy_keymap();
215
216 test_vconsole_convert_to_x11();
217 test_x11_convert_to_vconsole();
218
219 return 0;
220 }