~elementary-os/elementaryos/os-patch-at-spi2-core-xenial

« back to all changes in this revision

Viewing changes to dbind/dbtest.c

  • Committer: RabbitBot
  • Date: 2016-11-16 09:38:52 UTC
  • Revision ID: rabbitbot@elementary.io-20161116093852-xn6hcgpg5y25xooo
Initial import, version 2.18.3-4ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2008-2011 Novell, Inc.
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 */
 
19
 
 
20
#include <stdio.h>
 
21
#include <glib.h>
 
22
#include <string.h>
 
23
#include <dbind/dbind.h>
 
24
 
 
25
/* Wow! dbus is unpleasant to use */
 
26
 
 
27
#define DESKICE_PATH      "/Novell/ICEDesktop/Daemon"
 
28
#define DESKICE_NAMESPACE "Novell.ICEDesktop.Daemon"
 
29
 
 
30
void marshal (DBusMessage *msg, char *type, void *ptr)
 
31
{
 
32
    DBusMessageIter iter;
 
33
 
 
34
    dbus_message_iter_init_append (msg, &iter);
 
35
    dbind_any_marshal (&iter, &type, &ptr);
 
36
}
 
37
 
 
38
void demarshal (DBusMessage *msg, char *type, void *ptr)
 
39
{
 
40
    DBusMessageIter iter;
 
41
 
 
42
    if (!dbus_message_iter_init (msg, &iter))
 
43
        fprintf (stderr, "no data in msg\n");
 
44
    else
 
45
        dbind_any_demarshal (&iter, &type, &ptr);
 
46
}
 
47
 
 
48
#if 0
 
49
dbus_bool_t  dbus_message_marshal   (DBusMessage  *msg,
 
50
                                     char        **marshalled_data_p,
 
51
                                     int          *len_p);
 
52
 
 
53
void dump_msg (DBusMessage *msg)
 
54
{
 
55
    char *data = NULL;
 
56
    int   len, i, j;
 
57
 
 
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);
 
64
        }
 
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 : '.');
 
69
        }
 
70
    }
 
71
}
 
72
#endif
 
73
 
 
74
void test_simple ()
 
75
{
 
76
    dbus_int32_t v1, v2;
 
77
    DBusMessage *msg;
 
78
 
 
79
    msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
 
80
    v1 = 42;
 
81
    marshal (msg, "i", &v1);
 
82
    demarshal (msg, "i", &v2);
 
83
    g_assert (v2 == 42);
 
84
    g_assert (v1 == v2);
 
85
 
 
86
    dbind_any_free ("i", &v2); /* nop */
 
87
    dbus_message_unref (msg);
 
88
 
 
89
    fprintf (stderr, "simple ok\n");
 
90
}
 
91
 
 
92
void test_array ()
 
93
{
 
94
    GArray *a1, *a2;
 
95
    DBusMessage *msg;
 
96
 
 
97
    /* pod types */
 
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;
 
104
 
 
105
    msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
 
106
    marshal (msg, "ai", &a1);
 
107
    demarshal (msg, "ai", &a2);
 
108
 
 
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);
 
116
 
 
117
    dbind_any_free ("ai", &a2);
 
118
    dbus_message_unref (msg);
 
119
 
 
120
    fprintf (stderr, "array ok\n");
 
121
}
 
122
 
 
123
/* this taught me that the struct type is a mis-nomer, 
 
124
   it is generated by brackets */
 
125
void test_struct_native ()
 
126
{
 
127
    DBusMessage *msg;
 
128
    DBusMessageIter iter, arr, str;
 
129
 
 
130
    /* manually create ar(ss) */
 
131
    msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
 
132
 
 
133
    dbus_message_iter_init_append (msg, &iter);
 
134
 
 
135
    dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ss)", &arr);
 
136
    {
 
137
        char *foo;
 
138
        dbus_message_iter_open_container (&arr, DBUS_TYPE_STRUCT, NULL, &str);
 
139
 
 
140
        foo = "foo";
 
141
        dbus_message_iter_append_basic (&str, DBUS_TYPE_STRING, &foo);
 
142
        foo = "baa";
 
143
        dbus_message_iter_append_basic (&str, DBUS_TYPE_STRING, &foo);
 
144
        
 
145
        dbus_message_iter_close_container (&arr, &str);
 
146
    }
 
147
    dbus_message_iter_close_container (&iter, &arr);
 
148
 
 
149
    fprintf (stderr, "native struct marshalling ok\n");
 
150
    
 
151
    dbus_message_unref (msg);
 
152
}
 
153
 
 
154
 
 
155
void test_struct_simple ()
 
156
{
 
157
    typedef struct {
 
158
        char *foo;
 
159
        char *baa;
 
160
        char *baz;
 
161
    } FooBaa;
 
162
    GArray *a1 = NULL, *a2 = NULL;
 
163
    DBusMessage *msg;
 
164
 
 
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";
 
173
 
 
174
    msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
 
175
    marshal (msg, "a(sss)", &a1);
 
176
    demarshal (msg, "a(sss)", &a2);
 
177
 
 
178
    g_assert (a2 != NULL);
 
179
    g_assert (a2 != a1);
 
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"));
 
187
    
 
188
    fprintf (stderr, "simple struct ok\n");
 
189
 
 
190
    dbind_any_free ("a(sss)", &a2);
 
191
    dbus_message_unref (msg);
 
192
}
 
193
 
 
194
void test_struct_complex ()
 
195
{
 
196
    typedef struct {
 
197
        dbus_int32_t x, y;
 
198
    } Point;
 
199
    typedef struct {
 
200
        unsigned char pad1;
 
201
        double        val;
 
202
        Point         tl, br;
 
203
        char          pad2;
 
204
        char         *name;
 
205
    } 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 \
 
215
        TYPEOF_POINT \
 
216
        TYPEOF_POINT \
 
217
        DBUS_TYPE_BYTE_AS_STRING \
 
218
        DBUS_TYPE_STRING_AS_STRING \
 
219
    DBUS_STRUCT_END_CHAR_AS_STRING
 
220
 
 
221
 
 
222
    DBusMessage *msg;
 
223
    Complex c1, c2;
 
224
 
 
225
    memset (&c1, 0, sizeof (c1));
 
226
    memset (&c2, 0, sizeof (c2));
 
227
 
 
228
    c1.pad1 = 2;
 
229
    c1.val = 0.1327569;
 
230
    c1.tl.x = 1;
 
231
    c1.tl.y = 17;
 
232
    c1.br.x = 2587;
 
233
    c1.br.y = -1;
 
234
    c1.pad2 = 1;
 
235
    c1.name = "stroustrup";
 
236
 
 
237
    msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
 
238
    marshal (msg, TYPEOF_COMPLEX, &c1);
 
239
    demarshal (msg, TYPEOF_COMPLEX, &c2);
 
240
 
 
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"));
 
250
    
 
251
    fprintf (stderr, "complex struct ok\n");
 
252
 
 
253
    dbind_any_free (TYPEOF_COMPLEX, &c2);
 
254
    dbus_message_unref (msg);
 
255
}
 
256
 
 
257
void test_struct_with_array ()
 
258
{
 
259
    typedef struct {
 
260
        GArray *vals;
 
261
        unsigned char pad1;
 
262
    } ArrayStruct;
 
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
 
270
 
 
271
 
 
272
    DBusMessage *msg;
 
273
    GArray *a1, *a2;
 
274
    ArrayStruct *p, *q;
 
275
 
 
276
 
 
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;
 
284
    p[0].pad1 = 2;
 
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;
 
289
    p[1].pad1 = 8;
 
290
 
 
291
    msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
 
292
    marshal (msg, TYPEOF_ARRAYSTRUCT, &a1);
 
293
    demarshal (msg, TYPEOF_ARRAYSTRUCT, &a2);
 
294
 
 
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);
 
298
    
 
299
    fprintf (stderr, "struct with array ok\n");
 
300
 
 
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);
 
305
}
 
306
 
 
307
void test_twovals ()
 
308
{
 
309
    typedef struct {
 
310
        dbus_int32_t v1;
 
311
        dbus_int32_t v2;
 
312
    } TwoVal;
 
313
#define TYPEOF_TWOVAL \
 
314
        DBUS_TYPE_INT32_AS_STRING \
 
315
        DBUS_TYPE_INT32_AS_STRING \
 
316
 
 
317
    DBusMessage *msg;
 
318
    DBusMessageIter iter;
 
319
    TwoVal i, o;
 
320
    char *type_twoval = TYPEOF_TWOVAL;
 
321
    char *type;
 
322
    void *ptr;
 
323
 
 
324
    msg = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
 
325
    i.v1 = 42;
 
326
  i.v2 = 1764;
 
327
    dbus_message_iter_init_append (msg, &iter);
 
328
    type = type_twoval;
 
329
    ptr = &i;
 
330
    dbind_any_marshal (&iter, &type, &ptr);
 
331
    dbind_any_marshal (&iter, &type, &ptr);
 
332
    dbus_message_iter_init (msg, &iter);
 
333
    type = type_twoval;
 
334
    ptr = &o;
 
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);
 
341
 
 
342
    dbind_any_free ("ii", &o); /* nop */
 
343
    dbus_message_unref (msg);
 
344
 
 
345
    fprintf (stderr, "two-val ok\n");
 
346
}
 
347
 
 
348
void test_marshalling ()
 
349
{
 
350
    test_simple ();
 
351
    test_array ();
 
352
    test_struct_native ();
 
353
    test_struct_simple ();
 
354
    test_struct_complex ();
 
355
    test_struct_with_array ();
 
356
    test_twovals ();
 
357
 
 
358
    fprintf (stderr, "Marshalling ok\n");
 
359
}
 
360
 
 
361
void test_teamspaces (DBusConnection *bus)
 
362
{
 
363
    GArray *spaces;
 
364
    DBusError error;
 
365
    int i;
 
366
    typedef struct {
 
367
        char *name;
 
368
        char *id;
 
369
        char *url;
 
370
    } TeamSpace;
 
371
 
 
372
    dbus_error_init (&error);
 
373
    if (!dbind_method_call_reentrant (bus,
 
374
                                      NULL,
 
375
                                      DESKICE_PATH,
 
376
                                      DESKICE_NAMESPACE,
 
377
                                      "GetTeamList",
 
378
                                      &error,
 
379
                                      "=>a(sss)",
 
380
                                      &spaces)) {
 
381
        fprintf (stderr, "Error getting team spaces %s: %s\n",
 
382
                 error.name, error.message);
 
383
        dbus_error_free (&error);
 
384
        return;
 
385
    }
 
386
 
 
387
    if (!spaces) {
 
388
        fprintf (stderr, "no teamspaces\n");
 
389
        return;
 
390
    }
 
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);
 
395
    }
 
396
 
 
397
    dbind_any_free_ptr ("a(sss)", spaces);
 
398
}
 
399
 
 
400
void test_helpers ()
 
401
{
 
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");
 
407
}
 
408
 
 
409
int main (int argc, char **argv)
 
410
{
 
411
    DBusConnection *bus;
 
412
 
 
413
    bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
 
414
 
 
415
    test_helpers ();
 
416
    test_marshalling ();
 
417
    test_teamspaces (bus);
 
418
 
 
419
    return 0;
 
420
}