[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #undef G_DISABLE_ASSERT
26 #undef G_LOG_DOMAIN
27
28 #include <gmodule.h>
29 #include <string.h>
30
31 gchar* global_state;
32
33 G_MODULE_EXPORT void g_clash_func (void);
34
35 G_MODULE_EXPORT void
36 g_clash_func (void)
37 {
38   global_state = "global clash";
39 }
40
41 typedef void (*SimpleFunc) (void);
42 typedef void (*GModuleFunc) (GModule *);
43
44 static gchar **gplugin_a_state;
45 static gchar **gplugin_b_state;
46
47 static void
48 compare (const gchar *desc, const gchar *expected, const gchar *found)
49 {
50   if (!expected && !found)
51     return;
52   
53   if (expected && found && strcmp (expected, found) == 0)
54     return;
55     
56   g_error ("error: %s state should have been \"%s\", but is \"%s\"",
57            desc, expected ? expected : "NULL", found ? found : "NULL");
58 }
59
60 static void 
61 test_states (const gchar *global, const gchar *gplugin_a, 
62              const gchar *gplugin_b)
63 {       
64   compare ("global", global, global_state);
65   compare ("Plugin A", gplugin_a, *gplugin_a_state);
66   compare ("Plugin B", gplugin_b, *gplugin_b_state);
67   
68   global_state = *gplugin_a_state = *gplugin_b_state = NULL;
69 }
70  
71 static SimpleFunc plugin_clash_func = NULL;
72
73 int
74 main (int    argc,
75       char **argv)
76 {
77   GModule *module_self, *module_a, *module_b;
78   gchar *plugin_a, *plugin_b;
79   SimpleFunc f_a, f_b, f_self;
80   GModuleFunc gmod_f;
81
82   g_test_init (&argc, &argv, NULL);
83
84   if (!g_module_supported ())
85     g_error ("dynamic modules not supported");
86
87   plugin_a = g_test_build_filename (G_TEST_BUILT, "libmoduletestplugin_a", NULL);
88   plugin_b = g_test_build_filename (G_TEST_BUILT, "libmoduletestplugin_b", NULL);
89
90   /* module handles */
91   
92   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
93   if (!module_self)
94     g_error ("error: %s", g_module_error ());
95
96   if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
97     g_error ("error: %s", g_module_error ());
98
99   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
100   if (!module_a)
101     g_error ("error: %s", g_module_error ());
102
103   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
104   if (!module_b)
105     g_error ("error: %s", g_module_error ());
106
107   /* get plugin state vars */
108
109   if (!g_module_symbol (module_a, "gplugin_a_state", 
110                         (gpointer *) &gplugin_a_state))
111     g_error ("error: %s", g_module_error ());
112   
113   if (!g_module_symbol (module_b, "gplugin_b_state", 
114                         (gpointer *) &gplugin_b_state))
115     g_error ("error: %s", g_module_error ());
116   test_states (NULL, NULL, "check-init");
117   
118   /* get plugin specific symbols and call them
119    */
120   if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
121     g_error ("error: %s", g_module_error ());
122   test_states (NULL, NULL, NULL);
123  
124   if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
125     g_error ("error: %s", g_module_error ());
126   test_states (NULL, NULL, NULL);
127  
128   f_a ();
129   test_states (NULL, "Hello world", NULL);
130   
131   f_b ();
132   test_states (NULL, NULL, "Hello world");
133   
134   /* get and call globally clashing functions
135    */
136  
137   if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
138     g_error ("error: %s", g_module_error ());
139   test_states (NULL, NULL, NULL);
140
141   if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
142     g_error ("error: %s", g_module_error ());
143   test_states (NULL, NULL, NULL);
144  
145   if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
146     g_error ("error: %s", g_module_error ());
147   test_states (NULL, NULL, NULL);
148  
149   f_self ();
150   test_states ("global clash", NULL, NULL);
151   
152   f_a ();
153   test_states (NULL, "global clash", NULL);
154
155   f_b ();
156   test_states (NULL, NULL, "global clash");
157
158   /* get and call clashing plugin functions  */
159
160   if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
161     g_error ("error: %s", g_module_error ());
162   test_states (NULL, NULL, NULL);
163
164   if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
165     g_error ("error: %s", g_module_error ());
166   test_states (NULL, NULL, NULL);
167
168   plugin_clash_func = f_a;
169   plugin_clash_func ();
170   test_states (NULL, "plugin clash", NULL);
171
172   plugin_clash_func = f_b;
173   plugin_clash_func ();
174   test_states (NULL, NULL, "plugin clash");
175
176   /* call gmodule function from A  */
177
178   if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
179     g_error ("error: %s", g_module_error ());
180   test_states (NULL, NULL, NULL);
181
182   gmod_f (module_b);
183   test_states (NULL, NULL, "BOOH");
184  
185   gmod_f (module_a);
186   test_states (NULL, "BOOH", NULL);
187
188   /* unload plugins  */
189
190   if (!g_module_close (module_a))
191     g_error ("error: %s", g_module_error ());
192
193   if (!g_module_close (module_b))
194     g_error ("error: %s", g_module_error ());
195
196   g_free (plugin_a);
197   g_free (plugin_b);
198   g_module_close (module_self);
199   return 0;
200 }