Branch data Line data Source code
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 : 2 : g_clash_func (void)
42 : : {
43 : 2 : global_state = "global clash";
44 : 2 : }
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 : 108 : compare (const gchar *desc, const gchar *expected, const gchar *found)
54 : : {
55 [ + + + - ]: 108 : if (!expected && !found)
56 : 88 : return;
57 : :
58 [ + - + - : 20 : if (expected && found && strcmp (expected, found) == 0)
+ - ]
59 : 20 : return;
60 : :
61 [ # # # # ]: 0 : 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 : 36 : test_states (const gchar *global, const gchar *gplugin_a, const gchar *gplugin_b)
67 : : {
68 : 36 : compare ("global", global, global_state);
69 : 36 : compare ("Plugin A", gplugin_a, *gplugin_a_state);
70 : 36 : compare ("Plugin B", gplugin_b, *gplugin_b_state);
71 : :
72 : 36 : global_state = *gplugin_a_state = *gplugin_b_state = NULL;
73 : 36 : }
74 : :
75 : : static SimpleFunc plugin_clash_func = NULL;
76 : :
77 : : static void
78 : 2 : 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 : 2 : GError *error = NULL;
85 : :
86 [ - + ]: 2 : if (!g_module_supported ())
87 : 0 : g_error ("dynamic modules not supported");
88 : :
89 : 2 : plugin_a = g_test_build_filename (G_TEST_BUILT, MODULE_FILENAME_PREFIX "moduletestplugin_a_" MODULE_TYPE, NULL);
90 : 2 : plugin_b = g_test_build_filename (G_TEST_BUILT, MODULE_FILENAME_PREFIX "moduletestplugin_b_" MODULE_TYPE, NULL);
91 : :
92 : : /* module handles */
93 : :
94 : 2 : module_self = g_module_open_full (NULL, G_MODULE_BIND_LAZY, &error);
95 : 2 : g_assert_no_error (error);
96 [ - + ]: 2 : if (!module_self)
97 : 0 : 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 [ - + ]: 2 : if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
103 : 0 : g_error ("error: %s", g_module_error ());
104 : : #endif
105 : :
106 : 2 : module_a = g_module_open_full (plugin_a, G_MODULE_BIND_LAZY, &error);
107 : 2 : g_assert_no_error (error);
108 [ - + ]: 2 : if (!module_a)
109 : 0 : g_error ("error: %s", g_module_error ());
110 : :
111 : 2 : module_b = g_module_open_full (plugin_b, G_MODULE_BIND_LAZY, &error);
112 : 2 : g_assert_no_error (error);
113 [ - + ]: 2 : if (!module_b)
114 : 0 : g_error ("error: %s", g_module_error ());
115 : :
116 : : /* get plugin state vars */
117 : :
118 [ - + ]: 2 : if (!g_module_symbol (module_a, "gplugin_a_state",
119 : : (gpointer *) &gplugin_a_state))
120 : 0 : g_error ("error: %s", g_module_error ());
121 : :
122 [ - + ]: 2 : if (!g_module_symbol (module_b, "gplugin_b_state",
123 : : (gpointer *) &gplugin_b_state))
124 : 0 : g_error ("error: %s", g_module_error ());
125 : 2 : test_states (NULL, NULL, "check-init");
126 : :
127 : : /* get plugin specific symbols and call them */
128 : :
129 [ - + ]: 2 : if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
130 : 0 : g_error ("error: %s", g_module_error ());
131 : 2 : test_states (NULL, NULL, NULL);
132 : :
133 [ - + ]: 2 : if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
134 : 0 : g_error ("error: %s", g_module_error ());
135 : 2 : test_states (NULL, NULL, NULL);
136 : :
137 : 2 : f_a ();
138 : 2 : test_states (NULL, "Hello world", NULL);
139 : :
140 : 2 : f_b ();
141 : 2 : test_states (NULL, NULL, "Hello world");
142 : :
143 : : /* get and call globally clashing functions */
144 : :
145 [ - + ]: 2 : if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
146 : 0 : g_error ("error: %s", g_module_error ());
147 : 2 : test_states (NULL, NULL, NULL);
148 : :
149 [ - + ]: 2 : if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
150 : 0 : g_error ("error: %s", g_module_error ());
151 : 2 : test_states (NULL, NULL, NULL);
152 : :
153 [ - + ]: 2 : if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
154 : 0 : g_error ("error: %s", g_module_error ());
155 : 2 : test_states (NULL, NULL, NULL);
156 : :
157 : 2 : f_self ();
158 : 2 : test_states ("global clash", NULL, NULL);
159 : :
160 : 2 : f_a ();
161 : 2 : test_states (NULL, "global clash", NULL);
162 : :
163 : 2 : f_b ();
164 : 2 : test_states (NULL, NULL, "global clash");
165 : :
166 : : /* get and call clashing plugin functions */
167 : :
168 [ - + ]: 2 : if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
169 : 0 : g_error ("error: %s", g_module_error ());
170 : 2 : test_states (NULL, NULL, NULL);
171 : :
172 [ - + ]: 2 : if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
173 : 0 : g_error ("error: %s", g_module_error ());
174 : 2 : test_states (NULL, NULL, NULL);
175 : :
176 : 2 : plugin_clash_func = f_a;
177 : 2 : plugin_clash_func ();
178 : 2 : test_states (NULL, "plugin clash", NULL);
179 : :
180 : 2 : plugin_clash_func = f_b;
181 : 2 : plugin_clash_func ();
182 : 2 : test_states (NULL, NULL, "plugin clash");
183 : :
184 : : /* call gmodule function from A */
185 : :
186 [ - + ]: 2 : if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
187 : 0 : g_error ("error: %s", g_module_error ());
188 : 2 : test_states (NULL, NULL, NULL);
189 : :
190 : 2 : gmod_f (module_b);
191 : 2 : test_states (NULL, NULL, "BOOH");
192 : :
193 : 2 : gmod_f (module_a);
194 : 2 : test_states (NULL, "BOOH", NULL);
195 : :
196 : : /* unload plugins */
197 : :
198 [ - + ]: 2 : if (!g_module_close (module_a))
199 : 0 : g_error ("error: %s", g_module_error ());
200 : :
201 [ - + ]: 2 : if (!g_module_close (module_b))
202 : 0 : g_error ("error: %s", g_module_error ());
203 : :
204 : 2 : g_free (plugin_a);
205 : 2 : g_free (plugin_b);
206 : 2 : g_module_close (module_self);
207 : 2 : }
208 : :
209 : : static void
210 : 2 : test_module_invalid_libtool_archive (void)
211 : : {
212 : : int la_fd;
213 : 2 : gchar *la_filename = NULL;
214 : 2 : GModule *module = NULL;
215 : 2 : GError *local_error = NULL;
216 : :
217 : 2 : g_test_summary ("Test that opening an invalid .la file fails");
218 : :
219 : : /* Create an empty temporary file ending in `.la` */
220 : 2 : la_fd = g_file_open_tmp ("gmodule-invalid-XXXXXX.la", &la_filename, &local_error);
221 : 2 : g_assert_no_error (local_error);
222 : 2 : g_assert_true (g_str_has_suffix (la_filename, ".la"));
223 : 2 : g_close (la_fd, NULL);
224 : :
225 : : /* Try loading it */
226 : 2 : module = g_module_open_full (la_filename, 0, &local_error);
227 : 2 : g_assert_error (local_error, G_MODULE_ERROR, G_MODULE_ERROR_FAILED);
228 : 2 : g_assert_null (module);
229 : 2 : g_clear_error (&local_error);
230 : :
231 : 2 : (void) g_unlink (la_filename);
232 : :
233 : 2 : g_free (la_filename);
234 : 2 : }
235 : :
236 : : int
237 : 2 : main (int argc, char *argv[])
238 : : {
239 : 2 : g_test_init (&argc, &argv, NULL);
240 : :
241 : 2 : g_test_add_func ("/module/basics", test_module_basics);
242 : 2 : g_test_add_func ("/module/invalid-libtool-archive", test_module_invalid_libtool_archive);
243 : :
244 : 2 : return g_test_run ();
245 : : }
|