~ubuntu-branches/ubuntu/quantal/libbonobo/quantal-201207170711

« back to all changes in this revision

Viewing changes to tests/test-activation/bonobo-activation-test.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-02-18 14:40:51 UTC
  • mto: (3.1.1 etch) (1.1.25 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050218144051-fo4h9qh2gim8x3wt
Tags: upstream-2.8.1
ImportĀ upstreamĀ versionĀ 2.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <string.h>
 
5
#include <unistd.h>
 
6
 
 
7
#include <bonobo-activation/bonobo-activation.h>
 
8
#include <bonobo-activation/bonobo-activation-private.h>
 
9
#include "activation-server/server.h"
 
10
 
 
11
#include "empty.h"
 
12
#include "plugin.h"
 
13
 
 
14
#define TOTAL_TEST_SCORE 17
 
15
 
 
16
CORBA_Object name_service = CORBA_OBJECT_NIL;
 
17
 
 
18
static char *
 
19
bonobo_activation_exception_id (CORBA_Environment *ev)
 
20
{
 
21
        if (ev->_major == CORBA_USER_EXCEPTION) {
 
22
                if (!strcmp (ev->_id, "IDL:Bonobo/GeneralError:1.0")) {
 
23
                        Bonobo_GeneralError *err = CORBA_exception_value (ev);
 
24
                        
 
25
                        if (!err || !err->description) {
 
26
                                return "No general exception error message";
 
27
                        } else {
 
28
                                return err->description;
 
29
                        }
 
30
                } else {
 
31
                        return ev->_id;
 
32
                }
 
33
        } else {
 
34
                return CORBA_exception_id (ev);
 
35
        }
 
36
}
 
37
 
 
38
static gboolean
 
39
test_bonobo_activation_server (CORBA_Environment *ev, const char *type)
 
40
{
 
41
        CORBA_Object ns;
 
42
 
 
43
        ns = bonobo_activation_name_service_get (ev);
 
44
        if (ev->_major != CORBA_NO_EXCEPTION) {
 
45
                g_warning ("Exception '%s' (%s) finding bonobo_activation_server %s",
 
46
                           bonobo_activation_exception_id (ev), ev->_id, type);
 
47
                return FALSE;
 
48
        }
 
49
 
 
50
        if (name_service != CORBA_OBJECT_NIL &&
 
51
            name_service != ns) {
 
52
                g_warning ("bonobo_activation_server crashed %s", type);
 
53
                return FALSE;
 
54
        }
 
55
 
 
56
        if (name_service == CORBA_OBJECT_NIL)
 
57
                name_service = ns;
 
58
        else
 
59
                CORBA_Object_release (ns, ev);
 
60
 
 
61
        return TRUE;
 
62
}
 
63
 
 
64
static gboolean
 
65
test_object (CORBA_Object obj, CORBA_Environment *ev, const char *type)
 
66
{
 
67
        if (ev->_major != CORBA_NO_EXCEPTION) {
 
68
                g_warning ("Activation %s failed: %s\n", type,
 
69
                           bonobo_activation_exception_id (ev));
 
70
        } else if (CORBA_Object_is_nil (obj, ev)) {
 
71
                g_warning ("Activation %s failed (returned NIL but no exception)!", type);
 
72
        } else {
 
73
                return TRUE;
 
74
        }
 
75
 
 
76
        if (!test_bonobo_activation_server (ev, type)) {
 
77
                return FALSE;
 
78
        }
 
79
 
 
80
        return FALSE;
 
81
}
 
82
 
 
83
static int
 
84
test_plugin (CORBA_Object obj, CORBA_Environment *ev, const char *type)
 
85
{
 
86
        Plugin_doPluginTest (obj, ev);
 
87
 
 
88
        if (ev->_major != CORBA_NO_EXCEPTION) {
 
89
                g_warning ("Call failed: %s\n",
 
90
                           bonobo_activation_exception_id (ev));
 
91
                return 0;
 
92
        } else {
 
93
                fprintf (stderr, "Test %s succeeded\n", type);
 
94
                CORBA_Object_release (obj, ev);
 
95
                return 1;
 
96
        }
 
97
}
 
98
 
 
99
static int
 
100
test_empty (CORBA_Object obj, CORBA_Environment *ev, const char *type)
 
101
{
 
102
        Empty_doNothing (obj, ev);
 
103
 
 
104
        if (ev->_major != CORBA_NO_EXCEPTION) {
 
105
                g_warning ("Call failed: %s\n",
 
106
                           bonobo_activation_exception_id (ev));
 
107
                return 0;
 
108
        } else {
 
109
                fprintf (stderr, "Test %s succeeded\n", type);
 
110
                CORBA_Object_release (obj, ev);
 
111
                return 1;
 
112
        }
 
113
}
 
114
 
 
115
static int
 
116
idle_base_activation (gpointer user_data)
 
117
{
 
118
        /* This is a facile test, we always activate the
 
119
         * ActivationContext first and then get the OD from it */
 
120
        bonobo_activation_activation_context_get ();
 
121
 
 
122
        return FALSE;
 
123
}
 
124
 
 
125
static void
 
126
race_base_init (void)
 
127
{
 
128
        g_idle_add (idle_base_activation, NULL);
 
129
        /* to race with the activation context get in the same process */
 
130
        bonobo_activation_object_directory_get (NULL, NULL);
 
131
}
 
132
 
 
133
int passed = 0;
 
134
int failed = 0;
 
135
int async_done = 0;
 
136
 
 
137
static void
 
138
empty_activation_cb (CORBA_Object   obj,
 
139
                     const char    *error_reason, 
 
140
                     gpointer       user_data)
 
141
{
 
142
        CORBA_Environment ev;
 
143
        gboolean          ret = FALSE;
 
144
        char             *repo_id = user_data;
 
145
 
 
146
        CORBA_exception_init (&ev);
 
147
 
 
148
        if (error_reason)
 
149
                g_warning ("Async activation error activating '%s' : '%s'", repo_id, error_reason);
 
150
 
 
151
        else if (test_object (obj, &ev, "by async query"))
 
152
                ret = test_empty (obj, &ev, "by async query");
 
153
 
 
154
        if (ret) {
 
155
                passed++;
 
156
                fprintf (stderr, "PASSED %d of %d: async activation\n", passed + failed, TOTAL_TEST_SCORE);
 
157
        } else {
 
158
                failed++;
 
159
                fprintf (stderr, "FAILED %d of %d: async activation\n", passed + failed, TOTAL_TEST_SCORE);
 
160
        }
 
161
 
 
162
        CORBA_exception_free (&ev);
 
163
 
 
164
        async_done++;
 
165
}
 
166
 
 
167
static void
 
168
race_empty (CORBA_Environment *ev)
 
169
{
 
170
        bonobo_activation_activate_async (
 
171
                "repo_ids.has('IDL:Empty2:1.0')", NULL,
 
172
                0, empty_activation_cb, "IDL:Empty2:1.0", ev);
 
173
        g_assert (ev->_major == CORBA_NO_EXCEPTION);
 
174
 
 
175
        bonobo_activation_activate_async (
 
176
                "repo_ids.has('IDL:Empty:1.0')", NULL,
 
177
                0, empty_activation_cb, "IDL:Empty:1.0", ev);
 
178
        g_assert (ev->_major == CORBA_NO_EXCEPTION);
 
179
 
 
180
        while (async_done < 2)
 
181
                g_main_context_iteration (NULL, TRUE);
 
182
}
 
183
 
 
184
static void
 
185
listener_cb (BonoboListener *listener,
 
186
             const char *event_name,
 
187
             const CORBA_any *any,
 
188
             CORBA_Environment *ev,
 
189
             gpointer user_data)
 
190
{
 
191
        g_message ("Activation callback successfully called on %s",
 
192
                   bonobo_event_subtype (event_name));
 
193
}
 
194
 
 
195
 
 
196
int
 
197
main (int argc, char *argv[])
 
198
{
 
199
        CORBA_Object obj;
 
200
        CORBA_Environment ev;
 
201
        Bonobo_ServerInfoList *info;
 
202
        CORBA_Object ac;
 
203
        char *sort_by[4];
 
204
        char *query;
 
205
        int   i;
 
206
        GTimer *timer = g_timer_new ();
 
207
        Bonobo_EventSource event_source;
 
208
        
 
209
 
 
210
        CORBA_exception_init (&ev);
 
211
 
 
212
        bonobo_activation_object_directory_get (
 
213
                bonobo_activation_username_get (),
 
214
                bonobo_activation_hostname_get ());
 
215
 
 
216
        bonobo_init (&argc, &argv);
 
217
/*      putenv("Bonobo_BARRIER_INIT=1"); */
 
218
 
 
219
 
 
220
        event_source = bonobo_activation_activate_from_id
 
221
                ("OAFIID:Bonobo_Activation_EventSource", 0, NULL, &ev);
 
222
        if (event_source != CORBA_OBJECT_NIL) {
 
223
                passed++;
 
224
                fprintf (stderr, "PASSED %d of %d: Activation event source okay\n",
 
225
                         passed + failed, TOTAL_TEST_SCORE);
 
226
                bonobo_event_source_client_add_listener (event_source, listener_cb,
 
227
                                                         "Bonobo/ObjectDirectory", &ev, NULL);
 
228
        } else {
 
229
                failed++;
 
230
                fprintf (stderr, "FAILED %d of %d: Activation event source not found\n",
 
231
                         passed + failed, TOTAL_TEST_SCORE);
 
232
        }
 
233
 
 
234
 
 
235
        race_base_init ();
 
236
 
 
237
        sort_by[0] = "prefer_by_list_order(iid, ["
 
238
                "'OAFIID:nautilus_file_manager_icon_view:42681b21-d5ca-4837-87d2-394d88ecc058',"
 
239
                "'OAFIID:nautilus_file_manager_list_view:521e489d-0662-4ad7-ac3a-832deabe111c',"
 
240
                "'OAFIID:nautilus_music_view:9456b5d2-60a8-407f-a56e-d561e1821391'])";
 
241
        sort_by[1] = "iid != 'OAFIID:nautilus_content_loser:95901458-c68b-43aa-aaca-870ced11062d'";
 
242
        sort_by[2] = "iid != 'OAFIID:nautilus_sample_content_view:45c746bc-7d64-4346-90d5-6410463b43ae'";
 
243
        sort_by[3] = NULL;
 
244
 
 
245
        query = "( (((repo_ids.has_all (['IDL:Bonobo/Control:1.0',"
 
246
                "'IDL:Nautilus/View:1.0']) OR (repo_ids.has_one "
 
247
                "(['IDL:Bonobo/Control:1.0','IDL:Bonobo/Embeddable:1.0']) AND "
 
248
                "repo_ids.has_one (['IDL:Bonobo/PersistStream:1.0', "
 
249
                "'IDL:Bonobo/ProgressiveDataSink:1.0', "
 
250
                "'IDL:Bonobo/PersistFile:1.0']))) AND (bonobo:supported_mime_types.defined () OR "
 
251
                "bonobo:supported_uri_schemes.defined () OR "
 
252
                "bonobo:additional_uri_schemes.defined ()) AND "
 
253
                "(((NOT bonobo:supported_mime_types.defined () OR "
 
254
                "bonobo:supported_mime_types.has ('x-directory/normal') OR "
 
255
                "bonobo:supported_mime_types.has ('x-directory/*') OR "
 
256
                "bonobo:supported_mime_types.has ('*/*')) AND "
 
257
                "(NOT bonobo:supported_uri_schemes.defined () OR "
 
258
                "bonobo:supported_uri_schemes.has ('file') OR "
 
259
                "bonobo:supported_uri_schemes.has ('*'))) OR "
 
260
                "(bonobo:additional_uri_schemes.has ('file') OR "
 
261
                "bonobo:additional_uri_schemes.has ('*'))) AND "
 
262
                "nautilus:view_as_name.defined ()) OR false) AND "
 
263
                "(has (['OAFIID:nautilus_file_manager_icon_view:42681b21-d5ca-4837-87d2-394d88ecc058', "
 
264
                "'OAFIID:nautilus_file_manager_list_view:521e489d-0662-4ad7-ac3a-832deabe111c'], iid)) ) AND "
 
265
                "(NOT test_only.defined() OR NOT test_only)";
 
266
 
 
267
        ac = bonobo_activation_activation_context_get ();
 
268
 
 
269
        g_timer_start (timer);
 
270
 
 
271
        info = bonobo_activation_query (query, sort_by, &ev);
 
272
 
 
273
        for (i = 0; i < 1000; i++) {
 
274
                Bonobo_ServerInfoList *copy;
 
275
#if 0
 
276
                info = bonobo_activation_query (query, sort_by, &ev);
 
277
 
 
278
                if (ev._major == CORBA_NO_EXCEPTION) {
 
279
                        CORBA_free (info);
 
280
                } else {
 
281
                        fprintf (stderr, "Test of query failed '%s'\n",
 
282
                                 bonobo_activation_exception_id (&ev));
 
283
                }
 
284
#else
 
285
                copy = Bonobo_ServerInfoList_duplicate (info);
 
286
                CORBA_free (copy);
 
287
#endif
 
288
        }
 
289
        g_timer_stop (timer);
 
290
 
 
291
        fprintf (stderr, "Time to query '%g'\n", g_timer_elapsed (timer, NULL));
 
292
        if (ev._major == CORBA_NO_EXCEPTION) {
 
293
                passed++;
 
294
                fprintf (stderr, "PASSED %d of %d: timed query\n", passed + failed, TOTAL_TEST_SCORE);
 
295
        } else {
 
296
                failed++;
 
297
                fprintf (stderr, "FAILED %d of %d: timed query\n", passed + failed, TOTAL_TEST_SCORE);
 
298
                CORBA_exception_free (&ev);
 
299
        }
 
300
 
 
301
        /*
 
302
         *    We wait to see if the server (sever)
 
303
         * timeout is mis-behaving [ at this stage we
 
304
         * havn't registered anything with the server ]
 
305
         */
 
306
        fprintf (stderr, "Waiting to see if the server erroneously quits\n");
 
307
        sleep (SERVER_IDLE_QUIT_TIMEOUT * 2 / 1000);
 
308
        g_assert (ORBit_small_get_connection_status (ac) ==
 
309
                  ORBIT_CONNECTION_CONNECTED);
 
310
 
 
311
        race_empty (&ev);
 
312
        
 
313
        
 
314
        
 
315
        
 
316
 
 
317
        obj = bonobo_activation_activate_from_id ("OAFIID:Empty:19991025", 0, NULL, &ev);
 
318
        if (test_object (obj, &ev, "from id") && test_empty (obj, &ev, "from id")) {
 
319
                passed++;
 
320
                fprintf (stderr, "PASSED %d of %d: IID activation\n", passed + failed, TOTAL_TEST_SCORE);
 
321
        } else {
 
322
                failed++;
 
323
                fprintf (stderr, "FAILED %d of %d: IID activation\n", passed + failed, TOTAL_TEST_SCORE);
 
324
        }
 
325
 
 
326
        obj = bonobo_activation_activate_from_id ("OAFAID:[OAFIID:Empty:19991025]", 0, NULL, &ev);
 
327
        if (test_object (obj, &ev, "from aid") && test_empty (obj, &ev, "from aid")) {
 
328
                passed++;
 
329
                fprintf (stderr, "PASSED %d of %d: AID activation\n", passed + failed, TOTAL_TEST_SCORE);
 
330
        } else {
 
331
                failed++;
 
332
                fprintf (stderr, "FAILED %d of %d: AID activation\n", passed + failed, TOTAL_TEST_SCORE);
 
333
        }
 
334
 
 
335
        obj = bonobo_activation_activate_from_id ("OAFAID:[OAFIID:Plugin:20010713]",  0, NULL, &ev);
 
336
        if (test_object (obj, &ev, "from aid") && test_plugin (obj, &ev, "from aid")) {
 
337
                passed++;
 
338
                fprintf (stderr, "PASSED %d of %d: plugin activation\n", passed + failed, TOTAL_TEST_SCORE);
 
339
        } else {
 
340
                failed++;
 
341
                fprintf (stderr, "FAILED %d of %d: plugin activation\n", passed + failed, TOTAL_TEST_SCORE);
 
342
        }
 
343
 
 
344
        obj = bonobo_activation_activate_from_id ("OAFIID:Bogus:20000526", 0, NULL, &ev);
 
345
        if (ev._major != CORBA_NO_EXCEPTION) {
 
346
                passed++;
 
347
                fprintf (stderr, "PASSED %d of %d: Broken link test : %s\n",
 
348
                         passed + failed, TOTAL_TEST_SCORE, bonobo_activation_exception_id (&ev));
 
349
                CORBA_exception_free (&ev);
 
350
        } else {
 
351
                failed++;
 
352
                fprintf (stderr, "FAILED %d of %d: Broken link test\n", passed + failed, TOTAL_TEST_SCORE);
 
353
        }
 
354
 
 
355
        if (test_bonobo_activation_server (&ev, "with broken factory link")) {
 
356
                passed++;
 
357
                fprintf (stderr, "PASSED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
358
        } else {
 
359
                failed++;
 
360
                fprintf (stderr, "FAILED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
361
        }
 
362
 
 
363
        fprintf (stderr, "Broken exe test ");
 
364
        obj = bonobo_activation_activate_from_id ("OAFIID:Broken:20000530", 0, NULL, &ev);
 
365
        if (ev._major != CORBA_NO_EXCEPTION) {
 
366
                passed++;
 
367
                fprintf (stderr, "PASSED %d of %d: Broken exe test : %s\n",
 
368
                         passed + failed, TOTAL_TEST_SCORE, bonobo_activation_exception_id (&ev));
 
369
                CORBA_exception_free (&ev);
 
370
        } else {
 
371
                failed++;
 
372
                fprintf (stderr, "FAILED %d of %d: Broken exe test\n", passed + failed, TOTAL_TEST_SCORE);
 
373
        }
 
374
 
 
375
        if (test_bonobo_activation_server (&ev, "with broken factory link")) {
 
376
                passed++;
 
377
                fprintf (stderr, "PASSED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
378
        } else {
 
379
                failed++;
 
380
                fprintf (stderr, "FAILED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
381
        }
 
382
 
 
383
        obj = bonobo_activation_activate_from_id ("OAFIID:Circular:20000530", 0, NULL, &ev);
 
384
        if (ev._major != CORBA_NO_EXCEPTION) {
 
385
                passed++;
 
386
                fprintf (stderr, "PASSED %d of %d: Circular link test : %s\n",
 
387
                         passed + failed, TOTAL_TEST_SCORE, bonobo_activation_exception_id (&ev));
 
388
                CORBA_exception_free (&ev);
 
389
        } else {
 
390
                failed++;
 
391
                fprintf (stderr, "FAILED %d of %d: Circular link test\n", passed + failed, TOTAL_TEST_SCORE);
 
392
        }
 
393
 
 
394
        if (test_bonobo_activation_server (&ev, "with broken factory link")) {
 
395
                passed++;
 
396
                fprintf (stderr, "PASSED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
397
        } else {
 
398
                failed++;
 
399
                fprintf (stderr, "FAILED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
400
        }
 
401
 
 
402
        obj = bonobo_activation_activate_from_id ("OAFIID:NotInServer:20000717", 0, NULL, &ev);
 
403
        if (ev._major != CORBA_NO_EXCEPTION) {
 
404
                passed++;
 
405
                fprintf (stderr, "PASSED %d of %d: Server that doesn't register IID test : %s\n",
 
406
                         passed + failed, TOTAL_TEST_SCORE, bonobo_activation_exception_id (&ev));
 
407
                CORBA_exception_free (&ev);
 
408
        } else {
 
409
                failed++;
 
410
                fprintf (stderr, "FAILED %d of %d: Server that doesn't register IID test\n",
 
411
                         passed + failed, TOTAL_TEST_SCORE);
 
412
        }
 
413
 
 
414
        if (test_bonobo_activation_server (&ev, "with non-registering server")) {
 
415
                passed++;
 
416
                fprintf (stderr, "PASSED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
417
        } else {
 
418
                failed++;
 
419
                fprintf (stderr, "FAILED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
420
        }
 
421
 
 
422
        obj = bonobo_activation_activate_from_id ("OAFIID:BrokenNoType:20000808", 0, NULL, &ev);
 
423
        if (ev._major != CORBA_NO_EXCEPTION) {
 
424
                failed++;
 
425
                fprintf (stderr, "FAILED %d of %d: Server with IID but no type or location : %s\n",
 
426
                         passed + failed, TOTAL_TEST_SCORE, bonobo_activation_exception_id (&ev));
 
427
                CORBA_exception_free (&ev);
 
428
        } else if (obj) {
 
429
                failed++;
 
430
                fprintf (stderr, "FAILED %d of %d: Server with IID but no type or location\n",
 
431
                         passed + failed, TOTAL_TEST_SCORE);
 
432
        } else {
 
433
                passed++;
 
434
                fprintf (stderr, "PASSED %d of %d: Server with IID but no type or location\n",
 
435
                         passed + failed, TOTAL_TEST_SCORE);
 
436
        }
 
437
 
 
438
        if (test_bonobo_activation_server (&ev, "with no-type/loc server")) {
 
439
                passed++;
 
440
                fprintf (stderr, "PASSED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
441
        } else {
 
442
                failed++;
 
443
                fprintf (stderr, "FAILED %d of %d: activation server okay\n", passed + failed, TOTAL_TEST_SCORE);
 
444
        }
 
445
 
 
446
        fprintf (stderr, "\n%d of %d tests passed (%s)\n", passed,
 
447
                 TOTAL_TEST_SCORE,
 
448
                 passed == TOTAL_TEST_SCORE? "All": "some failures");
 
449
 
 
450
        if (passed < (TOTAL_TEST_SCORE * 2 / 3)) {
 
451
                fprintf (stderr, "It looks like you havn't installed broken.server "
 
452
                         "into ${prefix}/share/bonobo-activation/, this must be done "
 
453
                         "by hand to avoid problems with normal operation.\n");
 
454
                fprintf (stderr, "Another possibility is that you failed to kill "
 
455
                         "bonobo_activation_server before running make check; try running bonobo-slay.\n");
 
456
        }
 
457
 
 
458
        if (name_service != CORBA_OBJECT_NIL)
 
459
                CORBA_Object_release (name_service, &ev);
 
460
        
 
461
        if (event_source != CORBA_OBJECT_NIL)
 
462
                CORBA_Object_release (event_source, &ev);
 
463
 
 
464
        CORBA_exception_free (&ev);
 
465
 
 
466
        if (passed == TOTAL_TEST_SCORE) {
 
467
                if (bonobo_debug_shutdown ()) {
 
468
                        return 0;
 
469
                } else {
 
470
                        return 1;
 
471
                }
 
472
        } else {
 
473
                return 1;
 
474
        }
 
475
}