]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
keymap-util: add tests and fix one small bug
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 28 May 2016 21:53:00 +0000 (17:53 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 6 Jun 2016 13:22:33 +0000 (09:22 -0400)
When converting an empty x11 variant, we would not delete vconsole mapping
properly.

find_legacy_keymap() is made non-static. I think it's important to be able to
test it. In principle we could also test it through the higher-level interface
of x11_convert_to_vconsole, but x11_convert_to_vconsole also uses
find_converted_keymap, and it's better to test at this lower level.

Note that find_legacy_keymap might be a bit of a misnomer, because we'd probably
want to keep kbd-model-map even if the "legacy" layouts went away.  So we might
want to change this name, but I'm leaving that for another commit.

.gitignore
Makefile.am
src/locale/keymap-util.c
src/locale/keymap-util.h
src/locale/test-keymap-util.c [new file with mode: 0644]

index 091b400182d88fcf0b6eeb96178cbbe1c3c16b7d..f7db68b4a60248c4feaa29646b36d4c1f646dc45 100644 (file)
 /test-journal-stream
 /test-journal-syslog
 /test-journal-verify
+/test-keymap-util
 /test-libsystemd-sym*
 /test-libudev
 /test-libudev-sym*
index 4ff39987acef747f6d284fd93b6649fca2f88ab4..fc6f3bf6d5442c35918af9657c4430a8a972e7e5 100644 (file)
@@ -4782,6 +4782,18 @@ dist_pkgdata_DATA = \
        src/locale/kbd-model-map \
        src/locale/language-fallback-map
 
+test_keymap_util_SOURCES = \
+       src/locale/test-keymap-util.c \
+       src/locale/keymap-util.c \
+       src/locale/keymap-util.h
+
+test_keymap_util_LDADD = \
+       libshared.la \
+       -ldl
+
+tests += \
+       test-keymap-util
+
 localectl_SOURCES = \
        src/locale/localectl.c
 
index 1827014b89d03f43dc1975df130c807508cf19b5..68b80a48010cad02b9d0564440bd3b82ebe3d90c 100644 (file)
@@ -522,7 +522,7 @@ int find_converted_keymap(const char *x11_layout, const char *x11_variant, char
         return 0;
 }
 
-static int find_legacy_keymap(Context *c, char **new_keymap) {
+int find_legacy_keymap(Context *c, char **new_keymap) {
         _cleanup_fclose_ FILE *f;
         unsigned n = 0;
         unsigned best_matching = 0;
@@ -617,6 +617,7 @@ int find_language_fallback(const char *lang, char **language) {
         _cleanup_fclose_ FILE *f = NULL;
         unsigned n = 0;
 
+        assert(lang);
         assert(language);
 
         f = fopen(SYSTEMD_LANGUAGE_FALLBACK_MAP, "re");
@@ -651,7 +652,7 @@ int x11_convert_to_vconsole(Context *c) {
                         !isempty(c->vc_keymap) ||
                         !isempty(c->vc_keymap_toggle);
 
-                context_free_x11(c);
+                context_free_vconsole(c);
         } else {
                 char *new_keymap = NULL;
                 int r;
index 244dd625638698bf2c48f762e66cdf2c35ec66d3..20ef2a4a34f905680e5c27ec29dc9772c49ec502 100644 (file)
@@ -33,6 +33,7 @@ typedef struct Context {
 } Context;
 
 int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap);
+int find_legacy_keymap(Context *c, char **new_keymap);
 int find_language_fallback(const char *lang, char **language);
 
 int context_read_data(Context *c);
diff --git a/src/locale/test-keymap-util.c b/src/locale/test-keymap-util.c
new file mode 100644 (file)
index 0000000..680aae6
--- /dev/null
@@ -0,0 +1,216 @@
+/***
+  This file is part of systemd.
+
+  Copyright 2016 Zbigniew Jędrzejewski-Szmek
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "alloc-util.h"
+#include "keymap-util.h"
+#include "log.h"
+#include "string-util.h"
+
+static void test_find_language_fallback(void) {
+        _cleanup_free_ char *ans = NULL, *ans2 = NULL;
+        int r;
+
+        log_info("/* %s */", __func__);
+
+        r = find_language_fallback("foobar", &ans);
+        if (r == -ENOENT) {
+                log_info_errno(r, "Skipping language fallback tests: %m");
+                return;
+        }
+        assert_se(r == 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_errno(r, "Skipping find_converted_keymap tests: %m");
+                return;
+        }
+        assert_se(r == 1);
+        assert_se(streq(ans, "pl"));
+
+        assert_se(find_converted_keymap("pl", "dvorak", &ans) == 1);
+        assert_se(streq(ans, "pl-dvorak"));
+}
+
+static void test_find_legacy_keymap(void) {
+        Context c = {};
+        _cleanup_free_ char *ans = NULL, *ans2 = NULL;
+        int r;
+
+        log_info("/* %s */", __func__);
+
+        c.x11_layout = (char*) "foobar";
+        r = find_legacy_keymap(&c, &ans);
+        if (r == -ENOENT) {
+                log_info_errno(r, "Skipping test_legacy_keymap tests: %m");
+                return;
+        }
+        assert_se(r == 0);
+        assert_se(ans == NULL);
+
+        c.x11_layout = (char*) "pl";
+        assert_se(find_legacy_keymap(&c, &ans) == 0); /* should this be 1? */
+        assert_se(streq(ans, "pl2"));
+
+        c.x11_layout = (char*) "pl,ru";
+        assert_se(find_legacy_keymap(&c, &ans2) == 0); /* should this be 1? */
+        assert_se(streq(ans, "pl2"));
+}
+
+static void test_vconsole_convert_to_x11(void) {
+        _cleanup_(context_free) 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_free) Context c = {};
+
+        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);
+
+        assert_se(x11_convert_to_vconsole(&c) == 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"));
+}
+
+int main(int argc, char **argv) {
+        log_set_max_level(LOG_DEBUG);
+        log_parse_environment();
+
+        test_find_language_fallback();
+        test_find_converted_keymap();
+        test_find_legacy_keymap();
+
+        test_vconsole_convert_to_x11();
+        test_x11_convert_to_vconsole();
+
+        return 0;
+}