Release v2.5.91
[platform/core/uifw/at-spi2-atk.git] / dbind / dbtest.c
1 #include <stdio.h>
2 #include <glib.h>
3 #include <string.h>
4 #include <dbind/dbind.h>
5
6 /* Wow! dbus is unpleasant to use */
7
8 #define DESKICE_PATH      "/Novell/ICEDesktop/Daemon"
9 #define DESKICE_NAMESPACE "Novell.ICEDesktop.Daemon"
10
11 void marshal (DBusMessage *msg, char *type, void *ptr)
12 {
13     DBusMessageIter iter;
14
15     dbus_message_iter_init_append (msg, &iter);
16     dbind_any_marshal (&iter, &type, &ptr);
17 }
18
19 void demarshal (DBusMessage *msg, char *type, void *ptr)
20 {
21     DBusMessageIter iter;
22
23     if (!dbus_message_iter_init (msg, &iter))
24         fprintf (stderr, "no data in msg\n");
25     else
26         dbind_any_demarshal (&iter, &type, &ptr);
27 }
28
29 #if 0
30 dbus_bool_t  dbus_message_marshal   (DBusMessage  *msg,
31                                      char        **marshalled_data_p,
32                                      int          *len_p);
33
34 void dump_msg (DBusMessage *msg)
35 {
36     char *data = NULL;
37     int   len, i, j;
38
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);
45         }
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 : '.');
50         }
51     }
52 }
53 #endif
54
55 void test_simple ()
56 {
57     dbus_int32_t v1, v2;
58     DBusMessage *msg;
59
60     msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
61     v1 = 42;
62     marshal (msg, "i", &v1);
63     demarshal (msg, "i", &v2);
64     g_assert (v2 == 42);
65     g_assert (v1 == v2);
66
67     dbind_any_free ("i", &v2); /* nop */
68     dbus_message_unref (msg);
69
70     fprintf (stderr, "simple ok\n");
71 }
72
73 void test_array ()
74 {
75     GArray *a1, *a2;
76     DBusMessage *msg;
77
78     /* pod types */
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;
85
86     msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
87     marshal (msg, "ai", &a1);
88     demarshal (msg, "ai", &a2);
89
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);
97
98     dbind_any_free ("ai", &a2);
99     dbus_message_unref (msg);
100
101     fprintf (stderr, "array ok\n");
102 }
103
104 /* this taught me that the struct type is a mis-nomer, 
105    it is generated by brackets */
106 void test_struct_native ()
107 {
108     DBusMessage *msg;
109     DBusMessageIter iter, arr, str;
110
111     /* manually create ar(ss) */
112     msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
113
114     dbus_message_iter_init_append (msg, &iter);
115
116     dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ss)", &arr);
117     {
118         char *foo;
119         dbus_message_iter_open_container (&arr, DBUS_TYPE_STRUCT, NULL, &str);
120
121         foo = "foo";
122         dbus_message_iter_append_basic (&str, DBUS_TYPE_STRING, &foo);
123         foo = "baa";
124         dbus_message_iter_append_basic (&str, DBUS_TYPE_STRING, &foo);
125         
126         dbus_message_iter_close_container (&arr, &str);
127     }
128     dbus_message_iter_close_container (&iter, &arr);
129
130     fprintf (stderr, "native struct marshalling ok\n");
131     
132     dbus_message_unref (msg);
133 }
134
135
136 void test_struct_simple ()
137 {
138     typedef struct {
139         char *foo;
140         char *baa;
141         char *baz;
142     } FooBaa;
143     GArray *a1 = NULL, *a2 = NULL;
144     DBusMessage *msg;
145
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";
154
155     msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
156     marshal (msg, "a(sss)", &a1);
157     demarshal (msg, "a(sss)", &a2);
158
159     g_assert (a2 != NULL);
160     g_assert (a2 != a1);
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"));
168     
169     fprintf (stderr, "simple struct ok\n");
170
171     dbind_any_free ("a(sss)", &a2);
172     dbus_message_unref (msg);
173 }
174
175 void test_struct_complex ()
176 {
177     typedef struct {
178         dbus_int32_t x, y;
179     } Point;
180     typedef struct {
181         unsigned char pad1;
182         double        val;
183         Point         tl, br;
184         char          pad2;
185         char         *name;
186     } 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 \
196         TYPEOF_POINT \
197         TYPEOF_POINT \
198         DBUS_TYPE_BYTE_AS_STRING \
199         DBUS_TYPE_STRING_AS_STRING \
200     DBUS_STRUCT_END_CHAR_AS_STRING
201
202
203     DBusMessage *msg;
204     Complex c1, c2;
205
206     memset (&c1, 0, sizeof (c1));
207     memset (&c2, 0, sizeof (c2));
208
209     c1.pad1 = 2;
210     c1.val = 0.1327569;
211     c1.tl.x = 1;
212     c1.tl.y = 17;
213     c1.br.x = 2587;
214     c1.br.y = -1;
215     c1.pad2 = 1;
216     c1.name = "stroustrup";
217
218     msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
219     marshal (msg, TYPEOF_COMPLEX, &c1);
220     demarshal (msg, TYPEOF_COMPLEX, &c2);
221
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"));
231     
232     fprintf (stderr, "complex struct ok\n");
233
234     dbind_any_free (TYPEOF_COMPLEX, &c2);
235     dbus_message_unref (msg);
236 }
237
238 void test_struct_with_array ()
239 {
240     typedef struct {
241         GArray *vals;
242         unsigned char pad1;
243     } ArrayStruct;
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
251
252
253     DBusMessage *msg;
254     GArray *a1, *a2;
255     ArrayStruct *p, *q;
256
257
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;
265     p[0].pad1 = 2;
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;
270     p[1].pad1 = 8;
271
272     msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
273     marshal (msg, TYPEOF_ARRAYSTRUCT, &a1);
274     demarshal (msg, TYPEOF_ARRAYSTRUCT, &a2);
275
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);
279     
280     fprintf (stderr, "struct with array ok\n");
281
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);
286 }
287
288 void test_twovals ()
289 {
290     typedef struct {
291         dbus_int32_t v1;
292         dbus_int32_t v2;
293     } TwoVal;
294 #define TYPEOF_TWOVAL \
295         DBUS_TYPE_INT32_AS_STRING \
296         DBUS_TYPE_INT32_AS_STRING \
297
298     DBusMessage *msg;
299     DBusMessageIter iter;
300     TwoVal i, o;
301     char *type_twoval = TYPEOF_TWOVAL;
302     char *type;
303     void *ptr;
304
305     msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
306     i.v1 = 42;
307   i.v2 = 1764;
308     dbus_message_iter_init_append (msg, &iter);
309     type = type_twoval;
310     ptr = &i;
311     dbind_any_marshal (&iter, &type, &ptr);
312     dbind_any_marshal (&iter, &type, &ptr);
313     dbus_message_iter_init (msg, &iter);
314     type = type_twoval;
315     ptr = &o;
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);
322
323     dbind_any_free ("ii", &o); /* nop */
324     dbus_message_unref (msg);
325
326     fprintf (stderr, "two-val ok\n");
327 }
328
329 void test_marshalling ()
330 {
331     test_simple ();
332     test_array ();
333     test_struct_native ();
334     test_struct_simple ();
335     test_struct_complex ();
336     test_struct_with_array ();
337     test_twovals ();
338
339     fprintf (stderr, "Marshalling ok\n");
340 }
341
342 void test_teamspaces (DBusConnection *bus)
343 {
344     GArray *spaces;
345     DBusError error;
346     int i;
347     typedef struct {
348         char *name;
349         char *id;
350         char *url;
351     } TeamSpace;
352
353     dbus_error_init (&error);
354     if (!dbind_method_call_reentrant (bus,
355                                       NULL,
356                                       DESKICE_PATH,
357                                       DESKICE_NAMESPACE,
358                                       "GetTeamList",
359                                       &error,
360                                       "=>a(sss)",
361                                       &spaces)) {
362         fprintf (stderr, "Error getting team spaces %s: %s\n",
363                  error.name, error.message);
364         dbus_error_free (&error);
365         return;
366     }
367
368     if (!spaces) {
369         fprintf (stderr, "no teamspaces\n");
370         return;
371     }
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);
376     }
377
378     dbind_any_free_ptr ("a(sss)", spaces);
379 }
380
381 void test_helpers ()
382 {
383     dbind_find_c_alignment ("(sss)");
384     dbind_find_c_alignment ("a(sss)");
385     dbind_find_c_alignment ("(s(s)yd(d)s)");
386     fprintf (stderr, "helpers passed\n");
387 }
388
389 int main (int argc, char **argv)
390 {
391     DBusConnection *bus;
392     DBusError err;
393
394     dbus_error_init (&err);
395
396     bus = dbus_bus_get (DBUS_BUS_SESSION, &err);
397
398     test_helpers ();
399     test_marshalling ();
400     test_teamspaces (bus);
401
402     return 0;
403 }