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_features.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 stasis_topic_wait(ast_channel_topic_all_cached()); \
228 ao2_cleanup(stasis_cache_get(ast_channel_cache(), \
229 ast_channel_snapshot_type(), ast_channel_uniqueid(channel))); \
230 ao2_cleanup(channel); \
234 #define HANGUP_EVENT(channel, cause, dialstatus) do { \
235 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
236 extra = ast_json_pack("{s: i, s: s, s: s}", \
237 "hangupcause", cause, \
238 "hangupsource", "", \
239 "dialstatus", dialstatus); \
240 ast_test_validate(test, extra != NULL); \
241 APPEND_EVENT(channel, AST_CEL_HANGUP, NULL, extra, NULL); \
244 static void mid_test_sync(void);
246 static int append_expected_event(
247 struct ast_channel *chan,
248 enum ast_cel_event_type type,
249 const char *userdefevname,
250 struct ast_json *extra, const char *peer);
252 static int append_expected_event_snapshot(
253 struct ast_channel_snapshot *snapshot,
254 enum ast_cel_event_type type,
255 const char *userdefevname,
256 struct ast_json *extra, const char *peer);
258 static int append_dummy_event(void);
260 static void safe_channel_release(struct ast_channel *chan)
265 ast_channel_release(chan);
268 AST_TEST_DEFINE(test_cel_channel_creation)
270 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
271 struct ast_party_caller caller = ALICE_CALLERID;
275 info->name = __func__;
276 info->category = TEST_CATEGORY;
277 info->summary = "Test the CEL records created when a channel is created";
279 "Test the CEL records created when a channel is created";
280 return AST_TEST_NOT_RUN;
285 CREATE_ALICE_CHANNEL(chan, (&caller));
287 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
289 return AST_TEST_PASS;
292 AST_TEST_DEFINE(test_cel_unanswered_inbound_call)
294 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
295 struct ast_party_caller caller = ALICE_CALLERID;
299 info->name = __func__;
300 info->category = TEST_CATEGORY;
301 info->summary = "Test inbound unanswered calls";
303 "Test CEL records for a call that is\n"
304 "inbound to Asterisk, executes some dialplan, but\n"
305 "is never answered.\n";
306 return AST_TEST_NOT_RUN;
311 CREATE_ALICE_CHANNEL(chan, &caller);
313 EMULATE_APP_DATA(chan, 1, "Wait", "1");
315 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
317 return AST_TEST_PASS;
320 AST_TEST_DEFINE(test_cel_unanswered_outbound_call)
322 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
323 struct ast_party_caller caller = {
327 .id.number.valid = 1, };
331 info->name = __func__;
332 info->category = TEST_CATEGORY;
333 info->summary = "Test outbound unanswered calls";
335 "Test CEL records for a call that is\n"
336 "outbound to Asterisk but is never answered.\n";
337 return AST_TEST_NOT_RUN;
342 CREATE_ALICE_CHANNEL(chan, &caller);
344 ast_channel_exten_set(chan, "s");
345 ast_channel_context_set(chan, "default");
346 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
347 EMULATE_APP_DATA(chan, 0, "AppDial", "(Outgoing Line)");
348 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
350 return AST_TEST_PASS;
353 AST_TEST_DEFINE(test_cel_single_party)
355 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
356 struct ast_party_caller caller = ALICE_CALLERID;
360 info->name = __func__;
361 info->category = TEST_CATEGORY;
362 info->summary = "Test CEL for a single party";
364 "Test CEL records for a call that is\n"
365 "answered, but only involves a single channel\n";
366 return AST_TEST_NOT_RUN;
370 CREATE_ALICE_CHANNEL(chan, &caller);
372 ANSWER_CHANNEL(chan);
373 EMULATE_APP_DATA(chan, 2, "VoiceMailMain", "1");
375 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
377 return AST_TEST_PASS;
380 AST_TEST_DEFINE(test_cel_single_bridge)
382 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
383 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
385 struct ast_party_caller caller = ALICE_CALLERID;
389 info->name = __func__;
390 info->category = TEST_CATEGORY;
391 info->summary = "Test CEL for a single party entering/leaving a bridge";
393 "Test CEL records for a call that is\n"
394 "answered, enters a bridge, and leaves it.\n";
395 return AST_TEST_NOT_RUN;
399 bridge = ast_bridge_basic_new();
400 ast_test_validate(test, bridge != NULL);
402 CREATE_ALICE_CHANNEL(chan, &caller);
404 ANSWER_CHANNEL(chan);
405 EMULATE_APP_DATA(chan, 2, "Bridge", "");
408 ast_bridge_impart(bridge, chan, NULL, NULL, 0);
412 ast_bridge_depart(chan);
414 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
416 return AST_TEST_PASS;
419 AST_TEST_DEFINE(test_cel_single_bridge_continue)
421 RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
422 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
423 struct ast_party_caller caller = ALICE_CALLERID;
427 info->name = __func__;
428 info->category = TEST_CATEGORY;
429 info->summary = "Test CEL for a single party entering/leaving a bridge";
431 "Test CEL records for a call that is\n"
432 "answered, enters a bridge, and leaves it.\n";
433 return AST_TEST_NOT_RUN;
437 bridge = ast_bridge_basic_new();
438 ast_test_validate(test, bridge != NULL);
440 CREATE_ALICE_CHANNEL(chan, &caller);
442 ANSWER_CHANNEL(chan);
443 EMULATE_APP_DATA(chan, 2, "Bridge", "");
446 ast_bridge_impart(bridge, chan, NULL, NULL, 0);
450 ast_bridge_depart(chan);
452 EMULATE_APP_DATA(chan, 3, "Wait", "");
454 /* And then it hangs up */
455 HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
457 return AST_TEST_PASS;
460 AST_TEST_DEFINE(test_cel_single_twoparty_bridge_a)
462 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
463 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
464 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
465 struct ast_party_caller caller_alice = ALICE_CALLERID;
466 struct ast_party_caller caller_bob = BOB_CALLERID;
470 info->name = __func__;
471 info->category = TEST_CATEGORY;
472 info->summary = "Test CEL for a single party entering/leaving a bridge";
474 "Test CEL records for a call that is\n"
475 "answered, enters a bridge, and leaves it. In this scenario, the\n"
476 "Party A should answer the bridge first.\n";
477 return AST_TEST_NOT_RUN;
481 bridge = ast_bridge_basic_new();
482 ast_test_validate(test, bridge != NULL);
484 CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
486 CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
488 ANSWER_CHANNEL(chan_alice);
489 EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
491 ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
494 ANSWER_CHANNEL(chan_bob);
495 EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
497 ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
499 APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_bob));
501 ast_bridge_depart(chan_alice);
502 ast_bridge_depart(chan_bob);
503 APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_bob));
505 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
506 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
508 return AST_TEST_PASS;
511 AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
513 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
514 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
515 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
516 struct ast_party_caller caller_alice = ALICE_CALLERID;
517 struct ast_party_caller caller_bob = BOB_CALLERID;
521 info->name = __func__;
522 info->category = TEST_CATEGORY;
523 info->summary = "Test CEL for a single party entering/leaving a bridge";
525 "Test CEL records for a call that is\n"
526 "answered, enters a bridge, and leaves it. In this scenario, the\n"
527 "Party B should answer the bridge first.\n";
528 return AST_TEST_NOT_RUN;
532 bridge = ast_bridge_basic_new();
533 ast_test_validate(test, bridge != NULL);
535 CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
537 CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
539 ANSWER_CHANNEL(chan_alice);
540 EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
542 ANSWER_CHANNEL(chan_bob);
543 EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
546 ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
549 ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
551 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
553 ast_bridge_depart(chan_alice);
554 ast_bridge_depart(chan_bob);
555 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
557 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
558 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
560 return AST_TEST_PASS;
563 /* XXX Validation needs to be reworked on a per-channel basis before
564 * test_cel_single_multiparty_bridge and test_cel_dial_answer_multiparty
565 * can operate properly. */
567 AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
569 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
570 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
571 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
572 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
573 struct ast_party_caller caller_alice = ALICE_CALLERID;
574 struct ast_party_caller caller_bob = BOB_CALLERID;
575 struct ast_party_caller caller_charlie = CHARLIE_CALLERID;
579 info->name = __func__;
580 info->category = TEST_CATEGORY;
581 info->summary = "Test CEL for a single party entering/leaving a multi-party bridge";
583 "Test CEL records for a call that is\n"
584 "answered, enters a bridge, and leaves it. A total of three\n"
585 "parties perform this action.\n";
586 return AST_TEST_NOT_RUN;
590 bridge = ast_bridge_basic_new();
591 ast_test_validate(test, bridge != NULL);
593 CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
594 CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
595 CREATE_CHARLIE_CHANNEL(chan_charlie, &caller_charlie);
597 ANSWER_CHANNEL(chan_alice);
598 EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
602 ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
604 ANSWER_CHANNEL(chan_bob);
605 EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
608 ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
610 APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_bob));
612 ANSWER_CHANNEL(chan_charlie);
613 EMULATE_APP_DATA(chan_charlie, 2, "Bridge", "");
615 ast_bridge_impart(bridge, chan_charlie, NULL, NULL, 0);
617 BRIDGE_TO_CONF(chan_alice, chan_bob, chan_charlie, bridge);
619 CONF_EXIT(chan_alice, bridge);
620 CONF_EXIT(chan_bob, bridge);
621 CONF_EXIT(chan_charlie, bridge);
623 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
624 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
625 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
627 return AST_TEST_PASS;
631 #define EMULATE_DIAL(channel, dialstring) do { \
632 EMULATE_APP_DATA(channel, 1, "Dial", dialstring); \
633 if (append_expected_event(channel, AST_CEL_APP_START, NULL, NULL, NULL)) { \
634 return AST_TEST_FAIL; \
638 #define START_DIALED(caller, callee) \
639 START_DIALED_FULL(caller, callee, "200", "Bob")
641 #define START_DIALED_FULL(caller, callee, number, name) do { \
642 callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, number, NULL, NULL, ast_channel_linkedid(caller), 0, CHANNEL_TECH_NAME "/" name); \
643 if (append_expected_event(callee, AST_CEL_CHANNEL_START, NULL, NULL, NULL)) { \
644 return AST_TEST_FAIL; \
646 ast_set_flag(ast_channel_flags(callee), AST_FLAG_OUTGOING); \
647 EMULATE_APP_DATA(callee, 0, "AppDial", "(Outgoing Line)"); \
648 ast_channel_publish_dial(caller, callee, name, NULL); \
651 AST_TEST_DEFINE(test_cel_dial_unanswered)
653 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
654 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
655 struct ast_party_caller caller = ALICE_CALLERID;
659 info->name = __func__;
660 info->category = TEST_CATEGORY;
661 info->summary = "Test CEL for a dial that isn't answered";
663 "Test CEL records for a channel that\n"
664 "performs a dial operation that isn't answered\n";
665 return AST_TEST_NOT_RUN;
670 CREATE_ALICE_CHANNEL(chan_caller, &caller);
672 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
674 START_DIALED(chan_caller, chan_callee);
676 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
677 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
679 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER, "NOANSWER");
680 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER, "");
682 return AST_TEST_PASS;
686 AST_TEST_DEFINE(test_cel_dial_busy)
688 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
689 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
690 struct ast_party_caller caller = ALICE_CALLERID;
694 info->name = __func__;
695 info->category = TEST_CATEGORY;
696 info->summary = "Test CEL for a dial that results in a busy";
698 "Test CEL records for a channel that\n"
699 "performs a dial operation to an endpoint that's busy\n";
700 return AST_TEST_NOT_RUN;
705 CREATE_ALICE_CHANNEL(chan_caller, &caller);
707 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
709 START_DIALED(chan_caller, chan_callee);
711 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
712 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "BUSY");
714 HANGUP_CHANNEL(chan_caller, AST_CAUSE_BUSY, "BUSY");
715 HANGUP_CHANNEL(chan_callee, AST_CAUSE_BUSY, "");
717 return AST_TEST_PASS;
720 AST_TEST_DEFINE(test_cel_dial_congestion)
722 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
723 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
724 struct ast_party_caller caller = ALICE_CALLERID;
728 info->name = __func__;
729 info->category = TEST_CATEGORY;
730 info->summary = "Test CEL for a dial that results in congestion";
732 "Test CEL records for a channel that\n"
733 "performs a dial operation to an endpoint that's congested\n";
734 return AST_TEST_NOT_RUN;
739 CREATE_ALICE_CHANNEL(chan_caller, &caller);
741 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
743 START_DIALED(chan_caller, chan_callee);
745 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
746 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CONGESTION");
748 HANGUP_CHANNEL(chan_caller, AST_CAUSE_CONGESTION, "CONGESTION");
749 HANGUP_CHANNEL(chan_callee, AST_CAUSE_CONGESTION, "");
751 return AST_TEST_PASS;
754 AST_TEST_DEFINE(test_cel_dial_unavailable)
756 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
757 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
758 struct ast_party_caller caller = ALICE_CALLERID;
762 info->name = __func__;
763 info->category = TEST_CATEGORY;
764 info->summary = "Test CEL for a dial that results in unavailable";
766 "Test CEL records for a channel that\n"
767 "performs a dial operation to an endpoint that's unavailable\n";
768 return AST_TEST_NOT_RUN;
773 CREATE_ALICE_CHANNEL(chan_caller, &caller);
775 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
777 START_DIALED(chan_caller, chan_callee);
779 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
780 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CHANUNAVAIL");
782 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ROUTE_DESTINATION, "CHANUNAVAIL");
783 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ROUTE_DESTINATION, "");
785 return AST_TEST_PASS;
788 AST_TEST_DEFINE(test_cel_dial_caller_cancel)
790 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
791 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
792 struct ast_party_caller caller = ALICE_CALLERID;
796 info->name = __func__;
797 info->category = TEST_CATEGORY;
798 info->summary = "Test CEL for a dial where the caller cancels";
800 "Test CEL records for a channel that\n"
801 "performs a dial operation to an endpoint but then decides\n"
802 "to hang up, cancelling the dial\n";
803 return AST_TEST_NOT_RUN;
808 CREATE_ALICE_CHANNEL(chan_caller, &caller);
810 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
812 START_DIALED(chan_caller, chan_callee);
814 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
815 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CANCEL");
817 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
818 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "CANCEL");
820 return AST_TEST_PASS;
823 AST_TEST_DEFINE(test_cel_dial_parallel_failed)
825 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
826 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
827 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
828 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
829 struct ast_party_caller caller = ALICE_CALLERID;
833 info->name = __func__;
834 info->category = TEST_CATEGORY;
835 info->summary = "Test a parallel dial where all channels fail to answer";
837 "This tests dialing three parties: Bob, Charlie, David. Charlie\n"
838 "returns BUSY; David returns CONGESTION; Bob fails to answer and\n"
839 "Alice hangs up. Three records are created for Alice as a result.\n";
840 return AST_TEST_NOT_RUN;
845 CREATE_ALICE_CHANNEL(chan_caller, &caller);
847 /* Channel enters Dial app */
848 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David");
850 /* Outbound channels are created */
851 START_DIALED_FULL(chan_caller, chan_bob, "200", "Bob");
852 START_DIALED_FULL(chan_caller, chan_charlie, "300", "Charlie");
853 START_DIALED_FULL(chan_caller, chan_david, "400", "David");
856 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
858 /* Charlie is busy */
859 ast_channel_publish_dial(chan_caller, chan_charlie, NULL, "BUSY");
860 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_BUSY, "");
862 /* David is congested */
863 ast_channel_publish_dial(chan_caller, chan_david, NULL, "CONGESTION");
864 HANGUP_CHANNEL(chan_david, AST_CAUSE_CONGESTION, "");
866 /* Bob is canceled */
867 ast_channel_publish_dial(chan_caller, chan_bob, NULL, "CANCEL");
868 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
871 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "BUSY");
873 return AST_TEST_PASS;
876 AST_TEST_DEFINE(test_cel_dial_answer_no_bridge)
878 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
879 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
880 struct ast_party_caller caller = ALICE_CALLERID;
884 info->name = __func__;
885 info->category = TEST_CATEGORY;
886 info->summary = "Test dialing, answering, and not going into a bridge.";
888 "This is a weird one, but theoretically possible. You can perform\n"
889 "a dial, then bounce both channels to different priorities and\n"
890 "never have them enter a bridge together. Ew. This makes sure that\n"
891 "when we answer, we get a CEL, it gets ended at that point, and\n"
892 "that it gets finalized appropriately.\n";
893 return AST_TEST_NOT_RUN;
898 CREATE_ALICE_CHANNEL(chan_caller, &caller);
900 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
902 START_DIALED(chan_caller, chan_callee);
904 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
905 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
907 ANSWER_NO_APP(chan_caller);
908 ast_clear_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
909 ANSWER_NO_APP(chan_callee);
911 EMULATE_APP_DATA(chan_caller, 2, "Wait", "1");
912 EMULATE_APP_DATA(chan_callee, 1, "Wait", "1");
914 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
915 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
917 return AST_TEST_PASS;
920 AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_a)
922 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
923 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
924 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
925 struct ast_party_caller caller = ALICE_CALLERID;
929 info->name = __func__;
930 info->category = TEST_CATEGORY;
931 info->summary = "Test dialing, answering, and going into a 2-party bridge";
933 "The most 'basic' of scenarios\n";
934 return AST_TEST_NOT_RUN;
938 bridge = ast_bridge_basic_new();
939 ast_test_validate(test, bridge != NULL);
941 CREATE_ALICE_CHANNEL(chan_caller, &caller);
943 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
945 START_DIALED(chan_caller, chan_callee);
947 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
948 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
950 ANSWER_NO_APP(chan_caller);
951 ANSWER_NO_APP(chan_callee);
955 ast_bridge_impart(bridge, chan_caller, NULL, NULL, 0);
957 ast_bridge_impart(bridge, chan_callee, NULL, NULL, 0);
959 APPEND_EVENT(chan_caller, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_callee));
961 ast_bridge_depart(chan_caller);
962 ast_bridge_depart(chan_callee);
963 APPEND_EVENT(chan_caller, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_callee));
965 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
966 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
968 return AST_TEST_PASS;
971 AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
973 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
974 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
975 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
976 struct ast_party_caller caller = ALICE_CALLERID;
980 info->name = __func__;
981 info->category = TEST_CATEGORY;
982 info->summary = "Test dialing, answering, and going into a 2-party bridge";
984 "The most 'basic' of scenarios\n";
985 return AST_TEST_NOT_RUN;
989 bridge = ast_bridge_basic_new();
990 ast_test_validate(test, bridge != NULL);
992 CREATE_ALICE_CHANNEL(chan_caller, &caller);
994 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
996 START_DIALED(chan_caller, chan_callee);
998 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
999 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1001 ANSWER_NO_APP(chan_caller);
1002 ANSWER_NO_APP(chan_callee);
1005 ast_bridge_impart(bridge, chan_callee, NULL, NULL, 0);
1007 ast_bridge_impart(bridge, chan_caller, NULL, NULL, 0);
1009 APPEND_EVENT(chan_callee, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_caller));
1011 ast_bridge_depart(chan_caller);
1012 ast_bridge_depart(chan_callee);
1013 APPEND_EVENT(chan_callee, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_caller));
1015 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1016 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1018 return AST_TEST_PASS;
1022 AST_TEST_DEFINE(test_cel_dial_answer_multiparty)
1024 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1025 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1026 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1027 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1028 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
1029 struct ast_party_caller alice_caller = ALICE_CALLERID;
1030 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1034 info->name = __func__;
1035 info->category = TEST_CATEGORY;
1036 info->summary = "Test dialing, answering, and going into a multi-party bridge";
1038 "A little tricky to get to do, but possible with some redirects.\n";
1039 return AST_TEST_NOT_RUN;
1043 bridge = ast_bridge_basic_new();
1044 ast_test_validate(test, bridge != NULL);
1046 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1048 EMULATE_DIAL(chan_alice, CHANNEL_TECH_NAME "/Bob");
1050 START_DIALED(chan_alice, chan_bob);
1053 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1055 EMULATE_DIAL(chan_charlie, CHANNEL_TECH_NAME "/Bob");
1058 START_DIALED_FULL(chan_charlie, chan_david, "400", "David");
1060 ast_channel_state_set(chan_alice, AST_STATE_RINGING);
1062 ast_channel_state_set(chan_charlie, AST_STATE_RINGING);
1064 ast_channel_publish_dial(chan_alice, chan_bob, NULL, "ANSWER");
1066 ast_channel_publish_dial(chan_charlie, chan_david, NULL, "ANSWER");
1069 ANSWER_NO_APP(chan_alice);
1071 ANSWER_NO_APP(chan_bob);
1073 ANSWER_NO_APP(chan_charlie);
1075 ANSWER_NO_APP(chan_david);
1079 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_charlie, NULL, NULL, 0));
1081 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_david, NULL, NULL, 0));
1083 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1085 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0));
1087 BRIDGE_TO_CONF(chan_charlie, chan_david, chan_bob, bridge);
1089 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0));
1091 CONF_ENTER_EVENT(chan_alice, bridge);
1093 CONF_EXIT(chan_alice, bridge);
1094 CONF_EXIT(chan_bob, bridge);
1095 CONF_EXIT(chan_charlie, bridge);
1096 CONF_EXIT(chan_david, bridge);
1098 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "ANSWER");
1099 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1100 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "ANSWER");
1101 HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1103 return AST_TEST_PASS;
1107 AST_TEST_DEFINE(test_cel_blind_transfer)
1109 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1110 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1111 RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
1112 struct ast_party_caller alice_caller = ALICE_CALLERID;
1113 struct ast_party_caller bob_caller = BOB_CALLERID;
1114 struct ast_bridge_channel_pair pair;
1118 info->name = __func__;
1119 info->category = TEST_CATEGORY;
1120 info->summary = "Test blind transfers to an extension";
1122 "This test creates two channels, bridges them, and then"
1123 " blind transfers the bridge to an extension.\n";
1124 return AST_TEST_NOT_RUN;
1128 bridge = ast_bridge_basic_new();
1129 ast_test_validate(test, bridge != NULL);
1131 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1132 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1134 ANSWER_NO_APP(chan_alice);
1135 ANSWER_NO_APP(chan_bob);
1137 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0));
1140 ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0));
1142 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1144 pair.bridge = bridge;
1145 pair.channel = chan_alice;
1146 ast_bridge_publish_blind_transfer(1, AST_BRIDGE_TRANSFER_SUCCESS,
1147 &pair, "transfer_context", "transfer_extension");
1148 BLINDTRANSFER_EVENT(chan_alice, bridge, "transfer_extension", "transfer_context");
1150 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
1151 ast_test_validate(test, 0 == ast_bridge_depart(chan_alice));
1153 ast_test_validate(test, 0 == ast_bridge_depart(chan_bob));
1155 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1157 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1159 return AST_TEST_PASS;
1162 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
1164 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1165 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1166 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1167 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1168 RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1169 RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1170 struct ast_party_caller alice_caller = ALICE_CALLERID;
1171 struct ast_party_caller bob_caller = BOB_CALLERID;
1172 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1173 struct ast_party_caller david_caller = ALICE_CALLERID;
1177 info->name = __func__;
1178 info->category = TEST_CATEGORY;
1179 info->summary = "Test attended transfers between two pairs of bridged parties";
1181 "This test creates four channels, places each pair in"
1182 " a bridge, and then attended transfers the bridges"
1184 return AST_TEST_NOT_RUN;
1188 /* Create first set of bridged parties */
1189 bridge1 = ast_bridge_basic_new();
1190 ast_test_validate(test, bridge1 != NULL);
1192 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1193 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1194 ANSWER_NO_APP(chan_alice);
1195 ANSWER_NO_APP(chan_bob);
1197 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1200 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1202 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1204 /* Create second set of bridged parties */
1205 bridge2 = ast_bridge_basic_new();
1206 ast_test_validate(test, bridge2 != NULL);
1208 CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1209 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1210 ANSWER_NO_APP(chan_david);
1211 ANSWER_NO_APP(chan_charlie);
1213 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1216 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1218 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1220 /* Perform attended transfer */
1221 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
1223 ast_bridge_transfer_attended(chan_alice, chan_david);
1225 BRIDGE_TO_CONF(chan_charlie, chan_david, chan_bob, bridge2);
1226 CONF_EXIT_EVENT(chan_david, bridge2);
1228 ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1231 CONF_EXIT(chan_bob, bridge2);
1232 CONF_EXIT(chan_charlie, bridge2);
1234 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1236 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1238 HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1240 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1242 return AST_TEST_PASS;
1245 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
1247 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1248 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1249 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1250 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1251 RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1252 RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1253 struct ast_party_caller alice_caller = ALICE_CALLERID;
1254 struct ast_party_caller bob_caller = BOB_CALLERID;
1255 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1256 struct ast_party_caller david_caller = ALICE_CALLERID;
1260 info->name = __func__;
1261 info->category = TEST_CATEGORY;
1262 info->summary = "Test attended transfers between two pairs of"
1263 " bridged parties that results in a bridge merge";
1265 "This test creates four channels, places each pair"
1266 " in a bridge, and then attended transfers the bridges"
1267 " together causing a bridge merge.\n";
1268 return AST_TEST_NOT_RUN;
1272 /* Create first set of bridged parties */
1273 bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1274 AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1275 ast_test_validate(test, bridge1 != NULL);
1277 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1278 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1279 ANSWER_NO_APP(chan_alice);
1280 ANSWER_NO_APP(chan_bob);
1282 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1285 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1287 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1289 /* Create second set of bridged parties */
1290 bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1291 AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1292 ast_test_validate(test, bridge2 != NULL);
1294 CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1295 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1296 ANSWER_NO_APP(chan_david);
1297 ANSWER_NO_APP(chan_charlie);
1299 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1302 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1304 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1306 /* Perform attended transfer */
1307 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_david));
1309 ast_bridge_transfer_attended(chan_alice, chan_david);
1311 BRIDGE_TO_CONF(chan_bob, chan_alice, chan_charlie, bridge1);
1312 CONF_EXIT_EVENT(chan_alice, bridge1);
1314 ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1317 CONF_EXIT(chan_bob, bridge1);
1318 CONF_EXIT(chan_charlie, bridge1);
1320 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1322 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1324 HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1326 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1328 return AST_TEST_PASS;
1331 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
1333 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1334 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1335 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1336 RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1337 RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
1338 RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
1339 struct ast_party_caller alice_caller = ALICE_CALLERID;
1340 struct ast_party_caller bob_caller = BOB_CALLERID;
1341 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1342 struct ast_party_caller david_caller = ALICE_CALLERID;
1346 info->name = __func__;
1347 info->category = TEST_CATEGORY;
1348 info->summary = "Test attended transfers between two pairs of"
1349 " bridged parties that results in a bridge merge";
1351 "This test creates four channels, places each pair"
1352 " in a bridge, and then attended transfers the bridges"
1353 " together causing a bridge link.\n";
1354 return AST_TEST_NOT_RUN;
1358 /* Create first set of bridged parties */
1359 bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1360 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1361 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1362 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1363 ast_test_validate(test, bridge1 != NULL);
1365 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1366 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1367 ANSWER_NO_APP(chan_alice);
1368 ANSWER_NO_APP(chan_bob);
1370 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
1373 ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
1375 APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
1377 /* Create second set of bridged parties */
1378 bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1379 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1380 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1381 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
1382 ast_test_validate(test, bridge2 != NULL);
1384 CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1385 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1386 ANSWER_NO_APP(chan_david);
1387 ANSWER_NO_APP(chan_charlie);
1389 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
1392 ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
1394 APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
1396 /* Perform attended transfer */
1398 /* The following events can not be matched directly since nothing is known
1399 * about the linking local channel.
1400 * local channel ;1 and ;2 creation and ;2 answer */
1401 APPEND_DUMMY_EVENT();
1402 APPEND_DUMMY_EVENT();
1403 APPEND_DUMMY_EVENT();
1405 ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
1407 /* The two BRIDGE_TO_CONFs and CONF_EXITs are all racing to be first */
1409 /* BRIDGE_TO_CONF with primary charlie, peer david, and trigger channel ;2 */
1410 APPEND_DUMMY_EVENT();
1412 ast_bridge_transfer_attended(chan_alice, chan_david);
1415 /* BRIDGE_TO_CONF with primary bob, peer alice, and trigger channel ;1 */
1416 APPEND_DUMMY_EVENT();
1418 /* CONF_EXIT alice and david */
1419 APPEND_DUMMY_EVENT();
1420 APPEND_DUMMY_EVENT();
1423 APPEND_DUMMY_EVENT();
1426 CONF_EXIT(chan_bob, bridge1);
1427 CONF_EXIT(chan_charlie, bridge2);
1429 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1431 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1433 HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1435 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1437 return AST_TEST_PASS;
1440 AST_TEST_DEFINE(test_cel_dial_pickup)
1442 RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1443 RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1444 RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1445 struct ast_party_caller caller = ALICE_CALLERID;
1446 struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1450 info->name = __func__;
1451 info->category = TEST_CATEGORY;
1452 info->summary = "Test call pickup";
1454 "Test CEL records for a call that is\n"
1455 "inbound to Asterisk, executes some dialplan, and\n"
1457 return AST_TEST_NOT_RUN;
1462 CREATE_ALICE_CHANNEL(chan_caller, &caller);
1464 EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
1466 START_DIALED(chan_caller, chan_callee);
1468 ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1470 CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1473 SCOPED_CHANNELLOCK(lock, chan_callee);
1474 APPEND_EVENT(chan_callee, AST_CEL_PICKUP, NULL, NULL, ast_channel_name(chan_charlie));
1475 ast_test_validate(test, 0 == ast_do_pickup(chan_charlie, chan_callee));
1478 /* Hang up the masqueraded zombie */
1479 HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1481 ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1483 HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1484 HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1486 return AST_TEST_PASS;
1489 AST_TEST_DEFINE(test_cel_local_optimize)
1491 RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1492 RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1493 struct ast_party_caller alice_caller = ALICE_CALLERID;
1494 struct ast_party_caller bob_caller = BOB_CALLERID;
1495 RAII_VAR(struct ast_multi_channel_blob *, mc_blob, NULL, ao2_cleanup);
1496 RAII_VAR(struct ast_channel_snapshot *, alice_snapshot, NULL, ao2_cleanup);
1497 RAII_VAR(struct ast_channel_snapshot *, bob_snapshot, NULL, ao2_cleanup);
1498 RAII_VAR(struct stasis_message *, local_opt_begin, NULL, ao2_cleanup);
1499 RAII_VAR(struct stasis_message *, local_opt_end, NULL, ao2_cleanup);
1503 info->name = __func__;
1504 info->category = TEST_CATEGORY;
1505 info->summary = "Test local channel optimization record generation";
1507 "Test CEL records for two local channels being optimized\n"
1508 "out by sending a messages indicating local optimization\n"
1510 return AST_TEST_NOT_RUN;
1515 mc_blob = ast_multi_channel_blob_create(ast_json_null());
1516 ast_test_validate(test, mc_blob != NULL);
1518 CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1519 CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1521 alice_snapshot = ast_channel_snapshot_create(chan_alice);
1522 ast_test_validate(test, alice_snapshot != NULL);
1524 bob_snapshot = ast_channel_snapshot_create(chan_bob);
1525 ast_test_validate(test, bob_snapshot != NULL);
1527 ast_multi_channel_blob_add_channel(mc_blob, "1", alice_snapshot);
1528 ast_multi_channel_blob_add_channel(mc_blob, "2", bob_snapshot);
1530 local_opt_begin = stasis_message_create(ast_local_optimization_begin_type(), mc_blob);
1531 ast_test_validate(test, local_opt_begin != NULL);
1533 local_opt_end = stasis_message_create(ast_local_optimization_end_type(), mc_blob);
1534 ast_test_validate(test, local_opt_end != NULL);
1536 stasis_publish(ast_channel_topic(chan_alice), local_opt_begin);
1537 stasis_publish(ast_channel_topic(chan_alice), local_opt_end);
1538 APPEND_EVENT_SNAPSHOT(alice_snapshot, AST_CEL_LOCAL_OPTIMIZE, NULL, NULL, bob_snapshot->name);
1540 HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1541 HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1543 return AST_TEST_PASS;
1546 /*! Subscription for CEL events */
1547 static struct ast_event_sub *event_sub = NULL;
1549 /*! Container for astobj2 duplicated ast_events */
1550 static struct ao2_container *cel_received_events = NULL;
1552 /*! Container for expected CEL events */
1553 static struct ao2_container *cel_expected_events = NULL;
1555 static struct ast_event *ao2_dup_event(const struct ast_event *event)
1557 struct ast_event *event_dup;
1560 event_len = ast_event_get_size(event);
1562 event_dup = ao2_alloc(event_len, NULL);
1567 memcpy(event_dup, event, event_len);
1572 static void mid_test_sync(void)
1574 ast_mutex_lock(&mid_test_sync_lock);
1575 if (ao2_container_count(cel_expected_events) <= ao2_container_count(cel_received_events)) {
1576 ast_mutex_unlock(&mid_test_sync_lock);
1580 do_mid_test_sync = 1;
1581 ast_mutex_unlock(&mid_test_sync_lock);
1584 struct timeval start = ast_tvnow();
1585 struct timespec end = {
1586 .tv_sec = start.tv_sec + 15,
1587 .tv_nsec = start.tv_usec * 1000
1590 SCOPED_MUTEX(lock, &sync_lock);
1591 ast_cond_timedwait(&sync_out, &sync_lock, &end);
1595 static int append_event(struct ast_event *ev)
1597 RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1598 ao2_ev = ao2_dup_event(ev);
1603 ao2_link(cel_expected_events, ao2_ev);
1607 static int append_dummy_event(void)
1609 RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1610 RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1612 ev = ast_event_new(AST_EVENT_CUSTOM, AST_EVENT_IE_END);
1617 return append_event(ev);
1620 static int append_expected_event_snapshot(
1621 struct ast_channel_snapshot *snapshot,
1622 enum ast_cel_event_type type,
1623 const char *userdefevname,
1624 struct ast_json *extra, const char *peer)
1626 RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1627 ev = ast_cel_create_event(snapshot, type, userdefevname, extra, peer);
1632 return append_event(ev);
1635 static int append_expected_event(
1636 struct ast_channel *chan,
1637 enum ast_cel_event_type type,
1638 const char *userdefevname,
1639 struct ast_json *extra, const char *peer)
1641 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1642 snapshot = ast_channel_snapshot_create(chan);
1647 return append_expected_event_snapshot(snapshot, type, userdefevname, extra, peer);
1650 static void test_sub(const struct ast_event *event, void *data)
1652 struct ast_event *event_dup = ao2_dup_event(event);
1653 const char *sync_tag;
1654 SCOPED_MUTEX(mid_test_lock, &mid_test_sync_lock);
1660 sync_tag = ast_event_get_ie_str(event, AST_EVENT_IE_SERVICE);
1662 if (!strcmp(sync_tag, "SYNC")) {
1663 /* trigger things */
1664 SCOPED_MUTEX(lock, &sync_lock);
1665 ast_cond_signal(&sync_out);
1670 /* save the event for later processing */
1671 ao2_link(cel_received_events, event_dup);
1673 if (do_mid_test_sync) {
1674 int expected = ao2_container_count(cel_expected_events);
1675 int received = ao2_container_count(cel_received_events);
1676 if (expected <= received) {
1678 SCOPED_MUTEX(lock, &sync_lock);
1679 ast_cond_signal(&sync_out);
1680 do_mid_test_sync = 0;
1687 * \internal \brief Callback function called before each test executes
1689 static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
1691 ast_assert(event_sub == NULL);
1692 ast_assert(cel_received_events == NULL);
1693 ast_assert(cel_expected_events == NULL);
1695 ast_mutex_init(&mid_test_sync_lock);
1696 ast_mutex_init(&sync_lock);
1697 ast_cond_init(&sync_out, NULL);
1699 /* Back up the real CEL config and insert the test's config */
1700 saved_config = ast_cel_get_config();
1701 ast_cel_set_config(cel_test_config);
1703 /* init CEL event storage (degenerate hash table becomes a linked list) */
1704 cel_received_events = ao2_container_alloc(1, NULL, NULL);
1705 cel_expected_events = ao2_container_alloc(1, NULL, NULL);
1707 /* start the CEL event callback */
1708 event_sub = ast_event_subscribe(AST_EVENT_CEL, test_sub, "CEL Test Logging",
1709 NULL, AST_EVENT_IE_END);
1713 /*! \brief Check an IE value from two events, */
1714 static int match_ie_val(
1715 const struct ast_event *event1,
1716 const struct ast_event *event2,
1717 enum ast_event_ie_type type)
1719 enum ast_event_ie_pltype pltype = ast_event_get_ie_pltype(type);
1722 case AST_EVENT_IE_PLTYPE_UINT:
1724 uint32_t val = ast_event_get_ie_uint(event2, type);
1726 return (val == ast_event_get_ie_uint(event1, type)) ? 1 : 0;
1728 case AST_EVENT_IE_PLTYPE_STR:
1733 hash = ast_event_get_ie_str_hash(event2, type);
1734 if (hash != ast_event_get_ie_str_hash(event1, type)) {
1738 str = ast_event_get_ie_str(event2, type);
1740 const char *e1str, *e2str;
1741 e1str = ast_event_get_ie_str(event1, type);
1744 if (type == AST_EVENT_IE_DEVICE) {
1745 e1str = ast_tech_to_upper(ast_strdupa(e1str));
1746 e2str = ast_tech_to_upper(ast_strdupa(e2str));
1749 if (!strcmp(e1str, e2str)) {
1762 static int events_are_equal(struct ast_test *test, struct ast_event *received, struct ast_event *expected)
1764 struct ast_event_iterator iterator;
1767 if (ast_event_get_type(expected) == AST_EVENT_CUSTOM) {
1768 /* this event is flagged as a wildcard match */
1772 for (res = ast_event_iterator_init(&iterator, received); !res; res = ast_event_iterator_next(&iterator)) {
1773 /* XXX ignore sec/usec for now */
1775 int ie_type = ast_event_iterator_get_ie_type(&iterator);
1776 if (ie_type != AST_EVENT_IE_CEL_EVENT_TIME_USEC
1777 && ie_type != AST_EVENT_IE_EID
1778 && ie_type != AST_EVENT_IE_CEL_EVENT_TIME
1779 && !match_ie_val(received, expected, ie_type)) {
1780 ast_test_status_update(test, "Failed matching on field %s\n", ast_event_get_ie_type_name(ie_type));
1788 static int dump_event(struct ast_test *test, struct ast_event *event)
1790 struct ast_event_iterator i;
1792 if (ast_event_iterator_init(&i, event)) {
1793 ast_test_status_update(test, "Failed to initialize event iterator. :-(\n");
1797 ast_test_status_update(test, "Event: %s %s\n", ast_event_get_type_name(event),
1798 ast_cel_get_type_name(ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TYPE)));
1801 enum ast_event_ie_type ie_type;
1802 enum ast_event_ie_pltype ie_pltype;
1803 const char *ie_type_name;
1805 ie_type = ast_event_iterator_get_ie_type(&i);
1806 ie_type_name = ast_event_get_ie_type_name(ie_type);
1807 ie_pltype = ast_event_get_ie_pltype(ie_type);
1809 switch (ie_pltype) {
1810 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1811 case AST_EVENT_IE_PLTYPE_EXISTS:
1812 ast_test_status_update(test, "%s\n", ie_type_name);
1814 case AST_EVENT_IE_PLTYPE_STR:
1815 ast_test_status_update(test, "%.30s: %s\n", ie_type_name,
1816 ast_event_iterator_get_ie_str(&i));
1818 case AST_EVENT_IE_PLTYPE_UINT:
1819 ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
1820 ast_event_iterator_get_ie_uint(&i));
1822 case AST_EVENT_IE_PLTYPE_BITFLAGS:
1823 ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
1824 ast_event_iterator_get_ie_bitflags(&i));
1828 } while (!ast_event_iterator_next(&i));
1830 ast_test_status_update(test, "\n");
1835 static int check_events(struct ast_test *test, struct ao2_container *local_expected, struct ao2_container *local_received)
1837 struct ao2_iterator expected_it, received_it;
1838 struct ast_event *rx_event, *ex_event;
1841 if (ao2_container_count(local_expected) != ao2_container_count(local_received)) {
1842 ast_test_status_update(test, "Increasing verbosity since the number of expected events (%d)"
1843 " did not match number of received events (%d).\n",
1844 ao2_container_count(local_expected),
1845 ao2_container_count(local_received));
1849 expected_it = ao2_iterator_init(local_expected, 0);
1850 received_it = ao2_iterator_init(local_received, 0);
1851 rx_event = ao2_iterator_next(&received_it);
1852 ex_event = ao2_iterator_next(&expected_it);
1853 while (rx_event && ex_event) {
1854 if (!events_are_equal(test, rx_event, ex_event)) {
1855 ast_test_status_update(test, "Received event:\n");
1856 dump_event(test, rx_event);
1857 ast_test_status_update(test, "Expected event:\n");
1858 dump_event(test, ex_event);
1862 ast_test_status_update(test, "Compared events successfully%s\n", ast_event_get_type(ex_event) == AST_EVENT_CUSTOM ? " (wildcard match)" : "");
1863 dump_event(test, rx_event);
1865 ao2_cleanup(rx_event);
1866 ao2_cleanup(ex_event);
1867 rx_event = ao2_iterator_next(&received_it);
1868 ex_event = ao2_iterator_next(&expected_it);
1872 ast_test_status_update(test, "Received event:\n");
1873 dump_event(test, rx_event);
1874 ao2_cleanup(rx_event);
1878 ast_test_status_update(test, "Expected event:\n");
1879 dump_event(test, ex_event);
1880 ao2_cleanup(ex_event);
1886 static struct ast_event *create_sync_event(void)
1888 struct ast_event *event_dup;
1889 RAII_VAR(struct ast_event *, event, ao2_callback(cel_expected_events, 0, NULL, NULL), ao2_cleanup);
1896 event_len = ast_event_get_size(event);
1898 event_dup = ast_calloc(1, event_len);
1903 memcpy(event_dup, event, event_len);
1904 ast_event_append_ie_str(&event_dup, AST_EVENT_IE_SERVICE, "SYNC");
1910 * \internal \brief Callback function called after each test executes.
1911 * In addition to cleanup, this function also performs verification
1912 * that the events received during a test match the events that were
1913 * expected to have been generated during the test.
1915 static int cel_verify_and_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
1917 struct ast_event *sync;
1918 RAII_VAR(struct ao2_container *, local_expected, cel_expected_events, ao2_cleanup);
1919 RAII_VAR(struct ao2_container *, local_received, cel_received_events, ao2_cleanup);
1920 ast_assert(event_sub != NULL);
1921 ast_assert(cel_received_events != NULL);
1922 ast_assert(cel_expected_events != NULL);
1926 /* sync with the event system */
1927 sync = create_sync_event();
1928 ast_test_validate(test, sync != NULL);
1929 if (ast_event_queue(sync)) {
1930 ast_event_destroy(sync);
1931 ast_test_validate(test, NULL);
1933 struct timeval start = ast_tvnow();
1934 struct timespec end = {
1935 .tv_sec = start.tv_sec + 15,
1936 .tv_nsec = start.tv_usec * 1000
1939 SCOPED_MUTEX(lock, &sync_lock);
1940 ast_cond_timedwait(&sync_out, &sync_lock, &end);
1943 /* stop the CEL event callback and clean up storage structures*/
1944 ast_event_unsubscribe(event_sub);
1947 /* cleaned up by RAII_VAR's */
1948 cel_expected_events = NULL;
1949 cel_received_events = NULL;
1952 ast_test_validate(test, !check_events(test, local_expected, local_received));
1954 /* Restore the real CEL config */
1955 ast_cel_set_config(saved_config);
1956 ao2_cleanup(saved_config);
1957 saved_config = NULL;
1959 /* clean up the locks */
1960 ast_mutex_destroy(&sync_lock);
1961 ast_mutex_destroy(&mid_test_sync_lock);
1962 ast_cond_destroy(&sync_out);
1966 static int unload_module(void)
1968 AST_TEST_UNREGISTER(test_cel_channel_creation);
1969 AST_TEST_UNREGISTER(test_cel_unanswered_inbound_call);
1970 AST_TEST_UNREGISTER(test_cel_unanswered_outbound_call);
1971 AST_TEST_UNREGISTER(test_cel_single_party);
1972 AST_TEST_UNREGISTER(test_cel_single_bridge);
1973 AST_TEST_UNREGISTER(test_cel_single_bridge_continue);
1974 AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_a);
1975 AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_b);
1976 /*AST_TEST_UNREGISTER(test_cel_single_multiparty_bridge);*/
1978 AST_TEST_UNREGISTER(test_cel_dial_unanswered);
1979 AST_TEST_UNREGISTER(test_cel_dial_congestion);
1980 AST_TEST_UNREGISTER(test_cel_dial_busy);
1981 AST_TEST_UNREGISTER(test_cel_dial_unavailable);
1982 AST_TEST_UNREGISTER(test_cel_dial_caller_cancel);
1983 AST_TEST_UNREGISTER(test_cel_dial_parallel_failed);
1984 AST_TEST_UNREGISTER(test_cel_dial_answer_no_bridge);
1985 AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_a);
1986 AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_b);
1987 /*AST_TEST_UNREGISTER(test_cel_dial_answer_multiparty);*/
1989 AST_TEST_UNREGISTER(test_cel_blind_transfer);
1990 AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_swap);
1991 AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_merge);
1992 AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_link);
1994 AST_TEST_UNREGISTER(test_cel_dial_pickup);
1996 AST_TEST_UNREGISTER(test_cel_local_optimize);
1998 ast_channel_unregister(&test_cel_chan_tech);
2000 ao2_cleanup(cel_test_config);
2001 cel_test_config = NULL;
2006 static int load_module(void)
2008 /* build the test config */
2009 cel_test_config = ast_cel_general_config_alloc();
2010 if (!cel_test_config) {
2013 cel_test_config->enable = 1;
2014 if (ast_str_container_add(cel_test_config->apps, "dial")) {
2017 if (ast_str_container_add(cel_test_config->apps, "park")) {
2020 if (ast_str_container_add(cel_test_config->apps, "queue")) {
2023 cel_test_config->events |= 1<<AST_CEL_APP_START;
2024 cel_test_config->events |= 1<<AST_CEL_CHANNEL_START;
2025 cel_test_config->events |= 1<<AST_CEL_CHANNEL_END;
2026 cel_test_config->events |= 1<<AST_CEL_ANSWER;
2027 cel_test_config->events |= 1<<AST_CEL_HANGUP;
2028 cel_test_config->events |= 1<<AST_CEL_BRIDGE_START;
2029 cel_test_config->events |= 1<<AST_CEL_BRIDGE_END;
2030 cel_test_config->events |= 1<<AST_CEL_BRIDGE_TO_CONF;
2031 cel_test_config->events |= 1<<AST_CEL_CONF_ENTER;
2032 cel_test_config->events |= 1<<AST_CEL_CONF_EXIT;
2033 cel_test_config->events |= 1<<AST_CEL_BLINDTRANSFER;
2034 cel_test_config->events |= 1<<AST_CEL_ATTENDEDTRANSFER;
2035 cel_test_config->events |= 1<<AST_CEL_PICKUP;
2036 cel_test_config->events |= 1<<AST_CEL_LOCAL_OPTIMIZE;
2038 ast_channel_register(&test_cel_chan_tech);
2040 AST_TEST_REGISTER(test_cel_channel_creation);
2041 AST_TEST_REGISTER(test_cel_unanswered_inbound_call);
2042 AST_TEST_REGISTER(test_cel_unanswered_outbound_call);
2044 AST_TEST_REGISTER(test_cel_single_party);
2045 AST_TEST_REGISTER(test_cel_single_bridge);
2046 AST_TEST_REGISTER(test_cel_single_bridge_continue);
2047 AST_TEST_REGISTER(test_cel_single_twoparty_bridge_a);
2048 AST_TEST_REGISTER(test_cel_single_twoparty_bridge_b);
2049 /*AST_TEST_REGISTER(test_cel_single_multiparty_bridge);*/
2051 AST_TEST_REGISTER(test_cel_dial_unanswered);
2052 AST_TEST_REGISTER(test_cel_dial_congestion);
2053 AST_TEST_REGISTER(test_cel_dial_busy);
2054 AST_TEST_REGISTER(test_cel_dial_unavailable);
2055 AST_TEST_REGISTER(test_cel_dial_caller_cancel);
2056 AST_TEST_REGISTER(test_cel_dial_parallel_failed);
2057 AST_TEST_REGISTER(test_cel_dial_answer_no_bridge);
2058 AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_a);
2059 AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_b);
2060 /*AST_TEST_REGISTER(test_cel_dial_answer_multiparty);*/
2062 AST_TEST_REGISTER(test_cel_blind_transfer);
2063 AST_TEST_REGISTER(test_cel_attended_transfer_bridges_swap);
2064 AST_TEST_REGISTER(test_cel_attended_transfer_bridges_merge);
2065 AST_TEST_REGISTER(test_cel_attended_transfer_bridges_link);
2067 AST_TEST_REGISTER(test_cel_dial_pickup);
2069 AST_TEST_REGISTER(test_cel_local_optimize);
2071 /* ast_test_register_* has to happen after AST_TEST_REGISTER */
2072 /* Verify received vs expected events and clean things up after every test */
2073 ast_test_register_init(TEST_CATEGORY, test_cel_init_cb);
2074 ast_test_register_cleanup(TEST_CATEGORY, cel_verify_and_cleanup_cb);
2076 return AST_MODULE_LOAD_SUCCESS;
2079 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CEL unit tests");