• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Cosimo Cecchi <cosimoc@gnome.org>
19  */
20 
21 #include <gio/gio.h>
22 #include <unistd.h>
23 #include <string.h>
24 
25 #include "gdbus-tests.h"
26 
27 /* all tests rely on a shared mainloop */
28 static GMainLoop *loop = NULL;
29 
30 /* ---------------------------------------------------------------------------------------------------- */
31 
32 static void
proxy_new_cb(GObject * source_object,GAsyncResult * res,gpointer user_data)33 proxy_new_cb (GObject       *source_object,
34               GAsyncResult  *res,
35               gpointer       user_data)
36 {
37   GDBusProxy **ret = user_data;
38   GError *error;
39 
40   error = NULL;
41   *ret = g_dbus_proxy_new_finish (res, &error);
42   g_assert_no_error (error);
43   g_assert_nonnull (ret);
44 
45   g_main_loop_quit (loop);
46 }
47 
48 static void
test_proxy_unique_name(void)49 test_proxy_unique_name (void)
50 {
51   GDBusProxy *wp;
52   GDBusProxy *p;
53   GDBusProxy *ap;
54   GDBusConnection *c;
55   GError *error;
56   gchar *name_owner;
57   gchar **property_names;
58   GVariant *variant;
59   GVariant *result;
60   char *unique_name;
61 
62   session_bus_up ();
63 
64   error = NULL;
65   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
66   g_assert_no_error (error);
67   g_assert_nonnull (c);
68 
69   /* use a proxy to the well-known name to set things up */
70   wp = g_dbus_proxy_new_sync (c,
71                               G_DBUS_PROXY_FLAGS_NONE,
72                               NULL,                      /* GDBusInterfaceInfo* */
73                               "com.example.TestService", /* name */
74                               "/com/example/TestObject", /* object path */
75                               "com.example.Frob",        /* interface name */
76                               NULL,                      /* GCancellable */
77                               &error);
78   g_assert_no_error (error);
79 
80   /* this is safe; testserver will exit once the bus goes away */
81   g_assert_true (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
82 
83   /* check that we get the notify::g-name-owner signal */
84   _g_assert_property_notify (wp, "g-name-owner");
85 
86   /* now get the unique name of testserver's connection */
87   unique_name = g_dbus_proxy_get_name_owner (wp);
88 
89   /* if we create another a proxy with the service being available, check that
90    * it has a name owner and properties
91    */
92   error = NULL;
93   p = g_dbus_proxy_new_sync (c,
94                               G_DBUS_PROXY_FLAGS_NONE,
95                               NULL,                      /* GDBusInterfaceInfo* */
96                               unique_name,               /* name */
97                               "/com/example/TestObject", /* object path */
98                               "com.example.Frob",        /* interface name */
99                               NULL,                      /* GCancellable */
100                               &error);
101   g_assert_no_error (error);
102   name_owner = g_dbus_proxy_get_name_owner (p);
103   property_names = g_dbus_proxy_get_cached_property_names (p);
104   g_assert_true (g_dbus_is_unique_name (name_owner));
105   g_assert_nonnull (property_names);
106   g_assert_cmpint (g_strv_length (property_names), >, 0);
107   g_free (name_owner);
108   g_strfreev (property_names);
109 
110   /* also for async: we should have a name owner and cached properties */
111   g_dbus_proxy_new (c,
112                     G_DBUS_PROXY_FLAGS_NONE,
113                     NULL,                      /* GDBusInterfaceInfo* */
114                     unique_name,               /* name */
115                     "/com/example/TestObject", /* object path */
116                     "com.example.Frob",        /* interface name */
117                     NULL,                      /* GCancellable */
118                     (GAsyncReadyCallback) proxy_new_cb,
119                     &ap);
120   g_main_loop_run (loop);
121   name_owner = g_dbus_proxy_get_name_owner (ap);
122   property_names = g_dbus_proxy_get_cached_property_names (ap);
123   g_assert_true (g_dbus_is_unique_name (name_owner));
124   g_assert_nonnull (property_names);
125   g_assert_cmpint (g_strv_length (property_names), >, 0);
126   g_free (name_owner);
127   g_strfreev (property_names);
128 
129   /* Check property value is the initial value */
130   variant = g_dbus_proxy_get_cached_property (p, "y");
131   g_assert_nonnull (variant);
132   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
133   g_variant_unref (variant);
134   variant = g_dbus_proxy_get_cached_property (ap, "y");
135   g_assert_nonnull (variant);
136   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
137   g_variant_unref (variant);
138 
139   /* Check that properties are updated on p */
140   result = g_dbus_proxy_call_sync (p,
141                                    "FrobSetProperty",
142                                    g_variant_new ("(sv)",
143                                                   "y",
144                                                   g_variant_new_byte (42)),
145                                    G_DBUS_CALL_FLAGS_NONE,
146                                    -1,
147                                    NULL,
148                                    &error);
149   g_assert_no_error (error);
150   g_assert_nonnull (result);
151   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
152   g_variant_unref (result);
153   _g_assert_signal_received (p, "g-properties-changed");
154   variant = g_dbus_proxy_get_cached_property (p, "y");
155   g_assert_nonnull (variant);
156   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
157   g_variant_unref (variant);
158   variant = g_dbus_proxy_get_cached_property (ap, "y");
159   g_assert_nonnull (variant);
160   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
161   g_variant_unref (variant);
162 
163   /* Nuke the service and check that we get the signal and then don't
164    * have a name owner nor any cached properties
165    */
166   result = g_dbus_proxy_call_sync (p,
167                                    "Quit",
168                                    NULL,
169                                    G_DBUS_CALL_FLAGS_NONE,
170                                    -1,
171                                    NULL,
172                                    &error);
173   g_assert_no_error (error);
174   g_assert_nonnull (result);
175   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
176   g_variant_unref (result);
177   /* and wait... */
178   _g_assert_property_notify (p, "g-name-owner");
179   /* now we shouldn't have a name owner nor any cached properties */
180   g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
181   g_assert_null (g_dbus_proxy_get_cached_property_names (p));
182   g_assert_null (g_dbus_proxy_get_cached_property (p, "y"));
183 
184   g_object_unref (p);
185   g_object_unref (ap);
186 
187   g_object_unref (wp);
188   g_free (unique_name);
189 
190   g_object_unref (c);
191 
192   /* tear down bus */
193   session_bus_down ();
194 }
195 
196 /* ---------------------------------------------------------------------------------------------------- */
197 
198 int
main(int argc,char * argv[])199 main (int   argc,
200       char *argv[])
201 {
202   gint ret;
203 
204   g_test_init (&argc, &argv, NULL);
205 
206   /* all the tests rely on a shared main loop */
207   loop = g_main_loop_new (NULL, FALSE);
208 
209   g_test_dbus_unset ();
210 
211   g_test_add_func ("/gdbus/proxy-unique-name", test_proxy_unique_name);
212 
213   ret = g_test_run();
214   return ret;
215 }
216