test-privserver-client.c revision 0e2a099b55a50b9b5a0f948e134b5a88e2924262
15e359834f28b0580e7b79a057614d4e3592709acColin Walters#include "../test-utils.h" 25e359834f28b0580e7b79a057614d4e3592709acColin Walters 35e359834f28b0580e7b79a057614d4e3592709acColin Waltersstatic void 45e359834f28b0580e7b79a057614d4e3592709acColin Waltersdie (const char *message, ...) 55e359834f28b0580e7b79a057614d4e3592709acColin Walters{ 65e359834f28b0580e7b79a057614d4e3592709acColin Walters va_list args; 75e359834f28b0580e7b79a057614d4e3592709acColin Walters va_start (args, message); 85e359834f28b0580e7b79a057614d4e3592709acColin Walters vfprintf (stderr, message, args); 95e359834f28b0580e7b79a057614d4e3592709acColin Walters va_end (args); 105e359834f28b0580e7b79a057614d4e3592709acColin Walters exit (1); 115e359834f28b0580e7b79a057614d4e3592709acColin Walters} 125e359834f28b0580e7b79a057614d4e3592709acColin Walters 135e359834f28b0580e7b79a057614d4e3592709acColin Waltersstatic DBusHandlerResult 145e359834f28b0580e7b79a057614d4e3592709acColin Waltersfilter_private_message (DBusConnection *connection, 155e359834f28b0580e7b79a057614d4e3592709acColin Walters DBusMessage *message, 165e359834f28b0580e7b79a057614d4e3592709acColin Walters void *user_data) 175e359834f28b0580e7b79a057614d4e3592709acColin Walters{ 185e359834f28b0580e7b79a057614d4e3592709acColin Walters if (dbus_message_is_signal (message, 195e359834f28b0580e7b79a057614d4e3592709acColin Walters DBUS_INTERFACE_LOCAL, 205e359834f28b0580e7b79a057614d4e3592709acColin Walters "Disconnected")) 215e359834f28b0580e7b79a057614d4e3592709acColin Walters { 225e359834f28b0580e7b79a057614d4e3592709acColin Walters DBusLoop *loop = user_data; 235e359834f28b0580e7b79a057614d4e3592709acColin Walters _dbus_loop_quit (loop); 245e359834f28b0580e7b79a057614d4e3592709acColin Walters return DBUS_HANDLER_RESULT_HANDLED; 255e359834f28b0580e7b79a057614d4e3592709acColin Walters } 265e359834f28b0580e7b79a057614d4e3592709acColin Walters return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 275e359834f28b0580e7b79a057614d4e3592709acColin Walters} 285e359834f28b0580e7b79a057614d4e3592709acColin Walters 295e359834f28b0580e7b79a057614d4e3592709acColin Waltersstatic void 300e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Waltersopen_shutdown_private_connection (dbus_bool_t use_guid) 315e359834f28b0580e7b79a057614d4e3592709acColin Walters{ 325e359834f28b0580e7b79a057614d4e3592709acColin Walters DBusError error; 335e359834f28b0580e7b79a057614d4e3592709acColin Walters DBusLoop *loop; 345e359834f28b0580e7b79a057614d4e3592709acColin Walters DBusConnection *session; 355e359834f28b0580e7b79a057614d4e3592709acColin Walters DBusMessage *msg; 365e359834f28b0580e7b79a057614d4e3592709acColin Walters DBusMessage *reply; 375e359834f28b0580e7b79a057614d4e3592709acColin Walters DBusConnection *privconn; 380e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters char *addr; 390e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters char *comma; 405e359834f28b0580e7b79a057614d4e3592709acColin Walters 415e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_error_init (&error); 425e359834f28b0580e7b79a057614d4e3592709acColin Walters 435e359834f28b0580e7b79a057614d4e3592709acColin Walters loop = _dbus_loop_new (); 445e359834f28b0580e7b79a057614d4e3592709acColin Walters 455e359834f28b0580e7b79a057614d4e3592709acColin Walters session = dbus_bus_get (DBUS_BUS_SESSION, &error); 465e359834f28b0580e7b79a057614d4e3592709acColin Walters if (!session) 475e359834f28b0580e7b79a057614d4e3592709acColin Walters die ("couldn't access session bus\n"); 485e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_connection_set_exit_on_disconnect (session, FALSE); 495e359834f28b0580e7b79a057614d4e3592709acColin Walters msg = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuite.TestServer", 505e359834f28b0580e7b79a057614d4e3592709acColin Walters "/", 515e359834f28b0580e7b79a057614d4e3592709acColin Walters "org.freedesktop.DBus.TestSuite.TestServer", 525e359834f28b0580e7b79a057614d4e3592709acColin Walters "GetPrivateAddress"); 535e359834f28b0580e7b79a057614d4e3592709acColin Walters if (!(reply = dbus_connection_send_with_reply_and_block (session, msg, -1, &error))) 545e359834f28b0580e7b79a057614d4e3592709acColin Walters die ("couldn't send message: %s\n", error.message); 555e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_message_unref (msg); 565e359834f28b0580e7b79a057614d4e3592709acColin Walters if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &addr, DBUS_TYPE_INVALID)) 575e359834f28b0580e7b79a057614d4e3592709acColin Walters die ("couldn't parse message replym\n"); 585e359834f28b0580e7b79a057614d4e3592709acColin Walters printf ("got private temp address %s\n", addr); 590e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters addr = strdup (addr); 600e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters if (!use_guid) 610e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters { 620e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters char *comma = strrchr (addr, ','); 630e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters if (comma) 640e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters *comma = '\0'; 650e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters } 665e359834f28b0580e7b79a057614d4e3592709acColin Walters privconn = dbus_connection_open (addr, &error); 670e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters free (addr); 685e359834f28b0580e7b79a057614d4e3592709acColin Walters if (!privconn) 695e359834f28b0580e7b79a057614d4e3592709acColin Walters die ("couldn't connect to server direct connection: %s\n", error.message); 705e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_message_unref (reply); 715e359834f28b0580e7b79a057614d4e3592709acColin Walters 725e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_connection_set_exit_on_disconnect (privconn, FALSE); 735e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_connection_add_filter (privconn, filter_private_message, loop, NULL); 745e359834f28b0580e7b79a057614d4e3592709acColin Walters test_connection_setup (loop, privconn); 755e359834f28b0580e7b79a057614d4e3592709acColin Walters 765e359834f28b0580e7b79a057614d4e3592709acColin Walters msg = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuite.TestServer", 775e359834f28b0580e7b79a057614d4e3592709acColin Walters "/", 785e359834f28b0580e7b79a057614d4e3592709acColin Walters "org.freedesktop.DBus.TestSuite.TestServer", 795e359834f28b0580e7b79a057614d4e3592709acColin Walters "Quit"); 805e359834f28b0580e7b79a057614d4e3592709acColin Walters if (!dbus_connection_send (session, msg, NULL)) 815e359834f28b0580e7b79a057614d4e3592709acColin Walters die ("couldn't send Quit message\n"); 825e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_message_unref (msg); 835e359834f28b0580e7b79a057614d4e3592709acColin Walters 845e359834f28b0580e7b79a057614d4e3592709acColin Walters _dbus_loop_run (loop); 855e359834f28b0580e7b79a057614d4e3592709acColin Walters 865e359834f28b0580e7b79a057614d4e3592709acColin Walters test_connection_shutdown (loop, session); 875e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_connection_unref (session); 885e359834f28b0580e7b79a057614d4e3592709acColin Walters 895e359834f28b0580e7b79a057614d4e3592709acColin Walters test_connection_shutdown (loop, privconn); 905e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_connection_remove_filter (privconn, filter_private_message, loop); 915e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_connection_unref (privconn); 925e359834f28b0580e7b79a057614d4e3592709acColin Walters 935e359834f28b0580e7b79a057614d4e3592709acColin Walters _dbus_loop_unref (loop); 945e359834f28b0580e7b79a057614d4e3592709acColin Walters} 955e359834f28b0580e7b79a057614d4e3592709acColin Walters 965e359834f28b0580e7b79a057614d4e3592709acColin Waltersint 975e359834f28b0580e7b79a057614d4e3592709acColin Waltersmain (int argc, char *argv[]) 985e359834f28b0580e7b79a057614d4e3592709acColin Walters{ 990e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters open_shutdown_private_connection (TRUE); 1000e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters 1010e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters dbus_shutdown (); 1020e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters 1030e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters open_shutdown_private_connection (TRUE); 1040e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters 1050e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters dbus_shutdown (); 1060e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters 1070e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters open_shutdown_private_connection (FALSE); 1085e359834f28b0580e7b79a057614d4e3592709acColin Walters 1095e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_shutdown (); 1105e359834f28b0580e7b79a057614d4e3592709acColin Walters 1110e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters open_shutdown_private_connection (FALSE); 1125e359834f28b0580e7b79a057614d4e3592709acColin Walters 1135e359834f28b0580e7b79a057614d4e3592709acColin Walters dbus_shutdown (); 1145e359834f28b0580e7b79a057614d4e3592709acColin Walters 1155e359834f28b0580e7b79a057614d4e3592709acColin Walters return 0; 1165e359834f28b0580e7b79a057614d4e3592709acColin Walters} 117