| 1 |
/* GLib testing framework examples and tests |
| 2 |
* |
| 3 |
* Copyright (C) 2008-2010 Red Hat, Inc. |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General |
| 18 |
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
* |
| 20 |
* Author: David Zeuthen <[email protected]> |
| 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 |
|
| 34 |
static void |
| 35 |
proxy_new_cb (GObject *source_object, |
| 36 |
GAsyncResult *res, |
| 37 |
gpointer user_data) |
| 38 |
{ |
| 39 |
GDBusProxy **ret = user_data; |
| 40 |
GError *error; |
| 41 |
|
| 42 |
error = NULL; |
| 43 |
*ret = g_dbus_proxy_new_finish (res, &error); |
| 44 |
g_assert_no_error (error); |
| 45 |
g_assert (ret != NULL); |
| 46 |
|
| 47 |
g_main_loop_quit (loop); |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
test_proxy_well_known_name (void) |
| 52 |
{ |
| 53 |
GDBusProxy *p; |
| 54 |
GDBusProxy *p2; |
| 55 |
GDBusProxy *ap; |
| 56 |
GDBusProxy *ap2; |
| 57 |
GDBusConnection *c; |
| 58 |
GError *error; |
| 59 |
gchar *name_owner; |
| 60 |
gchar **property_names; |
| 61 |
GVariant *variant; |
| 62 |
GVariant *result; |
| 63 |
|
| 64 |
session_bus_up (); |
| 65 |
|
| 66 |
error = NULL; |
| 67 |
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); |
| 68 |
g_assert_no_error (error); |
| 69 |
g_assert (c != NULL); |
| 70 |
|
| 71 |
error = NULL; |
| 72 |
p = g_dbus_proxy_new_sync (c, |
| 73 |
G_DBUS_PROXY_FLAGS_NONE, |
| 74 |
NULL, /* GDBusInterfaceInfo* */ |
| 75 |
"com.example.TestService", /* name */ |
| 76 |
"/com/example/TestObject", /* object path */ |
| 77 |
"com.example.Frob", /* interface name */ |
| 78 |
NULL, /* GCancellable */ |
| 79 |
&error); |
| 80 |
g_assert_no_error (error); |
| 81 |
|
| 82 |
/* we shouldn't have a name owner nor any cached properties */ |
| 83 |
g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL); |
| 84 |
g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL); |
| 85 |
|
| 86 |
/* also for async: we shouldn't have a name owner nor any cached properties */ |
| 87 |
g_dbus_proxy_new (c, |
| 88 |
G_DBUS_PROXY_FLAGS_NONE, |
| 89 |
NULL, /* GDBusInterfaceInfo* */ |
| 90 |
"com.example.TestService", /* name */ |
| 91 |
"/com/example/TestObject", /* object path */ |
| 92 |
"com.example.Frob", /* interface name */ |
| 93 |
NULL, /* GCancellable */ |
| 94 |
(GAsyncReadyCallback) proxy_new_cb, |
| 95 |
&ap); |
| 96 |
g_main_loop_run (loop); |
| 97 |
g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL); |
| 98 |
g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL); |
| 99 |
|
| 100 |
/* this is safe; testserver will exit once the bus goes away */ |
| 101 |
g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL)); |
| 102 |
|
| 103 |
/* check that we get the notify::g-name-owner signal */ |
| 104 |
_g_assert_property_notify (p, "g-name-owner"); |
| 105 |
|
| 106 |
/* Now we should have a name owner as well as properties */ |
| 107 |
name_owner = g_dbus_proxy_get_name_owner (p); |
| 108 |
property_names = g_dbus_proxy_get_cached_property_names (p); |
| 109 |
g_assert (g_dbus_is_unique_name (name_owner)); |
| 110 |
g_assert (property_names != NULL && g_strv_length (property_names) > 0); |
| 111 |
g_free (name_owner); |
| 112 |
g_strfreev (property_names); |
| 113 |
|
| 114 |
/* if we create another proxy with the service being available, check that |
| 115 |
* it has a name owner and properties |
| 116 |
*/ |
| 117 |
error = NULL; |
| 118 |
p2 = g_dbus_proxy_new_sync (c, |
| 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 name */ |
| 124 |
NULL, /* GCancellable */ |
| 125 |
&error); |
| 126 |
g_assert_no_error (error); |
| 127 |
name_owner = g_dbus_proxy_get_name_owner (p2); |
| 128 |
property_names = g_dbus_proxy_get_cached_property_names (p2); |
| 129 |
g_assert (g_dbus_is_unique_name (name_owner)); |
| 130 |
g_assert (property_names != NULL && g_strv_length (property_names) > 0); |
| 131 |
g_free (name_owner); |
| 132 |
g_strfreev (property_names); |
| 133 |
|
| 134 |
/* also for async: we should have a name owner and cached properties */ |
| 135 |
g_dbus_proxy_new (c, |
| 136 |
G_DBUS_PROXY_FLAGS_NONE, |
| 137 |
NULL, /* GDBusInterfaceInfo* */ |
| 138 |
"com.example.TestService", /* name */ |
| 139 |
"/com/example/TestObject", /* object path */ |
| 140 |
"com.example.Frob", /* interface name */ |
| 141 |
NULL, /* GCancellable */ |
| 142 |
(GAsyncReadyCallback) proxy_new_cb, |
| 143 |
&ap2); |
| 144 |
g_main_loop_run (loop); |
| 145 |
name_owner = g_dbus_proxy_get_name_owner (ap2); |
| 146 |
property_names = g_dbus_proxy_get_cached_property_names (ap2); |
| 147 |
g_assert (g_dbus_is_unique_name (name_owner)); |
| 148 |
g_assert (property_names != NULL && g_strv_length (property_names) > 0); |
| 149 |
g_free (name_owner); |
| 150 |
g_strfreev (property_names); |
| 151 |
|
| 152 |
/* Check property value is the initial value */ |
| 153 |
variant = g_dbus_proxy_get_cached_property (p, "y"); |
| 154 |
g_assert (variant != NULL); |
| 155 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 1); |
| 156 |
g_variant_unref (variant); |
| 157 |
variant = g_dbus_proxy_get_cached_property (p2, "y"); |
| 158 |
g_assert (variant != NULL); |
| 159 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 1); |
| 160 |
g_variant_unref (variant); |
| 161 |
variant = g_dbus_proxy_get_cached_property (ap, "y"); |
| 162 |
g_assert (variant != NULL); |
| 163 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 1); |
| 164 |
g_variant_unref (variant); |
| 165 |
variant = g_dbus_proxy_get_cached_property (ap2, "y"); |
| 166 |
g_assert (variant != NULL); |
| 167 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 1); |
| 168 |
g_variant_unref (variant); |
| 169 |
|
| 170 |
/* Check that properties are updated on both p and p2 */ |
| 171 |
result = g_dbus_proxy_call_sync (p, |
| 172 |
"FrobSetProperty", |
| 173 |
g_variant_new ("(sv)", |
| 174 |
"y", |
| 175 |
g_variant_new_byte (42)), |
| 176 |
G_DBUS_CALL_FLAGS_NONE, |
| 177 |
-1, |
| 178 |
NULL, |
| 179 |
&error); |
| 180 |
g_assert_no_error (error); |
| 181 |
g_assert (result != NULL); |
| 182 |
g_assert_cmpstr (g_variant_get_type_string (result), ==, "()"); |
| 183 |
g_variant_unref (result); |
| 184 |
_g_assert_signal_received (p, "g-properties-changed"); |
| 185 |
variant = g_dbus_proxy_get_cached_property (p, "y"); |
| 186 |
g_assert (variant != NULL); |
| 187 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 42); |
| 188 |
g_variant_unref (variant); |
| 189 |
variant = g_dbus_proxy_get_cached_property (p2, "y"); |
| 190 |
g_assert (variant != NULL); |
| 191 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 42); |
| 192 |
g_variant_unref (variant); |
| 193 |
variant = g_dbus_proxy_get_cached_property (ap, "y"); |
| 194 |
g_assert (variant != NULL); |
| 195 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 42); |
| 196 |
g_variant_unref (variant); |
| 197 |
variant = g_dbus_proxy_get_cached_property (ap2, "y"); |
| 198 |
g_assert (variant != NULL); |
| 199 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 42); |
| 200 |
g_variant_unref (variant); |
| 201 |
|
| 202 |
/* Nuke the service and check that we get the signal and then don't |
| 203 |
* have a name owner nor any cached properties |
| 204 |
*/ |
| 205 |
result = g_dbus_proxy_call_sync (p, |
| 206 |
"Quit", |
| 207 |
NULL, |
| 208 |
G_DBUS_CALL_FLAGS_NONE, |
| 209 |
-1, |
| 210 |
NULL, |
| 211 |
&error); |
| 212 |
g_assert_no_error (error); |
| 213 |
g_assert (result != NULL); |
| 214 |
g_assert_cmpstr (g_variant_get_type_string (result), ==, "()"); |
| 215 |
g_variant_unref (result); |
| 216 |
/* and wait... */ |
| 217 |
_g_assert_property_notify (p, "g-name-owner"); |
| 218 |
/* now we shouldn't have a name owner nor any cached properties */ |
| 219 |
g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL); |
| 220 |
g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL); |
| 221 |
g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL); |
| 222 |
|
| 223 |
/* now bring back the server and wait for the proxy to be updated.. now |
| 224 |
* the 'y' property should be back at 1... |
| 225 |
*/ |
| 226 |
/* this is safe; testserver will exit once the bus goes away */ |
| 227 |
g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL)); |
| 228 |
|
| 229 |
/* check that we get the notify::g-name-owner signal */ |
| 230 |
_g_assert_property_notify (p, "g-name-owner"); |
| 231 |
/* Now we should have a name owner as well as properties */ |
| 232 |
name_owner = g_dbus_proxy_get_name_owner (p); |
| 233 |
property_names = g_dbus_proxy_get_cached_property_names (p); |
| 234 |
g_assert (g_dbus_is_unique_name (name_owner)); |
| 235 |
g_assert (property_names != NULL && g_strv_length (property_names) > 0); |
| 236 |
g_free (name_owner); |
| 237 |
g_strfreev (property_names); |
| 238 |
/* and finally check the 'y' property */ |
| 239 |
variant = g_dbus_proxy_get_cached_property (p, "y"); |
| 240 |
g_assert (variant != NULL); |
| 241 |
g_assert_cmpint (g_variant_get_byte (variant), ==, 1); |
| 242 |
g_variant_unref (variant); |
| 243 |
|
| 244 |
g_object_unref (p2); |
| 245 |
g_object_unref (p); |
| 246 |
g_object_unref (ap2); |
| 247 |
g_object_unref (ap); |
| 248 |
|
| 249 |
g_object_unref (c); |
| 250 |
|
| 251 |
/* tear down bus */ |
| 252 |
session_bus_down (); |
| 253 |
} |
| 254 |
|
| 255 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 256 |
|
| 257 |
int |
| 258 |
main (int argc, |
| 259 |
char *argv[]) |
| 260 |
{ |
| 261 |
gint ret; |
| 262 |
|
| 263 |
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); |
| 264 |
|
| 265 |
/* all the tests rely on a shared main loop */ |
| 266 |
loop = g_main_loop_new (NULL, FALSE); |
| 267 |
|
| 268 |
g_test_dbus_unset (); |
| 269 |
|
| 270 |
g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name); |
| 271 |
|
| 272 |
ret = g_test_run(); |
| 273 |
return ret; |
| 274 |
} |