Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "alloc-util.h"
4 : #include "keymap-util.h"
5 : #include "log.h"
6 : #include "string-util.h"
7 : #include "tests.h"
8 :
9 1 : static void test_find_language_fallback(void) {
10 1 : _cleanup_free_ char *ans = NULL, *ans2 = NULL;
11 :
12 1 : log_info("/*** %s ***/", __func__);
13 :
14 1 : assert_se(find_language_fallback("foobar", &ans) == 0);
15 1 : assert_se(ans == NULL);
16 :
17 1 : assert_se(find_language_fallback("csb", &ans) == 0);
18 1 : assert_se(ans == NULL);
19 :
20 1 : assert_se(find_language_fallback("csb_PL", &ans) == 1);
21 1 : assert_se(streq(ans, "csb:pl"));
22 :
23 1 : assert_se(find_language_fallback("szl_PL", &ans2) == 1);
24 1 : assert_se(streq(ans2, "szl:pl"));
25 1 : }
26 :
27 1 : static void test_find_converted_keymap(void) {
28 1 : _cleanup_free_ char *ans = NULL, *ans2 = NULL;
29 : int r;
30 :
31 1 : log_info("/*** %s ***/", __func__);
32 :
33 1 : assert_se(find_converted_keymap("pl", "foobar", &ans) == 0);
34 1 : assert_se(ans == NULL);
35 :
36 1 : r = find_converted_keymap("pl", NULL, &ans);
37 1 : if (r == 0) {
38 0 : log_info("Skipping rest of %s: keymaps are not installed", __func__);
39 0 : return;
40 : }
41 :
42 1 : assert_se(r == 1);
43 1 : assert_se(streq(ans, "pl"));
44 :
45 1 : assert_se(find_converted_keymap("pl", "dvorak", &ans2) == 1);
46 1 : assert_se(streq(ans2, "pl-dvorak"));
47 : }
48 :
49 1 : static void test_find_legacy_keymap(void) {
50 1 : Context c = {};
51 1 : _cleanup_free_ char *ans = NULL, *ans2 = NULL;
52 :
53 1 : log_info("/*** %s ***/", __func__);
54 :
55 1 : c.x11_layout = (char*) "foobar";
56 1 : assert_se(find_legacy_keymap(&c, &ans) == 0);
57 1 : assert_se(ans == NULL);
58 :
59 1 : c.x11_layout = (char*) "pl";
60 1 : assert_se(find_legacy_keymap(&c, &ans) == 1);
61 1 : assert_se(streq(ans, "pl2"));
62 :
63 1 : c.x11_layout = (char*) "pl,ru";
64 1 : assert_se(find_legacy_keymap(&c, &ans2) == 1);
65 1 : assert_se(streq(ans, "pl2"));
66 1 : }
67 :
68 1 : static void test_vconsole_convert_to_x11(void) {
69 1 : _cleanup_(context_clear) Context c = {};
70 :
71 1 : log_info("/*** %s ***/", __func__);
72 :
73 1 : log_info("/* test emptying first (:) */");
74 1 : assert_se(free_and_strdup(&c.x11_layout, "foo") >= 0);
75 1 : assert_se(free_and_strdup(&c.x11_variant, "bar") >= 0);
76 1 : assert_se(vconsole_convert_to_x11(&c) == 1);
77 1 : assert_se(c.x11_layout == NULL);
78 1 : assert_se(c.x11_variant == NULL);
79 :
80 1 : log_info("/* test emptying second (:) */");
81 :
82 1 : assert_se(vconsole_convert_to_x11(&c) == 0);
83 1 : assert_se(c.x11_layout == NULL);
84 1 : assert_se(c.x11_variant == NULL);
85 :
86 1 : log_info("/* test without variant, new mapping (es:) */");
87 1 : assert_se(free_and_strdup(&c.vc_keymap, "es") >= 0);
88 :
89 1 : assert_se(vconsole_convert_to_x11(&c) == 1);
90 1 : assert_se(streq(c.x11_layout, "es"));
91 1 : assert_se(c.x11_variant == NULL);
92 :
93 1 : log_info("/* test with known variant, new mapping (es:dvorak) */");
94 1 : assert_se(free_and_strdup(&c.vc_keymap, "es-dvorak") >= 0);
95 :
96 1 : assert_se(vconsole_convert_to_x11(&c) == 0); // FIXME
97 1 : assert_se(streq(c.x11_layout, "es"));
98 1 : assert_se(c.x11_variant == NULL); // FIXME: "dvorak"
99 :
100 1 : log_info("/* test with old mapping (fr:latin9) */");
101 1 : assert_se(free_and_strdup(&c.vc_keymap, "fr-latin9") >= 0);
102 :
103 1 : assert_se(vconsole_convert_to_x11(&c) == 1);
104 1 : assert_se(streq(c.x11_layout, "fr"));
105 1 : assert_se(streq(c.x11_variant, "latin9"));
106 :
107 1 : log_info("/* test with a compound mapping (ru,us) */");
108 1 : assert_se(free_and_strdup(&c.vc_keymap, "ru") >= 0);
109 :
110 1 : assert_se(vconsole_convert_to_x11(&c) == 1);
111 1 : assert_se(streq(c.x11_layout, "ru,us"));
112 1 : assert_se(c.x11_variant == NULL);
113 :
114 1 : log_info("/* test with a simple mapping (us) */");
115 1 : assert_se(free_and_strdup(&c.vc_keymap, "us") >= 0);
116 :
117 1 : assert_se(vconsole_convert_to_x11(&c) == 1);
118 1 : assert_se(streq(c.x11_layout, "us"));
119 1 : assert_se(c.x11_variant == NULL);
120 1 : }
121 :
122 1 : static void test_x11_convert_to_vconsole(void) {
123 1 : _cleanup_(context_clear) Context c = {};
124 : int r;
125 :
126 1 : log_info("/*** %s ***/", __func__);
127 :
128 1 : log_info("/* test emptying first (:) */");
129 1 : assert_se(free_and_strdup(&c.vc_keymap, "foobar") >= 0);
130 1 : assert_se(x11_convert_to_vconsole(&c) == 1);
131 1 : assert_se(c.vc_keymap == NULL);
132 :
133 1 : log_info("/* test emptying second (:) */");
134 :
135 1 : assert_se(x11_convert_to_vconsole(&c) == 0);
136 1 : assert_se(c.vc_keymap == NULL);
137 :
138 1 : log_info("/* test without variant, new mapping (es:) */");
139 1 : assert_se(free_and_strdup(&c.x11_layout, "es") >= 0);
140 :
141 1 : assert_se(x11_convert_to_vconsole(&c) == 1);
142 1 : assert_se(streq(c.vc_keymap, "es"));
143 :
144 1 : log_info("/* test with unknown variant, new mapping (es:foobar) */");
145 1 : assert_se(free_and_strdup(&c.x11_variant, "foobar") >= 0);
146 :
147 1 : assert_se(x11_convert_to_vconsole(&c) == 0);
148 1 : assert_se(streq(c.vc_keymap, "es"));
149 :
150 1 : log_info("/* test with known variant, new mapping (es:dvorak) */");
151 1 : assert_se(free_and_strdup(&c.x11_variant, "dvorak") >= 0);
152 :
153 1 : r = x11_convert_to_vconsole(&c);
154 1 : if (r == 0) {
155 0 : log_info("Skipping rest of %s: keymaps are not installed", __func__);
156 0 : return;
157 : }
158 :
159 1 : assert_se(r == 1);
160 1 : assert_se(streq(c.vc_keymap, "es-dvorak"));
161 :
162 1 : log_info("/* test with old mapping (fr:latin9) */");
163 1 : assert_se(free_and_strdup(&c.x11_layout, "fr") >= 0);
164 1 : assert_se(free_and_strdup(&c.x11_variant, "latin9") >= 0);
165 :
166 1 : assert_se(x11_convert_to_vconsole(&c) == 1);
167 1 : assert_se(streq(c.vc_keymap, "fr-latin9"));
168 :
169 1 : log_info("/* test with a compound mapping (us,ru:) */");
170 1 : assert_se(free_and_strdup(&c.x11_layout, "us,ru") >= 0);
171 1 : assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
172 :
173 1 : assert_se(x11_convert_to_vconsole(&c) == 1);
174 1 : assert_se(streq(c.vc_keymap, "us"));
175 :
176 1 : log_info("/* test with a compound mapping (ru,us:) */");
177 1 : assert_se(free_and_strdup(&c.x11_layout, "ru,us") >= 0);
178 1 : assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
179 :
180 1 : assert_se(x11_convert_to_vconsole(&c) == 1);
181 1 : assert_se(streq(c.vc_keymap, "ru"));
182 :
183 : /* https://bugzilla.redhat.com/show_bug.cgi?id=1333998 */
184 1 : log_info("/* test with a simple new mapping (ru:) */");
185 1 : assert_se(free_and_strdup(&c.x11_layout, "ru") >= 0);
186 1 : assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
187 :
188 1 : assert_se(x11_convert_to_vconsole(&c) == 0);
189 1 : assert_se(streq(c.vc_keymap, "ru"));
190 : }
191 :
192 1 : int main(int argc, char **argv) {
193 1 : test_setup_logging(LOG_DEBUG);
194 :
195 1 : test_find_language_fallback();
196 1 : test_find_converted_keymap();
197 1 : test_find_legacy_keymap();
198 :
199 1 : test_vconsole_convert_to_x11();
200 1 : test_x11_convert_to_vconsole();
201 :
202 1 : return 0;
203 : }
|