1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* e-test-server-utils.c - Test scaffolding to run tests with in-tree data server.
*
* Copyright (C) 2012 Intel Corporation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
* Authors: Tristan Van Berkom <tristanvb@openismus.com>
*/
#include "e-test-server-utils.h"
#define ADDRESS_BOOK_SOURCE_UID "test-address-book"
#define CALENDAR_SOURCE_UID "test-calendar"
/* FIXME, currently we are unable to achieve server activation
* twice in a single test case, so we're using one D-Bus server
* throughout an entire test suite.
*
* When this is fixed we can migrate the D-Bus initialization
* and teardown from e_test_server_utils_run() to
* e_test_server_utils_setup() and e_test_server_utils_teardown()
* and this will transparantly change the way tests run using
* this test framework.
*/
#define GLOBAL_DBUS_DAEMON 1
#if GLOBAL_DBUS_DAEMON
static GTestDBus *global_test_dbus = NULL;
#endif
typedef struct {
ETestServerFixture *fixture;
ETestServerClosure *closure;
} FixturePair;
static gboolean
test_installed_services (void)
{
static gint use_installed_services = -1;
if (use_installed_services < 0) {
if (g_getenv ("TEST_INSTALLED_SERVICES") != NULL)
use_installed_services = 1;
else
use_installed_services = 0;
}
return use_installed_services;
}
static void
setup_environment (void)
{
g_assert (g_setenv ("XDG_DATA_HOME", EDS_TEST_WORK_DIR, TRUE));
g_assert (g_setenv ("XDG_CACHE_HOME", EDS_TEST_WORK_DIR, TRUE));
g_assert (g_setenv ("XDG_CONFIG_HOME", EDS_TEST_WORK_DIR, TRUE));
g_assert (g_setenv ("GSETTINGS_SCHEMA_DIR", EDS_TEST_SCHEMA_DIR, TRUE));
g_assert (g_setenv ("EDS_CALENDAR_MODULES", EDS_TEST_CALENDAR_DIR, TRUE));
g_assert (g_setenv ("EDS_ADDRESS_BOOK_MODULES", EDS_TEST_ADDRESS_BOOK_DIR, TRUE));
g_assert (g_setenv ("GIO_USE_VFS", "local", TRUE));
}
static void
delete_work_directory (void)
{
/* XXX Instead of complex error checking here, we should ideally use
* a recursive GDir / g_unlink() function.
*
* We cannot use GFile and the recursive delete function without
* corrupting our contained D-Bus environment with service files
* from the OS.
*/
const gchar *argv[] = { "/bin/rm", "-rf", EDS_TEST_WORK_DIR, NULL };
gboolean spawn_succeeded;
gint exit_status;
spawn_succeeded = g_spawn_sync (
NULL, (gchar **) argv, NULL, 0, NULL, NULL,
NULL, NULL, &exit_status, NULL);
g_assert (spawn_succeeded);
g_assert (WIFEXITED (exit_status));
g_assert_cmpint (WEXITSTATUS (exit_status), ==, 0);
}
static gboolean
e_test_server_utils_bootstrap_timeout (FixturePair *pair)
{
g_error ("Timed out while waiting for ESource creation from the registry");
return FALSE;
}
static void
e_test_server_utils_source_added (ESourceRegistry *registry,
ESource *source,
FixturePair *pair)
{
GError *error = NULL;
switch (pair->closure->type) {
case E_TEST_SERVER_ADDRESS_BOOK:
if (g_strcmp0 (e_source_get_uid (source), ADDRESS_BOOK_SOURCE_UID) != 0)
return;
pair->fixture->service.book_client = e_book_client_new (source, &error);
if (!pair->fixture->service.book_client)
g_error ("Unable to create the test book: %s", error->message);
if (!e_client_open_sync (E_CLIENT (pair->fixture->service.book_client), FALSE, NULL, &error))
g_error ("Unable to open book client: %s", error->message);
break;
case E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK:
if (g_strcmp0 (e_source_get_uid (source), ADDRESS_BOOK_SOURCE_UID) != 0)
return;
pair->fixture->service.book = e_book_new (source, &error);
if (!pair->fixture->service.book)
g_error ("Unable to create the test book: %s", error->message);
if (!e_book_open (pair->fixture->service.book, FALSE, &error))
g_error ("Unable to open book: %s", error->message);
break;
case E_TEST_SERVER_CALENDAR:
if (g_strcmp0 (e_source_get_uid (source), CALENDAR_SOURCE_UID) != 0)
return;
pair->fixture->service.calendar_client = e_cal_client_new (source,
pair->closure->calendar_source_type,
&error);
if (!pair->fixture->service.calendar_client)
g_error ("Unable to create the test calendar: %s", error->message);
if (!e_client_open_sync (E_CLIENT (pair->fixture->service.calendar_client), FALSE, NULL, &error))
g_error ("Unable to open calendar client: %s", error->message);
break;
case E_TEST_SERVER_DEPRECATED_CALENDAR:
if (g_strcmp0 (e_source_get_uid (source), CALENDAR_SOURCE_UID) != 0)
return;
pair->fixture->service.calendar = e_cal_new (source, pair->closure->calendar_source_type);
if (!pair->fixture->service.calendar)
g_error ("Unable to create the test calendar");
if (!e_cal_open (pair->fixture->service.calendar, FALSE, &error))
g_error ("Unable to open calendar: %s", error->message);
break;
case E_TEST_SERVER_NONE:
return;
}
g_main_loop_quit (pair->fixture->loop);
}
static gboolean
e_test_server_utils_bootstrap_idle (FixturePair *pair)
{
ESourceBackend *backend = NULL;
ESource *scratch = NULL;
GError *error = NULL;
pair->fixture->registry = e_source_registry_new_sync (NULL, &error);
if (!pair->fixture->registry)
g_error ("Unable to create the test registry: %s", error->message);
g_signal_connect (pair->fixture->registry, "source-added",
G_CALLBACK (e_test_server_utils_source_added), pair);
/* Create an address book */
switch (pair->closure->type) {
case E_TEST_SERVER_ADDRESS_BOOK:
case E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK:
scratch = e_source_new_with_uid (ADDRESS_BOOK_SOURCE_UID, NULL, &error);
if (!scratch)
g_error ("Failed to create scratch source for an addressbook: %s", error->message);
/* Ensure Book type */
backend = e_source_get_extension (scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
e_source_backend_set_backend_name (backend, "local");
break;
case E_TEST_SERVER_CALENDAR:
case E_TEST_SERVER_DEPRECATED_CALENDAR:
scratch = e_source_new_with_uid (CALENDAR_SOURCE_UID, NULL, &error);
if (!scratch)
g_error ("Failed to create scratch source for a calendar: %s", error->message);
/* Ensure Calendar type source (how to specify the backend here ?? */
backend = e_source_get_extension (scratch, E_SOURCE_EXTENSION_CALENDAR);
e_source_backend_set_backend_name (backend, "local");
break;
case E_TEST_SERVER_NONE:
break;
}
if (scratch) {
if (pair->closure->customize)
pair->closure->customize (scratch, pair->closure);
if (!e_source_registry_commit_source_sync (pair->fixture->registry, scratch, NULL, &error))
g_error ("Unable to add new addressbook source to the registry: %s", error->message);
g_object_unref (scratch);
}
if (pair->closure->type != E_TEST_SERVER_NONE)
g_timeout_add (20 * 1000, (GSourceFunc) e_test_server_utils_bootstrap_timeout, pair);
else
g_main_loop_quit (pair->fixture->loop);
return FALSE;
}
/**
* e_test_server_utils_setup:
* @fixture: A #ETestServerFixture
* @user_data: A #ETestServerClosure or derived structure provided by the test.
*
* A setup function for the #ETestServerFixture fixture
*/
void
e_test_server_utils_setup (ETestServerFixture *fixture,
gconstpointer user_data)
{
ETestServerClosure *closure = (ETestServerClosure *) user_data;
FixturePair pair = { fixture, closure };
/* Create work directory */
g_assert (g_mkdir_with_parents (EDS_TEST_WORK_DIR, 0755) == 0);
fixture->loop = g_main_loop_new (NULL, FALSE);
if (!test_installed_services ()) {
#if !GLOBAL_DBUS_DAEMON
/* Create the global dbus-daemon for this test suite */
fixture->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
/* Add the private directory with our in-tree service files */
g_test_dbus_add_service_dir (fixture->dbus, EDS_TEST_DBUS_SERVICE_DIR);
/* Start the private D-Bus daemon */
g_test_dbus_up (fixture->dbus);
#else
fixture->dbus = global_test_dbus;
#endif
}
g_idle_add ((GSourceFunc) e_test_server_utils_bootstrap_idle, &pair);
g_main_loop_run (fixture->loop);
g_signal_handlers_disconnect_by_func (fixture->registry, e_test_server_utils_source_added, &pair);
}
/**
* e_test_server_utils_teardown:
* @fixture: A #ETestServerFixture
* @user_data: A #ETestServerClosure or derived structure provided by the test.
*
* A teardown function for the #ETestServerFixture fixture
*/
void
e_test_server_utils_teardown (ETestServerFixture *fixture,
gconstpointer user_data)
{
ETestServerClosure *closure = (ETestServerClosure *) user_data;
GError *error = NULL;
switch (closure->type) {
case E_TEST_SERVER_ADDRESS_BOOK:
if (!e_client_remove_sync (E_CLIENT (fixture->service.book_client), NULL, &error)) {
g_message ("Failed to remove test book: %s (ignoring)", error->message);
g_clear_error (&error);
}
g_object_unref (fixture->service.book_client);
fixture->service.book_client = NULL;
break;
case E_TEST_SERVER_DEPRECATED_ADDRESS_BOOK:
if (!e_book_remove (fixture->service.book, &error)) {
g_message ("Failed to remove test book: %s (ignoring)", error->message);
g_clear_error (&error);
}
g_object_unref (fixture->service.book);
fixture->service.book = NULL;
break;
case E_TEST_SERVER_CALENDAR:
if (!e_client_remove_sync (E_CLIENT (fixture->service.calendar_client), NULL, &error)) {
g_message ("Failed to remove test calendar: %s (ignoring)", error->message);
g_clear_error (&error);
}
g_object_unref (fixture->service.calendar_client);
fixture->service.calendar_client = NULL;
break;
case E_TEST_SERVER_DEPRECATED_CALENDAR:
if (!e_cal_remove (fixture->service.calendar, &error)) {
g_message ("Failed to remove test calendar: %s (ignoring)", error->message);
g_clear_error (&error);
}
g_object_unref (fixture->service.calendar);
fixture->service.calendar = NULL;
case E_TEST_SERVER_NONE:
break;
}
g_object_run_dispose (G_OBJECT (fixture->registry));
g_object_unref (fixture->registry);
fixture->registry = NULL;
g_main_loop_unref (fixture->loop);
fixture->loop = NULL;
if (!test_installed_services ()) {
#if !GLOBAL_DBUS_DAEMON
/* Teardown the D-Bus Daemon
*
* Note that we intentionally leak the TestDBus daemon
* in this case, presumably this is due to some leaked
* GDBusConnection reference counting
*/
g_test_dbus_down (fixture->dbus);
g_object_unref (fixture->dbus);
fixture->dbus = NULL;
#else
fixture->dbus = NULL;
#endif
}
/* Cleanup work directory */
if (!closure->keep_work_directory && !test_installed_services ())
delete_work_directory ();
/* Destroy dynamically allocated closure */
if (closure->destroy_closure_func)
closure->destroy_closure_func (closure);
}
gint
e_test_server_utils_run (void)
{
gint tests_ret;
/* Cleanup work directory */
delete_work_directory ();
setup_environment ();
#if GLOBAL_DBUS_DAEMON
if (!test_installed_services ()) {
/* Create the global dbus-daemon for this test suite */
global_test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
/* Add the private directory with our in-tree service files */
g_test_dbus_add_service_dir (global_test_dbus, EDS_TEST_DBUS_SERVICE_DIR);
/* Start the private D-Bus daemon */
g_test_dbus_up (global_test_dbus);
}
#endif
/* Run the GTest suite */
tests_ret = g_test_run ();
#if GLOBAL_DBUS_DAEMON
if (!test_installed_services ()) {
/* Teardown the D-Bus Daemon
*
* Note that we intentionally leak the TestDBus daemon
* in this case, presumably this is due to some leaked
* GDBusConnection reference counting
*/
g_test_dbus_stop (global_test_dbus);
/* g_object_unref (global_test_dbus); */
global_test_dbus = NULL;
}
#endif
return tests_ret;
}
|