From 08751fff5284af079f532a2a5f37fc6f0ac87c5c Mon Sep 17 00:00:00 2001 From: Simon McVittie <smcv@collabora.com> Date: Wed, 4 Aug 2021 17:47:40 +0100 Subject: [PATCH] tests: Assert that modules with ${LIB} or ${PLATFORM} are handled Signed-off-by: Simon McVittie <smcv@collabora.com> --- pressure-vessel/wrap-setup.c | 32 +++ pressure-vessel/wrap-setup.h | 1 + tests/pressure-vessel/wrap-setup.c | 306 +++++++++++++++-------------- 3 files changed, 190 insertions(+), 149 deletions(-) diff --git a/pressure-vessel/wrap-setup.c b/pressure-vessel/wrap-setup.c index ce51d6cc6..fdcaef856 100644 --- a/pressure-vessel/wrap-setup.c +++ b/pressure-vessel/wrap-setup.c @@ -520,6 +520,38 @@ append_preload_per_architecture (GPtrArray *argv, &details); path = srt_library_get_absolute_path (details); + if (flags & PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS) + { + /* Use mock results to get predictable behaviour in the unit + * tests, replacing the real result (above). This avoids needing + * to have real libraries in place when we do unit testing. + * + * tests/pressure-vessel/wrap-setup.c is the other side of this. */ + g_autofree gchar *lib = NULL; + const char *platform = NULL; + + /* As a mock ${LIB}, behave like Debian or the fdo SDK. */ + lib = g_strdup_printf ("lib/%s", pv_multiarch_details[i].tuple); + + /* As a mock ${PLATFORM}, use the first one listed. */ + platform = pv_multiarch_details[i].platforms[0]; + + mock_path = g_string_new (preload); + g_string_replace (mock_path, "$LIB", lib, 0); + g_string_replace (mock_path, "${LIB}", lib, 0); + g_string_replace (mock_path, "$PLATFORM", platform, 0); + g_string_replace (mock_path, "${PLATFORM}", platform, 0); + path = mock_path->str; + + /* As a special case, pretend one 64-bit library failed to load, + * so we can exercise what happens when there's only a 32-bit + * library available. */ + if (strstr (path, "only-32-bit") != NULL + && strcmp (pv_multiarch_details[i].tuple, + SRT_ABI_I386) != 0) + path = NULL; + } + if (path != NULL) { g_debug ("Found %s version of %s at %s", diff --git a/pressure-vessel/wrap-setup.h b/pressure-vessel/wrap-setup.h index 0c13cecee..12dac2cd6 100644 --- a/pressure-vessel/wrap-setup.h +++ b/pressure-vessel/wrap-setup.h @@ -55,6 +55,7 @@ typedef enum { PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX = (1 << 0), PV_APPEND_PRELOAD_FLAGS_REMOVE_GAME_OVERLAY = (1 << 1), + PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS = (1 << 2), PV_APPEND_PRELOAD_FLAGS_NONE = 0 } PvAppendPreloadFlags; diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c index c7b75de32..ea708c14f 100644 --- a/tests/pressure-vessel/wrap-setup.c +++ b/tests/pressure-vessel/wrap-setup.c @@ -259,7 +259,7 @@ populate_ld_preload (Fixture *f, "--ld-preload", preloads[i].string, f->env, - flags, + flags | PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS, runtime, exports); @@ -297,27 +297,37 @@ test_remap_ld_preload (Fixture *f, g_assert_cmpstr (g_ptr_array_index (argv, i++), ==, "--ld-preload=/app/lib/libpreloadA.so"); - /* TODO: We should split up /platform/plat-$PLATFORM/libpreloadP.so into - * its per-platform resolutions */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/platform/plat-")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/platform/plat-" + MOCK_PLATFORM_64 + "/libpreloadP.so:abi=" + SRT_ABI_X86_64)); - /* TODO: We should split up /opt/${LIB}/libpreloadL.so into - * its per-platform resolutions */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/opt/")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/platform/plat-" + MOCK_PLATFORM_32 + "/libpreloadP.so:abi=" + SRT_ABI_I386)); + + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/opt/" + MOCK_LIB_64 + "/libpreloadL.so:abi=" + SRT_ABI_X86_64)); + + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/opt/" + MOCK_LIB_32 + "/libpreloadL.so:abi=" + SRT_ABI_I386)); g_assert_cmpuint (argv->len, >, i); g_assert_cmpstr (g_ptr_array_index (argv, i++), @@ -343,17 +353,13 @@ test_remap_ld_preload (Fixture *f, g_assert_cmpstr (g_ptr_array_index (argv, i++), ==, "--ld-preload=/future/libs-$FUTURE/libpreloadF.so"); - /* TODO: We should split this up into its per-platform resolutions, - * then discard the 64-bit resolution because it doesn't exist, and - * only keep the 32-bit resolution */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/in-root-plat-")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/in-root-plat-" + MOCK_PLATFORM_32 + "-only-32-bit.so:abi=" + SRT_ABI_I386)); g_assert_cmpuint (argv->len, >, i); g_assert_cmpstr (g_ptr_array_index (argv, i++), @@ -384,22 +390,14 @@ test_remap_ld_preload (Fixture *f, * these. */ g_assert_false (flatpak_exports_path_is_visible (exports, "/opt")); g_assert_false (flatpak_exports_path_is_visible (exports, "/opt/lib")); - - if (!flatpak_exports_path_is_visible (exports, "/opt/" MOCK_LIB_32 "/libpreloadL.so")) - g_test_message ("TODO: Export filenames in /opt/$LIB"); - - if (!flatpak_exports_path_is_visible (exports, "/opt/" MOCK_LIB_64 "/libpreloadL.so")) - g_test_message ("TODO: Export filenames in /opt/$LIB"); + g_assert_true (flatpak_exports_path_is_visible (exports, "/opt/" MOCK_LIB_32 "/libpreloadL.so")); + g_assert_true (flatpak_exports_path_is_visible (exports, "/opt/" MOCK_LIB_64 "/libpreloadL.so")); /* We don't always export /platform, so we have to explicitly export * these. */ g_assert_false (flatpak_exports_path_is_visible (exports, "/platform")); - - if (!flatpak_exports_path_is_visible (exports, "/platform/plat-" MOCK_PLATFORM_32 "/libpreloadP.so")) - g_test_message ("TODO: Export filenames in /platform"); - - if (!flatpak_exports_path_is_visible (exports, "/platform/plat-" MOCK_PLATFORM_64 "/libpreloadP.so")) - g_test_message ("TODO: Export filenames in /platform"); + g_assert_true (flatpak_exports_path_is_visible (exports, "/platform/plat-" MOCK_PLATFORM_32 "/libpreloadP.so")); + g_assert_true (flatpak_exports_path_is_visible (exports, "/platform/plat-" MOCK_PLATFORM_64 "/libpreloadP.so")); /* FlatpakExports never exports /lib as /lib */ g_assert_false (flatpak_exports_path_is_visible (exports, "/lib")); @@ -446,27 +444,37 @@ test_remap_ld_preload_flatpak (Fixture *f, g_assert_cmpstr (g_ptr_array_index (argv, i++), ==, "--ld-preload=/run/parent/app/lib/libpreloadA.so"); - /* TODO: We should split up /platform/plat-$PLATFORM/libpreloadP.so into - * its per-platform resolutions */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/platform/plat-")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/platform/plat-" + MOCK_PLATFORM_64 + "/libpreloadP.so:abi=" + SRT_ABI_X86_64)); - /* TODO: We should split up /opt/${LIB}/libpreloadL.so into - * its per-platform resolutions */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/opt/")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/platform/plat-" + MOCK_PLATFORM_32 + "/libpreloadP.so:abi=" + SRT_ABI_I386)); + + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/opt/" + MOCK_LIB_64 + "/libpreloadL.so:abi=" + SRT_ABI_X86_64)); + + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/opt/" + MOCK_LIB_32 + "/libpreloadL.so:abi=" + SRT_ABI_I386)); g_assert_cmpuint (argv->len, >, i); g_assert_cmpstr (g_ptr_array_index (argv, i++), @@ -492,17 +500,13 @@ test_remap_ld_preload_flatpak (Fixture *f, g_assert_cmpstr (g_ptr_array_index (argv, i++), ==, "--ld-preload=/future/libs-$FUTURE/libpreloadF.so"); - /* TODO: We should split this up into its per-platform resolutions, - * then discard the 64-bit resolution because it doesn't exist, and - * only keep the 32-bit resolution */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/in-root-plat-")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/in-root-plat-" + MOCK_PLATFORM_32 + "-only-32-bit.so:abi=" + SRT_ABI_I386)); g_assert_cmpuint (argv->len, >, i); g_assert_cmpstr (g_ptr_array_index (argv, i++), @@ -541,27 +545,37 @@ test_remap_ld_preload_no_runtime (Fixture *f, g_assert_cmpstr (g_ptr_array_index (argv, i++), ==, "--ld-preload=/app/lib/libpreloadA.so"); - /* TODO: We should split up /platform/plat-$PLATFORM/libpreloadP.so into - * its per-platform resolutions */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/platform/plat-")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/platform/plat-" + MOCK_PLATFORM_64 + "/libpreloadP.so:abi=" + SRT_ABI_X86_64)); - /* TODO: We should split up /opt/${LIB}/libpreloadL.so into - * its per-platform resolutions */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/opt/")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/platform/plat-" + MOCK_PLATFORM_32 + "/libpreloadP.so:abi=" + SRT_ABI_I386)); + + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/opt/" + MOCK_LIB_64 + "/libpreloadL.so:abi=" + SRT_ABI_X86_64)); + + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/opt/" + MOCK_LIB_32 + "/libpreloadL.so:abi=" + SRT_ABI_I386)); g_assert_cmpuint (argv->len, >, i); g_assert_cmpstr (g_ptr_array_index (argv, i++), @@ -586,17 +600,13 @@ test_remap_ld_preload_no_runtime (Fixture *f, g_assert_cmpstr (g_ptr_array_index (argv, i++), ==, "--ld-preload=/future/libs-$FUTURE/libpreloadF.so"); - /* TODO: We should split this up into its per-platform resolutions, - * then discard the 64-bit resolution because it doesn't exist, and - * only keep the 32-bit resolution */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/in-root-plat-")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/in-root-plat-" + MOCK_PLATFORM_32 + "-only-32-bit.so:abi=" + SRT_ABI_I386)); g_assert_cmpuint (argv->len, >, i); g_assert_cmpstr (g_ptr_array_index (argv, i++), @@ -627,22 +637,14 @@ test_remap_ld_preload_no_runtime (Fixture *f, * these. */ g_assert_false (flatpak_exports_path_is_visible (exports, "/opt")); g_assert_false (flatpak_exports_path_is_visible (exports, "/opt/lib")); - - if (!flatpak_exports_path_is_visible (exports, "/opt/" MOCK_LIB_32 "/libpreloadL.so")) - g_test_message ("TODO: Export filenames in /opt/$LIB"); - - if (!flatpak_exports_path_is_visible (exports, "/opt/" MOCK_LIB_64 "/libpreloadL.so")) - g_test_message ("TODO: Export filenames in /opt/$LIB"); + g_assert_true (flatpak_exports_path_is_visible (exports, "/opt/" MOCK_LIB_32 "/libpreloadL.so")); + g_assert_true (flatpak_exports_path_is_visible (exports, "/opt/" MOCK_LIB_64 "/libpreloadL.so")); /* We don't always export /platform, so we have to explicitly export * these. */ g_assert_false (flatpak_exports_path_is_visible (exports, "/platform")); - - if (!flatpak_exports_path_is_visible (exports, "/platform/plat-" MOCK_PLATFORM_32 "/libpreloadP.so")) - g_test_message ("TODO: Export filenames in /platform"); - - if (!flatpak_exports_path_is_visible (exports, "/platform/plat-" MOCK_PLATFORM_64 "/libpreloadP.so")) - g_test_message ("TODO: Export filenames in /platform"); + g_assert_true (flatpak_exports_path_is_visible (exports, "/platform/plat-" MOCK_PLATFORM_32 "/libpreloadP.so")); + g_assert_true (flatpak_exports_path_is_visible (exports, "/platform/plat-" MOCK_PLATFORM_64 "/libpreloadP.so")); /* FlatpakExports never exports /lib as /lib */ g_assert_false (flatpak_exports_path_is_visible (exports, "/lib")); @@ -683,27 +685,37 @@ test_remap_ld_preload_flatpak_no_runtime (Fixture *f, g_assert_cmpstr (g_ptr_array_index (argv, i++), ==, "--ld-preload=/app/lib/libpreloadA.so"); - /* TODO: We should split up /platform/plat-$PLATFORM/libpreloadP.so into - * its per-platform resolutions */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/platform/plat-")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/platform/plat-" + MOCK_PLATFORM_64 + "/libpreloadP.so:abi=" + SRT_ABI_X86_64)); - /* TODO: We should split up /opt/${LIB}/libpreloadL.so into - * its per-platform resolutions */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/opt/")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/platform/plat-" + MOCK_PLATFORM_32 + "/libpreloadP.so:abi=" + SRT_ABI_I386)); + + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/opt/" + MOCK_LIB_64 + "/libpreloadL.so:abi=" + SRT_ABI_X86_64)); + + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/opt/" + MOCK_LIB_32 + "/libpreloadL.so:abi=" + SRT_ABI_I386)); g_assert_cmpuint (argv->len, >, i); g_assert_cmpstr (g_ptr_array_index (argv, i++), @@ -729,17 +741,13 @@ test_remap_ld_preload_flatpak_no_runtime (Fixture *f, g_assert_cmpstr (g_ptr_array_index (argv, i++), ==, "--ld-preload=/future/libs-$FUTURE/libpreloadF.so"); - /* TODO: We should split this up into its per-platform resolutions, - * then discard the 64-bit resolution because it doesn't exist, and - * only keep the 32-bit resolution */ - while (argv->len > i) - { - if (g_str_has_prefix (g_ptr_array_index (argv, i), - "--ld-preload=/in-root-plat-")) - i++; - else - break; - } + g_assert_cmpuint (argv->len, >, i); + g_assert_cmpstr (g_ptr_array_index (argv, i++), + ==, + ("--ld-preload=/in-root-plat-" + MOCK_PLATFORM_32 + "-only-32-bit.so:abi=" + SRT_ABI_I386)); g_assert_cmpuint (argv->len, >, i); g_assert_cmpstr (g_ptr_array_index (argv, i++), -- GitLab