]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
musl: test: several random fixlets for unit tests
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 9 Sep 2025 04:34:31 +0000 (13:34 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 13 Nov 2025 18:29:21 +0000 (03:29 +0900)
src/boot/test-efi-string.c
src/test/meson.build
src/test/test-os-util.c
src/test/test-seccomp.c
test/meson.build
test/test-sysusers.sh.in

index e0d3bd9b71612bec8b86c26cc9c0042f57801806..76a891ec1fa692af2b98d216cd443690f307e072 100644 (file)
@@ -410,12 +410,26 @@ TEST(startswith8) {
         ASSERT_NULL(startswith8(NULL, ""));
 }
 
-#define TEST_FNMATCH_ONE(pattern, haystack, expect)                                     \
-        ({                                                                              \
-                ASSERT_EQ(fnmatch(pattern, haystack, 0), expect ? 0 : FNM_NOMATCH);     \
-                ASSERT_EQ(efi_fnmatch(u##pattern, u##haystack), expect);                \
+#define TEST_FNMATCH_ONE_FULL(pattern, haystack, expect, skip_libc)     \
+        ({                                                              \
+                if (!skip_libc)                                         \
+                        ASSERT_EQ(fnmatch(pattern, haystack, 0), expect ? 0 : FNM_NOMATCH); \
+                ASSERT_EQ(efi_fnmatch(u##pattern, u##haystack), expect); \
         })
 
+#define TEST_FNMATCH_ONE(pattern, haystack, expect)             \
+        TEST_FNMATCH_ONE_FULL(pattern, haystack, expect, false)
+
+#ifdef __GLIBC__
+#define TEST_FNMATCH_ONE_MAY_SKIP_LIBC(pattern, haystack, expect)   \
+        TEST_FNMATCH_ONE_FULL(pattern, haystack, expect, false)
+#else
+/* It seems musl is too strict in handling "[]" (or has a bug?). Anyway, let's skip some test cases when
+ * built with musl. The behavior of efi_fnmatch() does not need to match musl's fnmatch(). */
+#define TEST_FNMATCH_ONE_MAY_SKIP_LIBC(pattern, haystack, expect)   \
+        TEST_FNMATCH_ONE_FULL(pattern, haystack, expect, true)
+#endif
+
 TEST(efi_fnmatch) {
         TEST_FNMATCH_ONE("", "", true);
         TEST_FNMATCH_ONE("abc", "abc", true);
@@ -447,18 +461,18 @@ TEST(efi_fnmatch) {
         TEST_FNMATCH_ONE("[abc", "a", false);
         TEST_FNMATCH_ONE("[][!] [][!] [][!]", "[ ] !", true);
         TEST_FNMATCH_ONE("[]-] []-]", "] -", true);
-        TEST_FNMATCH_ONE("[1\\]] [1\\]]", "1 ]", true);
+        TEST_FNMATCH_ONE_MAY_SKIP_LIBC("[1\\]] [1\\]]", "1 ]", true);
         TEST_FNMATCH_ONE("[$-\\+]", "&", true);
         TEST_FNMATCH_ONE("[1-3A-C] [1-3A-C]", "2 B", true);
         TEST_FNMATCH_ONE("[3-5] [3-5] [3-5]", "3 4 5", true);
         TEST_FNMATCH_ONE("[f-h] [f-h] [f-h]", "f g h", true);
-        TEST_FNMATCH_ONE("[a-c-f] [a-c-f] [a-c-f] [a-c-f] [a-c-f]", "a b c - f", true);
-        TEST_FNMATCH_ONE("[a-c-f]", "e", false);
+        TEST_FNMATCH_ONE_MAY_SKIP_LIBC("[a-c-f] [a-c-f] [a-c-f] [a-c-f] [a-c-f]", "a b c - f", true);
+        TEST_FNMATCH_ONE_MAY_SKIP_LIBC("[a-c-f]", "e", false);
         TEST_FNMATCH_ONE("[--0] [--0] [--0]", "- . 0", true);
         TEST_FNMATCH_ONE("[+--] [+--] [+--]", "+ , -", true);
         TEST_FNMATCH_ONE("[f-l]", "m", false);
         TEST_FNMATCH_ONE("[b]", "z-a", false);
-        TEST_FNMATCH_ONE("[a\\-z]", "b", false);
+        TEST_FNMATCH_ONE_MAY_SKIP_LIBC("[a\\-z]", "b", false);
         TEST_FNMATCH_ONE("?a*b[.-0]c", "/a/b/c", true);
         TEST_FNMATCH_ONE("debian-*-*-*.*", "debian-jessie-2018-06-17-kernel-image-5.10.0-16-amd64.efi", true);
 
@@ -674,8 +688,14 @@ TEST(xvasprintf_status) {
         test_printf_one("string");
         test_printf_one("%%-%%%%");
 
+#ifdef __GLIBC__
         test_printf_one("%p %p %32p %*p %*p", NULL, (void *) 0xF, &errno, 0, &saved_argc, 20, &saved_argv);
         test_printf_one("%-10p %-32p %-*p %-*p", NULL, &errno, 0, &saved_argc, 20, &saved_argv);
+#else
+        /* musl prints NULL as 0, while glibc and our implementation print it as (nil). */
+        test_printf_one("%p %32p %*p %*p", (void *) 0xF, &errno, 0, &saved_argc, 20, &saved_argv);
+        test_printf_one("%-32p %-*p %-*p", &errno, 0, &saved_argc, 20, &saved_argv);
+#endif
 
         test_printf_one("%c %3c %*c %*c %-8c", '1', '!', 0, 'a', 9, '_', '>');
 
index a1f16ae1a1cad46c2b4fcc09e65e5407d465a853..b46197e4b73420537a416aacc8bdca4a80db7577 100644 (file)
@@ -17,6 +17,7 @@ test_env = {
         'SYSTEMD_DEFAULT_LOCALE' : get_option('default-locale'),
         'SYSTEMD_SLOW_TESTS' : want_slow_tests ? '1' : '0',
         'PYTHONDONTWRITEBYTECODE' : '1',
+        'SYSTEMD_LIBC' : get_option('libc'),
 }
 if conf.get('ENABLE_LOCALED') == 1
         test_env += {'SYSTEMD_LANGUAGE_FALLBACK_MAP' : language_fallback_map}
index fcab1139c2b3fe8b427e206145961908808746d9..2f140c57196613515a5b08c711f30787ea1f1bec 100644 (file)
@@ -127,7 +127,7 @@ TEST(os_release_support_ended) {
 
         ASSERT_TRUE(os_release_support_ended("1999-01-01", false, NULL));
         ASSERT_FALSE(os_release_support_ended("2037-12-31", false, NULL));
-        assert_se(os_release_support_ended("-1-1-1", true, NULL) == -EINVAL);
+        ASSERT_ERROR(os_release_support_ended("1-1-1", true, NULL), ERANGE);
 
         r = os_release_support_ended(NULL, false, NULL);
         if (r < 0)
index b2e25e5b96828c9e7641a8d220143ef0525e3a4b..0a34584cff23cf9ebdd6b12a6586ba0c30e45716 100644 (file)
@@ -559,7 +559,11 @@ TEST(memory_deny_write_execute_mmap) {
                 p = mmap(NULL, page_size(), PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc64__) || defined(__arm__) || defined(__aarch64__) || defined(__loongarch_lp64)
                 assert_se(p == MAP_FAILED);
-                assert_se(errno == EPERM);
+#  ifdef __GLIBC__
+                ASSERT_EQ(errno, EPERM);
+#  else
+                ASSERT_EQ(errno, ENOMEM); /* musl maps EPERM to ENOMEM. See src/mman/mmap.c in musl. */
+#  endif
 #endif
                 /* Depending on kernel, libseccomp, and glibc versions, other architectures
                  * might fail or not. Let's not assert success. */
index 505a14aa81549c0e26766974df06be642838d7ce..1f1ec0a0e17d2ca11b9c89abb40ad1eab749958c 100644 (file)
@@ -13,6 +13,7 @@ if conf.get('ENABLE_SYSUSERS') == 1
                      # https://github.com/mesonbuild/meson/issues/2681
                      args : exe.full_path(),
                      depends : exe,
+                     env : test_env,
                      suite : 'sysusers')
 
                 if have_standalone_binaries
@@ -22,6 +23,7 @@ if conf.get('ENABLE_SYSUSERS') == 1
                              # https://github.com/mesonbuild/meson/issues/2681
                              args : exe.full_path(),
                              depends : exe,
+                             env : test_env,
                              suite : 'sysusers')
                 endif
         endif
index 321892359030258140e55b902a139e5a2a85fd3e..ae7bfee2fedd34bddbc70a8031798503e9c0641d 100755 (executable)
@@ -13,6 +13,12 @@ TESTDIR=$(mktemp --tmpdir --directory "test-sysusers.XXXXXXXXXX")
 # shellcheck disable=SC2064
 trap "rm -rf '$TESTDIR'" EXIT INT QUIT PIPE
 
+skip_nis() {
+    # musl seems to not support NIS entries. Let's skip the test case for NIS entries.
+    local path=${1:?}
+    [[ "${SYSTEMD_LIBC:-}" == musl && "${path##*/}" == "test-11.input" ]]
+}
+
 prepare_testdir() {
     mkdir -p "$TESTDIR/etc/sysusers.d/"
     mkdir -p "$TESTDIR/usr/lib/sysusers.d/"
@@ -50,6 +56,7 @@ rm -f "$TESTDIR"/etc/sysusers.d/* "$TESTDIR"/usr/lib/sysusers.d/*
 
 # happy tests
 for f in $(find "$SOURCE"/test-*.input | sort -V); do
+    skip_nis "$f" && continue
     echo "*** Running $f"
     prepare_testdir "${f%.input}"
     cp "$f" "$TESTDIR/usr/lib/sysusers.d/test.conf"
@@ -59,6 +66,7 @@ for f in $(find "$SOURCE"/test-*.input | sort -V); do
 done
 
 for f in $(find "$SOURCE"/test-*.input | sort -V); do
+    skip_nis "$f" && continue
     echo "*** Running $f on stdin"
     prepare_testdir "${f%.input}"
     touch "$TESTDIR/etc/sysusers.d/test.conf"
@@ -68,6 +76,7 @@ for f in $(find "$SOURCE"/test-*.input | sort -V); do
 done
 
 for f in $(find "$SOURCE"/test-*.input | sort -V); do
+    skip_nis "$f" && continue
     echo "*** Running $f on stdin with --replace"
     prepare_testdir "${f%.input}"
     touch "$TESTDIR/etc/sysusers.d/test.conf"
@@ -133,6 +142,7 @@ SYS_GID_MAX999
 EOF
 
 for f in $(find "$SOURCE"/test-*.input | sort -V); do
+    skip_nis "$f" && continue
     echo "*** Running $f (with login.defs)"
     prepare_testdir "${f%.input}"
     cp "$f" "$TESTDIR/usr/lib/sysusers.d/test.conf"
@@ -149,6 +159,7 @@ mv "$TESTDIR/etc/login.defs" "$TESTDIR/etc/login.defs.moved"
 ln -s ../../../../../etc/login.defs.moved "$TESTDIR/etc/login.defs"
 
 for f in $(find "$SOURCE"/test-*.input | sort -V); do
+    skip_nis "$f" && continue
     echo "*** Running $f (with login.defs symlinked)"
     prepare_testdir "${f%.input}"
     cp "$f" "$TESTDIR/usr/lib/sysusers.d/test.conf"
@@ -161,13 +172,22 @@ done
 
 rm -f "$TESTDIR"/etc/sysusers.d/* "$TESTDIR"/usr/lib/sysusers.d/*
 
+preprocess_error() {
+    # Convert error message for ERANGE from glibc to musl.
+    if [[ "${SYSTEMD_LIBC:-}" == musl ]]; then
+        sed -e 's/Numerical result out of range/Result not representable/' "${1:?}"
+    else
+        cat "${1:?}"
+    fi
+}
+
 # tests for error conditions
 for f in $(find "$SOURCE"/unhappy-*.input | sort -V); do
     echo "*** Running test $f"
     prepare_testdir "${f%.input}"
     cp "$f" "$TESTDIR/usr/lib/sysusers.d/test.conf"
     SYSTEMD_LOG_LEVEL=info "$SYSUSERS" --root="$TESTDIR" 2>&1 | tail -n1 | sed -r 's/^[^:]+:[^:]+://' >"$TESTDIR/err"
-    if ! diff -u "$TESTDIR/err"  "${f%.*}.expected-err" >&2; then
+    if ! diff -u "$TESTDIR/err"  <(preprocess_error "${f%.*}.expected-err") >&2; then
         echo >&2 "**** Unexpected error output for $f"
         cat >&2 "$TESTDIR/err"
         exit 1