]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/test-keymap-util.c
2 This file is part of systemd.
4 Copyright 2016 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
20 #include "alloc-util.h"
21 #include "keymap-util.h"
23 #include "string-util.h"
25 static void test_find_language_fallback(void) {
26 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
29 log_info("/* %s */", __func__
);
31 r
= find_language_fallback("foobar", &ans
);
33 log_info_errno(r
, "Skipping language fallback tests: %m");
37 assert_se(ans
== NULL
);
39 assert_se(find_language_fallback("csb", &ans
) == 0);
40 assert_se(ans
== NULL
);
42 assert_se(find_language_fallback("csb_PL", &ans
) == 1);
43 assert_se(streq(ans
, "csb:pl"));
45 assert_se(find_language_fallback("szl_PL", &ans2
) == 1);
46 assert_se(streq(ans2
, "szl:pl"));
49 static void test_find_converted_keymap(void) {
50 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
53 log_info("/* %s */", __func__
);
55 assert_se(find_converted_keymap("pl", "foobar", &ans
) == 0);
56 assert_se(ans
== NULL
);
58 r
= find_converted_keymap("pl", NULL
, &ans
);
60 log_info_errno(r
, "Skipping find_converted_keymap tests: %m");
64 assert_se(streq(ans
, "pl"));
66 assert_se(find_converted_keymap("pl", "dvorak", &ans
) == 1);
67 assert_se(streq(ans
, "pl-dvorak"));
70 static void test_find_legacy_keymap(void) {
72 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
75 log_info("/* %s */", __func__
);
77 c
.x11_layout
= (char*) "foobar";
78 r
= find_legacy_keymap(&c
, &ans
);
80 log_info_errno(r
, "Skipping test_legacy_keymap tests: %m");
84 assert_se(ans
== NULL
);
86 c
.x11_layout
= (char*) "pl";
87 assert_se(find_legacy_keymap(&c
, &ans
) == 1);
88 assert_se(streq(ans
, "pl2"));
90 c
.x11_layout
= (char*) "pl,ru";
91 assert_se(find_legacy_keymap(&c
, &ans2
) == 1);
92 assert_se(streq(ans
, "pl2"));
95 static void test_vconsole_convert_to_x11(void) {
96 _cleanup_(context_free
) Context c
= {};
98 log_info("/* %s */", __func__
);
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
);
107 log_info("/* test emptying second (:) */");
109 assert_se(vconsole_convert_to_x11(&c
) == 0);
110 assert_se(c
.x11_layout
== NULL
);
111 assert_se(c
.x11_variant
== NULL
);
113 log_info("/* test without variant, new mapping (es:) */");
114 assert_se(free_and_strdup(&c
.vc_keymap
, "es") >= 0);
116 assert_se(vconsole_convert_to_x11(&c
) == 1);
117 assert_se(streq(c
.x11_layout
, "es"));
118 assert_se(c
.x11_variant
== NULL
);
120 log_info("/* test with known variant, new mapping (es:dvorak) */");
121 assert_se(free_and_strdup(&c
.vc_keymap
, "es-dvorak") >= 0);
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"
127 log_info("/* test with old mapping (fr:latin9) */");
128 assert_se(free_and_strdup(&c
.vc_keymap
, "fr-latin9") >= 0);
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"));
134 log_info("/* test with a compound mapping (ru,us) */");
135 assert_se(free_and_strdup(&c
.vc_keymap
, "ru") >= 0);
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
);
141 log_info("/* test with a simple mapping (us) */");
142 assert_se(free_and_strdup(&c
.vc_keymap
, "us") >= 0);
144 assert_se(vconsole_convert_to_x11(&c
) == 1);
145 assert_se(streq(c
.x11_layout
, "us"));
146 assert_se(c
.x11_variant
== NULL
);
149 static void test_x11_convert_to_vconsole(void) {
150 _cleanup_(context_free
) Context c
= {};
152 log_info("/* %s */", __func__
);
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
);
159 log_info("/* test emptying second (:) */");
161 assert_se(x11_convert_to_vconsole(&c
) == 0);
162 assert_se(c
.vc_keymap
== NULL
);
164 log_info("/* test without variant, new mapping (es:) */");
165 assert_se(free_and_strdup(&c
.x11_layout
, "es") >= 0);
167 assert_se(x11_convert_to_vconsole(&c
) == 1);
168 assert_se(streq(c
.vc_keymap
, "es"));
170 log_info("/* test with unknown variant, new mapping (es:foobar) */");
171 assert_se(free_and_strdup(&c
.x11_variant
, "foobar") >= 0);
173 assert_se(x11_convert_to_vconsole(&c
) == 0);
174 assert_se(streq(c
.vc_keymap
, "es"));
176 log_info("/* test with known variant, new mapping (es:dvorak) */");
177 assert_se(free_and_strdup(&c
.x11_variant
, "dvorak") >= 0);
179 assert_se(x11_convert_to_vconsole(&c
) == 1);
180 assert_se(streq(c
.vc_keymap
, "es-dvorak"));
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);
186 assert_se(x11_convert_to_vconsole(&c
) == 1);
187 assert_se(streq(c
.vc_keymap
, "fr-latin9"));
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);
193 assert_se(x11_convert_to_vconsole(&c
) == 1);
194 assert_se(streq(c
.vc_keymap
, "us"));
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);
200 assert_se(x11_convert_to_vconsole(&c
) == 1);
201 assert_se(streq(c
.vc_keymap
, "ru"));
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);
208 assert_se(x11_convert_to_vconsole(&c
) == 0);
209 assert_se(streq(c
.vc_keymap
, "ru"));
212 int main(int argc
, char **argv
) {
213 log_set_max_level(LOG_DEBUG
);
214 log_parse_environment();
216 test_find_language_fallback();
217 test_find_converted_keymap();
218 test_find_legacy_keymap();
220 test_vconsole_convert_to_x11();
221 test_x11_convert_to_vconsole();