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_get_ie_type_name()
29 * - ast_event_get_ie_pltype()
30 * - ast_event_iterator_init()
31 * - ast_event_iterator_next()
32 * - ast_event_iterator_get_ie_type()
33 * - ast_event_iterator_get_ie_uint()
34 * - ast_event_iterator_get_ie_str()
38 <depend>TEST_FRAMEWORK</depend>
39 <support_level>core</support_level>
44 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
46 #include "asterisk/module.h"
47 #include "asterisk/utils.h"
48 #include "asterisk/test.h"
49 #include "asterisk/event.h"
51 static int check_event(struct ast_event *event, struct ast_test *test,
52 enum ast_event_type expected_type, const char *type_name,
53 const char *str, uint32_t uint)
55 enum ast_event_type type;
58 /* Check #1: Ensure event type is set properly. */
59 type = ast_event_get_type(event);
60 if (ast_event_get_type(event) != type) {
61 ast_test_status_update(test, "Expected event type: '%d', got '%d'\n",
66 /* Check #4: Check for the string IE */
67 if (strcmp(str, ast_event_get_ie_str(event, AST_EVENT_IE_CEL_USEREVENT_NAME))) {
68 ast_test_status_update(test, "Failed to get string IE.\n");
72 /* Check #5: Check for the uint IE */
73 if (uint != ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_AMAFLAGS)) {
74 ast_test_status_update(test, "Failed to get uint IE.\n");
78 /* Check #6: Check if a check for a str IE that isn't there works */
79 if ((foo = ast_event_get_ie_str(event, AST_EVENT_IE_CEL_CIDNAME))) {
80 ast_test_status_update(test, "CEL_CIDNAME IE check returned non-NULL %p\n", foo);
84 /* Check #7: Check if a check for a uint IE that isn't there returns 0 */
85 if (ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TIME_USEC)) {
86 ast_test_status_update(test, "UNIQUEID IE should be 0\n");
90 ast_test_status_update(test, "Event looks good.\n");
98 AST_TEST_DEFINE(event_new_test)
100 enum ast_test_result_state res = AST_TEST_PASS;
101 struct ast_event *event = NULL, *event2 = NULL;
103 static const enum ast_event_type type = AST_EVENT_CUSTOM;
104 static const char str[] = "SIP/alligatormittens";
105 static const uint32_t uint = 0xb00bface;
109 info->name = "ast_event_new_test";
110 info->category = "/main/event/";
111 info->summary = "Test event creation";
113 "This test exercises the API calls that allow allocation "
115 return AST_TEST_NOT_RUN;
121 * Test 2 methods of event creation:
123 * 1) Dynamic via appending each IE individually.
124 * 2) Statically, with all IEs in ast_event_new().
127 ast_test_status_update(test, "First, test dynamic event creation...\n");
129 if (!(event = ast_event_new(type, AST_EVENT_IE_END))) {
130 ast_test_status_update(test, "Failed to allocate ast_event object.\n");
135 if (ast_event_append_ie_str(&event, AST_EVENT_IE_CEL_USEREVENT_NAME, str)) {
136 ast_test_status_update(test, "Failed to append str IE\n");
141 if (ast_event_append_ie_uint(&event, AST_EVENT_IE_CEL_AMAFLAGS, uint)) {
142 ast_test_status_update(test, "Failed to append uint IE\n");
147 if (check_event(event, test, type, "Custom", str, uint)) {
148 ast_test_status_update(test, "Dynamically generated event broken\n");
153 event2 = ast_event_new(type,
154 AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, str,
155 AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, uint,
159 ast_test_status_update(test, "Failed to allocate ast_event object.\n");
164 if (check_event(event2, test, type, "Custom", str, uint)) {
165 ast_test_status_update(test, "Statically generated event broken\n");
170 if (ast_event_get_size(event) != ast_event_get_size(event2)) {
171 ast_test_status_update(test, "Events expected to be identical have different size: %d != %d\n",
172 (int) ast_event_get_size(event),
173 (int) ast_event_get_size(event2));
180 ast_event_destroy(event);
185 ast_event_destroy(event2);
192 struct event_sub_data {
196 static int unload_module(void)
198 AST_TEST_UNREGISTER(event_new_test);
203 static int load_module(void)
205 AST_TEST_REGISTER(event_new_test);
207 return AST_MODULE_LOAD_SUCCESS;
210 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_event API Tests");