summaryrefslogtreecommitdiff
path: root/src/locale/test-keymap-util.c
blob: bded12239949fc7fa1ece113cf365a0272b8a529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "keymap-util.h"
#include "log.h"
#include "string-util.h"
#include "tests.h"

static void test_find_language_fallback(void) {
        _cleanup_free_ char *ans = NULL, *ans2 = NULL;

        log_info("/*** %s ***/", __func__);

        assert_se(find_language_fallback("foobar", &ans) == 0);
        assert_se(ans == NULL);

        assert_se(find_language_fallback("csb", &ans) == 0);
        assert_se(ans == NULL);

        assert_se(find_language_fallback("csb_PL", &ans) == 1);
        assert_se(streq(ans, "csb:pl"));

        assert_se(find_language_fallback("szl_PL", &ans2) == 1);
        assert_se(streq(ans2, "szl:pl"));
}

static void test_find_converted_keymap(void) {
        _cleanup_free_ char *ans = NULL, *ans2 = NULL;
        int r;

        log_info("/*** %s ***/", __func__);

        assert_se(find_converted_keymap("pl", "foobar", &ans) == 0);
        assert_se(ans == NULL);

        r = find_converted_keymap("pl", NULL, &ans);
        if (r == 0) {
                log_info("Skipping rest of %s: keymaps are not installed", __func__);
                return;
        }

        assert_se(r == 1);
        assert_se(streq(ans, "pl"));

        assert_se(find_converted_keymap("pl", "dvorak", &ans2) == 1);
        assert_se(streq(ans2, "pl-dvorak"));
}

static void test_find_legacy_keymap(void) {
        Context c = {};
        _cleanup_free_ char *ans = NULL, *ans2 = NULL;

        log_info("/*** %s ***/", __func__);

        c.x11_layout = (char*) "foobar";
        assert_se(find_legacy_keymap(&c, &ans) == 0);
        assert_se(ans == NULL);

        c.x11_layout = (char*) "pl";
        assert_se(find_legacy_keymap(&c, &ans) == 1);
        assert_se(streq(ans, "pl2"));

        c.x11_layout = (char*) "pl,ru";
        assert_se(find_legacy_keymap(&c, &ans2) == 1);
        assert_se(streq(ans, "pl2"));
}

static void test_vconsole_convert_to_x11(void) {
        _cleanup_(context_clear) Context c = {};

        log_info("/*** %s ***/", __func__);

        log_info("/* test emptying first (:) */");
        assert_se(free_and_strdup(&c.x11_layout, "foo") >= 0);
        assert_se(free_and_strdup(&c.x11_variant, "bar") >= 0);
        assert_se(vconsole_convert_to_x11(&c) == 1);
        assert_se(c.x11_layout == NULL);
        assert_se(c.x11_variant == NULL);

        log_info("/* test emptying second (:) */");

        assert_se(vconsole_convert_to_x11(&c) == 0);
        assert_se(c.x11_layout == NULL);
        assert_se(c.x11_variant == NULL);

        log_info("/* test without variant, new mapping (es:) */");
        assert_se(free_and_strdup(&c.vc_keymap, "es") >= 0);

        assert_se(vconsole_convert_to_x11(&c) == 1);
        assert_se(streq(c.x11_layout, "es"));
        assert_se(c.x11_variant == NULL);

        log_info("/* test with known variant, new mapping (es:dvorak) */");
        assert_se(free_and_strdup(&c.vc_keymap, "es-dvorak") >= 0);

        assert_se(vconsole_convert_to_x11(&c) == 0); // FIXME
        assert_se(streq(c.x11_layout, "es"));
        assert_se(c.x11_variant == NULL); // FIXME: "dvorak"

        log_info("/* test with old mapping (fr:latin9) */");
        assert_se(free_and_strdup(&c.vc_keymap, "fr-latin9") >= 0);

        assert_se(vconsole_convert_to_x11(&c) == 1);
        assert_se(streq(c.x11_layout, "fr"));
        assert_se(streq(c.x11_variant, "latin9"));

        log_info("/* test with a compound mapping (ru,us) */");
        assert_se(free_and_strdup(&c.vc_keymap, "ru") >= 0);

        assert_se(vconsole_convert_to_x11(&c) == 1);
        assert_se(streq(c.x11_layout, "ru,us"));
        assert_se(c.x11_variant == NULL);

        log_info("/* test with a simple mapping (us) */");
        assert_se(free_and_strdup(&c.vc_keymap, "us") >= 0);

        assert_se(vconsole_convert_to_x11(&c) == 1);
        assert_se(streq(c.x11_layout, "us"));
        assert_se(c.x11_variant == NULL);
}

static void test_x11_convert_to_vconsole(void) {
        _cleanup_(context_clear) Context c = {};
        int r;

        log_info("/*** %s ***/", __func__);

        log_info("/* test emptying first (:) */");
        assert_se(free_and_strdup(&c.vc_keymap, "foobar") >= 0);
        assert_se(x11_convert_to_vconsole(&c) == 1);
        assert_se(c.vc_keymap == NULL);

        log_info("/* test emptying second (:) */");

        assert_se(x11_convert_to_vconsole(&c) == 0);
        assert_se(c.vc_keymap == NULL);

        log_info("/* test without variant, new mapping (es:) */");
        assert_se(free_and_strdup(&c.x11_layout, "es") >= 0);

        assert_se(x11_convert_to_vconsole(&c) == 1);
        assert_se(streq(c.vc_keymap, "es"));

        log_info("/* test with unknown variant, new mapping (es:foobar) */");
        assert_se(free_and_strdup(&c.x11_variant, "foobar") >= 0);

        assert_se(x11_convert_to_vconsole(&c) == 0);
        assert_se(streq(c.vc_keymap, "es"));

        log_info("/* test with known variant, new mapping (es:dvorak) */");
        assert_se(free_and_strdup(&c.x11_variant, "dvorak") >= 0);

        r = x11_convert_to_vconsole(&c);
        if (r == 0) {
                log_info("Skipping rest of %s: keymaps are not installed", __func__);
                return;
        }

        assert_se(r == 1);
        assert_se(streq(c.vc_keymap, "es-dvorak"));

        log_info("/* test with old mapping (fr:latin9) */");
        assert_se(free_and_strdup(&c.x11_layout, "fr") >= 0);
        assert_se(free_and_strdup(&c.x11_variant, "latin9") >= 0);

        assert_se(x11_convert_to_vconsole(&c) == 1);
        assert_se(streq(c.vc_keymap, "fr-latin9"));

        log_info("/* test with a compound mapping (us,ru:) */");
        assert_se(free_and_strdup(&c.x11_layout, "us,ru") >= 0);
        assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);

        assert_se(x11_convert_to_vconsole(&c) == 1);
        assert_se(streq(c.vc_keymap, "us"));

        log_info("/* test with a compound mapping (ru,us:) */");
        assert_se(free_and_strdup(&c.x11_layout, "ru,us") >= 0);
        assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);

        assert_se(x11_convert_to_vconsole(&c) == 1);
        assert_se(streq(c.vc_keymap, "ru"));

        /* https://bugzilla.redhat.com/show_bug.cgi?id=1333998 */
        log_info("/* test with a simple new mapping (ru:) */");
        assert_se(free_and_strdup(&c.x11_layout, "ru") >= 0);
        assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);

        assert_se(x11_convert_to_vconsole(&c) == 0);
        assert_se(streq(c.vc_keymap, "ru"));
}

int main(int argc, char **argv) {
        test_setup_logging(LOG_DEBUG);

        test_find_language_fallback();
        test_find_converted_keymap();
        test_find_legacy_keymap();

        test_vconsole_convert_to_x11();
        test_x11_convert_to_vconsole();

        return 0;
}