forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestmod_dbusrecv.c
282 lines (223 loc) · 7.46 KB
/
testmod_dbusrecv.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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "dbusrecv.h"
#include <stdio.h> // printf() & co
#include <kdbio/uv.h>
#include <kdbioplugin.h>
#include <uv.h>
#include <tests.h>
#include <tests_plugin.h>
#define TEST_TIMEOUT 1000
Key * test_callbackKey;
uv_loop_t * test_callbackLoop;
/** D-Bus bus type used by tests */
DBusBusType testBusType;
/**
* @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);
return NULL;
}
dbus_error_free (&error);
dbus_connection_set_exit_on_disconnect (connection, FALSE);
return connection;
}
/**
* @internal
* Send Elektra's D-Bus message.
*
* @param signalName signal name
* @param keyName key name
*/
static void dbusSendMessage (const char * signalName, const char * keyName)
{
DBusMessage * message;
const char * interface = "org.libelektra";
const char * path = "/org/libelektra/configuration";
DBusConnection * connection = getDbusConnection (testBusType);
exit_if_fail (connection != NULL, "could not get bus connection");
message = dbus_message_new_signal (path, interface, signalName);
exit_if_fail (message, "could not allocate dbus message");
exit_if_fail (dbus_message_append_args (message, DBUS_TYPE_STRING, &keyName, DBUS_TYPE_INVALID), "could not add message arguments");
dbus_connection_send (connection, message, NULL);
dbus_message_unref (message);
dbus_connection_unref (connection);
return;
}
/**
* Timeout for tests.
*
* Creates a failure and stops the event loop
*
* @param timerOp timer operation
*/
static void test_timerCallback (ElektraIoTimerOperation * timerOp ELEKTRA_UNUSED)
{
succeed_if (0, "timeout exceeded; test failed");
uv_stop (test_callbackLoop);
}
/**
* @internal
* Called by plugin when a notification was received.
* The key is saved to be evaluated by the current test and the event loop is
* stopped.
*
* @see ElektraNotificationCallback (kdbnotificationinternal.h)
*
* @param key changed key
* @param context notification callback context
*/
static void test_notificationCallback (Key * key, ElektraNotificationCallbackContext * context ELEKTRA_UNUSED)
{
test_callbackKey = key;
uv_stop (test_callbackLoop);
}
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;
}
else if (sessionBus)
{
testBusType = DBUS_BUS_SESSION;
}
success = 1;
}
if (systemBus) dbus_connection_unref (systemBus);
if (sessionBus) dbus_connection_unref (sessionBus);
return success;
}
static void test_commit (uv_loop_t * loop, ElektraIoInterface * binding)
{
printf ("test commit\n");
KeySet * conf = ksNew (1, keyNew ("user:/announce", KEY_VALUE, "once", KEY_END), KS_END);
PLUGIN_OPEN ("dbusrecv");
ksDel (plugin->global);
plugin->global =
ksNew (5, keyNew ("system:/elektra/io/binding", KEY_BINARY, KEY_SIZE, sizeof (binding), KEY_VALUE, &binding, KEY_END),
keyNew ("system:/elektra/notification/callback", KEY_FUNC, test_notificationCallback, KEY_END), KS_END);
// call open again after correctly setting up global keyset
plugin->kdbOpen (plugin, NULL);
char * expectedKeyName = "system:/tests/testmod_dbusrecv/changed";
dbusSendMessage ("Commit", expectedKeyName);
ElektraIoTimerOperation * timerOp = elektraIoNewTimerOperation (TEST_TIMEOUT, 1, test_timerCallback, NULL);
elektraIoBindingAddTimer (binding, timerOp);
test_callbackKey = NULL;
test_callbackLoop = loop;
uv_run (loop, UV_RUN_DEFAULT);
succeed_if_same_string (expectedKeyName, keyName (test_callbackKey));
elektraIoBindingRemoveTimer (timerOp);
elektraFree (timerOp);
keyDel (test_callbackKey);
ksDel (plugin->global);
PLUGIN_CLOSE ();
}
static void test_keyAdded (uv_loop_t * loop, ElektraIoInterface * binding)
{
printf ("test adding keys\n");
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("dbusrecv");
ksDel (plugin->global);
plugin->global =
ksNew (5, keyNew ("system:/elektra/io/binding", KEY_BINARY, KEY_SIZE, sizeof (binding), KEY_VALUE, &binding, KEY_END),
keyNew ("system:/elektra/notification/callback", KEY_FUNC, test_notificationCallback, KEY_END), KS_END);
// call open again after correctly setting up global keyset
plugin->kdbOpen (plugin, NULL);
char * expectedKeyName = "system:/tests/testmod_dbusrecv/added";
dbusSendMessage ("KeyAdded", expectedKeyName);
ElektraIoTimerOperation * timerOp = elektraIoNewTimerOperation (TEST_TIMEOUT, 1, test_timerCallback, NULL);
elektraIoBindingAddTimer (binding, timerOp);
test_callbackKey = NULL;
test_callbackLoop = loop;
uv_run (loop, UV_RUN_DEFAULT);
succeed_if_same_string (expectedKeyName, keyName (test_callbackKey));
elektraIoBindingRemoveTimer (timerOp);
elektraFree (timerOp);
keyDel (test_callbackKey);
ksDel (plugin->global);
PLUGIN_CLOSE ();
}
static void test_keyChanged (uv_loop_t * loop, ElektraIoInterface * binding)
{
printf ("test changing keys\n");
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("dbusrecv");
ksDel (plugin->global);
plugin->global =
ksNew (5, keyNew ("system:/elektra/io/binding", KEY_BINARY, KEY_SIZE, sizeof (binding), KEY_VALUE, &binding, KEY_END),
keyNew ("system:/elektra/notification/callback", KEY_FUNC, test_notificationCallback, KEY_END), KS_END);
// call open again after correctly setting up global keyset
plugin->kdbOpen (plugin, NULL);
char * expectedKeyName = "system:/tests/testmod_dbusrecv/changed";
dbusSendMessage ("KeyChanged", expectedKeyName);
ElektraIoTimerOperation * timerOp = elektraIoNewTimerOperation (TEST_TIMEOUT, 1, test_timerCallback, NULL);
elektraIoBindingAddTimer (binding, timerOp);
test_callbackKey = NULL;
test_callbackLoop = loop;
uv_run (loop, UV_RUN_DEFAULT);
succeed_if_same_string (expectedKeyName, keyName (test_callbackKey));
elektraIoBindingRemoveTimer (timerOp);
elektraFree (timerOp);
keyDel (test_callbackKey);
ksDel (plugin->global);
PLUGIN_CLOSE ();
}
int main (int argc, char ** argv)
{
printf ("DBUSRECV TESTS\n");
printf ("==============\n\n");
init (argc, argv);
// Test if dbus is available
if (test_prerequisites ())
{
uv_loop_t * loop = uv_default_loop ();
ElektraIoInterface * binding = elektraIoUvNew (loop);
// Test change types
test_commit (loop, binding);
test_keyAdded (loop, binding);
test_keyChanged (loop, binding);
elektraIoBindingCleanup (binding);
uv_run (loop, UV_RUN_ONCE);
#ifdef HAVE_LIBUV1
uv_loop_close (loop);
#elif HAVE_LIBUV0
uv_loop_delete (loop);
#endif
}
else
{
printf ("warning: no dbus daemon available; skipping tests that require dbus\n");
}
print_result ("testmod_dbusrecv");
dbus_shutdown ();
return nbError;
}