2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Kinsey Moore <kmoore@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 CEL unit tests
23 * \author Kinsey Moore <kmoore@digium.com>
28 <depend>TEST_FRAMEWORK</depend>
29 <support_level>core</support_level>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/module.h"
38 #include "asterisk/test.h"
39 #include "asterisk/cel.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/linkedlists.h"
42 #include "asterisk/chanvars.h"
43 #include "asterisk/utils.h"
44 #include "asterisk/causes.h"
45 #include "asterisk/time.h"
46 #include "asterisk/bridge.h"
47 #include "asterisk/bridge_basic.h"
48 #include "asterisk/stasis_channels.h"
49 #include "asterisk/stasis_bridges.h"
50 #include "asterisk/json.h"
51 #include "asterisk/features.h"
52 #include "asterisk/core_local.h"
54 #define TEST_CATEGORY "/main/cel/"
56 #define CHANNEL_TECH_NAME "CELTestChannel"
58 /*! \brief A placeholder for Asterisk's 'real' CEL configuration */
59 static struct ast_cel_general_config *saved_config;
61 /*! \brief The CEL config used for CEL unit tests */
62 static struct ast_cel_general_config *cel_test_config;
64 /*! \brief Lock used for synchronizing test execution stages with received events */
65 ast_mutex_t mid_test_sync_lock;
67 /*! \brief Lock used with sync_out for checking the end of test execution */
68 ast_mutex_t sync_lock;
70 /*! \brief Condition used for checking the end of test execution */
73 /*! \brief Flag used to trigger a mid-test synchronization, access controlled by mid_test_sync_lock */
74 int do_mid_test_sync = 0;
76 /*! \brief A channel technology used for the unit tests */
77 static struct ast_channel_tech test_cel_chan_tech = {
78 .type = CHANNEL_TECH_NAME,
79 .description = "Mock channel technology for CEL tests",
82 /*! \brief A 1 second sleep */
83 static struct timespec to_sleep = {1, 0};
85 static void do_sleep(void)
87 while ((nanosleep(&to_sleep, &to_sleep) == -1) && (errno == EINTR));
90 #define APPEND_EVENT(chan, ev_type, userevent, extra, peer) do { \
91 if (append_expected_event(chan, ev_type, userevent, extra, peer)) { \
92 return AST_TEST_FAIL; \
96 #define APPEND_EVENT_SNAPSHOT(snapshot, ev_type, userevent, extra, peer) do { \
97 if (append_expected_event_snapshot(snapshot, ev_type, userevent, extra, peer)) { \
98 return AST_TEST_FAIL; \
102 #define APPEND_DUMMY_EVENT() do { \
103 if (append_dummy_event()) { \
104 return AST_TEST_FAIL; \
108 #define CONF_EXIT(channel, bridge) do { \
109 ast_test_validate(test, 0 == ast_bridge_depart(channel)); \
110 CONF_EXIT_EVENT(channel, bridge); \
114 #define CONF_EXIT_EVENT(channel, bridge) do { \
115 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
116 extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
117 ast_test_validate(test, extra != NULL); \
118 APPEND_EVENT(channel, AST_CEL_CONF_EXIT, NULL, extra, NULL); \
121 #define CONF_EXIT_SNAPSHOT(channel, bridge) do { \
122 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
123 extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
124 ast_test_validate(test, extra != NULL); \
125 APPEND_EVENT_SNAPSHOT(channel, AST_CEL_CONF_EXIT, NULL, extra, NULL); \
128 #define CONF_ENTER_EVENT(channel, bridge) do { \
129 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
130 extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
131 ast_test_validate(test, extra != NULL); \
132 APPEND_EVENT(channel, AST_CEL_CONF_ENTER, NULL, extra, NULL); \
135 #define BRIDGE_TO_CONF(first, second, third, bridge) do { \
136 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
137 extra = ast_json_pack("{s: s, s: s}", \
138 "channel_name", ast_channel_name(third), \
139 "bridge_id", bridge->uniqueid); \
140 ast_test_validate(test, extra != NULL); \
141 APPEND_EVENT(first, AST_CEL_BRIDGE_TO_CONF, NULL, extra, ast_channel_name(second)); \
144 #define BLINDTRANSFER_EVENT(channel, bridge, extension, context) do { \
145 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
146 extra = ast_json_pack("{s: s, s: s, s: s}", \
147 "extension", extension, \
148 "context", context, \
149 "bridge_id", bridge->uniqueid); \
150 ast_test_validate(test, extra != NULL); \
151 APPEND_EVENT(channel, AST_CEL_BLINDTRANSFER, NULL, extra, NULL); \
154 #define ATTENDEDTRANSFER_BRIDGE(channel1, bridge1, channel2, bridge2) do { \
155 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
156 extra = ast_json_pack("{s: s, s: s, s: s}", \
157 "bridge1_id", bridge1->uniqueid, \
158 "channel2_name", ast_channel_name(channel2), \
159 "bridge2_id", bridge2->uniqueid); \
160 ast_test_validate(test, extra != NULL); \
161 APPEND_EVENT(channel1, AST_CEL_ATTENDEDTRANSFER, NULL, extra, NULL); \
164 /*! \brief Alice's Caller ID */
165 #define ALICE_CALLERID { .id.name.str = "Alice", .id.name.valid = 1, .id.number.str = "100", .id.number.valid = 1, }
167 /*! \brief Bob's Caller ID */
168 #define BOB_CALLERID { .id.name.str = "Bob", .id.name.valid = 1, .id.number.str = "200", .id.number.valid = 1, }
170 /*! \brief Charlie's Caller ID */
171 #define CHARLIE_CALLERID { .id.name.str = "Charlie", .id.name.valid = 1, .id.number.str = "300", .id.number.valid = 1, }
173 /*! \brief David's Caller ID */
174 #define DAVID_CALLERID { .id.name.str = "David", .id.name.valid = 1, .id.number.str = "400", .id.number.valid = 1, }
176 /*! \brief Create a \ref test_cel_chan_tech for Alice. */
177 #define CREATE_ALICE_CHANNEL(channel_var, caller_id) do { \
178 (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "100", "100", "default", NULL, 0, CHANNEL_TECH_NAME "/Alice"); \
179 APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL, NULL); \
182 /*! \brief Create a \ref test_cel_chan_tech for Bob. */
183 #define CREATE_BOB_CHANNEL(channel_var, caller_id) do { \
184 (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "200", "200", "default", NULL, 0, CHANNEL_TECH_NAME "/Bob"); \
185 APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL, NULL); \
188 /*! \brief Create a \ref test_cel_chan_tech for Charlie. */
189 #define CREATE_CHARLIE_CHANNEL(channel_var, caller_id) do { \
190 (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "300", "300", "default", NULL, 0, CHANNEL_TECH_NAME "/Charlie"); \
191 APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL, NULL); \
194 /*! \brief Create a \ref test_cel_chan_tech for David. */
195 #define CREATE_DAVID_CHANNEL(channel_var, caller_id) do { \
196 (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "400", "400", "default", NULL, 0, CHANNEL_TECH_NAME "/David"); \
197 APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL, NULL); \
200 /*! \brief Emulate a channel entering into an application */
201 #define EMULATE_APP_DATA(channel, priority, application, data) do { \
202 if ((priority) > 0) { \
203 ast_channel_priority_set((channel), (priority)); \
205 ast_channel_appl_set((channel), (application)); \
206 ast_channel_data_set((channel), (data)); \
207 ast_channel_publish_snapshot((channel)); \
210 #define ANSWER_CHANNEL(chan) do { \
211 EMULATE_APP_DATA(chan, 1, "Answer", ""); \
212 ANSWER_NO_APP(chan); \
215 #define ANSWER_NO_APP(chan) do { \
216 ast_setstate(chan, AST_STATE_UP); \
217 APPEND_EVENT(chan, AST_CEL_ANSWER, NULL, NULL, NULL); \
220 /*! \brief Hang up a test channel safely */
221 #define HANGUP_CHANNEL(channel, cause, dialstatus) do { \
222 ast_channel_hangupcause_set((channel), (cause)); \
223 ao2_ref(channel, +1); \
224 ast_hangup((channel)); \
225 HANGUP_EVENT(channel, cause, dialstatus); \
226 APPEND_EVENT(channel, AST_CEL_CHANNEL_END, NULL, NULL, NULL); \
227 ao2_cleanup(stasis_cache_get_extended(ast_channel_topic_all_cached(), \
228 ast_channel_snapshot_type(), ast_channel_uniqueid(channel), 1)); \
229 ao2_cleanup(channel); \
233 #define HANGUP_EVENT(channel, cause, dialstatus) do { \
234 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
235 extra = ast_json_pack("{s: i, s: s, s: s}", \
236 "hangupcause", cause, \
237 "hangupsource", "", \
238 "dialstatus", dialstatus); \
239 ast_test_validate(test, extra != NULL); \
240 APPEND_EVENT(channel, AST_CEL_HANGUP, NULL, extra, NULL); \
243 static void mid_test_sync(void);
245 static int append_expected_event(
246 struct ast_channel *chan,
247 enum ast_cel_event_type type,
248 const char *userdefevname,
249 struct ast_json *extra, const char *peer);
251 static int append_expected_event_snapshot(
252 struct ast_channel_snapshot *snapshot,
253 enum ast_cel_event_type type,
254 const char *userdefevname,
255 struct ast_json *extra, const char *peer);
257 static int append_dummy_event(void);
259 static void safe_channel_release(struct ast_channel *chan)
264 ast_channel_release(chan);
267 AST_TEST_DEFINE(test_cel_channel_creation)
269 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
270 struct ast_party_caller caller = ALICE_CALLERID;
274 info->name = __func__;
275 info->category = TEST_CATEGORY;
276 info->summary = "Test the CEL records created when a channel is created";
278 "Test the CEL records created when a channel is created";
279 return AST_TEST_NOT_RUN;
284 CREATE_ALICE_CHANNEL(chan, (&caller));
286 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
288 return AST_TEST_PASS;
291 AST_TEST_DEFINE(test_cel_unanswered_inbound_call)
293 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
294 struct ast_party_caller caller = ALICE_CALLERID;
298 info->name = __func__;
299 info->category = TEST_CATEGORY;
300 info->summary = "Test inbound unanswered calls";
302 "Test CEL records for a call that is\n"
303 "inbound to Asterisk, executes some dialplan, but\n"
304 "is never answered.\n";
305 return AST_TEST_NOT_RUN;
310 CREATE_ALICE_CHANNEL(chan, &caller);
312 EMULATE_APP_DATA(chan, 1, "Wait", "1");
314 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
316 return AST_TEST_PASS;
319 AST_TEST_DEFINE(test_cel_unanswered_outbound_call)
321 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
322 struct ast_party_caller caller = {
326 .id.number.valid = 1, };
330 info->name = __func__;
331 info->category = TEST_CATEGORY;
332 info->summary = "Test outbound unanswered calls";
334 "Test CEL records for a call that is\n"
335 "outbound to Asterisk but is never answered.\n";
336 return AST_TEST_NOT_RUN;
341 CREATE_ALICE_CHANNEL(chan, &caller);
343 ast_channel_exten_set(chan, "s");
344 ast_channel_context_set(chan, "default");
345 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
346 EMULATE_APP_DATA(chan, 0, "AppDial", "(Outgoing Line)");
347 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
349 return AST_TEST_PASS;
352 AST_TEST_DEFINE(test_cel_single_party)
354 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
355 struct ast_party_caller caller = ALICE_CALLERID;
359 info->name = __func__;
360 info->category = TEST_CATEGORY;
361 info->summary = "Test CEL for a single party";
363 "Test CEL records for a call that is\n"
364 "answered, but only involves a single channel\n";
365 return AST_TEST_NOT_RUN;
369 CREATE_ALICE_CHANNEL(chan, &caller);
371 ANSWER_CHANNEL(chan);
372 EMULATE_APP_DATA(chan, 2, "VoiceMailMain", "1");
374 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
376 return AST_TEST_PASS;
379 AST_TEST_DEFINE(test_cel_single_bridge)
381 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
382 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
384 struct ast_party_caller caller = ALICE_CALLERID;
388 info->name = __func__;
389 info->category = TEST_CATEGORY;
390 info->summary = "Test CEL for a single party entering/leaving a bridge";
392 "Test CEL records for a call that is\n"
393 "answered, enters a bridge, and leaves it.\n";
394 return AST_TEST_NOT_RUN;
398 bridge = ast_bridge_basic_new();
399 ast_test_validate(test, bridge != NULL);
401 CREATE_ALICE_CHANNEL(chan, &caller);
403 ANSWER_CHANNEL(chan);
404 EMULATE_APP_DATA(chan, 2, "Bridge", "");
407 ast_bridge_impart(bridge, chan, NULL, NULL, 0);
411 ast_bridge_depart(chan);
413 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
415 return AST_TEST_PASS;
418 AST_TEST_DEFINE(test_cel_single_bridge_continue)
420 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
421 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
422 struct ast_party_caller caller = ALICE_CALLERID;
426 info->name = __func__;
427 info->category = TEST_CATEGORY;
428 info->summary = "Test CEL for a single party entering/leaving a bridge";
430 "Test CEL records for a call that is\n"
431 "answered, enters a bridge, and leaves it.\n";
432 return AST_TEST_NOT_RUN;
436 bridge = ast_bridge_basic_new();
437 ast_test_validate(test, bridge != NULL);
439 CREATE_ALICE_CHANNEL(chan, &caller);
441 ANSWER_CHANNEL(chan);
442 EMULATE_APP_DATA(chan, 2, "Bridge", "");
445 ast_bridge_impart(bridge, chan, NULL, NULL, 0);
449 ast_bridge_depart(chan);
451 EMULATE_APP_DATA(chan, 3, "Wait", "");
453 /* And then it hangs up */
454 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
456 return AST_TEST_PASS;
459 AST_TEST_DEFINE(test_cel_single_twoparty_bridge_a)
461 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
462 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
463 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
464 struct ast_party_caller caller_alice = ALICE_CALLERID;
465 struct ast_party_caller caller_bob = BOB_CALLERID;
469 info->name = __func__;
470 info->category = TEST_CATEGORY;
471 info->summary = "Test CEL for a single party entering/leaving a bridge";
473 "Test CEL records for a call that is\n"
474 "answered, enters a bridge, and leaves it. In this scenario, the\n"
475 "Party A should answer the bridge first.\n";
476 return AST_TEST_NOT_RUN;
480 bridge = ast_bridge_basic_new();
481 ast_test_validate(test, bridge != NULL);
483 CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
485 CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
487 ANSWER_CHANNEL(chan_alice);
488 EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
490 ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
493 ANSWER_CHANNEL(chan_bob);
494 EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
496 ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
498 APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_bob));
500 ast_bridge_depart(chan_alice);
501 ast_bridge_depart(chan_bob);
502 APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_bob));
504 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
505 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
507 return AST_TEST_PASS;
510 AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
512 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
513 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
514 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
515 struct ast_party_caller caller_alice = ALICE_CALLERID;
516 struct ast_party_caller caller_bob = BOB_CALLERID;
520 info->name = __func__;
521 info->category = TEST_CATEGORY;
522 info->summary = "Test CEL for a single party entering/leaving a bridge";
524 "Test CEL records for a call that is\n"
525 "answered, enters a bridge, and leaves it. In this scenario, the\n"
526 "Party B should answer the bridge first.\n";
527 return AST_TEST_NOT_RUN;
531 bridge = ast_bridge_basic_new();
532 ast_test_validate(test, bridge != NULL);
534 CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
536 CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
538 ANSWER_CHANNEL(chan_alice);
539 EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
541 ANSWER_CHANNEL(chan_bob);
542 EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
545 ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
548 ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
550 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
552 ast_bridge_depart(chan_alice);
553 ast_bridge_depart(chan_bob);
554 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
556 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
557 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
559 return AST_TEST_PASS;
562 AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
564 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
565 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
566 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
567 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
568 struct ast_party_caller caller_alice = ALICE_CALLERID;
569 struct ast_party_caller caller_bob = BOB_CALLERID;
570 struct ast_party_caller caller_charlie = CHARLIE_CALLERID;
574 info->name = __func__;
575 info->category = TEST_CATEGORY;
576 info->summary = "Test CEL for a single party entering/leaving a multi-party bridge";
578 "Test CEL records for a call that is\n"
579 "answered, enters a bridge, and leaves it. A total of three\n"
580 "parties perform this action.\n";
581 return AST_TEST_NOT_RUN;
585 bridge = ast_bridge_basic_new();
586 ast_test_validate(test, bridge != NULL);
588 CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
589 CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
590 CREATE_CHARLIE_CHANNEL(chan_charlie, &caller_charlie);
592 ANSWER_CHANNEL(chan_alice);
593 EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
597 ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
599 ANSWER_CHANNEL(chan_bob);
600 EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
603 ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
605 APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_bob));
607 ANSWER_CHANNEL(chan_charlie);
608 EMULATE_APP_DATA(chan_charlie, 2, "Bridge", "");
610 ast_bridge_impart(bridge, chan_charlie, NULL, NULL, 0);
612 BRIDGE_TO_CONF(chan_alice, chan_bob, chan_charlie, bridge);
614 CONF_EXIT(chan_alice, bridge);
615 CONF_EXIT(chan_bob, bridge);
616 CONF_EXIT(chan_charlie, bridge);
618 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
619 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
620 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
622 return AST_TEST_PASS;
625 #define EMULATE_DIAL(channel, dialstring) do { \
626 EMULATE_APP_DATA(channel, 1, "Dial", dialstring); \
627 if (append_expected_event(channel, AST_CEL_APP_START, NULL, NULL, NULL)) { \
628 return AST_TEST_FAIL; \
632 #define START_DIALED(caller, callee) \
633 START_DIALED_FULL(caller, callee, "200", "Bob")
635 #define START_DIALED_FULL(caller, callee, number, name) do { \
636 callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, number, NULL, NULL, ast_channel_linkedid(caller), 0, CHANNEL_TECH_NAME "/" name); \
637 if (append_expected_event(callee, AST_CEL_CHANNEL_START, NULL, NULL, NULL)) { \
638 return AST_TEST_FAIL; \
640 ast_set_flag(ast_channel_flags(callee), AST_FLAG_OUTGOING); \
641 EMULATE_APP_DATA(callee, 0, "AppDial", "(Outgoing Line)"); \
642 ast_channel_publish_dial(caller, callee, name, NULL); \
645 AST_TEST_DEFINE(test_cel_dial_unanswered)
647 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
648 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
649 struct ast_party_caller caller = ALICE_CALLERID;
653 info->name = __func__;
654 info->category = TEST_CATEGORY;
655 info->summary = "Test CEL for a dial that isn't answered";
657 "Test CEL records for a channel that\n"
658 "performs a dial operation that isn't answered\n";
659 return AST_TEST_NOT_RUN;
664 CREATE_ALICE_CHANNEL(chan_caller, &caller);
666 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
668 START_DIALED(chan_caller, chan_callee);
670 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
671 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
673 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER, "NOANSWER");
674 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER, "");
676 return AST_TEST_PASS;
680 AST_TEST_DEFINE(test_cel_dial_busy)
682 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
683 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
684 struct ast_party_caller caller = ALICE_CALLERID;
688 info->name = __func__;
689 info->category = TEST_CATEGORY;
690 info->summary = "Test CEL for a dial that results in a busy";
692 "Test CEL records for a channel that\n"
693 "performs a dial operation to an endpoint that's busy\n";
694 return AST_TEST_NOT_RUN;
699 CREATE_ALICE_CHANNEL(chan_caller, &caller);
701 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
703 START_DIALED(chan_caller, chan_callee);
705 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
706 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "BUSY");
708 HANGUP_CHANNEL(chan_caller, AST_CAUSE_BUSY, "BUSY");
709 HANGUP_CHANNEL(chan_callee, AST_CAUSE_BUSY, "");
711 return AST_TEST_PASS;
714 AST_TEST_DEFINE(test_cel_dial_congestion)
716 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
717 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
718 struct ast_party_caller caller = ALICE_CALLERID;
722 info->name = __func__;
723 info->category = TEST_CATEGORY;
724 info->summary = "Test CEL for a dial that results in congestion";
726 "Test CEL records for a channel that\n"
727 "performs a dial operation to an endpoint that's congested\n";
728 return AST_TEST_NOT_RUN;
733 CREATE_ALICE_CHANNEL(chan_caller, &caller);
735 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
737 START_DIALED(chan_caller, chan_callee);
739 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
740 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CONGESTION");
742 HANGUP_CHANNEL(chan_caller, AST_CAUSE_CONGESTION, "CONGESTION");
743 HANGUP_CHANNEL(chan_callee, AST_CAUSE_CONGESTION, "");
745 return AST_TEST_PASS;
748 AST_TEST_DEFINE(test_cel_dial_unavailable)
750 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
751 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
752 struct ast_party_caller caller = ALICE_CALLERID;
756 info->name = __func__;
757 info->category = TEST_CATEGORY;
758 info->summary = "Test CEL for a dial that results in unavailable";
760 "Test CEL records for a channel that\n"
761 "performs a dial operation to an endpoint that's unavailable\n";
762 return AST_TEST_NOT_RUN;
767 CREATE_ALICE_CHANNEL(chan_caller, &caller);
769 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
771 START_DIALED(chan_caller, chan_callee);
773 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
774 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CHANUNAVAIL");
776 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ROUTE_DESTINATION, "CHANUNAVAIL");
777 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ROUTE_DESTINATION, "");
779 return AST_TEST_PASS;
782 AST_TEST_DEFINE(test_cel_dial_caller_cancel)
784 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
785 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
786 struct ast_party_caller caller = ALICE_CALLERID;
790 info->name = __func__;
791 info->category = TEST_CATEGORY;
792 info->summary = "Test CEL for a dial where the caller cancels";
794 "Test CEL records for a channel that\n"
795 "performs a dial operation to an endpoint but then decides\n"
796 "to hang up, cancelling the dial\n";
797 return AST_TEST_NOT_RUN;
802 CREATE_ALICE_CHANNEL(chan_caller, &caller);
804 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
806 START_DIALED(chan_caller, chan_callee);
808 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
809 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CANCEL");
811 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
812 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "CANCEL");
814 return AST_TEST_PASS;
817 AST_TEST_DEFINE(test_cel_dial_parallel_failed)
819 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
820 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
821 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
822 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
823 struct ast_party_caller caller = ALICE_CALLERID;
827 info->name = __func__;
828 info->category = TEST_CATEGORY;
829 info->summary = "Test a parallel dial where all channels fail to answer";
831 "This tests dialing three parties: Bob, Charlie, David. Charlie\n"
832 "returns BUSY; David returns CONGESTION; Bob fails to answer and\n"
833 "Alice hangs up. Three records are created for Alice as a result.\n";
834 return AST_TEST_NOT_RUN;
839 CREATE_ALICE_CHANNEL(chan_caller, &caller);
841 /* Channel enters Dial app */
842 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David");
844 /* Outbound channels are created */
845 START_DIALED_FULL(chan_caller, chan_bob, "200", "Bob");
846 START_DIALED_FULL(chan_caller, chan_charlie, "300", "Charlie");
847 START_DIALED_FULL(chan_caller, chan_david, "400", "David");
850 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
852 /* Charlie is busy */
853 ast_channel_publish_dial(chan_caller, chan_charlie, NULL, "BUSY");
854 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_BUSY, "");
856 /* David is congested */
857 ast_channel_publish_dial(chan_caller, chan_david, NULL, "CONGESTION");
858 HANGUP_CHANNEL(chan_david, AST_CAUSE_CONGESTION, "");
860 /* Bob is canceled */
861 ast_channel_publish_dial(chan_caller, chan_bob, NULL, "CANCEL");
862 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
865 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "BUSY");
867 return AST_TEST_PASS;
870 AST_TEST_DEFINE(test_cel_dial_answer_no_bridge)
872 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
873 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
874 struct ast_party_caller caller = ALICE_CALLERID;
878 info->name = __func__;
879 info->category = TEST_CATEGORY;
880 info->summary = "Test dialing, answering, and not going into a bridge.";
882 "This is a weird one, but theoretically possible. You can perform\n"
883 "a dial, then bounce both channels to different priorities and\n"
884 "never have them enter a bridge together. Ew. This makes sure that\n"
885 "when we answer, we get a CEL, it gets ended at that point, and\n"
886 "that it gets finalized appropriately.\n";
887 return AST_TEST_NOT_RUN;
892 CREATE_ALICE_CHANNEL(chan_caller, &caller);
894 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
896 START_DIALED(chan_caller, chan_callee);
898 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
899 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
901 ANSWER_NO_APP(chan_caller);
902 ast_clear_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
903 ANSWER_NO_APP(chan_callee);
905 EMULATE_APP_DATA(chan_caller, 2, "Wait", "1");
906 EMULATE_APP_DATA(chan_callee, 1, "Wait", "1");
908 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
909 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
911 return AST_TEST_PASS;
914 AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_a)
916 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
917 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
918 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
919 struct ast_party_caller caller = ALICE_CALLERID;
923 info->name = __func__;
924 info->category = TEST_CATEGORY;
925 info->summary = "Test dialing, answering, and going into a 2-party bridge";
927 "The most 'basic' of scenarios\n";
928 return AST_TEST_NOT_RUN;
932 bridge = ast_bridge_basic_new();
933 ast_test_validate(test, bridge != NULL);
935 CREATE_ALICE_CHANNEL(chan_caller, &caller);
937 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
939 START_DIALED(chan_caller, chan_callee);
941 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
942 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
944 ANSWER_NO_APP(chan_caller);
945 ANSWER_NO_APP(chan_callee);
949 ast_bridge_impart(bridge, chan_caller, NULL, NULL, 0);
951 ast_bridge_impart(bridge, chan_callee, NULL, NULL, 0);
953 APPEND_EVENT(chan_caller, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_callee));
955 ast_bridge_depart(chan_caller);
956 ast_bridge_depart(chan_callee);
957 APPEND_EVENT(chan_caller, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_callee));
959 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
960 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
962 return AST_TEST_PASS;
965 AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
967 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
968 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
969 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
970 struct ast_party_caller caller = ALICE_CALLERID;
974 info->name = __func__;
975 info->category = TEST_CATEGORY;
976 info->summary = "Test dialing, answering, and going into a 2-party bridge";
978 "The most 'basic' of scenarios\n";
979 return AST_TEST_NOT_RUN;
983 bridge = ast_bridge_basic_new();
984 ast_test_validate(test, bridge != NULL);
986 CREATE_ALICE_CHANNEL(chan_caller, &caller);
988 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
990 START_DIALED(chan_caller, chan_callee);
992 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
993 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
995 ANSWER_NO_APP(chan_caller);
996 ANSWER_NO_APP(chan_callee);
999 ast_bridge_impart(bridge, chan_callee, NULL, NULL, 0);
1001 ast_bridge_impart(bridge, chan_caller, NULL, NULL, 0);
1003 APPEND_EVENT(chan_callee, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_caller));
1005 ast_bridge_depart(chan_caller);
1006 ast_bridge_depart(chan_callee);
1007 APPEND_EVENT(chan_callee, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_caller));
1009 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1010 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1012 return AST_TEST_PASS;
1015 AST_TEST_DEFINE(test_cel_dial_answer_multiparty)
1017 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1018 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1019 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1020 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1021 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
1022 struct ast_party_caller alice_caller = ALICE_CALLERID;
1023 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1027 info->name = __func__;
1028 info->category = TEST_CATEGORY;
1029 info->summary = "Test dialing, answering, and going into a multi-party bridge";
1031 "A little tricky to get to do, but possible with some redirects.\n";
1032 return AST_TEST_NOT_RUN;
1036 bridge = ast_bridge_basic_new();
1037 ast_test_validate(test, bridge != NULL);
1039 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1041 EMULATE_DIAL(chan_alice, CHANNEL_TECH_NAME "/Bob");
1043 START_DIALED(chan_alice, chan_bob);
1046 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1048 EMULATE_DIAL(chan_charlie, CHANNEL_TECH_NAME "/Bob");
1051 START_DIALED_FULL(chan_charlie, chan_david, "400", "David");
1053 ast_channel_state_set(chan_alice, AST_STATE_RINGING);
1055 ast_channel_state_set(chan_charlie, AST_STATE_RINGING);
1057 ast_channel_publish_dial(chan_alice, chan_bob, NULL, "ANSWER");
1059 ast_channel_publish_dial(chan_charlie, chan_david, NULL, "ANSWER");
1062 ANSWER_NO_APP(chan_alice);
1064 ANSWER_NO_APP(chan_bob);
1066 ANSWER_NO_APP(chan_charlie);
1068 ANSWER_NO_APP(chan_david);
1072 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_charlie, NULL, NULL, 0));
1074 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_david, NULL, NULL, 0));
1076 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1078 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0));
1080 BRIDGE_TO_CONF(chan_charlie, chan_david, chan_bob, bridge);
1082 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0));
1084 CONF_ENTER_EVENT(chan_alice, bridge);
1086 CONF_EXIT(chan_alice, bridge);
1087 CONF_EXIT(chan_bob, bridge);
1088 CONF_EXIT(chan_charlie, bridge);
1089 CONF_EXIT(chan_david, bridge);
1091 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "ANSWER");
1092 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1093 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "ANSWER");
1094 HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1096 return AST_TEST_PASS;
1099 AST_TEST_DEFINE(test_cel_blind_transfer)
1101 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1102 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1103 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
1104 struct ast_party_caller alice_caller = ALICE_CALLERID;
1105 struct ast_party_caller bob_caller = BOB_CALLERID;
1106 struct ast_bridge_channel_pair pair;
1110 info->name = __func__;
1111 info->category = TEST_CATEGORY;
1112 info->summary = "Test blind transfers to an extension";
1114 "This test creates two channels, bridges them, and then"
1115 " blind transfers the bridge to an extension.\n";
1116 return AST_TEST_NOT_RUN;
1120 bridge = ast_bridge_basic_new();
1121 ast_test_validate(test, bridge != NULL);
1123 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1124 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1126 ANSWER_NO_APP(chan_alice);
1127 ANSWER_NO_APP(chan_bob);
1129 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0));
1132 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0));
1134 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1136 pair.bridge = bridge;
1137 pair.channel = chan_alice;
1138 ast_bridge_publish_blind_transfer(1, AST_BRIDGE_TRANSFER_SUCCESS,
1139 &pair, "transfer_context", "transfer_extension");
1140 BLINDTRANSFER_EVENT(chan_alice, bridge, "transfer_extension", "transfer_context");
1142 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
1143 ast_test_validate(test, 0 == ast_bridge_depart(chan_alice));
1145 ast_test_validate(test, 0 == ast_bridge_depart(chan_bob));
1147 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1149 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1151 return AST_TEST_PASS;
1154 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
1156 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1157 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1158 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1159 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1160 RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1161 RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1162 struct ast_party_caller alice_caller = ALICE_CALLERID;
1163 struct ast_party_caller bob_caller = BOB_CALLERID;
1164 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1165 struct ast_party_caller david_caller = ALICE_CALLERID;
1169 info->name = __func__;
1170 info->category = TEST_CATEGORY;
1171 info->summary = "Test attended transfers between two pairs of bridged parties";
1173 "This test creates four channels, places each pair in"
1174 " a bridge, and then attended transfers the bridges"
1176 return AST_TEST_NOT_RUN;
1180 /* Create first set of bridged parties */
1181 bridge1 = ast_bridge_basic_new();
1182 ast_test_validate(test, bridge1 != NULL);
1184 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1185 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1186 ANSWER_NO_APP(chan_alice);
1187 ANSWER_NO_APP(chan_bob);
1189 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1192 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1194 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1196 /* Create second set of bridged parties */
1197 bridge2 = ast_bridge_basic_new();
1198 ast_test_validate(test, bridge2 != NULL);
1200 CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1201 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1202 ANSWER_NO_APP(chan_david);
1203 ANSWER_NO_APP(chan_charlie);
1205 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1208 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1210 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1212 /* Perform attended transfer */
1213 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
1215 ast_bridge_transfer_attended(chan_alice, chan_david);
1217 BRIDGE_TO_CONF(chan_charlie, chan_david, chan_bob, bridge2);
1218 CONF_EXIT_EVENT(chan_david, bridge2);
1220 ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1223 CONF_EXIT(chan_bob, bridge2);
1224 CONF_EXIT(chan_charlie, bridge2);
1226 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1228 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1230 HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1232 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1234 return AST_TEST_PASS;
1237 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
1239 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1240 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1241 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1242 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1243 RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1244 RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1245 struct ast_party_caller alice_caller = ALICE_CALLERID;
1246 struct ast_party_caller bob_caller = BOB_CALLERID;
1247 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1248 struct ast_party_caller david_caller = ALICE_CALLERID;
1252 info->name = __func__;
1253 info->category = TEST_CATEGORY;
1254 info->summary = "Test attended transfers between two pairs of"
1255 " bridged parties that results in a bridge merge";
1257 "This test creates four channels, places each pair"
1258 " in a bridge, and then attended transfers the bridges"
1259 " together causing a bridge merge.\n";
1260 return AST_TEST_NOT_RUN;
1264 /* Create first set of bridged parties */
1265 bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1266 AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1267 ast_test_validate(test, bridge1 != NULL);
1269 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1270 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1271 ANSWER_NO_APP(chan_alice);
1272 ANSWER_NO_APP(chan_bob);
1274 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1277 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1279 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1281 /* Create second set of bridged parties */
1282 bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1283 AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1284 ast_test_validate(test, bridge2 != NULL);
1286 CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1287 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1288 ANSWER_NO_APP(chan_david);
1289 ANSWER_NO_APP(chan_charlie);
1291 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1294 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1296 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1298 /* Perform attended transfer */
1299 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_david));
1301 ast_bridge_transfer_attended(chan_alice, chan_david);
1303 BRIDGE_TO_CONF(chan_bob, chan_alice, chan_charlie, bridge1);
1304 CONF_EXIT_EVENT(chan_alice, bridge1);
1306 ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1309 CONF_EXIT(chan_bob, bridge1);
1310 CONF_EXIT(chan_charlie, bridge1);
1312 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1314 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1316 HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1318 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1320 return AST_TEST_PASS;
1323 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
1325 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1326 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1327 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1328 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1329 RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1330 RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1331 struct ast_party_caller alice_caller = ALICE_CALLERID;
1332 struct ast_party_caller bob_caller = BOB_CALLERID;
1333 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1334 struct ast_party_caller david_caller = ALICE_CALLERID;
1338 info->name = __func__;
1339 info->category = TEST_CATEGORY;
1340 info->summary = "Test attended transfers between two pairs of"
1341 " bridged parties that results in a bridge merge";
1343 "This test creates four channels, places each pair"
1344 " in a bridge, and then attended transfers the bridges"
1345 " together causing a bridge link.\n";
1346 return AST_TEST_NOT_RUN;
1350 /* Create first set of bridged parties */
1351 bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1352 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1353 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1354 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1355 ast_test_validate(test, bridge1 != NULL);
1357 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1358 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1359 ANSWER_NO_APP(chan_alice);
1360 ANSWER_NO_APP(chan_bob);
1362 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1365 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1367 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1369 /* Create second set of bridged parties */
1370 bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1371 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1372 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1373 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1374 ast_test_validate(test, bridge2 != NULL);
1376 CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1377 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1378 ANSWER_NO_APP(chan_david);
1379 ANSWER_NO_APP(chan_charlie);
1381 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1384 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1386 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1388 /* Perform attended transfer */
1390 /* The following events can not be matched directly since nothing is known
1391 * about the linking local channel.
1392 * local channel ;1 and ;2 creation and ;2 answer */
1393 APPEND_DUMMY_EVENT();
1394 APPEND_DUMMY_EVENT();
1395 APPEND_DUMMY_EVENT();
1397 ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1399 /* The two BRIDGE_TO_CONFs and CONF_EXITs are all racing to be first */
1401 /* BRIDGE_TO_CONF with primary charlie, peer david, and trigger channel ;2 */
1402 APPEND_DUMMY_EVENT();
1404 ast_bridge_transfer_attended(chan_alice, chan_david);
1407 /* BRIDGE_TO_CONF with primary bob, peer alice, and trigger channel ;1 */
1408 APPEND_DUMMY_EVENT();
1410 /* CONF_EXIT alice and david */
1411 APPEND_DUMMY_EVENT();
1412 APPEND_DUMMY_EVENT();
1415 APPEND_DUMMY_EVENT();
1418 CONF_EXIT(chan_bob, bridge1);
1419 CONF_EXIT(chan_charlie, bridge2);
1421 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1423 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1425 HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1427 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1429 return AST_TEST_PASS;
1432 AST_TEST_DEFINE(test_cel_dial_pickup)
1434 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1435 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1436 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1437 struct ast_party_caller caller = ALICE_CALLERID;
1438 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1442 info->name = __func__;
1443 info->category = TEST_CATEGORY;
1444 info->summary = "Test call pickup";
1446 "Test CEL records for a call that is\n"
1447 "inbound to Asterisk, executes some dialplan, and\n"
1449 return AST_TEST_NOT_RUN;
1454 CREATE_ALICE_CHANNEL(chan_caller, &caller);
1456 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
1458 START_DIALED(chan_caller, chan_callee);
1460 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1462 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1465 SCOPED_CHANNELLOCK(lock, chan_callee);
1466 APPEND_EVENT(chan_callee, AST_CEL_PICKUP, NULL, NULL, ast_channel_name(chan_charlie));
1467 ast_test_validate(test, 0 == ast_do_pickup(chan_charlie, chan_callee));
1470 /* Hang up the masqueraded zombie */
1471 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1473 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1475 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1476 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1478 return AST_TEST_PASS;
1481 AST_TEST_DEFINE(test_cel_local_optimize)
1483 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1484 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1485 struct ast_party_caller alice_caller = ALICE_CALLERID;
1486 struct ast_party_caller bob_caller = BOB_CALLERID;
1487 RAII_VAR(struct ast_multi_channel_blob *, mc_blob, NULL, ao2_cleanup);
1488 RAII_VAR(struct ast_channel_snapshot *, alice_snapshot, NULL, ao2_cleanup);
1489 RAII_VAR(struct ast_channel_snapshot *, bob_snapshot, NULL, ao2_cleanup);
1490 RAII_VAR(struct stasis_message *, local_opt_begin, NULL, ao2_cleanup);
1491 RAII_VAR(struct stasis_message *, local_opt_end, NULL, ao2_cleanup);
1495 info->name = __func__;
1496 info->category = TEST_CATEGORY;
1497 info->summary = "Test local channel optimization record generation";
1499 "Test CEL records for two local channels being optimized\n"
1500 "out by sending a messages indicating local optimization\n"
1502 return AST_TEST_NOT_RUN;
1507 mc_blob = ast_multi_channel_blob_create(ast_json_null());
1508 ast_test_validate(test, mc_blob != NULL);
1510 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1511 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1513 alice_snapshot = ast_channel_snapshot_create(chan_alice);
1514 ast_test_validate(test, alice_snapshot != NULL);
1516 bob_snapshot = ast_channel_snapshot_create(chan_bob);
1517 ast_test_validate(test, bob_snapshot != NULL);
1519 ast_multi_channel_blob_add_channel(mc_blob, "1", alice_snapshot);
1520 ast_multi_channel_blob_add_channel(mc_blob, "2", bob_snapshot);
1522 local_opt_begin = stasis_message_create(ast_local_optimization_begin_type(), mc_blob);
1523 ast_test_validate(test, local_opt_begin != NULL);
1525 local_opt_end = stasis_message_create(ast_local_optimization_end_type(), mc_blob);
1526 ast_test_validate(test, local_opt_end != NULL);
1528 stasis_publish(ast_channel_topic(chan_alice), local_opt_begin);
1529 stasis_publish(ast_channel_topic(chan_alice), local_opt_end);
1530 APPEND_EVENT_SNAPSHOT(alice_snapshot, AST_CEL_LOCAL_OPTIMIZE, NULL, NULL, bob_snapshot->name);
1532 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1533 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1535 return AST_TEST_PASS;
1538 /*! Subscription for CEL events */
1539 static struct ast_event_sub *event_sub = NULL;
1541 /*! Container for astobj2 duplicated ast_events */
1542 static struct ao2_container *cel_received_events = NULL;
1544 /*! Container for expected CEL events */
1545 static struct ao2_container *cel_expected_events = NULL;
1547 static struct ast_event *ao2_dup_event(const struct ast_event *event)
1549 struct ast_event *event_dup;
1552 event_len = ast_event_get_size(event);
1554 event_dup = ao2_alloc(event_len, NULL);
1559 memcpy(event_dup, event, event_len);
1564 static void mid_test_sync(void)
1566 ast_mutex_lock(&mid_test_sync_lock);
1567 if (ao2_container_count(cel_expected_events) <= ao2_container_count(cel_received_events)) {
1568 ast_mutex_unlock(&mid_test_sync_lock);
1572 do_mid_test_sync = 1;
1573 ast_mutex_unlock(&mid_test_sync_lock);
1576 struct timeval start = ast_tvnow();
1577 struct timespec end = {
1578 .tv_sec = start.tv_sec + 15,
1579 .tv_nsec = start.tv_usec * 1000
1582 SCOPED_MUTEX(lock, &sync_lock);
1583 ast_cond_timedwait(&sync_out, &sync_lock, &end);
1587 static int append_event(struct ast_event *ev)
1589 RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1590 ao2_ev = ao2_dup_event(ev);
1595 ao2_link(cel_expected_events, ao2_ev);
1599 static int append_dummy_event(void)
1601 RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1602 RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1604 ev = ast_event_new(AST_EVENT_CUSTOM, AST_EVENT_IE_END);
1609 return append_event(ev);
1612 static int append_expected_event_snapshot(
1613 struct ast_channel_snapshot *snapshot,
1614 enum ast_cel_event_type type,
1615 const char *userdefevname,
1616 struct ast_json *extra, const char *peer)
1618 RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1619 ev = ast_cel_create_event(snapshot, type, userdefevname, extra, peer);
1624 return append_event(ev);
1627 static int append_expected_event(
1628 struct ast_channel *chan,
1629 enum ast_cel_event_type type,
1630 const char *userdefevname,
1631 struct ast_json *extra, const char *peer)
1633 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1634 snapshot = ast_channel_snapshot_create(chan);
1639 return append_expected_event_snapshot(snapshot, type, userdefevname, extra, peer);
1642 static void test_sub(const struct ast_event *event, void *data)
1644 struct ast_event *event_dup = ao2_dup_event(event);
1645 const char *sync_tag;
1646 SCOPED_MUTEX(mid_test_lock, &mid_test_sync_lock);
1652 sync_tag = ast_event_get_ie_str(event, AST_EVENT_IE_SERVICE);
1654 if (!strcmp(sync_tag, "SYNC")) {
1655 /* trigger things */
1656 SCOPED_MUTEX(lock, &sync_lock);
1657 ast_cond_signal(&sync_out);
1662 /* save the event for later processing */
1663 ao2_link(cel_received_events, event_dup);
1665 if (do_mid_test_sync) {
1666 int expected = ao2_container_count(cel_expected_events);
1667 int received = ao2_container_count(cel_received_events);
1668 if (expected <= received) {
1670 SCOPED_MUTEX(lock, &sync_lock);
1671 ast_cond_signal(&sync_out);
1672 do_mid_test_sync = 0;
1679 * \internal \brief Callback function called before each test executes
1681 static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
1683 ast_assert(event_sub == NULL);
1684 ast_assert(cel_received_events == NULL);
1685 ast_assert(cel_expected_events == NULL);
1687 ast_mutex_init(&mid_test_sync_lock);
1688 ast_mutex_init(&sync_lock);
1689 ast_cond_init(&sync_out, NULL);
1691 /* Back up the real CEL config and insert the test's config */
1692 saved_config = ast_cel_get_config();
1693 ast_cel_set_config(cel_test_config);
1695 /* init CEL event storage (degenerate hash table becomes a linked list) */
1696 cel_received_events = ao2_container_alloc(1, NULL, NULL);
1697 cel_expected_events = ao2_container_alloc(1, NULL, NULL);
1699 /* start the CEL event callback */
1700 event_sub = ast_event_subscribe(AST_EVENT_CEL, test_sub, "CEL Test Logging",
1701 NULL, AST_EVENT_IE_END);
1705 /*! \brief Check an IE value from two events, */
1706 static int match_ie_val(
1707 const struct ast_event *event1,
1708 const struct ast_event *event2,
1709 enum ast_event_ie_type type)
1711 enum ast_event_ie_pltype pltype = ast_event_get_ie_pltype(type);
1714 case AST_EVENT_IE_PLTYPE_UINT:
1716 uint32_t val = ast_event_get_ie_uint(event2, type);
1718 return (val == ast_event_get_ie_uint(event1, type)) ? 1 : 0;
1720 case AST_EVENT_IE_PLTYPE_STR:
1725 hash = ast_event_get_ie_str_hash(event2, type);
1726 if (hash != ast_event_get_ie_str_hash(event1, type)) {
1730 str = ast_event_get_ie_str(event2, type);
1732 const char *e1str, *e2str;
1733 e1str = ast_event_get_ie_str(event1, type);
1736 if (type == AST_EVENT_IE_DEVICE) {
1737 e1str = ast_tech_to_upper(ast_strdupa(e1str));
1738 e2str = ast_tech_to_upper(ast_strdupa(e2str));
1741 if (!strcmp(e1str, e2str)) {
1754 static int events_are_equal(struct ast_test *test, struct ast_event *received, struct ast_event *expected)
1756 struct ast_event_iterator iterator;
1759 if (ast_event_get_type(expected) == AST_EVENT_CUSTOM) {
1760 /* this event is flagged as a wildcard match */
1764 for (res = ast_event_iterator_init(&iterator, received); !res; res = ast_event_iterator_next(&iterator)) {
1765 /* XXX ignore sec/usec for now */
1767 int ie_type = ast_event_iterator_get_ie_type(&iterator);
1768 if (ie_type != AST_EVENT_IE_CEL_EVENT_TIME_USEC
1769 && ie_type != AST_EVENT_IE_EID
1770 && ie_type != AST_EVENT_IE_CEL_EVENT_TIME
1771 && !match_ie_val(received, expected, ie_type)) {
1772 ast_test_status_update(test, "Failed matching on field %s\n", ast_event_get_ie_type_name(ie_type));
1780 static int dump_event(struct ast_test *test, struct ast_event *event)
1782 struct ast_event_iterator i;
1784 if (ast_event_iterator_init(&i, event)) {
1785 ast_test_status_update(test, "Failed to initialize event iterator. :-(\n");
1789 ast_test_status_update(test, "Event: %s %s\n", ast_event_get_type_name(event),
1790 ast_cel_get_type_name(ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TYPE)));
1793 enum ast_event_ie_type ie_type;
1794 enum ast_event_ie_pltype ie_pltype;
1795 const char *ie_type_name;
1797 ie_type = ast_event_iterator_get_ie_type(&i);
1798 ie_type_name = ast_event_get_ie_type_name(ie_type);
1799 ie_pltype = ast_event_get_ie_pltype(ie_type);
1801 switch (ie_pltype) {
1802 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1803 case AST_EVENT_IE_PLTYPE_EXISTS:
1804 ast_test_status_update(test, "%s\n", ie_type_name);
1806 case AST_EVENT_IE_PLTYPE_STR:
1807 ast_test_status_update(test, "%.30s: %s\n", ie_type_name,
1808 ast_event_iterator_get_ie_str(&i));
1810 case AST_EVENT_IE_PLTYPE_UINT:
1811 ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
1812 ast_event_iterator_get_ie_uint(&i));
1814 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1815 ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
1816 ast_event_iterator_get_ie_bitflags(&i));
1820 } while (!ast_event_iterator_next(&i));
1822 ast_test_status_update(test, "\n");
1827 static int check_events(struct ast_test *test, struct ao2_container *local_expected, struct ao2_container *local_received)
1829 struct ao2_iterator expected_it, received_it;
1830 struct ast_event *rx_event, *ex_event;
1833 if (ao2_container_count(local_expected) != ao2_container_count(local_received)) {
1834 ast_test_status_update(test, "Increasing verbosity since the number of expected events (%d)"
1835 " did not match number of received events (%d).\n",
1836 ao2_container_count(local_expected),
1837 ao2_container_count(local_received));
1841 expected_it = ao2_iterator_init(local_expected, 0);
1842 received_it = ao2_iterator_init(local_received, 0);
1843 rx_event = ao2_iterator_next(&received_it);
1844 ex_event = ao2_iterator_next(&expected_it);
1845 while (rx_event && ex_event) {
1846 if (!events_are_equal(test, rx_event, ex_event)) {
1847 ast_test_status_update(test, "Received event:\n");
1848 dump_event(test, rx_event);
1849 ast_test_status_update(test, "Expected event:\n");
1850 dump_event(test, ex_event);
1854 ast_test_status_update(test, "Compared events successfully%s\n", ast_event_get_type(ex_event) == AST_EVENT_CUSTOM ? " (wildcard match)" : "");
1855 dump_event(test, rx_event);
1857 ao2_cleanup(rx_event);
1858 ao2_cleanup(ex_event);
1859 rx_event = ao2_iterator_next(&received_it);
1860 ex_event = ao2_iterator_next(&expected_it);
1864 ast_test_status_update(test, "Received event:\n");
1865 dump_event(test, rx_event);
1866 ao2_cleanup(rx_event);
1870 ast_test_status_update(test, "Expected event:\n");
1871 dump_event(test, ex_event);
1872 ao2_cleanup(ex_event);
1878 static struct ast_event *create_sync_event(void)
1880 struct ast_event *event_dup;
1881 RAII_VAR(struct ast_event *, event, ao2_callback(cel_expected_events, 0, NULL, NULL), ao2_cleanup);
1888 event_len = ast_event_get_size(event);
1890 event_dup = ast_calloc(1, event_len);
1895 memcpy(event_dup, event, event_len);
1896 ast_event_append_ie_str(&event_dup, AST_EVENT_IE_SERVICE, "SYNC");
1902 * \internal \brief Callback function called after each test executes.
1903 * In addition to cleanup, this function also performs verification
1904 * that the events received during a test match the events that were
1905 * expected to have been generated during the test.
1907 static int cel_verify_and_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
1909 struct ast_event *sync;
1910 RAII_VAR(struct ao2_container *, local_expected, cel_expected_events, ao2_cleanup);
1911 RAII_VAR(struct ao2_container *, local_received, cel_received_events, ao2_cleanup);
1912 ast_assert(event_sub != NULL);
1913 ast_assert(cel_received_events != NULL);
1914 ast_assert(cel_expected_events != NULL);
1918 /* sync with the event system */
1919 sync = create_sync_event();
1920 ast_test_validate(test, sync != NULL);
1921 if (ast_event_queue(sync)) {
1922 ast_event_destroy(sync);
1923 ast_test_validate(test, NULL);
1925 struct timeval start = ast_tvnow();
1926 struct timespec end = {
1927 .tv_sec = start.tv_sec + 15,
1928 .tv_nsec = start.tv_usec * 1000
1931 SCOPED_MUTEX(lock, &sync_lock);
1932 ast_cond_timedwait(&sync_out, &sync_lock, &end);
1935 /* stop the CEL event callback and clean up storage structures*/
1936 ast_event_unsubscribe(event_sub);
1939 /* cleaned up by RAII_VAR's */
1940 cel_expected_events = NULL;
1941 cel_received_events = NULL;
1944 ast_test_validate(test, !check_events(test, local_expected, local_received));
1946 /* Restore the real CEL config */
1947 ast_cel_set_config(saved_config);
1948 ao2_cleanup(saved_config);
1949 saved_config = NULL;
1951 /* clean up the locks */
1952 ast_mutex_destroy(&sync_lock);
1953 ast_mutex_destroy(&mid_test_sync_lock);
1954 ast_cond_destroy(&sync_out);
1958 static int unload_module(void)
1960 AST_TEST_UNREGISTER(test_cel_channel_creation);
1961 AST_TEST_UNREGISTER(test_cel_unanswered_inbound_call);
1962 AST_TEST_UNREGISTER(test_cel_unanswered_outbound_call);
1963 AST_TEST_UNREGISTER(test_cel_single_party);
1964 AST_TEST_UNREGISTER(test_cel_single_bridge);
1965 AST_TEST_UNREGISTER(test_cel_single_bridge_continue);
1966 AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_a);
1967 AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_b);
1968 AST_TEST_UNREGISTER(test_cel_single_multiparty_bridge);
1970 AST_TEST_UNREGISTER(test_cel_dial_unanswered);
1971 AST_TEST_UNREGISTER(test_cel_dial_congestion);
1972 AST_TEST_UNREGISTER(test_cel_dial_busy);
1973 AST_TEST_UNREGISTER(test_cel_dial_unavailable);
1974 AST_TEST_UNREGISTER(test_cel_dial_caller_cancel);
1975 AST_TEST_UNREGISTER(test_cel_dial_parallel_failed);
1976 AST_TEST_UNREGISTER(test_cel_dial_answer_no_bridge);
1977 AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_a);
1978 AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_b);
1979 AST_TEST_UNREGISTER(test_cel_dial_answer_multiparty);
1981 AST_TEST_UNREGISTER(test_cel_blind_transfer);
1982 AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_swap);
1983 AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_merge);
1984 AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_link);
1986 AST_TEST_UNREGISTER(test_cel_dial_pickup);
1988 AST_TEST_UNREGISTER(test_cel_local_optimize);
1990 ast_channel_unregister(&test_cel_chan_tech);
1992 ao2_cleanup(cel_test_config);
1993 cel_test_config = NULL;
1998 static int load_module(void)
2000 /* build the test config */
2001 cel_test_config = ast_cel_general_config_alloc();
2002 if (!cel_test_config) {
2005 cel_test_config->enable = 1;
2006 if (ast_str_container_add(cel_test_config->apps, "dial")) {
2009 if (ast_str_container_add(cel_test_config->apps, "park")) {
2012 if (ast_str_container_add(cel_test_config->apps, "queue")) {
2015 cel_test_config->events |= 1<<AST_CEL_APP_START;
2016 cel_test_config->events |= 1<<AST_CEL_CHANNEL_START;
2017 cel_test_config->events |= 1<<AST_CEL_CHANNEL_END;
2018 cel_test_config->events |= 1<<AST_CEL_ANSWER;
2019 cel_test_config->events |= 1<<AST_CEL_HANGUP;
2020 cel_test_config->events |= 1<<AST_CEL_BRIDGE_START;
2021 cel_test_config->events |= 1<<AST_CEL_BRIDGE_END;
2022 cel_test_config->events |= 1<<AST_CEL_BRIDGE_TO_CONF;
2023 cel_test_config->events |= 1<<AST_CEL_CONF_ENTER;
2024 cel_test_config->events |= 1<<AST_CEL_CONF_EXIT;
2025 cel_test_config->events |= 1<<AST_CEL_BLINDTRANSFER;
2026 cel_test_config->events |= 1<<AST_CEL_ATTENDEDTRANSFER;
2027 cel_test_config->events |= 1<<AST_CEL_PICKUP;
2028 cel_test_config->events |= 1<<AST_CEL_LOCAL_OPTIMIZE;
2030 ast_channel_register(&test_cel_chan_tech);
2032 AST_TEST_REGISTER(test_cel_channel_creation);
2033 AST_TEST_REGISTER(test_cel_unanswered_inbound_call);
2034 AST_TEST_REGISTER(test_cel_unanswered_outbound_call);
2036 AST_TEST_REGISTER(test_cel_single_party);
2037 AST_TEST_REGISTER(test_cel_single_bridge);
2038 AST_TEST_REGISTER(test_cel_single_bridge_continue);
2039 AST_TEST_REGISTER(test_cel_single_twoparty_bridge_a);
2040 AST_TEST_REGISTER(test_cel_single_twoparty_bridge_b);
2041 AST_TEST_REGISTER(test_cel_single_multiparty_bridge);
2043 AST_TEST_REGISTER(test_cel_dial_unanswered);
2044 AST_TEST_REGISTER(test_cel_dial_congestion);
2045 AST_TEST_REGISTER(test_cel_dial_busy);
2046 AST_TEST_REGISTER(test_cel_dial_unavailable);
2047 AST_TEST_REGISTER(test_cel_dial_caller_cancel);
2048 AST_TEST_REGISTER(test_cel_dial_parallel_failed);
2049 AST_TEST_REGISTER(test_cel_dial_answer_no_bridge);
2050 AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_a);
2051 AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_b);
2052 AST_TEST_REGISTER(test_cel_dial_answer_multiparty);
2054 AST_TEST_REGISTER(test_cel_blind_transfer);
2055 AST_TEST_REGISTER(test_cel_attended_transfer_bridges_swap);
2056 AST_TEST_REGISTER(test_cel_attended_transfer_bridges_merge);
2057 AST_TEST_REGISTER(test_cel_attended_transfer_bridges_link);
2059 AST_TEST_REGISTER(test_cel_dial_pickup);
2061 AST_TEST_REGISTER(test_cel_local_optimize);
2063 /* ast_test_register_* has to happen after AST_TEST_REGISTER */
2064 /* Verify received vs expected events and clean things up after every test */
2065 ast_test_register_init(TEST_CATEGORY, test_cel_init_cb);
2066 ast_test_register_cleanup(TEST_CATEGORY, cel_verify_and_cleanup_cb);
2068 return AST_MODULE_LOAD_SUCCESS;
2071 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CEL unit tests");