2 * Copyright 2008-2011 Novell, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include <dbind/dbind.h>
25 /* Wow! dbus is unpleasant to use */
27 #define DESKICE_PATH "/Novell/ICEDesktop/Daemon"
28 #define DESKICE_NAMESPACE "Novell.ICEDesktop.Daemon"
30 void marshal (DBusMessage *msg, char *type, void *ptr)
34 dbus_message_iter_init_append (msg, &iter);
35 dbind_any_marshal (&iter, &type, &ptr);
38 void demarshal (DBusMessage *msg, char *type, void *ptr)
42 if (!dbus_message_iter_init (msg, &iter))
43 fprintf (stderr, "no data in msg\n");
45 dbind_any_demarshal (&iter, &type, &ptr);
49 dbus_bool_t dbus_message_marshal (DBusMessage *msg,
50 char **marshalled_data_p,
53 void dump_msg (DBusMessage *msg)
58 dbus_message_marshal (msg, &data, &len);
59 for (i = 0; i < (len+15)/16; i++) {
60 fprintf (stderr, "%4.d | ", i * 16);
61 for (j = 0; j < 16; j++) {
62 unsigned char c = (i*16+j <= len) ? data[i*16+j] : 0;
63 fprintf (stderr, "0x%.2x ", c);
65 fprintf (stderr, " | ");
66 for (j = 0; j < 16; j++) {
67 char c = (i*16+j <= len) ? data[i*16+j] : '\0';
68 fprintf (stderr, "%c", g_ascii_isprint (c) ? c : '.');
79 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
81 marshal (msg, "i", &v1);
82 demarshal (msg, "i", &v2);
86 dbind_any_free ("i", &v2); /* nop */
87 dbus_message_unref (msg);
89 fprintf (stderr, "simple ok\n");
98 a1 = g_array_new (FALSE, FALSE, sizeof (dbus_int32_t));
99 g_array_set_size (a1, 4);
100 g_array_index (a1, dbus_int32_t, 0) = 42;
101 g_array_index (a1, dbus_int32_t, 1) = 17;
102 g_array_index (a1, dbus_int32_t, 2) = 26;
103 g_array_index (a1, dbus_int32_t, 3) = 38;
105 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
106 marshal (msg, "ai", &a1);
107 demarshal (msg, "ai", &a2);
109 g_assert (a2 != NULL);
110 g_assert (a2->len == 4);
111 g_assert (g_array_index (a2, dbus_int32_t, 0) == 42);
112 g_assert (g_array_index (a2, dbus_int32_t, 1) == 17);
113 g_assert (g_array_index (a2, dbus_int32_t, 2) == 26);
114 g_assert (g_array_index (a2, dbus_int32_t, 3) == 38);
115 g_array_free (a1, TRUE);
117 dbind_any_free ("ai", &a2);
118 dbus_message_unref (msg);
120 fprintf (stderr, "array ok\n");
123 /* this taught me that the struct type is a mis-nomer,
124 it is generated by brackets */
125 void test_struct_native ()
128 DBusMessageIter iter, arr, str;
130 /* manually create ar(ss) */
131 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
133 dbus_message_iter_init_append (msg, &iter);
135 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ss)", &arr);
138 dbus_message_iter_open_container (&arr, DBUS_TYPE_STRUCT, NULL, &str);
141 dbus_message_iter_append_basic (&str, DBUS_TYPE_STRING, &foo);
143 dbus_message_iter_append_basic (&str, DBUS_TYPE_STRING, &foo);
145 dbus_message_iter_close_container (&arr, &str);
147 dbus_message_iter_close_container (&iter, &arr);
149 fprintf (stderr, "native struct marshalling ok\n");
151 dbus_message_unref (msg);
155 void test_struct_simple ()
162 GArray *a1 = NULL, *a2 = NULL;
165 a1 = g_array_new (FALSE, FALSE, sizeof (FooBaa));
166 g_array_set_size (a1, 2);
167 g_array_index (a1, FooBaa, 0).foo = "foo";
168 g_array_index (a1, FooBaa, 0).baa = "baa";
169 g_array_index (a1, FooBaa, 0).baz = "baz";
170 g_array_index (a1, FooBaa, 1).foo = "Foo";
171 g_array_index (a1, FooBaa, 1).baa = "baA";
172 g_array_index (a1, FooBaa, 1).baz = "BaZ";
174 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
175 marshal (msg, "a(sss)", &a1);
176 demarshal (msg, "a(sss)", &a2);
178 g_assert (a2 != NULL);
180 g_assert (a2->len == 2);
181 g_assert (!strcmp (g_array_index (a2, FooBaa, 0).foo, "foo"));
182 g_assert (!strcmp (g_array_index (a2, FooBaa, 0).baa, "baa"));
183 g_assert (!strcmp (g_array_index (a2, FooBaa, 0).baz, "baz"));
184 g_assert (!strcmp (g_array_index (a2, FooBaa, 1).foo, "Foo"));
185 g_assert (!strcmp (g_array_index (a2, FooBaa, 1).baa, "baA"));
186 g_assert (!strcmp (g_array_index (a2, FooBaa, 1).baz, "BaZ"));
188 fprintf (stderr, "simple struct ok\n");
190 dbind_any_free ("a(sss)", &a2);
191 dbus_message_unref (msg);
194 void test_struct_complex ()
206 #define TYPEOF_POINT \
207 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
208 DBUS_TYPE_INT32_AS_STRING \
209 DBUS_TYPE_INT32_AS_STRING \
210 DBUS_STRUCT_END_CHAR_AS_STRING
211 #define TYPEOF_COMPLEX \
212 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
213 DBUS_TYPE_BYTE_AS_STRING \
214 DBUS_TYPE_DOUBLE_AS_STRING \
217 DBUS_TYPE_BYTE_AS_STRING \
218 DBUS_TYPE_STRING_AS_STRING \
219 DBUS_STRUCT_END_CHAR_AS_STRING
225 memset (&c1, 0, sizeof (c1));
226 memset (&c2, 0, sizeof (c2));
235 c1.name = "stroustrup";
237 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
238 marshal (msg, TYPEOF_COMPLEX, &c1);
239 demarshal (msg, TYPEOF_COMPLEX, &c2);
241 g_assert (c2.pad1 == 2);
242 g_assert (c2.val == c1.val);
243 g_assert (c2.val != 0);
244 g_assert (c2.tl.x == 1);
245 g_assert (c2.tl.y == 17);
246 g_assert (c2.br.x == 2587);
247 g_assert (c2.br.y == -1);
248 g_assert (c2.pad2 == 1);
249 g_assert (!strcmp (c1.name, "stroustrup"));
251 fprintf (stderr, "complex struct ok\n");
253 dbind_any_free (TYPEOF_COMPLEX, &c2);
254 dbus_message_unref (msg);
257 void test_struct_with_array ()
263 #define TYPEOF_ARRAYSTRUCT \
264 DBUS_TYPE_ARRAY_AS_STRING \
265 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
266 DBUS_TYPE_ARRAY_AS_STRING \
267 DBUS_TYPE_UINT32_AS_STRING \
268 DBUS_TYPE_BYTE_AS_STRING \
269 DBUS_STRUCT_END_CHAR_AS_STRING
277 a1 = g_array_new (FALSE, FALSE, sizeof (ArrayStruct));
278 g_array_set_size (a1, 2);
279 p = &g_array_index (a1, ArrayStruct, 0);
280 p[0].vals = g_array_new (FALSE, FALSE, sizeof (dbus_uint32_t));
281 g_array_set_size (p[0].vals, 2);
282 g_array_index (p[0].vals, dbus_uint32_t, 0) = 1;
283 g_array_index (p[0].vals, dbus_uint32_t, 1) = 1000;
285 p[1].vals = g_array_new (FALSE, FALSE, sizeof (dbus_uint32_t));
286 g_array_set_size (p[1].vals, 2);
287 g_array_index (p[1].vals, dbus_uint32_t, 0) = 1000000;
288 g_array_index (p[1].vals, dbus_uint32_t, 1) = 1000000000;
291 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
292 marshal (msg, TYPEOF_ARRAYSTRUCT, &a1);
293 demarshal (msg, TYPEOF_ARRAYSTRUCT, &a2);
295 q = &g_array_index (a2, ArrayStruct, 0);
296 g_assert (p[0].pad1 == 2);
297 g_assert (g_array_index (p[1].vals, dbus_uint32_t, 1) == 1000000000);
299 fprintf (stderr, "struct with array ok\n");
301 dbind_any_free (TYPEOF_ARRAYSTRUCT, &a2);
302 dbus_message_unref (msg);
303 g_array_free (p[0].vals, TRUE);
304 g_array_free (p[1].vals, TRUE);
313 #define TYPEOF_TWOVAL \
314 DBUS_TYPE_INT32_AS_STRING \
315 DBUS_TYPE_INT32_AS_STRING \
318 DBusMessageIter iter;
320 char *type_twoval = TYPEOF_TWOVAL;
324 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
327 dbus_message_iter_init_append (msg, &iter);
330 dbind_any_marshal (&iter, &type, &ptr);
331 dbind_any_marshal (&iter, &type, &ptr);
332 dbus_message_iter_init (msg, &iter);
335 dbind_any_demarshal (&iter, &type, &ptr);
336 dbind_any_demarshal (&iter, &type, &ptr);
337 g_assert (o.v1 == 42);
338 g_assert (o.v2 == 1764);
339 g_assert (i.v1 == o.v1);
340 g_assert (i.v2 == o.v2);
342 dbind_any_free ("ii", &o); /* nop */
343 dbus_message_unref (msg);
345 fprintf (stderr, "two-val ok\n");
348 void test_marshalling ()
352 test_struct_native ();
353 test_struct_simple ();
354 test_struct_complex ();
355 test_struct_with_array ();
358 fprintf (stderr, "Marshalling ok\n");
361 void test_teamspaces (DBusConnection *bus)
372 dbus_error_init (&error);
373 if (!dbind_method_call_reentrant (bus,
381 fprintf (stderr, "Error getting team spaces %s: %s\n",
382 error.name, error.message);
383 dbus_error_free (&error);
388 fprintf (stderr, "no teamspaces\n");
391 fprintf (stderr, "%d teamspace(s)\n", spaces->len);
392 for (i = 0; i < spaces->len; i++) {
393 TeamSpace *space = &g_array_index (spaces, TeamSpace, i);
394 fprintf (stderr, "\t%d: %s, %s, %s\n", i, space->name, space->id, space->url);
397 dbind_any_free_ptr ("a(sss)", spaces);
402 dbind_find_c_alignment ("(sss)");
403 dbind_find_c_alignment ("a(sss)");
404 dbind_find_c_alignment ("(s(s)yd(d)s)");
405 dbind_find_c_alignment ("a{ss}");
406 fprintf (stderr, "helpers passed\n");
409 int main (int argc, char **argv)
413 bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
417 test_teamspaces (bus);