tests/test_cel: Fix CEL unit test failures caused by attended transfer changes
[asterisk/asterisk.git] / tests / test_cel.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Kinsey Moore <kmoore@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*!
20  * \file
21  * \brief CEL unit tests
22  *
23  * \author Kinsey Moore <kmoore@digium.com>
24  *
25  */
26
27 /*** MODULEINFO
28         <depend>TEST_FRAMEWORK</depend>
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <math.h>
37 #include "asterisk/module.h"
38 #include "asterisk/test.h"
39 #include "asterisk/cel.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/format_cache.h"
42 #include "asterisk/linkedlists.h"
43 #include "asterisk/chanvars.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/time.h"
47 #include "asterisk/bridge.h"
48 #include "asterisk/bridge_basic.h"
49 #include "asterisk/pickup.h"
50 #include "asterisk/stasis_channels.h"
51 #include "asterisk/stasis_bridges.h"
52 #include "asterisk/json.h"
53 #include "asterisk/features.h"
54 #include "asterisk/core_local.h"
55
56 #define TEST_CATEGORY "/main/cel/"
57
58 #define CHANNEL_TECH_NAME "CELTestChannel"
59
60 #define TEST_BACKEND_NAME "CEL Test Logging"
61
62 /*! \brief A placeholder for Asterisk's 'real' CEL configuration */
63 static struct ast_cel_general_config *saved_config;
64
65 /*! \brief The CEL config used for CEL unit tests */
66 static struct ast_cel_general_config *cel_test_config;
67
68 /*! \brief Lock used for synchronizing test execution stages with received events */
69 ast_mutex_t mid_test_sync_lock;
70
71 /*! \brief Lock used with sync_out for checking the end of test execution */
72 ast_mutex_t sync_lock;
73
74 /*! \brief Condition used for checking the end of test execution */
75 ast_cond_t sync_out;
76
77 /*! \brief Flag used to trigger a mid-test synchronization, access controlled by mid_test_sync_lock */
78 int do_mid_test_sync = 0;
79
80 /*! \brief A channel technology used for the unit tests */
81 static struct ast_channel_tech test_cel_chan_tech = {
82         .type = CHANNEL_TECH_NAME,
83         .description = "Mock channel technology for CEL tests",
84 };
85
86 /*! \brief A 1 second sleep */
87 static struct timespec to_sleep = {1, 0};
88
89 static void do_sleep(void)
90 {
91         while ((nanosleep(&to_sleep, &to_sleep) == -1) && (errno == EINTR)) {
92         }
93 }
94
95 #define APPEND_EVENT(chan, ev_type, userevent, extra) do { \
96         if (append_expected_event(chan, ev_type, userevent, extra, NULL)) { \
97                 return AST_TEST_FAIL; \
98         } \
99         } while (0)
100
101 #define APPEND_EVENT_PEER(chan, ev_type, userevent, extra, peer) do { \
102         if (append_expected_event(chan, ev_type, userevent, extra, peer)) { \
103                 return AST_TEST_FAIL; \
104         } \
105         } while (0)
106
107 #define APPEND_EVENT_SNAPSHOT(snapshot, ev_type, userevent, extra, peer) do { \
108         if (append_expected_event_snapshot(snapshot, ev_type, userevent, extra, peer)) { \
109                 return AST_TEST_FAIL; \
110         } \
111         } while (0)
112
113 #define APPEND_DUMMY_EVENT() do { \
114         if (append_dummy_event()) { \
115                 return AST_TEST_FAIL; \
116         } \
117         } while (0)
118
119 #define BRIDGE_EXIT(channel, bridge) do { \
120         ast_test_validate(test, !ast_bridge_depart(channel)); \
121         BRIDGE_EXIT_EVENT(channel, bridge); \
122         mid_test_sync(); \
123         } while (0)
124
125 #define BRIDGE_EXIT_EVENT(channel, bridge) do { \
126         RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
127         peer_str = test_cel_generate_peer_str(channel, bridge); \
128         ast_test_validate(test, peer_str != NULL); \
129         BRIDGE_EXIT_EVENT_PEER(channel, bridge, ast_str_buffer(peer_str)); \
130         } while (0)
131
132 #define BRIDGE_EXIT_EVENT_PEER(channel, bridge, peer) do { \
133         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
134         extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
135         ast_test_validate(test, extra != NULL); \
136         APPEND_EVENT_PEER(channel, AST_CEL_BRIDGE_EXIT, NULL, extra, peer); \
137         } while (0)
138
139 #define BRIDGE_EXIT_SNAPSHOT(channel, bridge) do { \
140         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
141         RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
142         peer_str = test_cel_generate_peer_str_snapshot(channel, bridge); \
143         ast_test_validate(test, peer_str != NULL); \
144         extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
145         ast_test_validate(test, extra != NULL); \
146         APPEND_EVENT_SNAPSHOT(channel, AST_CEL_BRIDGE_EXIT, NULL, extra, ast_str_buffer(peer_str)); \
147         } while (0)
148
149 #define BRIDGE_ENTER(channel, bridge) do { \
150         ast_test_validate(test, !ast_bridge_impart(bridge, channel, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE)); \
151         do_sleep(); \
152         BRIDGE_ENTER_EVENT(channel, bridge); \
153         mid_test_sync(); \
154         } while (0)
155
156 #define BRIDGE_ENTER_EVENT(channel, bridge) do { \
157         RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
158         peer_str = test_cel_generate_peer_str(channel, bridge); \
159         ast_test_validate(test, peer_str != NULL); \
160         BRIDGE_ENTER_EVENT_PEER(channel, bridge, ast_str_buffer(peer_str)); \
161         } while (0)
162
163 #define BRIDGE_ENTER_EVENT_PEER(channel, bridge, peer) do { \
164         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
165         extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
166         ast_test_validate(test, extra != NULL); \
167         APPEND_EVENT_PEER(channel, AST_CEL_BRIDGE_ENTER, NULL, extra, peer); \
168         } while (0)
169
170 #define BLINDTRANSFER_EVENT(channel, bridge, extension, context) do { \
171         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
172         extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}", \
173                 "extension", extension, \
174                 "context", context, \
175                 "bridge_id", bridge->uniqueid, \
176                 "transferee_channel_name", "N/A", \
177                 "transferee_channel_uniqueid", "N/A"); \
178         ast_test_validate(test, extra != NULL); \
179         APPEND_EVENT(channel, AST_CEL_BLINDTRANSFER, NULL, extra); \
180         } while (0)
181
182 #define ATTENDEDTRANSFER_BRIDGE(channel1, bridge1, channel2, bridge2, channel3, channel4) do { \
183         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
184         extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}", \
185                 "bridge1_id", bridge1->uniqueid, \
186                 "channel2_name", ast_channel_name(channel2), \
187                 "channel2_uniqueid", ast_channel_uniqueid(channel2), \
188                 "bridge2_id", bridge2->uniqueid, \
189                 "transferee_channel_name", ast_channel_name(channel4), \
190                 "transferee_channel_uniqueid", ast_channel_uniqueid(channel4), \
191                 "transfer_target_channel_name", ast_channel_name(channel3), \
192                 "transfer_target_channel_uniqueid", ast_channel_uniqueid(channel3)); \
193         ast_test_validate(test, extra != NULL); \
194         APPEND_EVENT(channel1, AST_CEL_ATTENDEDTRANSFER, NULL, extra); \
195         } while (0)
196
197 /*! \brief Alice's Caller ID */
198 #define ALICE_CALLERID { .id.name.str = "Alice", .id.name.valid = 1, .id.number.str = "100", .id.number.valid = 1, }
199
200 /*! \brief Bob's Caller ID */
201 #define BOB_CALLERID { .id.name.str = "Bob", .id.name.valid = 1, .id.number.str = "200", .id.number.valid = 1, }
202
203 /*! \brief Charlie's Caller ID */
204 #define CHARLIE_CALLERID { .id.name.str = "Charlie", .id.name.valid = 1, .id.number.str = "300", .id.number.valid = 1, }
205
206 /*! \brief David's Caller ID */
207 #define DAVID_CALLERID { .id.name.str = "David", .id.name.valid = 1, .id.number.str = "400", .id.number.valid = 1, }
208
209 /*! \brief Set ulaw format on channel */
210 #define SET_FORMATS(chan) do {\
211         struct ast_format_cap *caps;\
212         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);\
213         ast_format_cap_append(caps, ast_format_ulaw, 0);\
214         ast_channel_nativeformats_set((chan), caps);\
215         ast_channel_set_writeformat((chan), ast_format_ulaw);\
216         ast_channel_set_rawwriteformat((chan), ast_format_ulaw);\
217         ast_channel_set_readformat((chan), ast_format_ulaw);\
218         ast_channel_set_rawreadformat((chan), ast_format_ulaw);\
219         ao2_ref(caps, -1);\
220 } while (0)
221
222 /*! \brief Create a \ref test_cel_chan_tech for Alice. */
223 #define CREATE_ALICE_CHANNEL(channel_var, caller_id) do { \
224         (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "100", "100", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Alice"); \
225         SET_FORMATS((channel_var));\
226         APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
227         ast_channel_unlock((channel_var)); \
228         } while (0)
229
230 /*! \brief Create a \ref test_cel_chan_tech for Bob. */
231 #define CREATE_BOB_CHANNEL(channel_var, caller_id) do { \
232         (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "200", "200", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Bob"); \
233         SET_FORMATS((channel_var));\
234         APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
235         ast_channel_unlock((channel_var)); \
236         } while (0)
237
238 /*! \brief Create a \ref test_cel_chan_tech for Charlie. */
239 #define CREATE_CHARLIE_CHANNEL(channel_var, caller_id) do { \
240         (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "300", "300", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Charlie"); \
241         SET_FORMATS((channel_var));\
242         APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
243         ast_channel_unlock((channel_var)); \
244         } while (0)
245
246 /*! \brief Create a \ref test_cel_chan_tech for David. */
247 #define CREATE_DAVID_CHANNEL(channel_var, caller_id) do { \
248         (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "400", "400", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/David"); \
249         SET_FORMATS((channel_var));\
250         APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
251         ast_channel_unlock((channel_var)); \
252         } while (0)
253
254 /*! \brief Emulate a channel entering into an application */
255 #define EMULATE_APP_DATA(channel, priority, application, data) do { \
256         if ((priority) > 0) { \
257                 ast_channel_priority_set((channel), (priority)); \
258         } \
259         ast_channel_appl_set((channel), (application)); \
260         ast_channel_data_set((channel), (data)); \
261         ast_channel_publish_snapshot((channel)); \
262         } while (0)
263
264 #define ANSWER_CHANNEL(chan) do { \
265         EMULATE_APP_DATA(chan, 1, "Answer", ""); \
266         ANSWER_NO_APP(chan); \
267         } while (0)
268
269 #define ANSWER_NO_APP(chan) do { \
270         ast_setstate(chan, AST_STATE_UP); \
271         APPEND_EVENT(chan, AST_CEL_ANSWER, NULL, NULL); \
272         } while (0)
273
274 /*! \brief Hang up a test channel safely */
275 #define HANGUP_CHANNEL(channel, cause, dialstatus) do { \
276         ast_channel_hangupcause_set((channel), (cause)); \
277         ao2_ref(channel, +1); \
278         ast_hangup((channel)); \
279         HANGUP_EVENT(channel, cause, dialstatus); \
280         APPEND_EVENT(channel, AST_CEL_CHANNEL_END, NULL, NULL); \
281         ao2_cleanup(stasis_cache_get(ast_channel_cache(), \
282                 ast_channel_snapshot_type(), ast_channel_uniqueid(channel))); \
283         ao2_cleanup(channel); \
284         channel = NULL; \
285         } while (0)
286
287 #define HANGUP_EVENT(channel, cause, dialstatus) do { \
288         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
289         extra = ast_json_pack("{s: i, s: s, s: s}", \
290                 "hangupcause", cause, \
291                 "hangupsource", "", \
292                 "dialstatus", dialstatus); \
293         ast_test_validate(test, extra != NULL); \
294         APPEND_EVENT(channel, AST_CEL_HANGUP, NULL, extra); \
295         } while (0)
296
297 static void mid_test_sync(void);
298
299 static int append_expected_event(
300         struct ast_channel *chan,
301         enum ast_cel_event_type type,
302         const char *userdefevname,
303         struct ast_json *extra,
304         const char *peer);
305
306 static int append_expected_event_snapshot(
307         struct ast_channel_snapshot *snapshot,
308         enum ast_cel_event_type type,
309         const char *userdefevname,
310         struct ast_json *extra,
311         const char *peer);
312
313 static int append_dummy_event(void);
314
315 static struct ast_str *__test_cel_generate_peer_str(struct ast_channel_snapshot *chan, struct ast_bridge_snapshot *bridge)
316 {
317         struct ast_str *peer_str = ast_str_create(32);
318         struct ao2_iterator i;
319         char *current_chan = NULL;
320
321         if (!peer_str) {
322                 return NULL;
323         }
324
325         for (i = ao2_iterator_init(bridge->channels, 0);
326                 (current_chan = ao2_iterator_next(&i));
327                 ao2_cleanup(current_chan)) {
328                 RAII_VAR(struct ast_channel_snapshot *, current_snapshot,
329                         NULL,
330                         ao2_cleanup);
331
332                 /* Don't add the channel for which this message is being generated */
333                 if (!strcmp(current_chan, chan->uniqueid)) {
334                         continue;
335                 }
336
337                 current_snapshot = ast_channel_snapshot_get_latest(current_chan);
338                 if (!current_snapshot) {
339                         continue;
340                 }
341
342                 ast_str_append(&peer_str, 0, "%s,", current_snapshot->name);
343         }
344         ao2_iterator_destroy(&i);
345
346         /* Rip off the trailing comma */
347         ast_str_truncate(peer_str, -1);
348
349         return peer_str;
350 }
351
352 static struct ast_str *test_cel_generate_peer_str_snapshot(struct ast_channel_snapshot *chan, struct ast_bridge *bridge)
353 {
354         RAII_VAR(struct ast_bridge_snapshot *, snapshot,
355                 ast_bridge_snapshot_get_latest(bridge->uniqueid),
356                 ao2_cleanup);
357
358         if (!snapshot) {
359                 return NULL;
360         }
361
362         return __test_cel_generate_peer_str(chan, snapshot);
363 }
364
365 static struct ast_str *test_cel_generate_peer_str(struct ast_channel *chan, struct ast_bridge *bridge)
366 {
367         RAII_VAR(struct ast_channel_snapshot *, snapshot,
368                 ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)),
369                 ao2_cleanup);
370
371         if (!snapshot) {
372                 return NULL;
373         }
374
375         return test_cel_generate_peer_str_snapshot(snapshot, bridge);
376 }
377
378 static void safe_channel_release(struct ast_channel *chan)
379 {
380         if (!chan) {
381                 return;
382         }
383         ast_channel_release(chan);
384 }
385
386 static void safe_bridge_destroy(struct ast_bridge *bridge)
387 {
388         if (!bridge) {
389                 return;
390         }
391         ast_bridge_destroy(bridge, 0);
392 }
393
394 AST_TEST_DEFINE(test_cel_channel_creation)
395 {
396         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
397         struct ast_party_caller caller = ALICE_CALLERID;
398
399         switch (cmd) {
400         case TEST_INIT:
401                 info->name = __func__;
402                 info->category = TEST_CATEGORY;
403                 info->summary = "Test the CEL records created when a channel is created";
404                 info->description =
405                         "Test the CEL records created when a channel is created";
406                 return AST_TEST_NOT_RUN;
407         case TEST_EXECUTE:
408                 break;
409         }
410
411         CREATE_ALICE_CHANNEL(chan, (&caller));
412
413         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
414
415         return AST_TEST_PASS;
416 }
417
418 AST_TEST_DEFINE(test_cel_unanswered_inbound_call)
419 {
420         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
421         struct ast_party_caller caller = ALICE_CALLERID;
422
423         switch (cmd) {
424         case TEST_INIT:
425                 info->name = __func__;
426                 info->category = TEST_CATEGORY;
427                 info->summary = "Test inbound unanswered calls";
428                 info->description =
429                         "Test CEL records for a call that is\n"
430                         "inbound to Asterisk, executes some dialplan, but\n"
431                         "is never answered.\n";
432                 return AST_TEST_NOT_RUN;
433         case TEST_EXECUTE:
434                 break;
435         }
436
437         CREATE_ALICE_CHANNEL(chan, &caller);
438
439         EMULATE_APP_DATA(chan, 1, "Wait", "1");
440
441         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
442
443         return AST_TEST_PASS;
444 }
445
446 AST_TEST_DEFINE(test_cel_unanswered_outbound_call)
447 {
448         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
449         struct ast_party_caller caller = {
450                         .id.name.str = "",
451                         .id.name.valid = 1,
452                         .id.number.str = "",
453                         .id.number.valid = 1, };
454
455         switch (cmd) {
456         case TEST_INIT:
457                 info->name = __func__;
458                 info->category = TEST_CATEGORY;
459                 info->summary = "Test outbound unanswered calls";
460                 info->description =
461                         "Test CEL records for a call that is\n"
462                         "outbound to Asterisk but is never answered.\n";
463                 return AST_TEST_NOT_RUN;
464         case TEST_EXECUTE:
465                 break;
466         }
467
468         CREATE_ALICE_CHANNEL(chan, &caller);
469
470         ast_channel_exten_set(chan, "s");
471         ast_channel_context_set(chan, "default");
472         ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
473         EMULATE_APP_DATA(chan, 0, "AppDial", "(Outgoing Line)");
474         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
475
476         return AST_TEST_PASS;
477 }
478
479 AST_TEST_DEFINE(test_cel_single_party)
480 {
481         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
482         struct ast_party_caller caller = ALICE_CALLERID;
483
484         switch (cmd) {
485         case TEST_INIT:
486                 info->name = __func__;
487                 info->category = TEST_CATEGORY;
488                 info->summary = "Test CEL for a single party";
489                 info->description =
490                         "Test CEL records for a call that is\n"
491                         "answered, but only involves a single channel\n";
492                 return AST_TEST_NOT_RUN;
493         case TEST_EXECUTE:
494                 break;
495         }
496         CREATE_ALICE_CHANNEL(chan, &caller);
497
498         ANSWER_CHANNEL(chan);
499         EMULATE_APP_DATA(chan, 2, "VoiceMailMain", "1");
500
501         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
502
503         return AST_TEST_PASS;
504 }
505
506 AST_TEST_DEFINE(test_cel_single_bridge)
507 {
508         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
509         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
510
511         struct ast_party_caller caller = ALICE_CALLERID;
512
513         switch (cmd) {
514         case TEST_INIT:
515                 info->name = __func__;
516                 info->category = TEST_CATEGORY;
517                 info->summary = "Test CEL for a single party entering/leaving a bridge";
518                 info->description =
519                         "Test CEL records for a call that is\n"
520                         "answered, enters a bridge, and leaves it.\n";
521                 return AST_TEST_NOT_RUN;
522         case TEST_EXECUTE:
523                 break;
524         }
525         bridge = ast_bridge_basic_new();
526         ast_test_validate(test, bridge != NULL);
527
528         CREATE_ALICE_CHANNEL(chan, &caller);
529
530         ANSWER_CHANNEL(chan);
531         EMULATE_APP_DATA(chan, 2, "Bridge", "");
532
533         do_sleep();
534         BRIDGE_ENTER(chan, bridge);
535
536         do_sleep();
537
538         BRIDGE_EXIT(chan, bridge);
539
540         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
541
542         return AST_TEST_PASS;
543 }
544
545 AST_TEST_DEFINE(test_cel_single_bridge_continue)
546 {
547         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
548         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
549         struct ast_party_caller caller = ALICE_CALLERID;
550
551         switch (cmd) {
552         case TEST_INIT:
553                 info->name = __func__;
554                 info->category = TEST_CATEGORY;
555                 info->summary = "Test CEL for a single party entering/leaving a bridge";
556                 info->description =
557                         "Test CEL records for a call that is\n"
558                         "answered, enters a bridge, and leaves it.\n";
559                 return AST_TEST_NOT_RUN;
560         case TEST_EXECUTE:
561                 break;
562         }
563         bridge = ast_bridge_basic_new();
564         ast_test_validate(test, bridge != NULL);
565
566         CREATE_ALICE_CHANNEL(chan, &caller);
567
568         ANSWER_CHANNEL(chan);
569         EMULATE_APP_DATA(chan, 2, "Bridge", "");
570
571         do_sleep();
572         BRIDGE_ENTER(chan, bridge);
573
574         do_sleep();
575
576         BRIDGE_EXIT(chan, bridge);
577
578         EMULATE_APP_DATA(chan, 3, "Wait", "");
579
580         /* And then it hangs up */
581         HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
582
583         return AST_TEST_PASS;
584 }
585
586 AST_TEST_DEFINE(test_cel_single_twoparty_bridge_a)
587 {
588         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
589         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
590         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
591         struct ast_party_caller caller_alice = ALICE_CALLERID;
592         struct ast_party_caller caller_bob = BOB_CALLERID;
593
594         switch (cmd) {
595         case TEST_INIT:
596                 info->name = __func__;
597                 info->category = TEST_CATEGORY;
598                 info->summary = "Test CEL for a single party entering/leaving a bridge";
599                 info->description =
600                         "Test CEL records for a call that is\n"
601                         "answered, enters a bridge, and leaves it. In this scenario, the\n"
602                         "Party A should answer the bridge first.\n";
603                 return AST_TEST_NOT_RUN;
604         case TEST_EXECUTE:
605                 break;
606         }
607         bridge = ast_bridge_basic_new();
608         ast_test_validate(test, bridge != NULL);
609
610         CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
611
612         CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
613
614         ANSWER_CHANNEL(chan_alice);
615         EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
616
617         BRIDGE_ENTER(chan_alice, bridge);
618         do_sleep();
619
620         ANSWER_CHANNEL(chan_bob);
621         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
622
623         BRIDGE_ENTER(chan_bob, bridge);
624
625         BRIDGE_EXIT(chan_alice, bridge);
626         BRIDGE_EXIT(chan_bob, bridge);
627
628         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
629         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
630
631         return AST_TEST_PASS;
632 }
633
634 AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
635 {
636         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
637         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
638         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
639         struct ast_party_caller caller_alice = ALICE_CALLERID;
640         struct ast_party_caller caller_bob = BOB_CALLERID;
641
642         switch (cmd) {
643         case TEST_INIT:
644                 info->name = __func__;
645                 info->category = TEST_CATEGORY;
646                 info->summary = "Test CEL for a single party entering/leaving a bridge";
647                 info->description =
648                         "Test CEL records for a call that is\n"
649                         "answered, enters a bridge, and leaves it. In this scenario, the\n"
650                         "Party B should answer the bridge first.\n";
651                 return AST_TEST_NOT_RUN;
652         case TEST_EXECUTE:
653                 break;
654         }
655         bridge = ast_bridge_basic_new();
656         ast_test_validate(test, bridge != NULL);
657
658         CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
659
660         CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
661
662         ANSWER_CHANNEL(chan_alice);
663         EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
664
665         ANSWER_CHANNEL(chan_bob);
666         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
667         do_sleep();
668
669         BRIDGE_ENTER(chan_bob, bridge);
670
671         BRIDGE_ENTER(chan_alice, bridge);
672
673         BRIDGE_EXIT(chan_alice, bridge);
674         BRIDGE_EXIT(chan_bob, bridge);
675
676         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
677         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
678
679         return AST_TEST_PASS;
680 }
681
682 /* XXX Validation needs to be reworked on a per-channel basis before
683  * test_cel_single_multiparty_bridge and test_cel_dial_answer_multiparty
684  * can operate properly. */
685 #ifdef RACEY_TESTS
686 AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
687 {
688         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
689         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
690         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
691         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
692         struct ast_party_caller caller_alice = ALICE_CALLERID;
693         struct ast_party_caller caller_bob = BOB_CALLERID;
694         struct ast_party_caller caller_charlie = CHARLIE_CALLERID;
695
696         switch (cmd) {
697         case TEST_INIT:
698                 info->name = __func__;
699                 info->category = TEST_CATEGORY;
700                 info->summary = "Test CEL for a single party entering/leaving a multi-party bridge";
701                 info->description =
702                         "Test CEL records for a call that is\n"
703                         "answered, enters a bridge, and leaves it. A total of three\n"
704                         "parties perform this action.\n";
705                 return AST_TEST_NOT_RUN;
706         case TEST_EXECUTE:
707                 break;
708         }
709         bridge = ast_bridge_basic_new();
710         ast_test_validate(test, bridge != NULL);
711
712         CREATE_ALICE_CHANNEL(chan_alice, &caller_alice);
713         CREATE_BOB_CHANNEL(chan_bob, &caller_bob);
714         CREATE_CHARLIE_CHANNEL(chan_charlie, &caller_charlie);
715
716         ANSWER_CHANNEL(chan_alice);
717         EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
718
719         do_sleep();
720
721         BRIDGE_ENTER(chan_alice, bridge);
722
723         ANSWER_CHANNEL(chan_bob);
724         EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
725         do_sleep();
726
727         BRIDGE_ENTER(chan_bob, bridge);
728
729         ANSWER_CHANNEL(chan_charlie);
730         EMULATE_APP_DATA(chan_charlie, 2, "Bridge", "");
731         do_sleep();
732         BRIDGE_ENTER(chan_charlie, bridge);
733
734         BRIDGE_EXIT(chan_alice, bridge);
735         BRIDGE_EXIT(chan_bob, bridge);
736         BRIDGE_EXIT(chan_charlie, bridge);
737
738         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
739         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
740         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
741
742         return AST_TEST_PASS;
743 }
744 #endif
745
746 #define EMULATE_DIAL(channel, dialstring) do { \
747         EMULATE_APP_DATA(channel, 1, "Dial", dialstring); \
748         if (append_expected_event(channel, AST_CEL_APP_START, NULL, NULL, NULL)) { \
749                 return AST_TEST_FAIL; \
750         } \
751         } while (0)
752
753 #define START_DIALED(caller, callee) \
754         START_DIALED_FULL(caller, callee, "200", "Bob")
755
756 #define START_DIALED_FULL(caller, callee, number, name) do { \
757         callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, number, NULL, NULL, NULL, caller, 0, CHANNEL_TECH_NAME "/" name); \
758         SET_FORMATS(callee);\
759         ast_channel_unlock(callee); \
760         if (append_expected_event(callee, AST_CEL_CHANNEL_START, NULL, NULL, NULL)) { \
761                 return AST_TEST_FAIL; \
762         } \
763         ast_set_flag(ast_channel_flags(callee), AST_FLAG_OUTGOING); \
764         EMULATE_APP_DATA(callee, 0, "AppDial", "(Outgoing Line)"); \
765         ast_channel_publish_dial(caller, callee, name, NULL); \
766         } while (0)
767
768 AST_TEST_DEFINE(test_cel_dial_unanswered)
769 {
770         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
771         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
772         struct ast_party_caller caller = ALICE_CALLERID;
773
774         switch (cmd) {
775         case TEST_INIT:
776                 info->name = __func__;
777                 info->category = TEST_CATEGORY;
778                 info->summary = "Test CEL for a dial that isn't answered";
779                 info->description =
780                         "Test CEL records for a channel that\n"
781                         "performs a dial operation that isn't answered\n";
782                 return AST_TEST_NOT_RUN;
783         case TEST_EXECUTE:
784                 break;
785         }
786
787         CREATE_ALICE_CHANNEL(chan_caller, &caller);
788
789         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
790
791         START_DIALED(chan_caller, chan_callee);
792
793         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
794         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
795
796         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER, "NOANSWER");
797         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER, "");
798
799         return AST_TEST_PASS;
800 }
801
802 AST_TEST_DEFINE(test_cel_dial_unanswered_filter)
803 {
804         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
805         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
806         struct ast_party_caller caller = ALICE_CALLERID;
807
808         switch (cmd) {
809         case TEST_INIT:
810                 info->name = __func__;
811                 info->category = TEST_CATEGORY;
812                 info->summary = "Test CEL for a dial that isn't answered";
813                 info->description =
814                         "Test CEL records for a channel that\n"
815                         "performs a dial operation that isn't answered\n";
816                 return AST_TEST_NOT_RUN;
817         case TEST_EXECUTE:
818                 break;
819         }
820
821         CREATE_ALICE_CHANNEL(chan_caller, &caller);
822
823         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
824
825         START_DIALED(chan_caller, chan_callee);
826
827         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
828         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOT A VALID DIAL STATUS");
829         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
830
831         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER, "NOANSWER");
832         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER, "");
833
834         return AST_TEST_PASS;
835 }
836
837 AST_TEST_DEFINE(test_cel_dial_busy)
838 {
839         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
840         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
841         struct ast_party_caller caller = ALICE_CALLERID;
842
843         switch (cmd) {
844         case TEST_INIT:
845                 info->name = __func__;
846                 info->category = TEST_CATEGORY;
847                 info->summary = "Test CEL for a dial that results in a busy";
848                 info->description =
849                         "Test CEL records for a channel that\n"
850                         "performs a dial operation to an endpoint that's busy\n";
851                 return AST_TEST_NOT_RUN;
852         case TEST_EXECUTE:
853                 break;
854         }
855
856         CREATE_ALICE_CHANNEL(chan_caller, &caller);
857
858         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
859
860         START_DIALED(chan_caller, chan_callee);
861
862         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
863         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "BUSY");
864
865         HANGUP_CHANNEL(chan_caller, AST_CAUSE_BUSY, "BUSY");
866         HANGUP_CHANNEL(chan_callee, AST_CAUSE_BUSY, "");
867
868         return AST_TEST_PASS;
869 }
870
871 AST_TEST_DEFINE(test_cel_dial_congestion)
872 {
873         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
874         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
875         struct ast_party_caller caller = ALICE_CALLERID;
876
877         switch (cmd) {
878         case TEST_INIT:
879                 info->name = __func__;
880                 info->category = TEST_CATEGORY;
881                 info->summary = "Test CEL for a dial that results in congestion";
882                 info->description =
883                         "Test CEL records for a channel that\n"
884                         "performs a dial operation to an endpoint that's congested\n";
885                 return AST_TEST_NOT_RUN;
886         case TEST_EXECUTE:
887                 break;
888         }
889
890         CREATE_ALICE_CHANNEL(chan_caller, &caller);
891
892         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
893
894         START_DIALED(chan_caller, chan_callee);
895
896         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
897         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CONGESTION");
898
899         HANGUP_CHANNEL(chan_caller, AST_CAUSE_CONGESTION, "CONGESTION");
900         HANGUP_CHANNEL(chan_callee, AST_CAUSE_CONGESTION, "");
901
902         return AST_TEST_PASS;
903 }
904
905 AST_TEST_DEFINE(test_cel_dial_unavailable)
906 {
907         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
908         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
909         struct ast_party_caller caller = ALICE_CALLERID;
910
911         switch (cmd) {
912         case TEST_INIT:
913                 info->name = __func__;
914                 info->category = TEST_CATEGORY;
915                 info->summary = "Test CEL for a dial that results in unavailable";
916                 info->description =
917                         "Test CEL records for a channel that\n"
918                         "performs a dial operation to an endpoint that's unavailable\n";
919                 return AST_TEST_NOT_RUN;
920         case TEST_EXECUTE:
921                 break;
922         }
923
924         CREATE_ALICE_CHANNEL(chan_caller, &caller);
925
926         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
927
928         START_DIALED(chan_caller, chan_callee);
929
930         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
931         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CHANUNAVAIL");
932
933         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ROUTE_DESTINATION, "CHANUNAVAIL");
934         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ROUTE_DESTINATION, "");
935
936         return AST_TEST_PASS;
937 }
938
939 AST_TEST_DEFINE(test_cel_dial_caller_cancel)
940 {
941         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
942         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
943         struct ast_party_caller caller = ALICE_CALLERID;
944
945         switch (cmd) {
946         case TEST_INIT:
947                 info->name = __func__;
948                 info->category = TEST_CATEGORY;
949                 info->summary = "Test CEL for a dial where the caller cancels";
950                 info->description =
951                         "Test CEL records for a channel that\n"
952                         "performs a dial operation to an endpoint but then decides\n"
953                         "to hang up, cancelling the dial\n";
954                 return AST_TEST_NOT_RUN;
955         case TEST_EXECUTE:
956                 break;
957         }
958
959         CREATE_ALICE_CHANNEL(chan_caller, &caller);
960
961         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
962
963         START_DIALED(chan_caller, chan_callee);
964
965         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
966         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "CANCEL");
967
968         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
969         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "CANCEL");
970
971         return AST_TEST_PASS;
972 }
973
974 AST_TEST_DEFINE(test_cel_dial_parallel_failed)
975 {
976         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
977         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
978         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
979         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
980         struct ast_party_caller caller = ALICE_CALLERID;
981
982         switch (cmd) {
983         case TEST_INIT:
984                 info->name = __func__;
985                 info->category = TEST_CATEGORY;
986                 info->summary = "Test a parallel dial where all channels fail to answer";
987                 info->description =
988                         "This tests dialing three parties: Bob, Charlie, David. Charlie\n"
989                         "returns BUSY; David returns CONGESTION; Bob fails to answer and\n"
990                         "Alice hangs up. Three records are created for Alice as a result.\n";
991                 return AST_TEST_NOT_RUN;
992         case TEST_EXECUTE:
993                 break;
994         }
995
996         CREATE_ALICE_CHANNEL(chan_caller, &caller);
997
998         /* Channel enters Dial app */
999         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob&" CHANNEL_TECH_NAME "/Charlie&" CHANNEL_TECH_NAME "/David");
1000
1001         /* Outbound channels are created */
1002         START_DIALED_FULL(chan_caller, chan_bob, "200", "Bob");
1003         START_DIALED_FULL(chan_caller, chan_charlie, "300", "Charlie");
1004         START_DIALED_FULL(chan_caller, chan_david, "400", "David");
1005
1006         /* Dial starts */
1007         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1008
1009         /* Charlie is busy */
1010         ast_channel_publish_dial(chan_caller, chan_charlie, NULL, "BUSY");
1011         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_BUSY, "");
1012
1013         /* David is congested */
1014         ast_channel_publish_dial(chan_caller, chan_david, NULL, "CONGESTION");
1015         HANGUP_CHANNEL(chan_david, AST_CAUSE_CONGESTION, "");
1016
1017         /* Bob is canceled */
1018         ast_channel_publish_dial(chan_caller, chan_bob, NULL, "CANCEL");
1019         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1020
1021         /* Alice hangs up */
1022         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "BUSY");
1023
1024         return AST_TEST_PASS;
1025 }
1026
1027 AST_TEST_DEFINE(test_cel_dial_answer_no_bridge)
1028 {
1029         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1030         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1031         struct ast_party_caller caller = ALICE_CALLERID;
1032
1033         switch (cmd) {
1034         case TEST_INIT:
1035                 info->name = __func__;
1036                 info->category = TEST_CATEGORY;
1037                 info->summary = "Test dialing, answering, and not going into a bridge.";
1038                 info->description =
1039                         "This is a weird one, but theoretically possible. You can perform\n"
1040                         "a dial, then bounce both channels to different priorities and\n"
1041                         "never have them enter a bridge together. Ew. This makes sure that\n"
1042                         "when we answer, we get a CEL, it gets ended at that point, and\n"
1043                         "that it gets finalized appropriately.\n";
1044                 return AST_TEST_NOT_RUN;
1045         case TEST_EXECUTE:
1046                 break;
1047         }
1048
1049         CREATE_ALICE_CHANNEL(chan_caller, &caller);
1050
1051         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
1052
1053         START_DIALED(chan_caller, chan_callee);
1054
1055         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1056         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1057
1058         ANSWER_NO_APP(chan_caller);
1059         ast_clear_flag(ast_channel_flags(chan_callee), AST_FLAG_OUTGOING);
1060         ANSWER_NO_APP(chan_callee);
1061
1062         EMULATE_APP_DATA(chan_caller, 2, "Wait", "1");
1063         EMULATE_APP_DATA(chan_callee, 1, "Wait", "1");
1064
1065         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1066         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1067
1068         return AST_TEST_PASS;
1069 }
1070
1071 AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_a)
1072 {
1073         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1074         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1075         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
1076         struct ast_party_caller caller = ALICE_CALLERID;
1077
1078         switch (cmd) {
1079         case TEST_INIT:
1080                 info->name = __func__;
1081                 info->category = TEST_CATEGORY;
1082                 info->summary = "Test dialing, answering, and going into a 2-party bridge";
1083                 info->description =
1084                         "The most 'basic' of scenarios\n";
1085                 return AST_TEST_NOT_RUN;
1086         case TEST_EXECUTE:
1087                 break;
1088         }
1089         bridge = ast_bridge_basic_new();
1090         ast_test_validate(test, bridge != NULL);
1091
1092         CREATE_ALICE_CHANNEL(chan_caller, &caller);
1093
1094         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
1095
1096         START_DIALED(chan_caller, chan_callee);
1097
1098         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1099         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1100
1101         ANSWER_NO_APP(chan_caller);
1102         ANSWER_NO_APP(chan_callee);
1103
1104         do_sleep();
1105
1106         BRIDGE_ENTER(chan_caller, bridge);
1107         BRIDGE_ENTER(chan_callee, bridge);
1108
1109         BRIDGE_EXIT(chan_caller, bridge);
1110         BRIDGE_EXIT(chan_callee, bridge);
1111
1112         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1113         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1114
1115         return AST_TEST_PASS;
1116 }
1117
1118 AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
1119 {
1120         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1121         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1122         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
1123         struct ast_party_caller caller = ALICE_CALLERID;
1124
1125         switch (cmd) {
1126         case TEST_INIT:
1127                 info->name = __func__;
1128                 info->category = TEST_CATEGORY;
1129                 info->summary = "Test dialing, answering, and going into a 2-party bridge";
1130                 info->description =
1131                         "The most 'basic' of scenarios\n";
1132                 return AST_TEST_NOT_RUN;
1133         case TEST_EXECUTE:
1134                 break;
1135         }
1136         bridge = ast_bridge_basic_new();
1137         ast_test_validate(test, bridge != NULL);
1138
1139         CREATE_ALICE_CHANNEL(chan_caller, &caller);
1140
1141         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
1142
1143         START_DIALED(chan_caller, chan_callee);
1144
1145         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1146         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1147
1148         ANSWER_NO_APP(chan_caller);
1149         ANSWER_NO_APP(chan_callee);
1150
1151         do_sleep();
1152         BRIDGE_ENTER(chan_callee, bridge);
1153         BRIDGE_ENTER(chan_caller, bridge);
1154
1155         BRIDGE_EXIT(chan_caller, bridge);
1156         BRIDGE_EXIT(chan_callee, bridge);
1157
1158         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1159         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1160
1161         return AST_TEST_PASS;
1162 }
1163
1164 #ifdef RACEY_TESTS
1165 AST_TEST_DEFINE(test_cel_dial_answer_multiparty)
1166 {
1167         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1168         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1169         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1170         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1171         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
1172         struct ast_party_caller alice_caller = ALICE_CALLERID;
1173         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1174
1175         switch (cmd) {
1176         case TEST_INIT:
1177                 info->name = __func__;
1178                 info->category = TEST_CATEGORY;
1179                 info->summary = "Test dialing, answering, and going into a multi-party bridge";
1180                 info->description =
1181                         "A little tricky to get to do, but possible with some redirects.\n";
1182                 return AST_TEST_NOT_RUN;
1183         case TEST_EXECUTE:
1184                 break;
1185         }
1186         bridge = ast_bridge_basic_new();
1187         ast_test_validate(test, bridge != NULL);
1188
1189         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1190
1191         EMULATE_DIAL(chan_alice, CHANNEL_TECH_NAME "/Bob");
1192
1193         START_DIALED(chan_alice, chan_bob);
1194         do_sleep();
1195
1196         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1197         do_sleep();
1198         EMULATE_DIAL(chan_charlie, CHANNEL_TECH_NAME "/Bob");
1199         do_sleep();
1200
1201         START_DIALED_FULL(chan_charlie, chan_david, "400", "David");
1202
1203         ast_channel_state_set(chan_alice, AST_STATE_RINGING);
1204         do_sleep();
1205         ast_channel_state_set(chan_charlie, AST_STATE_RINGING);
1206         do_sleep();
1207         ast_channel_publish_dial(chan_alice, chan_bob, NULL, "ANSWER");
1208         do_sleep();
1209         ast_channel_publish_dial(chan_charlie, chan_david, NULL, "ANSWER");
1210         do_sleep();
1211
1212         ANSWER_NO_APP(chan_alice);
1213         do_sleep();
1214         ANSWER_NO_APP(chan_bob);
1215         do_sleep();
1216         ANSWER_NO_APP(chan_charlie);
1217         do_sleep();
1218         ANSWER_NO_APP(chan_david);
1219         do_sleep();
1220
1221         do_sleep();
1222         BRIDGE_ENTER(chan_charlie, bridge);
1223         BRIDGE_ENTER(chan_david, bridge);
1224         BRIDGE_ENTER(chan_bob, bridge);
1225         BRIDGE_ENTER(chan_alice, bridge);
1226
1227         BRIDGE_EXIT(chan_alice, bridge);
1228         BRIDGE_EXIT(chan_bob, bridge);
1229         BRIDGE_EXIT(chan_charlie, bridge);
1230         BRIDGE_EXIT(chan_david, bridge);
1231
1232         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "ANSWER");
1233         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1234         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "ANSWER");
1235         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1236
1237         return AST_TEST_PASS;
1238 }
1239 #endif
1240
1241 AST_TEST_DEFINE(test_cel_blind_transfer)
1242 {
1243         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1244         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1245         RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
1246         RAII_VAR(struct ast_blind_transfer_message *, transfer_msg, NULL, ao2_cleanup);
1247         struct ast_party_caller alice_caller = ALICE_CALLERID;
1248         struct ast_party_caller bob_caller = BOB_CALLERID;
1249
1250         switch (cmd) {
1251         case TEST_INIT:
1252                 info->name = __func__;
1253                 info->category = TEST_CATEGORY;
1254                 info->summary = "Test blind transfers to an extension";
1255                 info->description =
1256                         "This test creates two channels, bridges them, and then"
1257                         " blind transfers the bridge to an extension.\n";
1258                 return AST_TEST_NOT_RUN;
1259         case TEST_EXECUTE:
1260                 break;
1261         }
1262         bridge = ast_bridge_basic_new();
1263         ast_test_validate(test, bridge != NULL);
1264
1265         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1266         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1267
1268         ANSWER_NO_APP(chan_alice);
1269         ANSWER_NO_APP(chan_bob);
1270
1271         BRIDGE_ENTER(chan_bob, bridge);
1272         BRIDGE_ENTER(chan_alice, bridge);
1273
1274         ast_bridge_lock(bridge);
1275         transfer_msg = ast_blind_transfer_message_create(1, chan_alice,
1276                         "transfer_extension", "transfer_context");
1277         if (!transfer_msg) {
1278                 ast_bridge_unlock(bridge);
1279                 ast_test_status_update(test, "Failed to create transfer Stasis message\n");
1280                 return AST_TEST_FAIL;
1281         }
1282         transfer_msg->bridge = ast_bridge_snapshot_create(bridge);
1283         if (!transfer_msg->bridge) {
1284                 ast_bridge_unlock(bridge);
1285                 ast_test_status_update(test, "Failed to create bridge snapshot\n");
1286                 return AST_TEST_FAIL;
1287         }
1288         ast_bridge_unlock(bridge);
1289         transfer_msg->result = AST_BRIDGE_TRANSFER_SUCCESS;
1290         ast_bridge_publish_blind_transfer(transfer_msg);
1291         BLINDTRANSFER_EVENT(chan_alice, bridge, "transfer_extension", "transfer_context");
1292
1293         BRIDGE_EXIT(chan_alice, bridge);
1294         BRIDGE_EXIT(chan_bob, bridge);
1295
1296         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1297         do_sleep();
1298         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1299
1300         return AST_TEST_PASS;
1301 }
1302
1303 /* XXX Validation needs to take into account the BRIDGE_EXIT for Alice and the
1304  * ATTENDEDTRANSFER message are not guaranteed to be ordered
1305  */
1306 #ifdef RACEY_TESTS
1307 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
1308 {
1309         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1310         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1311         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1312         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1313         RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
1314         RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
1315         struct ast_party_caller alice_caller = ALICE_CALLERID;
1316         struct ast_party_caller bob_caller = BOB_CALLERID;
1317         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1318         struct ast_party_caller david_caller = ALICE_CALLERID;
1319
1320         switch (cmd) {
1321         case TEST_INIT:
1322                 info->name = __func__;
1323                 info->category = TEST_CATEGORY;
1324                 info->summary = "Test attended transfers between two pairs of bridged parties";
1325                 info->description =
1326                         "This test creates four channels, places each pair in"
1327                         " a bridge, and then attended transfers the bridges"
1328                         " together.\n";
1329                 return AST_TEST_NOT_RUN;
1330         case TEST_EXECUTE:
1331                 break;
1332         }
1333         /* Create first set of bridged parties */
1334         bridge1 = ast_bridge_basic_new();
1335         ast_test_validate(test, bridge1 != NULL);
1336
1337         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1338         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1339         ANSWER_NO_APP(chan_alice);
1340         ANSWER_NO_APP(chan_bob);
1341
1342         BRIDGE_ENTER(chan_bob, bridge1);
1343         BRIDGE_ENTER(chan_alice, bridge1);
1344
1345         /* Create second set of bridged parties */
1346         bridge2 = ast_bridge_basic_new();
1347         ast_test_validate(test, bridge2 != NULL);
1348
1349         CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1350         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1351         ANSWER_NO_APP(chan_david);
1352         ANSWER_NO_APP(chan_charlie);
1353
1354         BRIDGE_ENTER(chan_charlie, bridge2);
1355
1356         BRIDGE_ENTER(chan_david, bridge2);
1357         BRIDGE_EXIT_EVENT(chan_bob, bridge1);
1358         do_sleep();
1359
1360         /* Perform attended transfer */
1361         if (ast_bridge_transfer_attended(chan_alice, chan_david)) {
1362                 ast_test_status_update(test, "Attended transfer failed!\n");
1363                 return AST_TEST_FAIL;
1364         }
1365         do_sleep();
1366         BRIDGE_ENTER_EVENT_PEER(chan_bob, bridge2, "CELTestChannel/David,CELTestChannel/Charlie");
1367
1368         BRIDGE_EXIT_EVENT(chan_david, bridge2);
1369         BRIDGE_EXIT_EVENT(chan_alice, bridge1);
1370         ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
1371
1372         do_sleep();
1373         BRIDGE_EXIT(chan_bob, bridge2);
1374         BRIDGE_EXIT(chan_charlie, bridge2);
1375
1376         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1377         do_sleep();
1378         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1379         do_sleep();
1380         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1381         do_sleep();
1382         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1383
1384         return AST_TEST_PASS;
1385 }
1386 #endif
1387
1388 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
1389 {
1390         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1391         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1392         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1393         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1394         RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
1395         RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
1396         struct ast_party_caller alice_caller = ALICE_CALLERID;
1397         struct ast_party_caller bob_caller = BOB_CALLERID;
1398         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1399         struct ast_party_caller david_caller = ALICE_CALLERID;
1400
1401         switch (cmd) {
1402         case TEST_INIT:
1403                 info->name = __func__;
1404                 info->category = TEST_CATEGORY;
1405                 info->summary = "Test attended transfers between two pairs of"
1406                         " bridged parties that results in a bridge merge";
1407                 info->description =
1408                         "This test creates four channels, places each pair"
1409                         " in a bridge, and then attended transfers the bridges"
1410                         " together causing a bridge merge.\n";
1411                 return AST_TEST_NOT_RUN;
1412         case TEST_EXECUTE:
1413                 break;
1414         }
1415         /* Create first set of bridged parties */
1416         bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1417                 AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
1418                 "test_cel", "test_cel_atxfer_bridges_merge_1", NULL);
1419         ast_test_validate(test, bridge1 != NULL);
1420
1421         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1422         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1423         ANSWER_NO_APP(chan_alice);
1424         ANSWER_NO_APP(chan_bob);
1425
1426         BRIDGE_ENTER(chan_bob, bridge1);
1427         BRIDGE_ENTER(chan_alice, bridge1);
1428
1429         /* Create second set of bridged parties */
1430         bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1431                 AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
1432                 "test_cel", "test_cel_atxfer_bridges_merge_2", NULL);
1433         ast_test_validate(test, bridge2 != NULL);
1434
1435         CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1436         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1437         ANSWER_NO_APP(chan_david);
1438         ANSWER_NO_APP(chan_charlie);
1439
1440         BRIDGE_ENTER(chan_charlie, bridge2);
1441
1442         BRIDGE_ENTER(chan_david, bridge2);
1443
1444         /* Perform attended transfer */
1445         if (ast_bridge_transfer_attended(chan_alice, chan_david)) {
1446                 ast_test_status_update(test, "Attended transfer failed!\n");
1447                 return AST_TEST_FAIL;
1448         }
1449         do_sleep();
1450         BRIDGE_EXIT_EVENT_PEER(chan_charlie, bridge2, "CELTestChannel/David");
1451         BRIDGE_ENTER_EVENT_PEER(chan_charlie, bridge1, "CELTestChannel/Bob,CELTestChannel/Alice");
1452         BRIDGE_EXIT_EVENT(chan_david, bridge2);
1453         BRIDGE_EXIT_EVENT(chan_alice, bridge1);
1454
1455         ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
1456
1457         do_sleep();
1458         BRIDGE_EXIT(chan_bob, bridge1);
1459         BRIDGE_EXIT(chan_charlie, bridge1);
1460
1461         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1462         do_sleep();
1463         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1464         do_sleep();
1465         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1466         do_sleep();
1467         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1468
1469         return AST_TEST_PASS;
1470 }
1471
1472 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
1473 {
1474         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1475         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1476         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1477         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1478         RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
1479         RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
1480         struct ast_party_caller alice_caller = ALICE_CALLERID;
1481         struct ast_party_caller bob_caller = BOB_CALLERID;
1482         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1483         struct ast_party_caller david_caller = ALICE_CALLERID;
1484
1485         switch (cmd) {
1486         case TEST_INIT:
1487                 info->name = __func__;
1488                 info->category = TEST_CATEGORY;
1489                 info->summary = "Test attended transfers between two pairs of"
1490                         " bridged parties that results in a bridge merge";
1491                 info->description =
1492                         "This test creates four channels, places each pair"
1493                         " in a bridge, and then attended transfers the bridges"
1494                         " together causing a bridge link.\n";
1495                 return AST_TEST_NOT_RUN;
1496         case TEST_EXECUTE:
1497                 break;
1498         }
1499         /* Create first set of bridged parties */
1500         bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1501                 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1502                 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1503                 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
1504                 "test_cel", "test_cel_atxfer_bridges_link_1", NULL);
1505         ast_test_validate(test, bridge1 != NULL);
1506
1507         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1508         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1509         ANSWER_NO_APP(chan_alice);
1510         ANSWER_NO_APP(chan_bob);
1511
1512         BRIDGE_ENTER(chan_bob, bridge1);
1513         BRIDGE_ENTER(chan_alice, bridge1);
1514
1515         /* Create second set of bridged parties */
1516         bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1517                 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1518                 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1519                 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
1520                 "test_cel", "test_cel_atxfer_bridges_link_2", NULL);
1521         ast_test_validate(test, bridge2 != NULL);
1522
1523         CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1524         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1525         ANSWER_NO_APP(chan_david);
1526         ANSWER_NO_APP(chan_charlie);
1527
1528         BRIDGE_ENTER(chan_charlie, bridge2);
1529         BRIDGE_ENTER(chan_david, bridge2);
1530
1531         /* Perform attended transfer */
1532         ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
1533
1534         ast_bridge_transfer_attended(chan_alice, chan_david);
1535         do_sleep();
1536
1537         /* BRIDGE_EXIT alice and david */
1538         APPEND_DUMMY_EVENT();
1539         APPEND_DUMMY_EVENT();
1540
1541         do_sleep();
1542         BRIDGE_EXIT(chan_bob, bridge1);
1543         BRIDGE_EXIT(chan_charlie, bridge2);
1544
1545         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1546         do_sleep();
1547         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1548         do_sleep();
1549         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1550         do_sleep();
1551         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1552         do_sleep();
1553
1554         return AST_TEST_PASS;
1555 }
1556
1557 AST_TEST_DEFINE(test_cel_dial_pickup)
1558 {
1559         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1560         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1561         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1562         struct ast_party_caller caller = ALICE_CALLERID;
1563         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1564
1565         switch (cmd) {
1566         case TEST_INIT:
1567                 info->name = __func__;
1568                 info->category = TEST_CATEGORY;
1569                 info->summary = "Test call pickup";
1570                 info->description =
1571                         "Test CEL records for a call that is\n"
1572                         "inbound to Asterisk, executes some dialplan, and\n"
1573                         "is picked up.\n";
1574                 return AST_TEST_NOT_RUN;
1575         case TEST_EXECUTE:
1576                 break;
1577         }
1578
1579         CREATE_ALICE_CHANNEL(chan_caller, &caller);
1580
1581         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
1582
1583         START_DIALED(chan_caller, chan_callee);
1584
1585         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1586
1587         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1588
1589         {
1590                 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1591                 SCOPED_CHANNELLOCK(lock, chan_callee);
1592
1593                 extra = ast_json_pack("{s: s, s: s}", "pickup_channel", ast_channel_name(chan_charlie),
1594                         "pickup_channel_uniqueid", ast_channel_uniqueid(chan_charlie));
1595                 ast_test_validate(test, extra != NULL);
1596
1597                 APPEND_EVENT(chan_callee, AST_CEL_PICKUP, NULL, extra);
1598                 ast_test_validate(test, !ast_do_pickup(chan_charlie, chan_callee));
1599         }
1600
1601         /* Hang up the masqueraded zombie */
1602         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1603
1604         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1605
1606         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "CANCEL");
1607         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1608
1609         return AST_TEST_PASS;
1610 }
1611
1612 AST_TEST_DEFINE(test_cel_local_optimize)
1613 {
1614         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1615         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1616         struct ast_party_caller alice_caller = ALICE_CALLERID;
1617         struct ast_party_caller bob_caller = BOB_CALLERID;
1618         RAII_VAR(struct ast_multi_channel_blob *, mc_blob, NULL, ao2_cleanup);
1619         RAII_VAR(struct ast_channel_snapshot *, alice_snapshot, NULL, ao2_cleanup);
1620         RAII_VAR(struct ast_channel_snapshot *, bob_snapshot, NULL, ao2_cleanup);
1621         RAII_VAR(struct stasis_message *, local_opt_begin, NULL, ao2_cleanup);
1622         RAII_VAR(struct stasis_message *, local_opt_end, NULL, ao2_cleanup);
1623         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1624
1625         switch (cmd) {
1626         case TEST_INIT:
1627                 info->name = __func__;
1628                 info->category = TEST_CATEGORY;
1629                 info->summary = "Test local channel optimization record generation";
1630                 info->description =
1631                         "Test CEL records for two local channels being optimized\n"
1632                         "out by sending a messages indicating local optimization\n"
1633                         "begin and end\n";
1634                 return AST_TEST_NOT_RUN;
1635         case TEST_EXECUTE:
1636                 break;
1637         }
1638
1639         mc_blob = ast_multi_channel_blob_create(ast_json_null());
1640         ast_test_validate(test, mc_blob != NULL);
1641
1642         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1643         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1644
1645         ast_channel_lock(chan_alice);
1646         alice_snapshot = ast_channel_snapshot_create(chan_alice);
1647         ast_channel_unlock(chan_alice);
1648         ast_test_validate(test, alice_snapshot != NULL);
1649
1650         ast_channel_lock(chan_bob);
1651         bob_snapshot = ast_channel_snapshot_create(chan_bob);
1652         ast_channel_unlock(chan_bob);
1653         ast_test_validate(test, bob_snapshot != NULL);
1654
1655         ast_multi_channel_blob_add_channel(mc_blob, "1", alice_snapshot);
1656         ast_multi_channel_blob_add_channel(mc_blob, "2", bob_snapshot);
1657
1658         local_opt_begin = stasis_message_create(ast_local_optimization_begin_type(), mc_blob);
1659         ast_test_validate(test, local_opt_begin != NULL);
1660
1661         local_opt_end = stasis_message_create(ast_local_optimization_end_type(), mc_blob);
1662         ast_test_validate(test, local_opt_end != NULL);
1663
1664         stasis_publish(ast_channel_topic(chan_alice), local_opt_begin);
1665         stasis_publish(ast_channel_topic(chan_alice), local_opt_end);
1666
1667         extra = ast_json_pack("{s: s, s: s}", "local_two", bob_snapshot->name,
1668                 "local_two_uniqueid", bob_snapshot->uniqueid);
1669         ast_test_validate(test, extra != NULL);
1670
1671         APPEND_EVENT_SNAPSHOT(alice_snapshot, AST_CEL_LOCAL_OPTIMIZE, NULL, extra, NULL);
1672
1673         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1674         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1675
1676         return AST_TEST_PASS;
1677 }
1678
1679 /*! Container for astobj2 duplicated ast_events */
1680 static struct ao2_container *cel_received_events = NULL;
1681
1682 /*! Container for expected CEL events */
1683 static struct ao2_container *cel_expected_events = NULL;
1684
1685 static struct ast_event *ao2_dup_event(const struct ast_event *event)
1686 {
1687         struct ast_event *event_dup;
1688         uint16_t event_len;
1689
1690         event_len = ast_event_get_size(event);
1691
1692         event_dup = ao2_alloc(event_len, NULL);
1693         if (!event_dup) {
1694                 return NULL;
1695         }
1696
1697         memcpy(event_dup, event, event_len);
1698
1699         return event_dup;
1700 }
1701
1702 static void mid_test_sync(void)
1703 {
1704         ast_mutex_lock(&mid_test_sync_lock);
1705         if (ao2_container_count(cel_expected_events) <= ao2_container_count(cel_received_events)) {
1706                 ast_mutex_unlock(&mid_test_sync_lock);
1707                 return;
1708         }
1709
1710         do_mid_test_sync = 1;
1711         ast_mutex_unlock(&mid_test_sync_lock);
1712
1713         {
1714                 struct timeval start = ast_tvnow();
1715                 struct timespec end = {
1716                         .tv_sec = start.tv_sec + 15,
1717                         .tv_nsec = start.tv_usec * 1000
1718                 };
1719
1720                 SCOPED_MUTEX(lock, &sync_lock);
1721                 ast_cond_timedwait(&sync_out, &sync_lock, &end);
1722         }
1723 }
1724
1725 static int append_event(struct ast_event *ev)
1726 {
1727         RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1728         ao2_ev = ao2_dup_event(ev);
1729         if (!ao2_ev) {
1730                 return -1;
1731         }
1732
1733         ao2_link(cel_expected_events, ao2_ev);
1734         return 0;
1735 }
1736
1737 static int append_dummy_event(void)
1738 {
1739         RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1740         RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1741
1742         ev = ast_event_new(AST_EVENT_CUSTOM, AST_EVENT_IE_END);
1743         if (!ev) {
1744                 return -1;
1745         }
1746
1747         return append_event(ev);
1748 }
1749
1750 static int append_expected_event_snapshot(
1751         struct ast_channel_snapshot *snapshot,
1752         enum ast_cel_event_type type,
1753         const char *userdefevname,
1754         struct ast_json *extra,
1755         const char *peer)
1756 {
1757         RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1758         ev = ast_cel_create_event(snapshot, type, userdefevname, extra, peer);
1759         if (!ev) {
1760                 return -1;
1761         }
1762
1763         return append_event(ev);
1764 }
1765
1766 static int append_expected_event(
1767         struct ast_channel *chan,
1768         enum ast_cel_event_type type,
1769         const char *userdefevname,
1770         struct ast_json *extra,
1771         const char *peer)
1772 {
1773         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1774         ast_channel_lock(chan);
1775         snapshot = ast_channel_snapshot_create(chan);
1776         ast_channel_unlock(chan);
1777         if (!snapshot) {
1778                 return -1;
1779         }
1780
1781         return append_expected_event_snapshot(snapshot, type, userdefevname, extra, peer);
1782 }
1783
1784 static void test_sub(struct ast_event *event)
1785 {
1786         struct ast_event *event_dup = ao2_dup_event(event);
1787         const char *chan_name;
1788         SCOPED_MUTEX(mid_test_lock, &mid_test_sync_lock);
1789
1790         if (!event_dup) {
1791                 return;
1792         }
1793
1794         chan_name = ast_event_get_ie_str(event_dup, AST_EVENT_IE_CEL_CHANNAME);
1795         if (chan_name && strncmp(chan_name, CHANNEL_TECH_NAME, 14)) {
1796                 return;
1797         }
1798
1799         /* save the event for later processing */
1800         ao2_link(cel_received_events, event_dup);
1801
1802         if (do_mid_test_sync) {
1803                 int expected = ao2_container_count(cel_expected_events);
1804                 int received = ao2_container_count(cel_received_events);
1805                 if (expected <= received) {
1806                         {
1807                         SCOPED_MUTEX(lock, &sync_lock);
1808                         ast_cond_signal(&sync_out);
1809                         do_mid_test_sync = 0;
1810                         }
1811                 }
1812         }
1813 }
1814
1815 /*!
1816  * \internal
1817  * \brief Callback function called before each test executes
1818  */
1819 static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
1820 {
1821         ast_assert(cel_received_events == NULL);
1822         ast_assert(cel_expected_events == NULL);
1823
1824         ast_mutex_init(&mid_test_sync_lock);
1825         ast_mutex_init(&sync_lock);
1826         ast_cond_init(&sync_out, NULL);
1827
1828         /* Back up the real CEL config and insert the test's config */
1829         saved_config = ast_cel_get_config();
1830         ast_cel_set_config(cel_test_config);
1831
1832         /* init CEL event storage (degenerate hash table becomes a linked list) */
1833         cel_received_events = ao2_container_alloc(1, NULL, NULL);
1834         cel_expected_events = ao2_container_alloc(1, NULL, NULL);
1835
1836         /* start the CEL event callback */
1837         if (ast_cel_backend_register(TEST_BACKEND_NAME, test_sub)) {
1838                 return -1;
1839         }
1840         return 0;
1841 }
1842
1843 /*!
1844  * \brief Check two peer strings for equality
1845  *
1846  * \retval zero if the peer strings do not match
1847  * \retval non-zero if the peer strings match
1848  */
1849 static int test_cel_peer_strings_match(const char *str1, const char *str2)
1850 {
1851         struct ao2_container *intersection = ast_str_container_alloc(11);
1852         RAII_VAR(char *, str1_dup, ast_strdup(str1), ast_free);
1853         RAII_VAR(char *, str2_dup, ast_strdup(str2), ast_free);
1854         char *chan;
1855
1856         while ((chan = strsep(&str1_dup, ","))) {
1857                 ast_str_container_add(intersection, chan);
1858         }
1859
1860         while ((chan = strsep(&str2_dup, ","))) {
1861                 RAII_VAR(char *, ao2_chan, ao2_find(intersection, chan, OBJ_SEARCH_KEY), ao2_cleanup);
1862
1863                 /* item in str2 not in str1 */
1864                 if (!ao2_chan) {
1865                         return 0;
1866                 }
1867
1868                 ast_str_container_remove(intersection, chan);
1869         }
1870
1871         /* item in str1 not in str2 */
1872         if (ao2_container_count(intersection)) {
1873                 return 0;
1874         }
1875
1876         return 1;
1877 }
1878
1879 /*!
1880  * \brief Check an IE value from two events
1881  *
1882  * \retval zero if the IEs in the events of the specified type do not match
1883  * \retval non-zero if the IEs in the events of the specified type match
1884  */
1885 static int match_ie_val(
1886         const struct ast_event *event1,
1887         const struct ast_event *event2,
1888         enum ast_event_ie_type type)
1889 {
1890         enum ast_event_ie_pltype pltype = ast_event_get_ie_pltype(type);
1891
1892         /* XXX ignore sec/usec for now */
1893         if (type == AST_EVENT_IE_CEL_EVENT_TIME_USEC) {
1894                 return 1;
1895         }
1896
1897         if (type == AST_EVENT_IE_CEL_EVENT_TIME) {
1898                 return 1;
1899         }
1900
1901         switch (pltype) {
1902         case AST_EVENT_IE_PLTYPE_UINT:
1903         {
1904                 uint32_t val = ast_event_get_ie_uint(event2, type);
1905
1906                 return (val == ast_event_get_ie_uint(event1, type)) ? 1 : 0;
1907         }
1908         case AST_EVENT_IE_PLTYPE_STR:
1909         {
1910                 const char *str1 = ast_event_get_ie_str(event1, type);
1911                 const char *str2 = ast_event_get_ie_str(event2, type);
1912
1913                 if (!str1 && !str2) {
1914                         return 1;
1915                 } else if (!str1) {
1916                         return 0;
1917                 } else if (!str2) {
1918                         return 0;
1919                 }
1920
1921                 /* use special matching for CEL PEER field */
1922                 if (type == AST_EVENT_IE_CEL_PEER) {
1923                         return test_cel_peer_strings_match(str1, str2);
1924                 }
1925
1926                 return !strcmp(str1, str2);
1927         }
1928         case AST_EVENT_IE_PLTYPE_RAW:
1929         case AST_EVENT_IE_PLTYPE_BITFLAGS:
1930                 /* Fall through: just pass on these types */
1931                 return 1;
1932         default:
1933                 break;
1934         }
1935         return 0;
1936 }
1937
1938 static int events_are_equal(struct ast_test *test, struct ast_event *received, struct ast_event *expected)
1939 {
1940         struct ast_event_iterator iterator;
1941         int res;
1942
1943         if (ast_event_get_type(expected) == AST_EVENT_CUSTOM) {
1944                 /* this event is flagged as a wildcard match */
1945                 return 1;
1946         }
1947
1948         for (res = ast_event_iterator_init(&iterator, received); !res; res = ast_event_iterator_next(&iterator)) {
1949                 int ie_type = ast_event_iterator_get_ie_type(&iterator);
1950                 if (!match_ie_val(received, expected, ie_type)) {
1951                         ast_test_status_update(test, "Failed matching on field %s\n", ast_event_get_ie_type_name(ie_type));
1952                         return 0;
1953                 }
1954         }
1955
1956         return 1;
1957 }
1958
1959 static int dump_event(struct ast_test *test, struct ast_event *event)
1960 {
1961         struct ast_event_iterator i;
1962
1963         if (ast_event_iterator_init(&i, event)) {
1964                 ast_test_status_update(test, "Failed to initialize event iterator.  :-(\n");
1965                 return 0;
1966         }
1967
1968         ast_test_status_update(test, "Event: %s\n",
1969                 ast_cel_get_type_name(ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TYPE)));
1970
1971         do {
1972                 enum ast_event_ie_type ie_type;
1973                 enum ast_event_ie_pltype ie_pltype;
1974                 const char *ie_type_name;
1975
1976                 ie_type = ast_event_iterator_get_ie_type(&i);
1977                 ie_type_name = ast_event_get_ie_type_name(ie_type);
1978                 ie_pltype = ast_event_get_ie_pltype(ie_type);
1979
1980                 switch (ie_pltype) {
1981                 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1982                 case AST_EVENT_IE_PLTYPE_STR:
1983                         ast_test_status_update(test, "%.30s: %s\n", ie_type_name,
1984                                         ast_event_iterator_get_ie_str(&i));
1985                         break;
1986                 case AST_EVENT_IE_PLTYPE_UINT:
1987                         ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
1988                                         ast_event_iterator_get_ie_uint(&i));
1989                         break;
1990                 default:
1991                         break;
1992                 }
1993         } while (!ast_event_iterator_next(&i));
1994
1995         ast_test_status_update(test, "\n");
1996
1997         return 0;
1998 }
1999
2000 static int check_events(struct ast_test *test, struct ao2_container *local_expected, struct ao2_container *local_received)
2001 {
2002         struct ao2_iterator received_it;
2003         struct ao2_iterator expected_it;
2004         RAII_VAR(struct ast_event *, rx_event, NULL, ao2_cleanup);
2005         RAII_VAR(struct ast_event *, ex_event, NULL, ao2_cleanup);
2006         int debug = 0;
2007
2008         if (ao2_container_count(local_expected) != ao2_container_count(local_received)) {
2009                 ast_test_status_update(test, "Increasing verbosity since the number of expected events (%d)"
2010                         " did not match number of received events (%d).\n",
2011                         ao2_container_count(local_expected),
2012                         ao2_container_count(local_received));
2013                 debug = 1;
2014         }
2015
2016         received_it = ao2_iterator_init(local_received, 0);
2017         expected_it = ao2_iterator_init(local_expected, 0);
2018         rx_event = ao2_iterator_next(&received_it);
2019         ex_event = ao2_iterator_next(&expected_it);
2020         while (rx_event && ex_event) {
2021                 if (!events_are_equal(test, rx_event, ex_event)) {
2022                         ao2_iterator_destroy(&received_it);
2023                         ao2_iterator_destroy(&expected_it);
2024                         ast_test_status_update(test, "Received event:\n");
2025                         dump_event(test, rx_event);
2026                         ast_test_status_update(test, "Expected event:\n");
2027                         dump_event(test, ex_event);
2028                         return -1;
2029                 }
2030                 if (debug) {
2031                         ast_test_status_update(test, "Compared events successfully%s\n",
2032                                 ast_event_get_type(ex_event) == AST_EVENT_CUSTOM
2033                                         ? " (wildcard match)" : "");
2034                         dump_event(test, rx_event);
2035                 }
2036                 ao2_cleanup(rx_event);
2037                 ao2_cleanup(ex_event);
2038                 rx_event = ao2_iterator_next(&received_it);
2039                 ex_event = ao2_iterator_next(&expected_it);
2040         }
2041         ao2_iterator_destroy(&received_it);
2042         ao2_iterator_destroy(&expected_it);
2043
2044         if (rx_event) {
2045                 ast_test_status_update(test, "Received event:\n");
2046                 dump_event(test, rx_event);
2047                 return -1;
2048         }
2049         if (ex_event) {
2050                 ast_test_status_update(test, "Expected event:\n");
2051                 dump_event(test, ex_event);
2052                 return -1;
2053         }
2054         return 0;
2055 }
2056
2057 /*!
2058  * \internal
2059  * \brief Callback function called after each test executes.
2060  *
2061  * \details
2062  * In addition to cleanup, this function also performs verification
2063  * that the events received during a test match the events that were
2064  * expected to have been generated during the test.
2065  */
2066 static int cel_verify_and_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
2067 {
2068         RAII_VAR(struct ao2_container *, local_expected, cel_expected_events, ao2_cleanup);
2069         RAII_VAR(struct ao2_container *, local_received, cel_received_events, ao2_cleanup);
2070         ast_assert(cel_received_events != NULL);
2071         ast_assert(cel_expected_events != NULL);
2072
2073         do_sleep();
2074
2075         /* stop the CEL event callback and clean up storage structures*/
2076         ast_cel_backend_unregister(TEST_BACKEND_NAME);
2077
2078         /* cleaned up by RAII_VAR's */
2079         cel_expected_events = NULL;
2080         cel_received_events = NULL;
2081
2082         /* check events */
2083         ast_test_validate(test, !check_events(test, local_expected, local_received));
2084
2085         /* Restore the real CEL config */
2086         ast_cel_set_config(saved_config);
2087         ao2_cleanup(saved_config);
2088         saved_config = NULL;
2089
2090         /* clean up the locks */
2091         ast_mutex_destroy(&sync_lock);
2092         ast_mutex_destroy(&mid_test_sync_lock);
2093         ast_cond_destroy(&sync_out);
2094         return 0;
2095 }
2096
2097 static int unload_module(void)
2098 {
2099         AST_TEST_UNREGISTER(test_cel_channel_creation);
2100         AST_TEST_UNREGISTER(test_cel_unanswered_inbound_call);
2101         AST_TEST_UNREGISTER(test_cel_unanswered_outbound_call);
2102         AST_TEST_UNREGISTER(test_cel_single_party);
2103         AST_TEST_UNREGISTER(test_cel_single_bridge);
2104         AST_TEST_UNREGISTER(test_cel_single_bridge_continue);
2105         AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_a);
2106         AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_b);
2107 #ifdef RACEY_TESTS
2108         AST_TEST_UNREGISTER(test_cel_single_multiparty_bridge);
2109 #endif
2110
2111         AST_TEST_UNREGISTER(test_cel_dial_unanswered);
2112         AST_TEST_UNREGISTER(test_cel_dial_unanswered_filter);
2113         AST_TEST_UNREGISTER(test_cel_dial_congestion);
2114         AST_TEST_UNREGISTER(test_cel_dial_busy);
2115         AST_TEST_UNREGISTER(test_cel_dial_unavailable);
2116         AST_TEST_UNREGISTER(test_cel_dial_caller_cancel);
2117         AST_TEST_UNREGISTER(test_cel_dial_parallel_failed);
2118         AST_TEST_UNREGISTER(test_cel_dial_answer_no_bridge);
2119         AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_a);
2120         AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_b);
2121 #ifdef RACEY_TESTS
2122         AST_TEST_UNREGISTER(test_cel_dial_answer_multiparty);
2123         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_swap);
2124 #endif
2125
2126         AST_TEST_UNREGISTER(test_cel_blind_transfer);
2127         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_merge);
2128         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_link);
2129
2130         AST_TEST_UNREGISTER(test_cel_dial_pickup);
2131
2132         AST_TEST_UNREGISTER(test_cel_local_optimize);
2133
2134         ast_channel_unregister(&test_cel_chan_tech);
2135
2136         ao2_cleanup(cel_test_config);
2137         cel_test_config = NULL;
2138
2139         return 0;
2140 }
2141
2142 static int load_module(void)
2143 {
2144         /* build the test config */
2145         cel_test_config = ast_cel_general_config_alloc();
2146         if (!cel_test_config) {
2147                 return -1;
2148         }
2149         cel_test_config->enable = 1;
2150         if (ast_str_container_add(cel_test_config->apps, "dial")) {
2151                 return -1;
2152         }
2153         if (ast_str_container_add(cel_test_config->apps, "park")) {
2154                 return -1;
2155         }
2156         if (ast_str_container_add(cel_test_config->apps, "queue")) {
2157                 return -1;
2158         }
2159         cel_test_config->events |= 1<<AST_CEL_APP_START;
2160         cel_test_config->events |= 1<<AST_CEL_CHANNEL_START;
2161         cel_test_config->events |= 1<<AST_CEL_CHANNEL_END;
2162         cel_test_config->events |= 1<<AST_CEL_ANSWER;
2163         cel_test_config->events |= 1<<AST_CEL_HANGUP;
2164         cel_test_config->events |= 1<<AST_CEL_BRIDGE_ENTER;
2165         cel_test_config->events |= 1<<AST_CEL_BRIDGE_EXIT;
2166         cel_test_config->events |= 1<<AST_CEL_BLINDTRANSFER;
2167         cel_test_config->events |= 1<<AST_CEL_ATTENDEDTRANSFER;
2168         cel_test_config->events |= 1<<AST_CEL_PICKUP;
2169         cel_test_config->events |= 1<<AST_CEL_LOCAL_OPTIMIZE;
2170
2171         ast_channel_register(&test_cel_chan_tech);
2172
2173         AST_TEST_REGISTER(test_cel_channel_creation);
2174         AST_TEST_REGISTER(test_cel_unanswered_inbound_call);
2175         AST_TEST_REGISTER(test_cel_unanswered_outbound_call);
2176
2177         AST_TEST_REGISTER(test_cel_single_party);
2178         AST_TEST_REGISTER(test_cel_single_bridge);
2179         AST_TEST_REGISTER(test_cel_single_bridge_continue);
2180         AST_TEST_REGISTER(test_cel_single_twoparty_bridge_a);
2181         AST_TEST_REGISTER(test_cel_single_twoparty_bridge_b);
2182 #ifdef RACEY_TESTS
2183         AST_TEST_REGISTER(test_cel_single_multiparty_bridge);
2184 #endif
2185
2186         AST_TEST_REGISTER(test_cel_dial_unanswered);
2187         AST_TEST_REGISTER(test_cel_dial_unanswered_filter);
2188         AST_TEST_REGISTER(test_cel_dial_congestion);
2189         AST_TEST_REGISTER(test_cel_dial_busy);
2190         AST_TEST_REGISTER(test_cel_dial_unavailable);
2191         AST_TEST_REGISTER(test_cel_dial_caller_cancel);
2192         AST_TEST_REGISTER(test_cel_dial_parallel_failed);
2193         AST_TEST_REGISTER(test_cel_dial_answer_no_bridge);
2194         AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_a);
2195         AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_b);
2196 #ifdef RACEY_TESTS
2197         AST_TEST_REGISTER(test_cel_dial_answer_multiparty);
2198         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_swap);
2199 #endif
2200
2201         AST_TEST_REGISTER(test_cel_blind_transfer);
2202         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_merge);
2203         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_link);
2204
2205         AST_TEST_REGISTER(test_cel_dial_pickup);
2206
2207         AST_TEST_REGISTER(test_cel_local_optimize);
2208
2209         /* ast_test_register_* has to happen after AST_TEST_REGISTER */
2210         /* Verify received vs expected events and clean things up after every test */
2211         ast_test_register_init(TEST_CATEGORY, test_cel_init_cb);
2212         ast_test_register_cleanup(TEST_CATEGORY, cel_verify_and_cleanup_cb);
2213
2214         return AST_MODULE_LOAD_SUCCESS;
2215 }
2216
2217 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CEL unit tests");