]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/test-keymap-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "keymap-util.h"
6 #include "string-util.h"
8 static void test_find_language_fallback(void) {
9 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
11 log_info("/*** %s ***/", __func__
);
13 assert_se(find_language_fallback("foobar", &ans
) == 0);
14 assert_se(ans
== NULL
);
16 assert_se(find_language_fallback("csb", &ans
) == 0);
17 assert_se(ans
== NULL
);
19 assert_se(find_language_fallback("csb_PL", &ans
) == 1);
20 assert_se(streq(ans
, "csb:pl"));
22 assert_se(find_language_fallback("szl_PL", &ans2
) == 1);
23 assert_se(streq(ans2
, "szl:pl"));
26 static void test_find_converted_keymap(void) {
27 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
30 log_info("/*** %s ***/", __func__
);
32 assert_se(find_converted_keymap("pl", "foobar", &ans
) == 0);
33 assert_se(ans
== NULL
);
35 r
= find_converted_keymap("pl", NULL
, &ans
);
37 log_info("Skipping rest of %s: keymaps are not installed", __func__
);
42 assert_se(streq(ans
, "pl"));
44 assert_se(find_converted_keymap("pl", "dvorak", &ans2
) == 1);
45 assert_se(streq(ans2
, "pl-dvorak"));
48 static void test_find_legacy_keymap(void) {
50 _cleanup_free_
char *ans
= NULL
, *ans2
= NULL
;
52 log_info("/*** %s ***/", __func__
);
54 c
.x11_layout
= (char*) "foobar";
55 assert_se(find_legacy_keymap(&c
, &ans
) == 0);
56 assert_se(ans
== NULL
);
58 c
.x11_layout
= (char*) "pl";
59 assert_se(find_legacy_keymap(&c
, &ans
) == 1);
60 assert_se(streq(ans
, "pl2"));
62 c
.x11_layout
= (char*) "pl,ru";
63 assert_se(find_legacy_keymap(&c
, &ans2
) == 1);
64 assert_se(streq(ans
, "pl2"));
67 static void test_vconsole_convert_to_x11(void) {
68 _cleanup_(context_free
) Context c
= {};
70 log_info("/*** %s ***/", __func__
);
72 log_info("/* test emptying first (:) */");
73 assert_se(free_and_strdup(&c
.x11_layout
, "foo") >= 0);
74 assert_se(free_and_strdup(&c
.x11_variant
, "bar") >= 0);
75 assert_se(vconsole_convert_to_x11(&c
) == 1);
76 assert_se(c
.x11_layout
== NULL
);
77 assert_se(c
.x11_variant
== NULL
);
79 log_info("/* test emptying second (:) */");
81 assert_se(vconsole_convert_to_x11(&c
) == 0);
82 assert_se(c
.x11_layout
== NULL
);
83 assert_se(c
.x11_variant
== NULL
);
85 log_info("/* test without variant, new mapping (es:) */");
86 assert_se(free_and_strdup(&c
.vc_keymap
, "es") >= 0);
88 assert_se(vconsole_convert_to_x11(&c
) == 1);
89 assert_se(streq(c
.x11_layout
, "es"));
90 assert_se(c
.x11_variant
== NULL
);
92 log_info("/* test with known variant, new mapping (es:dvorak) */");
93 assert_se(free_and_strdup(&c
.vc_keymap
, "es-dvorak") >= 0);
95 assert_se(vconsole_convert_to_x11(&c
) == 0); // FIXME
96 assert_se(streq(c
.x11_layout
, "es"));
97 assert_se(c
.x11_variant
== NULL
); // FIXME: "dvorak"
99 log_info("/* test with old mapping (fr:latin9) */");
100 assert_se(free_and_strdup(&c
.vc_keymap
, "fr-latin9") >= 0);
102 assert_se(vconsole_convert_to_x11(&c
) == 1);
103 assert_se(streq(c
.x11_layout
, "fr"));
104 assert_se(streq(c
.x11_variant
, "latin9"));
106 log_info("/* test with a compound mapping (ru,us) */");
107 assert_se(free_and_strdup(&c
.vc_keymap
, "ru") >= 0);
109 assert_se(vconsole_convert_to_x11(&c
) == 1);
110 assert_se(streq(c
.x11_layout
, "ru,us"));
111 assert_se(c
.x11_variant
== NULL
);
113 log_info("/* test with a simple mapping (us) */");
114 assert_se(free_and_strdup(&c
.vc_keymap
, "us") >= 0);
116 assert_se(vconsole_convert_to_x11(&c
) == 1);
117 assert_se(streq(c
.x11_layout
, "us"));
118 assert_se(c
.x11_variant
== NULL
);
121 static void test_x11_convert_to_vconsole(void) {
122 _cleanup_(context_free
) Context c
= {};
125 log_info("/*** %s ***/", __func__
);
127 log_info("/* test emptying first (:) */");
128 assert_se(free_and_strdup(&c
.vc_keymap
, "foobar") >= 0);
129 assert_se(x11_convert_to_vconsole(&c
) == 1);
130 assert_se(c
.vc_keymap
== NULL
);
132 log_info("/* test emptying second (:) */");
134 assert_se(x11_convert_to_vconsole(&c
) == 0);
135 assert_se(c
.vc_keymap
== NULL
);
137 log_info("/* test without variant, new mapping (es:) */");
138 assert_se(free_and_strdup(&c
.x11_layout
, "es") >= 0);
140 assert_se(x11_convert_to_vconsole(&c
) == 1);
141 assert_se(streq(c
.vc_keymap
, "es"));
143 log_info("/* test with unknown variant, new mapping (es:foobar) */");
144 assert_se(free_and_strdup(&c
.x11_variant
, "foobar") >= 0);
146 assert_se(x11_convert_to_vconsole(&c
) == 0);
147 assert_se(streq(c
.vc_keymap
, "es"));
149 log_info("/* test with known variant, new mapping (es:dvorak) */");
150 assert_se(free_and_strdup(&c
.x11_variant
, "dvorak") >= 0);
152 r
= x11_convert_to_vconsole(&c
);
154 log_info("Skipping rest of %s: keymaps are not installed", __func__
);
159 assert_se(streq(c
.vc_keymap
, "es-dvorak"));
161 log_info("/* test with old mapping (fr:latin9) */");
162 assert_se(free_and_strdup(&c
.x11_layout
, "fr") >= 0);
163 assert_se(free_and_strdup(&c
.x11_variant
, "latin9") >= 0);
165 assert_se(x11_convert_to_vconsole(&c
) == 1);
166 assert_se(streq(c
.vc_keymap
, "fr-latin9"));
168 log_info("/* test with a compound mapping (us,ru:) */");
169 assert_se(free_and_strdup(&c
.x11_layout
, "us,ru") >= 0);
170 assert_se(free_and_strdup(&c
.x11_variant
, NULL
) >= 0);
172 assert_se(x11_convert_to_vconsole(&c
) == 1);
173 assert_se(streq(c
.vc_keymap
, "us"));
175 log_info("/* test with a compound mapping (ru,us:) */");
176 assert_se(free_and_strdup(&c
.x11_layout
, "ru,us") >= 0);
177 assert_se(free_and_strdup(&c
.x11_variant
, NULL
) >= 0);
179 assert_se(x11_convert_to_vconsole(&c
) == 1);
180 assert_se(streq(c
.vc_keymap
, "ru"));
182 /* https://bugzilla.redhat.com/show_bug.cgi?id=1333998 */
183 log_info("/* test with a simple new mapping (ru:) */");
184 assert_se(free_and_strdup(&c
.x11_layout
, "ru") >= 0);
185 assert_se(free_and_strdup(&c
.x11_variant
, NULL
) >= 0);
187 assert_se(x11_convert_to_vconsole(&c
) == 0);
188 assert_se(streq(c
.vc_keymap
, "ru"));
191 int main(int argc
, char **argv
) {
192 log_set_max_level(LOG_DEBUG
);
193 log_parse_environment();
195 test_find_language_fallback();
196 test_find_converted_keymap();
197 test_find_legacy_keymap();
199 test_vconsole_convert_to_x11();
200 test_x11_convert_to_vconsole();