]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/test-keymap-util.c
fa26a0294ebd0174270b9945065650a8a4dd7666
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
21 #include "alloc-util.h"
22 #include "keymap-util.h"
24 #include "string-util.h"
26 static void test_find_language_fallback(void) {
27 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
29 log_info("/*** %s ***/", __func__
);
31 assert_se(find_language_fallback("foobar", &ans
) == 0);
32 assert_se(ans
== NULL
);
34 assert_se(find_language_fallback("csb", &ans
) == 0);
35 assert_se(ans
== NULL
);
37 assert_se(find_language_fallback("csb_PL", &ans
) == 1);
38 assert_se(streq(ans
, "csb:pl"));
40 assert_se(find_language_fallback("szl_PL", &ans2
) == 1);
41 assert_se(streq(ans2
, "szl:pl"));
44 static void test_find_converted_keymap(void) {
45 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
48 log_info("/*** %s ***/", __func__
);
50 assert_se(find_converted_keymap("pl", "foobar", &ans
) == 0);
51 assert_se(ans
== NULL
);
53 r
= find_converted_keymap("pl", NULL
, &ans
);
55 log_info("Skipping rest of %s: keymaps are not installed", __func__
);
60 assert_se(streq(ans
, "pl"));
62 assert_se(find_converted_keymap("pl", "dvorak", &ans2
) == 1);
63 assert_se(streq(ans2
, "pl-dvorak"));
66 static void test_find_legacy_keymap(void) {
68 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
70 log_info("/*** %s ***/", __func__
);
72 c
.x11_layout
= (char*) "foobar";
73 assert_se(find_legacy_keymap(&c
, &ans
) == 0);
74 assert_se(ans
== NULL
);
76 c
.x11_layout
= (char*) "pl";
77 assert_se(find_legacy_keymap(&c
, &ans
) == 1);
78 assert_se(streq(ans
, "pl2"));
80 c
.x11_layout
= (char*) "pl,ru";
81 assert_se(find_legacy_keymap(&c
, &ans2
) == 1);
82 assert_se(streq(ans
, "pl2"));
85 static void test_vconsole_convert_to_x11(void) {
86 _cleanup_(context_free
) Context c
= {};
88 log_info("/*** %s ***/", __func__
);
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
);
97 log_info("/* test emptying second (:) */");
99 assert_se(vconsole_convert_to_x11(&c
) == 0);
100 assert_se(c
.x11_layout
== NULL
);
101 assert_se(c
.x11_variant
== NULL
);
103 log_info("/* test without variant, new mapping (es:) */");
104 assert_se(free_and_strdup(&c
.vc_keymap
, "es") >= 0);
106 assert_se(vconsole_convert_to_x11(&c
) == 1);
107 assert_se(streq(c
.x11_layout
, "es"));
108 assert_se(c
.x11_variant
== NULL
);
110 log_info("/* test with known variant, new mapping (es:dvorak) */");
111 assert_se(free_and_strdup(&c
.vc_keymap
, "es-dvorak") >= 0);
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"
117 log_info("/* test with old mapping (fr:latin9) */");
118 assert_se(free_and_strdup(&c
.vc_keymap
, "fr-latin9") >= 0);
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"));
124 log_info("/* test with a compound mapping (ru,us) */");
125 assert_se(free_and_strdup(&c
.vc_keymap
, "ru") >= 0);
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
);
131 log_info("/* test with a simple mapping (us) */");
132 assert_se(free_and_strdup(&c
.vc_keymap
, "us") >= 0);
134 assert_se(vconsole_convert_to_x11(&c
) == 1);
135 assert_se(streq(c
.x11_layout
, "us"));
136 assert_se(c
.x11_variant
== NULL
);
139 static void test_x11_convert_to_vconsole(void) {
140 _cleanup_(context_free
) Context c
= {};
143 log_info("/*** %s ***/", __func__
);
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
);
150 log_info("/* test emptying second (:) */");
152 assert_se(x11_convert_to_vconsole(&c
) == 0);
153 assert_se(c
.vc_keymap
== NULL
);
155 log_info("/* test without variant, new mapping (es:) */");
156 assert_se(free_and_strdup(&c
.x11_layout
, "es") >= 0);
158 assert_se(x11_convert_to_vconsole(&c
) == 1);
159 assert_se(streq(c
.vc_keymap
, "es"));
161 log_info("/* test with unknown variant, new mapping (es:foobar) */");
162 assert_se(free_and_strdup(&c
.x11_variant
, "foobar") >= 0);
164 assert_se(x11_convert_to_vconsole(&c
) == 0);
165 assert_se(streq(c
.vc_keymap
, "es"));
167 log_info("/* test with known variant, new mapping (es:dvorak) */");
168 assert_se(free_and_strdup(&c
.x11_variant
, "dvorak") >= 0);
170 r
= x11_convert_to_vconsole(&c
);
172 log_info("Skipping rest of %s: keymaps are not installed", __func__
);
177 assert_se(streq(c
.vc_keymap
, "es-dvorak"));
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);
183 assert_se(x11_convert_to_vconsole(&c
) == 1);
184 assert_se(streq(c
.vc_keymap
, "fr-latin9"));
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);
190 assert_se(x11_convert_to_vconsole(&c
) == 1);
191 assert_se(streq(c
.vc_keymap
, "us"));
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);
197 assert_se(x11_convert_to_vconsole(&c
) == 1);
198 assert_se(streq(c
.vc_keymap
, "ru"));
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);
205 assert_se(x11_convert_to_vconsole(&c
) == 0);
206 assert_se(streq(c
.vc_keymap
, "ru"));
209 int main(int argc
, char **argv
) {
210 log_set_max_level(LOG_DEBUG
);
211 log_parse_environment();
213 test_find_language_fallback();
214 test_find_converted_keymap();
215 test_find_legacy_keymap();
217 test_vconsole_convert_to_x11();
218 test_x11_convert_to_vconsole();