Fix memory leaks in the gdbus introspection parser
[platform/upstream/glib.git] / gio / tests / gdbus-introspection.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include "gdbus-tests.h"
28
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
31
32 /* ---------------------------------------------------------------------------------------------------- */
33 /* Test introspection parser */
34 /* ---------------------------------------------------------------------------------------------------- */
35
36 static void
37 test_introspection (GDBusProxy *proxy)
38 {
39   GError *error;
40   const gchar *xml_data;
41   GDBusNodeInfo *node_info;
42   GDBusInterfaceInfo *interface_info;
43   GDBusMethodInfo *method_info;
44   GDBusSignalInfo *signal_info;
45   GVariant *result;
46
47   error = NULL;
48
49   /*
50    * Invoke Introspect(), then parse the output.
51    */
52   result = g_dbus_proxy_call_sync (proxy,
53                                    "org.freedesktop.DBus.Introspectable.Introspect",
54                                    NULL,
55                                    G_DBUS_CALL_FLAGS_NONE,
56                                    -1,
57                                    NULL,
58                                    &error);
59   g_assert_no_error (error);
60   g_assert (result != NULL);
61   g_variant_get (result, "(&s)", &xml_data);
62
63   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
64   g_assert_no_error (error);
65   g_assert (node_info != NULL);
66
67   /* for now we only check a couple of things. TODO: check more things */
68
69   interface_info = g_dbus_node_info_lookup_interface (node_info, "com.example.NonExistantInterface");
70   g_assert (interface_info == NULL);
71
72   interface_info = g_dbus_node_info_lookup_interface (node_info, "org.freedesktop.DBus.Introspectable");
73   g_assert (interface_info != NULL);
74   method_info = g_dbus_interface_info_lookup_method (interface_info, "NonExistantMethod");
75   g_assert (method_info == NULL);
76   method_info = g_dbus_interface_info_lookup_method (interface_info, "Introspect");
77   g_assert (method_info != NULL);
78   g_assert (method_info->in_args != NULL);
79   g_assert (method_info->in_args[0] == NULL);
80   g_assert (method_info->out_args != NULL);
81   g_assert (method_info->out_args[0] != NULL);
82   g_assert (method_info->out_args[1] == NULL);
83   g_assert_cmpstr (method_info->out_args[0]->signature, ==, "s");
84
85   interface_info = g_dbus_node_info_lookup_interface (node_info, "com.example.Frob");
86   g_assert (interface_info != NULL);
87   signal_info = g_dbus_interface_info_lookup_signal (interface_info, "TestSignal");
88   g_assert (signal_info != NULL);
89   g_assert (signal_info->args != NULL);
90   g_assert (signal_info->args[0] != NULL);
91   g_assert_cmpstr (signal_info->args[0]->signature, ==, "s");
92   g_assert (signal_info->args[1] != NULL);
93   g_assert_cmpstr (signal_info->args[1]->signature, ==, "o");
94   g_assert (signal_info->args[2] != NULL);
95   g_assert_cmpstr (signal_info->args[2]->signature, ==, "v");
96   g_assert (signal_info->args[3] == NULL);
97
98   g_dbus_node_info_unref (node_info);
99   g_variant_unref (result);
100
101   g_main_loop_quit (loop);
102 }
103
104 static void
105 test_introspection_parser (void)
106 {
107   GDBusProxy *proxy;
108   GDBusConnection *connection;
109   GError *error;
110
111   session_bus_up ();
112
113   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
114    * until one can connect to the bus but that's not how things work right now
115    */
116   usleep (500 * 1000);
117
118   error = NULL;
119   connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
120                                NULL,
121                                &error);
122   g_assert_no_error (error);
123   error = NULL;
124   proxy = g_dbus_proxy_new_sync (connection,
125                                  G_DBUS_PROXY_FLAGS_NONE,
126                                  NULL,                      /* GDBusInterfaceInfo */
127                                  "com.example.TestService", /* name */
128                                  "/com/example/TestObject", /* object path */
129                                  "com.example.Frob",        /* interface */
130                                  NULL, /* GCancellable */
131                                  &error);
132   g_assert_no_error (error);
133
134   /* this is safe; testserver will exit once the bus goes away */
135   g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
136
137   _g_assert_property_notify (proxy, "g-name-owner");
138
139   test_introspection (proxy);
140
141   g_object_unref (proxy);
142   g_object_unref (connection);
143 }
144
145 /* check that a parse-generate roundtrip produces identical results
146  */
147 static void
148 test_generate (void)
149 {
150   GDBusNodeInfo *info;
151   GDBusNodeInfo *info2;
152   GDBusInterfaceInfo *iinfo;
153   GDBusMethodInfo *minfo;
154   GDBusSignalInfo *sinfo;
155   GDBusArgInfo *arginfo;
156   GDBusPropertyInfo *pinfo;
157   GDBusAnnotationInfo *aninfo;
158   const gchar *data =
159   "  <node>"
160   "    <interface name='com.example.Frob'>"
161   "      <annotation name='foo' value='bar'/>"
162   "      <method name='PairReturn'>"
163   "        <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
164   "        <arg type='u' name='somenumber' direction='in'/>"
165   "        <arg type='s' name='somestring' direction='out'/>"
166   "      </method>"
167   "      <signal name='HelloWorld'>"
168   "        <arg type='s' name='greeting' direction='out'/>"
169   "      </signal>"
170   "      <method name='Sleep'>"
171   "        <arg type='i' name='timeout' direction='in'/>"
172   "      </method>"
173   "      <property name='y' type='y' access='readwrite'/>"
174   "    </interface>"
175   "  </node>";
176
177   GString *string;
178   GString *string2;
179   GError *error;
180
181   error = NULL;
182   info = g_dbus_node_info_new_for_xml (data, &error);
183   g_assert_no_error (error);
184
185   iinfo = g_dbus_node_info_lookup_interface (info, "com.example.Frob");
186   aninfo = iinfo->annotations[0];
187   g_assert_cmpstr (aninfo->key, ==, "foo");
188   g_assert_cmpstr (aninfo->value, ==, "bar");
189   g_assert (iinfo->annotations[1] == NULL);
190   minfo = g_dbus_interface_info_lookup_method (iinfo, "PairReturn");
191   g_assert_cmpstr (g_dbus_annotation_info_lookup (minfo->annotations, "org.freedesktop.DBus.GLib.Async"), ==, "");
192   arginfo = minfo->in_args[0];
193   g_assert_cmpstr (arginfo->name, ==, "somenumber");
194   g_assert_cmpstr (arginfo->signature, ==, "u");
195   g_assert (minfo->in_args[1] == NULL);
196   arginfo = minfo->out_args[0];
197   g_assert_cmpstr (arginfo->name, ==, "somestring");
198   g_assert_cmpstr (arginfo->signature, ==, "s");
199   g_assert (minfo->out_args[1] == NULL);
200   sinfo = g_dbus_interface_info_lookup_signal (iinfo, "HelloWorld");
201   arginfo = sinfo->args[0];
202   g_assert_cmpstr (arginfo->name, ==, "greeting");
203   g_assert_cmpstr (arginfo->signature, ==, "s");
204   g_assert (sinfo->args[1] == NULL);
205   pinfo = g_dbus_interface_info_lookup_property (iinfo, "y");
206   g_assert_cmpstr (pinfo->signature, ==, "y");
207   g_assert_cmpint (pinfo->flags, ==, G_DBUS_PROPERTY_INFO_FLAGS_READABLE |
208                                      G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE);
209
210   string = g_string_new ("");
211   g_dbus_node_info_generate_xml (info, 2, string);
212
213   info2 = g_dbus_node_info_new_for_xml (string->str, &error);
214   string2 = g_string_new ("");
215   g_dbus_node_info_generate_xml (info2, 2, string2);
216
217   g_assert_cmpstr (string->str, ==, string2->str);
218   g_string_free (string, TRUE);
219   g_string_free (string2, TRUE);
220
221   g_dbus_node_info_unref (info);
222   g_dbus_node_info_unref (info2);
223 }
224
225 /* test that omitted direction attributes default to 'out' for signals,
226  * and 'in' for methods.
227  */
228 static void
229 test_default_direction (void)
230 {
231   GDBusNodeInfo *info;
232   GDBusInterfaceInfo *iinfo;
233   GDBusMethodInfo *minfo;
234   GDBusSignalInfo *sinfo;
235   GDBusArgInfo *arginfo;
236   const gchar *data =
237   "  <node>"
238   "    <interface name='com.example.Frob'>"
239   "      <signal name='HelloWorld'>"
240   "        <arg type='s' name='greeting'/>"
241   "      </signal>"
242   "      <method name='Sleep'>"
243   "        <arg type='i' name='timeout'/>"
244   "      </method>"
245   "    </interface>"
246   "  </node>";
247
248   GError *error;
249
250   error = NULL;
251   info = g_dbus_node_info_new_for_xml (data, &error);
252   g_assert_no_error (error);
253
254   iinfo = g_dbus_node_info_lookup_interface (info, "com.example.Frob");
255   sinfo = g_dbus_interface_info_lookup_signal (iinfo, "HelloWorld");
256   g_assert (sinfo->args != NULL);
257   arginfo = sinfo->args[0];
258   g_assert_cmpstr (arginfo->name, ==, "greeting");
259   g_assert (sinfo->args[1] == NULL);
260   minfo = g_dbus_interface_info_lookup_method (iinfo, "Sleep");
261   g_assert (minfo->in_args != NULL);
262   arginfo = minfo->in_args[0];
263   g_assert_cmpstr (arginfo->name, ==, "timeout");
264   g_assert (minfo->in_args[1] == NULL);
265
266   g_dbus_node_info_unref (info);
267 }
268
269 #if 0
270 /* XXX: need to figure out how generous we want to be here */
271 /* test that extraneous attributes are ignored
272  */
273 static void
274 test_extra_data (void)
275 {
276   GDBusNodeInfo *info;
277   const gchar *data =
278   "  <node>"
279   "    <interface name='com.example.Frob' version='1.0'>"
280   "      <annotation name='foo' value='bar' extra='bla'/>"
281   "      <method name='PairReturn' anotherattribute='bla'>"
282   "        <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
283   "        <arg type='u' name='somenumber' direction='in' spin='left'/>"
284   "        <arg type='s' name='somestring' direction='out'/>"
285   "      </method>"
286   "      <signal name='HelloWorld'>"
287   "        <arg type='s' name='somestring'/>"
288   "      </signal>"
289   "      <method name='Sleep'>"
290   "        <arg type='i' name='timeout' direction='in'/>"
291   "      </method>"
292   "      <property name='y' type='y' access='readwrite'/>"
293   "    </interface>"
294   "  </node>";
295   GError *error;
296
297   error = NULL;
298   info = g_dbus_node_info_new_for_xml (data, &error);
299   g_assert_no_error (error);
300
301   g_dbus_node_info_unref (info);
302 }
303 #endif
304
305 /* ---------------------------------------------------------------------------------------------------- */
306
307 int
308 main (int   argc,
309       char *argv[])
310 {
311   g_type_init ();
312   g_test_init (&argc, &argv, NULL);
313
314   /* all the tests rely on a shared main loop */
315   loop = g_main_loop_new (NULL, FALSE);
316
317   /* all the tests use a session bus with a well-known address that we can bring up and down
318    * using session_bus_up() and session_bus_down().
319    */
320   g_unsetenv ("DISPLAY");
321   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
322
323   g_test_add_func ("/gdbus/introspection-parser", test_introspection_parser);
324   g_test_add_func ("/gdbus/introspection-generate", test_generate);
325   g_test_add_func ("/gdbus/introspection-default-direction", test_default_direction);
326 #if 0
327   /* XXX: need to figure out how generous we want to be here */
328   g_test_add_func ("/gdbus/introspection-extra-data", test_extra_data);
329 #endif
330
331   return g_test_run();
332 }