summaryrefslogtreecommitdiff
path: root/gio/tests/gdbus-proxy-well-known-name.c
blob: db2e5cc1d6505a43596bcbf73bdae83a8aca9ef8 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* GLib testing framework examples and tests
 *
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include <gio/gio.h>
#include <unistd.h>
#include <string.h>

#include "gdbus-tests.h"

/* all tests rely on a shared mainloop */
static GMainLoop *loop = NULL;

/* ---------------------------------------------------------------------------------------------------- */

static void
proxy_new_cb (GObject       *source_object,
              GAsyncResult  *res,
              gpointer       user_data)
{
  GDBusProxy **ret = user_data;
  GError *error;

  error = NULL;
  *ret = g_dbus_proxy_new_finish (res, &error);
  g_assert_no_error (error);
  g_assert (ret != NULL);

  g_main_loop_quit (loop);
}

static void
test_proxy_well_known_name (void)
{
  GDBusProxy *p;
  GDBusProxy *p2;
  GDBusProxy *ap;
  GDBusProxy *ap2;
  GDBusConnection *c;
  GError *error;
  gchar *name_owner;
  gchar **property_names;
  GVariant *variant;
  GVariant *result;
  gchar *path;

  session_bus_up ();

  error = NULL;
  c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
  g_assert_no_error (error);
  g_assert (c != NULL);

  error = NULL;
  p = g_dbus_proxy_new_sync (c,
                             G_DBUS_PROXY_FLAGS_NONE,
                             NULL,                      /* GDBusInterfaceInfo* */
                             "com.example.TestService", /* name */
                             "/com/example/TestObject", /* object path */
                             "com.example.Frob",        /* interface name */
                             NULL,                      /* GCancellable */
                             &error);
  g_assert_no_error (error);

  /* we shouldn't have a name owner nor any cached properties */
  g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
  g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);

  /* also for async: we shouldn't have a name owner nor any cached properties */
  g_dbus_proxy_new (c,
                    G_DBUS_PROXY_FLAGS_NONE,
                    NULL,                      /* GDBusInterfaceInfo* */
                    "com.example.TestService", /* name */
                    "/com/example/TestObject", /* object path */
                    "com.example.Frob",        /* interface name */
                    NULL,                      /* GCancellable */
                    (GAsyncReadyCallback) proxy_new_cb,
                    &ap);
  g_main_loop_run (loop);
  g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
  g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);

  /* this is safe; testserver will exit once the bus goes away */
  path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
  g_assert (g_spawn_command_line_async (path, NULL));
  g_free (path);

  /* check that we get the notify::g-name-owner signal */
  _g_assert_property_notify (p, "g-name-owner");

  /* Now we should have a name owner as well as properties */
  name_owner = g_dbus_proxy_get_name_owner (p);
  property_names = g_dbus_proxy_get_cached_property_names (p);
  g_assert (g_dbus_is_unique_name (name_owner));
  g_assert (property_names != NULL && g_strv_length (property_names) > 0);
  g_free (name_owner);
  g_strfreev (property_names);

  /* if we create another proxy with the service being available, check that
   * it has a name owner and properties
   */
  error = NULL;
  p2 = g_dbus_proxy_new_sync (c,
                              G_DBUS_PROXY_FLAGS_NONE,
                              NULL,                      /* GDBusInterfaceInfo* */
                              "com.example.TestService", /* name */
                              "/com/example/TestObject", /* object path */
                              "com.example.Frob",        /* interface name */
                              NULL,                      /* GCancellable */
                              &error);
  g_assert_no_error (error);
  name_owner = g_dbus_proxy_get_name_owner (p2);
  property_names = g_dbus_proxy_get_cached_property_names (p2);
  g_assert (g_dbus_is_unique_name (name_owner));
  g_assert (property_names != NULL && g_strv_length (property_names) > 0);
  g_free (name_owner);
  g_strfreev (property_names);

  /* also for async: we should have a name owner and cached properties */
  g_dbus_proxy_new (c,
                    G_DBUS_PROXY_FLAGS_NONE,
                    NULL,                      /* GDBusInterfaceInfo* */
                    "com.example.TestService", /* name */
                    "/com/example/TestObject", /* object path */
                    "com.example.Frob",        /* interface name */
                    NULL,                      /* GCancellable */
                    (GAsyncReadyCallback) proxy_new_cb,
                    &ap2);
  g_main_loop_run (loop);
  name_owner = g_dbus_proxy_get_name_owner (ap2);
  property_names = g_dbus_proxy_get_cached_property_names (ap2);
  g_assert (g_dbus_is_unique_name (name_owner));
  g_assert (property_names != NULL && g_strv_length (property_names) > 0);
  g_free (name_owner);
  g_strfreev (property_names);

  /* Check property value is the initial value */
  variant = g_dbus_proxy_get_cached_property (p, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
  g_variant_unref (variant);
  variant = g_dbus_proxy_get_cached_property (p2, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
  g_variant_unref (variant);
  variant = g_dbus_proxy_get_cached_property (ap, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
  g_variant_unref (variant);
  variant = g_dbus_proxy_get_cached_property (ap2, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
  g_variant_unref (variant);

  /* Check that properties are updated on both p and p2 */
  result = g_dbus_proxy_call_sync (p,
                                   "FrobSetProperty",
                                   g_variant_new ("(sv)",
                                                  "y",
                                                  g_variant_new_byte (42)),
                                   G_DBUS_CALL_FLAGS_NONE,
                                   -1,
                                   NULL,
                                   &error);
  g_assert_no_error (error);
  g_assert (result != NULL);
  g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
  g_variant_unref (result);
  _g_assert_signal_received (p, "g-properties-changed");
  variant = g_dbus_proxy_get_cached_property (p, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
  g_variant_unref (variant);
  variant = g_dbus_proxy_get_cached_property (p2, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
  g_variant_unref (variant);
  variant = g_dbus_proxy_get_cached_property (ap, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
  g_variant_unref (variant);
  variant = g_dbus_proxy_get_cached_property (ap2, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
  g_variant_unref (variant);

  /* Nuke the service and check that we get the signal and then don't
   * have a name owner nor any cached properties
   */
  result = g_dbus_proxy_call_sync (p,
                                   "Quit",
                                   NULL,
                                   G_DBUS_CALL_FLAGS_NONE,
                                   -1,
                                   NULL,
                                   &error);
  g_assert_no_error (error);
  g_assert (result != NULL);
  g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
  g_variant_unref (result);
  /* and wait... */
  _g_assert_property_notify (p, "g-name-owner");
  /* now we shouldn't have a name owner nor any cached properties */
  g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
  g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
  g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);

  /* now bring back the server and wait for the proxy to be updated.. now
   * the 'y' property should be back at 1...
   */
  /* this is safe; testserver will exit once the bus goes away */
  path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
  g_assert (g_spawn_command_line_async (path, NULL));
  g_free (path);

  /* check that we get the notify::g-name-owner signal */
  _g_assert_property_notify (p, "g-name-owner");
  /* Now we should have a name owner as well as properties */
  name_owner = g_dbus_proxy_get_name_owner (p);
  property_names = g_dbus_proxy_get_cached_property_names (p);
  g_assert (g_dbus_is_unique_name (name_owner));
  g_assert (property_names != NULL && g_strv_length (property_names) > 0);
  g_free (name_owner);
  g_strfreev (property_names);
  /* and finally check the 'y' property */
  variant = g_dbus_proxy_get_cached_property (p, "y");
  g_assert (variant != NULL);
  g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
  g_variant_unref (variant);

  g_object_unref (p2);
  g_object_unref (p);
  g_object_unref (ap2);
  g_object_unref (ap);

  g_object_unref (c);

  /* tear down bus */
  session_bus_down ();
}

/* ---------------------------------------------------------------------------------------------------- */

int
main (int   argc,
      char *argv[])
{
  gint ret;

  g_test_init (&argc, &argv, NULL);

  /* all the tests rely on a shared main loop */
  loop = g_main_loop_new (NULL, FALSE);

  g_test_dbus_unset ();

  g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);

  ret = g_test_run();
  return ret;
}