signals: Fix leak in test
[platform/upstream/glib.git] / tests / module-test.c
1 /* module-test.c - test program for GMODULE
2  * Copyright (C) 1998 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
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 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <gmodule.h>
31 #include <string.h>
32
33 gchar* global_state;
34
35 G_MODULE_EXPORT void g_clash_func (void);
36
37 G_MODULE_EXPORT void
38 g_clash_func (void)
39 {
40   global_state = "global clash";
41 }
42
43 typedef void (*SimpleFunc) (void);
44 typedef void (*GModuleFunc) (GModule *);
45
46 static gchar **gplugin_a_state;
47 static gchar **gplugin_b_state;
48
49 static void
50 compare (const gchar *desc, const gchar *expected, const gchar *found)
51 {
52   if (!expected && !found)
53     return;
54   
55   if (expected && found && strcmp (expected, found) == 0)
56     return;
57     
58   g_error ("error: %s state should have been \"%s\", but is \"%s\"",
59            desc, expected ? expected : "NULL", found ? found : "NULL");
60 }
61
62 static void 
63 test_states (const gchar *global, const gchar *gplugin_a, 
64              const gchar *gplugin_b)
65 {       
66   compare ("global", global, global_state);
67   compare ("Plugin A", gplugin_a, *gplugin_a_state);
68   compare ("Plugin B", gplugin_b, *gplugin_b_state);
69   
70   global_state = *gplugin_a_state = *gplugin_b_state = NULL;
71 }
72  
73 static SimpleFunc plugin_clash_func = NULL;
74
75 int
76 main (int    argc,
77       char **argv)
78 {
79   GModule *module_self, *module_a, *module_b;
80   gchar *plugin_a, *plugin_b;
81   SimpleFunc f_a, f_b, f_self;
82   GModuleFunc gmod_f;
83
84   g_test_init (&argc, &argv, 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, "libmoduletestplugin_a", NULL);
90   plugin_b = g_test_build_filename (G_TEST_BUILT, "libmoduletestplugin_b", NULL);
91
92   /* module handles */
93   
94   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
95   if (!module_self)
96     g_error ("error: %s", g_module_error ());
97
98   if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
99     g_error ("error: %s", g_module_error ());
100
101   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
102   if (!module_a)
103     g_error ("error: %s", g_module_error ());
104
105   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
106   if (!module_b)
107     g_error ("error: %s", g_module_error ());
108
109   /* get plugin state vars */
110
111   if (!g_module_symbol (module_a, "gplugin_a_state", 
112                         (gpointer *) &gplugin_a_state))
113     g_error ("error: %s", g_module_error ());
114   
115   if (!g_module_symbol (module_b, "gplugin_b_state", 
116                         (gpointer *) &gplugin_b_state))
117     g_error ("error: %s", g_module_error ());
118   test_states (NULL, NULL, "check-init");
119   
120   /* get plugin specific symbols and call them
121    */
122   if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
123     g_error ("error: %s", g_module_error ());
124   test_states (NULL, NULL, NULL);
125  
126   if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
127     g_error ("error: %s", g_module_error ());
128   test_states (NULL, NULL, NULL);
129  
130   f_a ();
131   test_states (NULL, "Hello world", NULL);
132   
133   f_b ();
134   test_states (NULL, NULL, "Hello world");
135   
136   /* get and call globally clashing functions
137    */
138  
139   if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
140     g_error ("error: %s", g_module_error ());
141   test_states (NULL, NULL, NULL);
142
143   if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
144     g_error ("error: %s", g_module_error ());
145   test_states (NULL, NULL, NULL);
146  
147   if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
148     g_error ("error: %s", g_module_error ());
149   test_states (NULL, NULL, NULL);
150  
151   f_self ();
152   test_states ("global clash", NULL, NULL);
153   
154   f_a ();
155   test_states (NULL, "global clash", NULL);
156
157   f_b ();
158   test_states (NULL, NULL, "global clash");
159
160   /* get and call clashing plugin functions  */
161
162   if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
163     g_error ("error: %s", g_module_error ());
164   test_states (NULL, NULL, NULL);
165
166   if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
167     g_error ("error: %s", g_module_error ());
168   test_states (NULL, NULL, NULL);
169
170   plugin_clash_func = f_a;
171   plugin_clash_func ();
172   test_states (NULL, "plugin clash", NULL);
173
174   plugin_clash_func = f_b;
175   plugin_clash_func ();
176   test_states (NULL, NULL, "plugin clash");
177
178   /* call gmodule function from A  */
179
180   if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
181     g_error ("error: %s", g_module_error ());
182   test_states (NULL, NULL, NULL);
183
184   gmod_f (module_b);
185   test_states (NULL, NULL, "BOOH");
186  
187   gmod_f (module_a);
188   test_states (NULL, "BOOH", NULL);
189
190   /* unload plugins  */
191
192   if (!g_module_close (module_a))
193     g_error ("error: %s", g_module_error ());
194
195   if (!g_module_close (module_b))
196     g_error ("error: %s", g_module_error ());
197
198   g_free (plugin_a);
199   g_free (plugin_b);
200   g_module_close (module_self);
201   return 0;
202 }