2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Tests for the ast_event API
23 * \author Russell Bryant <russell@digium.com>
27 * \todo API Calls not yet touched by a test: XXX TODO
28 * - ast_event_queue_and_cache()
29 * - ast_event_get_cached()
30 * - ast_event_report_subs()
31 * - ast_event_dump_cache()
32 * - ast_event_get_ie_type_name()
33 * - ast_event_get_ie_pltype()
34 * - ast_event_str_to_event_type()
35 * - ast_event_str_to_ie_type()
36 * - ast_event_iterator_init()
37 * - ast_event_iterator_next()
38 * - ast_event_iterator_get_ie_type()
39 * - ast_event_iterator_get_ie_uint()
40 * - ast_event_iterator_get_ie_bitflags()
41 * - ast_event_iterator_get_ie_str()
42 * - ast_event_iterator_get_ie_raw()
46 <depend>TEST_FRAMEWORK</depend>
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
53 #include "asterisk/module.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/test.h"
56 #include "asterisk/event.h"
58 static int check_event(struct ast_event *event, struct ast_test *test,
59 enum ast_event_type expected_type, const char *type_name,
60 const char *str, uint32_t uint, uint32_t bitflags)
62 enum ast_event_type type;
65 /* Check #1: Ensure event type is set properly. */
66 type = ast_event_get_type(event);
67 if (ast_event_get_type(event) != type) {
68 ast_test_status_update(test, "Expected event type: '%d', got '%d'\n",
73 /* Check #2: Check string representation of event type */
74 if (strcmp(type_name, ast_event_get_type_name(event))) {
75 ast_test_status_update(test, "Didn't get expected type name: '%s' != '%s'\n",
76 type_name, ast_event_get_type_name(event));
80 /* Check #3: Check for automatically included EID */
81 if (memcmp(&ast_eid_default, ast_event_get_ie_raw(event, AST_EVENT_IE_EID), sizeof(ast_eid_default))) {
82 ast_test_status_update(test, "Failed to get EID\n");
86 /* Check #4: Check for the string IE */
87 if (strcmp(str, ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX))) {
88 ast_test_status_update(test, "Failed to get string IE.\n");
92 /* Check #5: Check for the uint IE */
93 if (uint != ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS)) {
94 ast_test_status_update(test, "Failed to get uint IE.\n");
98 /* Check #6: Check for the bitflags IE */
99 if (bitflags != ast_event_get_ie_bitflags(event, AST_EVENT_IE_OLDMSGS)) {
100 ast_test_status_update(test, "Failed to get bitflags IE.\n");
104 /* Check #7: Check if a check for a str IE that isn't there works */
105 if ((foo = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE))) {
106 ast_test_status_update(test, "DEVICE IE check returned non-NULL %p\n", foo);
110 /* Check #8: Check if a check for a uint IE that isn't there returns 0 */
111 if (ast_event_get_ie_uint(event, AST_EVENT_IE_STATE)) {
112 ast_test_status_update(test, "OLDMSGS IE should be 0\n");
116 ast_test_status_update(test, "Event looks good.\n");
124 AST_TEST_DEFINE(event_new_test)
126 enum ast_test_result_state res = AST_TEST_PASS;
127 struct ast_event *event = NULL, *event2 = NULL;
129 static const enum ast_event_type type = AST_EVENT_CUSTOM;
130 static const char str[] = "SIP/alligatormittens";
131 static const uint32_t uint = 0xb00bface;
132 static const uint32_t bitflags = 0x12488421;
136 info->name = "ast_event_new_test";
137 info->category = "/main/event/";
138 info->summary = "Test event creation";
140 "This test exercises the API calls that allow allocation "
142 return AST_TEST_NOT_RUN;
148 * Test 2 methods of event creation:
150 * 1) Dynamic via appending each IE individually.
151 * 2) Statically, with all IEs in ast_event_new().
154 ast_test_status_update(test, "First, test dynamic event creation...\n");
156 if (!(event = ast_event_new(type, AST_EVENT_IE_END))) {
157 ast_test_status_update(test, "Failed to allocate ast_event object.\n");
162 if (ast_event_append_ie_str(&event, AST_EVENT_IE_MAILBOX, str)) {
163 ast_test_status_update(test, "Failed to append str IE\n");
168 if (ast_event_append_ie_uint(&event, AST_EVENT_IE_NEWMSGS, uint)) {
169 ast_test_status_update(test, "Failed to append uint IE\n");
174 if (ast_event_append_ie_bitflags(&event, AST_EVENT_IE_OLDMSGS, bitflags)) {
175 ast_test_status_update(test, "Failed to append bitflags IE\n");
180 if (ast_event_append_eid(&event)) {
181 ast_test_status_update(test, "Failed to append EID\n");
186 if (check_event(event, test, type, "Custom", str, uint, bitflags)) {
187 ast_test_status_update(test, "Dynamically generated event broken\n");
192 event2 = ast_event_new(type,
193 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, str,
194 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, uint,
195 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, bitflags,
199 ast_test_status_update(test, "Failed to allocate ast_event object.\n");
204 if (check_event(event2, test, type, "Custom", str, uint, bitflags)) {
205 ast_test_status_update(test, "Statically generated event broken\n");
210 if (ast_event_get_size(event) != ast_event_get_size(event2)) {
211 ast_test_status_update(test, "Events expected to be identical have different size: %d != %d\n",
212 (int) ast_event_get_size(event),
213 (int) ast_event_get_size(event2));
220 ast_event_destroy(event);
225 ast_event_destroy(event2);
232 struct event_sub_data {
236 static void event_sub_cb(const struct ast_event *event, void *d)
238 struct event_sub_data *data = d;
245 * \brief Test event subscriptions
247 * - Query for existing Subscriptions:
248 * - ast_event_check_subscriber()
250 AST_TEST_DEFINE(event_sub_test)
252 enum ast_test_result_state res = AST_TEST_PASS;
253 struct ast_event *event;
255 enum ast_event_subscriber_res sub_res;
257 struct ast_event_sub *sub;
258 struct event_sub_data data;
259 const unsigned int expected_count;
274 info->name = "ast_event_subscribe_test";
275 info->category = "/main/event/";
276 info->summary = "Test event subscriptions";
278 "This test exercises the API calls that allow subscriptions "
280 return AST_TEST_NOT_RUN;
287 * - allocate normally
288 * - subscribe to all CUSTOM events
291 * - allocate dynamically
292 * - subscribe to all CUSTOM events
293 * - add payload checks
296 * - allocate normally
297 * - subscribe to all events with an IE check
300 test_subs[0].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb, "test_sub", &test_subs[0].data,
302 if (!test_subs[0].sub) {
303 ast_test_status_update(test, "Failed to create test_subs[0].sub\n");
308 if (strcmp(ast_event_subscriber_get_description(test_subs[0].sub), "test_sub")) {
309 ast_test_status_update(test,
310 "Unexpected subscription description on test_subs[0].sub\n");
315 test_subs[1].sub = ast_event_subscribe_new(AST_EVENT_CUSTOM, event_sub_cb, &test_subs[1].data);
316 if (!test_subs[1].sub) {
317 ast_test_status_update(test, "Failed to create test_subs[1].sub\n");
322 /* For the sake of exercising destruction before activation */
323 ast_event_sub_destroy(test_subs[1].sub);
325 test_subs[1].sub = ast_event_subscribe_new(AST_EVENT_CUSTOM, event_sub_cb, &test_subs[1].data);
326 if (!test_subs[1].sub) {
327 ast_test_status_update(test, "Failed to create test_subs[1].sub\n");
332 if (strcmp(ast_event_subscriber_get_description(test_subs[1].sub), "")) {
333 ast_event_sub_destroy(test_subs[1].sub);
334 test_subs[1].sub = NULL;
335 ast_test_status_update(test,
336 "Unexpected subscription description on test_subs[1].sub\n");
341 if (ast_event_sub_append_ie_uint(test_subs[1].sub, AST_EVENT_IE_NEWMSGS, 3)) {
342 ast_event_sub_destroy(test_subs[1].sub);
343 test_subs[1].sub = NULL;
344 ast_test_status_update(test, "Failed to append uint IE to test_subs[1].sub\n");
349 if (ast_event_sub_append_ie_bitflags(test_subs[1].sub, AST_EVENT_IE_NEWMSGS, 1)) {
350 ast_event_sub_destroy(test_subs[1].sub);
351 test_subs[1].sub = NULL;
352 ast_test_status_update(test, "Failed to append bitflags IE to test_subs[1].sub\n");
357 if (ast_event_sub_append_ie_str(test_subs[1].sub, AST_EVENT_IE_DEVICE, "FOO/bar")) {
358 ast_event_sub_destroy(test_subs[1].sub);
359 test_subs[1].sub = NULL;
360 ast_test_status_update(test, "Failed to append str IE to test_subs[1].sub\n");
365 if (ast_event_sub_append_ie_raw(test_subs[1].sub, AST_EVENT_IE_MAILBOX, "800 km",
367 ast_event_sub_destroy(test_subs[1].sub);
368 test_subs[1].sub = NULL;
369 ast_test_status_update(test, "Failed to append raw IE to test_subs[1].sub\n");
374 if (ast_event_sub_append_ie_exists(test_subs[1].sub, AST_EVENT_IE_DEVICE)) {
375 ast_event_sub_destroy(test_subs[1].sub);
376 test_subs[1].sub = NULL;
377 ast_test_status_update(test, "Failed to append exists IE to test_subs[1].sub\n");
382 if (ast_event_sub_activate(test_subs[1].sub)) {
383 ast_event_sub_destroy(test_subs[1].sub);
384 test_subs[1].sub = NULL;
385 ast_test_status_update(test, "Failed to activate test_subs[1].sub\n");
390 test_subs[2].sub = ast_event_subscribe(AST_EVENT_ALL, event_sub_cb, "test_sub", &test_subs[2].data,
391 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
393 if (!test_subs[2].sub) {
394 ast_test_status_update(test, "Failed to create test_subs[2].sub\n");
400 * Exercise the API call to check for existing subscriptions.
403 sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
404 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
406 if (sub_res != AST_EVENT_SUB_EXISTS) {
407 ast_test_status_update(test, "subscription did not exist\n");
412 sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
413 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "BOOBS",
415 if (sub_res != AST_EVENT_SUB_NONE) {
416 ast_test_status_update(test, "Someone subscribed to updates on boobs, lol? (%d)\n", sub_res);
422 * Fire off some events and track what was received in the callback
425 * - simple custom event (will match sub 1 and 3)
428 * - custom event with payloads that satisfy every payload check
429 * for sub #2 (will match sub 1, 2, and 3)
432 * - custom event that should only match sub #1
435 event = ast_event_new(AST_EVENT_CUSTOM,
436 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
439 ast_test_status_update(test, "Failed to create event\n");
443 if (ast_event_queue(event)) {
444 ast_event_destroy(event);
446 ast_test_status_update(test, "Failed to queue event\n");
451 event = ast_event_new(AST_EVENT_CUSTOM,
452 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
453 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "800 km", strlen("800 km"),
454 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 3,
457 ast_test_status_update(test, "Failed to create event\n");
461 if (ast_event_queue(event)) {
462 ast_event_destroy(event);
464 ast_test_status_update(test, "Failed to queue event\n");
469 event = ast_event_new(AST_EVENT_CUSTOM,
470 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "blah",
471 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "801 km", strlen("801 km"),
472 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 0,
475 ast_test_status_update(test, "Failed to create event\n");
479 if (ast_event_queue(event)) {
480 ast_event_destroy(event);
482 ast_test_status_update(test, "Failed to queue event\n");
489 * Check the results of the test.
491 * First of all, event distribution is asynchronous from the event producer,
492 * so knowing when to continue from here and check results is an instance of
493 * the halting problem. A few seconds really should be more than enough time.
494 * If something was actually blocking event distribution that long, I would call
503 ast_test_status_update(test, "Sleeping a few seconds to allow event propagation...\n");
506 for (i = 0; i < ARRAY_LEN(test_subs); i++) {
507 if (test_subs[i].data.count != test_subs[i].expected_count) {
508 ast_test_status_update(test, "Unexpected callback count, %u != %u for #%d\n",
509 test_subs[i].data.count, test_subs[i].expected_count, i);
515 for (i = 0; i < ARRAY_LEN(test_subs); i++) {
516 if (test_subs[i].sub) {
517 test_subs[i].sub = ast_event_unsubscribe(test_subs[i].sub);
524 static int unload_module(void)
526 AST_TEST_UNREGISTER(event_new_test);
527 AST_TEST_UNREGISTER(event_sub_test);
532 static int load_module(void)
534 AST_TEST_REGISTER(event_new_test);
535 AST_TEST_REGISTER(event_sub_test);
537 return AST_MODULE_LOAD_SUCCESS;
540 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_event API Tests");