]>
git.proxmox.com Git - 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
;
28 log_info("/*** %s ***/", __func__
);
30 assert_se(find_language_fallback("foobar", &ans
) == 0);
31 assert_se(ans
== NULL
);
33 assert_se(find_language_fallback("csb", &ans
) == 0);
34 assert_se(ans
== NULL
);
36 assert_se(find_language_fallback("csb_PL", &ans
) == 1);
37 assert_se(streq(ans
, "csb:pl"));
39 assert_se(find_language_fallback("szl_PL", &ans2
) == 1);
40 assert_se(streq(ans2
, "szl:pl"));
43 static void test_find_converted_keymap(void) {
44 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
47 log_info("/*** %s ***/", __func__
);
49 assert_se(find_converted_keymap("pl", "foobar", &ans
) == 0);
50 assert_se(ans
== NULL
);
52 r
= find_converted_keymap("pl", NULL
, &ans
);
54 log_info("Skipping rest of %s: keymaps are not installed", __func__
);
59 assert_se(streq(ans
, "pl"));
61 assert_se(find_converted_keymap("pl", "dvorak", &ans2
) == 1);
62 assert_se(streq(ans2
, "pl-dvorak"));
65 static void test_find_legacy_keymap(void) {
67 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
69 log_info("/*** %s ***/", __func__
);
71 c
.x11_layout
= (char*) "foobar";
72 assert_se(find_legacy_keymap(&c
, &ans
) == 0);
73 assert_se(ans
== NULL
);
75 c
.x11_layout
= (char*) "pl";
76 assert_se(find_legacy_keymap(&c
, &ans
) == 1);
77 assert_se(streq(ans
, "pl2"));
79 c
.x11_layout
= (char*) "pl,ru";
80 assert_se(find_legacy_keymap(&c
, &ans2
) == 1);
81 assert_se(streq(ans
, "pl2"));
84 static void test_vconsole_convert_to_x11(void) {
85 _cleanup_(context_free
) Context c
= {};
87 log_info("/*** %s ***/", __func__
);
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
);
96 log_info("/* test emptying second (:) */");
98 assert_se(vconsole_convert_to_x11(&c
) == 0);
99 assert_se(c
.x11_layout
== NULL
);
100 assert_se(c
.x11_variant
== NULL
);
102 log_info("/* test without variant, new mapping (es:) */");
103 assert_se(free_and_strdup(&c
.vc_keymap
, "es") >= 0);
105 assert_se(vconsole_convert_to_x11(&c
) == 1);
106 assert_se(streq(c
.x11_layout
, "es"));
107 assert_se(c
.x11_variant
== NULL
);
109 log_info("/* test with known variant, new mapping (es:dvorak) */");
110 assert_se(free_and_strdup(&c
.vc_keymap
, "es-dvorak") >= 0);
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"
116 log_info("/* test with old mapping (fr:latin9) */");
117 assert_se(free_and_strdup(&c
.vc_keymap
, "fr-latin9") >= 0);
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"));
123 log_info("/* test with a compound mapping (ru,us) */");
124 assert_se(free_and_strdup(&c
.vc_keymap
, "ru") >= 0);
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
);
130 log_info("/* test with a simple mapping (us) */");
131 assert_se(free_and_strdup(&c
.vc_keymap
, "us") >= 0);
133 assert_se(vconsole_convert_to_x11(&c
) == 1);
134 assert_se(streq(c
.x11_layout
, "us"));
135 assert_se(c
.x11_variant
== NULL
);
138 static void test_x11_convert_to_vconsole(void) {
139 _cleanup_(context_free
) Context c
= {};
142 log_info("/*** %s ***/", __func__
);
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
);
149 log_info("/* test emptying second (:) */");
151 assert_se(x11_convert_to_vconsole(&c
) == 0);
152 assert_se(c
.vc_keymap
== NULL
);
154 log_info("/* test without variant, new mapping (es:) */");
155 assert_se(free_and_strdup(&c
.x11_layout
, "es") >= 0);
157 assert_se(x11_convert_to_vconsole(&c
) == 1);
158 assert_se(streq(c
.vc_keymap
, "es"));
160 log_info("/* test with unknown variant, new mapping (es:foobar) */");
161 assert_se(free_and_strdup(&c
.x11_variant
, "foobar") >= 0);
163 assert_se(x11_convert_to_vconsole(&c
) == 0);
164 assert_se(streq(c
.vc_keymap
, "es"));
166 log_info("/* test with known variant, new mapping (es:dvorak) */");
167 assert_se(free_and_strdup(&c
.x11_variant
, "dvorak") >= 0);
169 r
= x11_convert_to_vconsole(&c
);
171 log_info("Skipping rest of %s: keymaps are not installed", __func__
);
176 assert_se(streq(c
.vc_keymap
, "es-dvorak"));
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);
182 assert_se(x11_convert_to_vconsole(&c
) == 1);
183 assert_se(streq(c
.vc_keymap
, "fr-latin9"));
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);
189 assert_se(x11_convert_to_vconsole(&c
) == 1);
190 assert_se(streq(c
.vc_keymap
, "us"));
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);
196 assert_se(x11_convert_to_vconsole(&c
) == 1);
197 assert_se(streq(c
.vc_keymap
, "ru"));
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);
204 assert_se(x11_convert_to_vconsole(&c
) == 0);
205 assert_se(streq(c
.vc_keymap
, "ru"));
208 int main(int argc
, char **argv
) {
209 log_set_max_level(LOG_DEBUG
);
210 log_parse_environment();
212 test_find_language_fallback();
213 test_find_converted_keymap();
214 test_find_legacy_keymap();
216 test_vconsole_convert_to_x11();
217 test_x11_convert_to_vconsole();