1 /* module-test.c - test program for GMODULE
2 * Copyright (C) 1998 Tim Janik
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 */
26
27 #include <gmodule.h>
28 #include <glib/gstdio.h>
29
30 #ifdef _MSC_VER
31 # define MODULE_FILENAME_PREFIX ""
32 #else
33 # define MODULE_FILENAME_PREFIX "lib"
34 #endif
35
36 gchar *global_state = NULL;
37
38 G_MODULE_EXPORT void g_clash_func (void);
39
40 G_MODULE_EXPORT void
41 g_clash_func (void)
42 {
43 global_state = "global clash";
44 }
45
46 typedef void (*SimpleFunc) (void);
47 typedef void (*GModuleFunc) (GModule *);
48
49 static gchar **gplugin_a_state;
50 static gchar **gplugin_b_state;
51
52 static void
53 compare (const gchar *desc, const gchar *expected, const gchar *found)
54 {
55 if (!expected && !found)
56 return;
57
58 if (expected && found && strcmp (expected, found) == 0)
59 return;
60
61 g_error ("error: %s state should have been \"%s\", but is \"%s\"",
62 desc, expected ? expected : "NULL", found ? found : "NULL");
63 }
64
65 static void
66 test_states (const gchar *global, const gchar *gplugin_a, const gchar *gplugin_b)
67 {
68 compare ("global", global, global_state);
69 compare ("Plugin A", gplugin_a, *gplugin_a_state);
70 compare ("Plugin B", gplugin_b, *gplugin_b_state);
71
72 global_state = *gplugin_a_state = *gplugin_b_state = NULL;
73 }
74
75 static SimpleFunc plugin_clash_func = NULL;
76
77 static void
78 test_module_basics (void)
79 {
80 GModule *module_self, *module_a, *module_b;
81 gchar *plugin_a, *plugin_b;
82 SimpleFunc f_a, f_b, f_self;
83 GModuleFunc gmod_f;
84 GError *error = NULL;
85
86 if (!g_module_supported ())
87 g_error ("dynamic modules not supported");
88
89 plugin_a = g_test_build_filename (G_TEST_BUILT, MODULE_FILENAME_PREFIX "moduletestplugin_a_" MODULE_TYPE, NULL);
90 plugin_b = g_test_build_filename (G_TEST_BUILT, MODULE_FILENAME_PREFIX "moduletestplugin_b_" MODULE_TYPE, NULL);
91
92 /* module handles */
93
94 module_self = g_module_open_full (NULL, G_MODULE_BIND_LAZY, &error);
95 g_assert_no_error (error);
96 if (!module_self)
97 g_error ("error: %s", g_module_error ());
98
99 /* On Windows static compilation mode, glib API symbols are not
100 * exported dynamically by definition. */
101 #if !defined(G_PLATFORM_WIN32) || !defined(GLIB_STATIC_COMPILATION)
102 if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
103 g_error ("error: %s", g_module_error ());
104 #endif
105
106 module_a = g_module_open_full (plugin_a, G_MODULE_BIND_LAZY, &error);
107 g_assert_no_error (error);
108 if (!module_a)
109 g_error ("error: %s", g_module_error ());
110
111 module_b = g_module_open_full (plugin_b, G_MODULE_BIND_LAZY, &error);
112 g_assert_no_error (error);
113 if (!module_b)
114 g_error ("error: %s", g_module_error ());
115
116 /* get plugin state vars */
117
118 if (!g_module_symbol (module_a, "gplugin_a_state",
119 (gpointer *) &gplugin_a_state))
120 g_error ("error: %s", g_module_error ());
121
122 if (!g_module_symbol (module_b, "gplugin_b_state",
123 (gpointer *) &gplugin_b_state))
124 g_error ("error: %s", g_module_error ());
125 test_states (NULL, NULL, "check-init");
126
127 /* get plugin specific symbols and call them */
128
129 if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
130 g_error ("error: %s", g_module_error ());
131 test_states (NULL, NULL, NULL);
132
133 if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
134 g_error ("error: %s", g_module_error ());
135 test_states (NULL, NULL, NULL);
136
137 f_a ();
138 test_states (NULL, "Hello world", NULL);
139
140 f_b ();
141 test_states (NULL, NULL, "Hello world");
142
143 /* get and call globally clashing functions */
144
145 if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
146 g_error ("error: %s", g_module_error ());
147 test_states (NULL, NULL, NULL);
148
149 if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
150 g_error ("error: %s", g_module_error ());
151 test_states (NULL, NULL, NULL);
152
153 if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
154 g_error ("error: %s", g_module_error ());
155 test_states (NULL, NULL, NULL);
156
157 f_self ();
158 test_states ("global clash", NULL, NULL);
159
160 f_a ();
161 test_states (NULL, "global clash", NULL);
162
163 f_b ();
164 test_states (NULL, NULL, "global clash");
165
166 /* get and call clashing plugin functions */
167
168 if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
169 g_error ("error: %s", g_module_error ());
170 test_states (NULL, NULL, NULL);
171
172 if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
173 g_error ("error: %s", g_module_error ());
174 test_states (NULL, NULL, NULL);
175
176 plugin_clash_func = f_a;
177 plugin_clash_func ();
178 test_states (NULL, "plugin clash", NULL);
179
180 plugin_clash_func = f_b;
181 plugin_clash_func ();
182 test_states (NULL, NULL, "plugin clash");
183
184 /* call gmodule function from A */
185
186 if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
187 g_error ("error: %s", g_module_error ());
188 test_states (NULL, NULL, NULL);
189
190 gmod_f (module_b);
191 test_states (NULL, NULL, "BOOH");
192
193 gmod_f (module_a);
194 test_states (NULL, "BOOH", NULL);
195
196 /* unload plugins */
197
198 if (!g_module_close (module_a))
199 g_error ("error: %s", g_module_error ());
200
201 if (!g_module_close (module_b))
202 g_error ("error: %s", g_module_error ());
203
204 g_free (plugin_a);
205 g_free (plugin_b);
206 g_module_close (module_self);
207 }
208
209 static void
210 test_module_invalid_libtool_archive (void)
211 {
212 int la_fd;
213 gchar *la_filename = NULL;
214 GModule *module = NULL;
215 GError *local_error = NULL;
216
217 g_test_summary ("Test that opening an invalid .la file fails");
218
219 /* Create an empty temporary file ending in `.la` */
220 la_fd = g_file_open_tmp ("gmodule-invalid-XXXXXX.la", &la_filename, &local_error);
221 g_assert_no_error (local_error);
222 g_assert_true (g_str_has_suffix (la_filename, ".la"));
223 g_close (la_fd, NULL);
224
225 /* Try loading it */
226 module = g_module_open_full (la_filename, 0, &local_error);
227 g_assert_error (local_error, G_MODULE_ERROR, G_MODULE_ERROR_FAILED);
228 g_assert_null (module);
229 g_clear_error (&local_error);
230
231 (void) g_unlink (la_filename);
232
233 g_free (la_filename);
234 }
235
236 int
237 main (int argc, char *argv[])
238 {
239 g_test_init (&argc, &argv, NULL);
240
241 g_test_add_func ("/module/basics", test_module_basics);
242 g_test_add_func ("/module/invalid-libtool-archive", test_module_invalid_libtool_archive);
243
244 return g_test_run ();
245 }