2010-05-06 20:13:59 +02:00
|
|
|
/* GLib testing framework examples and tests
|
|
|
|
*
|
2010-05-10 14:07:28 +02:00
|
|
|
* Copyright (C) 2008-2010 Red Hat, Inc.
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
|
|
|
* Author: David Zeuthen <davidz@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gio/gio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gdbus-tests.h"
|
|
|
|
|
|
|
|
/* all tests rely on a shared mainloop */
|
|
|
|
static GMainLoop *loop = NULL;
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
/* Test introspection parser */
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2010-06-11 21:45:18 +02:00
|
|
|
test_introspection (GDBusProxy *proxy)
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
|
|
|
GError *error;
|
|
|
|
const gchar *xml_data;
|
|
|
|
GDBusNodeInfo *node_info;
|
2010-07-19 21:45:27 +02:00
|
|
|
GDBusInterfaceInfo *interface_info;
|
|
|
|
GDBusMethodInfo *method_info;
|
|
|
|
GDBusSignalInfo *signal_info;
|
2010-05-06 20:13:59 +02:00
|
|
|
GVariant *result;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invoke Introspect(), then parse the output.
|
|
|
|
*/
|
2010-05-10 17:47:08 +02:00
|
|
|
result = g_dbus_proxy_call_sync (proxy,
|
|
|
|
"org.freedesktop.DBus.Introspectable.Introspect",
|
|
|
|
NULL,
|
|
|
|
G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
-1,
|
|
|
|
NULL,
|
|
|
|
&error);
|
2010-05-06 20:13:59 +02:00
|
|
|
g_assert_no_error (error);
|
|
|
|
g_assert (result != NULL);
|
2010-05-14 20:07:15 +02:00
|
|
|
g_variant_get (result, "(&s)", &xml_data);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
g_assert (node_info != NULL);
|
|
|
|
|
|
|
|
/* for now we only check a couple of things. TODO: check more things */
|
|
|
|
|
|
|
|
interface_info = g_dbus_node_info_lookup_interface (node_info, "com.example.NonExistantInterface");
|
|
|
|
g_assert (interface_info == NULL);
|
|
|
|
|
|
|
|
interface_info = g_dbus_node_info_lookup_interface (node_info, "org.freedesktop.DBus.Introspectable");
|
|
|
|
g_assert (interface_info != NULL);
|
|
|
|
method_info = g_dbus_interface_info_lookup_method (interface_info, "NonExistantMethod");
|
|
|
|
g_assert (method_info == NULL);
|
|
|
|
method_info = g_dbus_interface_info_lookup_method (interface_info, "Introspect");
|
|
|
|
g_assert (method_info != NULL);
|
2011-02-15 08:27:54 +01:00
|
|
|
g_assert (method_info->in_args != NULL);
|
|
|
|
g_assert (method_info->in_args[0] == NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
g_assert (method_info->out_args != NULL);
|
|
|
|
g_assert (method_info->out_args[0] != NULL);
|
|
|
|
g_assert (method_info->out_args[1] == NULL);
|
|
|
|
g_assert_cmpstr (method_info->out_args[0]->signature, ==, "s");
|
|
|
|
|
|
|
|
interface_info = g_dbus_node_info_lookup_interface (node_info, "com.example.Frob");
|
|
|
|
g_assert (interface_info != NULL);
|
|
|
|
signal_info = g_dbus_interface_info_lookup_signal (interface_info, "TestSignal");
|
|
|
|
g_assert (signal_info != NULL);
|
|
|
|
g_assert (signal_info->args != NULL);
|
|
|
|
g_assert (signal_info->args[0] != NULL);
|
|
|
|
g_assert_cmpstr (signal_info->args[0]->signature, ==, "s");
|
|
|
|
g_assert (signal_info->args[1] != NULL);
|
|
|
|
g_assert_cmpstr (signal_info->args[1]->signature, ==, "o");
|
|
|
|
g_assert (signal_info->args[2] != NULL);
|
|
|
|
g_assert_cmpstr (signal_info->args[2]->signature, ==, "v");
|
|
|
|
g_assert (signal_info->args[3] == NULL);
|
|
|
|
|
|
|
|
g_dbus_node_info_unref (node_info);
|
|
|
|
g_variant_unref (result);
|
|
|
|
|
|
|
|
g_main_loop_quit (loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_introspection_parser (void)
|
|
|
|
{
|
2010-06-11 21:45:18 +02:00
|
|
|
GDBusProxy *proxy;
|
|
|
|
GDBusConnection *connection;
|
|
|
|
GError *error;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-06-11 21:45:18 +02:00
|
|
|
error = NULL;
|
|
|
|
connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
|
|
|
|
NULL,
|
|
|
|
&error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
error = NULL;
|
|
|
|
proxy = g_dbus_proxy_new_sync (connection,
|
|
|
|
G_DBUS_PROXY_FLAGS_NONE,
|
|
|
|
NULL, /* GDBusInterfaceInfo */
|
|
|
|
"com.example.TestService", /* name */
|
|
|
|
"/com/example/TestObject", /* object path */
|
|
|
|
"com.example.Frob", /* interface */
|
|
|
|
NULL, /* GCancellable */
|
|
|
|
&error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
/* this is safe; testserver will exit once the bus goes away */
|
2013-05-28 20:24:47 +02:00
|
|
|
g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-06-11 21:45:18 +02:00
|
|
|
_g_assert_property_notify (proxy, "g-name-owner");
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-06-11 21:45:18 +02:00
|
|
|
test_introspection (proxy);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-06-11 21:45:18 +02:00
|
|
|
g_object_unref (proxy);
|
|
|
|
g_object_unref (connection);
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
2010-08-30 14:49:41 +02:00
|
|
|
/* check that a parse-generate roundtrip produces identical results
|
|
|
|
*/
|
2010-08-23 06:36:36 +02:00
|
|
|
static void
|
|
|
|
test_generate (void)
|
|
|
|
{
|
|
|
|
GDBusNodeInfo *info;
|
|
|
|
GDBusNodeInfo *info2;
|
|
|
|
GDBusInterfaceInfo *iinfo;
|
|
|
|
GDBusMethodInfo *minfo;
|
|
|
|
GDBusSignalInfo *sinfo;
|
|
|
|
GDBusArgInfo *arginfo;
|
|
|
|
GDBusPropertyInfo *pinfo;
|
|
|
|
GDBusAnnotationInfo *aninfo;
|
|
|
|
const gchar *data =
|
|
|
|
" <node>"
|
|
|
|
" <interface name='com.example.Frob'>"
|
|
|
|
" <annotation name='foo' value='bar'/>"
|
|
|
|
" <method name='PairReturn'>"
|
|
|
|
" <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
|
|
|
|
" <arg type='u' name='somenumber' direction='in'/>"
|
|
|
|
" <arg type='s' name='somestring' direction='out'/>"
|
|
|
|
" </method>"
|
|
|
|
" <signal name='HelloWorld'>"
|
2010-08-30 14:49:41 +02:00
|
|
|
" <arg type='s' name='greeting' direction='out'/>"
|
2010-08-23 06:36:36 +02:00
|
|
|
" </signal>"
|
|
|
|
" <method name='Sleep'>"
|
|
|
|
" <arg type='i' name='timeout' direction='in'/>"
|
|
|
|
" </method>"
|
2014-01-08 17:01:55 +01:00
|
|
|
" <property name='y' type='y' access='readwrite'>"
|
|
|
|
" <annotation name='needs-escaping' value='bar<>'"'/>"
|
|
|
|
" </property>"
|
2010-08-23 06:36:36 +02:00
|
|
|
" </interface>"
|
|
|
|
" </node>";
|
|
|
|
|
|
|
|
GString *string;
|
|
|
|
GString *string2;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
info = g_dbus_node_info_new_for_xml (data, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
iinfo = g_dbus_node_info_lookup_interface (info, "com.example.Frob");
|
|
|
|
aninfo = iinfo->annotations[0];
|
|
|
|
g_assert_cmpstr (aninfo->key, ==, "foo");
|
|
|
|
g_assert_cmpstr (aninfo->value, ==, "bar");
|
|
|
|
g_assert (iinfo->annotations[1] == NULL);
|
|
|
|
minfo = g_dbus_interface_info_lookup_method (iinfo, "PairReturn");
|
|
|
|
g_assert_cmpstr (g_dbus_annotation_info_lookup (minfo->annotations, "org.freedesktop.DBus.GLib.Async"), ==, "");
|
|
|
|
arginfo = minfo->in_args[0];
|
|
|
|
g_assert_cmpstr (arginfo->name, ==, "somenumber");
|
|
|
|
g_assert_cmpstr (arginfo->signature, ==, "u");
|
|
|
|
g_assert (minfo->in_args[1] == NULL);
|
|
|
|
arginfo = minfo->out_args[0];
|
|
|
|
g_assert_cmpstr (arginfo->name, ==, "somestring");
|
|
|
|
g_assert_cmpstr (arginfo->signature, ==, "s");
|
|
|
|
g_assert (minfo->out_args[1] == NULL);
|
|
|
|
sinfo = g_dbus_interface_info_lookup_signal (iinfo, "HelloWorld");
|
2010-08-30 14:49:41 +02:00
|
|
|
arginfo = sinfo->args[0];
|
|
|
|
g_assert_cmpstr (arginfo->name, ==, "greeting");
|
2010-08-23 06:36:36 +02:00
|
|
|
g_assert_cmpstr (arginfo->signature, ==, "s");
|
2010-08-30 14:49:41 +02:00
|
|
|
g_assert (sinfo->args[1] == NULL);
|
2010-08-23 06:36:36 +02:00
|
|
|
pinfo = g_dbus_interface_info_lookup_property (iinfo, "y");
|
|
|
|
g_assert_cmpstr (pinfo->signature, ==, "y");
|
|
|
|
g_assert_cmpint (pinfo->flags, ==, G_DBUS_PROPERTY_INFO_FLAGS_READABLE |
|
|
|
|
G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE);
|
|
|
|
|
|
|
|
string = g_string_new ("");
|
|
|
|
g_dbus_node_info_generate_xml (info, 2, string);
|
|
|
|
|
|
|
|
info2 = g_dbus_node_info_new_for_xml (string->str, &error);
|
|
|
|
string2 = g_string_new ("");
|
|
|
|
g_dbus_node_info_generate_xml (info2, 2, string2);
|
|
|
|
|
|
|
|
g_assert_cmpstr (string->str, ==, string2->str);
|
|
|
|
g_string_free (string, TRUE);
|
|
|
|
g_string_free (string2, TRUE);
|
|
|
|
|
|
|
|
g_dbus_node_info_unref (info);
|
|
|
|
g_dbus_node_info_unref (info2);
|
|
|
|
}
|
|
|
|
|
2010-08-30 14:49:41 +02:00
|
|
|
/* test that omitted direction attributes default to 'out' for signals,
|
|
|
|
* and 'in' for methods.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_default_direction (void)
|
|
|
|
{
|
|
|
|
GDBusNodeInfo *info;
|
|
|
|
GDBusInterfaceInfo *iinfo;
|
|
|
|
GDBusMethodInfo *minfo;
|
|
|
|
GDBusSignalInfo *sinfo;
|
|
|
|
GDBusArgInfo *arginfo;
|
|
|
|
const gchar *data =
|
|
|
|
" <node>"
|
|
|
|
" <interface name='com.example.Frob'>"
|
|
|
|
" <signal name='HelloWorld'>"
|
|
|
|
" <arg type='s' name='greeting'/>"
|
|
|
|
" </signal>"
|
|
|
|
" <method name='Sleep'>"
|
|
|
|
" <arg type='i' name='timeout'/>"
|
|
|
|
" </method>"
|
|
|
|
" </interface>"
|
|
|
|
" </node>";
|
|
|
|
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
info = g_dbus_node_info_new_for_xml (data, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
iinfo = g_dbus_node_info_lookup_interface (info, "com.example.Frob");
|
|
|
|
sinfo = g_dbus_interface_info_lookup_signal (iinfo, "HelloWorld");
|
|
|
|
g_assert (sinfo->args != NULL);
|
|
|
|
arginfo = sinfo->args[0];
|
|
|
|
g_assert_cmpstr (arginfo->name, ==, "greeting");
|
|
|
|
g_assert (sinfo->args[1] == NULL);
|
|
|
|
minfo = g_dbus_interface_info_lookup_method (iinfo, "Sleep");
|
|
|
|
g_assert (minfo->in_args != NULL);
|
|
|
|
arginfo = minfo->in_args[0];
|
|
|
|
g_assert_cmpstr (arginfo->name, ==, "timeout");
|
|
|
|
g_assert (minfo->in_args[1] == NULL);
|
|
|
|
|
|
|
|
g_dbus_node_info_unref (info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_extra_data (void)
|
|
|
|
{
|
|
|
|
GDBusNodeInfo *info;
|
|
|
|
const gchar *data =
|
|
|
|
" <node>"
|
|
|
|
" <interface name='com.example.Frob' version='1.0'>"
|
2013-10-29 14:47:34 +01:00
|
|
|
" <doc:doc><doc:description><doc:para>Blah blah</doc:para></doc:description></doc:doc>"
|
|
|
|
" <method name='DownloadPackages'>"
|
|
|
|
" <arg type='u' name='somenumber' direction='in'>"
|
|
|
|
" <doc:doc><doc:summary><doc:para>"
|
|
|
|
" See <doc:ulink url='http:///example.com'>example</doc:ulink>"
|
|
|
|
" </doc:para></doc:summary></doc:doc>"
|
|
|
|
" </arg>"
|
|
|
|
" <arg type='s' name='somestring' direction='out'>"
|
|
|
|
" <doc:doc><doc:summary><doc:para>"
|
|
|
|
" More docs"
|
|
|
|
" </doc:para></doc:summary></doc:doc>"
|
|
|
|
" </arg>"
|
2010-08-30 14:49:41 +02:00
|
|
|
" </method>"
|
|
|
|
" <signal name='HelloWorld'>"
|
|
|
|
" <arg type='s' name='somestring'/>"
|
|
|
|
" </signal>"
|
|
|
|
" <method name='Sleep'>"
|
|
|
|
" <arg type='i' name='timeout' direction='in'/>"
|
|
|
|
" </method>"
|
|
|
|
" <property name='y' type='y' access='readwrite'/>"
|
|
|
|
" </interface>"
|
|
|
|
" </node>";
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
info = g_dbus_node_info_new_for_xml (data, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
g_dbus_node_info_unref (info);
|
|
|
|
}
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
2013-11-09 20:23:15 +01:00
|
|
|
gint ret;
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
|
|
|
/* all the tests rely on a shared main loop */
|
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
|
|
|
|
g_test_add_func ("/gdbus/introspection-parser", test_introspection_parser);
|
2010-08-23 06:36:36 +02:00
|
|
|
g_test_add_func ("/gdbus/introspection-generate", test_generate);
|
2010-08-30 14:49:41 +02:00
|
|
|
g_test_add_func ("/gdbus/introspection-default-direction", test_default_direction);
|
|
|
|
g_test_add_func ("/gdbus/introspection-extra-data", test_extra_data);
|
2010-08-23 06:36:36 +02:00
|
|
|
|
2013-11-09 20:23:15 +01:00
|
|
|
ret = session_bus_run ();
|
|
|
|
|
|
|
|
while (g_main_context_iteration (NULL, FALSE));
|
|
|
|
g_main_loop_unref (loop);
|
|
|
|
|
|
|
|
return ret;
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|