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