Merge remote branch 'gvdb/master'
[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->out_args != NULL);
80   g_assert (method_info->out_args[0] != NULL);
81   g_assert (method_info->out_args[1] == NULL);
82   g_assert_cmpstr (method_info->out_args[0]->signature, ==, "s");
83
84   interface_info = g_dbus_node_info_lookup_interface (node_info, "com.example.Frob");
85   g_assert (interface_info != NULL);
86   signal_info = g_dbus_interface_info_lookup_signal (interface_info, "TestSignal");
87   g_assert (signal_info != NULL);
88   g_assert (signal_info->args != NULL);
89   g_assert (signal_info->args[0] != NULL);
90   g_assert_cmpstr (signal_info->args[0]->signature, ==, "s");
91   g_assert (signal_info->args[1] != NULL);
92   g_assert_cmpstr (signal_info->args[1]->signature, ==, "o");
93   g_assert (signal_info->args[2] != NULL);
94   g_assert_cmpstr (signal_info->args[2]->signature, ==, "v");
95   g_assert (signal_info->args[3] == NULL);
96
97   g_dbus_node_info_unref (node_info);
98   g_variant_unref (result);
99
100   g_main_loop_quit (loop);
101 }
102
103 static void
104 test_introspection_parser (void)
105 {
106   GDBusProxy *proxy;
107   GDBusConnection *connection;
108   GError *error;
109
110   session_bus_up ();
111
112   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
113    * until one can connect to the bus but that's not how things work right now
114    */
115   usleep (500 * 1000);
116
117   error = NULL;
118   connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
119                                NULL,
120                                &error);
121   g_assert_no_error (error);
122   error = NULL;
123   proxy = g_dbus_proxy_new_sync (connection,
124                                  G_DBUS_PROXY_FLAGS_NONE,
125                                  NULL,                      /* GDBusInterfaceInfo */
126                                  "com.example.TestService", /* name */
127                                  "/com/example/TestObject", /* object path */
128                                  "com.example.Frob",        /* interface */
129                                  NULL, /* GCancellable */
130                                  &error);
131   g_assert_no_error (error);
132
133   /* this is safe; testserver will exit once the bus goes away */
134   g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
135
136   _g_assert_property_notify (proxy, "g-name-owner");
137
138   test_introspection (proxy);
139
140   g_object_unref (proxy);
141   g_object_unref (connection);
142 }
143
144 /* check that a parse-generate roundtrip produces identical results
145  */
146 static void
147 test_generate (void)
148 {
149   GDBusNodeInfo *info;
150   GDBusNodeInfo *info2;
151   GDBusInterfaceInfo *iinfo;
152   GDBusMethodInfo *minfo;
153   GDBusSignalInfo *sinfo;
154   GDBusArgInfo *arginfo;
155   GDBusPropertyInfo *pinfo;
156   GDBusAnnotationInfo *aninfo;
157   const gchar *data =
158   "  <node>"
159   "    <interface name='com.example.Frob'>"
160   "      <annotation name='foo' value='bar'/>"
161   "      <method name='PairReturn'>"
162   "        <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
163   "        <arg type='u' name='somenumber' direction='in'/>"
164   "        <arg type='s' name='somestring' direction='out'/>"
165   "      </method>"
166   "      <signal name='HelloWorld'>"
167   "        <arg type='s' name='greeting' direction='out'/>"
168   "      </signal>"
169   "      <method name='Sleep'>"
170   "        <arg type='i' name='timeout' direction='in'/>"
171   "      </method>"
172   "      <property name='y' type='y' access='readwrite'/>"
173   "    </interface>"
174   "  </node>";
175
176   GString *string;
177   GString *string2;
178   GError *error;
179
180   error = NULL;
181   info = g_dbus_node_info_new_for_xml (data, &error);
182   g_assert_no_error (error);
183
184   iinfo = g_dbus_node_info_lookup_interface (info, "com.example.Frob");
185   aninfo = iinfo->annotations[0];
186   g_assert_cmpstr (aninfo->key, ==, "foo");
187   g_assert_cmpstr (aninfo->value, ==, "bar");
188   g_assert (iinfo->annotations[1] == NULL);
189   minfo = g_dbus_interface_info_lookup_method (iinfo, "PairReturn");
190   g_assert_cmpstr (g_dbus_annotation_info_lookup (minfo->annotations, "org.freedesktop.DBus.GLib.Async"), ==, "");
191   arginfo = minfo->in_args[0];
192   g_assert_cmpstr (arginfo->name, ==, "somenumber");
193   g_assert_cmpstr (arginfo->signature, ==, "u");
194   g_assert (minfo->in_args[1] == NULL);
195   arginfo = minfo->out_args[0];
196   g_assert_cmpstr (arginfo->name, ==, "somestring");
197   g_assert_cmpstr (arginfo->signature, ==, "s");
198   g_assert (minfo->out_args[1] == NULL);
199   sinfo = g_dbus_interface_info_lookup_signal (iinfo, "HelloWorld");
200   arginfo = sinfo->args[0];
201   g_assert_cmpstr (arginfo->name, ==, "greeting");
202   g_assert_cmpstr (arginfo->signature, ==, "s");
203   g_assert (sinfo->args[1] == NULL);
204   pinfo = g_dbus_interface_info_lookup_property (iinfo, "y");
205   g_assert_cmpstr (pinfo->signature, ==, "y");
206   g_assert_cmpint (pinfo->flags, ==, G_DBUS_PROPERTY_INFO_FLAGS_READABLE |
207                                      G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE);
208
209   string = g_string_new ("");
210   g_dbus_node_info_generate_xml (info, 2, string);
211
212   info2 = g_dbus_node_info_new_for_xml (string->str, &error);
213   string2 = g_string_new ("");
214   g_dbus_node_info_generate_xml (info2, 2, string2);
215
216   g_assert_cmpstr (string->str, ==, string2->str);
217   g_string_free (string, TRUE);
218   g_string_free (string2, TRUE);
219
220   g_dbus_node_info_unref (info);
221   g_dbus_node_info_unref (info2);
222 }
223
224 /* test that omitted direction attributes default to 'out' for signals,
225  * and 'in' for methods.
226  */
227 static void
228 test_default_direction (void)
229 {
230   GDBusNodeInfo *info;
231   GDBusInterfaceInfo *iinfo;
232   GDBusMethodInfo *minfo;
233   GDBusSignalInfo *sinfo;
234   GDBusArgInfo *arginfo;
235   const gchar *data =
236   "  <node>"
237   "    <interface name='com.example.Frob'>"
238   "      <signal name='HelloWorld'>"
239   "        <arg type='s' name='greeting'/>"
240   "      </signal>"
241   "      <method name='Sleep'>"
242   "        <arg type='i' name='timeout'/>"
243   "      </method>"
244   "    </interface>"
245   "  </node>";
246
247   GError *error;
248
249   error = NULL;
250   info = g_dbus_node_info_new_for_xml (data, &error);
251   g_assert_no_error (error);
252
253   iinfo = g_dbus_node_info_lookup_interface (info, "com.example.Frob");
254   sinfo = g_dbus_interface_info_lookup_signal (iinfo, "HelloWorld");
255   g_assert (sinfo->args != NULL);
256   arginfo = sinfo->args[0];
257   g_assert_cmpstr (arginfo->name, ==, "greeting");
258   g_assert (sinfo->args[1] == NULL);
259   minfo = g_dbus_interface_info_lookup_method (iinfo, "Sleep");
260   g_assert (minfo->in_args != NULL);
261   arginfo = minfo->in_args[0];
262   g_assert_cmpstr (arginfo->name, ==, "timeout");
263   g_assert (minfo->in_args[1] == NULL);
264
265   g_dbus_node_info_unref (info);
266 }
267
268 #if 0
269 /* XXX: need to figure out how generous we want to be here */
270 /* test that extraneous attributes are ignored
271  */
272 static void
273 test_extra_data (void)
274 {
275   GDBusNodeInfo *info;
276   const gchar *data =
277   "  <node>"
278   "    <interface name='com.example.Frob' version='1.0'>"
279   "      <annotation name='foo' value='bar' extra='bla'/>"
280   "      <method name='PairReturn' anotherattribute='bla'>"
281   "        <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
282   "        <arg type='u' name='somenumber' direction='in' spin='left'/>"
283   "        <arg type='s' name='somestring' direction='out'/>"
284   "      </method>"
285   "      <signal name='HelloWorld'>"
286   "        <arg type='s' name='somestring'/>"
287   "      </signal>"
288   "      <method name='Sleep'>"
289   "        <arg type='i' name='timeout' direction='in'/>"
290   "      </method>"
291   "      <property name='y' type='y' access='readwrite'/>"
292   "    </interface>"
293   "  </node>";
294   GError *error;
295
296   error = NULL;
297   info = g_dbus_node_info_new_for_xml (data, &error);
298   g_assert_no_error (error);
299
300   g_dbus_node_info_unref (info);
301 }
302 #endif
303
304 /* ---------------------------------------------------------------------------------------------------- */
305
306 int
307 main (int   argc,
308       char *argv[])
309 {
310   g_type_init ();
311   g_test_init (&argc, &argv, NULL);
312
313   /* all the tests rely on a shared main loop */
314   loop = g_main_loop_new (NULL, FALSE);
315
316   /* all the tests use a session bus with a well-known address that we can bring up and down
317    * using session_bus_up() and session_bus_down().
318    */
319   g_unsetenv ("DISPLAY");
320   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
321
322   g_test_add_func ("/gdbus/introspection-parser", test_introspection_parser);
323   g_test_add_func ("/gdbus/introspection-generate", test_generate);
324   g_test_add_func ("/gdbus/introspection-default-direction", test_default_direction);
325 #if 0
326   /* XXX: need to figure out how generous we want to be here */
327   g_test_add_func ("/gdbus/introspection-extra-data", test_extra_data);
328 #endif
329
330   return g_test_run();
331 }