forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_dbus.c
476 lines (372 loc) · 12.1 KB
/
testmod_dbus.c
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "dbus.h"
#include <stdio.h> // printf() & co
#include <time.h> // time()
#include <tests.h>
#include <tests_plugin.h>
typedef struct
{
char * lookupSignalName;
char * receivedKeyName;
DBusConnection * connection;
int stop;
} TestContext;
#define TEST_TIMEOUT 1
#define TEST_DISPATCH_TIMEOUT 200
/** D-Bus bus type used by tests */
DBusBusType testBusType;
/** key namespace to use for tests */
char * testKeyNamespace;
/**
* @internal
* Process D-Bus messages and check for expected message.
*
* @param connection D-Bus connection
* @param message received D-Bus message
* @param data test context
* @return message handler result
*/
static DBusHandlerResult receiveMessageHandler (DBusConnection * connection ELEKTRA_UNUSED, DBusMessage * message, void * data)
{
TestContext * context = (TestContext *) data;
char * interface = "org.libelektra";
if (dbus_message_is_signal (message, interface, context->lookupSignalName))
{
char * keyName;
DBusError error;
dbus_error_init (&error);
// read key name from message
dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &keyName, DBUS_TYPE_INVALID);
if (dbus_error_is_set (&error))
{
ELEKTRA_LOG_WARNING ("Failed to read message: %s", error.message);
}
else
{
// Message received, stop dispatching
context->receivedKeyName = keyName;
context->stop = 1;
}
dbus_error_free (&error);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
/**
* @internal
* Dispatch messages and declares timeout if dispatching is not stopped within
* TEST_TIMEOUT.
*
* @param context test context
*/
static void runDispatch (TestContext * context)
{
time_t now;
time_t start = time (NULL);
context->stop = 0;
while (!context->stop && dbus_connection_read_write_dispatch (context->connection, TEST_DISPATCH_TIMEOUT))
{
now = time (NULL);
// Stop dispatching after one second
if (now - start > TEST_TIMEOUT)
{
succeed_if (0, "timeout exceeded; test failed");
break;
}
}
}
/**
* @internal
* Create new test context.
*
* @param connection D-Bus connection
* @param signalName Expected signal name
* @return Context
*/
static TestContext * createTestContext (DBusConnection * connection, char * signalName)
{
TestContext * context = elektraMalloc (sizeof *context);
exit_if_fail (context, "malloc failed");
context->lookupSignalName = signalName;
context->connection = connection;
context->receivedKeyName = "";
return context;
}
/**
* @internal
* Get and setup D-Bus connection.
*
* @param type D-Bus bus type
* @return D-Bus connection or NULL on error
*/
static DBusConnection * getDbusConnection (DBusBusType type)
{
DBusError error;
dbus_error_init (&error);
DBusConnection * connection = dbus_bus_get (type, &error);
if (connection == NULL)
{
printf ("connect: Failed to open connection to %s message bus: %s\n", (type == DBUS_BUS_SYSTEM) ? "system" : "session",
error.message);
dbus_error_free (&error);
dbus_shutdown ();
return NULL;
}
dbus_error_free (&error);
dbus_connection_set_exit_on_disconnect (connection, FALSE);
return connection;
}
static int test_prerequisites (void)
{
printf ("testing prerequisites\n");
printf ("detecting available bus types - please ignore single error messages prefixed with \"connect:\"\n");
DBusConnection * systemBus = getDbusConnection (DBUS_BUS_SYSTEM);
DBusConnection * sessionBus = getDbusConnection (DBUS_BUS_SESSION);
int success = 0;
if (systemBus != NULL || sessionBus != NULL)
{
// Set bus type for tests
// NOTE brew dbus on MacOs supports session by out of the box while session
// bus is not available without further configuration on Linux
if (systemBus)
{
testBusType = DBUS_BUS_SYSTEM;
testKeyNamespace = "system";
}
else if (sessionBus)
{
testBusType = DBUS_BUS_SESSION;
testKeyNamespace = "user";
}
success = 1;
}
if (systemBus) dbus_connection_unref (systemBus);
if (sessionBus) dbus_connection_unref (sessionBus);
return success;
}
static void test_keyAdded (void)
{
printf ("test adding keys\n");
// (namespace)/tests/foo
Key * parentKey = keyNew (testKeyNamespace, KEY_END);
keyAddName (parentKey, "tests/foo");
// (namespace)/tests/foo/bar
Key * toAdd = keyDup (parentKey);
keyAddName (toAdd, "bar");
keySetString (toAdd, "test");
KeySet * ks = ksNew (0, KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("dbus");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// add key to keyset
ksAppendKey (ks, toAdd);
DBusConnection * connection = getDbusConnection (testBusType);
TestContext * context = createTestContext (connection, "KeyAdded");
elektraDbusSetupReceiveMessage (connection, receiveMessageHandler, (void *) context);
plugin->kdbSet (plugin, ks, parentKey);
runDispatch (context);
succeed_if_same_string (keyName (toAdd), context->receivedKeyName);
elektraFree (context);
elektraDbusTeardownReceiveMessage (connection, receiveMessageHandler, (void *) context);
dbus_connection_unref (connection);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_keyChanged (void)
{
printf ("test changing keys\n");
// All keys created by keyNew have the KEY_FLAG_SYNC set and will be
// detected as changed by the dbus plugin
// This flag is only cleared after kdbSet or when keys come from a backend.
// (namespace)/tests/foo
Key * parentKey = keyNew (testKeyNamespace, KEY_END);
keyAddName (parentKey, "tests/foo");
// (namespace)/tests/foo/bar
Key * toChange = keyDup (parentKey);
keyAddName (toChange, "bar");
keySetString (toChange, "test");
KeySet * ks = ksNew (2, toChange, KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("dbus");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// change key in keyset
keySetString (toChange, "new value");
DBusConnection * connection = getDbusConnection (testBusType);
TestContext * context = createTestContext (connection, "KeyChanged");
elektraDbusSetupReceiveMessage (connection, receiveMessageHandler, (void *) context);
plugin->kdbSet (plugin, ks, parentKey);
runDispatch (context);
succeed_if_same_string (keyName (toChange), context->receivedKeyName);
elektraFree (context);
elektraDbusTeardownReceiveMessage (connection, receiveMessageHandler, (void *) context);
dbus_connection_unref (connection);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_keyDeleted (void)
{
printf ("test deleting keys\n");
// (namespace)/tests/foo
Key * parentKey = keyNew (testKeyNamespace, KEY_END);
keyAddName (parentKey, "tests/foo");
// (namespace)/tests/foo/bar
Key * toDelete = keyDup (parentKey);
keyAddName (toDelete, "bar");
keySetString (toDelete, "test");
KeySet * ks = ksNew (1, keyDup (toDelete), KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("dbus");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// remove key from keyset
Key * deleted = ksLookup (ks, toDelete, KDB_O_POP);
succeed_if (deleted != NULL, "key was not found");
DBusConnection * connection = getDbusConnection (testBusType);
TestContext * context = createTestContext (connection, "KeyDeleted");
elektraDbusSetupReceiveMessage (connection, receiveMessageHandler, (void *) context);
plugin->kdbSet (plugin, ks, parentKey);
runDispatch (context);
succeed_if_same_string (keyName (toDelete), context->receivedKeyName);
elektraFree (context);
elektraDbusTeardownReceiveMessage (connection, receiveMessageHandler, (void *) context);
dbus_connection_unref (connection);
keyDel (toDelete);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_announceOnce (void)
{
printf ("test announce once\n");
// (namespace)/tests/foo
Key * parentKey = keyNew (testKeyNamespace, KEY_END);
keyAddName (parentKey, "tests/foo");
// (namespace)/tests/foo/bar/#0
Key * toAdd1 = keyDup (parentKey);
keyAddName (toAdd1, "bar/#0");
keySetString (toAdd1, "test");
// (namespace)/tests/foo/bar/#1
Key * toAdd2 = keyDup (toAdd1);
keySetBaseName (toAdd2, "#1");
// (namespace)/tests/foo/bar
Key * toChange = keyDup (parentKey);
keyAddName (toChange, "bar");
keySetString (toChange, "test");
KeySet * ks = ksNew (1, toChange, KS_END);
KeySet * conf = ksNew (1, keyNew ("/announce", KEY_VALUE, "once", KEY_END), KS_END);
PLUGIN_OPEN ("dbus");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// modify keyset
ksAppendKey (ks, toAdd1);
ksAppendKey (ks, toAdd2);
keySetString (toChange, "new value");
DBusConnection * connection = getDbusConnection (testBusType);
TestContext * context = createTestContext (connection, "Commit");
elektraDbusSetupReceiveMessage (connection, receiveMessageHandler, (void *) context);
plugin->kdbSet (plugin, ks, parentKey);
runDispatch (context);
succeed_if_same_string (keyName (parentKey), context->receivedKeyName);
elektraFree (context);
elektraDbusTeardownReceiveMessage (connection, receiveMessageHandler, (void *) context);
dbus_connection_unref (connection);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_cascadedChangeNotification (void)
{
printf ("test change notification with cascaded parent key\n");
Key * parentKey = keyNew ("/tests/foo", KEY_END);
// (namespace)/tests/foo
Key * completeParentKey = keyNew (testKeyNamespace, KEY_END);
keyAddName (completeParentKey, "tests/foo");
// (namespace)/tests/foo/bar
Key * toAdd = keyDup (completeParentKey);
keyAddName (toAdd, "bar");
keySetString (toAdd, "test");
KeySet * ks = ksNew (1, completeParentKey, KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("dbus");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// add key to keyset
ksAppendKey (ks, toAdd);
DBusConnection * connection = getDbusConnection (testBusType);
TestContext * context = createTestContext (connection, "KeyAdded");
elektraDbusSetupReceiveMessage (connection, receiveMessageHandler, (void *) context);
plugin->kdbSet (plugin, ks, parentKey);
runDispatch (context);
succeed_if_same_string (keyName (toAdd), context->receivedKeyName);
elektraFree (context);
elektraDbusTeardownReceiveMessage (connection, receiveMessageHandler, (void *) context);
dbus_connection_unref (connection);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_cascadedAnnounceOnce (void)
{
printf ("test announce once with cascaded parent key\n");
Key * parentKey = keyNew ("/tests/foo", KEY_END);
// (namespace)/tests/foo
Key * completeParentKey = keyNew (testKeyNamespace, KEY_END);
keyAddName (completeParentKey, "tests/foo");
// (namespace)/tests/foo/bar
Key * toAdd = keyDup (completeParentKey);
keyAddName (toAdd, "bar");
keySetString (toAdd, "test");
KeySet * ks = ksNew (1, completeParentKey, KS_END);
KeySet * conf = ksNew (1, keyNew ("/announce", KEY_VALUE, "once", KEY_END), KS_END);
PLUGIN_OPEN ("dbus");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// add key to keyset
ksAppendKey (ks, toAdd);
DBusConnection * connection = getDbusConnection (testBusType);
TestContext * context = createTestContext (connection, "Commit");
elektraDbusSetupReceiveMessage (connection, receiveMessageHandler, (void *) context);
plugin->kdbSet (plugin, ks, parentKey);
runDispatch (context);
succeed_if_same_string (keyName (completeParentKey), context->receivedKeyName);
elektraFree (context);
elektraDbusTeardownReceiveMessage (connection, receiveMessageHandler, (void *) context);
dbus_connection_unref (connection);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
int main (int argc, char ** argv)
{
printf ("DBUS TESTS\n");
printf ("==========\n\n");
init (argc, argv);
// Test if dbus is available
if (test_prerequisites ())
{
// Test added, changed & deleted
test_keyAdded ();
test_keyChanged ();
test_keyDeleted ();
test_announceOnce ();
test_cascadedAnnounceOnce ();
test_cascadedChangeNotification ();
}
else
{
printf ("warning no dbus daemon available; skipping tests that require dbus\n");
}
print_result ("testmod_dbus");
dbus_shutdown ();
return nbError;
}