diff --git a/meson.build b/meson.build index cb0900b7473a571563fbceef7d23bc0e3c01ca04..a85b27105242a582c3f5f62d8545e85e49d1182e 100644 --- a/meson.build +++ b/meson.build @@ -284,9 +284,13 @@ xlib = dependency( libdl = c_compiler.find_library('dl', required : false) -libglnx = subproject('libglnx') +libglnx_dep = subproject('libglnx').get_variable('libglnx_dep') -project_include_dirs = include_directories('.') +project_include_dirs = include_directories( + '.', + 'subprojects', + 'subprojects/libglnx', +) pv_include_dirs = include_directories( '.', diff --git a/pressure-vessel/meson.build b/pressure-vessel/meson.build index 8ec9c8b41d7a19bc2498e311e0906a8849a99717..166a10efa9573b4acd60c2a8290ade8d92626e24 100644 --- a/pressure-vessel/meson.build +++ b/pressure-vessel/meson.build @@ -100,7 +100,7 @@ pressure_vessel_utils = static_library( threads, gio_unix, libelf, - libglnx.get_variable('libglnx_dep'), + libglnx_dep, ], include_directories : pv_include_dirs, install: false, @@ -137,7 +137,7 @@ executable( pressure_vessel_utils_dep, threads, gio_unix, - libglnx.get_variable('libglnx_dep'), + libglnx_dep, ], include_directories : pv_include_dirs, install : true, @@ -156,7 +156,7 @@ executable( pressure_vessel_utils_dep, threads, gio_unix, - libglnx.get_variable('libglnx_dep'), + libglnx_dep, ], include_directories : pv_include_dirs, install : true, @@ -176,7 +176,7 @@ executable( pressure_vessel_utils_dep, threads, gio_unix, - libglnx.get_variable('libglnx_dep'), + libglnx_dep, ], include_directories : pv_include_dirs, install : true, @@ -207,7 +207,7 @@ executable( libsteamrt_static_dep, gio_unix, xau, - libglnx.get_variable('libglnx_dep'), + libglnx_dep, ], include_directories : pv_include_dirs, install : true, diff --git a/steam-runtime-tools/glib-compat.h b/steam-runtime-tools/glib-compat.h index 8e8b0db074c0f5b5d98e8da603ac3551e7d9e3de..adeac63fc4f4aba67d4810db01c71230f772e988 100644 --- a/steam-runtime-tools/glib-compat.h +++ b/steam-runtime-tools/glib-compat.h @@ -35,7 +35,7 @@ # define G_SPAWN_DEFAULT (0) #endif -#if !GLIB_CHECK_VERSION(2, 44, 0) +#if !defined(g_steal_pointer) #define g_steal_pointer(x) _srt_steal_pointer (x) /* A simplified version of g_steal_pointer without type-safety. */ static inline gpointer @@ -50,7 +50,7 @@ _srt_steal_pointer (gpointer pointer_to_pointer) } #endif -#if !GLIB_CHECK_VERSION(2, 34, 0) +#if !defined(g_clear_pointer) #define g_clear_pointer(x, destroy) \ _srt_clear_pointer (x, (GDestroyNotify) (void (*)(void)) destroy) /* A simplified version of g_clear_pointer without type-safety. */ diff --git a/tests/check-requirements-cli.c b/tests/check-requirements-cli.c index 684bfa87994532bb81d148344214dc02b2b8181f..75d294354773762cb501f369bf64ba13ff5169ca 100644 --- a/tests/check-requirements-cli.c +++ b/tests/check-requirements-cli.c @@ -25,6 +25,8 @@ #include <steam-runtime-tools/steam-runtime-tools.h> +#include <libglnx.h> + #include <steam-runtime-tools/glib-compat.h> #include <glib.h> diff --git a/tests/meson.build b/tests/meson.build index 1637002cf9bb3e74ad5f8faaa363e5a26a91e95a..88bcd87206f1072a3f908ba7ba0450f66ce20a85 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -44,10 +44,34 @@ if get_option('bin') ] endif -tests_utils = [ - 'fake-home.c', - 'test-utils.c', -] +test_utils = static_library( + 'test-utils', + sources : [ + 'fake-home.c', + 'test-utils.c', + ], + dependencies : [ + glib, + gobject, + gio_unix, + json_glib, + libglnx_dep, + libsteamrt_dep, + ], + include_directories : project_include_dirs, + install: false, +) +test_utils_dep = declare_dependency( + dependencies : [ + glib, + gobject, + gio_unix, + json_glib, + libglnx_dep, + libsteamrt_dep, + ], + link_with : test_utils, +) tests_dir = join_paths( get_option('libexecdir'), @@ -110,11 +134,19 @@ executable( foreach test_name : tests exe = executable( 'test-' + test_name, - files(test_name + '.c') + tests_utils, + files(test_name + '.c'), c_args : [ '-D_SRT_MULTIARCH="' + multiarch + '"', ], - dependencies : [glib, gobject, json_glib, libsteamrt_dep], + dependencies : [ + glib, + gobject, + gio_unix, + json_glib, + libglnx_dep, + libsteamrt_dep, + test_utils_dep, + ], include_directories : project_include_dirs, install : get_option('installed_tests'), install_dir : tests_dir, @@ -277,7 +309,7 @@ foreach arch : [ arch + '-' + helper, helper + '.c', c_args : ['-DMOCK_ARCHITECTURE_' + arch], - dependencies : [glib, gio], + dependencies : [glib, gio, test_utils_dep], include_directories : project_include_dirs, install: true, install_dir: tests_dir diff --git a/tests/mock-container-capsule-capture-libs.c b/tests/mock-container-capsule-capture-libs.c index 2626bba15ca86a3d0929d51e33e5567c60bfb395..721498875038b8ebf74f6e27725707ad1e541d5a 100644 --- a/tests/mock-container-capsule-capture-libs.c +++ b/tests/mock-container-capsule-capture-libs.c @@ -23,6 +23,8 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include <libglnx.h> + #include <steam-runtime-tools/glib-compat.h> #include <errno.h> diff --git a/tests/pressure-vessel/bwrap-lock.c b/tests/pressure-vessel/bwrap-lock.c index 88b8fed161fa3e2c13a37e6601d5aa438dbe1c3e..dc64f8c9a5541c78768c207929456da6bcffd515 100644 --- a/tests/pressure-vessel/bwrap-lock.c +++ b/tests/pressure-vessel/bwrap-lock.c @@ -32,7 +32,7 @@ #include "libglnx/libglnx.h" #include "bwrap-lock.h" -#include "test-utils.h" +#include "tests/test-utils.h" #include "utils.h" typedef struct diff --git a/tests/pressure-vessel/meson.build b/tests/pressure-vessel/meson.build index 5489263f2f277075516bce2884095b3ad271e65e..f60a2e77b5746a9724626ff033d0a304d7a6f67e 100644 --- a/tests/pressure-vessel/meson.build +++ b/tests/pressure-vessel/meson.build @@ -50,8 +50,12 @@ compiled_tests = [ foreach test_name : compiled_tests exe = executable( 'test-' + test_name, - files(test_name + '.c', 'test-utils.c'), - dependencies : [gio_unix, libglnx.get_variable('libglnx_dep')], + files(test_name + '.c'), + dependencies : [ + gio_unix, + libglnx_dep, + test_utils_dep, + ], link_with : [pressure_vessel_utils], include_directories : pv_include_dirs, install : false, @@ -153,7 +157,7 @@ foreach helper : helpers ], dependencies : [ gio_unix, - libglnx.get_variable('libglnx_dep'), + libglnx_dep, ], link_with : [ pressure_vessel_utils, diff --git a/tests/pressure-vessel/resolve-in-sysroot.c b/tests/pressure-vessel/resolve-in-sysroot.c index 7256764fc52b179c53d23805120401b020017add..f2af8354eafec5edbfdf7181739654c2acd54d92 100644 --- a/tests/pressure-vessel/resolve-in-sysroot.c +++ b/tests/pressure-vessel/resolve-in-sysroot.c @@ -33,7 +33,7 @@ #include "libglnx/libglnx.h" #include "resolve-in-sysroot.h" -#include "test-utils.h" +#include "tests/test-utils.h" #include "utils.h" typedef struct diff --git a/tests/pressure-vessel/test-utils.c b/tests/pressure-vessel/test-utils.c deleted file mode 100644 index fde49e1e88cf1615339ba34d5002148032a633b3..0000000000000000000000000000000000000000 --- a/tests/pressure-vessel/test-utils.c +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright © 2019-2020 Collabora Ltd. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "test-utils.h" - -#include <glib.h> -#include "libglnx/libglnx.h" - -TestsOpenFdSet -tests_check_fd_leaks_enter (void) -{ - g_autoptr(GHashTable) ret = NULL; - g_auto(GLnxDirFdIterator) iter = { FALSE }; - g_autoptr(GError) error = NULL; - - ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - - glnx_dirfd_iterator_init_at (AT_FDCWD, "/proc/self/fd", TRUE, - &iter, &error); - g_assert_no_error (error); - - while (TRUE) - { - g_autofree gchar *target = NULL; - struct dirent *dent; - gint64 which; - char *endptr; - - glnx_dirfd_iterator_next_dent (&iter, &dent, NULL, &error); - g_assert_no_error (error); - - if (dent == NULL) - break; - - if (g_str_equal (dent->d_name, ".") || g_str_equal (dent->d_name, "..")) - continue; - - which = g_ascii_strtoll (dent->d_name, &endptr, 10); - - if (endptr == NULL || *endptr != '\0') - { - g_warning ("Found unexpected entry \"%s\" in /proc/self/fd", - dent->d_name); - continue; - } - - if (which == (gint64) iter.fd) - continue; - - /* ignore error, just let it be NULL */ - target = glnx_readlinkat_malloc (iter.fd, dent->d_name, NULL, NULL); - g_hash_table_replace (ret, g_strdup (dent->d_name), - g_steal_pointer (&target)); - } - - return g_steal_pointer (&ret); -} - -void -tests_check_fd_leaks_leave (TestsOpenFdSet fds) -{ - g_auto(GLnxDirFdIterator) iter = { FALSE }; - g_autoptr(GError) error = NULL; - - glnx_dirfd_iterator_init_at (AT_FDCWD, "/proc/self/fd", TRUE, - &iter, &error); - g_assert_no_error (error); - - while (TRUE) - { - g_autofree gchar *target = NULL; - gpointer val = NULL; - gint64 which; - struct dirent *dent; - char *endptr; - - glnx_dirfd_iterator_next_dent (&iter, &dent, NULL, &error); - g_assert_no_error (error); - - if (dent == NULL) - break; - - if (g_str_equal (dent->d_name, ".") || g_str_equal (dent->d_name, "..")) - continue; - - which = g_ascii_strtoll (dent->d_name, &endptr, 10); - - if (endptr == NULL || *endptr != '\0') - { - g_warning ("Found unexpected entry \"%s\" in /proc/self/fd", - dent->d_name); - continue; - } - - if (which == (gint64) iter.fd) - continue; - - /* ignore error, just let it be NULL */ - target = glnx_readlinkat_malloc (iter.fd, dent->d_name, NULL, NULL); - - if (g_hash_table_lookup_extended (fds, dent->d_name, NULL, &val)) - { - g_assert_cmpstr (target, ==, val); - } - else - { - g_error ("fd %s \"%s\" was leaked", - dent->d_name, target == NULL ? "(null)" : target); - } - } - - g_hash_table_unref (fds); -} diff --git a/tests/pressure-vessel/test-utils.h b/tests/pressure-vessel/test-utils.h deleted file mode 100644 index 8d8ecc8c189fcce5cdecee3b66d592b12306d921..0000000000000000000000000000000000000000 --- a/tests/pressure-vessel/test-utils.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright © 2019-2020 Collabora Ltd. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#pragma once - -/* - * Simplified implementations of some of the GLib test assertion macros, - * for use with older GLib versions. - */ - -#include <glib.h> - -/* for its backports of g_test_skip(), etc. */ -#include <libglnx/libglnx.h> - -#ifndef g_assert_true -#define g_assert_true(x) g_assert ((x)) -#endif - -#ifndef g_assert_false -#define g_assert_false(x) g_assert (!(x)) -#endif - -#ifndef g_assert_cmpint -#define g_assert_cmpint(a, op, b) g_assert ((a) op (b)) -#endif - -#ifndef g_assert_cmpmem -#define g_assert_cmpmem(m1, l1, m2, l2) \ - g_assert (l1 == l2 && memcmp (m1, m2, l1) == 0) -#endif - -#ifndef g_assert_cmpstr -#define g_assert_cmpstr(a, op, b) g_assert (g_strcmp0 ((a), (b)) op 0) -#endif - -/* - * Other assorted test helpers. - */ - -typedef GHashTable *TestsOpenFdSet; -TestsOpenFdSet tests_check_fd_leaks_enter (void); -void tests_check_fd_leaks_leave (TestsOpenFdSet fds); diff --git a/tests/pressure-vessel/utils.c b/tests/pressure-vessel/utils.c index 57f944ba3ecaaf8fbac90158b4562abcdf1e0418..1625efb5a5785a7df53b8f69d3210bf9b270a96d 100644 --- a/tests/pressure-vessel/utils.c +++ b/tests/pressure-vessel/utils.c @@ -32,7 +32,7 @@ #include "glib-backports.h" #include "libglnx/libglnx.h" -#include "test-utils.h" +#include "tests/test-utils.h" #include "utils.h" typedef struct diff --git a/tests/pressure-vessel/wait-for-child-processes.c b/tests/pressure-vessel/wait-for-child-processes.c index f00c88c241fe267d1f267d805bbc18c66771b64d..edd3d0307ae9cfa7f7de3924a52a66c893313c52 100644 --- a/tests/pressure-vessel/wait-for-child-processes.c +++ b/tests/pressure-vessel/wait-for-child-processes.c @@ -35,7 +35,7 @@ #include "glib-backports.h" #include "libglnx/libglnx.h" -#include "test-utils.h" +#include "tests/test-utils.h" #include "utils.h" typedef struct diff --git a/tests/system-info-cli.c b/tests/system-info-cli.c index 665afc7981181ac381d25a409d5f3d8a92639e19..ba3cac9fdbf2926dd31c1dbc035a7d4600ef3684 100644 --- a/tests/system-info-cli.c +++ b/tests/system-info-cli.c @@ -23,6 +23,8 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include <libglnx.h> + #include <steam-runtime-tools/steam-runtime-tools.h> #include <steam-runtime-tools/glib-compat.h> diff --git a/tests/system-info.c b/tests/system-info.c index 00e3a901f334586fc47342e954c2efc1690a5caf..5dc48f4db35db9f8fef843c3e8bbe32b74394485 100644 --- a/tests/system-info.c +++ b/tests/system-info.c @@ -23,6 +23,8 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include <libglnx.h> + #include <steam-runtime-tools/steam-runtime-tools.h> #include <steam-runtime-tools/glib-compat.h> diff --git a/tests/test-utils.c b/tests/test-utils.c index c3a6dded9d3da0343345ee8f5488902c522b7bd6..a45007d7dc583fdba89abe95c15cbbceb2f77634 100644 --- a/tests/test-utils.c +++ b/tests/test-utils.c @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Collabora Ltd. + * Copyright © 2019-2020 Collabora Ltd. * * SPDX-License-Identifier: MIT * @@ -23,9 +23,13 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "test-utils.h" +#include "tests/test-utils.h" +#include <glib.h> #include <glib-object.h> + +#include "libglnx/libglnx.h" + #include "steam-runtime-tools/glib-compat.h" #include <steam-runtime-tools/steam-runtime-tools.h> @@ -91,4 +95,110 @@ _srt_global_teardown_private_xdg_dirs (void) fake_home_parent = NULL; return result; -} \ No newline at end of file +} + +TestsOpenFdSet +tests_check_fd_leaks_enter (void) +{ + g_autoptr(GHashTable) ret = NULL; + g_auto(GLnxDirFdIterator) iter = { FALSE }; + g_autoptr(GError) error = NULL; + + ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + glnx_dirfd_iterator_init_at (AT_FDCWD, "/proc/self/fd", TRUE, + &iter, &error); + g_assert_no_error (error); + + while (TRUE) + { + g_autofree gchar *target = NULL; + struct dirent *dent; + gint64 which; + char *endptr; + + glnx_dirfd_iterator_next_dent (&iter, &dent, NULL, &error); + g_assert_no_error (error); + + if (dent == NULL) + break; + + if (g_str_equal (dent->d_name, ".") || g_str_equal (dent->d_name, "..")) + continue; + + which = g_ascii_strtoll (dent->d_name, &endptr, 10); + + if (endptr == NULL || *endptr != '\0') + { + g_warning ("Found unexpected entry \"%s\" in /proc/self/fd", + dent->d_name); + continue; + } + + if (which == (gint64) iter.fd) + continue; + + /* ignore error, just let it be NULL */ + target = glnx_readlinkat_malloc (iter.fd, dent->d_name, NULL, NULL); + g_hash_table_replace (ret, g_strdup (dent->d_name), + g_steal_pointer (&target)); + } + + return g_steal_pointer (&ret); +} + +void +tests_check_fd_leaks_leave (TestsOpenFdSet fds) +{ + g_auto(GLnxDirFdIterator) iter = { FALSE }; + g_autoptr(GError) error = NULL; + + glnx_dirfd_iterator_init_at (AT_FDCWD, "/proc/self/fd", TRUE, + &iter, &error); + g_assert_no_error (error); + + while (TRUE) + { + g_autofree gchar *target = NULL; + gpointer val = NULL; + gint64 which; + struct dirent *dent; + char *endptr; + + glnx_dirfd_iterator_next_dent (&iter, &dent, NULL, &error); + g_assert_no_error (error); + + if (dent == NULL) + break; + + if (g_str_equal (dent->d_name, ".") || g_str_equal (dent->d_name, "..")) + continue; + + which = g_ascii_strtoll (dent->d_name, &endptr, 10); + + if (endptr == NULL || *endptr != '\0') + { + g_warning ("Found unexpected entry \"%s\" in /proc/self/fd", + dent->d_name); + continue; + } + + if (which == (gint64) iter.fd) + continue; + + /* ignore error, just let it be NULL */ + target = glnx_readlinkat_malloc (iter.fd, dent->d_name, NULL, NULL); + + if (g_hash_table_lookup_extended (fds, dent->d_name, NULL, &val)) + { + g_assert_cmpstr (target, ==, val); + } + else + { + g_error ("fd %s \"%s\" was leaked", + dent->d_name, target == NULL ? "(null)" : target); + } + } + + g_hash_table_unref (fds); +} diff --git a/tests/test-utils.h b/tests/test-utils.h index 2973e2bc720ad216d58e6319635d8e18d262bcbc..5a86dcae9efa4918b9f9b41931c1860fc903cbdb 100644 --- a/tests/test-utils.h +++ b/tests/test-utils.h @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Collabora Ltd. + * Copyright © 2019-2020 Collabora Ltd. * * SPDX-License-Identifier: MIT * @@ -32,6 +32,9 @@ #include <glib.h> +/* for its backports of g_test_skip(), etc. */ +#include <libglnx/libglnx.h> + #ifndef g_assert_true #define g_assert_true(x) g_assert ((x)) #endif @@ -53,17 +56,13 @@ #define g_assert_cmpstr(a, op, b) g_assert (g_strcmp0 ((a), (b)) op 0) #endif -#ifndef g_assert_nonnull -#define g_assert_nonnull(x) g_assert ((x) != NULL) -#endif - -#ifndef g_assert_null -#define g_assert_null(x) g_assert ((x) == NULL) -#endif - -#if !GLIB_CHECK_VERSION(2, 38, 0) -#define g_test_skip(msg) g_test_message ("SKIP: %s", msg) -#endif +/* + * Other assorted test helpers. + */ gchar *_srt_global_setup_private_xdg_dirs (void); gboolean _srt_global_teardown_private_xdg_dirs (void); + +typedef GHashTable *TestsOpenFdSet; +TestsOpenFdSet tests_check_fd_leaks_enter (void); +void tests_check_fd_leaks_leave (TestsOpenFdSet fds);