2013-12-24 06:03:20 +01:00
|
|
|
/* Unit tests for GIOModule
|
|
|
|
* Copyright (C) 2013 Red Hat, Inc
|
|
|
|
* Author: Matthias Clasen
|
|
|
|
*
|
|
|
|
* This work is provided "as is"; redistribution and modification
|
|
|
|
* in whole or in part, in any medium, physical or electronic is
|
|
|
|
* permitted without restriction.
|
|
|
|
*
|
|
|
|
* This work 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.
|
|
|
|
*
|
|
|
|
* In no event shall the authors or contributors be liable for any
|
|
|
|
* direct, indirect, incidental, special, exemplary, or consequential
|
|
|
|
* damages (including, but not limited to, procurement of substitute
|
|
|
|
* goods or services; loss of use, data, or profits; or business
|
|
|
|
* interruption) however caused and on any theory of liability, whether
|
|
|
|
* in contract, strict liability, or tort (including negligence or
|
|
|
|
* otherwise) arising in any way out of the use of this software, even
|
|
|
|
* if advised of the possibility of such damage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gio/gio.h>
|
2019-07-15 13:01:04 +02:00
|
|
|
#include <glibconfig.h>
|
2013-12-24 06:03:20 +01:00
|
|
|
|
Improve g_module_open(), deprecate G_MODULE_SUFFIX
G_MODULE_SUFFIX is deprecated now because you will get the wrong
results using it most of the time:
1. The suffix on macOS is usually 'dylib', but it's 'so' when using
Autotools, so there's no way to get the suffix correct using
a pre-processor macro.
2. Prefixes also vary in a platform-specific way. You may or may not have
a 'lib' prefix for the name on Windows and on Cygwin the prefix is
'cyg'.
3. The library name itself can vary per platform. For instance, you may
want to load foo-1.dll on Windows and libfoo.1.dylib on macOS. This
is for libraries, not modules, but that is still a use-case that
people use the GModule API for.
g_module_build_path() does take care of (2) on Cygwin, but it
fundamentally cannot handle the possibility of multiple options for
the module name, since it does not do any I/O. Hence, it is also
deprecated.
Instead, g_module_open() has been improved so that it takes care of
all this by searching the filesystem for combinations of possible
suffixes and prefixes on each platform. Along the way, the
documentation for it was also improved to make it clearer what it
does.
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/520
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/1413
2022-10-14 01:04:13 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define MODULE_FILENAME(x) "" x ".dll"
|
|
|
|
#else
|
|
|
|
#define MODULE_FILENAME(x) "lib" x ".dll"
|
|
|
|
#endif
|
|
|
|
#elif defined(G_OS_DARWIN)
|
|
|
|
#define MODULE_FILENAME(x) "lib" x ".dylib"
|
2019-06-21 07:04:34 +02:00
|
|
|
#else
|
Improve g_module_open(), deprecate G_MODULE_SUFFIX
G_MODULE_SUFFIX is deprecated now because you will get the wrong
results using it most of the time:
1. The suffix on macOS is usually 'dylib', but it's 'so' when using
Autotools, so there's no way to get the suffix correct using
a pre-processor macro.
2. Prefixes also vary in a platform-specific way. You may or may not have
a 'lib' prefix for the name on Windows and on Cygwin the prefix is
'cyg'.
3. The library name itself can vary per platform. For instance, you may
want to load foo-1.dll on Windows and libfoo.1.dylib on macOS. This
is for libraries, not modules, but that is still a use-case that
people use the GModule API for.
g_module_build_path() does take care of (2) on Cygwin, but it
fundamentally cannot handle the possibility of multiple options for
the module name, since it does not do any I/O. Hence, it is also
deprecated.
Instead, g_module_open() has been improved so that it takes care of
all this by searching the filesystem for combinations of possible
suffixes and prefixes on each platform. Along the way, the
documentation for it was also improved to make it clearer what it
does.
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/520
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/1413
2022-10-14 01:04:13 +02:00
|
|
|
#define MODULE_FILENAME(x) "lib" x ".so"
|
2019-06-21 07:04:34 +02:00
|
|
|
#endif
|
|
|
|
|
2013-12-24 06:03:20 +01:00
|
|
|
static void
|
|
|
|
test_extension_point (void)
|
|
|
|
{
|
|
|
|
GIOExtensionPoint *ep, *ep2;
|
|
|
|
GIOExtension *ext;
|
|
|
|
GList *list;
|
|
|
|
GType req;
|
|
|
|
GTypeClass *class;
|
|
|
|
|
|
|
|
ep = g_io_extension_point_lookup ("test-extension-point");
|
|
|
|
g_assert_null (ep);
|
|
|
|
ep = g_io_extension_point_register ("test-extension-point");
|
|
|
|
ep2 = g_io_extension_point_lookup ("test-extension-point");
|
|
|
|
g_assert (ep2 == ep);
|
|
|
|
|
|
|
|
req = g_io_extension_point_get_required_type (ep);
|
|
|
|
g_assert (req == G_TYPE_INVALID);
|
|
|
|
g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
|
|
|
|
req = g_io_extension_point_get_required_type (ep);
|
|
|
|
g_assert (req == G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
list = g_io_extension_point_get_extensions (ep);
|
|
|
|
g_assert_null (list);
|
|
|
|
|
|
|
|
g_io_extension_point_implement ("test-extension-point",
|
|
|
|
G_TYPE_VFS,
|
|
|
|
"extension1",
|
|
|
|
10);
|
|
|
|
|
|
|
|
g_io_extension_point_implement ("test-extension-point",
|
|
|
|
G_TYPE_OBJECT,
|
|
|
|
"extension2",
|
|
|
|
20);
|
|
|
|
|
|
|
|
list = g_io_extension_point_get_extensions (ep);
|
|
|
|
g_assert_cmpint (g_list_length (list), ==, 2);
|
|
|
|
|
|
|
|
ext = list->data;
|
|
|
|
g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension2");
|
|
|
|
g_assert (g_io_extension_get_type (ext) == G_TYPE_OBJECT);
|
|
|
|
g_assert (g_io_extension_get_priority (ext) == 20);
|
|
|
|
class = g_io_extension_ref_class (ext);
|
|
|
|
g_assert (class == g_type_class_peek (G_TYPE_OBJECT));
|
|
|
|
g_type_class_unref (class);
|
|
|
|
|
|
|
|
ext = list->next->data;
|
|
|
|
g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension1");
|
|
|
|
g_assert (g_io_extension_get_type (ext) == G_TYPE_VFS);
|
|
|
|
g_assert (g_io_extension_get_priority (ext) == 10);
|
|
|
|
}
|
|
|
|
|
2022-06-23 11:09:15 +02:00
|
|
|
#define INHERIT_ALL (G_TEST_SUBPROCESS_INHERIT_STDIN | \
|
|
|
|
G_TEST_SUBPROCESS_INHERIT_STDOUT | \
|
|
|
|
G_TEST_SUBPROCESS_INHERIT_STDERR)
|
|
|
|
|
2013-12-24 06:03:20 +01:00
|
|
|
static void
|
|
|
|
test_module_scan_all (void)
|
|
|
|
{
|
2019-07-15 13:01:04 +02:00
|
|
|
#ifdef GLIB_STATIC_COMPILATION
|
|
|
|
/* The plugin module is statically linked with a separate copy
|
|
|
|
* of GLib so g_io_extension_point_implement won't work. */
|
|
|
|
g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds.");
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2013-12-24 06:03:20 +01:00
|
|
|
if (g_test_subprocess ())
|
|
|
|
{
|
|
|
|
GIOExtensionPoint *ep;
|
|
|
|
GIOExtension *ext;
|
|
|
|
GList *list;
|
|
|
|
ep = g_io_extension_point_register ("test-extension-point");
|
2014-01-13 22:12:31 +01:00
|
|
|
g_io_modules_scan_all_in_directory (g_test_get_filename (G_TEST_BUILT, "modules", NULL));
|
2013-12-24 06:03:20 +01:00
|
|
|
list = g_io_extension_point_get_extensions (ep);
|
|
|
|
g_assert_cmpint (g_list_length (list), ==, 2);
|
|
|
|
ext = list->data;
|
|
|
|
g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-b");
|
|
|
|
ext = list->next->data;
|
|
|
|
g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a");
|
|
|
|
return;
|
|
|
|
}
|
2022-06-23 11:09:15 +02:00
|
|
|
g_test_trap_subprocess (NULL, 0, INHERIT_ALL);
|
2013-12-24 06:03:20 +01:00
|
|
|
g_test_trap_assert_passed ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_module_scan_all_with_scope (void)
|
|
|
|
{
|
2019-07-15 13:01:04 +02:00
|
|
|
#ifdef GLIB_STATIC_COMPILATION
|
|
|
|
/* Disabled for the same reason as test_module_scan_all. */
|
|
|
|
g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds.");
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2013-12-24 06:03:20 +01:00
|
|
|
if (g_test_subprocess ())
|
|
|
|
{
|
|
|
|
GIOExtensionPoint *ep;
|
|
|
|
GIOModuleScope *scope;
|
|
|
|
GIOExtension *ext;
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
ep = g_io_extension_point_register ("test-extension-point");
|
|
|
|
scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
|
Improve g_module_open(), deprecate G_MODULE_SUFFIX
G_MODULE_SUFFIX is deprecated now because you will get the wrong
results using it most of the time:
1. The suffix on macOS is usually 'dylib', but it's 'so' when using
Autotools, so there's no way to get the suffix correct using
a pre-processor macro.
2. Prefixes also vary in a platform-specific way. You may or may not have
a 'lib' prefix for the name on Windows and on Cygwin the prefix is
'cyg'.
3. The library name itself can vary per platform. For instance, you may
want to load foo-1.dll on Windows and libfoo.1.dylib on macOS. This
is for libraries, not modules, but that is still a use-case that
people use the GModule API for.
g_module_build_path() does take care of (2) on Cygwin, but it
fundamentally cannot handle the possibility of multiple options for
the module name, since it does not do any I/O. Hence, it is also
deprecated.
Instead, g_module_open() has been improved so that it takes care of
all this by searching the filesystem for combinations of possible
suffixes and prefixes on each platform. Along the way, the
documentation for it was also improved to make it clearer what it
does.
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/520
Closes https://gitlab.gnome.org/GNOME/glib/-/issues/1413
2022-10-14 01:04:13 +02:00
|
|
|
g_io_module_scope_block (scope, MODULE_FILENAME ("testmoduleb"));
|
2014-01-13 22:12:31 +01:00
|
|
|
g_io_modules_scan_all_in_directory_with_scope (g_test_get_filename (G_TEST_BUILT, "modules", NULL), scope);
|
2013-12-24 06:03:20 +01:00
|
|
|
list = g_io_extension_point_get_extensions (ep);
|
|
|
|
g_assert_cmpint (g_list_length (list), ==, 1);
|
|
|
|
ext = list->data;
|
|
|
|
g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a");
|
|
|
|
g_io_module_scope_free (scope);
|
|
|
|
return;
|
|
|
|
}
|
2022-06-23 11:09:15 +02:00
|
|
|
g_test_trap_subprocess (NULL, 0, INHERIT_ALL);
|
2013-12-24 06:03:20 +01:00
|
|
|
g_test_trap_assert_passed ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
|
|
|
g_test_add_func ("/giomodule/extension-point", test_extension_point);
|
|
|
|
g_test_add_func ("/giomodule/module-scan-all", test_module_scan_all);
|
|
|
|
g_test_add_func ("/giomodule/module-scan-all-with-scope", test_module_scan_all_with_scope);
|
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|