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