summaryrefslogtreecommitdiffstats
path: root/test/glib/test-dbus-glib.c
blob: fb8be4cce3f8dec1647895503fb30f374c80c4eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* -*- mode: C; c-file-style: "gnu" -*- */
#include "dbus-glib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main (int argc, char **argv)
{
  DBusConnection *connection;
  GMainLoop *loop;
  GError *error;
  DBusGProxy *driver;
  DBusGProxy *proxy;
  DBusPendingCall *call;
  char **service_list;
  int service_list_len;
  int i;
  dbus_uint32_t result;
  char *str;
  
  g_type_init ();
  
  loop = g_main_loop_new (NULL, FALSE);

  error = NULL;
  connection = dbus_bus_get_with_g_main (DBUS_BUS_SESSION,
                                         &error);
  if (connection == NULL)
    {
      g_printerr ("Failed to open connection to bus: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }

  /* Create a proxy object for the "bus driver" */
  
  driver = dbus_gproxy_new_for_service (connection,
                                        DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
                                        DBUS_PATH_ORG_FREEDESKTOP_DBUS,
                                        DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);

  /* Call ListServices method */
  
  call = dbus_gproxy_begin_call (driver, "ListServices", DBUS_TYPE_INVALID);

  error = NULL;
  if (!dbus_gproxy_end_call (driver, call, &error,
                             DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
                             &service_list, &service_list_len,
                             DBUS_TYPE_INVALID))
    {
      g_printerr ("Failed to complete ListServices call: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }

  g_print ("Services on the message bus:\n");
  i = 0;
  while (i < service_list_len)
    {
      g_assert (service_list[i] != NULL);
      g_print ("  %s\n", service_list[i]);
      ++i;
    }
  g_assert (service_list[i] == NULL);
  
  dbus_free_string_array (service_list);

  /* Test handling of unknown method */
  call = dbus_gproxy_begin_call (driver, "ThisMethodDoesNotExist",
                                 DBUS_TYPE_STRING,
                                 "blah blah blah blah blah",
                                 DBUS_TYPE_INT32,
                                 10,
                                 DBUS_TYPE_INVALID);

  error = NULL;
  if (dbus_gproxy_end_call (driver, call, &error,
                            DBUS_TYPE_INVALID))
    {
      g_printerr ("Calling nonexistent method succeeded!\n");
      exit (1);
    }

  g_print ("Got EXPECTED error from calling unknown method: %s\n",
           error->message);
  g_error_free (error);
  
  /* Activate a service */
  call = dbus_gproxy_begin_call (driver, "ActivateService",
                                 DBUS_TYPE_STRING,
                                 "org.freedesktop.DBus.TestSuiteEchoService",
                                 DBUS_TYPE_UINT32,
                                 0,
                                 DBUS_TYPE_INVALID);

  error = NULL;
  if (!dbus_gproxy_end_call (driver, call, &error,
                             DBUS_TYPE_UINT32, &result,
                             DBUS_TYPE_INVALID))
    {
      g_printerr ("Failed to complete Activate call: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }

  g_print ("Activation of echo service = 0x%x\n", result);

  /* Activate a service again */
  call = dbus_gproxy_begin_call (driver, "ActivateService",
                                 DBUS_TYPE_STRING,
                                 "org.freedesktop.DBus.TestSuiteEchoService",
                                 DBUS_TYPE_UINT32,
                                 0,
                                 DBUS_TYPE_INVALID);

  error = NULL;
  if (!dbus_gproxy_end_call (driver, call, &error,
                             DBUS_TYPE_UINT32, &result,
                             DBUS_TYPE_INVALID))
    {
      g_printerr ("Failed to complete Activate call: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }

  g_print ("Duplicate activation of echo service = 0x%x\n", result);

  /* Talk to the new service */
  
  proxy = dbus_gproxy_new_for_service (connection,
                                       "org.freedesktop.DBus.TestSuiteEchoService",
                                       "/fixme/the/test/service/ignores/this", /* FIXME */
                                       "org.freedesktop.TestSuite");
  
  call = dbus_gproxy_begin_call (proxy, "Echo",
                                 DBUS_TYPE_STRING,
                                 "my string hello",
                                 DBUS_TYPE_INVALID);

  error = NULL;
  if (!dbus_gproxy_end_call (proxy, call, &error,
                             DBUS_TYPE_STRING, &str,
                             DBUS_TYPE_INVALID))
    {
      g_printerr ("Failed to complete Echo call: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }

  g_print ("String echoed = \"%s\"\n", str);
  dbus_free (str);
  
  g_object_unref (G_OBJECT (driver));
  g_object_unref (G_OBJECT (proxy));
  
  g_print ("Successfully completed %s\n", argv[0]);
  
  return 0;
}