1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
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.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include "gdbus-tests.h"
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
32 /* ---------------------------------------------------------------------------------------------------- */
33 /* Test introspection parser */
34 /* ---------------------------------------------------------------------------------------------------- */
37 test_introspection (GDBusProxy *proxy)
40 const gchar *xml_data;
41 GDBusNodeInfo *node_info;
42 GDBusInterfaceInfo *interface_info;
43 GDBusMethodInfo *method_info;
44 GDBusSignalInfo *signal_info;
50 * Invoke Introspect(), then parse the output.
52 result = g_dbus_proxy_call_sync (proxy,
53 "org.freedesktop.DBus.Introspectable.Introspect",
55 G_DBUS_CALL_FLAGS_NONE,
59 g_assert_no_error (error);
60 g_assert (result != NULL);
61 g_variant_get (result, "(&s)", &xml_data);
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);
67 /* for now we only check a couple of things. TODO: check more things */
69 interface_info = g_dbus_node_info_lookup_interface (node_info, "com.example.NonExistantInterface");
70 g_assert (interface_info == NULL);
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");
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);
98 g_dbus_node_info_unref (node_info);
99 g_variant_unref (result);
101 g_main_loop_quit (loop);
105 test_introspection_parser (void)
108 GDBusConnection *connection;
113 connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
116 g_assert_no_error (error);
118 proxy = g_dbus_proxy_new_sync (connection,
119 G_DBUS_PROXY_FLAGS_NONE,
120 NULL, /* GDBusInterfaceInfo */
121 "com.example.TestService", /* name */
122 "/com/example/TestObject", /* object path */
123 "com.example.Frob", /* interface */
124 NULL, /* GCancellable */
126 g_assert_no_error (error);
128 /* this is safe; testserver will exit once the bus goes away */
129 path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
130 g_assert (g_spawn_command_line_async (path, NULL));
133 _g_assert_property_notify (proxy, "g-name-owner");
135 test_introspection (proxy);
137 g_object_unref (proxy);
138 g_object_unref (connection);
141 /* check that a parse-generate roundtrip produces identical results
147 GDBusNodeInfo *info2;
148 GDBusInterfaceInfo *iinfo;
149 GDBusMethodInfo *minfo;
150 GDBusSignalInfo *sinfo;
151 GDBusArgInfo *arginfo;
152 GDBusPropertyInfo *pinfo;
153 GDBusAnnotationInfo *aninfo;
156 " <interface name='com.example.Frob'>"
157 " <annotation name='foo' value='bar'/>"
158 " <method name='PairReturn'>"
159 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
160 " <arg type='u' name='somenumber' direction='in'/>"
161 " <arg type='s' name='somestring' direction='out'/>"
163 " <signal name='HelloWorld'>"
164 " <arg type='s' name='greeting' direction='out'/>"
166 " <method name='Sleep'>"
167 " <arg type='i' name='timeout' direction='in'/>"
169 " <property name='y' type='y' access='readwrite'/>"
178 info = g_dbus_node_info_new_for_xml (data, &error);
179 g_assert_no_error (error);
181 iinfo = g_dbus_node_info_lookup_interface (info, "com.example.Frob");
182 aninfo = iinfo->annotations[0];
183 g_assert_cmpstr (aninfo->key, ==, "foo");
184 g_assert_cmpstr (aninfo->value, ==, "bar");
185 g_assert (iinfo->annotations[1] == NULL);
186 minfo = g_dbus_interface_info_lookup_method (iinfo, "PairReturn");
187 g_assert_cmpstr (g_dbus_annotation_info_lookup (minfo->annotations, "org.freedesktop.DBus.GLib.Async"), ==, "");
188 arginfo = minfo->in_args[0];
189 g_assert_cmpstr (arginfo->name, ==, "somenumber");
190 g_assert_cmpstr (arginfo->signature, ==, "u");
191 g_assert (minfo->in_args[1] == NULL);
192 arginfo = minfo->out_args[0];
193 g_assert_cmpstr (arginfo->name, ==, "somestring");
194 g_assert_cmpstr (arginfo->signature, ==, "s");
195 g_assert (minfo->out_args[1] == NULL);
196 sinfo = g_dbus_interface_info_lookup_signal (iinfo, "HelloWorld");
197 arginfo = sinfo->args[0];
198 g_assert_cmpstr (arginfo->name, ==, "greeting");
199 g_assert_cmpstr (arginfo->signature, ==, "s");
200 g_assert (sinfo->args[1] == NULL);
201 pinfo = g_dbus_interface_info_lookup_property (iinfo, "y");
202 g_assert_cmpstr (pinfo->signature, ==, "y");
203 g_assert_cmpint (pinfo->flags, ==, G_DBUS_PROPERTY_INFO_FLAGS_READABLE |
204 G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE);
206 string = g_string_new ("");
207 g_dbus_node_info_generate_xml (info, 2, string);
209 info2 = g_dbus_node_info_new_for_xml (string->str, &error);
210 string2 = g_string_new ("");
211 g_dbus_node_info_generate_xml (info2, 2, string2);
213 g_assert_cmpstr (string->str, ==, string2->str);
214 g_string_free (string, TRUE);
215 g_string_free (string2, TRUE);
217 g_dbus_node_info_unref (info);
218 g_dbus_node_info_unref (info2);
221 /* test that omitted direction attributes default to 'out' for signals,
222 * and 'in' for methods.
225 test_default_direction (void)
228 GDBusInterfaceInfo *iinfo;
229 GDBusMethodInfo *minfo;
230 GDBusSignalInfo *sinfo;
231 GDBusArgInfo *arginfo;
234 " <interface name='com.example.Frob'>"
235 " <signal name='HelloWorld'>"
236 " <arg type='s' name='greeting'/>"
238 " <method name='Sleep'>"
239 " <arg type='i' name='timeout'/>"
247 info = g_dbus_node_info_new_for_xml (data, &error);
248 g_assert_no_error (error);
250 iinfo = g_dbus_node_info_lookup_interface (info, "com.example.Frob");
251 sinfo = g_dbus_interface_info_lookup_signal (iinfo, "HelloWorld");
252 g_assert (sinfo->args != NULL);
253 arginfo = sinfo->args[0];
254 g_assert_cmpstr (arginfo->name, ==, "greeting");
255 g_assert (sinfo->args[1] == NULL);
256 minfo = g_dbus_interface_info_lookup_method (iinfo, "Sleep");
257 g_assert (minfo->in_args != NULL);
258 arginfo = minfo->in_args[0];
259 g_assert_cmpstr (arginfo->name, ==, "timeout");
260 g_assert (minfo->in_args[1] == NULL);
262 g_dbus_node_info_unref (info);
266 /* XXX: need to figure out how generous we want to be here */
267 /* test that extraneous attributes are ignored
270 test_extra_data (void)
275 " <interface name='com.example.Frob' version='1.0'>"
276 " <annotation name='foo' value='bar' extra='bla'/>"
277 " <method name='PairReturn' anotherattribute='bla'>"
278 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
279 " <arg type='u' name='somenumber' direction='in' spin='left'/>"
280 " <arg type='s' name='somestring' direction='out'/>"
282 " <signal name='HelloWorld'>"
283 " <arg type='s' name='somestring'/>"
285 " <method name='Sleep'>"
286 " <arg type='i' name='timeout' direction='in'/>"
288 " <property name='y' type='y' access='readwrite'/>"
294 info = g_dbus_node_info_new_for_xml (data, &error);
295 g_assert_no_error (error);
297 g_dbus_node_info_unref (info);
301 /* ---------------------------------------------------------------------------------------------------- */
309 g_test_init (&argc, &argv, NULL);
311 /* all the tests rely on a shared main loop */
312 loop = g_main_loop_new (NULL, FALSE);
316 g_test_add_func ("/gdbus/introspection-parser", test_introspection_parser);
317 g_test_add_func ("/gdbus/introspection-generate", test_generate);
318 g_test_add_func ("/gdbus/introspection-default-direction", test_default_direction);
320 /* XXX: need to figure out how generous we want to be here */
321 g_test_add_func ("/gdbus/introspection-extra-data", test_extra_data);