4 #include <dbind/dbind.h>
6 /* Wow! dbus is unpleasant to use */
8 #define DESKICE_PATH "/Novell/ICEDesktop/Daemon"
9 #define DESKICE_NAMESPACE "Novell.ICEDesktop.Daemon"
11 void marshal (DBusMessage *msg, char *type, void *ptr)
15 dbus_message_iter_init_append (msg, &iter);
16 dbind_any_marshal (&iter, &type, &ptr);
19 void demarshal (DBusMessage *msg, char *type, void *ptr)
23 if (!dbus_message_iter_init (msg, &iter))
24 fprintf (stderr, "no data in msg\n");
26 dbind_any_demarshal (&iter, &type, &ptr);
30 dbus_bool_t dbus_message_marshal (DBusMessage *msg,
31 char **marshalled_data_p,
34 void dump_msg (DBusMessage *msg)
39 dbus_message_marshal (msg, &data, &len);
40 for (i = 0; i < (len+15)/16; i++) {
41 fprintf (stderr, "%4.d | ", i * 16);
42 for (j = 0; j < 16; j++) {
43 unsigned char c = (i*16+j <= len) ? data[i*16+j] : 0;
44 fprintf (stderr, "0x%.2x ", c);
46 fprintf (stderr, " | ");
47 for (j = 0; j < 16; j++) {
48 char c = (i*16+j <= len) ? data[i*16+j] : '\0';
49 fprintf (stderr, "%c", g_ascii_isprint (c) ? c : '.');
60 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
62 marshal (msg, "i", &v1);
63 demarshal (msg, "i", &v2);
67 dbind_any_free ("i", &v2); /* nop */
68 dbus_message_unref (msg);
70 fprintf (stderr, "simple ok\n");
79 a1 = g_array_new (FALSE, FALSE, sizeof (dbus_int32_t));
80 g_array_set_size (a1, 4);
81 g_array_index (a1, dbus_int32_t, 0) = 42;
82 g_array_index (a1, dbus_int32_t, 1) = 17;
83 g_array_index (a1, dbus_int32_t, 2) = 26;
84 g_array_index (a1, dbus_int32_t, 3) = 38;
86 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
87 marshal (msg, "ai", &a1);
88 demarshal (msg, "ai", &a2);
90 g_assert (a2 != NULL);
91 g_assert (a2->len == 4);
92 g_assert (g_array_index (a2, dbus_int32_t, 0) == 42);
93 g_assert (g_array_index (a2, dbus_int32_t, 1) == 17);
94 g_assert (g_array_index (a2, dbus_int32_t, 2) == 26);
95 g_assert (g_array_index (a2, dbus_int32_t, 3) == 38);
96 g_array_free (a1, TRUE);
98 dbind_any_free ("ai", &a2);
99 dbus_message_unref (msg);
101 fprintf (stderr, "array ok\n");
104 /* this taught me that the struct type is a mis-nomer,
105 it is generated by brackets */
106 void test_struct_native ()
109 DBusMessageIter iter, arr, str;
111 /* manually create ar(ss) */
112 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
114 dbus_message_iter_init_append (msg, &iter);
116 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ss)", &arr);
119 dbus_message_iter_open_container (&arr, DBUS_TYPE_STRUCT, NULL, &str);
122 dbus_message_iter_append_basic (&str, DBUS_TYPE_STRING, &foo);
124 dbus_message_iter_append_basic (&str, DBUS_TYPE_STRING, &foo);
126 dbus_message_iter_close_container (&arr, &str);
128 dbus_message_iter_close_container (&iter, &arr);
130 fprintf (stderr, "native struct marshalling ok\n");
132 dbus_message_unref (msg);
136 void test_struct_simple ()
143 GArray *a1 = NULL, *a2 = NULL;
146 a1 = g_array_new (FALSE, FALSE, sizeof (FooBaa));
147 g_array_set_size (a1, 2);
148 g_array_index (a1, FooBaa, 0).foo = "foo";
149 g_array_index (a1, FooBaa, 0).baa = "baa";
150 g_array_index (a1, FooBaa, 0).baz = "baz";
151 g_array_index (a1, FooBaa, 1).foo = "Foo";
152 g_array_index (a1, FooBaa, 1).baa = "baA";
153 g_array_index (a1, FooBaa, 1).baz = "BaZ";
155 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
156 marshal (msg, "a(sss)", &a1);
157 demarshal (msg, "a(sss)", &a2);
159 g_assert (a2 != NULL);
161 g_assert (a2->len == 2);
162 g_assert (!strcmp (g_array_index (a2, FooBaa, 0).foo, "foo"));
163 g_assert (!strcmp (g_array_index (a2, FooBaa, 0).baa, "baa"));
164 g_assert (!strcmp (g_array_index (a2, FooBaa, 0).baz, "baz"));
165 g_assert (!strcmp (g_array_index (a2, FooBaa, 1).foo, "Foo"));
166 g_assert (!strcmp (g_array_index (a2, FooBaa, 1).baa, "baA"));
167 g_assert (!strcmp (g_array_index (a2, FooBaa, 1).baz, "BaZ"));
169 fprintf (stderr, "simple struct ok\n");
171 dbind_any_free ("a(sss)", &a2);
172 dbus_message_unref (msg);
175 void test_struct_complex ()
187 #define TYPEOF_POINT \
188 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
189 DBUS_TYPE_INT32_AS_STRING \
190 DBUS_TYPE_INT32_AS_STRING \
191 DBUS_STRUCT_END_CHAR_AS_STRING
192 #define TYPEOF_COMPLEX \
193 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
194 DBUS_TYPE_BYTE_AS_STRING \
195 DBUS_TYPE_DOUBLE_AS_STRING \
198 DBUS_TYPE_BYTE_AS_STRING \
199 DBUS_TYPE_STRING_AS_STRING \
200 DBUS_STRUCT_END_CHAR_AS_STRING
206 memset (&c1, 0, sizeof (c1));
207 memset (&c2, 0, sizeof (c2));
216 c1.name = "stroustrup";
218 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
219 marshal (msg, TYPEOF_COMPLEX, &c1);
220 demarshal (msg, TYPEOF_COMPLEX, &c2);
222 g_assert (c2.pad1 == 2);
223 g_assert (c2.val == c1.val);
224 g_assert (c2.val != 0);
225 g_assert (c2.tl.x == 1);
226 g_assert (c2.tl.y == 17);
227 g_assert (c2.br.x == 2587);
228 g_assert (c2.br.y == -1);
229 g_assert (c2.pad2 == 1);
230 g_assert (!strcmp (c1.name, "stroustrup"));
232 fprintf (stderr, "complex struct ok\n");
234 dbind_any_free (TYPEOF_COMPLEX, &c2);
235 dbus_message_unref (msg);
238 void test_struct_with_array ()
244 #define TYPEOF_ARRAYSTRUCT \
245 DBUS_TYPE_ARRAY_AS_STRING \
246 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
247 DBUS_TYPE_ARRAY_AS_STRING \
248 DBUS_TYPE_UINT32_AS_STRING \
249 DBUS_TYPE_BYTE_AS_STRING \
250 DBUS_STRUCT_END_CHAR_AS_STRING
258 a1 = g_array_new (FALSE, FALSE, sizeof (ArrayStruct));
259 g_array_set_size (a1, 2);
260 p = &g_array_index (a1, ArrayStruct, 0);
261 p[0].vals = g_array_new (FALSE, FALSE, sizeof (dbus_uint32_t));
262 g_array_set_size (p[0].vals, 2);
263 g_array_index (p[0].vals, dbus_uint32_t, 0) = 1;
264 g_array_index (p[0].vals, dbus_uint32_t, 1) = 1000;
266 p[1].vals = g_array_new (FALSE, FALSE, sizeof (dbus_uint32_t));
267 g_array_set_size (p[1].vals, 2);
268 g_array_index (p[1].vals, dbus_uint32_t, 0) = 1000000;
269 g_array_index (p[1].vals, dbus_uint32_t, 1) = 1000000000;
272 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
273 marshal (msg, TYPEOF_ARRAYSTRUCT, &a1);
274 demarshal (msg, TYPEOF_ARRAYSTRUCT, &a2);
276 q = &g_array_index (a2, ArrayStruct, 0);
277 g_assert (p[0].pad1 == 2);
278 g_assert (g_array_index (p[1].vals, dbus_uint32_t, 1) == 1000000000);
280 fprintf (stderr, "struct with array ok\n");
282 dbind_any_free (TYPEOF_ARRAYSTRUCT, &a2);
283 dbus_message_unref (msg);
284 g_array_free (p[0].vals, TRUE);
285 g_array_free (p[1].vals, TRUE);
294 #define TYPEOF_TWOVAL \
295 DBUS_TYPE_INT32_AS_STRING \
296 DBUS_TYPE_INT32_AS_STRING \
299 DBusMessageIter iter;
301 char *type_twoval = TYPEOF_TWOVAL;
305 msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
308 dbus_message_iter_init_append (msg, &iter);
311 dbind_any_marshal (&iter, &type, &ptr);
312 dbind_any_marshal (&iter, &type, &ptr);
313 dbus_message_iter_init (msg, &iter);
316 dbind_any_demarshal (&iter, &type, &ptr);
317 dbind_any_demarshal (&iter, &type, &ptr);
318 g_assert (o.v1 == 42);
319 g_assert (o.v2 == 1764);
320 g_assert (i.v1 == o.v1);
321 g_assert (i.v2 == o.v2);
323 dbind_any_free ("ii", &o); /* nop */
324 dbus_message_unref (msg);
326 fprintf (stderr, "two-val ok\n");
329 void test_marshalling ()
333 test_struct_native ();
334 test_struct_simple ();
335 test_struct_complex ();
336 test_struct_with_array ();
339 fprintf (stderr, "Marshalling ok\n");
342 void test_teamspaces (DBusConnection *bus)
353 dbus_error_init (&error);
354 if (!dbind_method_call_reentrant (bus,
362 fprintf (stderr, "Error getting team spaces %s: %s\n",
363 error.name, error.message);
364 dbus_error_free (&error);
369 fprintf (stderr, "no teamspaces\n");
372 fprintf (stderr, "%d teamspace(s)\n", spaces->len);
373 for (i = 0; i < spaces->len; i++) {
374 TeamSpace *space = &g_array_index (spaces, TeamSpace, i);
375 fprintf (stderr, "\t%d: %s, %s, %s\n", i, space->name, space->id, space->url);
378 dbind_any_free_ptr ("a(sss)", spaces);
383 dbind_find_c_alignment ("(sss)");
384 dbind_find_c_alignment ("a(sss)");
385 dbind_find_c_alignment ("(s(s)yd(d)s)");
386 dbind_find_c_alignment ("a{ss}");
387 fprintf (stderr, "helpers passed\n");
390 int main (int argc, char **argv)
395 dbus_error_init (&err);
397 bus = dbus_bus_get (DBUS_BUS_SESSION, &err);
401 test_teamspaces (bus);