| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "localed-util.h" |
| 7 |
#include "log.h" |
| 8 |
#include "string-util.h" |
| 9 |
#include "tests.h" |
| 10 |
|
| 11 |
TEST(find_language_fallback) { |
| 12 |
_cleanup_free_ char *ans = NULL, *ans2 = NULL; |
| 13 |
|
| 14 |
ASSERT_OK_ZERO(find_language_fallback("foobar", &ans)); |
| 15 |
ASSERT_NULL(ans); |
| 16 |
|
| 17 |
ASSERT_OK_ZERO(find_language_fallback("csb", &ans)); |
| 18 |
ASSERT_NULL(ans); |
| 19 |
|
| 20 |
ASSERT_OK_POSITIVE(find_language_fallback("csb_PL", &ans)); |
| 21 |
ASSERT_STREQ(ans, "csb:pl"); |
| 22 |
|
| 23 |
ASSERT_OK_POSITIVE(find_language_fallback("szl_PL", &ans2)); |
| 24 |
ASSERT_STREQ(ans2, "szl:pl"); |
| 25 |
} |
| 26 |
|
| 27 |
TEST(find_converted_keymap) { |
| 28 |
_cleanup_free_ char *ans = NULL, *ans2 = NULL; |
| 29 |
int r; |
| 30 |
|
| 31 |
ASSERT_OK_ZERO(find_converted_keymap( |
| 32 |
&(X11Context) { |
| 33 |
.layout = (char*) "pl", |
| 34 |
.variant = (char*) "foobar", |
| 35 |
}, &ans)); |
| 36 |
ASSERT_NULL(ans); |
| 37 |
|
| 38 |
ASSERT_OK(r = find_converted_keymap( |
| 39 |
&(X11Context) { |
| 40 |
.layout = (char*) "pl", |
| 41 |
}, &ans)); |
| 42 |
if (r == 0) |
| 43 |
return (void) log_tests_skipped("keymaps are not installed"); |
| 44 |
|
| 45 |
ASSERT_STREQ(ans, "pl"); |
| 46 |
ans = mfree(ans); |
| 47 |
|
| 48 |
ASSERT_OK_POSITIVE(find_converted_keymap( |
| 49 |
&(X11Context) { |
| 50 |
.layout = (char*) "pl", |
| 51 |
.variant = (char*) "dvorak", |
| 52 |
}, &ans2)); |
| 53 |
ASSERT_STREQ(ans2, "pl-dvorak"); |
| 54 |
} |
| 55 |
|
| 56 |
TEST(find_legacy_keymap) { |
| 57 |
X11Context xc = {}; |
| 58 |
_cleanup_free_ char *ans = NULL, *ans2 = NULL; |
| 59 |
|
| 60 |
xc.layout = (char*) "foobar"; |
| 61 |
ASSERT_OK_ZERO(find_legacy_keymap(&xc, &ans)); |
| 62 |
ASSERT_NULL(ans); |
| 63 |
|
| 64 |
xc.layout = (char*) "pl"; |
| 65 |
ASSERT_OK_POSITIVE(find_legacy_keymap(&xc, &ans)); |
| 66 |
ASSERT_STREQ(ans, "pl2"); |
| 67 |
|
| 68 |
xc.layout = (char*) "pl,ru"; |
| 69 |
ASSERT_OK_POSITIVE(find_legacy_keymap(&xc, &ans2)); |
| 70 |
ASSERT_STREQ(ans, "pl2"); |
| 71 |
} |
| 72 |
|
| 73 |
TEST(vconsole_convert_to_x11) { |
| 74 |
_cleanup_(x11_context_clear) X11Context xc = {}; |
| 75 |
_cleanup_(vc_context_clear) VCContext vc = {}; |
| 76 |
int r; |
| 77 |
|
| 78 |
log_info("/* test empty keymap */"); |
| 79 |
ASSERT_OK(vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 80 |
ASSERT_TRUE(x11_context_isempty(&xc)); |
| 81 |
|
| 82 |
log_info("/* test without variant, new mapping (es:) */"); |
| 83 |
ASSERT_OK(free_and_strdup(&vc.keymap, "es")); |
| 84 |
ASSERT_OK(vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 85 |
ASSERT_STREQ(xc.layout, "es"); |
| 86 |
ASSERT_NULL(xc.variant); |
| 87 |
x11_context_clear(&xc); |
| 88 |
|
| 89 |
log_info("/* test with known variant, new mapping (es:dvorak) */"); |
| 90 |
ASSERT_OK(free_and_strdup(&vc.keymap, "es-dvorak")); |
| 91 |
ASSERT_OK(vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 92 |
ASSERT_STREQ(xc.layout, "es"); |
| 93 |
ASSERT_STREQ(xc.variant, "dvorak"); |
| 94 |
x11_context_clear(&xc); |
| 95 |
|
| 96 |
log_info("/* test with old mapping (fr:latin9) */"); |
| 97 |
ASSERT_OK(free_and_strdup(&vc.keymap, "fr-latin9")); |
| 98 |
ASSERT_OK(vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 99 |
ASSERT_STREQ(xc.layout, "fr"); |
| 100 |
ASSERT_STREQ(xc.variant, "latin9"); |
| 101 |
x11_context_clear(&xc); |
| 102 |
|
| 103 |
log_info("/* test with a compound mapping (ru,us) */"); |
| 104 |
ASSERT_OK(free_and_strdup(&vc.keymap, "ru")); |
| 105 |
ASSERT_OK(vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 106 |
ASSERT_STREQ(xc.layout, "ru,us"); |
| 107 |
ASSERT_NULL(xc.variant); |
| 108 |
x11_context_clear(&xc); |
| 109 |
|
| 110 |
log_info("/* test with a simple mapping (us) */"); |
| 111 |
ASSERT_OK(free_and_strdup(&vc.keymap, "us")); |
| 112 |
ASSERT_OK(vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 113 |
ASSERT_STREQ(xc.layout, "us"); |
| 114 |
ASSERT_NULL(xc.variant); |
| 115 |
x11_context_clear(&xc); |
| 116 |
|
| 117 |
/* "gh" has no mapping in kbd-model-map and kbd provides a converted keymap for this layout. */ |
| 118 |
log_info("/* test with a converted keymap (gh:) */"); |
| 119 |
ASSERT_OK(free_and_strdup(&vc.keymap, "gh")); |
| 120 |
ASSERT_OK(r = vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 121 |
if (r == 0) |
| 122 |
return (void) log_tests_skipped("keymaps are not installed"); |
| 123 |
|
| 124 |
ASSERT_STREQ(xc.layout, "gh"); |
| 125 |
ASSERT_NULL(xc.variant); |
| 126 |
x11_context_clear(&xc); |
| 127 |
|
| 128 |
log_info("/* test with converted keymap and with a known variant (gh:ewe) */"); |
| 129 |
ASSERT_OK(free_and_strdup(&vc.keymap, "gh-ewe")); |
| 130 |
ASSERT_OK_POSITIVE(vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 131 |
ASSERT_STREQ(xc.layout, "gh"); |
| 132 |
ASSERT_STREQ(xc.variant, "ewe"); |
| 133 |
x11_context_clear(&xc); |
| 134 |
|
| 135 |
log_info("/* test with converted keymap and with an unknown variant (gh:ewe) */"); |
| 136 |
ASSERT_OK(free_and_strdup(&vc.keymap, "gh-foobar")); |
| 137 |
ASSERT_OK_POSITIVE(vconsole_convert_to_x11(&vc, x11_context_verify, &xc)); |
| 138 |
ASSERT_STREQ(xc.layout, "gh"); |
| 139 |
ASSERT_NULL(xc.variant); |
| 140 |
x11_context_clear(&xc); |
| 141 |
} |
| 142 |
|
| 143 |
TEST(x11_convert_to_vconsole) { |
| 144 |
_cleanup_(x11_context_clear) X11Context xc = {}; |
| 145 |
_cleanup_(vc_context_clear) VCContext vc = {}; |
| 146 |
|
| 147 |
log_info("/* test empty layout (:) */"); |
| 148 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 149 |
ASSERT_TRUE(vc_context_isempty(&vc)); |
| 150 |
|
| 151 |
log_info("/* test without variant, new mapping (es:) */"); |
| 152 |
ASSERT_OK(free_and_strdup(&xc.layout, "es")); |
| 153 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 154 |
ASSERT_STREQ(vc.keymap, "es"); |
| 155 |
vc_context_clear(&vc); |
| 156 |
|
| 157 |
log_info("/* test with unknown variant, new mapping (es:foobar) */"); |
| 158 |
ASSERT_OK(free_and_strdup(&xc.variant, "foobar")); |
| 159 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 160 |
ASSERT_STREQ(vc.keymap, "es"); |
| 161 |
vc_context_clear(&vc); |
| 162 |
|
| 163 |
log_info("/* test with known variant, new mapping (es:dvorak) */"); |
| 164 |
ASSERT_OK(free_and_strdup(&xc.variant, "dvorak")); |
| 165 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 166 |
if (vc_context_isempty(&vc)) { |
| 167 |
log_info("Skipping rest of %s: keymaps are not installed", __func__); |
| 168 |
return; |
| 169 |
} |
| 170 |
ASSERT_STREQ(vc.keymap, "es-dvorak"); |
| 171 |
vc_context_clear(&vc); |
| 172 |
|
| 173 |
/* es no-variant test is not very good as the desired match |
| 174 |
comes first in the list so will win if both candidates score |
| 175 |
the same. in this case the desired match comes second so will |
| 176 |
not win unless we correctly give the no-variant match a bonus |
| 177 |
*/ |
| 178 |
log_info("/* test without variant, desired match second (bg,us:) */"); |
| 179 |
ASSERT_OK(free_and_strdup(&xc.layout, "bg,us")); |
| 180 |
ASSERT_OK(free_and_strdup(&xc.variant, NULL)); |
| 181 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 182 |
ASSERT_STREQ(vc.keymap, "bg_bds-utf8"); |
| 183 |
vc_context_clear(&vc); |
| 184 |
|
| 185 |
/* same, but with variant specified as "," */ |
| 186 |
log_info("/* test with variant as ',', desired match second (bg,us:) */"); |
| 187 |
ASSERT_OK(free_and_strdup(&xc.variant, ",")); |
| 188 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 189 |
ASSERT_STREQ(vc.keymap, "bg_bds-utf8"); |
| 190 |
vc_context_clear(&vc); |
| 191 |
|
| 192 |
log_info("/* test with old mapping (fr:latin9) */"); |
| 193 |
ASSERT_OK(free_and_strdup(&xc.layout, "fr")); |
| 194 |
ASSERT_OK(free_and_strdup(&xc.variant, "latin9")); |
| 195 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 196 |
ASSERT_STREQ(vc.keymap, "fr-latin9"); |
| 197 |
vc_context_clear(&vc); |
| 198 |
|
| 199 |
/* https://bugzilla.redhat.com/show_bug.cgi?id=1039185 */ |
| 200 |
/* us,ru is the x config users want, but they still want ru |
| 201 |
as the console layout in this case */ |
| 202 |
log_info("/* test with a compound mapping (us,ru:) */"); |
| 203 |
ASSERT_OK(free_and_strdup(&xc.layout, "us,ru")); |
| 204 |
ASSERT_OK(free_and_strdup(&xc.variant, NULL)); |
| 205 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 206 |
ASSERT_STREQ(vc.keymap, "ru"); |
| 207 |
vc_context_clear(&vc); |
| 208 |
|
| 209 |
log_info("/* test with a compound mapping (ru,us:) */"); |
| 210 |
ASSERT_OK(free_and_strdup(&xc.layout, "ru,us")); |
| 211 |
ASSERT_OK(free_and_strdup(&xc.variant, NULL)); |
| 212 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 213 |
ASSERT_STREQ(vc.keymap, "ru"); |
| 214 |
vc_context_clear(&vc); |
| 215 |
|
| 216 |
/* https://bugzilla.redhat.com/show_bug.cgi?id=1333998 */ |
| 217 |
log_info("/* test with a simple new mapping (ru:) */"); |
| 218 |
ASSERT_OK(free_and_strdup(&xc.layout, "ru")); |
| 219 |
ASSERT_OK(free_and_strdup(&xc.variant, NULL)); |
| 220 |
ASSERT_OK(x11_convert_to_vconsole(&xc, &vc)); |
| 221 |
ASSERT_STREQ(vc.keymap, "ru"); |
| 222 |
} |
| 223 |
|
| 224 |
static int intro(void) { |
| 225 |
_cleanup_free_ char *map = NULL; |
| 226 |
|
| 227 |
ASSERT_OK(get_testdata_dir("test-keymap-util/kbd-model-map", &map)); |
| 228 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_KBD_MODEL_MAP", map, /* overwrite= */ true)); |
| 229 |
|
| 230 |
return EXIT_SUCCESS; |
| 231 |
} |
| 232 |
|
| 233 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |