594167
From 85e06430f03fe3078d5443d80ce53721fa88b285 Mon Sep 17 00:00:00 2001
594167
From: Yu Watanabe <watanabe.yu+github@gmail.com>
594167
Date: Tue, 14 Jun 2022 09:14:00 +0900
594167
Subject: [PATCH] test: import localed tests from debian/ubuntu test suite
594167
594167
(cherry picked from commit 47ba157cc083dcc5e5d7dcbdc1e27db0d6dced3e)
594167
594167
Related: #2087652
594167
---
594167
 test/TEST-73-LOCALE/Makefile    |   1 +
594167
 test/TEST-73-LOCALE/test.sh     |  10 +
594167
 test/units/testsuite-73.service |  10 +
594167
 test/units/testsuite-73.sh      | 391 ++++++++++++++++++++++++++++++++
594167
 4 files changed, 412 insertions(+)
594167
 create mode 120000 test/TEST-73-LOCALE/Makefile
594167
 create mode 100755 test/TEST-73-LOCALE/test.sh
594167
 create mode 100644 test/units/testsuite-73.service
594167
 create mode 100755 test/units/testsuite-73.sh
594167
594167
diff --git a/test/TEST-73-LOCALE/Makefile b/test/TEST-73-LOCALE/Makefile
594167
new file mode 120000
594167
index 0000000000..e9f93b1104
594167
--- /dev/null
594167
+++ b/test/TEST-73-LOCALE/Makefile
594167
@@ -0,0 +1 @@
594167
+../TEST-01-BASIC/Makefile
594167
\ No newline at end of file
594167
diff --git a/test/TEST-73-LOCALE/test.sh b/test/TEST-73-LOCALE/test.sh
594167
new file mode 100755
594167
index 0000000000..a33ddaba86
594167
--- /dev/null
594167
+++ b/test/TEST-73-LOCALE/test.sh
594167
@@ -0,0 +1,10 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -e
594167
+
594167
+TEST_DESCRIPTION="test localed"
594167
+
594167
+# shellcheck source=test/test-functions
594167
+. "${TEST_BASE_DIR:?}/test-functions"
594167
+
594167
+do_test "$@"
594167
diff --git a/test/units/testsuite-73.service b/test/units/testsuite-73.service
594167
new file mode 100644
594167
index 0000000000..bee8c4215d
594167
--- /dev/null
594167
+++ b/test/units/testsuite-73.service
594167
@@ -0,0 +1,10 @@
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+[Unit]
594167
+Description=TEST-73-LOCALE
594167
+
594167
+[Service]
594167
+ExecStartPre=rm -f /failed /testok
594167
+ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
594167
+StandardOutput=journal+console
594167
+StandardError=journal+console
594167
+Type=oneshot
594167
diff --git a/test/units/testsuite-73.sh b/test/units/testsuite-73.sh
594167
new file mode 100755
594167
index 0000000000..d96e9f4151
594167
--- /dev/null
594167
+++ b/test/units/testsuite-73.sh
594167
@@ -0,0 +1,391 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+
594167
+set -eux
594167
+set -o pipefail
594167
+
594167
+# shellcheck source=test/units/assert.sh
594167
+. "$(dirname "$0")"/assert.sh
594167
+
594167
+enable_debug() {
594167
+    mkdir -p /run/systemd/system/systemd-localed.service.d
594167
+    cat >>/run/systemd/system/systemd-localed.service.d/override.conf <
594167
+[Service]
594167
+Environment=SYSTEMD_LOG_LEVEL=debug
594167
+EOF
594167
+
594167
+    mkdir -p /run/systemd/system/systemd-vconsole-setup.service.d
594167
+    cat >>/run/systemd/system/systemd-vconsole-setup.service.d/override.conf <
594167
+[Unit]
594167
+StartLimitIntervalSec=0
594167
+
594167
+[Service]
594167
+Environment=SYSTEMD_LOG_LEVEL=debug
594167
+EOF
594167
+
594167
+    systemctl daemon-reload
594167
+}
594167
+
594167
+restore_locale() {
594167
+    if [[ -d /usr/lib/locale/xx_XX.UTF-8 ]]; then
594167
+        rmdir /usr/lib/locale/xx_XX.UTF-8
594167
+    fi
594167
+
594167
+    if [[ -f /tmp/locale.conf.bak ]]; then
594167
+        mv /tmp/locale.conf.bak /etc/locale.conf
594167
+    else
594167
+        rm -f /etc/locale.conf
594167
+    fi
594167
+
594167
+    if [[ -f /tmp/default-locale.bak ]]; then
594167
+        mv /tmp/default-locale.bak /etc/default/locale
594167
+    else
594167
+        rm -f /etc/default/locale
594167
+        rmdir --ignore-fail-on-non-empty /etc/default
594167
+    fi
594167
+
594167
+    if [[ -f /tmp/locale.gen.bak ]]; then
594167
+        mv /tmp/locale.gen.bak /etc/locale.gen
594167
+    else
594167
+        rm -f /etc/locale.gen
594167
+    fi
594167
+}
594167
+
594167
+test_locale() {
594167
+    local i output
594167
+
594167
+    if [[ -f /etc/locale.conf ]]; then
594167
+        cp /etc/locale.conf /tmp/locale.conf.bak
594167
+    fi
594167
+
594167
+    # Debian/Ubuntu specific file
594167
+    if [[ -f /etc/default/locale ]]; then
594167
+        cp /etc/default/locale /tmp/default-locale.bak
594167
+    fi
594167
+
594167
+    if [[ -f /etc/locale.gen ]]; then
594167
+        cp /etc/locale.gen /tmp/locale.gen.bak
594167
+    fi
594167
+
594167
+    # remove locale.conf to make /etc/default/locale used by Debian/Ubuntu
594167
+    rm -f /etc/locale.conf
594167
+    # also remove /etc/default/locale
594167
+    rm -f /etc/default/locale
594167
+    # and create /etc/default to make /etc/default/locale created by localed
594167
+    mkdir -p /etc/default
594167
+
594167
+    trap restore_locale RETURN
594167
+
594167
+    if command -v locale-gen >/dev/null 2>&1 &&
594167
+           ! localectl list-locales | grep -F "en_US.UTF-8"; then
594167
+        # ensure at least one utf8 locale exist
594167
+        echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
594167
+        locale-gen en_US.UTF-8
594167
+    fi
594167
+
594167
+    # create invalid locale
594167
+    mkdir -p /usr/lib/locale/xx_XX.UTF-8
594167
+    assert_not_in "xx_XX.UTF-8" "$(localectl list-locales)"
594167
+
594167
+    if [[ -z "$(localectl list-locales)" ]]; then
594167
+        echo "No locale installed, skipping test."
594167
+        return
594167
+    fi
594167
+
594167
+    # warn when kernel command line has locale settings
594167
+    output=$(SYSTEMD_PROC_CMDLINE="locale.LANG=C.UTF-8 locale.LC_CTYPE=ja_JP.UTF-8" localectl 2>&1)
594167
+    assert_in "Warning:" "$output"
594167
+    assert_in "Command Line: LANG=C.UTF-8" "$output"
594167
+    assert_in "LC_CTYPE=ja_JP.UTF-8" "$output"
594167
+    assert_in "System Locale:" "$output"
594167
+
594167
+    # change locale
594167
+    for i in $(localectl list-locales); do
594167
+        assert_rc 0 localectl set-locale "LANG=C" "LC_CTYPE=$i"
594167
+        if [[ -f /etc/default/locale ]]; then
594167
+            assert_eq "$(cat /etc/default/locale)" "LANG=C
594167
+LC_CTYPE=$i"
594167
+        else
594167
+            assert_eq "$(cat /etc/locale.conf)" "LANG=C
594167
+LC_CTYPE=$i"
594167
+        fi
594167
+        output=$(localectl)
594167
+        assert_in "System Locale: LANG=C" "$output"
594167
+        assert_in "LC_CTYPE=$i" "$output"
594167
+        output=$(systemctl show-environment)
594167
+        assert_in "LANG=C" "$output"
594167
+        assert_in "LC_CTYPE=$i" "$output"
594167
+
594167
+        assert_rc 0 localectl set-locale "$i"
594167
+        if [[ -f /etc/default/locale ]]; then
594167
+            # Debian/Ubuntu patch is buggy, and LC_CTYPE= still exists.
594167
+            assert_eq "$(cat /etc/default/locale)" "LANG=$i
594167
+LC_CTYPE=$i"
594167
+        else
594167
+            assert_eq "$(cat /etc/locale.conf)" "LANG=$i"
594167
+        fi
594167
+        output=$(localectl)
594167
+        assert_in "System Locale: LANG=$i" "$output"
594167
+        assert_not_in "LC_CTYPE=" "$output"
594167
+        output=$(systemctl show-environment)
594167
+        assert_in "LANG=$i" "$output"
594167
+        assert_not_in "LC_CTYPE=" "$output"
594167
+    done
594167
+
594167
+    # test if localed auto-runs locale-gen
594167
+    if command -v locale-gen >/dev/null 2>&1 &&
594167
+           ! localectl list-locales | grep -F "de_DE.UTF-8"; then
594167
+
594167
+        # clear previous locale
594167
+        systemctl stop systemd-localed.service
594167
+        rm -f /etc/locale.conf /etc/default/locale
594167
+
594167
+        # change locale
594167
+        assert_rc 0 localectl set-locale de_DE.UTF-8
594167
+        if [[ -f /etc/default/locale ]]; then
594167
+            assert_eq "$(cat /etc/default/locale)" "LANG=de_DE.UTF-8"
594167
+        else
594167
+            assert_eq "$(cat /etc/locale.conf)" "LANG=de_DE.UTF-8"
594167
+        fi
594167
+        assert_in "System Locale: LANG=de_DE.UTF-8" "$(localectl)"
594167
+        assert_in "LANG=de_DE.UTF-8" "$(systemctl show-environment)"
594167
+
594167
+        # ensure tested locale exists and works now
594167
+        assert_in "de_DE.UTF-8" "$(localectl list-locales)"
594167
+    fi
594167
+}
594167
+
594167
+backup_keymap() {
594167
+    if [[ -f /etc/vconsole.conf ]]; then
594167
+        cp /etc/vconsole.conf /tmp/vconsole.conf.bak
594167
+    fi
594167
+
594167
+    if [[ -f /etc/X11/xorg.conf.d/00-keyboard.conf ]]; then
594167
+        cp /etc/X11/xorg.conf.d/00-keyboard.conf /tmp/00-keyboard.conf.bak
594167
+    fi
594167
+
594167
+    # Debian/Ubuntu specific file
594167
+    if [[ -f /etc/default/keyboard ]]; then
594167
+        cp /etc/default/keyboard /tmp/default-keyboard.bak
594167
+    fi
594167
+
594167
+    mkdir -p /etc/default
594167
+}
594167
+
594167
+restore_keymap() {
594167
+    if [[ -f /tmp/vconsole.conf.bak ]]; then
594167
+        mv /tmp/vconsole.conf.bak /etc/vconsole.conf
594167
+    else
594167
+        rm -f /etc/vconsole.conf
594167
+    fi
594167
+
594167
+    if [[ -f /tmp/00-keyboard.conf.bak ]]; then
594167
+        mv /tmp/00-keyboard.conf.bak /etc/X11/xorg.conf.d/00-keyboard.conf
594167
+    else
594167
+        rm -f /etc/X11/xorg.conf.d/00-keyboard.conf
594167
+    fi
594167
+
594167
+    if [[ -f /tmp/default-keyboard.bak ]]; then
594167
+        mv /tmp/default-keyboard.bak /etc/default/keyboard
594167
+    else
594167
+        rm -f /etc/default/keyboard
594167
+        rmdir --ignore-fail-on-non-empty /etc/default
594167
+    fi
594167
+}
594167
+
594167
+wait_vconsole_setup() {
594167
+    local i ss
594167
+    for ((i = 0; i < 20; i++)); do
594167
+        if (( i != 0 )); then sleep .5; fi
594167
+        ss="$(systemctl --property SubState --value show systemd-vconsole-setup.service)"
594167
+        if [[ "$ss" == "exited" || "$ss" == "dead" || "$ss" == "condition" ]]; then
594167
+            return 0
594167
+        elif [[ "$ss" == "failed" ]]; then
594167
+            echo "WARNING: systemd-vconsole-setup.service failed, ignoring." >&2
594167
+            systemctl reset-failed systemd-vconsole-setup.service
594167
+            return 0
594167
+        fi
594167
+    done
594167
+
594167
+    systemctl status systemd-vconsole-setup.service
594167
+    return 1
594167
+}
594167
+
594167
+test_vc_keymap() {
594167
+    local i output
594167
+
594167
+    if [[ -z "$(localectl list-keymaps)" ]]; then
594167
+        echo "No vconsole keymap installed, skipping test."
594167
+        return
594167
+    fi
594167
+
594167
+    backup_keymap
594167
+    trap restore_keymap RETURN
594167
+
594167
+    # should activate daemon and work
594167
+    assert_in "VC Keymap:" "$(localectl)"
594167
+
594167
+    for i in $(localectl list-keymaps); do
594167
+        # clear previous conversion from VC -> X11 keymap
594167
+        systemctl stop systemd-localed.service
594167
+        wait_vconsole_setup
594167
+        rm -f /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard
594167
+
594167
+        # set VC keymap
594167
+        assert_rc 0 localectl set-keymap "$i"
594167
+        output=$(localectl)
594167
+
594167
+        # check VC keymap
594167
+        assert_in "KEYMAP=$i" "$(cat /etc/vconsole.conf)"
594167
+        assert_in "VC Keymap: $i" "$output"
594167
+
594167
+        # check VC -> X11 keymap conversion
594167
+        if [[ "$i" == "us" ]]; then
594167
+            assert_in "X11 Layout: us" "$output"
594167
+            assert_in "X11 Model: pc105\+inet" "$output"
594167
+            assert_not_in "X11 Variant:" "$output"
594167
+            assert_in "X11 Options: terminate:ctrl_alt_bksp" "$output"
594167
+        elif [[ "$i" == "us-acentos" ]]; then
594167
+            assert_in "X11 Layout: us" "$output"
594167
+            assert_in 'X11 Model: pc105$' "$output"
594167
+            assert_in "X11 Variant: intl" "$output"
594167
+            assert_in "X11 Options: terminate:ctrl_alt_bksp" "$output"
594167
+        elif [[ "$i" =~ ^us-.* ]]; then
594167
+            assert_in "X11 Layout: n/a" "$output"
594167
+            assert_not_in "X11 Model:" "$output"
594167
+            assert_not_in "X11 Variant:" "$output"
594167
+            assert_not_in "X11 Options:" "$output"
594167
+        fi
594167
+    done
594167
+
594167
+    # gets along without config file
594167
+    systemctl stop systemd-localed.service
594167
+    wait_vconsole_setup
594167
+    rm -f /etc/vconsole.conf
594167
+    assert_in "VC Keymap: n/a" "$(localectl)"
594167
+}
594167
+
594167
+test_x11_keymap() {
594167
+    local output
594167
+
594167
+    if [[ -z "$(localectl list-x11-keymap-layouts)" ]]; then
594167
+        echo "No x11 keymap installed, skipping test."
594167
+        return
594167
+    fi
594167
+
594167
+    backup_keymap
594167
+    trap restore_keymap RETURN
594167
+
594167
+    # should activate daemon and work
594167
+    assert_in "X11 Layout:" "$(localectl)"
594167
+
594167
+    # set x11 keymap (layout, model, variant, options)
594167
+    assert_rc 0 localectl set-x11-keymap us pc105+inet intl terminate:ctrl_alt_bksp
594167
+
594167
+    if [[ -f /etc/default/keyboard ]]; then
594167
+        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us
594167
+XKBMODEL=pc105+inet
594167
+XKBVARIANT=intl
594167
+XKBOPTIONS=terminate:ctrl_alt_bksp"
594167
+    else
594167
+        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
594167
+        assert_in 'Option "XkbLayout" "us"' "$output"
594167
+        assert_in 'Option "XkbModel" "pc105\+inet"' "$output"
594167
+        assert_in 'Option "XkbVariant" "intl"' "$output"
594167
+        assert_in 'Option "XkbOptions" "terminate:ctrl_alt_bksp"' "$output"
594167
+    fi
594167
+
594167
+    output=$(localectl)
594167
+    assert_in "X11 Layout: us" "$output"
594167
+    assert_in "X11 Model: pc105\+inet" "$output"
594167
+    assert_in "X11 Variant: intl" "$output"
594167
+    assert_in "X11 Options: terminate:ctrl_alt_bksp" "$output"
594167
+
594167
+    # Debian/Ubuntu patch is buggy, unspecified settings are not cleared
594167
+    rm -f /etc/default/keyboard
594167
+
594167
+    # set x11 keymap (layout, model, variant)
594167
+    assert_rc 0 localectl set-x11-keymap us pc105+inet intl
594167
+
594167
+    if [[ -f /etc/default/keyboard ]]; then
594167
+        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us
594167
+XKBMODEL=pc105+inet
594167
+XKBVARIANT=intl"
594167
+    else
594167
+        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
594167
+        assert_in 'Option "XkbLayout" "us"' "$output"
594167
+        assert_in 'Option "XkbModel" "pc105\+inet"' "$output"
594167
+        assert_in 'Option "XkbVariant" "intl"' "$output"
594167
+        assert_not_in 'Option "XkbOptions"' "$output"
594167
+    fi
594167
+
594167
+    output=$(localectl)
594167
+    assert_in "X11 Layout: us" "$output"
594167
+    assert_in "X11 Model: pc105\+inet" "$output"
594167
+    assert_in "X11 Variant: intl" "$output"
594167
+    assert_not_in "X11 Options:" "$output"
594167
+
594167
+    # Debian/Ubuntu patch is buggy, unspecified settings are not cleared
594167
+    rm -f /etc/default/keyboard
594167
+
594167
+    # set x11 keymap (layout, model)
594167
+    assert_rc 0 localectl set-x11-keymap us pc105+inet
594167
+
594167
+    if [[ -f /etc/default/keyboard ]]; then
594167
+        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us
594167
+XKBMODEL=pc105+inet"
594167
+    else
594167
+        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
594167
+        assert_in 'Option "XkbLayout" "us"' "$output"
594167
+        assert_in 'Option "XkbModel" "pc105\+inet"' "$output"
594167
+        assert_not_in 'Option "XkbVariant"' "$output"
594167
+        assert_not_in 'Option "XkbOptions"' "$output"
594167
+    fi
594167
+
594167
+    output=$(localectl)
594167
+    assert_in "X11 Layout: us" "$output"
594167
+    assert_in "X11 Model: pc105\+inet" "$output"
594167
+    assert_not_in "X11 Variant:" "$output"
594167
+    assert_not_in "X11 Options:" "$output"
594167
+
594167
+    # Debian/Ubuntu patch is buggy, unspecified settings are not cleared
594167
+    rm -f /etc/default/keyboard
594167
+
594167
+    # set x11 keymap (layout)
594167
+    assert_rc 0 localectl set-x11-keymap us
594167
+
594167
+    if [[ -f /etc/default/keyboard ]]; then
594167
+        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us"
594167
+    else
594167
+        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
594167
+        assert_in 'Option "XkbLayout" "us"' "$output"
594167
+        assert_not_in 'Option "XkbModel"' "$output"
594167
+        assert_not_in 'Option "XkbVariant"' "$output"
594167
+        assert_not_in 'Option "XkbOptions"' "$output"
594167
+    fi
594167
+
594167
+    output=$(localectl)
594167
+    assert_in "X11 Layout: us" "$output"
594167
+    assert_not_in "X11 Model:" "$output"
594167
+    assert_not_in "X11 Variant:" "$output"
594167
+    assert_not_in "X11 Options:" "$output"
594167
+
594167
+    # gets along without config file
594167
+    systemctl stop systemd-localed.service
594167
+    rm -f /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard
594167
+    output=$(localectl)
594167
+    assert_in "X11 Layout: n/a" "$output"
594167
+    assert_not_in "X11 Model:" "$output"
594167
+    assert_not_in "X11 Variant:" "$output"
594167
+    assert_not_in "X11 Options:" "$output"
594167
+}
594167
+
594167
+: >/failed
594167
+
594167
+enable_debug
594167
+test_locale
594167
+test_vc_keymap
594167
+test_x11_keymap
594167
+
594167
+touch /testok
594167
+rm /failed