1 /* Unit tests for GIOModule
2 * Copyright (C) 2013 Red Hat, Inc
3 * Author: Matthias Clasen
4 *
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 *
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
10 *
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
23 */
24
25 #include <gio/gio.h>
26 #include <glibconfig.h>
27
28 #ifdef G_OS_WIN32
29 #ifdef _MSC_VER
30 #define MODULE_FILENAME(x) "" x ".dll"
31 #else
32 #define MODULE_FILENAME(x) "lib" x ".dll"
33 #endif
34 #elif defined(__APPLE__)
35 #define MODULE_FILENAME(x) "lib" x ".dylib"
36 #else
37 #define MODULE_FILENAME(x) "lib" x ".so"
38 #endif
39
40 static void
41 test_extension_point (void)
42 {
43 GIOExtensionPoint *ep, *ep2;
44 GIOExtension *ext;
45 GList *list;
46 GType req;
47 GTypeClass *class;
48
49 ep = g_io_extension_point_lookup ("test-extension-point");
50 g_assert_null (ep);
51 ep = g_io_extension_point_register ("test-extension-point");
52 ep2 = g_io_extension_point_lookup ("test-extension-point");
53 g_assert (ep2 == ep);
54
55 req = g_io_extension_point_get_required_type (ep);
56 g_assert (req == G_TYPE_INVALID);
57 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
58 req = g_io_extension_point_get_required_type (ep);
59 g_assert (req == G_TYPE_OBJECT);
60
61 list = g_io_extension_point_get_extensions (ep);
62 g_assert_null (list);
63
64 g_io_extension_point_implement ("test-extension-point",
65 G_TYPE_VFS,
66 "extension1",
67 10);
68
69 g_io_extension_point_implement ("test-extension-point",
70 G_TYPE_OBJECT,
71 "extension2",
72 20);
73
74 list = g_io_extension_point_get_extensions (ep);
75 g_assert_cmpint (g_list_length (list), ==, 2);
76
77 ext = list->data;
78 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension2");
79 g_assert (g_io_extension_get_type (ext) == G_TYPE_OBJECT);
80 g_assert (g_io_extension_get_priority (ext) == 20);
81 class = g_io_extension_ref_class (ext);
82 g_assert (class == g_type_class_peek (G_TYPE_OBJECT));
83 g_type_class_unref (class);
84
85 ext = list->next->data;
86 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension1");
87 g_assert (g_io_extension_get_type (ext) == G_TYPE_VFS);
88 g_assert (g_io_extension_get_priority (ext) == 10);
89 }
90
91 #define INHERIT_ALL (G_TEST_SUBPROCESS_INHERIT_STDIN | \
92 G_TEST_SUBPROCESS_INHERIT_STDOUT | \
93 G_TEST_SUBPROCESS_INHERIT_STDERR)
94
95 static void
96 test_module_scan_all (void)
97 {
98 #ifdef GLIB_STATIC_COMPILATION
99 /* The plugin module is statically linked with a separate copy
100 * of GLib so g_io_extension_point_implement won't work. */
101 g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds.");
102 return;
103 #endif
104
105 if (g_test_subprocess ())
106 {
107 GIOExtensionPoint *ep;
108 GIOExtension *ext;
109 GList *list;
110 ep = g_io_extension_point_register ("test-extension-point");
111 g_io_modules_scan_all_in_directory (g_test_get_filename (G_TEST_BUILT, "modules", NULL));
112 list = g_io_extension_point_get_extensions (ep);
113 g_assert_cmpint (g_list_length (list), ==, 2);
114 ext = list->data;
115 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-b");
116 ext = list->next->data;
117 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a");
118 return;
119 }
120 g_test_trap_subprocess (NULL, 0, INHERIT_ALL);
121 g_test_trap_assert_passed ();
122 }
123
124 static void
125 test_module_scan_all_with_scope (void)
126 {
127 #ifdef GLIB_STATIC_COMPILATION
128 /* Disabled for the same reason as test_module_scan_all. */
129 g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds.");
130 return;
131 #endif
132
133 if (g_test_subprocess ())
134 {
135 GIOExtensionPoint *ep;
136 GIOModuleScope *scope;
137 GIOExtension *ext;
138 GList *list;
139
140 ep = g_io_extension_point_register ("test-extension-point");
141 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
142 g_io_module_scope_block (scope, MODULE_FILENAME ("testmoduleb"));
143 g_io_modules_scan_all_in_directory_with_scope (g_test_get_filename (G_TEST_BUILT, "modules", NULL), scope);
144 list = g_io_extension_point_get_extensions (ep);
145 g_assert_cmpint (g_list_length (list), ==, 1);
146 ext = list->data;
147 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a");
148 g_io_module_scope_free (scope);
149 return;
150 }
151 g_test_trap_subprocess (NULL, 0, INHERIT_ALL);
152 g_test_trap_assert_passed ();
153 }
154
155 int
156 main (int argc, char *argv[])
157 {
158 g_test_init (&argc, &argv, NULL);
159
160 g_test_add_func ("/giomodule/extension-point", test_extension_point);
161 g_test_add_func ("/giomodule/module-scan-all", test_module_scan_all);
162 g_test_add_func ("/giomodule/module-scan-all-with-scope", test_module_scan_all_with_scope);
163
164 return g_test_run ();
165 }