os/ossrv/ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-shell-service.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-shell-service.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,195 @@
     1.4 +/* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
     1.5 +#include "test-utils.h"
     1.6 +
     1.7 +static DBusLoop *loop;
     1.8 +static dbus_bool_t already_quit = FALSE;
     1.9 +static const char* echo_path = "/org/freedesktop/TestSuite";
    1.10 +
    1.11 +typedef struct
    1.12 +{
    1.13 +  int argc;
    1.14 +  char **argv;
    1.15 +} EchoData;
    1.16 +
    1.17 +static void
    1.18 +quit (void)
    1.19 +{
    1.20 +  if (!already_quit)
    1.21 +    {
    1.22 +      _dbus_loop_quit (loop);
    1.23 +      already_quit = TRUE;
    1.24 +    }
    1.25 +}
    1.26 +
    1.27 +static void
    1.28 +die (const char *message)
    1.29 +{
    1.30 +  fprintf (stderr, "*** test-service: %s", message);
    1.31 +  exit (1);
    1.32 +}
    1.33 +
    1.34 +static DBusHandlerResult
    1.35 +handle_echo (DBusConnection     *connection,
    1.36 +             DBusMessage        *message)
    1.37 +{
    1.38 +  DBusError error;
    1.39 +  DBusMessage *reply;
    1.40 +  DBusMessageIter iter;
    1.41 +  int i;
    1.42 +  EchoData *d;
    1.43 +
    1.44 +  _dbus_verbose ("sending reply to Echo method\n");
    1.45 +
    1.46 +  if (!dbus_connection_get_object_path_data (connection, echo_path, (void **)&d))
    1.47 +      die ("No memory");
    1.48 +
    1.49 +
    1.50 +  dbus_error_init (&error);
    1.51 +
    1.52 +  reply = dbus_message_new_method_return (message);
    1.53 +  if (reply == NULL)
    1.54 +    die ("No memory\n");
    1.55 +  
    1.56 +  dbus_message_iter_init_append (reply, &iter);
    1.57 +  for (i = 0; i < d->argc; ++i)
    1.58 +    if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &(d->argv[i])))
    1.59 +      die ("No memory\n");
    1.60 +
    1.61 +  if (!dbus_connection_send (connection, reply, NULL))
    1.62 +    die ("No memory\n");
    1.63 +
    1.64 +  fprintf (stderr, "Shell echo service echoed the command line\n");
    1.65 +  
    1.66 +  dbus_message_unref (reply);
    1.67 +    
    1.68 +  return DBUS_HANDLER_RESULT_HANDLED;
    1.69 +}
    1.70 +
    1.71 +static void
    1.72 +path_unregistered_func (DBusConnection  *connection,
    1.73 +                        void            *user_data)
    1.74 +{
    1.75 +  /* connection was finalized */
    1.76 +}
    1.77 +
    1.78 +static DBusHandlerResult
    1.79 +path_message_func (DBusConnection  *connection,
    1.80 +                   DBusMessage     *message,
    1.81 +                   void            *user_data)
    1.82 +{
    1.83 +  if (dbus_message_is_method_call (message,
    1.84 +                                   "org.freedesktop.TestSuite",
    1.85 +                                   "Echo"))
    1.86 +    return handle_echo (connection, message);
    1.87 +  else if (dbus_message_is_method_call (message,
    1.88 +                                        "org.freedesktop.TestSuite",
    1.89 +                                        "Exit"))
    1.90 +    {
    1.91 +      quit ();
    1.92 +      return DBUS_HANDLER_RESULT_HANDLED;
    1.93 +    }
    1.94 +  else
    1.95 +    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    1.96 +}
    1.97 +
    1.98 +static DBusObjectPathVTable
    1.99 +echo_vtable = {
   1.100 +  path_unregistered_func,
   1.101 +  path_message_func,
   1.102 +  NULL,
   1.103 +};
   1.104 +
   1.105 +static DBusHandlerResult
   1.106 +filter_func (DBusConnection     *connection,
   1.107 +             DBusMessage        *message,
   1.108 +             void               *user_data)
   1.109 +{
   1.110 +  if (dbus_message_is_signal (message,
   1.111 +                              DBUS_INTERFACE_LOCAL,
   1.112 +                              "Disconnected"))
   1.113 +    {
   1.114 +      quit ();
   1.115 +      return DBUS_HANDLER_RESULT_HANDLED;
   1.116 +    }
   1.117 +  else
   1.118 +    {
   1.119 +      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
   1.120 +    }
   1.121 +}
   1.122 +
   1.123 +int
   1.124 +main (int    argc,
   1.125 +      char **argv)
   1.126 +{
   1.127 +  DBusConnection *connection;
   1.128 +  DBusError error;
   1.129 +  EchoData echo_data;
   1.130 +  int result;
   1.131 +  
   1.132 +  echo_data.argc = argc;
   1.133 +  echo_data.argv = argv;
   1.134 +  
   1.135 +  dbus_error_init (&error);
   1.136 +  connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
   1.137 +  if (connection == NULL)
   1.138 +    {
   1.139 +      fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
   1.140 +               error.message);
   1.141 +      dbus_error_free (&error);
   1.142 +      return 1;
   1.143 +    }
   1.144 +
   1.145 +  loop = _dbus_loop_new ();
   1.146 +  if (loop == NULL)
   1.147 +    die ("No memory\n");
   1.148 +  
   1.149 +  if (!test_connection_setup (loop, connection))
   1.150 +    die ("No memory\n");
   1.151 +
   1.152 +  if (!dbus_connection_add_filter (connection,
   1.153 +                                   filter_func, NULL, NULL))
   1.154 +    die ("No memory");
   1.155 +
   1.156 +  if (!dbus_connection_register_object_path (connection,
   1.157 +                                             echo_path,
   1.158 +                                             &echo_vtable,
   1.159 +                                             (void*) &echo_data))
   1.160 +    die ("No memory");
   1.161 +
   1.162 +  {
   1.163 +    void *d;
   1.164 +    if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
   1.165 +      die ("No memory");
   1.166 +    if (d != (void*) &echo_data)
   1.167 +      die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
   1.168 +  }
   1.169 +  
   1.170 +  result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess",
   1.171 +                                  0, &error);
   1.172 +  if (dbus_error_is_set (&error))
   1.173 +    {
   1.174 +      fprintf (stderr, "Error %s\n", error.message);
   1.175 +      _dbus_verbose ("*** Failed to acquire service: %s\n",
   1.176 +                     error.message);
   1.177 +      dbus_error_free (&error);
   1.178 +      exit (1);
   1.179 +    }
   1.180 +
   1.181 +  _dbus_verbose ("*** Test service entering main loop\n");
   1.182 +  _dbus_loop_run (loop);
   1.183 +
   1.184 +  test_connection_shutdown (loop, connection);
   1.185 +
   1.186 +  dbus_connection_remove_filter (connection, filter_func, NULL);
   1.187 +  
   1.188 +  dbus_connection_unref (connection);
   1.189 +
   1.190 +  _dbus_loop_unref (loop);
   1.191 +  loop = NULL;
   1.192 +  
   1.193 +  dbus_shutdown ();
   1.194 +
   1.195 +  _dbus_verbose ("*** Test service exiting\n");
   1.196 +  
   1.197 +  return 0;
   1.198 +}