CI: Add https credentials to gerrit checkouts
[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 #include <math.h>
35 #include "asterisk/module.h"
36 #include "asterisk/test.h"
37 #include "asterisk/cel.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/format_cache.h"
40 #include "asterisk/linkedlists.h"
41 #include "asterisk/chanvars.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/causes.h"
44 #include "asterisk/time.h"
45 #include "asterisk/bridge.h"
46 #include "asterisk/bridge_basic.h"
47 #include "asterisk/pickup.h"
48 #include "asterisk/stasis_channels.h"
49 #include "asterisk/stasis_bridges.h"
50 #include "asterisk/json.h"
51 #include "asterisk/features.h"
52 #include "asterisk/core_local.h"
53
54 #define TEST_CATEGORY "/main/cel/"
55
56 #define CHANNEL_TECH_NAME "CELTestChannel"
57
58 #define TEST_BACKEND_NAME "CEL Test Logging"
59
60 /*! \brief A placeholder for Asterisk's 'real' CEL configuration */
61 static struct ast_cel_general_config *saved_config;
62
63 /*! \brief The CEL config used for CEL unit tests */
64 static struct ast_cel_general_config *cel_test_config;
65
66 /*! \brief Lock used for synchronizing test execution stages with received events */
67 ast_mutex_t mid_test_sync_lock;
68
69 /*! \brief Lock used with sync_out for checking the end of test execution */
70 ast_mutex_t sync_lock;
71
72 /*! \brief Condition used for checking the end of test execution */
73 ast_cond_t sync_out;
74
75 /*! \brief Flag used to trigger a mid-test synchronization, access controlled by mid_test_sync_lock */
76 int do_mid_test_sync = 0;
77
78 /*! \brief A channel technology used for the unit tests */
79 static struct ast_channel_tech test_cel_chan_tech = {
80         .type = CHANNEL_TECH_NAME,
81         .description = "Mock channel technology for CEL tests",
82 };
83
84 /*! \brief A 1 second sleep */
85 static struct timespec to_sleep = {1, 0};
86
87 static void do_sleep(void)
88 {
89         while ((nanosleep(&to_sleep, &to_sleep) == -1) && (errno == EINTR)) {
90         }
91 }
92
93 #define APPEND_EVENT(chan, ev_type, userevent, extra) do { \
94         if (append_expected_event(chan, ev_type, userevent, extra, NULL)) { \
95                 return AST_TEST_FAIL; \
96         } \
97         } while (0)
98
99 #define APPEND_EVENT_PEER(chan, ev_type, userevent, extra, peer) do { \
100         if (append_expected_event(chan, ev_type, userevent, extra, peer)) { \
101                 return AST_TEST_FAIL; \
102         } \
103         } while (0)
104
105 #define APPEND_EVENT_SNAPSHOT(snapshot, ev_type, userevent, extra, peer) do { \
106         if (append_expected_event_snapshot(snapshot, ev_type, userevent, extra, peer)) { \
107                 return AST_TEST_FAIL; \
108         } \
109         } while (0)
110
111 #define APPEND_DUMMY_EVENT() do { \
112         if (append_dummy_event()) { \
113                 return AST_TEST_FAIL; \
114         } \
115         } while (0)
116
117 #define BRIDGE_EXIT(channel, bridge) do { \
118         ast_test_validate(test, !ast_bridge_depart(channel)); \
119         BRIDGE_EXIT_EVENT(channel, bridge); \
120         mid_test_sync(); \
121         } while (0)
122
123 #define BRIDGE_EXIT_EVENT(channel, bridge) do { \
124         RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
125         peer_str = test_cel_generate_peer_str(channel, bridge); \
126         ast_test_validate(test, peer_str != NULL); \
127         BRIDGE_EXIT_EVENT_PEER(channel, bridge, ast_str_buffer(peer_str)); \
128         } while (0)
129
130 #define BRIDGE_EXIT_EVENT_PEER(channel, bridge, peer) do { \
131         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
132         extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
133         ast_test_validate(test, extra != NULL); \
134         APPEND_EVENT_PEER(channel, AST_CEL_BRIDGE_EXIT, NULL, extra, peer); \
135         } while (0)
136
137 #define BRIDGE_EXIT_SNAPSHOT(channel, bridge) do { \
138         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
139         RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
140         peer_str = test_cel_generate_peer_str_snapshot(channel, bridge); \
141         ast_test_validate(test, peer_str != NULL); \
142         extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
143         ast_test_validate(test, extra != NULL); \
144         APPEND_EVENT_SNAPSHOT(channel, AST_CEL_BRIDGE_EXIT, NULL, extra, ast_str_buffer(peer_str)); \
145         } while (0)
146
147 #define BRIDGE_ENTER(channel, bridge) do { \
148         ast_test_validate(test, !ast_bridge_impart(bridge, channel, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE)); \
149         do_sleep(); \
150         BRIDGE_ENTER_EVENT(channel, bridge); \
151         mid_test_sync(); \
152         } while (0)
153
154 #define BRIDGE_ENTER_EVENT(channel, bridge) do { \
155         RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
156         peer_str = test_cel_generate_peer_str(channel, bridge); \
157         ast_test_validate(test, peer_str != NULL); \
158         BRIDGE_ENTER_EVENT_PEER(channel, bridge, ast_str_buffer(peer_str)); \
159         } while (0)
160
161 #define BRIDGE_ENTER_EVENT_PEER(channel, bridge, peer) do { \
162         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
163         extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
164         ast_test_validate(test, extra != NULL); \
165         APPEND_EVENT_PEER(channel, AST_CEL_BRIDGE_ENTER, NULL, extra, peer); \
166         } while (0)
167
168 #define BLINDTRANSFER_EVENT(channel, bridge, extension, context) do { \
169         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
170         extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}", \
171                 "extension", extension, \
172                 "context", context, \
173                 "bridge_id", bridge->uniqueid, \
174                 "transferee_channel_name", "N/A", \
175                 "transferee_channel_uniqueid", "N/A"); \
176         ast_test_validate(test, extra != NULL); \
177         APPEND_EVENT(channel, AST_CEL_BLINDTRANSFER, NULL, extra); \
178         } while (0)
179
180 #define ATTENDEDTRANSFER_BRIDGE(channel1, bridge1, channel2, bridge2, channel3, channel4) do { \
181         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
182         extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}", \
183                 "bridge1_id", bridge1->uniqueid, \
184                 "channel2_name", ast_channel_name(channel2), \
185                 "channel2_uniqueid", ast_channel_uniqueid(channel2), \
186                 "bridge2_id", bridge2->uniqueid, \
187                 "transferee_channel_name", ast_channel_name(channel4), \
188                 "transferee_channel_uniqueid", ast_channel_uniqueid(channel4), \
189                 "transfer_target_channel_name", ast_channel_name(channel3), \
190                 "transfer_target_channel_uniqueid", ast_channel_uniqueid(channel3)); \
191         ast_test_validate(test, extra != NULL); \
192         APPEND_EVENT(channel1, AST_CEL_ATTENDEDTRANSFER, NULL, extra); \
193         } while (0)
194
195 /*! \brief Alice's Caller ID */
196 #define ALICE_CALLERID { .id.name.str = "Alice", .id.name.valid = 1, .id.number.str = "100", .id.number.valid = 1, }
197
198 /*! \brief Bob's Caller ID */
199 #define BOB_CALLERID { .id.name.str = "Bob", .id.name.valid = 1, .id.number.str = "200", .id.number.valid = 1, }
200
201 /*! \brief Charlie's Caller ID */
202 #define CHARLIE_CALLERID { .id.name.str = "Charlie", .id.name.valid = 1, .id.number.str = "300", .id.number.valid = 1, }
203
204 /*! \brief David's Caller ID */
205 #define DAVID_CALLERID { .id.name.str = "David", .id.name.valid = 1, .id.number.str = "400", .id.number.valid = 1, }
206
207 /*! \brief Set ulaw format on channel */
208 #define SET_FORMATS(chan) do {\
209         struct ast_format_cap *caps;\
210         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);\
211         ast_format_cap_append(caps, ast_format_ulaw, 0);\
212         ast_channel_nativeformats_set((chan), caps);\
213         ast_channel_set_writeformat((chan), ast_format_ulaw);\
214         ast_channel_set_rawwriteformat((chan), ast_format_ulaw);\
215         ast_channel_set_readformat((chan), ast_format_ulaw);\
216         ast_channel_set_rawreadformat((chan), ast_format_ulaw);\
217         ao2_ref(caps, -1);\
218 } while (0)
219
220 /*! \brief Create a \ref test_cel_chan_tech for Alice. */
221 #define CREATE_ALICE_CHANNEL(channel_var, caller_id) do { \
222         (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"); \
223         SET_FORMATS((channel_var));\
224         APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
225         ast_channel_unlock((channel_var)); \
226         } while (0)
227
228 /*! \brief Create a \ref test_cel_chan_tech for Bob. */
229 #define CREATE_BOB_CHANNEL(channel_var, caller_id) do { \
230         (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"); \
231         SET_FORMATS((channel_var));\
232         APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
233         ast_channel_unlock((channel_var)); \
234         } while (0)
235
236 /*! \brief Create a \ref test_cel_chan_tech for Charlie. */
237 #define CREATE_CHARLIE_CHANNEL(channel_var, caller_id) do { \
238         (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"); \
239         SET_FORMATS((channel_var));\
240         APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
241         ast_channel_unlock((channel_var)); \
242         } while (0)
243
244 /*! \brief Create a \ref test_cel_chan_tech for David. */
245 #define CREATE_DAVID_CHANNEL(channel_var, caller_id) do { \
246         (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"); \
247         SET_FORMATS((channel_var));\
248         APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
249         ast_channel_unlock((channel_var)); \
250         } while (0)
251
252 /*! \brief Emulate a channel entering into an application */
253 #define EMULATE_APP_DATA(channel, priority, application, data) do { \
254         if ((priority) > 0) { \
255                 ast_channel_priority_set((channel), (priority)); \
256         } \
257         ast_channel_appl_set((channel), (application)); \
258         ast_channel_data_set((channel), (data)); \
259         ast_channel_publish_snapshot((channel)); \
260         } while (0)
261
262 #define ANSWER_CHANNEL(chan) do { \
263         EMULATE_APP_DATA(chan, 1, "Answer", ""); \
264         ANSWER_NO_APP(chan); \
265         } while (0)
266
267 #define ANSWER_NO_APP(chan) do { \
268         ast_setstate(chan, AST_STATE_UP); \
269         APPEND_EVENT(chan, AST_CEL_ANSWER, NULL, NULL); \
270         } while (0)
271
272 /*! \brief Hang up a test channel safely */
273 #define HANGUP_CHANNEL(channel, cause, dialstatus) do { \
274         ast_channel_hangupcause_set((channel), (cause)); \
275         ao2_ref(channel, +1); \
276         ast_hangup((channel)); \
277         HANGUP_EVENT(channel, cause, dialstatus); \
278         APPEND_EVENT(channel, AST_CEL_CHANNEL_END, NULL, NULL); \
279         ao2_cleanup(stasis_cache_get(ast_channel_cache(), \
280                 ast_channel_snapshot_type(), ast_channel_uniqueid(channel))); \
281         ao2_cleanup(channel); \
282         channel = NULL; \
283         } while (0)
284
285 #define HANGUP_EVENT(channel, cause, dialstatus) do { \
286         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
287         extra = ast_json_pack("{s: i, s: s, s: s}", \
288                 "hangupcause", cause, \
289                 "hangupsource", "", \
290                 "dialstatus", dialstatus); \
291         ast_test_validate(test, extra != NULL); \
292         APPEND_EVENT(channel, AST_CEL_HANGUP, NULL, extra); \
293         } while (0)
294
295 static void mid_test_sync(void);
296
297 static int append_expected_event(
298         struct ast_channel *chan,
299         enum ast_cel_event_type type,
300         const char *userdefevname,
301         struct ast_json *extra,
302         const char *peer);
303
304 static int append_expected_event_snapshot(
305         struct ast_channel_snapshot *snapshot,
306         enum ast_cel_event_type type,
307         const char *userdefevname,
308         struct ast_json *extra,
309         const char *peer);
310
311 #ifdef RACEY_TESTS
312 static int append_dummy_event(void);
313 #endif
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.";
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.";
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";
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.";
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.";
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.";
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.";
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.";
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";
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";
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";
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";
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";
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";
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.";
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.";
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";
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";
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.";
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.";
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.";
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.";
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 /* XXX Validation needs to take into account the BRIDGE_EXIT for David and the
1473  * ATTENDEDTRANSFER message are not guaranteed to be ordered
1474  */
1475 #ifdef RACEY_TESTS
1476 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
1477 {
1478         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1479         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1480         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1481         RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
1482         RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
1483         RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
1484         struct ast_party_caller alice_caller = ALICE_CALLERID;
1485         struct ast_party_caller bob_caller = BOB_CALLERID;
1486         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1487         struct ast_party_caller david_caller = ALICE_CALLERID;
1488
1489         switch (cmd) {
1490         case TEST_INIT:
1491                 info->name = __func__;
1492                 info->category = TEST_CATEGORY;
1493                 info->summary = "Test attended transfers between two pairs of"
1494                         " bridged parties that results in a bridge merge";
1495                 info->description =
1496                         "This test creates four channels, places each pair"
1497                         " in a bridge, and then attended transfers the bridges"
1498                         " together causing a bridge link.";
1499                 return AST_TEST_NOT_RUN;
1500         case TEST_EXECUTE:
1501                 break;
1502         }
1503         /* Create first set of bridged parties */
1504         bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1505                 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1506                 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1507                 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
1508                 "test_cel", "test_cel_atxfer_bridges_link_1", NULL);
1509         ast_test_validate(test, bridge1 != NULL);
1510
1511         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1512         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1513         ANSWER_NO_APP(chan_alice);
1514         ANSWER_NO_APP(chan_bob);
1515
1516         BRIDGE_ENTER(chan_bob, bridge1);
1517         BRIDGE_ENTER(chan_alice, bridge1);
1518
1519         /* Create second set of bridged parties */
1520         bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
1521                 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
1522                 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
1523                 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
1524                 "test_cel", "test_cel_atxfer_bridges_link_2", NULL);
1525         ast_test_validate(test, bridge2 != NULL);
1526
1527         CREATE_DAVID_CHANNEL(chan_david, &david_caller);
1528         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1529         ANSWER_NO_APP(chan_david);
1530         ANSWER_NO_APP(chan_charlie);
1531
1532         BRIDGE_ENTER(chan_charlie, bridge2);
1533         BRIDGE_ENTER(chan_david, bridge2);
1534
1535         /* Perform attended transfer */
1536         ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
1537
1538         ast_bridge_transfer_attended(chan_alice, chan_david);
1539         do_sleep();
1540
1541         /* BRIDGE_EXIT alice and david */
1542         APPEND_DUMMY_EVENT();
1543         APPEND_DUMMY_EVENT();
1544
1545         do_sleep();
1546         BRIDGE_EXIT(chan_bob, bridge1);
1547         BRIDGE_EXIT(chan_charlie, bridge2);
1548
1549         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1550         do_sleep();
1551         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1552         do_sleep();
1553         HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
1554         do_sleep();
1555         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1556         do_sleep();
1557
1558         return AST_TEST_PASS;
1559 }
1560 #endif
1561
1562 AST_TEST_DEFINE(test_cel_dial_pickup)
1563 {
1564         RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
1565         RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
1566         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
1567         struct ast_party_caller caller = ALICE_CALLERID;
1568         struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
1569
1570         switch (cmd) {
1571         case TEST_INIT:
1572                 info->name = __func__;
1573                 info->category = TEST_CATEGORY;
1574                 info->summary = "Test call pickup";
1575                 info->description =
1576                         "Test CEL records for a call that is\n"
1577                         "inbound to Asterisk, executes some dialplan, and\n"
1578                         "is picked up.";
1579                 return AST_TEST_NOT_RUN;
1580         case TEST_EXECUTE:
1581                 break;
1582         }
1583
1584         CREATE_ALICE_CHANNEL(chan_caller, &caller);
1585
1586         EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
1587
1588         START_DIALED(chan_caller, chan_callee);
1589
1590         ast_channel_state_set(chan_caller, AST_STATE_RINGING);
1591
1592         CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
1593
1594         {
1595                 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1596                 SCOPED_CHANNELLOCK(lock, chan_callee);
1597
1598                 extra = ast_json_pack("{s: s, s: s}", "pickup_channel", ast_channel_name(chan_charlie),
1599                         "pickup_channel_uniqueid", ast_channel_uniqueid(chan_charlie));
1600                 ast_test_validate(test, extra != NULL);
1601
1602                 APPEND_EVENT(chan_callee, AST_CEL_PICKUP, NULL, extra);
1603                 ast_test_validate(test, !ast_do_pickup(chan_charlie, chan_callee));
1604         }
1605
1606         /* Hang up the masqueraded zombie */
1607         HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
1608
1609         ast_channel_publish_dial(chan_caller, chan_callee, NULL, "ANSWER");
1610
1611         HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
1612         HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
1613
1614         return AST_TEST_PASS;
1615 }
1616
1617 AST_TEST_DEFINE(test_cel_local_optimize)
1618 {
1619         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
1620         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
1621         struct ast_party_caller alice_caller = ALICE_CALLERID;
1622         struct ast_party_caller bob_caller = BOB_CALLERID;
1623         RAII_VAR(struct ast_multi_channel_blob *, mc_blob, NULL, ao2_cleanup);
1624         RAII_VAR(struct ast_channel_snapshot *, alice_snapshot, NULL, ao2_cleanup);
1625         RAII_VAR(struct ast_channel_snapshot *, bob_snapshot, NULL, ao2_cleanup);
1626         RAII_VAR(struct stasis_message *, local_opt_begin, NULL, ao2_cleanup);
1627         RAII_VAR(struct stasis_message *, local_opt_end, NULL, ao2_cleanup);
1628         RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1629
1630         switch (cmd) {
1631         case TEST_INIT:
1632                 info->name = __func__;
1633                 info->category = TEST_CATEGORY;
1634                 info->summary = "Test local channel optimization record generation";
1635                 info->description =
1636                         "Test CEL records for two local channels being optimized\n"
1637                         "out by sending a messages indicating local optimization\n"
1638                         "begin and end";
1639                 return AST_TEST_NOT_RUN;
1640         case TEST_EXECUTE:
1641                 break;
1642         }
1643
1644         mc_blob = ast_multi_channel_blob_create(ast_json_null());
1645         ast_test_validate(test, mc_blob != NULL);
1646
1647         CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
1648         CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
1649
1650         ast_channel_lock(chan_alice);
1651         alice_snapshot = ast_channel_snapshot_create(chan_alice);
1652         ast_channel_unlock(chan_alice);
1653         ast_test_validate(test, alice_snapshot != NULL);
1654
1655         ast_channel_lock(chan_bob);
1656         bob_snapshot = ast_channel_snapshot_create(chan_bob);
1657         ast_channel_unlock(chan_bob);
1658         ast_test_validate(test, bob_snapshot != NULL);
1659
1660         ast_multi_channel_blob_add_channel(mc_blob, "1", alice_snapshot);
1661         ast_multi_channel_blob_add_channel(mc_blob, "2", bob_snapshot);
1662
1663         local_opt_begin = stasis_message_create(ast_local_optimization_begin_type(), mc_blob);
1664         ast_test_validate(test, local_opt_begin != NULL);
1665
1666         local_opt_end = stasis_message_create(ast_local_optimization_end_type(), mc_blob);
1667         ast_test_validate(test, local_opt_end != NULL);
1668
1669         stasis_publish(ast_channel_topic(chan_alice), local_opt_begin);
1670         stasis_publish(ast_channel_topic(chan_alice), local_opt_end);
1671
1672         extra = ast_json_pack("{s: s, s: s}", "local_two", bob_snapshot->name,
1673                 "local_two_uniqueid", bob_snapshot->uniqueid);
1674         ast_test_validate(test, extra != NULL);
1675
1676         APPEND_EVENT_SNAPSHOT(alice_snapshot, AST_CEL_LOCAL_OPTIMIZE, NULL, extra, NULL);
1677
1678         HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
1679         HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
1680
1681         return AST_TEST_PASS;
1682 }
1683
1684 /*! Container for astobj2 duplicated ast_events */
1685 static struct ao2_container *cel_received_events = NULL;
1686
1687 /*! Container for expected CEL events */
1688 static struct ao2_container *cel_expected_events = NULL;
1689
1690 static struct ast_event *ao2_dup_event(const struct ast_event *event)
1691 {
1692         struct ast_event *event_dup;
1693         uint16_t event_len;
1694
1695         event_len = ast_event_get_size(event);
1696
1697         event_dup = ao2_alloc(event_len, NULL);
1698         if (!event_dup) {
1699                 return NULL;
1700         }
1701
1702         memcpy(event_dup, event, event_len);
1703
1704         return event_dup;
1705 }
1706
1707 static void mid_test_sync(void)
1708 {
1709         ast_mutex_lock(&mid_test_sync_lock);
1710         if (ao2_container_count(cel_expected_events) <= ao2_container_count(cel_received_events)) {
1711                 ast_mutex_unlock(&mid_test_sync_lock);
1712                 return;
1713         }
1714
1715         do_mid_test_sync = 1;
1716         ast_mutex_unlock(&mid_test_sync_lock);
1717
1718         {
1719                 struct timeval start = ast_tvnow();
1720                 struct timespec end = {
1721                         .tv_sec = start.tv_sec + 15,
1722                         .tv_nsec = start.tv_usec * 1000
1723                 };
1724
1725                 SCOPED_MUTEX(lock, &sync_lock);
1726                 ast_cond_timedwait(&sync_out, &sync_lock, &end);
1727         }
1728 }
1729
1730 static int append_event(struct ast_event *ev)
1731 {
1732         RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1733         ao2_ev = ao2_dup_event(ev);
1734         if (!ao2_ev) {
1735                 return -1;
1736         }
1737
1738         ao2_link(cel_expected_events, ao2_ev);
1739         return 0;
1740 }
1741
1742 #ifdef RACEY_TESTS
1743 static int append_dummy_event(void)
1744 {
1745         RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1746         RAII_VAR(struct ast_event *, ao2_ev, NULL, ao2_cleanup);
1747
1748         ev = ast_event_new(AST_EVENT_CUSTOM, AST_EVENT_IE_END);
1749         if (!ev) {
1750                 return -1;
1751         }
1752
1753         return append_event(ev);
1754 }
1755 #endif
1756
1757 static int append_expected_event_snapshot(
1758         struct ast_channel_snapshot *snapshot,
1759         enum ast_cel_event_type type,
1760         const char *userdefevname,
1761         struct ast_json *extra,
1762         const char *peer)
1763 {
1764         RAII_VAR(struct ast_event *, ev, NULL, ast_free);
1765         ev = ast_cel_create_event(snapshot, type, userdefevname, extra, peer);
1766         if (!ev) {
1767                 return -1;
1768         }
1769
1770         return append_event(ev);
1771 }
1772
1773 static int append_expected_event(
1774         struct ast_channel *chan,
1775         enum ast_cel_event_type type,
1776         const char *userdefevname,
1777         struct ast_json *extra,
1778         const char *peer)
1779 {
1780         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1781         ast_channel_lock(chan);
1782         snapshot = ast_channel_snapshot_create(chan);
1783         ast_channel_unlock(chan);
1784         if (!snapshot) {
1785                 return -1;
1786         }
1787
1788         return append_expected_event_snapshot(snapshot, type, userdefevname, extra, peer);
1789 }
1790
1791 static void test_sub(struct ast_event *event)
1792 {
1793         struct ast_event *event_dup = ao2_dup_event(event);
1794         const char *chan_name;
1795         SCOPED_MUTEX(mid_test_lock, &mid_test_sync_lock);
1796
1797         if (!event_dup) {
1798                 return;
1799         }
1800
1801         chan_name = ast_event_get_ie_str(event_dup, AST_EVENT_IE_CEL_CHANNAME);
1802         if (chan_name && strncmp(chan_name, CHANNEL_TECH_NAME, 14)) {
1803                 return;
1804         }
1805
1806         /* save the event for later processing */
1807         ao2_link(cel_received_events, event_dup);
1808
1809         if (do_mid_test_sync) {
1810                 int expected = ao2_container_count(cel_expected_events);
1811                 int received = ao2_container_count(cel_received_events);
1812                 if (expected <= received) {
1813                         {
1814                         SCOPED_MUTEX(lock, &sync_lock);
1815                         ast_cond_signal(&sync_out);
1816                         do_mid_test_sync = 0;
1817                         }
1818                 }
1819         }
1820 }
1821
1822 /*!
1823  * \internal
1824  * \brief Callback function called before each test executes
1825  */
1826 static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
1827 {
1828         ast_assert(cel_received_events == NULL);
1829         ast_assert(cel_expected_events == NULL);
1830
1831         ast_mutex_init(&mid_test_sync_lock);
1832         ast_mutex_init(&sync_lock);
1833         ast_cond_init(&sync_out, NULL);
1834
1835         /* Back up the real CEL config and insert the test's config */
1836         saved_config = ast_cel_get_config();
1837         ast_cel_set_config(cel_test_config);
1838
1839         /* init CEL event storage (degenerate hash table becomes a linked list) */
1840         cel_received_events = ao2_container_alloc(1, NULL, NULL);
1841         cel_expected_events = ao2_container_alloc(1, NULL, NULL);
1842
1843         /* start the CEL event callback */
1844         if (ast_cel_backend_register(TEST_BACKEND_NAME, test_sub)) {
1845                 return -1;
1846         }
1847         return 0;
1848 }
1849
1850 /*!
1851  * \brief Check two peer strings for equality
1852  *
1853  * \retval zero if the peer strings do not match
1854  * \retval non-zero if the peer strings match
1855  */
1856 static int test_cel_peer_strings_match(const char *str1, const char *str2)
1857 {
1858         struct ao2_container *intersection = ast_str_container_alloc(11);
1859         RAII_VAR(char *, str1_dup, ast_strdup(str1), ast_free);
1860         RAII_VAR(char *, str2_dup, ast_strdup(str2), ast_free);
1861         char *chan;
1862
1863         while ((chan = strsep(&str1_dup, ","))) {
1864                 ast_str_container_add(intersection, chan);
1865         }
1866
1867         while ((chan = strsep(&str2_dup, ","))) {
1868                 RAII_VAR(char *, ao2_chan, ao2_find(intersection, chan, OBJ_SEARCH_KEY), ao2_cleanup);
1869
1870                 /* item in str2 not in str1 */
1871                 if (!ao2_chan) {
1872                         return 0;
1873                 }
1874
1875                 ast_str_container_remove(intersection, chan);
1876         }
1877
1878         /* item in str1 not in str2 */
1879         if (ao2_container_count(intersection)) {
1880                 return 0;
1881         }
1882
1883         return 1;
1884 }
1885
1886 /*!
1887  * \brief Check an IE value from two events
1888  *
1889  * \retval zero if the IEs in the events of the specified type do not match
1890  * \retval non-zero if the IEs in the events of the specified type match
1891  */
1892 static int match_ie_val(
1893         const struct ast_event *event1,
1894         const struct ast_event *event2,
1895         enum ast_event_ie_type type)
1896 {
1897         enum ast_event_ie_pltype pltype = ast_event_get_ie_pltype(type);
1898
1899         /* XXX ignore sec/usec for now */
1900         if (type == AST_EVENT_IE_CEL_EVENT_TIME_USEC) {
1901                 return 1;
1902         }
1903
1904         if (type == AST_EVENT_IE_CEL_EVENT_TIME) {
1905                 return 1;
1906         }
1907
1908         switch (pltype) {
1909         case AST_EVENT_IE_PLTYPE_UINT:
1910         {
1911                 uint32_t val = ast_event_get_ie_uint(event2, type);
1912
1913                 return (val == ast_event_get_ie_uint(event1, type)) ? 1 : 0;
1914         }
1915         case AST_EVENT_IE_PLTYPE_STR:
1916         {
1917                 const char *str1 = ast_event_get_ie_str(event1, type);
1918                 const char *str2 = ast_event_get_ie_str(event2, type);
1919
1920                 if (!str1 && !str2) {
1921                         return 1;
1922                 } else if (!str1) {
1923                         return 0;
1924                 } else if (!str2) {
1925                         return 0;
1926                 }
1927
1928                 /* use special matching for CEL PEER field */
1929                 if (type == AST_EVENT_IE_CEL_PEER) {
1930                         return test_cel_peer_strings_match(str1, str2);
1931                 }
1932
1933                 return !strcmp(str1, str2);
1934         }
1935         case AST_EVENT_IE_PLTYPE_RAW:
1936         case AST_EVENT_IE_PLTYPE_BITFLAGS:
1937                 /* Fall through: just pass on these types */
1938                 return 1;
1939         default:
1940                 break;
1941         }
1942         return 0;
1943 }
1944
1945 static int events_are_equal(struct ast_test *test, struct ast_event *received, struct ast_event *expected)
1946 {
1947         struct ast_event_iterator iterator;
1948         int res;
1949
1950         if (ast_event_get_type(expected) == AST_EVENT_CUSTOM) {
1951                 /* this event is flagged as a wildcard match */
1952                 return 1;
1953         }
1954
1955         for (res = ast_event_iterator_init(&iterator, received); !res; res = ast_event_iterator_next(&iterator)) {
1956                 int ie_type = ast_event_iterator_get_ie_type(&iterator);
1957                 if (!match_ie_val(received, expected, ie_type)) {
1958                         ast_test_status_update(test, "Failed matching on field %s\n", ast_event_get_ie_type_name(ie_type));
1959                         return 0;
1960                 }
1961         }
1962
1963         return 1;
1964 }
1965
1966 static int dump_event(struct ast_test *test, struct ast_event *event)
1967 {
1968         struct ast_event_iterator i;
1969
1970         if (ast_event_iterator_init(&i, event)) {
1971                 ast_test_status_update(test, "Failed to initialize event iterator.  :-(\n");
1972                 return 0;
1973         }
1974
1975         ast_test_status_update(test, "Event: %s\n",
1976                 ast_cel_get_type_name(ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TYPE)));
1977
1978         do {
1979                 enum ast_event_ie_type ie_type;
1980                 enum ast_event_ie_pltype ie_pltype;
1981                 const char *ie_type_name;
1982
1983                 ie_type = ast_event_iterator_get_ie_type(&i);
1984                 ie_type_name = ast_event_get_ie_type_name(ie_type);
1985                 ie_pltype = ast_event_get_ie_pltype(ie_type);
1986
1987                 switch (ie_pltype) {
1988                 case AST_EVENT_IE_PLTYPE_UNKNOWN:
1989                 case AST_EVENT_IE_PLTYPE_STR:
1990                         ast_test_status_update(test, "%.30s: %s\n", ie_type_name,
1991                                         ast_event_iterator_get_ie_str(&i));
1992                         break;
1993                 case AST_EVENT_IE_PLTYPE_UINT:
1994                         ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
1995                                         ast_event_iterator_get_ie_uint(&i));
1996                         break;
1997                 default:
1998                         break;
1999                 }
2000         } while (!ast_event_iterator_next(&i));
2001
2002         ast_test_status_update(test, "\n");
2003
2004         return 0;
2005 }
2006
2007 static int check_events(struct ast_test *test, struct ao2_container *local_expected, struct ao2_container *local_received)
2008 {
2009         struct ao2_iterator received_it;
2010         struct ao2_iterator expected_it;
2011         RAII_VAR(struct ast_event *, rx_event, NULL, ao2_cleanup);
2012         RAII_VAR(struct ast_event *, ex_event, NULL, ao2_cleanup);
2013         int debug = 0;
2014
2015         if (ao2_container_count(local_expected) != ao2_container_count(local_received)) {
2016                 ast_test_status_update(test, "Increasing verbosity since the number of expected events (%d)"
2017                         " did not match number of received events (%d).\n",
2018                         ao2_container_count(local_expected),
2019                         ao2_container_count(local_received));
2020                 debug = 1;
2021         }
2022
2023         received_it = ao2_iterator_init(local_received, 0);
2024         expected_it = ao2_iterator_init(local_expected, 0);
2025         rx_event = ao2_iterator_next(&received_it);
2026         ex_event = ao2_iterator_next(&expected_it);
2027         while (rx_event && ex_event) {
2028                 if (!events_are_equal(test, rx_event, ex_event)) {
2029                         ao2_iterator_destroy(&received_it);
2030                         ao2_iterator_destroy(&expected_it);
2031                         ast_test_status_update(test, "Received event:\n");
2032                         dump_event(test, rx_event);
2033                         ast_test_status_update(test, "Expected event:\n");
2034                         dump_event(test, ex_event);
2035                         return -1;
2036                 }
2037                 if (debug) {
2038                         ast_test_status_update(test, "Compared events successfully%s\n",
2039                                 ast_event_get_type(ex_event) == AST_EVENT_CUSTOM
2040                                         ? " (wildcard match)" : "");
2041                         dump_event(test, rx_event);
2042                 }
2043                 ao2_cleanup(rx_event);
2044                 ao2_cleanup(ex_event);
2045                 rx_event = ao2_iterator_next(&received_it);
2046                 ex_event = ao2_iterator_next(&expected_it);
2047         }
2048         ao2_iterator_destroy(&received_it);
2049         ao2_iterator_destroy(&expected_it);
2050
2051         if (rx_event) {
2052                 ast_test_status_update(test, "Received event:\n");
2053                 dump_event(test, rx_event);
2054                 return -1;
2055         }
2056         if (ex_event) {
2057                 ast_test_status_update(test, "Expected event:\n");
2058                 dump_event(test, ex_event);
2059                 return -1;
2060         }
2061         return 0;
2062 }
2063
2064 /*!
2065  * \internal
2066  * \brief Callback function called after each test executes.
2067  *
2068  * \details
2069  * In addition to cleanup, this function also performs verification
2070  * that the events received during a test match the events that were
2071  * expected to have been generated during the test.
2072  */
2073 static int cel_verify_and_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
2074 {
2075         RAII_VAR(struct ao2_container *, local_expected, cel_expected_events, ao2_cleanup);
2076         RAII_VAR(struct ao2_container *, local_received, cel_received_events, ao2_cleanup);
2077         ast_assert(cel_received_events != NULL);
2078         ast_assert(cel_expected_events != NULL);
2079
2080         do_sleep();
2081
2082         /* stop the CEL event callback and clean up storage structures*/
2083         ast_cel_backend_unregister(TEST_BACKEND_NAME);
2084
2085         /* cleaned up by RAII_VAR's */
2086         cel_expected_events = NULL;
2087         cel_received_events = NULL;
2088
2089         /* check events */
2090         ast_test_validate(test, !check_events(test, local_expected, local_received));
2091
2092         /* Restore the real CEL config */
2093         ast_cel_set_config(saved_config);
2094         ao2_cleanup(saved_config);
2095         saved_config = NULL;
2096
2097         /* clean up the locks */
2098         ast_mutex_destroy(&sync_lock);
2099         ast_mutex_destroy(&mid_test_sync_lock);
2100         ast_cond_destroy(&sync_out);
2101         return 0;
2102 }
2103
2104 static int unload_module(void)
2105 {
2106         AST_TEST_UNREGISTER(test_cel_channel_creation);
2107         AST_TEST_UNREGISTER(test_cel_unanswered_inbound_call);
2108         AST_TEST_UNREGISTER(test_cel_unanswered_outbound_call);
2109         AST_TEST_UNREGISTER(test_cel_single_party);
2110         AST_TEST_UNREGISTER(test_cel_single_bridge);
2111         AST_TEST_UNREGISTER(test_cel_single_bridge_continue);
2112         AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_a);
2113         AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_b);
2114 #ifdef RACEY_TESTS
2115         AST_TEST_UNREGISTER(test_cel_single_multiparty_bridge);
2116 #endif
2117
2118         AST_TEST_UNREGISTER(test_cel_dial_unanswered);
2119         AST_TEST_UNREGISTER(test_cel_dial_unanswered_filter);
2120         AST_TEST_UNREGISTER(test_cel_dial_congestion);
2121         AST_TEST_UNREGISTER(test_cel_dial_busy);
2122         AST_TEST_UNREGISTER(test_cel_dial_unavailable);
2123         AST_TEST_UNREGISTER(test_cel_dial_caller_cancel);
2124         AST_TEST_UNREGISTER(test_cel_dial_parallel_failed);
2125         AST_TEST_UNREGISTER(test_cel_dial_answer_no_bridge);
2126         AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_a);
2127         AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_b);
2128 #ifdef RACEY_TESTS
2129         AST_TEST_UNREGISTER(test_cel_dial_answer_multiparty);
2130         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_swap);
2131         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_link);
2132 #endif
2133
2134         AST_TEST_UNREGISTER(test_cel_blind_transfer);
2135         AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_merge);
2136
2137         AST_TEST_UNREGISTER(test_cel_dial_pickup);
2138
2139         AST_TEST_UNREGISTER(test_cel_local_optimize);
2140
2141         ast_channel_unregister(&test_cel_chan_tech);
2142
2143         ao2_cleanup(cel_test_config);
2144         cel_test_config = NULL;
2145
2146         return 0;
2147 }
2148
2149 static int load_module(void)
2150 {
2151         /* build the test config */
2152         cel_test_config = ast_cel_general_config_alloc();
2153         if (!cel_test_config) {
2154                 return -1;
2155         }
2156         cel_test_config->enable = 1;
2157         if (ast_str_container_add(cel_test_config->apps, "dial")) {
2158                 return -1;
2159         }
2160         if (ast_str_container_add(cel_test_config->apps, "park")) {
2161                 return -1;
2162         }
2163         if (ast_str_container_add(cel_test_config->apps, "queue")) {
2164                 return -1;
2165         }
2166         cel_test_config->events |= 1<<AST_CEL_APP_START;
2167         cel_test_config->events |= 1<<AST_CEL_CHANNEL_START;
2168         cel_test_config->events |= 1<<AST_CEL_CHANNEL_END;
2169         cel_test_config->events |= 1<<AST_CEL_ANSWER;
2170         cel_test_config->events |= 1<<AST_CEL_HANGUP;
2171         cel_test_config->events |= 1<<AST_CEL_BRIDGE_ENTER;
2172         cel_test_config->events |= 1<<AST_CEL_BRIDGE_EXIT;
2173         cel_test_config->events |= 1<<AST_CEL_BLINDTRANSFER;
2174         cel_test_config->events |= 1<<AST_CEL_ATTENDEDTRANSFER;
2175         cel_test_config->events |= 1<<AST_CEL_PICKUP;
2176         cel_test_config->events |= 1<<AST_CEL_LOCAL_OPTIMIZE;
2177
2178         ast_channel_register(&test_cel_chan_tech);
2179
2180         AST_TEST_REGISTER(test_cel_channel_creation);
2181         AST_TEST_REGISTER(test_cel_unanswered_inbound_call);
2182         AST_TEST_REGISTER(test_cel_unanswered_outbound_call);
2183
2184         AST_TEST_REGISTER(test_cel_single_party);
2185         AST_TEST_REGISTER(test_cel_single_bridge);
2186         AST_TEST_REGISTER(test_cel_single_bridge_continue);
2187         AST_TEST_REGISTER(test_cel_single_twoparty_bridge_a);
2188         AST_TEST_REGISTER(test_cel_single_twoparty_bridge_b);
2189 #ifdef RACEY_TESTS
2190         AST_TEST_REGISTER(test_cel_single_multiparty_bridge);
2191 #endif
2192
2193         AST_TEST_REGISTER(test_cel_dial_unanswered);
2194         AST_TEST_REGISTER(test_cel_dial_unanswered_filter);
2195         AST_TEST_REGISTER(test_cel_dial_congestion);
2196         AST_TEST_REGISTER(test_cel_dial_busy);
2197         AST_TEST_REGISTER(test_cel_dial_unavailable);
2198         AST_TEST_REGISTER(test_cel_dial_caller_cancel);
2199         AST_TEST_REGISTER(test_cel_dial_parallel_failed);
2200         AST_TEST_REGISTER(test_cel_dial_answer_no_bridge);
2201         AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_a);
2202         AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_b);
2203 #ifdef RACEY_TESTS
2204         AST_TEST_REGISTER(test_cel_dial_answer_multiparty);
2205         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_swap);
2206         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_link);
2207 #endif
2208
2209         AST_TEST_REGISTER(test_cel_blind_transfer);
2210         AST_TEST_REGISTER(test_cel_attended_transfer_bridges_merge);
2211
2212         AST_TEST_REGISTER(test_cel_dial_pickup);
2213
2214         AST_TEST_REGISTER(test_cel_local_optimize);
2215
2216         /* ast_test_register_* has to happen after AST_TEST_REGISTER */
2217         /* Verify received vs expected events and clean things up after every test */
2218         ast_test_register_init(TEST_CATEGORY, test_cel_init_cb);
2219         ast_test_register_cleanup(TEST_CATEGORY, cel_verify_and_cleanup_cb);
2220
2221         return AST_MODULE_LOAD_SUCCESS;
2222 }
2223
2224 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CEL unit tests");