| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "mock-service.h" |
| 9 |
#include "mock-dbus-tests.h" |
| 10 |
|
| 11 |
#include <gio/gio.h> |
| 12 |
#include <gio/gunixfdlist.h> |
| 13 |
#include <glib-unix.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
typedef struct { |
| 17 |
GDBusConnection *connection; |
| 18 |
GDBusObjectManagerServer *object_manager; |
| 19 |
GHashTable *other_names; |
| 20 |
GList *never_return_invocations; |
| 21 |
} MockData; |
| 22 |
|
| 23 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 24 |
|
| 25 |
static gboolean |
| 26 |
on_handle_hello_world (TestFrobber *object, |
| 27 |
GDBusMethodInvocation *invocation, |
| 28 |
const gchar *greeting, |
| 29 |
gpointer user_data) |
| 30 |
{ |
| 31 |
gchar *response; |
| 32 |
response = g_strdup_printf ("Word! You said `%s'. I'm Skeleton, btw!", greeting); |
| 33 |
test_frobber_complete_hello_world (object, invocation, response); |
| 34 |
g_free (response); |
| 35 |
return TRUE; |
| 36 |
} |
| 37 |
|
| 38 |
static gboolean |
| 39 |
on_handle_never_return (TestFrobber *object, |
| 40 |
GDBusMethodInvocation *invocation, |
| 41 |
gpointer user_data) |
| 42 |
{ |
| 43 |
MockData *data = user_data; |
| 44 |
/* just ignoring the call causes long TestDBus shutdown errors, as the unanswered call spooks around in the server's brain until |
| 45 |
* the global D-Bus timeout; so remember them and answer on shutdown */ |
| 46 |
data->never_return_invocations = g_list_append (data->never_return_invocations, g_object_ref (invocation)); |
| 47 |
return TRUE; |
| 48 |
} |
| 49 |
|
| 50 |
static gboolean |
| 51 |
on_handle_test_primitive_types (TestFrobber *object, |
| 52 |
GDBusMethodInvocation *invocation, |
| 53 |
guchar val_byte, |
| 54 |
gboolean val_boolean, |
| 55 |
gint16 val_int16, |
| 56 |
guint16 val_uint16, |
| 57 |
gint val_int32, |
| 58 |
guint val_uint32, |
| 59 |
gint64 val_int64, |
| 60 |
guint64 val_uint64, |
| 61 |
gdouble val_double, |
| 62 |
const gchar *val_string, |
| 63 |
const gchar *val_objpath, |
| 64 |
const gchar *val_signature, |
| 65 |
const gchar *val_bytestring, |
| 66 |
gpointer user_data) |
| 67 |
{ |
| 68 |
gchar *s1; |
| 69 |
gchar *s2; |
| 70 |
gchar *s3; |
| 71 |
s1 = g_strdup_printf ("Word! You said `%s'. Rock'n'roll!", val_string); |
| 72 |
s2 = g_strdup_printf ("/modified%s", val_objpath); |
| 73 |
s3 = g_strdup_printf ("assgit%s", val_signature); |
| 74 |
test_frobber_complete_test_primitive_types (object, |
| 75 |
invocation, |
| 76 |
10 + val_byte, |
| 77 |
!val_boolean, |
| 78 |
100 + val_int16, |
| 79 |
1000 + val_uint16, |
| 80 |
10000 + val_int32, |
| 81 |
100000 + val_uint32, |
| 82 |
1000000 + val_int64, |
| 83 |
10000000 + val_uint64, |
| 84 |
val_double / G_PI, |
| 85 |
s1, |
| 86 |
s2, |
| 87 |
s3, |
| 88 |
"bytestring!\xff"); |
| 89 |
g_free (s1); |
| 90 |
g_free (s2); |
| 91 |
g_free (s3); |
| 92 |
return TRUE; |
| 93 |
} |
| 94 |
|
| 95 |
static gboolean |
| 96 |
on_handle_test_non_primitive_types (TestFrobber *object, |
| 97 |
GDBusMethodInvocation *invocation, |
| 98 |
GVariant *dict_s_to_s, |
| 99 |
GVariant *dict_s_to_pairs, |
| 100 |
GVariant *a_struct, |
| 101 |
const gchar* const *array_of_strings, |
| 102 |
const gchar* const *array_of_objpaths, |
| 103 |
GVariant *array_of_signatures, |
| 104 |
const gchar* const *array_of_bytestrings, |
| 105 |
gpointer user_data) |
| 106 |
{ |
| 107 |
gchar *s; |
| 108 |
GString *str; |
| 109 |
str = g_string_new (NULL); |
| 110 |
s = g_variant_print (dict_s_to_s, TRUE); |
| 111 |
g_string_append (str, s); |
| 112 |
g_free (s); |
| 113 |
s = g_variant_print (dict_s_to_pairs, TRUE); |
| 114 |
g_string_append (str, s); |
| 115 |
g_free (s); |
| 116 |
s = g_variant_print (a_struct, TRUE); |
| 117 |
g_string_append (str, s); |
| 118 |
g_free (s); |
| 119 |
s = g_strjoinv (", ", (gchar **)array_of_strings); |
| 120 |
g_string_append_printf (str, "array_of_strings: [%s] ", s); |
| 121 |
g_free (s); |
| 122 |
s = g_strjoinv (", ", (gchar **)array_of_objpaths); |
| 123 |
g_string_append_printf (str, "array_of_objpaths: [%s] ", s); |
| 124 |
g_free (s); |
| 125 |
s = g_variant_print (array_of_signatures, TRUE); |
| 126 |
g_string_append_printf (str, "array_of_signatures: %s ", s); |
| 127 |
g_free (s); |
| 128 |
s = g_strjoinv (", ", (gchar **)array_of_bytestrings); |
| 129 |
g_string_append_printf (str, "array_of_bytestrings: [%s] ", s); |
| 130 |
g_free (s); |
| 131 |
test_frobber_complete_test_non_primitive_types (object, invocation, str->str); |
| 132 |
g_string_free (str, TRUE); |
| 133 |
return TRUE; |
| 134 |
} |
| 135 |
|
| 136 |
static gboolean |
| 137 |
on_handle_test_variant (TestFrobber *object, |
| 138 |
GDBusMethodInvocation *invocation, |
| 139 |
GVariant *v, |
| 140 |
gpointer user_data) |
| 141 |
{ |
| 142 |
test_frobber_complete_test_variant (object, invocation); |
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
static gboolean |
| 147 |
on_handle_request_signal_emission (TestFrobber *object, |
| 148 |
GDBusMethodInvocation *invocation, |
| 149 |
gint which_one, |
| 150 |
gpointer user_data) |
| 151 |
{ |
| 152 |
if (which_one == 0) |
| 153 |
{ |
| 154 |
const gchar *a_strv[] = {"foo", "frobber", NULL}; |
| 155 |
const gchar *a_objpath_array[] = {"/foo", "/foo/bar", NULL}; |
| 156 |
GVariant *a_variant = g_variant_new_parsed ("{'first': (42, 42), 'second': (43, 43)}"); |
| 157 |
test_frobber_emit_test_signal (object, 43, a_strv, a_objpath_array, a_variant); /* consumes a_variant */ |
| 158 |
test_frobber_complete_request_signal_emission (object, invocation); |
| 159 |
} |
| 160 |
return TRUE; |
| 161 |
} |
| 162 |
|
| 163 |
static gboolean |
| 164 |
on_handle_request_property_mods (TestFrobber *object, |
| 165 |
GDBusMethodInvocation *invocation, |
| 166 |
gpointer user_data) |
| 167 |
{ |
| 168 |
test_frobber_set_y (object, test_frobber_get_y (object) + 1); |
| 169 |
test_frobber_set_i (object, test_frobber_get_i (object) + 1); |
| 170 |
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object)); |
| 171 |
test_frobber_complete_request_multi_property_mods (object, invocation); |
| 172 |
return TRUE; |
| 173 |
} |
| 174 |
|
| 175 |
static gboolean |
| 176 |
on_handle_request_multi_property_mods (TestFrobber *object, |
| 177 |
GDBusMethodInvocation *invocation, |
| 178 |
gpointer user_data) |
| 179 |
{ |
| 180 |
test_frobber_set_y (object, test_frobber_get_y (object) + 1); |
| 181 |
test_frobber_set_i (object, test_frobber_get_i (object) + 1); |
| 182 |
test_frobber_set_y (object, test_frobber_get_y (object) + 1); |
| 183 |
test_frobber_set_i (object, test_frobber_get_i (object) + 1); |
| 184 |
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object)); |
| 185 |
test_frobber_set_y (object, test_frobber_get_y (object) + 1); |
| 186 |
test_frobber_set_i (object, test_frobber_get_i (object) + 1); |
| 187 |
test_frobber_complete_request_multi_property_mods (object, invocation); |
| 188 |
return TRUE; |
| 189 |
} |
| 190 |
|
| 191 |
static gboolean |
| 192 |
on_handle_property_cancellation (TestFrobber *object, |
| 193 |
GDBusMethodInvocation *invocation, |
| 194 |
gpointer user_data) |
| 195 |
{ |
| 196 |
guint n; |
| 197 |
n = test_frobber_get_n (object); |
| 198 |
/* This queues up a PropertiesChange event */ |
| 199 |
test_frobber_set_n (object, n + 1); |
| 200 |
/* this modifies the queued up event */ |
| 201 |
test_frobber_set_n (object, n); |
| 202 |
/* this flushes all PropertiesChanges event (sends the D-Bus message right |
| 203 |
* away, if any - there should not be any) |
| 204 |
*/ |
| 205 |
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object)); |
| 206 |
/* this makes us return the reply D-Bus method */ |
| 207 |
test_frobber_complete_property_cancellation (object, invocation); |
| 208 |
return TRUE; |
| 209 |
} |
| 210 |
|
| 211 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 212 |
|
| 213 |
static gboolean |
| 214 |
on_handle_create_object (TestFrobber *object, |
| 215 |
GDBusMethodInvocation *invocation, |
| 216 |
const gchar *at_path, |
| 217 |
gpointer user_data) |
| 218 |
{ |
| 219 |
MockData *data = user_data; |
| 220 |
GDBusObject *previous; |
| 221 |
|
| 222 |
previous = g_dbus_object_manager_get_object (G_DBUS_OBJECT_MANAGER (data->object_manager), at_path); |
| 223 |
if (previous != NULL) |
| 224 |
{ |
| 225 |
g_dbus_method_invocation_return_error (invocation, |
| 226 |
G_IO_ERROR, G_IO_ERROR_FAILED, |
| 227 |
"Sorry, object already exists at %s", |
| 228 |
at_path); |
| 229 |
g_object_unref (previous); |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
TestObjectSkeleton *new_object; |
| 234 |
TestFrobber *frobber; |
| 235 |
|
| 236 |
new_object = test_object_skeleton_new (at_path); |
| 237 |
frobber = test_frobber_skeleton_new (); |
| 238 |
test_object_skeleton_set_frobber (new_object, frobber); |
| 239 |
g_dbus_object_manager_server_export (data->object_manager, G_DBUS_OBJECT_SKELETON (new_object)); |
| 240 |
g_object_unref (frobber); |
| 241 |
g_object_unref (new_object); |
| 242 |
|
| 243 |
g_signal_connect (frobber, |
| 244 |
"handle-request-property-mods", |
| 245 |
G_CALLBACK (on_handle_request_property_mods), |
| 246 |
NULL); |
| 247 |
test_frobber_complete_create_object (object, invocation); |
| 248 |
} |
| 249 |
return TRUE; |
| 250 |
} |
| 251 |
|
| 252 |
static gboolean |
| 253 |
on_handle_delete_object (TestFrobber *object, |
| 254 |
GDBusMethodInvocation *invocation, |
| 255 |
const gchar *path, |
| 256 |
gpointer user_data) |
| 257 |
{ |
| 258 |
MockData *data = user_data; |
| 259 |
GDBusObject *previous; |
| 260 |
|
| 261 |
previous = g_dbus_object_manager_get_object (G_DBUS_OBJECT_MANAGER (data->object_manager), path); |
| 262 |
if (previous != NULL) |
| 263 |
{ |
| 264 |
g_warn_if_fail (g_dbus_object_manager_server_unexport (data->object_manager, path)); |
| 265 |
test_frobber_complete_delete_object (object, invocation); |
| 266 |
g_object_unref (previous); |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
g_dbus_method_invocation_return_error (invocation, |
| 271 |
G_IO_ERROR, G_IO_ERROR_FAILED, |
| 272 |
"Sorry, there is no object at %s", |
| 273 |
path); |
| 274 |
} |
| 275 |
return TRUE; |
| 276 |
} |
| 277 |
|
| 278 |
static gboolean |
| 279 |
on_handle_delete_all_objects (TestFrobber *object, |
| 280 |
GDBusMethodInvocation *invocation, |
| 281 |
gpointer user_data) |
| 282 |
{ |
| 283 |
MockData *data = user_data; |
| 284 |
const gchar *path; |
| 285 |
GList *objects; |
| 286 |
GList *l; |
| 287 |
|
| 288 |
objects = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (data->object_manager)); |
| 289 |
for (l = objects; l != NULL; l = g_list_next (l)) |
| 290 |
{ |
| 291 |
path = g_dbus_object_get_object_path (l->data); |
| 292 |
if (!g_str_has_suffix (path, "/frobber")) |
| 293 |
g_warn_if_fail (g_dbus_object_manager_server_unexport (data->object_manager, path)); |
| 294 |
} |
| 295 |
|
| 296 |
test_frobber_complete_delete_all_objects (object, invocation); |
| 297 |
g_list_free_full (objects, g_object_unref); |
| 298 |
return TRUE; |
| 299 |
} |
| 300 |
|
| 301 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 302 |
|
| 303 |
static gboolean |
| 304 |
on_handle_test_asv (TestFrobber *object, |
| 305 |
GDBusMethodInvocation *invocation, |
| 306 |
GVariant *asv, |
| 307 |
gpointer user_data) |
| 308 |
{ |
| 309 |
gchar *s; |
| 310 |
s = g_variant_print (asv, TRUE); |
| 311 |
test_frobber_complete_test_asv (object, invocation, s); |
| 312 |
g_free (s); |
| 313 |
return TRUE; |
| 314 |
} |
| 315 |
|
| 316 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 317 |
|
| 318 |
static gboolean |
| 319 |
on_handle_add_alpha (TestFrobber *frobber, |
| 320 |
GDBusMethodInvocation *invocation, |
| 321 |
gpointer user_data) |
| 322 |
{ |
| 323 |
TestObjectSkeleton *enclosing; |
| 324 |
enclosing = TEST_OBJECT_SKELETON (g_dbus_interface_get_object (G_DBUS_INTERFACE (frobber))); |
| 325 |
if (test_object_peek_alpha (TEST_OBJECT (enclosing)) == NULL) |
| 326 |
{ |
| 327 |
TestAlpha *iface = test_alpha_skeleton_new (); |
| 328 |
test_object_skeleton_set_alpha (enclosing, iface); |
| 329 |
g_object_unref (iface); |
| 330 |
} |
| 331 |
test_frobber_complete_add_alpha (frobber, invocation); |
| 332 |
return TRUE; |
| 333 |
} |
| 334 |
|
| 335 |
static gboolean |
| 336 |
on_handle_remove_alpha (TestFrobber *frobber, |
| 337 |
GDBusMethodInvocation *invocation, |
| 338 |
gpointer user_data) |
| 339 |
{ |
| 340 |
TestObjectSkeleton *enclosing; |
| 341 |
enclosing = TEST_OBJECT_SKELETON (g_dbus_interface_get_object (G_DBUS_INTERFACE (frobber))); |
| 342 |
if (test_object_peek_alpha (TEST_OBJECT (enclosing)) != NULL) |
| 343 |
test_object_skeleton_set_alpha (enclosing, NULL); |
| 344 |
test_frobber_complete_add_alpha (frobber, invocation); |
| 345 |
return TRUE; |
| 346 |
} |
| 347 |
|
| 348 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 349 |
|
| 350 |
typedef struct { |
| 351 |
GDBusMethodInvocation *invocation; |
| 352 |
} ClaimNameData; |
| 353 |
|
| 354 |
static void |
| 355 |
claim_name_data_free (gpointer data) |
| 356 |
{ |
| 357 |
ClaimNameData *claim_data = data; |
| 358 |
if (claim_data->invocation) |
| 359 |
g_object_unref (claim_data->invocation); |
| 360 |
g_free (claim_data); |
| 361 |
} |
| 362 |
|
| 363 |
static void |
| 364 |
on_other_name_acquired (GDBusConnection *connection, |
| 365 |
const gchar *name, |
| 366 |
gpointer user_data) |
| 367 |
{ |
| 368 |
ClaimNameData *claim_data = user_data; |
| 369 |
if (claim_data->invocation) |
| 370 |
{ |
| 371 |
g_dbus_method_invocation_return_value (claim_data->invocation, NULL); |
| 372 |
g_clear_object (&claim_data->invocation); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
static void |
| 377 |
on_other_name_lost (GDBusConnection *connection, |
| 378 |
const gchar *name, |
| 379 |
gpointer user_data) |
| 380 |
{ |
| 381 |
ClaimNameData *claim_data = user_data; |
| 382 |
if (claim_data->invocation) |
| 383 |
{ |
| 384 |
g_dbus_method_invocation_return_error (claim_data->invocation, |
| 385 |
G_IO_ERROR, G_IO_ERROR_FAILED, |
| 386 |
"Couldn't claim name: %s", name); |
| 387 |
g_message ("couldn't claim name: %s", name); |
| 388 |
g_clear_object (&claim_data->invocation); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
static gboolean |
| 393 |
on_claim_other_name (TestFrobber *frobber, |
| 394 |
GDBusMethodInvocation *invocation, |
| 395 |
const gchar *name, |
| 396 |
gpointer user_data) |
| 397 |
{ |
| 398 |
ClaimNameData *claim_data; |
| 399 |
MockData *mock_data = user_data; |
| 400 |
guint id; |
| 401 |
|
| 402 |
g_return_val_if_fail (g_hash_table_lookup (mock_data->other_names, name) == NULL, FALSE); |
| 403 |
|
| 404 |
claim_data = g_new0 (ClaimNameData, 1); |
| 405 |
claim_data->invocation = g_object_ref (invocation); |
| 406 |
id = g_bus_own_name_on_connection (mock_data->connection, name, |
| 407 |
G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_NONE, |
| 408 |
on_other_name_acquired, |
| 409 |
on_other_name_lost, |
| 410 |
claim_data, claim_name_data_free); |
| 411 |
|
| 412 |
g_hash_table_replace (mock_data->other_names, g_strdup (name), GUINT_TO_POINTER (id)); |
| 413 |
return TRUE; |
| 414 |
} |
| 415 |
|
| 416 |
static gboolean |
| 417 |
on_release_other_name (TestFrobber *frobber, |
| 418 |
GDBusMethodInvocation *invocation, |
| 419 |
const gchar *name, |
| 420 |
gpointer user_data) |
| 421 |
{ |
| 422 |
MockData *mock_data = user_data; |
| 423 |
guint id; |
| 424 |
|
| 425 |
g_return_val_if_fail (g_hash_table_lookup (mock_data->other_names, name) != NULL, FALSE); |
| 426 |
|
| 427 |
id = GPOINTER_TO_UINT (g_hash_table_lookup (mock_data->other_names, name)); |
| 428 |
g_hash_table_remove (mock_data->other_names, name); |
| 429 |
g_bus_unown_name (id); |
| 430 |
|
| 431 |
g_dbus_method_invocation_return_value (invocation, NULL); |
| 432 |
return TRUE; |
| 433 |
|
| 434 |
} |
| 435 |
|
| 436 |
static gboolean |
| 437 |
on_tell_me_your_name (TestFrobber *frobber, |
| 438 |
GDBusMethodInvocation *invocation, |
| 439 |
gpointer user_data) |
| 440 |
{ |
| 441 |
GDBusMessage *message = g_dbus_method_invocation_get_message (invocation); |
| 442 |
GVariant *name = g_variant_new_string (g_dbus_message_get_destination (message)); |
| 443 |
g_dbus_method_invocation_return_value (invocation, g_variant_new_tuple (&name, 1)); |
| 444 |
return TRUE; |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
static gboolean |
| 449 |
test_fd_pipe_readable (gint fd, |
| 450 |
GIOCondition condition, |
| 451 |
gpointer user_data) |
| 452 |
{ |
| 453 |
const char *expected = "Hello, fd"; |
| 454 |
size_t expected_len = strlen (expected); |
| 455 |
char buffer[100]; |
| 456 |
size_t n; |
| 457 |
|
| 458 |
g_assert (condition == G_IO_IN); |
| 459 |
|
| 460 |
n = read (fd, buffer, 100); |
| 461 |
g_assert (n == expected_len); |
| 462 |
g_assert (strncmp (buffer, expected, expected_len) == 0); |
| 463 |
|
| 464 |
close (fd); |
| 465 |
|
| 466 |
return G_SOURCE_REMOVE; |
| 467 |
} |
| 468 |
|
| 469 |
static gboolean |
| 470 |
test_fd_deferred_close (gpointer user_data) |
| 471 |
{ |
| 472 |
gint fd = GPOINTER_TO_INT (user_data); |
| 473 |
|
| 474 |
close (fd); |
| 475 |
|
| 476 |
return G_SOURCE_REMOVE; |
| 477 |
} |
| 478 |
|
| 479 |
static gboolean |
| 480 |
on_make_test_fd (TestFrobber *frobber, |
| 481 |
GDBusMethodInvocation *invocation, |
| 482 |
const char *type, |
| 483 |
gpointer user_data) |
| 484 |
{ |
| 485 |
gint fds[2] = { -1, -1 }; |
| 486 |
GUnixFDList *fd_list; |
| 487 |
const char *data = "Hello, fd"; |
| 488 |
size_t data_size = strlen (data); |
| 489 |
size_t n; |
| 490 |
GError *error = NULL; |
| 491 |
|
| 492 |
g_unix_open_pipe (fds, FD_CLOEXEC, &error); |
| 493 |
g_assert_no_error (error); |
| 494 |
|
| 495 |
fd_list = g_unix_fd_list_new (); |
| 496 |
|
| 497 |
if (g_str_equal (type, "readable")) |
| 498 |
{ |
| 499 |
g_unix_fd_list_append (fd_list, fds[0], &error); |
| 500 |
g_assert_no_error (error); |
| 501 |
close (fds[0]); |
| 502 |
|
| 503 |
n = write(fds[1], data, data_size); |
| 504 |
g_assert (n == data_size); |
| 505 |
|
| 506 |
/* |
| 507 |
* Close this fd a bit later, because closing the writing side |
| 508 |
* makes CockpitPipe close itself immediately, which breaks |
| 509 |
* testing that writing into this read-only pipe fails. |
| 510 |
*/ |
| 511 |
g_timeout_add_seconds (5, test_fd_deferred_close, GINT_TO_POINTER (fds[1])); |
| 512 |
} |
| 513 |
else if (g_str_equal (type, "writable")) |
| 514 |
{ |
| 515 |
g_unix_fd_list_append (fd_list, fds[1], &error); |
| 516 |
g_assert_no_error (error); |
| 517 |
close (fds[1]); |
| 518 |
|
| 519 |
g_unix_fd_add (fds[0], G_IO_IN, test_fd_pipe_readable, NULL); |
| 520 |
} |
| 521 |
else |
| 522 |
g_assert_not_reached (); |
| 523 |
|
| 524 |
g_dbus_method_invocation_return_value_with_unix_fd_list (invocation, g_variant_new ("(h)", 0), fd_list); |
| 525 |
|
| 526 |
return TRUE; |
| 527 |
} |
| 528 |
|
| 529 |
/* ------------------------------------------------------------------------ |
| 530 |
* Non object manager stuff |
| 531 |
*/ |
| 532 |
|
| 533 |
static GVariant * |
| 534 |
clique_get_property (GDBusConnection *connection, |
| 535 |
const gchar *sender, |
| 536 |
const gchar *object_path, |
| 537 |
const gchar *interface_name, |
| 538 |
const gchar *property_name, |
| 539 |
GError **error, |
| 540 |
gpointer user_data) |
| 541 |
{ |
| 542 |
/* The only property is Friend */ |
| 543 |
gchar *friend = user_data; |
| 544 |
return g_variant_new_object_path (friend); |
| 545 |
} |
| 546 |
|
| 547 |
static gboolean |
| 548 |
on_create_clique (TestFrobber *frobber, |
| 549 |
GDBusMethodInvocation *invocation, |
| 550 |
const gchar *name, |
| 551 |
gpointer user_data) |
| 552 |
{ |
| 553 |
GDBusConnection *connection = g_dbus_method_invocation_get_connection (invocation); |
| 554 |
GError *error = NULL; |
| 555 |
gchar *path = NULL; |
| 556 |
gchar *friend; |
| 557 |
gint i; |
| 558 |
|
| 559 |
static const GDBusInterfaceVTable vtable = { |
| 560 |
.method_call = NULL, |
| 561 |
.get_property = clique_get_property, |
| 562 |
.set_property = NULL, |
| 563 |
}; |
| 564 |
|
| 565 |
for (i = 0; i < 3; i++) |
| 566 |
{ |
| 567 |
g_free (path); |
| 568 |
path = g_strdup_printf ("/cliques/%s/%d", name, i); |
| 569 |
|
| 570 |
friend = g_strdup_printf ("/cliques/%s/%d", name, (i + 1) % 3); |
| 571 |
|
| 572 |
g_dbus_connection_register_object (connection, path, |
| 573 |
test_clique_interface_info (), |
| 574 |
&vtable, friend, g_free, &error); |
| 575 |
if (error) |
| 576 |
{ |
| 577 |
g_critical ("Couldn't register new clique: %s", error->message); |
| 578 |
g_clear_error (&error); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
test_frobber_complete_create_clique (frobber, invocation, path); |
| 583 |
g_free (path); |
| 584 |
return TRUE; |
| 585 |
} |
| 586 |
|
| 587 |
static GVariant * |
| 588 |
hidden_get_property (GDBusConnection *connection, |
| 589 |
const gchar *sender, |
| 590 |
const gchar *object_path, |
| 591 |
const gchar *interface_name, |
| 592 |
const gchar *property_name, |
| 593 |
GError **error, |
| 594 |
gpointer user_data) |
| 595 |
{ |
| 596 |
/* The only property is Name */ |
| 597 |
gchar *name = user_data; |
| 598 |
return g_variant_new_string (name); |
| 599 |
} |
| 600 |
|
| 601 |
static gboolean |
| 602 |
on_emit_hidden (TestFrobber *frobber, |
| 603 |
GDBusMethodInvocation *invocation, |
| 604 |
const gchar *name, |
| 605 |
gpointer user_data) |
| 606 |
{ |
| 607 |
GDBusConnection *connection = g_dbus_method_invocation_get_connection (invocation); |
| 608 |
GError *error = NULL; |
| 609 |
gchar *path = NULL; |
| 610 |
|
| 611 |
static const GDBusInterfaceVTable vtable = { |
| 612 |
.method_call = NULL, |
| 613 |
.get_property = hidden_get_property, |
| 614 |
.set_property = NULL, |
| 615 |
}; |
| 616 |
|
| 617 |
path = g_strdup_printf ("/hidden/%s", name); |
| 618 |
|
| 619 |
g_dbus_connection_register_object (connection, path, |
| 620 |
test_hidden_interface_info (), |
| 621 |
&vtable, g_strdup (name), g_free, &error); |
| 622 |
if (error) |
| 623 |
{ |
| 624 |
g_critical ("Couldn't register new hidden: %s", error->message); |
| 625 |
g_clear_error (&error); |
| 626 |
} |
| 627 |
|
| 628 |
g_dbus_connection_emit_signal (connection, NULL, path, |
| 629 |
"com.redhat.Cockpit.DBusTests.Hidden", "Yooohooo", |
| 630 |
g_variant_new ("()"), &error); |
| 631 |
|
| 632 |
if (error) |
| 633 |
{ |
| 634 |
g_critical ("Couldn't emit signal on hidden: %s", error->message); |
| 635 |
g_clear_error (&error); |
| 636 |
} |
| 637 |
|
| 638 |
/* Now emit a signal from it */ |
| 639 |
test_frobber_complete_emit_hidden (frobber, invocation); |
| 640 |
g_free (path); |
| 641 |
return TRUE; |
| 642 |
} |
| 643 |
|
| 644 |
/* ---------------------------------------------------------------------------------------------------- |
| 645 |
* An Introspect() that actually fails |
| 646 |
*/ |
| 647 |
|
| 648 |
static void |
| 649 |
introspect_fail_method_call (GDBusConnection *connection, |
| 650 |
const gchar *sender, |
| 651 |
const gchar *object_path, |
| 652 |
const gchar *interface_name, |
| 653 |
const gchar *method_name, |
| 654 |
GVariant *parameters, |
| 655 |
GDBusMethodInvocation *invocation, |
| 656 |
gpointer user_data) |
| 657 |
{ |
| 658 |
const gchar *dbus_error = user_data; |
| 659 |
g_dbus_method_invocation_return_dbus_error (invocation, dbus_error, dbus_error); |
| 660 |
} |
| 661 |
|
| 662 |
static void |
| 663 |
mock_service_create_introspect_fail (GDBusConnection *connection) |
| 664 |
{ |
| 665 |
static const gchar introspectable_xml[] = |
| 666 |
"<node>" |
| 667 |
" <interface name=\"org.freedesktop.DBus.Introspectable\">" |
| 668 |
" <method name=\"Introspect\">" |
| 669 |
" <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>" |
| 670 |
" </method>" |
| 671 |
" </interface>" |
| 672 |
"</node>"; |
| 673 |
|
| 674 |
const GDBusInterfaceVTable introspect_vtable = { |
| 675 |
.method_call = introspect_fail_method_call, |
| 676 |
}; |
| 677 |
|
| 678 |
GDBusNodeInfo *node_info; |
| 679 |
GDBusInterfaceInfo *interface_info; |
| 680 |
GError *error = NULL; |
| 681 |
|
| 682 |
node_info = g_dbus_node_info_new_for_xml (introspectable_xml, &error); |
| 683 |
g_assert_no_error (error); |
| 684 |
|
| 685 |
interface_info = g_dbus_node_info_lookup_interface (node_info, "org.freedesktop.DBus.Introspectable"); |
| 686 |
g_assert (interface_info != NULL); |
| 687 |
|
| 688 |
/* Return a failure when introspecting this object path */ |
| 689 |
g_dbus_connection_register_object (connection, "/introspect/unknown", interface_info, &introspect_vtable, |
| 690 |
"org.freedesktop.DBus.Error.UnknownObject", NULL, &error); |
| 691 |
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) |
| 692 |
g_error_free (error); |
| 693 |
else |
| 694 |
g_assert_no_error (error); |
| 695 |
|
| 696 |
g_dbus_node_info_unref (node_info); |
| 697 |
} |
| 698 |
|
| 699 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 700 |
|
| 701 |
static void |
| 702 |
mock_data_free (gpointer data) |
| 703 |
{ |
| 704 |
MockData *mock_data = data; |
| 705 |
|
| 706 |
/* answer all NeverReturn() calls now, to avoid service shutdown hang */ |
| 707 |
for (GList *elem = mock_data->never_return_invocations; elem; elem = elem->next) |
| 708 |
g_dbus_method_invocation_return_value (elem->data, NULL); |
| 709 |
g_list_free_full (mock_data->never_return_invocations, g_object_unref); |
| 710 |
|
| 711 |
g_object_unref (mock_data->connection); |
| 712 |
g_hash_table_destroy (mock_data->other_names); |
| 713 |
g_free (mock_data); |
| 714 |
} |
| 715 |
|
| 716 |
GObject * |
| 717 |
mock_service_create_and_export (GDBusConnection *connection, |
| 718 |
const gchar *object_manager_path) |
| 719 |
{ |
| 720 |
GError *error; |
| 721 |
TestFrobber *exported_frobber; |
| 722 |
TestObjectSkeleton *exported_object; |
| 723 |
GDBusObjectManagerServer *object_manager; |
| 724 |
MockData *mock_data; |
| 725 |
gchar *path; |
| 726 |
|
| 727 |
mock_data = g_new0 (MockData, 1); |
| 728 |
mock_data->connection = g_object_ref (connection); |
| 729 |
mock_data->other_names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
| 730 |
|
| 731 |
/* Test that we can export an object using the generated |
| 732 |
* TestFrobberSkeleton subclass. Notes: |
| 733 |
* |
| 734 |
* 1. We handle methods by simply connecting to the appropriate |
| 735 |
* GObject signal. |
| 736 |
* |
| 737 |
* 2. Property storage is taken care of by the class; we can |
| 738 |
* use g_object_get()/g_object_set() (and the generated |
| 739 |
* C bindings at will) |
| 740 |
*/ |
| 741 |
error = NULL; |
| 742 |
exported_frobber = test_frobber_skeleton_new (); |
| 743 |
test_frobber_set_ay (exported_frobber, "ABCabc"); |
| 744 |
test_frobber_set_y (exported_frobber, 42); |
| 745 |
test_frobber_set_d (exported_frobber, 43.0); |
| 746 |
test_frobber_set_finally_normal_name (exported_frobber, "There aint no place like home"); |
| 747 |
test_frobber_set_writeonly_property (exported_frobber, "Mr. Burns"); |
| 748 |
test_frobber_set_readonly_property (exported_frobber, "blah"); |
| 749 |
|
| 750 |
object_manager = g_dbus_object_manager_server_new (object_manager_path); |
| 751 |
mock_data->object_manager = object_manager; |
| 752 |
g_object_set_data_full (G_OBJECT (object_manager), "mock-data", mock_data, mock_data_free); |
| 753 |
|
| 754 |
path = g_strdup_printf ("%s/frobber", object_manager_path); |
| 755 |
exported_object = test_object_skeleton_new (path); |
| 756 |
g_free (path); |
| 757 |
|
| 758 |
test_object_skeleton_set_frobber (exported_object, exported_frobber); |
| 759 |
g_dbus_object_manager_server_export (object_manager, G_DBUS_OBJECT_SKELETON (exported_object)); |
| 760 |
g_object_unref (exported_object); |
| 761 |
|
| 762 |
g_dbus_object_manager_server_set_connection (object_manager, connection); |
| 763 |
|
| 764 |
g_assert_no_error (error); |
| 765 |
g_signal_connect (exported_frobber, "handle-hello-world", |
| 766 |
G_CALLBACK (on_handle_hello_world), NULL); |
| 767 |
g_signal_connect (exported_frobber, "handle-never-return", |
| 768 |
G_CALLBACK (on_handle_never_return), mock_data); |
| 769 |
g_signal_connect (exported_frobber, |
| 770 |
"handle-test-primitive-types", |
| 771 |
G_CALLBACK (on_handle_test_primitive_types), NULL); |
| 772 |
g_signal_connect (exported_frobber, |
| 773 |
"handle-test-non-primitive-types", |
| 774 |
G_CALLBACK (on_handle_test_non_primitive_types), NULL); |
| 775 |
g_signal_connect (exported_frobber, |
| 776 |
"handle-test-variant", |
| 777 |
G_CALLBACK (on_handle_test_variant), NULL); |
| 778 |
g_signal_connect (exported_frobber, |
| 779 |
"handle-request-signal-emission", |
| 780 |
G_CALLBACK (on_handle_request_signal_emission), NULL); |
| 781 |
g_signal_connect (exported_frobber, |
| 782 |
"handle-request-property-mods", |
| 783 |
G_CALLBACK (on_handle_request_property_mods), NULL); |
| 784 |
g_signal_connect (exported_frobber, |
| 785 |
"handle-request-multi-property-mods", |
| 786 |
G_CALLBACK (on_handle_request_multi_property_mods), NULL); |
| 787 |
g_signal_connect (exported_frobber, |
| 788 |
"handle-property-cancellation", |
| 789 |
G_CALLBACK (on_handle_property_cancellation), NULL); |
| 790 |
g_signal_connect (exported_frobber, |
| 791 |
"handle-delete-all-objects", |
| 792 |
G_CALLBACK (on_handle_delete_all_objects), mock_data); |
| 793 |
g_signal_connect (exported_frobber, |
| 794 |
"handle-create-object", |
| 795 |
G_CALLBACK (on_handle_create_object), mock_data); |
| 796 |
g_signal_connect (exported_frobber, |
| 797 |
"handle-delete-object", |
| 798 |
G_CALLBACK (on_handle_delete_object), mock_data); |
| 799 |
g_signal_connect (exported_frobber, |
| 800 |
"handle-test-asv", |
| 801 |
G_CALLBACK (on_handle_test_asv), NULL); |
| 802 |
g_signal_connect (exported_frobber, |
| 803 |
"handle-add-alpha", |
| 804 |
G_CALLBACK (on_handle_add_alpha), NULL); |
| 805 |
g_signal_connect (exported_frobber, |
| 806 |
"handle-remove-alpha", |
| 807 |
G_CALLBACK (on_handle_remove_alpha), NULL); |
| 808 |
g_signal_connect (exported_frobber, "handle-create-clique", |
| 809 |
G_CALLBACK (on_create_clique), NULL); |
| 810 |
g_signal_connect (exported_frobber, "handle-emit-hidden", |
| 811 |
G_CALLBACK (on_emit_hidden), NULL); |
| 812 |
|
| 813 |
g_signal_connect (exported_frobber, "handle-claim-other-name", |
| 814 |
G_CALLBACK (on_claim_other_name), mock_data); |
| 815 |
g_signal_connect (exported_frobber, "handle-release-other-name", |
| 816 |
G_CALLBACK (on_release_other_name), mock_data); |
| 817 |
g_signal_connect (exported_frobber, "handle-tell-me-your-name", |
| 818 |
G_CALLBACK (on_tell_me_your_name), mock_data); |
| 819 |
g_signal_connect (exported_frobber, "handle-make-test-fd", |
| 820 |
G_CALLBACK (on_make_test_fd), mock_data); |
| 821 |
|
| 822 |
g_object_unref (exported_frobber); |
| 823 |
mock_service_create_introspect_fail (connection); |
| 824 |
return G_OBJECT (object_manager); |
| 825 |
} |